]> git.jsancho.org Git - lugaru.git/blob - Source/Sprite.cpp
Stopped using Account pointers, and removed general difficulty setting (difficulty...
[lugaru.git] / Source / Sprite.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
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.
11
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.
16
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/>.
19 */
20
21 #include "Sprite.h"
22 #include "Person.h"
23 #include "Game.h"
24 extern XYZ viewer;
25 extern float viewdistance;
26 extern float fadestart;
27 extern int environment;
28 extern float texscale;
29 extern Light light;
30 extern float multiplier;
31 extern float gravity;
32 extern Terrain terrain;
33 extern Objects objects;
34 extern int detail;
35 extern XYZ viewerfacing;
36 extern int bloodtoggle;
37 extern XYZ windvector;
38
39 // init statics
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;
51
52 float Sprite::checkdelay = 0;
53
54 vector<Sprite*> Sprite::sprites = vector<Sprite*>();
55
56 //Functions
57 void Sprite::Draw()
58 {
59     int k;
60     static float M[16];
61     static XYZ point;
62     static float distancemult;
63     static int lasttype;
64     static int lastspecial;
65     static int whichpatchx, whichpatchz;
66     static XYZ start, end, colpoint;
67     static bool check;
68     static bool blend;
69     static float tempmult;
70     static XYZ difference;
71     static float lightcolor[3];
72     static float viewdistsquared = viewdistance * viewdistance;
73     static XYZ tempviewer;
74
75     tempviewer = viewer + viewerfacing * 6;
76     check = 0;
77
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];
81
82     checkdelay -= multiplier * 10;
83
84     if (checkdelay <= 0) {
85         check = 1;
86         checkdelay = 1;
87     }
88
89     lasttype = -1;
90     lastspecial = -1;
91     glEnable(GL_BLEND);
92     glDisable(GL_LIGHTING);
93     glDisable(GL_CULL_FACE);
94     glEnable(GL_TEXTURE_2D);
95     blend = 1;
96     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
97     glDepthMask(0);
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) {
102             case cloudsprite:
103                 cloudtexture.bind();
104                 if (!blend) {
105                     blend = 1;
106                     glAlphaFunc(GL_GREATER, 0.0001);
107                     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
108                 }
109                 break;
110             case breathsprite:
111             case cloudimpactsprite:
112                 cloudimpacttexture.bind();
113                 if (!blend) {
114                     blend = 1;
115                     glAlphaFunc(GL_GREATER, 0.0001);
116                     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
117                 }
118                 break;
119             case smoketype:
120                 smoketexture.bind();
121                 if (!blend) {
122                     blend = 1;
123                     glAlphaFunc(GL_GREATER, 0.0001);
124                     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
125                 }
126                 break;
127             case bloodsprite:
128                 bloodtexture.bind();
129                 if (!blend) {
130                     blend = 1;
131                     glAlphaFunc(GL_GREATER, 0.0001);
132                     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
133                 }
134                 break;
135             case splintersprite :
136                 if (lastspecial != sprites[i]->special) {
137                     if (sprites[i]->special == 0)
138                         splintertexture.bind();
139                     if (sprites[i]->special == 1)
140                         leaftexture.bind();
141                     if (sprites[i]->special == 2)
142                         snowflaketexture.bind();
143                     if (sprites[i]->special == 3)
144                         toothtexture.bind();
145                     if (!blend) {
146                         blend = 1;
147                         glAlphaFunc(GL_GREATER, 0.0001);
148                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
149                     }
150                 }
151                 break;
152             case snowsprite:
153                 snowflaketexture.bind();
154                 if (!blend) {
155                     blend = 1;
156                     glAlphaFunc(GL_GREATER, 0.0001);
157                     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
158                 }
159                 break;
160             case weaponshinesprite:
161                 shinetexture.bind();
162                 if (blend) {
163                     blend = 0;
164                     glAlphaFunc(GL_GREATER, 0.001);
165                     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
166                 }
167                 break;
168             case flamesprite:
169             case weaponflamesprite:
170                 flametexture.bind();
171                 if (blend || lasttype == bloodflamesprite) {
172                     blend = 0;
173                     glAlphaFunc(GL_GREATER, 0.3);
174                     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
175                 }
176                 break;
177             case bloodflamesprite:
178                 bloodflametexture.bind();
179                 if (blend) {
180                     blend = 0;
181                     glAlphaFunc(GL_GREATER, 0.3);
182                     glBlendFunc(GL_ONE, GL_ZERO);
183                 }
184                 break;
185             }
186         }
187         if (sprites[i]->type == snowsprite)
188             distancemult = (144 - (distsq(&tempviewer, &sprites[i]->position) - (144 * fadestart)) * (1 / (1 - fadestart))) / 144;
189         else
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);
194             else
195                 glColor4f(sprites[i]->color[0], sprites[i]->color[1], sprites[i]->color[2], sprites[i]->opacity * distancemult);
196         } else {
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);
199             else
200                 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], sprites[i]->opacity * distancemult);
201         }
202         lasttype = sprites[i]->type;
203         lastspecial = sprites[i]->special;
204         glMatrixMode(GL_MODELVIEW);
205         glPushMatrix();
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);
211         }
212         if (sprites[i]->type == snowsprite) {
213             glRotatef(sprites[i]->rotation * .2, 0, .3, 1);
214             glTranslatef(1, 0, 0);
215         }
216         glGetFloatv(GL_MODELVIEW_MATRIX, M);
217         point.x = M[12];
218         point.y = M[13];
219         point.z = M[14];
220         glLoadIdentity();
221         glTranslatef(point.x, point.y, point.z);
222
223         glRotatef(sprites[i]->rotation, 0, 0, 1);
224
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);
228         }
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);
235             }
236         }
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);
241                 else
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);
243             } else {
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);
246                 else
247                     glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], 1);
248             }
249         }
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);
253             else
254                 glColor4f(sprites[i]->color[0]*lightcolor[0], sprites[i]->color[1]*lightcolor[1], sprites[i]->color[2]*lightcolor[2], 1);
255         }
256
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);
270         glEnd();
271         glPopMatrix();
272     }
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;
279         }
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;
285
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;
290         }
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;
296             } else {
297                 XYZ slowdown;
298                 slowdown = sprites[i]->velocity * -1;
299                 Normalise(&slowdown);
300                 slowdown *= multiplier;
301                 sprites[i]->velocity += slowdown;
302             }
303         }
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;
315         }
316         if (sprites[i]->type == bloodsprite) {
317             bool spritehit = 0;
318             sprites[i]->rotation += multiplier * 100;
319             sprites[i]->velocity.y += gravity * multiplier;
320             if (check) {
321                 XYZ where, startpoint, endpoint, movepoint, footpoint;
322                 float rotationpoint;
323                 int whichtri;
324
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);
331                         startpoint = where;
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);
336                         endpoint = where;
337
338                         movepoint = 0;
339                         rotationpoint = 0;
340                         whichtri = Person::players[j]->skeleton.drawmodel.LineCheck(&startpoint, &endpoint, &footpoint, &movepoint, &rotationpoint);
341                         if (whichtri != -1) {
342                             spritehit = 1;
343                             Person::players[j]->DoBloodBigWhere(0, 160, sprites[i]->oldposition);
344                             DeleteSprite(i);
345                         }
346                     }
347                 }
348
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]) {
353                         if (!spritehit)
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;
358                                 if (!spritehit)
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);
362                                         DeleteSprite(i);
363                                         spritehit = 1;
364                                     }
365                             }
366                     }
367                 if (!spritehit)
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);
370                         DeleteSprite(i);
371                     }
372             }
373         }
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;
381         }
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;
391             }
392         }
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;
408         }
409         if (sprites[i]->opacity <= 0 || sprites[i]->size <= 0)
410             DeleteSprite(i);
411     }
412     if (check)
413         for (int i = sprites.size() - 1; i >= 0; i--) {
414             sprites[i]->oldposition = sprites[i]->position;
415         }
416     glAlphaFunc(GL_GREATER, 0.0001);
417     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
418 }
419
420 void Sprite::DeleteSprite(int i)
421 {
422     sprites.erase(sprites.begin() + i);
423 }
424
425 void Sprite::MakeSprite(int atype, XYZ where, XYZ avelocity, float red, float green, float blue, float asize, float aopacity)
426 {
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;
444         }
445     }
446 }
447
448 Sprite::Sprite()
449 {
450     oldposition = 0;
451     position = 0;
452     velocity = 0;
453     size = 0;
454     initialsize = 0;
455     type = 0;
456     special = 0;
457     memset(color, 0, sizeof(color));
458     opacity = 0;
459     rotation = 0;
460     alivetime = 0;
461     speed = 0;
462     rotatespeed = 0;
463 }
464
465 void Sprite::clearTextures()
466 {
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();
478 }
479