naev 0.10.4
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.;
30int gatherable_load (void)
31{
33 return 0;
34}
35
36void gatherable_cleanup (void)
37{
39 gatherable_stack = NULL;
40}
41
52int gatherable_init( const Commodity* com, vec2 pos, vec2 vel, double lifeleng, int qtt, unsigned int player_only )
53{
55 g->type = com;
56 g->pos = pos;
57 g->vel = vel;
58 g->timer = 0.;
59 g->quantity = qtt;
60 g->sx = RNG( 0, com->gfx_space->sx );
61 g->sy = RNG( 0, com->gfx_space->sy );
62 g->player_only = player_only;
63
64 if (lifeleng < 0.)
65 g->lifeleng = RNGF()*100. + 50.;
66 else
67 g->lifeleng = lifeleng;
68
69 return g-gatherable_stack;
70}
71
77void gatherable_update( double dt )
78{
79 /* Update the timer for "full cargo" message. */
80 noscoop_timer += dt;
81
82 for (int i=0; i < array_size(gatherable_stack); i++) {
84 g->timer += dt;
85 g->pos.x += dt*gatherable_stack[i].vel.x;
86 g->pos.y += dt*gatherable_stack[i].vel.y;
87
88 /* Remove the gatherable */
89 if (g->timer > g->lifeleng) {
90 array_erase( &gatherable_stack, g, g+1 );
91 i--;
92 }
93 }
94}
95
99void gatherable_free( void )
100{
102}
103
108{
109 for (int i=0; i < array_size(gatherable_stack); i++) {
110 Gatherable *gat = &gatherable_stack[i];
111 gl_renderSprite( gat->type->gfx_space, gat->pos.x, gat->pos.y, gat->sx, gat->sy, NULL );
112 }
113}
114
122int gatherable_getClosest( vec2 pos, double rad )
123{
124 int curg = -1;
125 double mindist = INFINITY;
126
127 for (int i=0; i < array_size(gatherable_stack); i++) {
128 Gatherable *gat = &gatherable_stack[i];
129 double curdist = vec2_dist(&pos, &gat->pos);
130 if ( (curdist<mindist) && (curdist<rad) ) {
131 curg = i;
132 mindist = curdist;
133 }
134 }
135 return curg;
136}
137
146int gatherable_getPos( vec2* pos, vec2* vel, int id )
147{
148 Gatherable *gat;
149
150 if ((id < 0) || (id > array_size(gatherable_stack)-1) ) {
151 vectnull( pos );
152 vectnull( vel );
153 return 0;
154 }
155
156 gat = &gatherable_stack[id];
157 *pos = gat->pos;
158 *vel = gat->vel;
159
160 return 1;
161}
162
169{
170 for (int i=0; i < array_size(gatherable_stack); i++) {
171 Gatherable *gat = &gatherable_stack[i];
172
173 /* Only player can gather player only stuff. */
174 if (gat->player_only && !pilot_isPlayer(p))
175 continue;
176
177 if (vec2_dist( &p->solid->pos, &gat->pos ) < GATHER_DIST ) {
178 /* Add cargo to pilot. */
179 int q = pilot_cargoAdd( p, gat->type, gat->quantity, 0 );
180
181 if (q>0) {
182 if (pilot_isPlayer(p)) {
183 HookParam hparam[3];
184 player_message( n_("%d ton of %s gathered", "%d tons of %s gathered", q), q, _(gat->type->name) );
185
186 /* Run hooks. */
187 hparam[0].type = HOOK_PARAM_STRING;
188 hparam[0].u.str = gat->type->name;
189 hparam[1].type = HOOK_PARAM_NUMBER;
190 hparam[1].u.num = q;
191 hparam[2].type = HOOK_PARAM_SENTINEL;
192 hooks_runParam( "gather", hparam );
193 }
194
195 /* Remove the object from space. */
196 array_erase( &gatherable_stack, gat, gat+1 );
197
198 /* Test if there is still cargo space */
199 if ((pilot_cargoFree(p) < 1) && (pilot_isPlayer(p)))
200 player_message( _("No more cargo space available") );
201 }
202 else if ((pilot_isPlayer(p)) && (noscoop_timer > 2.)) {
203 noscoop_timer = 0.;
204 player_message( _("Cannot gather material: no more cargo space available") );
205 }
206 }
207 }
208}
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:146
void gatherable_free(void)
Frees all the gatherables.
Definition: gatherable.c:99
int gatherable_init(const Commodity *com, vec2 pos, vec2 vel, double lifeleng, int qtt, unsigned int player_only)
Initializes a gatherable object.
Definition: gatherable.c:52
void gatherable_gather(Pilot *p)
See if the pilot can gather anything.
Definition: gatherable.c:168
static Gatherable * gatherable_stack
Definition: gatherable.c:27
void gatherable_update(double dt)
Updates all gatherable objects.
Definition: gatherable.c:77
#define GATHER_DIST
Definition: gatherable.c:24
static float noscoop_timer
Definition: gatherable.c:28
int gatherable_getClosest(vec2 pos, double rad)
Gets the closest gatherable from a given position, within a given radius.
Definition: gatherable.c:122
void gatherable_render(void)
Renders all the gatherables.
Definition: gatherable.c:107
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
Header file with generic functions and naev-specifics.
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.
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.
Definition: pilot_cargo.c:169
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
vec2 pos
Definition: gatherable.h:16
double timer
Definition: gatherable.h:18
const Commodity * type
Definition: gatherable.h:15
unsigned int player_only
Definition: gatherable.h:23
int quantity
Definition: gatherable.h:20
vec2 vel
Definition: gatherable.h:17
The actual hook parameter.
Definition: hook.h:35
const char * str
Definition: hook.h:39
union HookParam::@4 u
HookParamType type
Definition: hook.h:36
double num
Definition: hook.h:38
The representation of an in-game pilot.
Definition: pilot.h:210
double sx
Definition: opengl_tex.h:42
double sy
Definition: opengl_tex.h:43
Represents a 2d vector.
Definition: vec2.h:32
double y
Definition: vec2.h:34
double x
Definition: vec2.h:33