Skocz do zawartości

AI Mała modyfikacja


Rekomendowane odpowiedzi

Cześć, chcę przerobić troszkę AI które kiedyś dał mi Pablo-GM, ale za ch... nie jestem w stanie pojąć jego kodu więc przedstawię go wam i powiem co chcę zmienić:

 

Oto kod AI mojego "sojusznika"

CREATE

GML
// AGGRESSIVE ENEMY

// (runs around map searching for the player, then attacks the player if sighted)

// Initialize the path and store the id in a variable

path_id = path_add()

 

// Set the variables used in the AI

moving = 0

dir = 0

enemy_nearest = noone

enemy_in_sight = 0

enemy_angle = 0

turret_nearest = noone

turret_angle = 0

can_move = 0

can_spot = 1

can_shoot = 0

shooting = 0

on_turret = 0

gx = 0

gy = 0

 

// Set variables (these ones can be adjusted to taylor your AI) - fun to mess around with. Try adjusting some values.

hp = 100 // Health

spotted = 0 // If you make this value start on 1, then the enemy knows where you are right from the start. (he knows where you live! 0_0)

move_speed = 2 // Self explanitory

turn_speed = 0.15 // How fast the AI turns (its not really a speed, more of a sensitivity)

pause_time = 20 // Minimum time that the enemy will spend to stop and look around (when not alert of player)

max_range = 600 // AI only fires when enemy is within this range

max_view_angle = 90 // this value is basically the angle of the view cone divided by 2. i.e. if you 360° vision, put 180

shoot_speed = 15 // Minimum time the AI will pause for in between firing shots.

shoot_speed_random = 0 // (random time which is added on, more means less regular shooting pattern)

inaccuracy = 6 // we dont want the enemy being too accurate at shooting ^_^

critical_health = 40 // (if hp gets lower or equal, then AI tries to take cover if you fire at them.

bravery = 7 // the lower this value is, the more the AI runs away like a sissy (this is fun to mess around with)

prec_wall_collision = 0 // shouldn't really be changed unless you know what you are doing.

team_alert = 0 // team_alert defines whether or not one enemy can alert the rest of the team of you and your location

turret_use = 5 // Whether or not the AI can use a turret & how likely it happens. higher the value, less chance. 0=never 1=always

turret_radius = 200 // Max distance in which the AI will consider mounting a turret

turret_time = 300 // After this period of inactivity on the turret, the AI will dismount & go find you.

// These values can be changed if your object names are different to mine. This makes putting this AI into your game alot easier.

// See, I think about these things. <__<

// (if there is multiple objects for these (such as bullets), then you will have to make a parent object, and use that (Like I did with obj_block_par))

my_enemy = parentwroga

wall_parent = blockparent

bullet = m1_kumpel

ally = parentkumpla

 

 

// set move alarm

alarm[1] = pause_time

 

Alarm0

GML
if spotted = 0

{

moving = 0

}

 

Alarm1

 

GML
can_move = 1

Alarm2

 

GML
spotted = 1

alarm[4] = pause_time/2

if team_alert

{

with(parentkumpla)

{

if can_spot = 1

{

can_spot = 0

alarm[2] = 20

}

}

}

Alarm3

 

GML
spotted = 1

alarm[4] = pause_time/2

if team_alert

{

with(parentkumpla)

{

if can_spot = 1

{

can_spot = 0

alarm[2] = 20

}

}

}

Alarm4

 

GML
can_shoot = 1

Alarm5

 

GML
turret_nearest.user = noone

turret_nearest.mounted = 0

on_turret = 0

can_shoot = 1

can_move = 1

 

 

STEP

 

GML
///////////// ENEMY LOGIC STEP EVENT

 

// Update info on nearest enemy

if instance_exists(my_enemy)

