/** * @brief The pseudo-ai of the beam weapons. * * @param w Weapon to do the thinking. * @param dt Current delta tick. */ static void think_beam( Weapon* w, const double dt ) { (void)dt; Pilot *p, *t; double diff; Vector2d v; /* Get pilot, if pilot is dead beam is destroyed. */ p = pilot_get(w->parent); if (p==NULL) { w->timer = -1.; /* Hack to make it get destroyed next update. */ return; } /* Check if pilot has enough energy left to keep beam active. */ p->energy -= dt*w->outfit->u.bem.energy; if (p->energy < 0.) { p->energy = 0.; w->timer = -1; return; } /* Use mount position. */ pilot_getMount( p, w->mount, &v ); w->solid->pos.x = p->solid->pos.x + v.x; w->solid->pos.y = p->solid->pos.y + v.y; /* Handle aiming. */ switch (w->outfit->type) { case OUTFIT_TYPE_BEAM: w->solid->dir = p->solid->dir; break; case OUTFIT_TYPE_TURRET_BEAM: /* Get target, if target is dead beam stops moving. */ t = pilot_get(w->target); if (t==NULL) { weapon_setTurn( w, 0. ); return; } if (w->target == w->parent) /* Invalid target, tries to follow shooter. */ diff = angle_diff(w->solid->dir, p->solid->dir); else diff = angle_diff(w->solid->dir, /* Get angle to target pos */ vect_angle(&w->solid->pos, &t->solid->pos)); weapon_setTurn( w, CLAMP( -w->outfit->u.bem.turn, w->outfit->u.bem.turn, 10 * diff * w->outfit->u.bem.turn )); break; default: return; } }
/** * @brief Handles approaching a position with autonav. * * @param[in] pos Position to go to. * @param[out] dist2 Square distance left to target. * @param count_target If 1 it subtracts the braking distance from dist2. Otherwise it returns the full distance. * @return 1 on completion. */ static int player_autonavApproach( const Vector2d *pos, double *dist2, int count_target ) { double d, t, vel, dist; /* Only accelerate if facing move dir. */ d = pilot_face( player.p, vect_angle( &player.p->solid->pos, pos ) ); if (FABS(d) < MIN_DIR_ERR) { if (player_acc < 1.) player_accel( 1. ); } else if (player_acc > 0.) player_accelOver(); /* Get current time to reach target. */ t = MIN( 1.5*player.p->speed, VMOD(player.p->solid->vel) ) / (player.p->thrust / player.p->solid->mass); /* Get velocity. */ vel = MIN( player.p->speed, VMOD(player.p->solid->vel) ); /* Get distance. */ dist = vel*(t+1.1*M_PI/player.p->turn) - 0.5*(player.p->thrust/player.p->solid->mass)*t*t; /* Output distance^2 */ d = vect_dist( pos, &player.p->solid->pos ); dist = d - dist; if (count_target) *dist2 = dist; else *dist2 = d; /* See if should start braking. */ if (dist < 0.) { player_accelOver(); return 1; } return 0; }
/** * @brief Adds a fleet to the system. * * You can then iterate over the pilots to change parameters like so: * @code * p = pilot.add( "Sml Trader Convoy" ) * for k,v in pairs(p) do * v:setHostile() * end * @endcode * * @usage p = pilot.add( "Pirate Hyena" ) -- Just adds the pilot (will jump in). * @usage p = pilot.add( "Trader Llama", "dummy" ) -- Overrides AI with dummy ai. * @usage p = pilot.add( "Sml Trader Convoy", "def", vec2.new( 1000, 200 ) ) -- Pilot won't jump in, will just appear. * @usage p = pilot.add( "Empire Pacifier", "def", vec2.new( 1000, 1000 ), true ) -- Have the pilot jump in. * * @luaparam fleetname Name of the fleet to add. * @luaparam ai If set will override the standard fleet AI. "def" means use default. * @luaparam pos Position to create pilots around instead of choosing randomly. * @luaparam jump true if pilots should jump in, false by default if pos is defined. * @luareturn Table populated with all the pilots created. The keys are ordered numbers. * @luafunc add( fleetname, ai, pos, jump ) */ static int pilot_addFleet( lua_State *L ) { NLUA_MIN_ARGS(1); Fleet *flt; const char *fltname, *fltai; int i, j; unsigned int p; double a; double d; Vector2d vv,vp, vn; FleetPilot *plt; LuaPilot lp; LuaVector *lv; int jump; /* Parse first argument - Fleet Name */ fltname = luaL_checkstring(L,1); /* Parse second argument - Fleet AI Override */ if (lua_gettop(L) > 1) { fltai = luaL_checkstring(L,2); if (strcmp(fltai, "def")==0) /* Check if set to default */ fltai = NULL; } else fltai = NULL; /* Parse third argument - Position */ if (lua_gettop(L) > 2) { lv = luaL_checkvector(L,3); } else lv = NULL; if (lua_gettop(L) > 3) { jump = lua_toboolean(L,4); } else { /* Only jump by default if not position was passed. */ if (lv==NULL) jump = 1; else jump = 0; } /* Needed to determine angle. */ vectnull(&vn); /* pull the fleet */ flt = fleet_get( fltname ); if (flt == NULL) { NLUA_ERROR(L,"Fleet '%s' doesn't exist.", fltname); return 0; } /* Use position passed if possible. */ if (lv != NULL) { if (!jump) vectcpy( &vp, &lv->vec ); else { /* Pilot is jumping in, we'll only use the vector angle. */ d = RNGF()*(HYPERSPACE_ENTER_MAX-HYPERSPACE_ENTER_MIN) + HYPERSPACE_ENTER_MIN; vect_pset( &vp, d, VANGLE(lv->vec) ); } } else { d = RNGF()*(HYPERSPACE_ENTER_MAX-HYPERSPACE_ENTER_MIN) + HYPERSPACE_ENTER_MIN; vect_pset( &vp, d, RNGF() * 2.*M_PI); } /* now we start adding pilots and toss ids into the table we return */ j = 0; lua_newtable(L); for (i=0; i<flt->npilots; i++) { plt = &flt->pilots[i]; if (RNG(0,100) <= plt->chance) { /* fleet displacement */ vect_cadd(&vp, RNG(75,150) * (RNG(0,1) ? 1 : -1), RNG(75,150) * (RNG(0,1) ? 1 : -1)); /* Set velocity only if no position is set.. */ if (lv != NULL) { if (jump) { a = vect_angle(&vp,&vn); vect_pset( &vv, HYPERSPACE_VEL, a ); } else { a = RNGF() * 2.*M_PI; vectnull( &vv ); } } else { /* Entering via hyperspace. */ a = vect_angle(&vp,&vn); vect_pset( &vv, HYPERSPACE_VEL, a ); } /* Make sure angle is sane. */ if (a < 0.) a += 2.*M_PI; /* Create the pilot. */ p = fleet_createPilot( flt, plt, a, &vp, &vv, fltai, 0 ); /* we push each pilot created into a table and return it */ lua_pushnumber(L,++j); /* index, starts with 1 */ lp.pilot = p; lua_pushpilot(L,lp); /* value = LuaPilot */ lua_rawset(L,-3); /* store the value in the table */ } } return 1; }
/** * @brief The AI of seeker missiles. * * @param w Weapon to do the thinking. * @param dt Current delta tick. */ static void think_seeker( Weapon* w, const double dt ) { double diff; double vel; Pilot *p; int effect; Vector2d v; double t; if (w->target == w->parent) return; /* no self shooting */ p = pilot_get(w->target); /* no null pilot_nstack */ if (p==NULL) { weapon_setThrust( w, 0. ); weapon_setTurn( w, 0. ); return; } /* Handle by status. */ switch (w->status) { case WEAPON_STATUS_OK: if (w->lockon < 0.) w->status = WEAPON_STATUS_LOCKEDON; break; case WEAPON_STATUS_LOCKEDON: /* Check to see if can get jammed */ if ((p->jam_range != 0.) && /* Target has jammer and weapon is in range */ (vect_dist(&w->solid->pos,&p->solid->pos) < p->jam_range)) { /* Check to see if weapon gets jammed */ if (RNGF() < p->jam_chance - w->outfit->u.amm.resist) { w->status = WEAPON_STATUS_JAMMED; /* Give it a nice random effect */ effect = RNG(0,3); switch (effect) { case 0: /* Stuck in left loop */ weapon_setTurn( w, w->outfit->u.amm.turn ); break; case 1: /* Stuck in right loop */ weapon_setTurn( w, -w->outfit->u.amm.turn ); break; default: /* Blow up. */ w->timer = -1.; break; } } else /* Can't get jammed anymore */ w->status = WEAPON_STATUS_UNJAMMED; } /* Purpose fallthrough */ case WEAPON_STATUS_UNJAMMED: /* Work as expected */ /* Smart seekers take into account ship velocity. */ if (w->outfit->u.amm.ai == 2) { /* Calculate time to reach target. */ vect_cset( &v, p->solid->pos.x - w->solid->pos.x, p->solid->pos.y - w->solid->pos.y ); t = vect_odist( &v ) / w->outfit->u.amm.speed; /* Calculate target's movement. */ vect_cset( &v, v.x + t*(p->solid->vel.x - w->solid->vel.x), v.y + t*(p->solid->vel.y - w->solid->vel.y) ); /* Get the angle now. */ diff = angle_diff(w->solid->dir, VANGLE(v) ); } /* Other seekers are stupid. */ else { diff = angle_diff(w->solid->dir, /* Get angle to target pos */ vect_angle(&w->solid->pos, &p->solid->pos)); } /* Set turn. */ weapon_setTurn( w, CLAMP( -w->outfit->u.amm.turn, w->outfit->u.amm.turn, 10 * diff * w->outfit->u.amm.turn )); break; case WEAPON_STATUS_JAMMED: /* Continue doing whatever */ /* Do nothing, dir_vel should be set already if needed */ break; default: WARN("Unknown weapon status for '%s'", w->outfit->name); break; } /* Limit speed here */ vel = MIN(w->outfit->u.amm.speed, VMOD(w->solid->vel) + w->outfit->u.amm.thrust*dt); vect_pset( &w->solid->vel, vel, w->solid->dir ); /*limit_speed( &w->solid->vel, w->outfit->u.amm.speed, dt );*/ }
/** * @brief Creates a new weapon. * * @param outfit Outfit which spawned the weapon. * @param dir Direction the shooter is facing. * @param pos Position of the shooter. * @param vel Velocity of the shooter. * @param parent Shooter ID. * @param target Target ID of the shooter. * @return A pointer to the newly created weapon. */ static Weapon* weapon_create( const Outfit* outfit, const double dir, const Vector2d* pos, const Vector2d* vel, const unsigned int parent, const unsigned int target ) { Vector2d v; double mass, rdir; Pilot *pilot_target; double x,y, t, dist; Weapon* w; /* Create basic features */ w = malloc(sizeof(Weapon)); memset(w, 0, sizeof(Weapon)); w->faction = pilot_get(parent)->faction; /* non-changeable */ w->parent = parent; /* non-changeable */ w->target = target; /* non-changeable */ w->outfit = outfit; /* non-changeable */ w->update = weapon_update; w->status = WEAPON_STATUS_OK; w->strength = 1.; switch (outfit->type) { /* Bolts treated together */ case OUTFIT_TYPE_BOLT: case OUTFIT_TYPE_TURRET_BOLT: /* Only difference is the direction of fire */ if ((outfit->type == OUTFIT_TYPE_TURRET_BOLT) && (w->parent!=w->target) && (w->target != 0)) { /* Must have valid target */ pilot_target = pilot_get(w->target); if (pilot_target == NULL) rdir = dir; else { /* Get the distance */ dist = vect_dist( pos, &pilot_target->solid->pos ); /* Aim. */ if (dist > outfit->u.blt.range*1.2) { x = pilot_target->solid->pos.x - pos->x; y = pilot_target->solid->pos.y - pos->y; } else { /* Try to predict where the enemy will be. */ /* Time for shots to reach that distance */ t = dist / (w->outfit->u.blt.speed + VMOD(*vel)); /* Position is calculated on where it should be */ x = (pilot_target->solid->pos.x + pilot_target->solid->vel.x*t) - (pos->x + vel->x*t); y = (pilot_target->solid->pos.y + pilot_target->solid->vel.y*t) - (pos->y + vel->y*t); } /* Set angle to face. */ rdir = ANGLE(x, y); } } else /* fire straight */ rdir = dir; rdir += RNG_2SIGMA() * outfit->u.blt.accuracy/2. * 1./180.*M_PI; if (rdir < 0.) rdir += 2.*M_PI; else if (rdir >= 2.*M_PI) rdir -= 2.*M_PI; mass = 1; /* Lasers are presumed to have unitary mass */ vectcpy( &v, vel ); vect_cadd( &v, outfit->u.blt.speed*cos(rdir), outfit->u.blt.speed*sin(rdir)); w->timer = outfit->u.blt.range / outfit->u.blt.speed; w->falloff = w->timer - outfit->u.blt.falloff / outfit->u.blt.speed; w->solid = solid_create( mass, rdir, pos, &v ); w->voice = sound_playPos( w->outfit->u.blt.sound, w->solid->pos.x, w->solid->pos.y, w->solid->vel.x, w->solid->vel.y); break; /* Beam weapons are treated together. */ case OUTFIT_TYPE_BEAM: case OUTFIT_TYPE_TURRET_BEAM: if ((outfit->type == OUTFIT_TYPE_TURRET_BEAM) && (w->parent!=w->target)) { pilot_target = pilot_get(target); rdir = (pilot_target == NULL) ? dir : vect_angle(pos, &pilot_target->solid->pos); } else rdir = dir; if (rdir < 0.) rdir += 2.*M_PI; else if (rdir >= 2.*M_PI) rdir -= 2.*M_PI; mass = 1.; /**< Needs a mass. */ w->solid = solid_create( mass, rdir, pos, NULL ); w->think = think_beam; w->timer = outfit->u.bem.duration; w->voice = sound_playPos( w->outfit->u.bem.sound, w->solid->pos.x, w->solid->pos.y, w->solid->vel.x, w->solid->vel.y); break; /* Treat seekers together. */ case OUTFIT_TYPE_AMMO: case OUTFIT_TYPE_TURRET_AMMO: if (w->outfit->type == OUTFIT_TYPE_TURRET_AMMO) { pilot_target = pilot_get(w->target); if (pilot_target == NULL) rdir = dir; else { /* Get the distance */ dist = vect_dist( pos, &pilot_target->solid->pos ); /* Aim. */ /* Try to predict where the enemy will be. */ /* Time for shots to reach that distance */ if (outfit->u.amm.thrust == 0.) t = dist / (w->outfit->u.amm.speed + VMOD(*vel)); else t = dist / w->outfit->u.amm.speed; /* Position is calculated on where it should be */ x = (pilot_target->solid->pos.x + pilot_target->solid->vel.x*t) - (pos->x + vel->x*t); y = (pilot_target->solid->pos.y + pilot_target->solid->vel.y*t) - (pos->y + vel->y*t); /* Set angle to face. */ rdir = ANGLE(x, y); } } else { rdir = dir; } if (outfit->u.amm.accuracy != 0.) { rdir += RNG_2SIGMA() * outfit->u.amm.accuracy/2. * 1./180.*M_PI; if ((rdir > 2.*M_PI) || (rdir < 0.)) rdir = fmod(rdir, 2.*M_PI); } if (rdir < 0.) rdir += 2.*M_PI; else if (rdir >= 2.*M_PI) rdir -= 2.*M_PI; /* If thrust is 0. we assume it starts out at speed. */ vectcpy( &v, vel ); if (outfit->u.amm.thrust == 0.) vect_cadd( &v, cos(rdir) * w->outfit->u.amm.speed, sin(rdir) * w->outfit->u.amm.speed ); /* Set up ammo details. */ mass = w->outfit->mass; w->lockon = outfit->u.amm.lockon; w->timer = outfit->u.amm.duration; w->solid = solid_create( mass, rdir, pos, &v ); if (w->outfit->u.amm.thrust != 0.) weapon_setThrust( w, w->outfit->u.amm.thrust * mass ); /* Handle seekers. */ if (w->outfit->u.amm.ai > 0) { w->think = think_seeker; /* AI is the same atm. */ /* If they are seeking a pilot, increment lockon counter. */ pilot_target = pilot_get(target); if (pilot_target != NULL) pilot_target->lockons++; } /* Play sound. */ w->voice = sound_playPos(w->outfit->u.amm.sound, w->solid->pos.x, w->solid->pos.y, w->solid->vel.x, w->solid->vel.y); break; /* just dump it where the player is */ default: WARN("Weapon of type '%s' has no create implemented yet!", w->outfit->name); w->solid = solid_create( 1., dir, pos, vel ); break; } /* Set life to timer. */ w->life = w->timer; return w; }