Merge branch 'master' of git://github.com/BTAxis/naev into testmission
[naev.git] / src / nlua_player.c
blob99143258ed09ca0b461d8de09763f228140550c9
1 /*
2 * See Licensing and Copyright notice in naev.h
3 */
5 /**
6 * @file nlua_player.c
8 * @brief Lua player module.
9 */
12 #include "nlua_misn.h"
14 #include "naev.h"
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <math.h>
21 #include "lua.h"
22 #include "lauxlib.h"
24 #include "nlua.h"
25 #include "nluadef.h"
26 #include "nlua_pilot.h"
27 #include "nlua_vec2.h"
28 #include "log.h"
29 #include "player.h"
30 #include "board.h"
31 #include "mission.h"
32 #include "event.h"
33 #include "land.h"
34 #include "nlua_system.h"
35 #include "map.h"
36 #include "hook.h"
39 /* player */
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 },
83 {0,0}
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 },
92 {0,0}
93 }; /**< Conditional player lua methods. */
96 /**
97 * @brief Loads the player lua library.
98 * @param L Lua state.
99 * @param readonly Whether to open in read-only form.
101 int nlua_loadPlayer( lua_State *L, int readonly )
103 if (readonly == 0)
104 luaL_register(L, "player", playerL_methods);
105 else
106 luaL_register(L, "player", playerL_cond_methods);
107 return 0;
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:
116 * @code
117 * pname = player.name()
118 * shipname = player.ship()
119 * freecargo = player.freeCargo()
120 * rating = player.getRating()
121 * @endcode
122 * @luamod player
125 * @brief Gets the player's name.
127 * @luareturn The name of the player.
128 * @luafunc name()
130 static int playerL_getname( lua_State *L )
132 lua_pushstring(L,player_name);
133 return 1;
136 * @brief Gets the player's ship's name.
138 * @luareturn The name of the ship the player is currently in.
139 * @luafunc ship()
141 static int playerL_shipname( lua_State *L )
143 lua_pushstring(L,player->name);
144 return 1;
147 * @brief Gets the free cargo space the player has.
149 * @luareturn The free cargo space in tons of the player.
150 * @brief freeCargo()
152 static int playerL_freeSpace( lua_State *L )
154 lua_pushnumber(L, pilot_cargoFree(player) );
155 return 1;
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 )
167 int money;
169 money = luaL_checkint(L,1);
170 player_modCredits( money );
172 return 0;
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.
180 * @luafunc credits()
182 static int playerL_credits( lua_State *L )
184 lua_pushnumber(L,player->credits);
185 return 1;
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 )
195 const char* str;
197 str = luaL_checkstring(L,1);
198 player_messageRaw(str);
200 return 0;
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 )
212 int f;
213 double mod;
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 );
221 return 0;
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 )
233 NLUA_MIN_ARGS(2);
234 int f;
235 double mod;
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 );
242 return 0;
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 )
253 NLUA_MIN_ARGS(1);
254 int f;
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));
261 return 1;
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());
274 return 2;
278 * @brief Gets the player's position.
280 * @usage v = player.pos()
282 * @luareturn The position of the player (Vec2).
283 * @luafunc pos()
285 static int playerL_getPosition( lua_State *L )
287 LuaVector v;
289 vectcpy( &v.vec, &player->solid->pos );
290 lua_pushvector(L, v);
291 return 1;
295 * @brief Gets the player's associated pilot.
297 * @luareturn The player's pilot.
298 * @luafunc pilot()
300 static int playerL_getPilot( lua_State *L )
302 LuaPilot lp;
303 lp.pilot = PLAYER_ID;
304 lua_pushpilot(L, lp);
305 return 1;
310 * @brief Gets the amount of fuel a player has.
312 * @usage fuel = player.fuel()
314 * @luareturn The player's fuel.
315 * @luafunc fuel()
317 static int playerL_fuel( lua_State *L )
319 lua_pushnumber(L,player->fuel);
320 return 1;
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 )
335 double f;
337 if (lua_gettop(L) > 0) {
338 f = luaL_checknumber(L,1);
339 player->fuel += f;
341 else
342 player->fuel = player->fuel_max;
344 /* Make sure value is sane. */
345 player->fuel = CLAMP(0, player->fuel_max, player->fuel);
347 return 0;
352 * @brief Unboards the player from it's boarded target.
354 * Use from inside a board hook.
356 * @usage player.unboard()
358 * @luafunc unboard()
360 static int playerL_unboard( lua_State *L )
362 (void) L;
363 board_unboard();
364 return 0;
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.
374 * @luafunc takeoff()
376 static int playerL_takeoff( lua_State *L )
378 (void) L;
380 if (landed)
381 landed = 0;
383 return 0;
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 )
399 const char *str;
400 Outfit *o;
401 int q;
403 /* Defaults. */
404 q = 1;
406 /* Handle parameters. */
407 str = luaL_checkstring(L, 1);
408 if (lua_gettop(L) > 1)
409 q = luaL_checkint(L, 2);
411 /* Get outfit. */
412 o = outfit_get( str );
413 if (o==NULL) {
414 NLUA_ERROR(L, "Outfit '%s' not found.", str);
415 return 0;
418 /* Add the outfits. */
419 player_addOutfit( o, q );
420 return 0;
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;
438 Ship *s;
440 /* Handle parameters. */
441 str = luaL_checkstring(L, 1);
442 name = luaL_checkstring(L, 2);
444 /* Get ship. */
445 s = ship_get(str);
446 if (s==NULL) {
447 NLUA_ERROR(L, "Ship '%s' not found.", str);
448 return 0;
451 /* Add the ship. */
452 player_newShip( s, 0., 0, 0., 0., 0., name );
453 return 0;
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 )
468 const char *str;
469 int id;
471 /* Handle parameters. */
472 str = luaL_checkstring(L, 1);
474 /* Get mission ID. */
475 id = mission_getID( str );
476 if (id == -1) {
477 NLUA_ERROR(L, "Mission '%s' not found in stack", str);
478 return 0;
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 )
496 const char *str;
497 int id;
499 /* Handle parameters. */
500 str = luaL_checkstring(L, 1);
502 /* Get event ID. */
503 id = event_dataID( str );
504 if (id == -1) {
505 NLUA_ERROR(L, "Event '%s' not found in stack", str);
506 return 0;
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 )
525 LuaSystem *sys;
527 /* Get a system. */
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. */
540 map_clear();
542 /* Add the escorts. */
543 player_addEscorts();
545 /* Run hooks - order is important. */
546 hooks_run( "jumpin" );
547 hooks_run( "enter" );
548 events_trigger( EVENT_TRIGGER_ENTER );
550 return 0;