associating *ENTITY correctly. Here's the whole code in wdl. Maybe someone has a better way of writing an AI that forages.
///////////////////////////////////////////////////////////////////////////////////////////////////////
// copy / overwrite the files inside this folder in your game folder (work if you are using the templates)
// don't forget to include robby.wdl in your main game file (office.wdl if you are using the templates)
// place a model in your level and attach it the robby_the_robot action
///////////////////////////////////////////////////////////////////////////////////////////////////////
var standing_time;
var walking_time;
var walk_speed;
var front_coords;
var creature_distance;
///////////////////////////////////////////////////////////////////////////////////////////////////////
entity* grub;
entity* creature;
string antenna_pcx = <antenna.pcx>;
define healthpoints = skill18;
///////////////////////////////////////////////////////////////////////////////////////////////////////
string health_str = "Player Health: Enemy Health:";
string healthbar_mdl = <hbar.mdl>;
//sound sorry_wav = <sorry.wav>;
///////////////////////////////////////////////////////////////////////////////////////////////////////
font comic_font, <comic20.pcx>, 34, 38;
///////////////////////////////////////////////////////////////////////////////////////////////////////
function robby_event();
function check_front();
///////////////////////////////////////////////////////////////////////////////////////////////////////
//panel time_pan // a simple panel, without a bitmap, used for the score
//{
// pos_x = 0;
// pos_y = 0;
// digits 520, 400, 3, comic_font, 1, standing_time; // displays the score with 3 digits
// digits 520, 440, 3, comic_font, 1, walking_time; // displays the score with 3 digits
// flags = refresh, d3d, visible;
//}
//
//text info_txt
//{
// pos_x = 350;
// pos_y = 400;
// font = comic_font;
// string = "Stand:\n Walk:";
// flags = visible;
//}
///////////////////////////////////////////////////////////////////////////////////////////////////////
function display_health( )
{
my.passable = on;
while (you != null)
{
my.x = you.x;
my.y = you.y;
my.z = you.z + 50;
my.scale_y = you.healthpoints / 5;
vec_set(temp, camera.x);
vec_sub(temp, my.x);
vec_to_angle (my.pan, temp);
my.tilt = 0;
wait (1);
}
my.invisible = on;
}
action robby_the_robot
{
creature = me;
creature.healthpoints = 100;
my.enable_entity = on;
my.enable_impact = on;
my.enable_block = on;
ent_create (healthbar_mdl, my.pos, display_health);
my.event = robby_event;
// my.ENABLE_SCAN = on;
//
// my.event = rotate_now;
ent_create(antenna_pcx, nullvector, check_front);
while(1)
{
standing_time = 3 + random(1); // stand for 3..10 seconds
while (standing_time > 0)
{
standing_time -= time / 16;
ent_cycle("stand", my.skill20); // play "stand" animation
my.skill20 += 2 * time;
my.skill20 %= 100; // loop
wait (1);
}
walking_time = 10 + random(10); // walk for 10..20 seconds
walk_speed.x = 3;
walk_speed.y = 0;
walk_speed.z = 0;
walk_speed *= time; // keep the same speed at different frame rates
wait (1);
while (walking_time > 0)
{
///////////////////////////////////////////
while (grub == null) //////<---------this code gets rid of empty pointer error, but freezes the AI after he picks up the food.
{wait (1);}
if (vec_dist (my.x, grub.pos) < 200) // the player approaches the food
{
// name_ent2();
vec_set(temp, grub.pos);
vec_sub(temp, my.pos);
vec_to_angle(my.pan, temp);
my.tilt = 0; // I'm a maniac
walk_speed.x = 10 * time;
walk_speed.y = 0;
walk_speed.z = 0;
ent_move(creature_distance, nullvector);
ent_cycle("walk", my.skill19); // play walk frames animation
my.skill19 += 5 * time; // "walk" animation speed
// ent_playsound (my, walk_snd, 50);
wait (1);
}
/////////////////////////////////////////////
if (content(front_coords) == content_solid)
{
my.skill40 = my.pan;
my.skill41 = 30 + random(90);
while (my.pan < my.skill40 + my.skill41) // rotate 30..120 degrees
{
my.pan += 3 * time;
wait (1);
}
}
else
{
walking_time -= time / 16;
ent_cycle("walk", my.skill20); // play "walk" animation
my.skill20 += 4 * time;
my.skill20 %= 100; // loop
move_mode = ignore_passable; // ignore passable entities
result = ent_move (walk_speed, nullvector);
if (result == 0) // got stuck!
{
walk_speed.x *= -1; // reversed movement
my.skill40 = my.pan;
my.skill41 = 30 + random(90);
while (my.pan < my.skill40 + my.skill41) // rotate 30..120 degrees
{
my.pan += 3 * time;
ent_cycle("walk", my.skill20); // play reversed "walk" animation
my.skill20 -= 4 * time;
my.skill20 %= 100; // loop
move_mode = ignore_passable; // ignore passable entities
ent_move (walk_speed, nullvector);
wait (1);
}
walk_speed.x *= -1; // restore walk_speed
}
}
wait (1);
}
wait (1);
}
}
function check_front()
{
// my.invisible = on; // remove the comment to make the antenna invisible
my.passable = on;
my.transparent = on;
while (1)
{
my.x = you.x + 30 * cos(you.pan); // the antenna is placed
my.y = you.y + 30 * sin(you.pan); // 30 quants in front of the player
my.z = you.z;
vec_set (front_coords, my.pos);
wait (1);
}
}
function robby_event()
{
exclusive_global;
if (event_type == event_entity) // collided with an entity (the player)
{
// snd_play (sorry_wav, 50, 0);
}
}
action food
{
grub = me;
// you.size +=1;
wait (1);
var randomhealth;
randomhealth = 25 + random(20);
my.passable = on;
while (creature == null) {wait (1);}
while (vec_dist (my.x, creature.x) > 25) {wait (1);}
creature.healthpoints += 40; // add 25 health points
// effect (particle_sparks, 2, target, normal);
ent_remove (me);
}
action animatetrig
{
my.passable = on;
while (player == null) {wait (1);}
if (vec_dist (my.x, player.x) < 50) // if the player comes closer than 50 quants attack him
{
while (1)
{var anim_speed;
my.skill88 = 0;
while (my.skill88 < 10) // play this animation for 10 seconds
{
my.skill88 += time_step / 16;
anim_speed += 2 * time_step; // 2 = walk animation speed
ent_animate(my, "open", anim_speed, ANM_CYCLE);
wait (1);
}
my.skill88 = 0;
while (my.skill88 < 10) // play this animation for 10 seconds
{
my.skill88 += time_step / 16;
anim_speed += 3 * time_step; // 3 = jump animation speed
ent_animate(my, "roll", anim_speed, ANM_CYCLE);
wait (1);
}
wait (1);
}
}
}