2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
25 extern float viewdistance;
26 extern float fadestart;
27 extern int environment;
28 extern float texscale;
30 extern float multiplier;
32 extern Terrain terrain;
33 extern Objects objects;
35 extern XYZ viewerfacing;
36 extern int bloodtoggle;
37 extern XYZ windvector;
40 Texture Sprite::cloudtexture;
41 Texture Sprite::cloudimpacttexture;
42 Texture Sprite::bloodtexture;
43 Texture Sprite::flametexture;
44 Texture Sprite::bloodflametexture;
45 Texture Sprite::smoketexture;
46 Texture Sprite::snowflaketexture;
47 Texture Sprite::shinetexture;
48 Texture Sprite::splintertexture;
49 Texture Sprite::leaftexture;
50 Texture Sprite::toothtexture;
52 float Sprite::checkdelay = 0;
54 vector<Sprite*> Sprite::sprites = vector<Sprite*>();
62 static float distancemult;
64 static int lastspecial;
65 static int whichpatchx, whichpatchz;
66 static XYZ start, end, colpoint;
69 static float tempmult;
70 static XYZ difference;
71 static float lightcolor[3];
72 static float viewdistsquared = viewdistance * viewdistance;
73 static XYZ tempviewer;
75 tempviewer = viewer + viewerfacing * 6;
78 lightcolor[0] = light.color[0] * .5 + light.ambient[0];
79 lightcolor[1] = light.color[1] * .5 + light.ambient[1];
80 lightcolor[2] = light.color[2] * .5 + light.ambient[2];
82 checkdelay -= multiplier * 10;
84 if (checkdelay <= 0) {
92 glDisable(GL_LIGHTING);
93 glDisable(GL_CULL_FACE);
94 glEnable(GL_TEXTURE_2D);
96 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
98 glAlphaFunc(GL_GREATER, 0.0001);
99 for (unsigned i = 0; i < sprites.size(); i++) {
100 if (lasttype != sprites[i]->type) {
101 switch (sprites[i]->type) {
106 glAlphaFunc(GL_GREATER, 0.0001);
107 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
111 case cloudimpactsprite:
112 cloudimpacttexture.bind();
115 glAlphaFunc(GL_GREATER, 0.0001);
116 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
123 glAlphaFunc(GL_GREATER, 0.0001);
124 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
131 glAlphaFunc(GL_GREATER, 0.0001);
132 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
135 case splintersprite :
136 if (lastspecial != sprites[i]->special) {
137 if (sprites[i]->special == 0)
138 splintertexture.bind();
139 if (sprites[i]->special == 1)
141 if (sprites[i]->special == 2)
142 snowflaketexture.bind();
143 if (sprites[i]->special == 3)
147 glAlphaFunc(GL_GREATER, 0.0001);
148 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
153 snowflaketexture.bind();
156 glAlphaFunc(GL_GREATER, 0.0001);
157 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
160 case weaponshinesprite:
164 glAlphaFunc(GL_GREATER, 0.001);
165 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
169 case weaponflamesprite:
171 if (blend || lasttype == bloodflamesprite) {
173 glAlphaFunc(GL_GREATER, 0.3);
174 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
177 case bloodflamesprite:
178 bloodflametexture.bind();
181 glAlphaFunc(GL_GREATER, 0.3);
182 glBlendFunc(GL_ONE, GL_ZERO);
187 if (sprites[i]->type == snowsprite)
188 distancemult = (144 - (distsq(&tempviewer, &sprites[i]->position) - (144 * fadestart)) * (1 / (1 - fadestart))) / 144;
190 distancemult = (viewdistsquared - (distsq(&viewer, &sprites[i]->position) - (viewdistsquared * fadestart)) * (1 / (1 - fadestart))) / viewdistsquared;
191 if (sprites[i]->type == flamesprite) {
192 if (distancemult >= 1)
193 glColor4f(sprites[i]->color[0], sprites[i]->color[1], sprites[i]->color[2], sprites[i]->opacity);
195 glColor4f(sprites[i]->color[0], sprites[i]->color[1], sprites[i]->color[2], sprites[i]->opacity * distancemult);
197 if (distancemult >= 1)
198 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], sprites[i]->opacity);
200 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], sprites[i]->opacity * distancemult);
202 lasttype = sprites[i]->type;
203 lastspecial = sprites[i]->special;
204 glMatrixMode(GL_MODELVIEW);
206 glTranslatef(sprites[i]->position.x, sprites[i]->position.y, sprites[i]->position.z);
207 if ((sprites[i]->type == flamesprite || sprites[i]->type == weaponflamesprite || sprites[i]->type == weaponshinesprite)) {
208 difference = viewer - sprites[i]->position;
209 Normalise(&difference);
210 glTranslatef(difference.x * sprites[i]->size / 4, difference.y * sprites[i]->size / 4, difference.z * sprites[i]->size / 4);
212 if (sprites[i]->type == snowsprite) {
213 glRotatef(sprites[i]->rotation * .2, 0, .3, 1);
214 glTranslatef(1, 0, 0);
216 glGetFloatv(GL_MODELVIEW_MATRIX, M);
221 glTranslatef(point.x, point.y, point.z);
223 glRotatef(sprites[i]->rotation, 0, 0, 1);
225 if ((sprites[i]->type == flamesprite || sprites[i]->type == weaponflamesprite || sprites[i]->type == weaponshinesprite || sprites[i]->type == bloodflamesprite)) {
226 if (sprites[i]->alivetime < .14)
227 glScalef(sprites[i]->alivetime / .14, sprites[i]->alivetime / .14, sprites[i]->alivetime / .14);
229 if (sprites[i]->type == smoketype || sprites[i]->type == snowsprite || sprites[i]->type == weaponshinesprite || sprites[i]->type == breathsprite) {
230 if (sprites[i]->alivetime < .3) {
231 if (distancemult >= 1)
232 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], sprites[i]->opacity * sprites[i]->alivetime / .3);
233 if (distancemult < 1)
234 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], sprites[i]->opacity * distancemult * sprites[i]->alivetime / .3);
237 if (sprites[i]->type == splintersprite && sprites[i]->special > 0 && sprites[i]->special != 3) {
238 if (sprites[i]->alivetime < .2) {
239 if (distancemult >= 1)
240 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], sprites[i]->alivetime / .2);
242 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], distancemult * sprites[i]->alivetime / .2);
244 if (distancemult >= 1)
245 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], 1);
247 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], 1);
250 if (sprites[i]->type == splintersprite && (sprites[i]->special == 0 || sprites[i]->special == 3)) {
251 if (distancemult >= 1)
252 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], 1);
254 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], 1);
257 glBegin(GL_TRIANGLES);
258 glTexCoord2f(1.0f, 1.0f);
259 glVertex3f( .5 * sprites[i]->size, .5 * sprites[i]->size, 0.0f);
260 glTexCoord2f(0.0f, 1.0f);
261 glVertex3f(-.5 * sprites[i]->size, .5 * sprites[i]->size, 0.0f);
262 glTexCoord2f(1.0f, 0.0f);
263 glVertex3f( .5 * sprites[i]->size, -.5 * sprites[i]->size, 0.0f);
264 glTexCoord2f(0.0f, 0.0f);
265 glVertex3f(-.5 * sprites[i]->size, -.5 * sprites[i]->size, 0.0f);
266 glTexCoord2f(1.0f, 0.0f);
267 glVertex3f( .5 * sprites[i]->size, -.5 * sprites[i]->size, 0.0f);
268 glTexCoord2f(0.0f, 1.0f);
269 glVertex3f(-.5 * sprites[i]->size, .5 * sprites[i]->size, 0.0f);
273 tempmult = multiplier;
274 for (int i = sprites.size() - 1; i >= 0; i--) {
275 multiplier = tempmult;
276 if (sprites[i]->type != snowsprite) {
277 sprites[i]->position += sprites[i]->velocity * multiplier;
278 sprites[i]->velocity += windvector * multiplier;
280 if (sprites[i]->type == flamesprite || sprites[i]->type == smoketype)
281 sprites[i]->position += windvector * multiplier / 2;
282 if ((sprites[i]->type == flamesprite || sprites[i]->type == weaponflamesprite || sprites[i]->type == weaponshinesprite || sprites[i]->type == bloodflamesprite))
283 multiplier *= sprites[i]->speed * .7;
284 sprites[i]->alivetime += multiplier;
286 if (sprites[i]->type == cloudsprite || sprites[i]->type == cloudimpactsprite) {
287 sprites[i]->opacity -= multiplier / 2;
288 sprites[i]->size += multiplier / 2;
289 sprites[i]->velocity.y += gravity * multiplier * .25;
291 if (sprites[i]->type == breathsprite) {
292 sprites[i]->opacity -= multiplier / 2;
293 sprites[i]->size += multiplier / 2;
294 if (findLength(&sprites[i]->velocity) <= multiplier) {
295 sprites[i]->velocity = 0;
298 slowdown = sprites[i]->velocity * -1;
299 Normalise(&slowdown);
300 slowdown *= multiplier;
301 sprites[i]->velocity += slowdown;
304 if (sprites[i]->type == snowsprite) {
305 sprites[i]->size -= multiplier / 120;
306 sprites[i]->rotation += multiplier * 360;
307 sprites[i]->position.y -= multiplier;
308 sprites[i]->position += windvector * multiplier;
309 if (sprites[i]->position.y < tempviewer.y - 6) sprites[i]->position.y += 12;
310 if (sprites[i]->position.y > tempviewer.y + 6) sprites[i]->position.y -= 12;
311 if (sprites[i]->position.z < tempviewer.z - 6) sprites[i]->position.z += 12;
312 if (sprites[i]->position.z > tempviewer.z + 6) sprites[i]->position.z -= 12;
313 if (sprites[i]->position.x < tempviewer.x - 6) sprites[i]->position.x += 12;
314 if (sprites[i]->position.x > tempviewer.x + 6) sprites[i]->position.x -= 12;
316 if (sprites[i]->type == bloodsprite) {
318 sprites[i]->rotation += multiplier * 100;
319 sprites[i]->velocity.y += gravity * multiplier;
321 XYZ where, startpoint, endpoint, movepoint, footpoint;
325 for (unsigned j = 0; j < Person::players.size(); j++) {
326 if (!spritehit && Person::players[j]->dead && sprites[i]->alivetime > .1) {
327 where = sprites[i]->oldposition;
328 where -= Person::players[j]->coords;
329 if (!Person::players[j]->skeleton.free)
330 where = DoRotation(where, 0, -Person::players[j]->yaw, 0);
332 where = sprites[i]->position;
333 where -= Person::players[j]->coords;
334 if (!Person::players[j]->skeleton.free)
335 where = DoRotation(where, 0, -Person::players[j]->yaw, 0);
340 whichtri = Person::players[j]->skeleton.drawmodel.LineCheck(&startpoint, &endpoint, &footpoint, &movepoint, &rotationpoint);
341 if (whichtri != -1) {
343 Person::players[j]->DoBloodBigWhere(0, 160, sprites[i]->oldposition);
349 whichpatchx = sprites[i]->position.x / (terrain.size / subdivision * terrain.scale);
350 whichpatchz = sprites[i]->position.z / (terrain.size / subdivision * terrain.scale);
351 if (whichpatchx > 0 && whichpatchz > 0 && whichpatchx < subdivision && whichpatchz < subdivision)
352 if (terrain.patchobjectnum[whichpatchx][whichpatchz]) {
354 for (int j = 0; j < terrain.patchobjectnum[whichpatchx][whichpatchz]; j++) {
355 k = terrain.patchobjects[whichpatchx][whichpatchz][j];
356 start = sprites[i]->oldposition;
357 end = sprites[i]->position;
359 if (objects.model[k].LineCheck(&start, &end, &colpoint, &objects.position[k], &objects.yaw[k]) != -1) {
360 if (detail == 2 || (detail == 1 && abs(Random() % 4) == 0) || (detail == 0 && abs(Random() % 8) == 0))
361 objects.model[k].MakeDecal(blooddecalfast, DoRotation(colpoint - objects.position[k], 0, -objects.yaw[k], 0), sprites[i]->size * 1.6, .5, Random() % 360);
368 if (sprites[i]->position.y < terrain.getHeight(sprites[i]->position.x, sprites[i]->position.z)) {
369 terrain.MakeDecal(blooddecalfast, sprites[i]->position, sprites[i]->size * 1.6, .6, Random() % 360);
374 if (sprites[i]->type == splintersprite) {
375 sprites[i]->rotation += sprites[i]->rotatespeed * multiplier;
376 sprites[i]->opacity -= multiplier / 2;
377 if (sprites[i]->special == 0 || sprites[i]->special == 2 || sprites[i]->special == 3)
378 sprites[i]->velocity.y += gravity * multiplier;
379 if (sprites[i]->special == 1)
380 sprites[i]->velocity.y += gravity * multiplier * .5;
382 if (sprites[i]->type == flamesprite || sprites[i]->type == weaponflamesprite || sprites[i]->type == weaponshinesprite || sprites[i]->type == bloodflamesprite) {
383 sprites[i]->rotation += multiplier * sprites[i]->rotatespeed;
384 sprites[i]->opacity -= multiplier * 5 / 4;
385 if (sprites[i]->type != weaponshinesprite && sprites[i]->type != bloodflamesprite)
386 if (sprites[i]->opacity < .5 && sprites[i]->opacity + multiplier * 5 / 4 >= .5 && (abs(Random() % 4) == 0 || (sprites[i]->initialsize > 2 && Random() % 2 == 0)))
387 MakeSprite(smoketype, sprites[i]->position, sprites[i]->velocity, .9, .9, .6, sprites[i]->size * 1.2, .4);
388 if (sprites[i]->alivetime > .14 && (sprites[i]->type == flamesprite)) {
389 sprites[i]->velocity = 0;
390 sprites[i]->velocity.y = 1.5;
393 if (sprites[i]->type == smoketype) {
394 sprites[i]->opacity -= multiplier / 3 / sprites[i]->initialsize;
395 sprites[i]->color[0] -= multiplier;
396 sprites[i]->color[1] -= multiplier;
397 sprites[i]->color[2] -= multiplier;
398 if (sprites[i]->color[0] < .6)
399 sprites[i]->color[0] = .6;
400 if (sprites[i]->color[1] < .6)
401 sprites[i]->color[1] = .6;
402 if (sprites[i]->color[2] < .6)
403 sprites[i]->color[2] = .6;
404 sprites[i]->size += multiplier;
405 sprites[i]->velocity = 0;
406 sprites[i]->velocity.y = 1.5;
407 sprites[i]->rotation += multiplier * sprites[i]->rotatespeed / 5;
409 if (sprites[i]->opacity <= 0 || sprites[i]->size <= 0)
413 for (int i = sprites.size() - 1; i >= 0; i--) {
414 sprites[i]->oldposition = sprites[i]->position;
416 glAlphaFunc(GL_GREATER, 0.0001);
417 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
420 void Sprite::DeleteSprite(int i)
422 sprites.erase(sprites.begin() + i);
425 void Sprite::MakeSprite(int atype, XYZ where, XYZ avelocity, float red, float green, float blue, float asize, float aopacity)
427 if (sprites.size() < max_sprites - 1) {
428 sprites.push_back(new Sprite());
429 if ((atype != bloodsprite && atype != bloodflamesprite) || bloodtoggle) {
430 sprites.back()->special = 0;
431 sprites.back()->type = atype;
432 sprites.back()->position = where;
433 sprites.back()->oldposition = where;
434 sprites.back()->velocity = avelocity;
435 sprites.back()->alivetime = 0;
436 sprites.back()->opacity = aopacity;
437 sprites.back()->size = asize;
438 sprites.back()->initialsize = asize;
439 sprites.back()->color[0] = red;
440 sprites.back()->color[1] = green;
441 sprites.back()->color[2] = blue;
442 sprites.back()->rotatespeed = abs(Random() % 720) - 360;
443 sprites.back()->speed = float(abs(Random() % 100)) / 200 + 1.5;
457 memset(color, 0, sizeof(color));
465 void Sprite::clearTextures()
467 toothtexture.destroy();
468 cloudtexture.destroy();
469 cloudimpacttexture.destroy();
470 bloodtexture.destroy();
471 flametexture.destroy();
472 bloodflametexture.destroy();
473 smoketexture.destroy();
474 snowflaketexture.destroy();
475 shinetexture.destroy();
476 splintertexture.destroy();
477 leaftexture.destroy();