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 This file is part of Lugaru.
9 Lugaru is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 Lugaru is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
23 #include "Objects/Weapons.hpp"
25 #include "Animation/Animation.hpp"
26 #include "Audio/Sounds.hpp"
27 #include "Audio/openal_wrapper.hpp"
29 #include "Level/Awards.hpp"
30 #include "Tutorial.hpp"
32 extern float multiplier;
33 extern Terrain terrain;
35 extern int environment;
37 extern FRUSTUM frustum;
39 extern float realmultiplier;
41 extern float slomodelay;
42 extern bool cellophane;
43 extern float texdetail;
44 extern GLubyte bloodText[512 * 512 * 3];
45 extern int bloodtoggle;
46 extern bool autoslomo;
47 extern float camerashake;
49 extern float viewdistance;
50 extern float blackout;
52 extern int numthrowkill;
54 Model Weapon::throwingknifemodel;
55 Texture Weapon::knifetextureptr;
56 Texture Weapon::lightbloodknifetextureptr;
57 Texture Weapon::bloodknifetextureptr;
59 Model Weapon::swordmodel;
60 Texture Weapon::swordtextureptr;
61 Texture Weapon::lightbloodswordtextureptr;
62 Texture Weapon::bloodswordtextureptr;
64 Model Weapon::staffmodel;
65 Texture Weapon::stafftextureptr;
67 Weapon::Weapon(int t, int o)
81 void Weapon::setType(int t)
101 /* Load weapons models and textures */
104 LOG("Loading weapon data...");
106 knifetextureptr.load("Textures/Knife.png", 0);
107 bloodknifetextureptr.load("Textures/BloodKnife.png", 0);
108 lightbloodknifetextureptr.load("Textures/BloodKnifeLight.png", 0);
109 swordtextureptr.load("Textures/Sword.jpg", 1);
110 bloodswordtextureptr.load("Textures/SwordBlood.jpg", 1);
111 lightbloodswordtextureptr.load("Textures/SwordBloodLight.jpg", 1);
112 stafftextureptr.load("Textures/Staff.jpg", 1);
114 throwingknifemodel.load("Models/ThrowingKnife.solid");
115 throwingknifemodel.Scale(.001, .001, .001);
116 throwingknifemodel.Rotate(90, 0, 0);
117 throwingknifemodel.Rotate(0, 90, 0);
118 throwingknifemodel.flat = 0;
119 throwingknifemodel.CalculateNormals(1);
121 swordmodel.load("Models/Sword.solid");
122 swordmodel.Scale(.001, .001, .001);
123 swordmodel.Rotate(90, 0, 0);
124 swordmodel.Rotate(0, 90, 0);
125 swordmodel.Rotate(0, 0, 90);
127 swordmodel.CalculateNormals(1);
129 staffmodel.load("Models/Staff.solid");
130 staffmodel.Scale(.005, .005, .005);
131 staffmodel.Rotate(90, 0, 0);
132 staffmodel.Rotate(0, 90, 0);
133 staffmodel.Rotate(0, 0, 90);
135 staffmodel.CalculateNormals(1);
138 void Weapon::doStuff(int i)
140 static int whichpatchx, whichpatchz, whichhit;
141 static XYZ start, end, colpoint, normalrot, footvel, footpoint;
142 static XYZ terrainnormal;
145 static XYZ newpoint1, newpoint2;
146 static float friction = 3.5;
147 static float elasticity = .4;
148 static XYZ bounceness;
149 static float frictionness;
150 static float closestdistance;
151 static float distance;
153 static XYZ closestpoint;
154 static XYZ closestswordpoint;
155 static float tempmult;
157 if (multiplier <= 0) {
164 if (damage >= 2 && type == staff && owner != -1) { // the staff breaks
165 emit_sound_at(staffbreaksound, tippoint);
167 for (int j = 0; j < 40; j++) {
168 tempvel.x = float(abs(Random() % 100) - 50) / 20;
169 tempvel.y = float(abs(Random() % 100) - 50) / 20;
170 tempvel.z = float(abs(Random() % 100) - 50) / 20;
171 Sprite::MakeSprite(splintersprite, position + (tippoint - position) * ((float)j - 8) / 32, tempvel * .5, 115 / 255, 73 / 255, 12 / 255, .1, 1);
174 Person::players[owner]->weaponactive = -1;
175 Person::players[owner]->num_weapons--;
176 if (Person::players[owner]->num_weapons) {
177 Person::players[owner]->weaponids[0] = Person::players[owner]->weaponids[Person::players[owner]->num_weapons];
178 if (Person::players[owner]->weaponstuck == Person::players[owner]->num_weapons)
179 Person::players[owner]->weaponstuck = 0;
190 oldposition = position;
191 oldtippoint = tippoint;
192 if (owner == -1 && (velocity.x || velocity.y || velocity.z) && !physics) { // if the weapon is flying
193 position += velocity * multiplier;
194 tippoint += velocity * multiplier;
195 whichpatchx = position.x / (terrain.size / subdivision * terrain.scale);
196 whichpatchz = position.z / (terrain.size / subdivision * terrain.scale);
197 if (whichpatchx > 0 && whichpatchz > 0 && whichpatchx < subdivision && whichpatchz < subdivision) {
198 for (unsigned int j = 0; j < terrain.patchobjects[whichpatchx][whichpatchz].size(); j++) { // check for collision
199 unsigned int k = terrain.patchobjects[whichpatchx][whichpatchz][j];
202 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
203 if (whichhit != -1) {
204 if (Object::objects[k]->type == treetrunktype) {
205 Object::objects[k]->model.MakeDecal(breakdecal, DoRotation(colpoint - Object::objects[k]->position, 0, -Object::objects[k]->yaw, 0), .1, 1, Random() % 360);
206 normalrot = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0);
209 position = colpoint - normalrot * .1;
210 else if (type == sword)
211 position = colpoint - normalrot * .2;
212 else if (type == staff)
213 position = colpoint - normalrot * .2;
214 XYZ temppoint1, temppoint2;
218 temppoint2 = normalrot;
219 distance = findDistance(&temppoint1, &temppoint2);
220 rotation2 = asin((temppoint1.y - temppoint2.y) / distance);
221 rotation2 *= 360 / 6.28;
224 rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2));
225 rotation1 *= 360 / 6.28;
226 if (temppoint1.x > temppoint2.x)
227 rotation1 = 360 - rotation1;
236 emit_sound_at(knifesheathesound, position, 128.);
240 Sprite::MakeSprite(cloudimpactsprite, position, velocity, 1, 1, 1, .8, .3);
244 position -= velocity * multiplier;
245 tippoint -= velocity * multiplier;
246 tipvelocity = velocity;
252 if (velocity.x || velocity.y || velocity.z) {
253 for (unsigned j = 0; j < Person::players.size(); j++) {
255 footpoint = DoRotation((Person::players[j]->jointPos(abdomen) + Person::players[j]->jointPos(neck)) / 2, 0, Person::players[j]->yaw, 0) * Person::players[j]->scale + Person::players[j]->coords;
256 if (owner == -1 && distsqflat(&position, &Person::players[j]->coords) < 1.5 &&
257 distsq(&position, &Person::players[j]->coords) < 4 && Person::players[j]->weaponstuck == -1 &&
258 !Person::players[j]->skeleton.free && (int(j) != oldowner)) {
259 if ((Person::players[j]->aitype != attacktypecutoff || abs(Random() % 6) == 0 || (Person::players[j]->animTarget != backhandspringanim && Person::players[j]->animTarget != rollanim && Person::players[j]->animTarget != flipanim && Random() % 2 == 0)) && !missed) {
260 if ((Person::players[j]->creature == wolftype && Random() % 3 != 0 && Person::players[j]->weaponactive == -1 && (Person::players[j]->isIdle() || Person::players[j]->isRun() || Person::players[j]->animTarget == walkanim)) ||
261 (Person::players[j]->creature == rabbittype && Random() % 2 == 0 && Person::players[j]->aitype == attacktypecutoff && Person::players[j]->weaponactive == -1)) {
262 emit_sound_at(knifedrawsound, Person::players[j]->coords, 128.);
264 Person::players[j]->animTarget = removeknifeanim;
265 Person::players[j]->frameTarget = 1;
266 Person::players[j]->target = 1;
267 Person::players[j]->takeWeapon(i);
269 Person::players[j]->aitype = attacktypecutoff;
273 Person::players[j]->num_weapons++;
274 Person::players[j]->weaponstuck = Person::players[j]->num_weapons - 1;
275 if (normaldotproduct(Person::players[j]->facing, velocity) > 0)
276 Person::players[j]->weaponstuckwhere = 1;
278 Person::players[j]->weaponstuckwhere = 0;
280 Person::players[j]->weaponids[Person::players[j]->num_weapons - 1] = i;
282 Person::players[j]->RagDoll(0);
283 Person::players[j]->jointVel(abdomen) += velocity * 2;
284 Person::players[j]->jointVel(neck) += velocity * 2;
285 Person::players[j]->jointVel(rightshoulder) += velocity * 2;
286 Person::players[j]->jointVel(leftshoulder) += velocity * 2;
287 if (bloodtoggle && !Tutorial::active)
288 Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 0, 0, .8, .3);
289 if (Tutorial::active)
290 Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 1, 1, .8, .3);
291 footvel = tippoint - position;
293 if (bloodtoggle && !Tutorial::active)
294 Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * -1, 1, 0, 0, .6, 1);
296 if (!Tutorial::active) {
297 if (Person::players[j]->weaponstuckwhere == 0)
298 Person::players[j]->DoBloodBig(2, 205);
299 if (Person::players[j]->weaponstuckwhere == 1)
300 Person::players[j]->DoBloodBig(2, 200);
301 Person::players[j]->damage += 200 / Person::players[j]->armorhigh;
302 Person::players[j]->deathbleeding = 1;
303 Person::players[j]->bloodloss += (200 + abs((float)(Random() % 40)) - 20) / Person::players[j]->armorhigh;
309 emit_sound_at(fleshstabsound, position, 128.);
311 if (Animation::animations[Person::players[0]->animTarget].height == highheight)
312 award_bonus(0, ninja);
314 award_bonus(0, Bullseyebonus);
322 if (position.y < terrain.getHeight(position.x, position.z)) {
323 if (terrain.getOpacity(position.x, position.z) < .2) {
325 if (terrain.lineTerrain(oldposition, position, &colpoint) != -1) {
326 position = colpoint * terrain.scale;
328 position.y = terrain.getHeight(position.x, position.z);
331 terrain.MakeDecal(shadowdecalpermanent, position, .06, .5, 0);
332 normalrot = terrain.getNormal(position.x, position.z) * -1;
334 glMatrixMode(GL_MODELVIEW);
338 glRotatef(bigrotation, 0, 1, 0);
339 glRotatef(bigtilt2, 1, 0, 0);
340 glRotatef(bigtilt, 0, 0, 1);
341 glRotatef(-rotation1 + 90, 0, 1, 0);
342 glRotatef(-rotation2 + 90, 0, 0, 1);
343 glRotatef(-rotation3, 0, 1, 0);
344 glRotatef(smallrotation, 1, 0, 0);
345 glRotatef(smallrotation2, 0, 1, 0);
346 glTranslatef(0, 0, 1);
347 glGetFloatv(GL_MODELVIEW_MATRIX, M);
352 position -= tippoint * .15;
353 XYZ temppoint1, temppoint2;
362 emit_sound_at(knifesheathesound, position, 128.);
365 terrainlight = terrain.getLighting(position.x, position.z);
366 if (environment == snowyenvironment) {
367 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4)
368 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
369 } else if (environment == grassyenvironment) {
370 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4)
371 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x * 90 / 255, terrainlight.y * 70 / 255, terrainlight.z * 8 / 255, .5, .5);
372 } else if (environment == desertenvironment) {
373 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4)
374 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x * 190 / 255, terrainlight.y * 170 / 255, terrainlight.z * 108 / 255, .5, .7);
381 position -= velocity * multiplier;
382 tippoint -= velocity * multiplier;
383 tipvelocity = velocity;
386 if (velocity.x != 0 || velocity.z != 0 || velocity.y != 0) {
387 velocity.y += gravity * multiplier;
389 XYZ temppoint1, temppoint2;
393 temppoint2 = velocity;
394 distance = findDistance(&temppoint1, &temppoint2);
395 rotation2 = asin((temppoint1.y - temppoint2.y) / distance);
396 rotation2 *= 360 / 6.28;
399 rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2));
400 rotation1 *= 360 / 6.28;
407 if (temppoint1.x > temppoint2.x)
408 rotation1 = 360 - rotation1;
417 tempmult = multiplier;
419 for (int l = 0; l < 10; l++) {
420 if (owner == -1 && (velocity.x || velocity.y || velocity.z) && physics) {
422 position += velocity * multiplier;
423 tippoint += tipvelocity * multiplier;
426 midp = (position * mass + tippoint * tipmass) / (mass + tipmass);
427 vel = tippoint - midp;
429 newpoint1 = midp - vel * length * (tipmass / (mass + tipmass));
430 newpoint2 = midp + vel * length * (mass / (mass + tipmass));
432 if (freetime > .04) {
433 velocity = velocity + (newpoint1 - position) / multiplier;
434 tipvelocity = tipvelocity + (newpoint2 - tippoint) / multiplier;
437 position = newpoint1;
438 tippoint = newpoint2;
441 whichpatchx = (position.x) / (terrain.size / subdivision * terrain.scale);
442 whichpatchz = (position.z) / (terrain.size / subdivision * terrain.scale);
443 if (whichpatchx > 0 && whichpatchz > 0 && whichpatchx < subdivision && whichpatchz < subdivision) {
444 for (unsigned int j = 0; j < terrain.patchobjects[whichpatchx][whichpatchz].size(); j++) {
445 unsigned int k = terrain.patchobjects[whichpatchx][whichpatchz][j];
449 start = tippoint - (position - tippoint) / 5;
450 end = position + (position - tippoint) / 30;
451 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
452 if (whichhit != -1) {
454 diff = (colpoint - position);
458 tippoint += (colpoint - position) + diff * .05;
459 position = colpoint + diff * .05;
460 oldtippoint = tippoint;
461 oldposition = tippoint;
464 start = position - (tippoint - position) / 5;
465 end = tippoint + (tippoint - position) / 30;
466 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
467 if (whichhit != -1) {
469 diff = (colpoint - tippoint);
473 position += (colpoint - tippoint) + diff * .05;
474 tippoint = colpoint + diff * .05;
475 oldposition = position;
476 oldtippoint = tippoint;
483 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
484 if (whichhit != -1) {
487 terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1;
488 ReflectVector(&velocity, &terrainnormal);
489 position += terrainnormal * .002;
491 bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal)));
492 if (findLengthfast(&velocity) < findLengthfast(&bounceness))
494 frictionness = abs(normaldotproduct(velocity, terrainnormal));
495 velocity -= bounceness;
496 if (1 - friction * frictionness > 0)
497 velocity *= 1 - friction * frictionness;
500 velocity += bounceness * elasticity;
502 if (findLengthfast(&bounceness) > 1) {
505 whichsound = footstepsound3 + abs(Random() % 2);
507 whichsound = clank1sound + abs(Random() % 4);
508 emit_sound_at(whichsound, position, 128 * findLengthfast(&bounceness));
513 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
514 if (whichhit != -1) {
517 terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1;
518 ReflectVector(&tipvelocity, &terrainnormal);
519 tippoint += terrainnormal * .002;
521 bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal)));
522 if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness))
524 frictionness = abs(normaldotproduct(tipvelocity, terrainnormal));
525 tipvelocity -= bounceness;
526 if (1 - friction * frictionness > 0)
527 tipvelocity *= 1 - friction * frictionness;
530 tipvelocity += bounceness * elasticity;
532 if (findLengthfast(&bounceness) > 1) {
535 whichsound = footstepsound3 + abs(Random() % 2);
537 whichsound = clank1sound + abs(Random() % 4);
538 emit_sound_at(whichsound, position, 128 * findLengthfast(&bounceness));
542 if ((Object::objects[k]->type != boxtype && Object::objects[k]->type != platformtype && Object::objects[k]->type != walltype && Object::objects[k]->type != weirdtype) || Object::objects[k]->pitch != 0)
543 for (int m = 0; m < 2; m++) {
544 mid = (position * (21 + (float)m * 10) + tippoint * (19 - (float)m * 10)) / 40;
546 oldmid = (oldposition * (21 + (float)m * 10) + oldtippoint * (19 - (float)m * 10)) / 40;
550 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
551 if (whichhit != -1) {
554 terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1;
555 ReflectVector(&velocity, &terrainnormal);
557 bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal)));
558 if (findLengthfast(&velocity) < findLengthfast(&bounceness))
560 frictionness = abs(normaldotproduct(velocity, terrainnormal));
561 velocity -= bounceness;
562 if (1 - friction * frictionness > 0)
563 velocity *= 1 - friction * frictionness;
566 velocity += bounceness * elasticity;
568 if (findLengthfast(&bounceness) > 1) {
571 whichsound = footstepsound3 + abs(Random() % 2);
573 whichsound = clank1sound + abs(Random() % 4);
574 emit_sound_at(whichsound, mid, 128 * findLengthfast(&bounceness));
576 position += (mid - oldmid2) * (20 / (1 + (float)m * 10));
579 mid = (position * (19 - (float)m * 10) + tippoint * (21 + (float)m * 10)) / 40;
581 oldmid = (oldposition * (19 - (float)m * 10) + oldtippoint * (21 + (float)m * 10)) / 40;
585 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
586 if (whichhit != -1) {
589 terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1;
590 ReflectVector(&tipvelocity, &terrainnormal);
592 bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal)));
593 if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness))
595 frictionness = abs(normaldotproduct(tipvelocity, terrainnormal));
596 tipvelocity -= bounceness;
597 if (1 - friction * frictionness > 0)
598 tipvelocity *= 1 - friction * frictionness;
601 tipvelocity += bounceness * elasticity;
603 if (findLengthfast(&bounceness) > 1) {
606 whichsound = footstepsound3 + abs(Random() % 2);
608 whichsound = clank1sound + abs(Random() % 4);
609 emit_sound_at(whichsound, mid, 128 * findLengthfast(&bounceness));
611 tippoint += (mid - oldmid2) * (20 / (1 + (float)m * 10));
617 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
618 if (whichhit != -1) {
620 closestdistance = -1;
621 closestswordpoint = colpoint;
622 point[0] = DoRotation(Object::objects[k]->model.getTriangleVertex(whichhit, 0), 0, Object::objects[k]->yaw, 0) + Object::objects[k]->position;
623 point[1] = DoRotation(Object::objects[k]->model.getTriangleVertex(whichhit, 1), 0, Object::objects[k]->yaw, 0) + Object::objects[k]->position;
624 point[2] = DoRotation(Object::objects[k]->model.getTriangleVertex(whichhit, 2), 0, Object::objects[k]->yaw, 0) + Object::objects[k]->position;
625 if (DistancePointLine(&closestswordpoint, &point[0], &point[1], &distance, &colpoint)) {
626 if (distance < closestdistance || closestdistance == -1) {
627 closestpoint = colpoint;
628 closestdistance = distance;
631 if (DistancePointLine(&closestswordpoint, &point[1], &point[2], &distance, &colpoint)) {
632 if (distance < closestdistance || closestdistance == -1) {
633 closestpoint = colpoint;
634 closestdistance = distance;
637 if (DistancePointLine(&closestswordpoint, &point[2], &point[0], &distance, &colpoint)) {
638 if (distance < closestdistance || closestdistance == -1) {
639 closestpoint = colpoint;
640 closestdistance = distance;
643 if (closestdistance != -1 && isnormal(closestdistance)) {
644 if (DistancePointLine(&closestpoint, &position, &tippoint, &distance, &colpoint)) {
645 closestswordpoint = colpoint;
646 velocity += (closestpoint - closestswordpoint);
647 tipvelocity += (closestpoint - closestswordpoint);
648 position += (closestpoint - closestswordpoint);
649 tippoint += (closestpoint - closestswordpoint);
657 whichhit = terrain.lineTerrain(oldposition, position, &colpoint);
658 if (whichhit != -1 || position.y < terrain.getHeight(position.x, position.z)) {
661 position = colpoint * terrain.scale;
663 position.y = terrain.getHeight(position.x, position.z);
665 terrainnormal = terrain.getNormal(position.x, position.z);
666 ReflectVector(&velocity, &terrainnormal);
667 position += terrainnormal * .002;
668 bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal)));
669 if (findLengthfast(&velocity) < findLengthfast(&bounceness))
671 frictionness = abs(normaldotproduct(velocity, terrainnormal));
672 velocity -= bounceness;
673 if (1 - friction * frictionness > 0)
674 velocity *= 1 - friction * frictionness;
677 if (terrain.getOpacity(position.x, position.z) < .2)
678 velocity += bounceness * elasticity * .3;
680 velocity += bounceness * elasticity;
682 if (findLengthfast(&bounceness) > 1) {
684 if (terrain.getOpacity(position.x, position.z) > .2) {
686 whichsound = footstepsound3 + abs(Random() % 2);
688 whichsound = clank1sound + abs(Random() % 4);
690 whichsound = footstepsound + abs(Random() % 2);
692 emit_sound_at(whichsound, position,
693 findLengthfast(&bounceness) * (terrain.getOpacity(position.x, position.z) > .2 ? 128. : 32.));
695 if (terrain.getOpacity(position.x, position.z) < .2) {
697 terrainlight = terrain.getLighting(position.x, position.z);
698 if (environment == snowyenvironment) {
699 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4)
700 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
701 } else if (environment == grassyenvironment) {
702 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4)
703 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x * 90 / 255, terrainlight.y * 70 / 255, terrainlight.z * 8 / 255, .5, .5);
704 } else if (environment == desertenvironment) {
705 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4)
706 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x * 190 / 255, terrainlight.y * 170 / 255, terrainlight.z * 108 / 255, .5, .7);
711 whichhit = terrain.lineTerrain(oldtippoint, tippoint, &colpoint);
712 if (whichhit != -1 || tippoint.y < terrain.getHeight(tippoint.x, tippoint.z)) {
714 tippoint = colpoint * terrain.scale;
716 tippoint.y = terrain.getHeight(tippoint.x, tippoint.z);
718 terrainnormal = terrain.getNormal(tippoint.x, tippoint.z);
719 ReflectVector(&tipvelocity, &terrainnormal);
720 tippoint += terrainnormal * .002;
721 bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal)));
722 if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness))
724 frictionness = abs(normaldotproduct(tipvelocity, terrainnormal));
725 tipvelocity -= bounceness;
726 if (1 - friction * frictionness > 0)
727 tipvelocity *= 1 - friction * frictionness;
730 if (terrain.getOpacity(tippoint.x, tippoint.z) < .2)
731 tipvelocity += bounceness * elasticity * .3;
733 tipvelocity += bounceness * elasticity;
735 if (findLengthfast(&bounceness) > 1) {
737 if (terrain.getOpacity(tippoint.x, tippoint.z) > .2) {
739 whichsound = footstepsound3 + abs(Random() % 2);
741 whichsound = clank1sound + abs(Random() % 4);
743 whichsound = footstepsound + abs(Random() % 2);
745 emit_sound_at(whichsound, tippoint,
746 findLengthfast(&bounceness) * (terrain.getOpacity(tippoint.x, tippoint.z) > .2 ? 128. : 32.));
748 if (terrain.getOpacity(tippoint.x, tippoint.z) < .2) {
750 terrainlight = terrain.getLighting(tippoint.x, tippoint.z);
751 if (environment == snowyenvironment) {
752 if (distsq(&tippoint, &viewer) < viewdistance * viewdistance / 4)
753 Sprite::MakeSprite(cloudsprite, tippoint, tipvelocity, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
754 } else if (environment == grassyenvironment) {
755 if (distsq(&tippoint, &viewer) < viewdistance * viewdistance / 4)
756 Sprite::MakeSprite(cloudsprite, tippoint, tipvelocity, terrainlight.x * 90 / 255, terrainlight.y * 70 / 255, terrainlight.z * 8 / 255, .5, .5);
757 } else if (environment == desertenvironment) {
758 if (distsq(&tippoint, &viewer) < viewdistance * viewdistance / 4)
759 Sprite::MakeSprite(cloudsprite, tippoint, tipvelocity, terrainlight.x * 190 / 255, terrainlight.y * 170 / 255, terrainlight.z * 108 / 255, .5, .7);
766 mid = position + tippoint;
768 mid += (position - mid) / 20;
770 if (mid.y < terrain.getHeight(mid.x, mid.z)) {
772 mid.y = terrain.getHeight(mid.x, mid.z);
774 terrainnormal = terrain.getNormal(mid.x, mid.z);
775 ReflectVector(&velocity, &terrainnormal);
776 //mid+=terrainnormal*.002;
777 bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal)));
778 if (findLengthfast(&velocity) < findLengthfast(&bounceness))
780 frictionness = abs(normaldotproduct(velocity, terrainnormal));
781 velocity -= bounceness;
782 if (1 - friction * frictionness > 0)
783 velocity *= 1 - friction * frictionness;
786 if (terrain.getOpacity(mid.x, mid.z) < .2)
787 velocity += bounceness * elasticity * .3;
789 velocity += bounceness * elasticity;
791 if (findLengthfast(&bounceness) > 1) {
793 if (terrain.getOpacity(mid.x, mid.z) > .2) {
795 whichsound = footstepsound3 + abs(Random() % 2);
797 whichsound = clank1sound + abs(Random() % 4);
799 whichsound = footstepsound + abs(Random() % 2);
801 emit_sound_at(whichsound, mid,
802 findLengthfast(&bounceness) * (terrain.getOpacity(position.x, position.z) > .2
806 position += (mid - oldmid) * 20;
809 mid = position + tippoint;
811 mid += (tippoint - mid) / 20;
813 if (mid.y < terrain.getHeight(mid.x, mid.z)) {
815 mid.y = terrain.getHeight(mid.x, mid.z);
817 terrainnormal = terrain.getNormal(mid.x, mid.z);
818 ReflectVector(&tipvelocity, &terrainnormal);
819 //mid+=terrainnormal*.002;
820 bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal)));
821 if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness))
823 frictionness = abs(normaldotproduct(tipvelocity, terrainnormal));
824 tipvelocity -= bounceness;
825 if (1 - friction * frictionness > 0)
826 tipvelocity *= 1 - friction * frictionness;
829 if (terrain.getOpacity(mid.x, mid.z) < .2)
830 tipvelocity += bounceness * elasticity * .3;
832 tipvelocity += bounceness * elasticity;
834 if (findLengthfast(&bounceness) > 1) {
836 if (terrain.getOpacity(mid.x, mid.z) > .2) {
838 whichsound = footstepsound3 + abs(Random() % 2);
840 whichsound = clank1sound + abs(Random() % 4);
842 whichsound = footstepsound + abs(Random() % 2);
844 emit_sound_at(whichsound, mid,
845 findLengthfast(&bounceness) * (terrain.getOpacity(position.x, position.z) > .2
849 tippoint += (mid - oldmid) * 20;
852 velocity.y += gravity * multiplier;
853 tipvelocity.y += gravity * multiplier;
856 XYZ temppoint1, temppoint2;
859 temppoint1 = position;
860 temppoint2 = tippoint;
861 distance = findDistance(&temppoint1, &temppoint2);
862 rotation2 = asin((temppoint1.y - temppoint2.y) / distance);
863 rotation2 *= 360 / 6.28;
866 rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2));
867 rotation1 *= 360 / 6.28;
874 if (temppoint1.x > temppoint2.x)
875 rotation1 = 360 - rotation1;
878 if (findLengthfast(&velocity) < .3 && findLengthfast(&tipvelocity) < .3 && hitsomething) {
879 freetime += multiplier;
889 multiplier = tempmult;
890 if (blooddrip && bloody) {
891 blooddripdelay -= blooddrip * multiplier / 2;
892 blooddrip -= multiplier;
897 if (blooddripdelay < 0 && bloodtoggle) {
901 bloodloc = position + (tippoint - position) * .7;
905 Sprite::MakeSprite(bloodsprite, bloodloc, bloodvel, 1, 1, 1, .03, 1);
910 flamedelay -= multiplier;
911 if (onfire && flamedelay <= 0) {
913 flamedelay -= multiplier;
916 normalrot = Person::players[owner]->velocity;
920 if (Person::players[owner]->onterrain) {
924 Sprite::MakeSprite(weaponflamesprite, position + tippoint * (((float)abs(Random() % 100)) / 600 + .05), normalrot, 1, 1, 1, (.6 + (float)abs(Random() % 100) / 200 - .25) * 1 / 3, 1);
925 Sprite::setLastSpriteSpeed(4);
926 Sprite::setLastSpriteAlivetime(.3);
930 if (!onfire && owner == -1 && type != staff) {
931 flamedelay -= multiplier;
932 if (flamedelay <= 0) {
934 flamedelay -= multiplier;
936 if (Random() % 50 == 0 && distsq(&position, &viewer) > 80) {
938 shinepoint = position + (tippoint - position) * (((float)abs(Random() % 100)) / 100);
939 Sprite::MakeSprite(weaponshinesprite, shinepoint, normalrot, 1, 1, 1, (.1 + (float)abs(Random() % 100) / 200 - .25) * 1 / 3 * fast_sqrt(findDistance(&shinepoint, &viewer)), 1);
940 Sprite::setLastSpriteSpeed(4);
941 Sprite::setLastSpriteAlivetime(.3);
947 void Weapons::DoStuff()
951 for (std::vector<Weapon>::iterator weapon = begin(); weapon != end(); ++weapon) {
952 weapon->doStuff(i++);
958 static XYZ terrainlight;
959 static GLfloat M[16];
961 if ((frustum.SphereInFrustum(position.x, position.y, position.z, 1) &&
962 distsq(&viewer, &position) < viewdistance * viewdistance)) {
966 if (velocity.x && !physics)
971 if (Person::players[owner]->occluded < 25)
972 if ((frustum.SphereInFrustum(Person::players[owner]->coords.x, Person::players[owner]->coords.y + Person::players[owner]->scale * 3, Person::players[owner]->coords.z, Person::players[owner]->scale * 8) && distsq(&viewer, &Person::players[owner]->coords) < viewdistance * viewdistance) || Person::players[owner]->skeleton.free == 3)
975 (Person::players[owner]->animTarget == knifeslashstartanim ||
976 Person::players[owner]->animTarget == swordsneakattackanim ||
977 (Person::players[owner]->animCurrent == staffhitanim && Person::players[owner]->frameCurrent > 1) ||
978 (Person::players[owner]->animCurrent == staffhitreversedanim && Person::players[owner]->frameCurrent > 1) ||
979 (Person::players[owner]->animCurrent == staffspinhitanim && Person::players[owner]->frameCurrent > 1) ||
980 (Person::players[owner]->animCurrent == staffspinhitreversedanim && Person::players[owner]->frameCurrent > 1) ||
981 (Person::players[owner]->animCurrent == staffgroundsmashanim && Person::players[owner]->frameCurrent > 1) ||
982 (Person::players[owner]->animTarget == swordslashanim && Person::players[owner]->frameTarget < 7) ||
983 Person::players[owner]->animTarget == crouchstabanim ||
984 Person::players[owner]->animTarget == swordslashreversalanim ||
985 Person::players[owner]->animTarget == swordslashreversedanim ||
986 Person::players[owner]->animTarget == knifefollowanim ||
987 Person::players[owner]->animTarget == swordgroundstabanim ||
988 Person::players[owner]->animTarget == knifethrowanim) &&
989 Person::players[owner]->animTarget == lastdrawnanim &&
990 !Person::players[owner]->skeleton.free) {
995 if (Person::players[owner]->animTarget == swordgroundstabanim) {
996 lastdrawnrotation1 = rotation1;
997 lastdrawnrotation2 = rotation2;
998 lastdrawnrotation3 = rotation3;
999 lastdrawnbigrotation = bigrotation;
1000 lastdrawnbigtilt = bigtilt;
1001 lastdrawnbigtilt2 = bigtilt2;
1002 lastdrawnsmallrotation = smallrotation;
1003 lastdrawnsmallrotation2 = smallrotation2;
1007 terrainlight = terrain.getLighting(position.x, position.z);
1008 if (drawhowmany > 0) {
1009 glAlphaFunc(GL_GREATER, 0.01);
1011 for (int j = drawhowmany; j > 0; j--) {
1012 glMatrixMode(GL_MODELVIEW);
1014 glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, j / drawhowmany);
1016 glTranslatef(position.x * (((float)(j)) / drawhowmany) + lastdrawnposition.x * (1 - ((float)(j)) / drawhowmany), position.y * (((float)(j)) / drawhowmany) + lastdrawnposition.y * (1 - ((float)(j)) / drawhowmany), position.z * (((float)(j)) / drawhowmany) + lastdrawnposition.z * (1 - ((float)(j)) / drawhowmany));
1018 glTranslatef(position.x * (((float)(j)) / drawhowmany) + lastdrawnposition.x * (1 - ((float)(j)) / drawhowmany), position.y * (((float)(j)) / drawhowmany) - .02 + lastdrawnposition.y * (1 - ((float)(j)) / drawhowmany), position.z * (((float)(j)) / drawhowmany) + lastdrawnposition.z * (1 - ((float)(j)) / drawhowmany));
1019 glRotatef(bigrotation * (((float)(j)) / drawhowmany) + lastdrawnbigrotation * (1 - ((float)(j)) / drawhowmany), 0, 1, 0);
1020 glRotatef(bigtilt2 * (((float)(j)) / drawhowmany) + lastdrawnbigtilt2 * (1 - ((float)(j)) / drawhowmany), 1, 0, 0);
1021 glRotatef(bigtilt * (((float)(j)) / drawhowmany) + lastdrawnbigtilt * (1 - ((float)(j)) / drawhowmany), 0, 0, 1);
1022 glRotatef(-rotation1 * (((float)(j)) / drawhowmany) - lastdrawnrotation1 * (1 - ((float)(j)) / drawhowmany) + 90, 0, 1, 0);
1023 glRotatef(-rotation2 * (((float)(j)) / drawhowmany) - lastdrawnrotation2 * (1 - ((float)(j)) / drawhowmany) + 90, 0, 0, 1);
1024 glRotatef(-rotation3 * (((float)(j)) / drawhowmany) - lastdrawnrotation3 * (1 - ((float)(j)) / drawhowmany), 0, 1, 0);
1025 glRotatef(smallrotation * (((float)(j)) / drawhowmany) + lastdrawnsmallrotation * (1 - ((float)(j)) / drawhowmany), 1, 0, 0);
1026 glRotatef(smallrotation2 * (((float)(j)) / drawhowmany) + lastdrawnsmallrotation2 * (1 - ((float)(j)) / drawhowmany), 0, 1, 0);
1029 if (Person::players[owner]->animTarget == staffhitanim || Person::players[owner]->animCurrent == staffhitanim || Person::players[owner]->animTarget == staffhitreversedanim || Person::players[owner]->animCurrent == staffhitreversedanim) {
1030 glTranslatef(0, 0, -.3);
1032 if (Person::players[owner]->animTarget == staffgroundsmashanim || Person::players[owner]->animCurrent == staffgroundsmashanim || Person::players[owner]->animTarget == staffspinhitreversedanim || Person::players[owner]->animCurrent == staffspinhitreversedanim || Person::players[owner]->animTarget == staffspinhitanim || Person::players[owner]->animCurrent == staffspinhitanim) {
1033 glTranslatef(0, 0, -.1);
1037 glEnable(GL_LIGHTING);
1040 if (!bloody || !bloodtoggle)
1041 throwingknifemodel.drawdifftex(knifetextureptr);
1044 throwingknifemodel.drawdifftex(lightbloodknifetextureptr);
1046 throwingknifemodel.drawdifftex(bloodknifetextureptr);
1050 if (!bloody || !bloodtoggle)
1051 swordmodel.drawdifftex(swordtextureptr);
1054 swordmodel.drawdifftex(lightbloodswordtextureptr);
1056 swordmodel.drawdifftex(bloodswordtextureptr);
1060 staffmodel.drawdifftex(stafftextureptr);
1067 lastdrawnposition = position;
1068 lastdrawntippoint = tippoint;
1069 lastdrawnrotation1 = rotation1;
1070 lastdrawnrotation2 = rotation2;
1071 lastdrawnrotation3 = rotation3;
1072 lastdrawnbigrotation = bigrotation;
1073 lastdrawnbigtilt = bigtilt;
1074 lastdrawnbigtilt2 = bigtilt2;
1075 lastdrawnsmallrotation = smallrotation;
1076 lastdrawnsmallrotation2 = smallrotation2;
1078 lastdrawnanim = Person::players[owner]->animCurrent;
1081 glMatrixMode(GL_MODELVIEW);
1084 glTranslatef(position.x, position.y - .02, position.z);
1085 glRotatef(bigrotation, 0, 1, 0);
1086 glRotatef(bigtilt2, 1, 0, 0);
1087 glRotatef(bigtilt, 0, 0, 1);
1088 glRotatef(-rotation1 + 90, 0, 1, 0);
1089 glRotatef(-rotation2 + 90, 0, 0, 1);
1090 glRotatef(-rotation3, 0, 1, 0);
1091 glRotatef(smallrotation, 1, 0, 0);
1092 glRotatef(smallrotation2, 0, 1, 0);
1093 glTranslatef(0, 0, length);
1094 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1103 void Weapon::drop(XYZ v, XYZ tv, bool sethitsomething)
1109 if (sethitsomething) {
1117 void Weapon::thrown(XYZ v, bool sethitsomething)
1119 drop(v, v, sethitsomething);
1126 glAlphaFunc(GL_GREATER, 0.9);
1127 glEnable(GL_TEXTURE_2D);
1129 glEnable(GL_CULL_FACE);
1130 glCullFace(GL_FRONT);
1133 for (std::vector<Weapon>::iterator weapon = begin(); weapon != end(); ++weapon) {