{

enemy_nearest = instance_nearest(x,y,my_enemy)

enemy_in_sight = !collision_line(x,y,enemy_nearest.x,enemy_nearest.y,wall_parent,prec_wall_collision,0)

enemy_angle = point_direction(x,y,enemy_nearest.x,enemy_nearest.y)

}

else

{

enemy_nearest = noone

enemy_in_sight = 0

}

 

// Moves object toward the next point on the path (if its meant to be moving)

if moving = 1

{

 

if path_get_number(path_id) > 1

{

if !collision_line(x,y,path_get_point_x(path_id,1),path_get_point_y(path_id,1),wall_parent,prec_wall_collision,1)

{

path_delete_point(path_id,0)

}

}

 

// move toward next point in path

mp_potential_step(path_get_point_x(path_id,0),path_get_point_y(path_id,0),move_speed,0)

 

if !spotted

{

dir = point_direction(x,y,path_get_point_x(path_id,0),path_get_point_y(path_id,0))

}

else

{

if !enemy_in_sight

&& !on_turret

{

dir = point_direction(x,y,path_get_point_x(path_id,0),path_get_point_y(path_id,0))

}

}

 

// if reached destination

if point_distance(x,y,path_get_point_x(path_id,path_get_number(path_id)-1),path_get_point_y(path_id,path_get_number(path_id)-1)) < max(sprite_height,sprite_width)/2

{

moving = 0

path_clear_points(path_id)

can_move = 0

alarm[1] = 1

 

if spotted = 0 // if player has not been spotted yet, then look around.

{

alarm[1] = pause_time+random(pause_time)

 

if floor(random(2)) = 1

{

dir += max_view_angle + random(max_view_angle)

}

else

{

dir -= max_view_angle + random(max_view_angle)

}

}

}

}

 

 

 

 

// This rotates the image angle towards dir.

d = dir - image_angle

 

if d<-180 {d+=360}

if d>180 {d-=360}

 

image_angle+=d*turn_speed

 

 

////////////////////////////

////// MAIN LOGIC

// reset shooting variable

shooting = 0

 

if spotted = 1

