naev 0.10.4
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 if (board_stopboard) {
74 board_boarded = 0;
75 return 0;
76 }
77
78 /* Set up environment first time. */
79 if (board_env == LUA_NOREF) {
80 board_env = nlua_newEnv();
82
83 size_t bufsize;
84 char *buf = ndata_read( BOARD_PATH, &bufsize );
85 if (nlua_dobufenv(board_env, buf, bufsize, BOARD_PATH) != 0) {
86 WARN( _("Error loading file: %s\n"
87 "%s\n"
88 "Most likely Lua file has improper syntax, please check"),
89 BOARD_PATH, lua_tostring(naevL,-1));
90 board_boarded = 0;
91 free(buf);
92 return -1;
93 }
94 free(buf);
95 }
96
97 /* Run Lua. */
98 nlua_getenv(naevL, board_env,"board");
99 lua_pushpilot(naevL, p->id);
100 if (nlua_pcall(board_env, 1, 0)) { /* error has occurred */
101 WARN( _("Board: '%s'"), lua_tostring(naevL,-1));
102 lua_pop(naevL,1);
103 }
104 board_boarded = 0;
105 return 0;
106}
107
113int player_tryBoard( int noisy )
114{
115 Pilot *p;
116 char c;
117
118 /* Not disabled. */
119 if (pilot_isDisabled(player.p))
120 return PLAYER_BOARD_IMPOSSIBLE;
121
122 if (player.p->target==PLAYER_ID) {
123 /* We don't try to find far away targets, only nearest and see if it matches.
124 * However, perhaps looking for first boardable target within a certain range
125 * could be more interesting. */
127 p = pilot_getTarget( player.p );
128 if ((p == NULL) ||
129 (!pilot_isDisabled(p) && !pilot_isFlag(p,PILOT_BOARDABLE)) ||
130 pilot_isFlag(p,PILOT_NOBOARD)) {
132 player_message( "#r%s", _("You need a target to board first!") );
133 return PLAYER_BOARD_IMPOSSIBLE;
134 }
135 }
136 else
137 p = pilot_getTarget( player.p );
139
140 /* More checks. */
141 if (pilot_isFlag(p,PILOT_NOBOARD)) {
142 player_message( "#r%s", _("Target ship can not be boarded.") );
143 return PLAYER_BOARD_IMPOSSIBLE;
144 }
145 else if (!pilot_isDisabled(p) && !pilot_isFlag(p,PILOT_BOARDABLE)) {
146 player_message( "#r%s", _("You cannot board a ship that isn't disabled!") );
147 return PLAYER_BOARD_IMPOSSIBLE;
148 }
149 else if (pilot_isFlag(p,PILOT_BOARDED)) {
150 player_message( "#r%s", _("Your target cannot be boarded again.") );
151 return PLAYER_BOARD_IMPOSSIBLE;
152 }
153 else if (vec2_dist(&player.p->solid->pos,&p->solid->pos) >
154 p->ship->gfx_space->sw * PILOT_SIZE_APPROX) {
155 if (noisy)
156 player_message( "#r%s", _("You are too far away to board your target.") );
157 return PLAYER_BOARD_RETRY;
158 }
159 else if (vec2_dist2( &player.p->solid->vel, &p->solid->vel ) > pow2(MAX_HYPERSPACE_VEL)) {
160 if (noisy)
161 player_message( "#r%s", _("You are going too fast to board the ship.") );
162 return PLAYER_BOARD_RETRY;
163 }
164
165 /* Handle fighters. */
166 if (pilot_isFlag(p, PILOT_CARRIED) && (p->dockpilot == PLAYER_ID)) {
167 if (pilot_dock( p, player.p )) {
168 WARN(_("Unable to recover fighter."));
169 return PLAYER_BOARD_IMPOSSIBLE;
170 }
171 player_message(_("#oYou recover your %s fighter."), p->name);
172 player.p->ptarget = NULL; /* Have to clear target cache. */
173 return PLAYER_BOARD_OK;
174 }
175
176 /* Set speed to target's speed. */
177 vec2_cset(&player.p->solid->vel, VX(p->solid->vel), VY(p->solid->vel));
178
179 /* Is boarded. */
180 board_boarded = 1;
181
182 /* Mark pilot as boarded only if it isn't being active boarded. */
183 if (!pilot_isFlag(p,PILOT_BOARDABLE))
184 pilot_setFlag(p,PILOT_BOARDED);
185 player_message(_("#oBoarding ship #%c%s#0."), c, p->name);
186
187 /* Don't unboard. */
188 board_stopboard = 0;
189
190 hook_addFunc( board_hook, p, "safe" );
191 return PLAYER_BOARD_OK;
192}
193
197void board_unboard (void)
198{
199 board_stopboard = 1;
200}
201
209{
210 Pilot *target;
211 HookParam hparam[2];
212
213 /* Make sure target is valid. */
214 target = pilot_getTarget( p );
215 if (target == NULL) {
216 DEBUG("NO TARGET");
217 return 0;
218 }
219
220 /* Check if can board. */
221 if (!pilot_isDisabled(target))
222 return 0;
223 else if (vec2_dist(&p->solid->pos, &target->solid->pos) >
224 target->ship->gfx_space->sw * PILOT_SIZE_APPROX )
225 return 0;
226 else if (vec2_dist2( &p->solid->vel, &target->solid->vel ) > pow2(MAX_HYPERSPACE_VEL))
227 return 0;
228 else if (pilot_isFlag(target,PILOT_BOARDED))
229 return 0;
230
231 /* Set speed to target's speed. */
232 vec2_cset(&p->solid->vel, VX(target->solid->vel), VY(target->solid->vel));
233
234 /* Set the boarding flag. */
235 pilot_setFlag(target, PILOT_BOARDED);
236 pilot_setFlag(p, PILOT_BOARDING);
237
238 /* Set time it takes to board. */
239 p->ptimer = 3.;
240
241 /* Run pilot board hook. */
242 hparam[0].type = HOOK_PARAM_PILOT;
243 hparam[0].u.lp = p->id;
244 hparam[1].type = HOOK_PARAM_SENTINEL;
245 pilot_runHookParam(target, PILOT_HOOK_BOARD_ALL, hparam, 1);
246 hparam[0].u.lp = target->id;
247 pilot_runHookParam(p, PILOT_HOOK_BOARDING, hparam, 1);
248
249 return 1;
250}
251
258{
259 /* Make sure target is valid. */
260 Pilot *target = pilot_getTarget( p );
261 if (target == NULL)
262 return;
263
264 /* In the case of the player take fewer credits. */
265 if (pilot_isPlayer(target)) {
266 char creds[ ECON_CRED_STRLEN ];
267 credits_t worth = MIN( 0.1*pilot_worth(target), target->credits );
268 p->credits += worth * p->stats.loot_mod;
269 target->credits -= worth;
270 credits2str( creds, worth, 2 );
272 _("#%c%s#0 has plundered %s from your ship!"),
273 pilot_getFactionColourChar(p), p->name, creds );
274 }
275 else {
276 /* Steal stuff, we only do credits for now. */
277 p->credits += target->credits * p->stats.loot_mod;
278 target->credits = 0.;
279 }
280
281 /* Finish the boarding. */
282 pilot_rmFlag(p, PILOT_BOARDING);
283}
Provides macros to work with dynamic arrays.
int pilot_board(Pilot *p)
Has a pilot attempt to board another pilot.
Definition: board.c:208
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:257
int player_tryBoard(int noisy)
Attempt to board the player's target.
Definition: board.c:113
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:197
void player_message(const char *fmt,...)
Adds a mesg to the queue to be displayed on screen.
Definition: gui.c:330
int hooks_runParam(const char *stack, const HookParam *param)
Runs all the hooks of stack.
Definition: hook.c:967
unsigned int hook_addFunc(int(*func)(void *), void *data, const char *stack)
Adds a function hook to be run.
Definition: hook.c:582
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:760
LuaPilot * lua_pushpilot(lua_State *L, LuaPilot pilot)
Pushes a pilot on the stack.
Definition: nlua_pilot.c:495
char pilot_getFactionColourChar(const Pilot *p)
Gets the faction colour char, works like faction_getColourChar but for a pilot.
Definition: pilot.c:1125
credits_t pilot_worth(const Pilot *p)
Gets the price or worth of a pilot in credits.
Definition: pilot.c:3917
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:1598
Pilot * pilot_getTarget(Pilot *p)
Gets the target of a pilot using a fancy caching system.
Definition: pilot.c:607
int pilot_runHookParam(Pilot *p, int hook_type, const HookParam *param, int nparam)
Tries to run a pilot hook if he has it.
Definition: pilot_hook.c:37
int pilot_dock(Pilot *p, Pilot *target)
Docks the pilot on its target pilot.
Definition: pilot_outfit.c:189
void player_targetNearest(void)
Player targets nearest pilot.
Definition: player.c:2275
Player_t player
Definition: player.c:73
void player_targetClear(void)
Clears the player's ship, spob or hyperspace target, in that order.
Definition: player.c:2198
static const double c[]
Definition: rng.c:264
The actual hook parameter.
Definition: hook.h:35
LuaPilot lp
Definition: hook.h:41
union HookParam::@4 u
HookParamType type
Definition: hook.h:36
The representation of an in-game pilot.
Definition: pilot.h:210
Solid * solid
Definition: pilot.h:220
unsigned int id
Definition: pilot.h:211
credits_t credits
Definition: pilot.h:317
const Ship * ship
Definition: pilot.h:219
void * ptarget
Definition: pilot.h:335
unsigned int target
Definition: pilot.h:334
Pilot * p
Definition: player.h:101
glTexture * gfx_space
Definition: ship.h:137
vec2 vel
Definition: physics.h:21
vec2 pos
Definition: physics.h:22
double sw
Definition: opengl_tex.h:44