naev 0.12.5
gatherable.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include "vec2.h"
11#include <stdio.h>
12
13#include "naev.h"
15
16#include "gatherable.h"
17
18#include "array.h"
19#include "hook.h"
20#include "player.h"
21#include "rng.h"
22
23/* Gatherables */
24#define GATHER_DIST 30.
25
26/* gatherables stack */
28 NULL;
29static float noscoop_timer = 1.;
31
32/* Prototypes. */
33static int gatherable_gather( Gatherable *got, Pilot *p );
34
40int gatherable_load( void )
41{
43 il_create( &gather_qtquery, 1 );
44 return 0;
45}
46
51{
53 gatherable_stack = NULL;
54 il_destroy( &gather_qtquery );
55}
56
68int gatherable_init( const Commodity *com, const vec2 *pos, const vec2 *vel,
69 double lifeleng, int qtt, unsigned int player_only )
70{
72 memset( g, 0, sizeof( Gatherable ) );
73 g->type = com;
74 g->pos = *pos;
75 g->vel = *vel;
76 g->timer = 0.;
77 g->quantity = qtt;
78 g->sx = RNG( 0, com->gfx_space->sx - 1 );
79 g->sy = RNG( 0, com->gfx_space->sy - 1 );
80 g->player_only = player_only;
81
82 if ( lifeleng < 0. )
83 g->lifeleng = RNGF() * 100. + 50.;
84 else
85 g->lifeleng = lifeleng;
86
87 return g - gatherable_stack;
88}
89
95void gatherable_update( double dt )
96{
97 Pilot *const *pilot_stack = pilot_getAll();
98
99 /* Update the timer for "full cargo" message. */
100 noscoop_timer += dt;
101
102 for ( int i = array_size( gatherable_stack ) - 1; i >= 0; i-- ) {
104 const int r = ceil( GATHER_DIST );
105 int x, y;
106
107 g->timer += dt;
108 g->pos.x += dt * g->vel.x;
109 g->pos.y += dt * g->vel.y;
110
111 /* Remove the gatherable */
112 if ( g->timer > g->lifeleng ) {
113 array_erase( &gatherable_stack, g, g + 1 );
114 continue;
115 }
116
117 /* Only player can gather player only stuff. */
118 if ( g->player_only ) {
119 if ( ( player.p != NULL ) &&
120 vec2_dist2( &player.p->solid.pos, &g->pos ) <=
121 pow2( GATHER_DIST ) )
123 continue;
124 }
125
126 /* Check if can be picked up. */
127 x = round( g->pos.x );
128 y = round( g->pos.y );
129 pilot_collideQueryIL( &gather_qtquery, x - r, y - r, x + r, y + r );
130 for ( int j = 0; j < il_size( &gather_qtquery ); j++ ) {
131 Pilot *p = pilot_stack[il_get( &gather_qtquery, j, 0 )];
132
133 /* See if in distance. */
134 if ( vec2_dist2( &p->solid.pos, &g->pos ) > pow2( GATHER_DIST ) )
135 continue;
136
137 /* Try to pick up. */
138 if ( gatherable_gather( g, p ) )
139 break;
140 }
141 }
142}
143
152
157{
158 for ( int i = 0; i < array_size( gatherable_stack ); i++ ) {
159 const Gatherable *gat = &gatherable_stack[i];
160 gl_renderSprite( gat->type->gfx_space, gat->pos.x, gat->pos.y, gat->sx,
161 gat->sy, NULL );
162 }
163}
164
173int gatherable_getClosest( const vec2 *pos, double rad )
174{
175 int curg = -1;
176 double mindist = INFINITY;
177
178 for ( int i = 0; i < array_size( gatherable_stack ); i++ ) {
179 Gatherable *gat = &gatherable_stack[i];
180 double curdist = vec2_dist( pos, &gat->pos );
181 if ( ( curdist < mindist ) && ( curdist < rad ) ) {
182 curg = i;
183 mindist = curdist;
184 }
185 }
186 return curg;
187}
188
197int gatherable_getPos( vec2 *pos, vec2 *vel, int id )
198{
199 Gatherable *gat;
200
201 if ( ( id < 0 ) || ( id > array_size( gatherable_stack ) - 1 ) ) {
202 vectnull( pos );
203 vectnull( vel );
204 return 0;
205 }
206
207 gat = &gatherable_stack[id];
208 *pos = gat->pos;
209 *vel = gat->vel;
210
211 return 1;
212}
213
220static int gatherable_gather( Gatherable *gat, Pilot *p )
221{
222 int q;
223
224 /* Must not be dead. */
225 if ( pilot_isFlag( p, PILOT_DELETE ) || pilot_isFlag( p, PILOT_DEAD ) )
226 return 0;
227
228 /* Must not be hidden nor invisible. */
229 if ( pilot_isFlag( p, PILOT_HIDE ) || pilot_isFlag( p, PILOT_INVISIBLE ) )
230 return 0;
231
232 /* Disabled pilots can't pick up stuff. */
233 if ( pilot_isDisabled( p ) )
234 return 0;
235
236 /* Try to add cargo to pilot. */
237 q = pilot_cargoAdd( p, gat->type, gat->quantity, 0 );
238
239 if ( q > 0 ) {
240 if ( pilot_isPlayer( p ) ) {
241 HookParam hparam[3];
243 n_( "%d ton of %s gathered", "%d tons of %s gathered", q ), q,
244 _( gat->type->name ) );
245
246 /* Run hooks. */
247 hparam[0].type = HOOK_PARAM_COMMODITY;
248 hparam[0].u.commodity = (Commodity *)gat->type; /* TODO not cast. */
249 hparam[1].type = HOOK_PARAM_NUMBER;
250 hparam[1].u.num = q;
251 hparam[2].type = HOOK_PARAM_SENTINEL;
252 hooks_runParam( "gather", hparam );
253 }
254
255 /* Remove the object from space. */
256 array_erase( &gatherable_stack, gat, gat + 1 );
257
258 /* Test if there is still cargo space */
259 if ( ( pilot_cargoFree( p ) < 1 ) && ( pilot_isPlayer( p ) ) )
260 player_message( _( "No more cargo space available" ) );
261 return 1;
262 } else if ( ( pilot_isPlayer( p ) ) && ( noscoop_timer > 2. ) ) {
263 noscoop_timer = 0.;
265 _( "Cannot gather material: no more cargo space available" ) );
266 }
267 return 0;
268}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition array.h:170
#define array_end(array)
Returns a pointer to the end of the reserved memory space.
Definition array.h:214
#define array_erase(ptr_array, first, last)
Erases elements in interval [first, last).
Definition array.h:148
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:179
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
Definition array.h:122
#define array_begin(array)
Returns a pointer to the beginning of the reserved memory space.
Definition array.h:206
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
Definition array.h:93
int gatherable_getPos(vec2 *pos, vec2 *vel, int id)
Returns the position and velocity of a gatherable.
Definition gatherable.c:197
void gatherable_free(void)
Frees all the gatherables.
Definition gatherable.c:147
int gatherable_init(const Commodity *com, const vec2 *pos, const vec2 *vel, double lifeleng, int qtt, unsigned int player_only)
Initializes a gatherable object.
Definition gatherable.c:68
int gatherable_getClosest(const vec2 *pos, double rad)
Gets the closest gatherable from a given position, within a given radius.
Definition gatherable.c:173
int gatherable_load(void)
Loads the gatherable system.
Definition gatherable.c:40
void gatherable_cleanup(void)
Cleans up after the gatherable system.
Definition gatherable.c:50
static IntList gather_qtquery
Definition gatherable.c:30
static Gatherable * gatherable_stack
Definition gatherable.c:27
static int gatherable_gather(Gatherable *got, Pilot *p)
See if the pilot can gather anything.
Definition gatherable.c:220
void gatherable_update(double dt)
Updates all gatherable objects.
Definition gatherable.c:95
#define GATHER_DIST
Definition gatherable.c:24
static float noscoop_timer
Definition gatherable.c:29
void gatherable_render(void)
Renders all the gatherables.
Definition gatherable.c:156
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
Header file with generic functions and naev-specifics.
#define pow2(x)
Definition naev.h:53
void gl_renderSprite(const glTexture *sprite, double bx, double by, int sx, int sy, const glColour *c)
Blits a sprite, position is relative to the player.
static Pilot ** pilot_stack
Definition pilot.c:51
Pilot *const * pilot_getAll(void)
Gets the pilot stack.
Definition pilot.c:93
int pilot_cargoFree(const Pilot *p)
Gets the pilot's free cargo space.
Definition pilot_cargo.c:53
int pilot_cargoAdd(Pilot *pilot, const Commodity *cargo, int quantity, unsigned int id)
Tries to add quantity of cargo to pilot.
Player_t player
Definition player.c:77
Represents a commodity.
Definition commodity.h:57
char * name
Definition commodity.h:58
glTexture * gfx_space
Definition commodity.h:69
Represents stuff that can be gathered.
Definition gatherable.h:14
const Commodity * type
Definition gatherable.h:15
The actual hook parameter.
Definition hook.h:40
HookParamType type
Definition hook.h:41
Commodity * commodity
Definition hook.h:49
union HookParam::@325330313164266255110350307176363262300250041145 u
double num
Definition hook.h:43
The representation of an in-game pilot.
Definition pilot.h:263
double sx
Definition opengl_tex.h:51
double sy
Definition opengl_tex.h:52
Represents a 2d vector.
Definition vec2.h:45
double y
Definition vec2.h:47
double x
Definition vec2.h:46