{

if instance_exists(enemy_nearest)

{

if enemy_in_sight = 1

{

dir = enemy_angle

 

/// Check if the enemy is shootable

df = dir - image_angle

if df<-180 {df+=360}

if df>180 {df-=360}

df = abs(df)

 

if on_turret

{

if df < inaccuracy+turret_nearest.inaccuracy*2

&& collision_line(x,y,x+lengthdir_x(turret_nearest.max_range,dir),y+lengthdir_y(turret_nearest.max_range,dir),enemy_nearest,prec_wall_collision,1)

&& !collision_line(x,y,x+lengthdir_x(turret_nearest.max_range,dir),y+lengthdir_y(turret_nearest.max_range,dir),ally,prec_wall_collision,1)

{

event_user(1) // shoot

shooting = 1

}

}

else

{

if df < inaccuracy*2

&& collision_line(x,y,x+lengthdir_x(max_range,dir),y+lengthdir_y(max_range,dir),enemy_nearest,prec_wall_collision,1)

&& !collision_line(x,y,x+lengthdir_x(max_range,dir),y+lengthdir_y(max_range,dir),ally,prec_wall_collision,1)

{

event_user(1) // shoot

shooting = 1

}

}

 

/// Turret mounting

if instance_exists(turret_nearest)

&& turret_use > 0

&& !on_turret

{

if point_distance(x,y,turret_nearest.x,turret_nearest.y) < turret_radius

&& !collision_line(x,y,turret_nearest.x,turret_nearest.y,wall_parent,prec_wall_collision,1)

&& turret_nearest.user = noone

&& id = instance_nearest(turret_nearest.x,turret_nearest.y,ally)

&& floor(random(turret_use)) = 0

{

if point_distance(x,y,turret_nearest.x,turret_nearest.y) < turret_nearest.mount_radius

{

turret_nearest.user = id

on_turret = 1

can_shoot = 0

}

else

{

if can_move

{

if mp_grid_path(global.mp_grid,path_id,x,y,turret_nearest.x-lengthdir_x(turret_nearest.mount_radius+10,turret_angle),turret_nearest.y-lengthdir_y(turret_nearest.mount_radius+10,turret_angle),1)

{

moving = 1

can_move = 0

alarm[1] = path_get_length(path_id)/move_speed+random(pause_time*3)

}

}

}

}

}

 

// Turret dismounting

if on_turret

&& enemy_in_sight

{

alarm[5] = turret_time

}

 

// if the enemy should try to strafe to dodge the bullet

if instance_exists(bullet)

{

if point_distance(x,y,instance_nearest(x,y,bullet).x,instance_nearest(x,y,bullet).y) < 200

&& floor(random(bravery)) = 0

{

if hp <= critical_health

{

//// if health is critical, then try and take cover.

gx = x+lengthdir_x(random(100),random(360))

gy = y+lengthdir_y(random(100),random(360))

 

if place_free(gx,gy)

&& collision_line(enemy_nearest.x,enemy_nearest.y,gx,gy,wall_parent,prec_wall_collision,1)

{

/// calculate path to the destination

if mp_grid_path(global.mp_grid,path_id,x,y,gx,gy,1)

{

moving = 1

can_move = 0

alarm[1] = path_get_length(path_id)/move_speed+random(pause_time*3)

alarm[0] = path_get_length(path_id)/move_speed+30

}

}

else

{

//// cant take cover, so just strafe to try and avoid

gx = x + lengthdir_x(sprite_width+random(60),dir+90+random(180))

gy = y + lengthdir_y(sprite_width+random(60),dir+90+random(180))

 

if place_free(gx,gy)

{

/// calculate path to the destination

if mp_grid_path(global.mp_grid,path_id,x,y,gx,gy,1)

{

moving = 1

alarm[0] = path_get_length(path_id)/move_speed+30

}

}

}

 

}

else

{

//// else just try avoid it

gx = x + lengthdir_x(sprite_width+random(60),dir+90+random(180))

gy = y + lengthdir_y(sprite_width+random(60),dir+90+random(180))

 

if place_free(gx,gy)

{

/// calculate path to the destination

if mp_grid_path(global.mp_grid,path_id,x,y,gx,gy,1)

{

moving = 1

alarm[0] = path_get_length(path_id)/move_speed+30

}

}

}

}

}

}

else //enemy out of sight

{

if can_move = 1

{

//// out of sight! move toward enemy!

gx = x+lengthdir_x(point_distance(x,y,enemy_nearest.x,enemy_nearest.y)-random(300)+random(300),enemy_angle-random(max_view_angle)+random(max_view_angle))

gy = y+lengthdir_y(point_distance(x,y,enemy_nearest.x,enemy_nearest.y)-random(300)+random(300),enemy_angle-random(max_view_angle)+random(max_view_angle))

 

if place_free(gx,gy)

&& !collision_line(enemy_nearest.x,enemy_nearest.y,gx,gy,wall_parent,prec_wall_collision,1)

{

/// calculate path to the destination

if mp_grid_path(global.mp_grid,path_id,x,y,gx,gy,1)

{

moving = 1

alarm[0] = path_get_length(path_id)/move_speed+30

can_move = 0

alarm[1] = pause_time+30+random(30)

}

}

}

}

}

}

else

{

if moving = 0

&& can_move = 1

{

event_user(0)

/// User event 0 - called when AI has not spotted the enemy yet.

}

 

// The player has not yet been spotted, check if player is in view

if instance_exists(my_enemy)

{

/// Check if the enemy is within viewing angle

df = enemy_angle - image_angle

if df<-180 {df+=360}

if df>180 {df-=360}

df = abs(df)

 

if enemy_in_sight = 1

&& df <= max_view_angle

{

alarm[2] = 1

can_spot = 0

}

 

if instance_exists(bullet)

{

if !collision_line(x,y,instance_nearest(x,y,bullet).x,instance_nearest(x,y,bullet).y,wall_parent,0,1)

{

alarm[2] = pause_time/bravery

can_spot = 0

}

}

}

}

 

