2 * See Licensing and Copyright notice in naev.h
8 * @brief Lua player module.
12 #include "nlua_misn.h"
26 #include "nlua_pilot.h"
27 #include "nlua_vec2.h"
34 #include "nlua_system.h"
40 static int playerL_getname( lua_State
*L
);
41 static int playerL_shipname( lua_State
*L
);
42 static int playerL_freeSpace( lua_State
*L
);
43 static int playerL_pay( lua_State
*L
);
44 static int playerL_credits( lua_State
*L
);
45 static int playerL_msg( lua_State
*L
);
46 static int playerL_modFaction( lua_State
*L
);
47 static int playerL_modFactionRaw( lua_State
*L
);
48 static int playerL_getFaction( lua_State
*L
);
49 static int playerL_getRating( lua_State
*L
);
50 static int playerL_getPosition( lua_State
*L
);
51 static int playerL_getPilot( lua_State
*L
);
52 static int playerL_fuel( lua_State
*L
);
53 static int playerL_refuel( lua_State
*L
);
54 static int playerL_unboard( lua_State
*L
);
55 static int playerL_takeoff( lua_State
*L
);
56 static int playerL_addOutfit( lua_State
*L
);
57 static int playerL_addShip( lua_State
*L
);
58 static int playerL_misnDone( lua_State
*L
);
59 static int playerL_evtDone( lua_State
*L
);
60 static int playerL_teleport( lua_State
*L
);
61 static const luaL_reg playerL_methods
[] = {
62 { "name", playerL_getname
},
63 { "ship", playerL_shipname
},
64 { "freeCargo", playerL_freeSpace
},
65 { "pay", playerL_pay
},
66 { "credits", playerL_credits
},
67 { "msg", playerL_msg
},
68 { "modFaction", playerL_modFaction
},
69 { "modFactionRaw", playerL_modFactionRaw
},
70 { "getFaction", playerL_getFaction
},
71 { "getRating", playerL_getRating
},
72 { "pos", playerL_getPosition
},
73 { "pilot", playerL_getPilot
},
74 { "fuel", playerL_fuel
},
75 { "refuel", playerL_refuel
},
76 { "unboard", playerL_unboard
},
77 { "takeoff", playerL_takeoff
},
78 { "addOutfit", playerL_addOutfit
},
79 { "addShip", playerL_addShip
},
80 { "misnDone", playerL_misnDone
},
81 { "evtDone", playerL_evtDone
},
82 { "teleport", playerL_teleport
},
84 }; /**< Player lua methods. */
85 static const luaL_reg playerL_cond_methods
[] = {
86 { "name", playerL_getname
},
87 { "ship", playerL_shipname
},
88 { "getFaction", playerL_getFaction
},
89 { "getRating", playerL_getRating
},
90 { "misnDone", playerL_misnDone
},
91 { "evtDone", playerL_evtDone
},
93 }; /**< Conditional player lua methods. */
97 * @brief Loads the player lua library.
99 * @param readonly Whether to open in read-only form.
101 int nlua_loadPlayer( lua_State
*L
, int readonly
)
104 luaL_register(L
, "player", playerL_methods
);
106 luaL_register(L
, "player", playerL_cond_methods
);
112 * @brief Lua bindings to interact with the player.
114 * These bindings let you modify stuff about the player and find out special
115 * information. General usage would be calls like:
117 * pname = player.name()
118 * shipname = player.ship()
119 * freecargo = player.freeCargo()
120 * rating = player.getRating()
125 * @brief Gets the player's name.
127 * @luareturn The name of the player.
130 static int playerL_getname( lua_State
*L
)
132 lua_pushstring(L
,player_name
);
136 * @brief Gets the player's ship's name.
138 * @luareturn The name of the ship the player is currently in.
141 static int playerL_shipname( lua_State
*L
)
143 lua_pushstring(L
,player
->name
);
147 * @brief Gets the free cargo space the player has.
149 * @luareturn The free cargo space in tons of the player.
152 static int playerL_freeSpace( lua_State
*L
)
154 lua_pushnumber(L
, pilot_cargoFree(player
) );
158 * @brief Pays the player an amount of money.
160 * @usage player.pay( 500 ) -- Gives player 500 credits
162 * @luaparam amount Amount of money to pay the player in credits.
163 * @luafunc pay( amount )
165 static int playerL_pay( lua_State
*L
)
169 money
= luaL_checkint(L
,1);
170 player_modCredits( money
);
175 * @brief Gets how many credits the player has on him.
177 * @usage monies = player.credits()
179 * @luareturn The amount of credits the player has on him.
182 static int playerL_credits( lua_State
*L
)
184 lua_pushnumber(L
,player
->credits
);
188 * @brief Sends the player an ingame message.
190 * @luaparam message Message to send the player.
191 * @luafunc msg( message )
193 static int playerL_msg( lua_State
*L
)
197 str
= luaL_checkstring(L
,1);
198 player_messageRaw(str
);
203 * @brief Increases the player's standing to a faction by an amount. This will
204 * affect player's standing with that faction's allies and enemies also.
206 * @luaparam faction Name of the faction.
207 * @luaparam mod Amount to modify standing by.
208 * @luafunc modFaction( faction, mod )
210 static int playerL_modFaction( lua_State
*L
)
215 if (lua_isstring(L
,1)) f
= faction_get( lua_tostring(L
,1) );
216 else NLUA_INVALID_PARAMETER();
218 mod
= luaL_checknumber(L
,2);
219 faction_modPlayer( f
, mod
);
224 * @brief Increases the player's standing to a faction by a fixed amount without
225 * touching other faction standings.
227 * @luaparam faction Name of the faction.
228 * @luaparam mod Amount to modify standing by.
229 * @luafunc modFactionRaw( faction, mod )
231 static int playerL_modFactionRaw( lua_State
*L
)
237 if (lua_isstring(L
,1)) f
= faction_get( lua_tostring(L
,1) );
238 else NLUA_INVALID_PARAMETER();
239 mod
= luaL_checknumber(L
,2);
240 faction_modPlayerRaw( f
, mod
);
245 * @brief Gets the standing of the player with a certain faction.
247 * @luaparam faction Faction to get the standing of.
248 * @luareturn The faction standing.
249 * @luafunc getFaction( faction )
251 static int playerL_getFaction( lua_State
*L
)
256 if (lua_isstring(L
,1)) f
= faction_get( lua_tostring(L
,1) );
257 else NLUA_INVALID_PARAMETER();
259 lua_pushnumber(L
, faction_getPlayer(f
));
264 * @brief Gets the player's combat rating.
266 * @luareturn Returns the combat rating (in raw number) and the actual
267 * standing in human readable form.
268 * @luafunc getRating()
270 static int playerL_getRating( lua_State
*L
)
272 lua_pushnumber(L
, player_crating
);
273 lua_pushstring(L
, player_rating());
278 * @brief Gets the player's position.
280 * @usage v = player.pos()
282 * @luareturn The position of the player (Vec2).
285 static int playerL_getPosition( lua_State
*L
)
289 vectcpy( &v
.vec
, &player
->solid
->pos
);
290 lua_pushvector(L
, v
);
295 * @brief Gets the player's associated pilot.
297 * @luareturn The player's pilot.
300 static int playerL_getPilot( lua_State
*L
)
303 lp
.pilot
= PLAYER_ID
;
304 lua_pushpilot(L
, lp
);
310 * @brief Gets the amount of fuel a player has.
312 * @usage fuel = player.fuel()
314 * @luareturn The player's fuel.
317 static int playerL_fuel( lua_State
*L
)
319 lua_pushnumber(L
,player
->fuel
);
325 * @brief Refuels the player.
327 * @usage player.refuel() -- Refuel fully
328 * @usage player.refuel( 200 ) -- Refuels partially
330 * @param fuel Amount of fuel to add, will set to max if nil.
331 * @luafunc refuel( fuel )
333 static int playerL_refuel( lua_State
*L
)
337 if (lua_gettop(L
) > 0) {
338 f
= luaL_checknumber(L
,1);
342 player
->fuel
= player
->fuel_max
;
344 /* Make sure value is sane. */
345 player
->fuel
= CLAMP(0, player
->fuel_max
, player
->fuel
);
352 * @brief Unboards the player from it's boarded target.
354 * Use from inside a board hook.
356 * @usage player.unboard()
360 static int playerL_unboard( lua_State
*L
)
369 * @brief Forces the player to take off if he is landed.
371 * Assume the pilot is still landed until the current running function returns
372 * If you want to create pilots on take off please hook the takeoff/land hooks.
376 static int playerL_takeoff( lua_State
*L
)
388 * @brief Adds an outfit to the player's outfit list.
390 * @usage player.addOutfit( "Laser Cannon" ) -- Gives the player a laser cannon
391 * @usage player.addOutfit( "Plasma Blaster", 2 ) -- Gives the player two plasma blasters
393 * @luaparam name Name of the outfit to give.
394 * @luaparam q Optional parameter that sets the quantity to give (default 1).
395 * @luafunc addOutfit( name, q )
397 static int playerL_addOutfit( lua_State
*L
)
406 /* Handle parameters. */
407 str
= luaL_checkstring(L
, 1);
408 if (lua_gettop(L
) > 1)
409 q
= luaL_checkint(L
, 2);
412 o
= outfit_get( str
);
414 NLUA_ERROR(L
, "Outfit '%s' not found.", str
);
418 /* Add the outfits. */
419 player_addOutfit( o
, q
);
425 * @brief Gives the player a new ship.
427 * @note Should be given when landed, ideally on a planet with a shipyard.
429 * @usage player.addShip( "Pirate Kestrel", "Seiryuu" ) -- Gives the player a Pirate Kestrel named Seiryuu if player cancels the naming.
431 * @luaparam ship Name of the ship to add.
432 * @luaparam name Name to give the ship if player refuses to name it.
433 * @luafunc addShip( ship, name )
435 static int playerL_addShip( lua_State
*L
)
437 const char *str
, *name
;
440 /* Handle parameters. */
441 str
= luaL_checkstring(L
, 1);
442 name
= luaL_checkstring(L
, 2);
447 NLUA_ERROR(L
, "Ship '%s' not found.", str
);
452 player_newShip( s
, 0., 0, 0., 0., 0., name
);
458 * @brief Checks to see if player has done a mission.
460 * @usage if player.misnDone( "The Space Family" ) then -- Player finished mission
462 * @luaparam name Name of the mission to check.
463 * @luareturn true if mission was finished, false if it wasn't.
464 * @luafunc misnDone( name )
466 static int playerL_misnDone( lua_State
*L
)
471 /* Handle parameters. */
472 str
= luaL_checkstring(L
, 1);
474 /* Get mission ID. */
475 id
= mission_getID( str
);
477 NLUA_ERROR(L
, "Mission '%s' not found in stack", str
);
481 return player_missionAlreadyDone( id
);
486 * @brief Checks to see if player has done an event.
488 * @usage if player.evtDone( "Shipwreck" ) then -- Player finished event
490 * @luaparam name Name of the event to check.
491 * @luareturn true if event was finished, false if it wasn't.
492 * @luafunc evtDone( name )
494 static int playerL_evtDone( lua_State
*L
)
499 /* Handle parameters. */
500 str
= luaL_checkstring(L
, 1);
503 id
= event_dataID( str
);
505 NLUA_ERROR(L
, "Event '%s' not found in stack", str
);
509 return player_eventAlreadyDone( id
);
514 * @brief Teleports the player to a new system.
516 * Does not change the position nor velocity of the player, which will probably be wrong in the new system.
518 * @usage player.teleport( system.get("Arcanis") ) -- Teleports the player to arcanis.
520 * @luaparam sys System to teleport the player to.
521 * @luafunc teleport( sys )
523 static int playerL_teleport( lua_State
*L
)
528 sys
= luaL_checksystem(L
,1);
530 /* Jump out hook is run first. */
531 hooks_run( "jumpout" );
533 /* Just in case remove hyperspace flags. */
534 pilot_rmFlag( player
, PILOT_HYPERSPACE
| PILOT_HYP_BEGIN
| PILOT_HYP_PREP
);
536 /* Go to the new system. */
537 space_init( sys
->s
->name
);
539 /* Map gets deformed when jumping this way. */
542 /* Add the escorts. */
545 /* Run hooks - order is important. */
546 hooks_run( "jumpin" );
547 hooks_run( "enter" );
548 events_trigger( EVENT_TRIGGER_ENTER
);