naev 0.11.5
board.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include "naev.h"
13#include "board.h"
14
15#include "array.h"
16#include "commodity.h"
17#include "damagetype.h"
18#include "hook.h"
19#include "log.h"
20#include "nstring.h"
21#include "ndata.h"
22#include "nlua.h"
23#include "pilot.h"
24#include "player.h"
25#include "rng.h"
26#include "space.h"
27#include "toolkit.h"
28
29#define BOARDING_WIDTH 380
30#define BOARDING_HEIGHT 200
32#define BUTTON_WIDTH 50
33#define BUTTON_HEIGHT 30
35static int board_stopboard = 0;
36static int board_boarded = 0;
37static nlua_env board_env = LUA_NOREF;
43{
44 return board_boarded;
45}
46
47int board_hook( void *data )
48{
49 HookParam hparam[2];
50 Pilot *p = (Pilot*) data;
51
52 /* Don't allow boarding own escorts. */
53 if (pilot_isWithPlayer( p )) {
54 p->stress = 0.;
55 p->dtimer_accum = 0;
57 player_message(_("#gYou reactivate the systems of %s."), p->name);
58 return 0;
59 }
60
61 /*
62 * run hook if needed
63 */
64 hparam[0].type = HOOK_PARAM_PILOT;
65 hparam[0].u.lp = PLAYER_ID;
66 hparam[1].type = HOOK_PARAM_SENTINEL;
67 pilot_runHookParam(p, PILOT_HOOK_BOARD, hparam, 1);
68 pilot_runHookParam(p, PILOT_HOOK_BOARD_ALL, hparam, 1);
69 hparam[0].u.lp = p->id;
70 hooks_runParam( "board", hparam );
71 pilot_runHookParam(player.p, PILOT_HOOK_BOARDING, hparam, 1);
72
73 /* Run outfit stuff. */
75
76 if (board_stopboard) {
77 board_boarded = 0;
78 return 0;
79 }
80
81 /* Set up environment first time. */
82 if (board_env == LUA_NOREF) {
83 board_env = nlua_newEnv();
85
86 size_t bufsize;
87 char *buf = ndata_read( BOARD_PATH, &bufsize );
88 if (nlua_dobufenv(board_env, buf, bufsize, BOARD_PATH) != 0) {
89 WARN( _("Error loading file: %s\n"
90 "%s\n"
91 "Most likely Lua file has improper syntax, please check"),
92 BOARD_PATH, lua_tostring(naevL,-1));
93 board_boarded = 0;
94 free(buf);
95 return -1;
96 }
97 free(buf);
98 }
99
100 /* Run Lua. */
101 nlua_getenv(naevL, board_env,"board");
102 lua_pushpilot(naevL, p->id);
103 if (nlua_pcall(board_env, 1, 0)) { /* error has occurred */
104 WARN( _("Board: '%s'"), lua_tostring(naevL,-1));
105 lua_pop(naevL,1);
106 }
107 board_boarded = 0;
108 return 0;
109}
110
114int player_canBoard( int noisy )
115{
116 Pilot *p;
117
118 /* Not disabled. */
119 if (pilot_isDisabled(player.p))
120 return PLAYER_BOARD_IMPOSSIBLE;
121
122 /* Can't board if no pilot. */
123 if (player.p->target==PLAYER_ID)
124 return PLAYER_BOARD_IMPOSSIBLE;
125 p = pilot_getTarget( player.p );
126
127 /* More checks. */
128 if (pilot_isFlag(p,PILOT_NOBOARD)) {
129 if (noisy)
130 player_message( "#r%s", _("Target ship can not be boarded.") );
131 return PLAYER_BOARD_IMPOSSIBLE;
132 }
133 else if (!pilot_isDisabled(p) && !pilot_isFlag(p,PILOT_BOARDABLE)) {
134 if (noisy)
135 player_message( "#r%s", _("You cannot board a ship that isn't disabled!") );
136 return PLAYER_BOARD_IMPOSSIBLE;
137 }
138 else if (pilot_isFlag(p,PILOT_BOARDED)) {
139 if (noisy)
140 player_message( "#r%s", _("Your target cannot be boarded again.") );
141 return PLAYER_BOARD_IMPOSSIBLE;
142 }
143
144 return PLAYER_BOARD_OK;
145}
146
152int player_tryBoard( int noisy )
153{
154 Pilot *p;
155 char c;
156
157 if (player_canBoard( noisy )==PLAYER_BOARD_IMPOSSIBLE)
158 return PLAYER_BOARD_IMPOSSIBLE;
159
160 /* Should have a pilot target by now or failed. */
161 p = pilot_getTarget( player.p );
163
164 if (vec2_dist(&player.p->solid.pos,&p->solid.pos) >
165 p->ship->gfx_space->sw * PILOT_SIZE_APPROX) {
166 if (noisy)
167 player_message( "#r%s", _("You are too far away to board your target.") );
168 return PLAYER_BOARD_RETRY;
169 }
170 else if (vec2_dist2( &player.p->solid.vel, &p->solid.vel ) > pow2(MAX_HYPERSPACE_VEL)) {
171 if (noisy)
172 player_message( "#r%s", _("You are going too fast to board the ship.") );
173 return PLAYER_BOARD_RETRY;
174 }
175
176 /* Handle fighters. */
177 if (pilot_isFlag(p, PILOT_CARRIED) && (p->dockpilot == PLAYER_ID)) {
178 if (pilot_dock( p, player.p )) {
179 WARN(_("Unable to recover fighter."));
180 return PLAYER_BOARD_IMPOSSIBLE;
181 }
182 player_message(_("#oYou recover your %s fighter."), p->name);
183 player.p->ptarget = NULL; /* Have to clear target cache. */
184 return PLAYER_BOARD_OK;
185 }
186
187 /* Set speed to target's speed. */
188 vec2_cset(&player.p->solid.vel, VX(p->solid.vel), VY(p->solid.vel));
189
190 /* Is boarded. */
191 board_boarded = 1;
192
193 /* Mark pilot as boarded only if it isn't being active boarded. */
194 if (!pilot_isFlag(p,PILOT_BOARDABLE))
195 pilot_setFlag(p,PILOT_BOARDED);
196 player_message(_("#oBoarding ship #%c%s#0."), c, p->name);
197
198 /* Don't unboard. */
199 board_stopboard = 0;
200
201 hook_addFunc( board_hook, p, "safe" );
202 return PLAYER_BOARD_OK;
203}
204
208void board_unboard (void)
209{
210 board_stopboard = 1;
211}
212
220{
221 Pilot *target;
222 HookParam hparam[2];
223
224 /* Make sure target is valid. */
225 target = pilot_getTarget( p );
226 if (target == NULL) {
227 DEBUG("NO TARGET");
228 return 0;
229 }
230
231 /* Check if can board. */
232 if (!pilot_isDisabled(target))
233 return 0;
234 else if (vec2_dist(&p->solid.pos, &target->solid.pos) >
235 target->ship->gfx_space->sw * PILOT_SIZE_APPROX )
236 return 0;
237 else if (vec2_dist2( &p->solid.vel, &target->solid.vel ) > pow2(MAX_HYPERSPACE_VEL))
238 return 0;
239 else if (pilot_isFlag(target,PILOT_BOARDED))
240 return 0;
241
242 /* Set speed to target's speed. */
243 vec2_cset(&p->solid.vel, VX(target->solid.vel), VY(target->solid.vel));
244
245 /* Set the boarding flag. */
246 pilot_setFlag(target, PILOT_BOARDED);
247 pilot_setFlag(p, PILOT_BOARDING);
248
249 /* Set time it takes to board. */
250 p->ptimer = 3.;
251
252 /* Run pilot board hook. */
253 hparam[0].type = HOOK_PARAM_PILOT;
254 hparam[0].u.lp = p->id;
255 hparam[1].type = HOOK_PARAM_SENTINEL;
256 pilot_runHookParam(target, PILOT_HOOK_BOARD_ALL, hparam, 1);
257 hparam[0].u.lp = target->id;
258 pilot_runHookParam(p, PILOT_HOOK_BOARDING, hparam, 1);
259
260 /* Run outfit stuff. */
261 pilot_outfitLOnboard( p, target );
262
263 return 1;
264}
265
272{
273 /* Make sure target is valid. */
274 Pilot *target = pilot_getTarget( p );
275 if (target == NULL)
276 return;
277
278 /* In the case of the player take fewer credits. */
279 if (pilot_isPlayer(target)) {
280 char creds[ ECON_CRED_STRLEN ];
281 credits_t worth = MIN( 0.1*pilot_worth(target,0), target->credits );
282 p->credits += worth * p->stats.loot_mod;
283 target->credits -= worth;
284 credits2str( creds, worth, 2 );
286 _("#%c%s#0 has plundered %s from your ship!"),
287 pilot_getFactionColourChar(p), p->name, creds );
288 }
289 else {
290 /* Steal stuff, we only do credits for now. */
291 p->credits += target->credits * p->stats.loot_mod;
292 target->credits = 0.;
293 }
294
295 /* Finish the boarding. */
296 pilot_rmFlag(p, PILOT_BOARDING);
297}
Provides macros to work with dynamic arrays.
int player_canBoard(int noisy)
Sees if the pilot can board a pilot.
Definition board.c:114
int pilot_board(Pilot *p)
Has a pilot attempt to board another pilot.
Definition board.c:219
static nlua_env board_env
Definition board.c:37
static int board_stopboard
Definition board.c:35
static int board_boarded
Definition board.c:36
void pilot_boardComplete(Pilot *p)
Finishes the boarding.
Definition board.c:271
int player_tryBoard(int noisy)
Attempt to board the player's target.
Definition board.c:152
int player_isBoarded(void)
Gets if the player is boarded.
Definition board.c:42
void board_unboard(void)
Forces unboarding of the pilot.
Definition board.c:208
void credits2str(char *str, credits_t credits, int decimals)
Converts credits to a usable string for displaying.
Definition commodity.c:59
void player_message(const char *fmt,...)
Adds a mesg to the queue to be displayed on screen.
Definition gui.c:335
int hooks_runParam(const char *stack, const HookParam *param)
Runs all the hooks of stack.
Definition hook.c:979
unsigned int hook_addFunc(int(*func)(void *), void *data, const char *stack)
Adds a function hook to be run.
Definition hook.c:594
Header file with generic functions and naev-specifics.
#define MIN(x, y)
Definition naev.h:40
#define pow2(x)
Definition naev.h:46
void * ndata_read(const char *path, size_t *filesize)
Reads a file from the ndata (will be NUL terminated).
Definition ndata.c:154
int nlua_loadStandard(nlua_env env)
Loads the standard Naev Lua API.
Definition nlua.c:798
LuaPilot * lua_pushpilot(lua_State *L, LuaPilot pilot)
Pushes a pilot on the stack.
Definition nlua_pilot.c:563
char pilot_getFactionColourChar(const Pilot *p)
Gets the faction colour char, works like faction_getColourChar but for a pilot.
Definition pilot.c:1080
void pilot_updateDisable(Pilot *p, unsigned int shooter)
Handles pilot disabling. Set or unset the disable status depending on health and stress values.
Definition pilot.c:1550
Pilot * pilot_getTarget(Pilot *p)
Gets the target of a pilot using a fancy caching system.
Definition pilot.c:634
credits_t pilot_worth(const Pilot *p, int count_unique)
Gets the price or worth of a pilot in credits.
Definition pilot.c:4096
void pilot_outfitLOnboard(Pilot *pilot, const Pilot *target)
Runs Lua outfits when pilot boards a target.
Player_t player
Definition player.c:74
static const double c[]
Definition rng.c:264
The actual hook parameter.
Definition hook.h:38
LuaPilot lp
Definition hook.h:44
HookParamType type
Definition hook.h:39
union HookParam::@25 u
The representation of an in-game pilot.
Definition pilot.h:217
unsigned int id
Definition pilot.h:218
credits_t credits
Definition pilot.h:325
const Ship * ship
Definition pilot.h:226
Solid solid
Definition pilot.h:227
void * ptarget
Definition pilot.h:343
unsigned int target
Definition pilot.h:342
Pilot * p
Definition player.h:101
glTexture * gfx_space
Definition ship.h:138
vec2 vel
Definition physics.h:48
vec2 pos
Definition physics.h:49
double sw
Definition opengl_tex.h:46