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