#define car_speed skill55
SOUND* aiskids1_wav = "aiskids1.wav";
SOUND* carai1_wav = "carai1.wav";
ENTITY* dummy1;
ENTITY* carai1_dummy;
ENTITY* carai1;
action car_ai()
{
carai1 = my;
var distance = 0;
var previous_pan;
var following_pan;
var min_speed = 20;
var max_speed;
var carai_engine;
var engine_factor;
var dist_z;
var skids_once = 0;
VECTOR last_pos[3];
VECTOR direction[3];
VECTOR temp[3];
max_speed = 35;
dummy1 = ent_create(NULL, nullvector, NULL);
// create another (carai1_dummy) dummy entity that will move on the path
// the ai car will use the same x, y, pan, tilt and roll with the carai1_dummy entity that moves on the path
// but will compute its own z using c_trace; this way, the nodes don't have to be place precisely above the ground
carai1_dummy = ent_create(NULL, nullvector, NULL);
path_set(dummy1, "path_001"); // make sure to name your path this way
engine_factor = 2 + random(2); // start with a random engine sound frecquency each and every time
carai_engine = ent_playloop(carai1, carai1_wav, 300); // start playing the car ai engine sound in a loop
while(1)
{
previous_pan = carai1.pan;
// place the carai1 entity on the path
path_spline(dummy1, carai1_dummy.x, distance);
distance += dummy1.car_speed * time_step;
// let carai1 look ahead
vec_diff(direction, carai1_dummy.x, last_pos);
vec_to_angle(carai1_dummy.pan, direction);
vec_set(last_pos, carai1_dummy.x);
carai1.pan = carai1_dummy.pan;
carai1.tilt = carai1_dummy.tilt;
carai1.roll = carai1_dummy.roll;
carai1.x = carai1_dummy.x;
carai1.y = carai1_dummy.y;
vec_set (temp.x, carai1_dummy.x);
temp.z -= 2000;
dist_z = c_trace(carai1_dummy.x, temp.x, IGNORE_ME | IGNORE_PASSABLE | IGNORE_MODELS); // ignore the "real" carai
carai1.z = carai1_dummy.z - dist_z + 35; // 35 = experimental value
// sets a variable car ai engine frequency, depending on the speed of the car
snd_tune(carai_engine, 0, dummy1.car_speed * engine_factor, 0);
wait(1);
following_pan = carai1.pan;
if (abs(following_pan - previous_pan) > 1) // sudden direction change? Then lower the speed of the AI car
{
dummy1.car_speed -= 4 * time_step;
if (skids_once == 0)
{
skids_once = 1;
if (random(1) > 0.9)
{
ent_playsound(dummy1, aiskids1_wav, 3000);
}
}
}
else
{
skids_once = 0;
dummy1.car_speed += 2 * time_step;
}
dummy1.car_speed = clamp(dummy1.car_speed, min_speed, max_speed);
}
}