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