User Defined 0

 

GML
/// User event 0 - called when AI has not spotted the enemy yet. (in this example, the AI just runs around the room.)

//

//// find random place in room

gx = round(random(room_width))

gy = round(random(room_height))

 

if place_free(gx,gy)

{

if mp_grid_path(global.mp_grid,path_id,x,y,gx,gy,1)

{

moving = 1

max_points = path_get_number(path_id)

next_point = 0

alarm[0] = path_get_length(path_id)/move_speed+30

}

}

 

User Defined 1

GML
/// event user one: called when AI has a sight on the enemy and should shoot it.

// (I made it a user defined event for convenience of customization)

if can_shoot

&& !on_turret

{

b = instance_create(x+lengthdir_x(64,image_angle-15),y+lengthdir_y(64,image_angle-15),bullet)

b.direction = image_angle+random(inaccuracy)-random(inaccuracy)

can_shoot = 0

alarm[4] = shoot_speed+random(shoot_speed_random)

}

 

 

Jest jeszcze obiekt kontrol którego kod jest taki :

 

GML
////// THE FOLLOWING CODE IS IMPORTANT, make sure it is in your game (calculates grid used for the pathfinding)

// the other 2 events in this object are not needed, and are for the example.

grid_size = 64;

// This is the grid size for the enemy motiong planning. Smaller grid size means more processing is

// needed, but gives a more accurate motion plan. Ideally this should be bigger than, or the same size as, the enemy.

// Create the grid that the enemys will use for motion planning.

global.mp_grid = mp_grid_create(0,0,room_width/grid_size,room_height/grid_size,grid_size,grid_size);

// Add the forbidden zones to the grid

mp_grid_add_instances(global.mp_grid,blockparent,0)

 

 

Teraz tłumaczę czego mi potrzeba :

 

1. chcę, aby mój przyjaciel podążał za mną bo jak ja się oddalam to żołnierze stoją jak te dupy w polu, chcę żeby nie oddalali się ode mnie na odległość większą niż 400 pikseli.

 

2. chcę aby sojusznik musiał przeładować broń co określoną ilość pocisków, żeby jego ostatni pocisk który wystrzeli był inny od pozostałych.

 

3. i żeby podczas oddania strzału na ułamek sekundy zmienił się jego sprite na sprite strzelającego.

 

 

parentwroga - osoba w ktora strzela sojusznik

 

parentkumpla - przyjacielskie boty

 

Za pomoc dam creditsy. :)

Odnośnik do komentarza
Udostępnij na innych stronach

Ok tutaj jest link do silnika gry : Link

 

Jeśli chcecie wiedzieć o co chodzi z tym ostatnim pociskiem innym niż pozostałe to wystrzelcie cały "magazynek" z broni "m1 garand"

 

aha i na wszelkie błędy które wyskoczą na ekranie klikajcie ignore. Przepraszam za nie ale to dopiero wersja testowa.

 

Dziękuję za pomoc :)

Odnośnik do komentarza
Udostępnij na innych stronach

Jeśli chcesz dodać odpowiedź, zaloguj się lub zarejestruj nowe konto

Jedynie zarejestrowani użytkownicy mogą komentować zawartość tej strony.

Zarejestruj nowe konto

Załóż nowe konto. To bardzo proste!

Zarejestruj się

Zaloguj się

Posiadasz już konto? Zaloguj się poniżej.

Zaloguj się
  • Ostatnio przeglądający   0 użytkowników

    • Brak zarejestrowanych użytkowników przeglądających tę stronę.
×
×
  • Dodaj nową pozycję...