forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatmos.cpp
378 lines (334 loc) · 9.42 KB
/
atmos.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2012 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file atmos.c
*
* Handles atmospherics such as snow and rain.
*/
#include "lib/framework/frame.h"
#include "lib/ivis_opengl/piematrix.h"
#include "atmos.h"
#include "display3d.h"
#include "effects.h"
#include "hci.h"
#include "loop.h"
#include "map.h"
#include "miscimd.h"
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Shift all this gubbins into a .h file if it makes it into game
// -----------------------------------------------------------------------------
/* Roughly one per tile */
#define MAX_ATMOS_PARTICLES (MAP_MAXWIDTH * MAP_MAXHEIGHT)
#define SNOW_SPEED_DRIFT (40 - rand()%80)
#define SNOW_SPEED_FALL (0-(rand()%40 + 80))
#define RAIN_SPEED_DRIFT (rand()%50)
#define RAIN_SPEED_FALL (0-((rand()%300) + 700))
#define TYPE_WATER 75
#define TYPE_LAND 76
enum AP_TYPE
{
AP_RAIN,
AP_SNOW
};
enum AP_STATUS
{
APS_ACTIVE,
APS_INACTIVE,
};
static ATPART asAtmosParts[MAX_ATMOS_PARTICLES];
static UDWORD freeParticle;
static UDWORD weather;
/* Setup all the particles */
void atmosInitSystem( void )
{
UDWORD i;
for(i=0; i<MAX_ATMOS_PARTICLES; i++)
{
/* None are being used initially */
asAtmosParts[i].status = APS_INACTIVE;
}
/* Start at the beginning */
freeParticle = 0;
/* No weather to start with */
weather = WT_NONE;
}
// -----------------------------------------------------------------------------
/* Makes a particle wrap around - if it goes off the grid, then it returns
on the other side - provided it's still on world... Which it should be */
static void testParticleWrap(ATPART *psPart)
{
/* Gone off left side */
if(psPart->position.x < player.p.x-world_coord(visibleTiles.x)/2)
{
psPart->position.x += world_coord(visibleTiles.x);
}
/* Gone off right side */
else if(psPart->position.x > (player.p.x + world_coord(visibleTiles.x)/2))
{
psPart->position.x -= world_coord(visibleTiles.x);
}
/* Gone off top */
if(psPart->position.z < player.p.z - world_coord(visibleTiles.y)/2)
{
psPart->position.z += world_coord(visibleTiles.y);
}
/* Gone off bottom */
else if(psPart->position.z > (player.p.z + world_coord(visibleTiles.y)/2))
{
psPart->position.z -= world_coord(visibleTiles.y);
}
}
// -----------------------------------------------------------------------------
/* Moves one of the particles */
static void processParticle(ATPART *psPart)
{
SDWORD groundHeight;
Vector3i pos;
UDWORD x,y;
MAPTILE *psTile;
/* Only move if the game isn't paused */
if(!gamePaused())
{
/* Move the particle - frame rate controlled */
psPart->position.x += graphicsTimeAdjustedIncrement(psPart->velocity.x);
psPart->position.y += graphicsTimeAdjustedIncrement(psPart->velocity.y);
psPart->position.z += graphicsTimeAdjustedIncrement(psPart->velocity.z);
/* Wrap it around if it's gone off grid... */
testParticleWrap(psPart);
/* If it's gone off the WORLD... */
if(psPart->position.x<0 || psPart->position.z<0 ||
psPart->position.x>((mapWidth-1)*TILE_UNITS) ||
psPart->position.z>((mapHeight-1)*TILE_UNITS) )
{
/* The kill it */
psPart->status = APS_INACTIVE;
return;
}
/* What height is the ground under it? Only do if low enough...*/
if(psPart->position.y < 255*ELEVATION_SCALE)
{
/* Get ground height */
groundHeight = map_Height(psPart->position.x, psPart->position.z);
/* Are we below ground? */
if ((int)psPart->position.y < groundHeight
|| psPart->position.y < 0.f)
{
/* Kill it and return */
psPart->status = APS_INACTIVE;
if(psPart->type == AP_RAIN)
{
x = map_coord(psPart->position.x);
y = map_coord(psPart->position.z);
psTile = mapTile(x,y);
if (terrainType(psTile) == TER_WATER && TEST_TILE_VISIBLE(selectedPlayer,psTile))
{
pos.x = psPart->position.x;
pos.z = psPart->position.z;
pos.y = groundHeight;
effectSetSize(60);
addEffect(&pos,EFFECT_EXPLOSION,EXPLOSION_TYPE_SPECIFIED,true,getImdFromIndex(MI_SPLASH),0);
}
}
return;
}
}
if(psPart->type == AP_SNOW)
{
if(rand()%30 == 1)
{
psPart->velocity.z = (float)SNOW_SPEED_DRIFT;
}
if(rand()%30 == 1)
{
psPart->velocity.x = (float)SNOW_SPEED_DRIFT;
}
}
}
}
// -----------------------------------------------------------------------------
/* Adds a particle to the system if it can */
static void atmosAddParticle(Vector3i *pos, AP_TYPE type)
{
UDWORD activeCount;
UDWORD i;
for(i=freeParticle,activeCount=0; (asAtmosParts[i].status==APS_ACTIVE)
&& activeCount<MAX_ATMOS_PARTICLES; i++)
{
activeCount++;
/* Check for wrap around */
if(i>= (MAX_ATMOS_PARTICLES-1))
{
/* Go back to the first one */
i = 0;
}
}
/* Check the list isn't just full of essential effects */
if(activeCount>=MAX_ATMOS_PARTICLES-1)
{
/* All of the particles active!?!? */
return;
}
else
{
freeParticle = i;
}
/* Record it's type */
asAtmosParts[freeParticle].type = (UBYTE)type;
/* Make it active */
asAtmosParts[freeParticle].status = APS_ACTIVE;
/* Setup the imd */
switch(type)
{
case AP_SNOW:
asAtmosParts[freeParticle].imd = getImdFromIndex(MI_SNOW);
asAtmosParts[freeParticle].size = 80;
break;
case AP_RAIN:
asAtmosParts[freeParticle].imd = getImdFromIndex(MI_RAIN);
asAtmosParts[freeParticle].size = 50;
break;
default:
break;
}
/* Setup position */
asAtmosParts[freeParticle].position.x = (float)pos->x;
asAtmosParts[freeParticle].position.y = (float)pos->y;
asAtmosParts[freeParticle].position.z = (float)pos->z;
/* Setup its velocity */
if(type == AP_RAIN)
{
asAtmosParts[freeParticle].velocity.x = (float)RAIN_SPEED_DRIFT;
asAtmosParts[freeParticle].velocity.y = (float)RAIN_SPEED_FALL;
asAtmosParts[freeParticle].velocity.z = (float)RAIN_SPEED_DRIFT;
}
else
{
asAtmosParts[freeParticle].velocity.x = (float)SNOW_SPEED_DRIFT;
asAtmosParts[freeParticle].velocity.y = (float)SNOW_SPEED_FALL;
asAtmosParts[freeParticle].velocity.z = (float)SNOW_SPEED_DRIFT;
}
}
// -----------------------------------------------------------------------------
/* Move the particles */
void atmosUpdateSystem( void )
{
UDWORD i;
UDWORD numberToAdd;
Vector3i pos;
// we don't want to do any of this while paused.
if(!gamePaused() && weather!=WT_NONE)
{
for(i = 0; i < MAX_ATMOS_PARTICLES; i++)
{
/* See if it's active */
if(asAtmosParts[i].status == APS_ACTIVE)
{
processParticle(&asAtmosParts[i]);
}
}
/* This bit below needs to go into a "precipitation function" */
numberToAdd = ((weather==WT_SNOWING) ? 2 : 4);
/* Temporary stuff - just adds a few particles! */
for(i=0; i<numberToAdd; i++)
{
pos.x = player.p.x;
pos.z = player.p.z;
pos.x += world_coord(rand()%visibleTiles.x-visibleTiles.x/2);
pos.z += world_coord(rand()%visibleTiles.x-visibleTiles.y/2);
pos.y = 1000;
/* If we've got one on the grid */
if(pos.x>0 && pos.z>0 &&
pos.x<(SDWORD)world_coord(mapWidth-1) &&
pos.z<(SDWORD)world_coord(mapHeight-1) )
{
/* On grid, so which particle shall we add? */
switch(weather)
{
case WT_SNOWING:
atmosAddParticle(&pos,AP_SNOW);
break;
case WT_RAINING:
atmosAddParticle(&pos,AP_RAIN);
break;
case WT_NONE:
break;
default:
break;
}
}
}
}
}
// -----------------------------------------------------------------------------
void atmosDrawParticles( void )
{
UDWORD i;
if(weather==WT_NONE)
{
return;
}
/* Traverse the list */
for(i=0; i<MAX_ATMOS_PARTICLES; i++)
{
/* Don't bother unless it's active */
if(asAtmosParts[i].status == APS_ACTIVE)
{
/* Is it on the grid */
if (clipXY(asAtmosParts[i].position.x, asAtmosParts[i].position.z))
{
renderParticle(&asAtmosParts[i]);
}
}
}
}
// -----------------------------------------------------------------------------
void renderParticle( ATPART *psPart )
{
Vector3i dv;
/* Transform it */
dv.x = psPart->position.x - player.p.x;
dv.y = psPart->position.y;
dv.z = -(psPart->position.z - player.p.z);
pie_MatBegin(); /* Push the current matrix */
pie_TRANSLATE(dv.x,dv.y,dv.z);
/* Make it face camera */
pie_MatRotY(-player.r.y);
pie_MatRotY(-player.r.x);
/* Scale it... */
pie_MatScale(psPart->size / 100.f);
/* Draw it... */
pie_Draw3DShape(psPart->imd, 0, 0, WZCOL_WHITE, 0, 0);
pie_MatEnd();
}
// -----------------------------------------------------------------------------
void atmosSetWeatherType(WT_CLASS type)
{
if(type == WT_NONE)
{
atmosInitSystem();
}
else
{
weather = type;
}
}
// -----------------------------------------------------------------------------
WT_CLASS atmosGetWeatherType( void )
{
return (WT_CLASS)weather;
}