/** * @brief Gets the pilots available in the system by a certain criteria. * * @usage p = pilot.get() -- Gets all the pilots * @usage p = pilot.get( { faction.get("Empire") } ) -- Only gets empire pilots. * * @luaparam f If f is a table of factions, it will only get pilots matching those factions. Otherwise it gets all the pilots. * @luareturn A table containing the pilots. * @luafunc get( f ) */ static int pilot_getPilots( lua_State *L ) { int i, j, k; int *factions; int nfactions; LuaFaction *f; LuaPilot p; /* Check for belonging to faction. */ if (lua_istable(L,1)) { /* Get table length and preallocate. */ nfactions = (int) lua_objlen(L,1); factions = malloc( sizeof(int) * nfactions ); /* Load up the table. */ lua_pushnil(L); i = 0; while (lua_next(L, -2) != 0) { f = lua_tofaction(L, -1); factions[i++] = f->f; lua_pop(L,1); } /* Now put all the matching pilots in a table. */ lua_newtable(L); k = 1; for (i=0; i<pilot_nstack; i++) { for (j=0; j<nfactions; j++) { if ((pilot_stack[i]->faction == factions[j]) && !pilot_isDisabled(pilot_stack[i])) { lua_pushnumber(L, k++); /* key */ p.pilot = pilot_stack[i]->id; lua_pushpilot(L, p); /* value */ lua_rawset(L,-3); /* table[key] = value */ break; /* Continue to next pilot. */ } } } /* clean up. */ free(factions); } else { /* Now put all the matching pilots in a table. */ lua_newtable(L); k = 1; for (i=0; i<pilot_nstack; i++) { if (!pilot_isDisabled(pilot_stack[i])) { lua_pushnumber(L, k++); /* key */ p.pilot = pilot_stack[i]->id; lua_pushpilot(L, p); /* value */ lua_rawset(L,-3); /* table[key] = value */ break; /* Continue to next pilot. */ } } } return 1; }
/** * @brief Gets the player's associated pilot. * * @luareturn The player's pilot. * @luafunc pilot() */ static int playerL_getPilot( lua_State *L ) { LuaPilot lp; lp.pilot = PLAYER_ID; lua_pushpilot(L, lp); return 1; }
/** * @brief Gets the player's pilot. * * @usage player = pilot.player() * * @luareturn Pilot pointing to the player. * @luafunc player() */ static int pilot_getPlayer( lua_State *L ) { LuaPilot lp; if (player == NULL) { lua_pushnil(L); return 1; } lp.pilot = player->id; lua_pushpilot(L,lp); return 1; }
/** * @brief Have a pilot order its escorts to attack its target. * * @param parent Pilot giving the order. */ int escorts_attack( Pilot *parent ) { int ret; Pilot *t; /* Avoid killing self. */ t = pilot_get(parent->target); if (t == NULL) return 1; if (t->faction == parent->faction) return 1; /* Send command. */ ret = 1; if (parent->target != parent->id) { lua_pushpilot(naevL, parent->target); ret = escort_command( parent, "e_attack", -1 ); lua_pop(naevL, 1); } if ((ret == 0) && (parent == player.p)) player_message(_("\agEscorts: \a0Attacking %s."), t->name); return ret; }
/** * @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; }