2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2017 - 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 int bloodtoggle;
45 extern bool autoslomo;
46 extern float camerashake;
48 extern float viewdistance;
49 extern float blackout;
51 extern int numthrowkill;
53 Model Weapon::throwingknifemodel;
54 Texture Weapon::knifetextureptr;
55 Texture Weapon::lightbloodknifetextureptr;
56 Texture Weapon::bloodknifetextureptr;
58 Model Weapon::swordmodel;
59 Texture Weapon::swordtextureptr;
60 Texture Weapon::lightbloodswordtextureptr;
61 Texture Weapon::bloodswordtextureptr;
63 Model Weapon::staffmodel;
64 Texture Weapon::stafftextureptr;
66 Weapon::Weapon(int t, int o)
80 void Weapon::setType(int t)
100 /* Load weapons models and textures */
103 LOG("Loading weapon data...");
105 knifetextureptr.load("Textures/Knife.png", 0);
106 bloodknifetextureptr.load("Textures/BloodKnife.png", 0);
107 lightbloodknifetextureptr.load("Textures/BloodKnifeLight.png", 0);
108 swordtextureptr.load("Textures/Sword.jpg", 1);
109 bloodswordtextureptr.load("Textures/SwordBlood.jpg", 1);
110 lightbloodswordtextureptr.load("Textures/SwordBloodLight.jpg", 1);
111 stafftextureptr.load("Textures/Staff.jpg", 1);
113 throwingknifemodel.load("Models/ThrowingKnife.solid");
114 throwingknifemodel.Scale(.001, .001, .001);
115 throwingknifemodel.Rotate(90, 0, 0);
116 throwingknifemodel.Rotate(0, 90, 0);
117 throwingknifemodel.flat = 0;
118 throwingknifemodel.CalculateNormals(1);
120 swordmodel.load("Models/Sword.solid");
121 swordmodel.Scale(.001, .001, .001);
122 swordmodel.Rotate(90, 0, 0);
123 swordmodel.Rotate(0, 90, 0);
124 swordmodel.Rotate(0, 0, 90);
126 swordmodel.CalculateNormals(1);
128 staffmodel.load("Models/Staff.solid");
129 staffmodel.Scale(.005, .005, .005);
130 staffmodel.Rotate(90, 0, 0);
131 staffmodel.Rotate(0, 90, 0);
132 staffmodel.Rotate(0, 0, 90);
134 staffmodel.CalculateNormals(1);
137 void Weapon::doStuff(int i)
139 static int whichpatchx, whichpatchz, whichhit;
140 static XYZ start, end, colpoint, normalrot, footvel, footpoint;
141 static XYZ terrainnormal;
144 static XYZ newpoint1, newpoint2;
145 static float friction = 3.5;
146 static float elasticity = .4;
147 static XYZ bounceness;
148 static float frictionness;
149 static float closestdistance;
150 static float distance;
152 static XYZ closestpoint;
153 static XYZ closestswordpoint;
154 static float tempmult;
156 if (multiplier <= 0) {
163 if (damage >= 2 && type == staff && owner != -1) { // the staff breaks
164 emit_sound_at(staffbreaksound, tippoint);
166 for (int j = 0; j < 40; j++) {
167 tempvel.x = float(abs(Random() % 100) - 50) / 20;
168 tempvel.y = float(abs(Random() % 100) - 50) / 20;
169 tempvel.z = float(abs(Random() % 100) - 50) / 20;
170 Sprite::MakeSprite(splintersprite, position + (tippoint - position) * ((float)j - 8) / 32, tempvel * .5, 115 / 255, 73 / 255, 12 / 255, .1, 1);
173 Person::players[owner]->weaponactive = -1;
174 Person::players[owner]->num_weapons--;
175 if (Person::players[owner]->num_weapons) {
176 Person::players[owner]->weaponids[0] = Person::players[owner]->weaponids[Person::players[owner]->num_weapons];
177 if (Person::players[owner]->weaponstuck == Person::players[owner]->num_weapons) {
178 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;
215 XYZ temppoint1, temppoint2;
219 temppoint2 = normalrot;
220 distance = findDistance(&temppoint1, &temppoint2);
221 rotation2 = asin((temppoint1.y - temppoint2.y) / distance);
222 rotation2 *= 360 / 6.28;
225 rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2));
226 rotation1 *= 360 / 6.28;
227 if (temppoint1.x > temppoint2.x) {
228 rotation1 = 360 - rotation1;
238 emit_sound_at(knifesheathesound, position, 128.);
242 Sprite::MakeSprite(cloudimpactsprite, position, velocity, 1, 1, 1, .8, .3);
246 position -= velocity * multiplier;
247 tippoint -= velocity * multiplier;
248 tipvelocity = velocity;
254 if (velocity.x || velocity.y || velocity.z) {
255 for (unsigned j = 0; j < Person::players.size(); j++) {
257 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;
258 if (owner == -1 && distsqflat(&position, &Person::players[j]->coords) < 1.5 &&
259 distsq(&position, &Person::players[j]->coords) < 4 && Person::players[j]->weaponstuck == -1 &&
260 !Person::players[j]->skeleton.free && (int(j) != oldowner)) {
261 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) {
262 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)) ||
263 (Person::players[j]->creature == rabbittype && Random() % 2 == 0 && Person::players[j]->aitype == attacktypecutoff && Person::players[j]->weaponactive == -1)) {
264 emit_sound_at(knifedrawsound, Person::players[j]->coords, 128.);
266 Person::players[j]->animTarget = removeknifeanim;
267 Person::players[j]->frameTarget = 1;
268 Person::players[j]->target = 1;
269 Person::players[j]->takeWeapon(i);
271 Person::players[j]->aitype = attacktypecutoff;
276 Person::players[j]->num_weapons++;
277 Person::players[j]->weaponstuck = Person::players[j]->num_weapons - 1;
278 if (normaldotproduct(Person::players[j]->facing, velocity) > 0) {
279 Person::players[j]->weaponstuckwhere = 1;
281 Person::players[j]->weaponstuckwhere = 0;
284 Person::players[j]->weaponids[Person::players[j]->num_weapons - 1] = i;
286 Person::players[j]->RagDoll(0);
287 Person::players[j]->jointVel(abdomen) += velocity * 2;
288 Person::players[j]->jointVel(neck) += velocity * 2;
289 Person::players[j]->jointVel(rightshoulder) += velocity * 2;
290 Person::players[j]->jointVel(leftshoulder) += velocity * 2;
291 if (bloodtoggle && !Tutorial::active) {
292 Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 0, 0, .8, .3);
294 if (Tutorial::active) {
295 Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 1, 1, .8, .3);
297 footvel = tippoint - position;
299 if (bloodtoggle && !Tutorial::active) {
300 Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * -1, 1, 0, 0, .6, 1);
303 if (!Tutorial::active) {
304 if (Person::players[j]->weaponstuckwhere == 0) {
305 Person::players[j]->DoBloodBig(2, 205);
307 if (Person::players[j]->weaponstuckwhere == 1) {
308 Person::players[j]->DoBloodBig(2, 200);
310 Person::players[j]->damage += 200 / Person::players[j]->armorhigh;
311 Person::players[j]->deathbleeding = 1;
312 Person::players[j]->bloodloss += (200 + abs((float)(Random() % 40)) - 20) / Person::players[j]->armorhigh;
318 emit_sound_at(fleshstabsound, position, 128.);
320 if (Animation::animations[Person::players[0]->animTarget].height == highheight) {
321 award_bonus(0, ninja);
323 award_bonus(0, Bullseyebonus);
332 if (position.y < terrain.getHeight(position.x, position.z)) {
333 if (terrain.getOpacity(position.x, position.z) < .2) {
335 if (terrain.lineTerrain(oldposition, position, &colpoint) != -1) {
336 position = colpoint * terrain.scale;
338 position.y = terrain.getHeight(position.x, position.z);
341 terrain.MakeDecal(shadowdecalpermanent, position, .06, .5, 0);
342 normalrot = terrain.getNormal(position.x, position.z) * -1;
344 glMatrixMode(GL_MODELVIEW);
348 glRotatef(bigrotation, 0, 1, 0);
349 glRotatef(bigtilt2, 1, 0, 0);
350 glRotatef(bigtilt, 0, 0, 1);
351 glRotatef(-rotation1 + 90, 0, 1, 0);
352 glRotatef(-rotation2 + 90, 0, 0, 1);
353 glRotatef(-rotation3, 0, 1, 0);
354 glRotatef(smallrotation, 1, 0, 0);
355 glRotatef(smallrotation2, 0, 1, 0);
356 glTranslatef(0, 0, 1);
357 glGetFloatv(GL_MODELVIEW_MATRIX, M);
362 position -= tippoint * .15;
363 XYZ temppoint1, temppoint2;
372 emit_sound_at(knifesheathesound, position, 128.);
375 terrainlight = terrain.getLighting(position.x, position.z);
376 if (environment == snowyenvironment) {
377 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4) {
378 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
380 } else if (environment == grassyenvironment) {
381 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4) {
382 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x * 90 / 255, terrainlight.y * 70 / 255, terrainlight.z * 8 / 255, .5, .5);
384 } else if (environment == desertenvironment) {
385 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4) {
386 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x * 190 / 255, terrainlight.y * 170 / 255, terrainlight.z * 108 / 255, .5, .7);
394 position -= velocity * multiplier;
395 tippoint -= velocity * multiplier;
396 tipvelocity = velocity;
399 if (velocity.x != 0 || velocity.z != 0 || velocity.y != 0) {
400 velocity.y += gravity * multiplier;
402 XYZ temppoint1, temppoint2;
406 temppoint2 = velocity;
407 distance = findDistance(&temppoint1, &temppoint2);
408 rotation2 = asin((temppoint1.y - temppoint2.y) / distance);
409 rotation2 *= 360 / 6.28;
412 rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2));
413 rotation1 *= 360 / 6.28;
420 if (temppoint1.x > temppoint2.x) {
421 rotation1 = 360 - rotation1;
431 tempmult = multiplier;
433 for (int l = 0; l < 10; l++) {
434 if (owner == -1 && (velocity.x || velocity.y || velocity.z) && physics) {
436 position += velocity * multiplier;
437 tippoint += tipvelocity * multiplier;
440 midp = (position * mass + tippoint * tipmass) / (mass + tipmass);
441 vel = tippoint - midp;
443 newpoint1 = midp - vel * length * (tipmass / (mass + tipmass));
444 newpoint2 = midp + vel * length * (mass / (mass + tipmass));
446 if (freetime > .04) {
447 velocity = velocity + (newpoint1 - position) / multiplier;
448 tipvelocity = tipvelocity + (newpoint2 - tippoint) / multiplier;
451 position = newpoint1;
452 tippoint = newpoint2;
455 whichpatchx = (position.x) / (terrain.size / subdivision * terrain.scale);
456 whichpatchz = (position.z) / (terrain.size / subdivision * terrain.scale);
457 if (whichpatchx > 0 && whichpatchz > 0 && whichpatchx < subdivision && whichpatchz < subdivision) {
458 for (unsigned int j = 0; j < terrain.patchobjects[whichpatchx][whichpatchz].size(); j++) {
459 unsigned int k = terrain.patchobjects[whichpatchx][whichpatchz][j];
463 start = tippoint - (position - tippoint) / 5;
464 end = position + (position - tippoint) / 30;
465 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
466 if (whichhit != -1) {
468 diff = (colpoint - position);
472 tippoint += (colpoint - position) + diff * .05;
473 position = colpoint + diff * .05;
474 oldtippoint = tippoint;
475 oldposition = tippoint;
478 start = position - (tippoint - position) / 5;
479 end = tippoint + (tippoint - position) / 30;
480 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
481 if (whichhit != -1) {
483 diff = (colpoint - tippoint);
487 position += (colpoint - tippoint) + diff * .05;
488 tippoint = colpoint + diff * .05;
489 oldposition = position;
490 oldtippoint = tippoint;
497 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
498 if (whichhit != -1) {
501 terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1;
502 ReflectVector(&velocity, &terrainnormal);
503 position += terrainnormal * .002;
505 bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal)));
506 if (findLengthfast(&velocity) < findLengthfast(&bounceness)) {
509 frictionness = abs(normaldotproduct(velocity, terrainnormal));
510 velocity -= bounceness;
511 if (1 - friction * frictionness > 0) {
512 velocity *= 1 - friction * frictionness;
516 velocity += bounceness * elasticity;
518 if (findLengthfast(&bounceness) > 1) {
521 whichsound = footstepsound3 + abs(Random() % 2);
523 whichsound = clank1sound + abs(Random() % 4);
525 emit_sound_at(whichsound, position, 128 * findLengthfast(&bounceness));
530 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
531 if (whichhit != -1) {
534 terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1;
535 ReflectVector(&tipvelocity, &terrainnormal);
536 tippoint += terrainnormal * .002;
538 bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal)));
539 if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness)) {
542 frictionness = abs(normaldotproduct(tipvelocity, terrainnormal));
543 tipvelocity -= bounceness;
544 if (1 - friction * frictionness > 0) {
545 tipvelocity *= 1 - friction * frictionness;
549 tipvelocity += bounceness * elasticity;
551 if (findLengthfast(&bounceness) > 1) {
554 whichsound = footstepsound3 + abs(Random() % 2);
556 whichsound = clank1sound + abs(Random() % 4);
558 emit_sound_at(whichsound, position, 128 * findLengthfast(&bounceness));
562 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) {
563 for (int m = 0; m < 2; m++) {
564 mid = (position * (21 + (float)m * 10) + tippoint * (19 - (float)m * 10)) / 40;
566 oldmid = (oldposition * (21 + (float)m * 10) + oldtippoint * (19 - (float)m * 10)) / 40;
570 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
571 if (whichhit != -1) {
574 terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1;
575 ReflectVector(&velocity, &terrainnormal);
577 bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal)));
578 if (findLengthfast(&velocity) < findLengthfast(&bounceness)) {
581 frictionness = abs(normaldotproduct(velocity, terrainnormal));
582 velocity -= bounceness;
583 if (1 - friction * frictionness > 0) {
584 velocity *= 1 - friction * frictionness;
588 velocity += bounceness * elasticity;
590 if (findLengthfast(&bounceness) > 1) {
593 whichsound = footstepsound3 + abs(Random() % 2);
595 whichsound = clank1sound + abs(Random() % 4);
597 emit_sound_at(whichsound, mid, 128 * findLengthfast(&bounceness));
599 position += (mid - oldmid2) * (20 / (1 + (float)m * 10));
602 mid = (position * (19 - (float)m * 10) + tippoint * (21 + (float)m * 10)) / 40;
604 oldmid = (oldposition * (19 - (float)m * 10) + oldtippoint * (21 + (float)m * 10)) / 40;
608 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
609 if (whichhit != -1) {
612 terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1;
613 ReflectVector(&tipvelocity, &terrainnormal);
615 bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal)));
616 if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness)) {
619 frictionness = abs(normaldotproduct(tipvelocity, terrainnormal));
620 tipvelocity -= bounceness;
621 if (1 - friction * frictionness > 0) {
622 tipvelocity *= 1 - friction * frictionness;
626 tipvelocity += bounceness * elasticity;
628 if (findLengthfast(&bounceness) > 1) {
631 whichsound = footstepsound3 + abs(Random() % 2);
633 whichsound = clank1sound + abs(Random() % 4);
635 emit_sound_at(whichsound, mid, 128 * findLengthfast(&bounceness));
637 tippoint += (mid - oldmid2) * (20 / (1 + (float)m * 10));
643 whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw);
644 if (whichhit != -1) {
646 closestdistance = -1;
647 closestswordpoint = colpoint;
648 point[0] = DoRotation(Object::objects[k]->model.getTriangleVertex(whichhit, 0), 0, Object::objects[k]->yaw, 0) + Object::objects[k]->position;
649 point[1] = DoRotation(Object::objects[k]->model.getTriangleVertex(whichhit, 1), 0, Object::objects[k]->yaw, 0) + Object::objects[k]->position;
650 point[2] = DoRotation(Object::objects[k]->model.getTriangleVertex(whichhit, 2), 0, Object::objects[k]->yaw, 0) + Object::objects[k]->position;
651 if (DistancePointLine(&closestswordpoint, &point[0], &point[1], &distance, &colpoint)) {
652 if (distance < closestdistance || closestdistance == -1) {
653 closestpoint = colpoint;
654 closestdistance = distance;
657 if (DistancePointLine(&closestswordpoint, &point[1], &point[2], &distance, &colpoint)) {
658 if (distance < closestdistance || closestdistance == -1) {
659 closestpoint = colpoint;
660 closestdistance = distance;
663 if (DistancePointLine(&closestswordpoint, &point[2], &point[0], &distance, &colpoint)) {
664 if (distance < closestdistance || closestdistance == -1) {
665 closestpoint = colpoint;
666 closestdistance = distance;
669 if (closestdistance != -1 && isnormal(closestdistance)) {
670 if (DistancePointLine(&closestpoint, &position, &tippoint, &distance, &colpoint)) {
671 closestswordpoint = colpoint;
672 velocity += (closestpoint - closestswordpoint);
673 tipvelocity += (closestpoint - closestswordpoint);
674 position += (closestpoint - closestswordpoint);
675 tippoint += (closestpoint - closestswordpoint);
683 whichhit = terrain.lineTerrain(oldposition, position, &colpoint);
684 if (whichhit != -1 || position.y < terrain.getHeight(position.x, position.z)) {
686 if (whichhit != -1) {
687 position = colpoint * terrain.scale;
689 position.y = terrain.getHeight(position.x, position.z);
692 terrainnormal = terrain.getNormal(position.x, position.z);
693 ReflectVector(&velocity, &terrainnormal);
694 position += terrainnormal * .002;
695 bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal)));
696 if (findLengthfast(&velocity) < findLengthfast(&bounceness)) {
699 frictionness = abs(normaldotproduct(velocity, terrainnormal));
700 velocity -= bounceness;
701 if (1 - friction * frictionness > 0) {
702 velocity *= 1 - friction * frictionness;
706 if (terrain.getOpacity(position.x, position.z) < .2) {
707 velocity += bounceness * elasticity * .3;
709 velocity += bounceness * elasticity;
712 if (findLengthfast(&bounceness) > 1) {
714 if (terrain.getOpacity(position.x, position.z) > .2) {
716 whichsound = footstepsound3 + abs(Random() % 2);
718 whichsound = clank1sound + abs(Random() % 4);
721 whichsound = footstepsound + abs(Random() % 2);
723 emit_sound_at(whichsound, position,
724 findLengthfast(&bounceness) * (terrain.getOpacity(position.x, position.z) > .2 ? 128. : 32.));
726 if (terrain.getOpacity(position.x, position.z) < .2) {
728 terrainlight = terrain.getLighting(position.x, position.z);
729 if (environment == snowyenvironment) {
730 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4) {
731 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
733 } else if (environment == grassyenvironment) {
734 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4) {
735 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x * 90 / 255, terrainlight.y * 70 / 255, terrainlight.z * 8 / 255, .5, .5);
737 } else if (environment == desertenvironment) {
738 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4) {
739 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x * 190 / 255, terrainlight.y * 170 / 255, terrainlight.z * 108 / 255, .5, .7);
745 whichhit = terrain.lineTerrain(oldtippoint, tippoint, &colpoint);
746 if (whichhit != -1 || tippoint.y < terrain.getHeight(tippoint.x, tippoint.z)) {
747 if (whichhit != -1) {
748 tippoint = colpoint * terrain.scale;
750 tippoint.y = terrain.getHeight(tippoint.x, tippoint.z);
753 terrainnormal = terrain.getNormal(tippoint.x, tippoint.z);
754 ReflectVector(&tipvelocity, &terrainnormal);
755 tippoint += terrainnormal * .002;
756 bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal)));
757 if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness)) {
760 frictionness = abs(normaldotproduct(tipvelocity, terrainnormal));
761 tipvelocity -= bounceness;
762 if (1 - friction * frictionness > 0) {
763 tipvelocity *= 1 - friction * frictionness;
767 if (terrain.getOpacity(tippoint.x, tippoint.z) < .2) {
768 tipvelocity += bounceness * elasticity * .3;
770 tipvelocity += bounceness * elasticity;
773 if (findLengthfast(&bounceness) > 1) {
775 if (terrain.getOpacity(tippoint.x, tippoint.z) > .2) {
777 whichsound = footstepsound3 + abs(Random() % 2);
779 whichsound = clank1sound + abs(Random() % 4);
782 whichsound = footstepsound + abs(Random() % 2);
784 emit_sound_at(whichsound, tippoint,
785 findLengthfast(&bounceness) * (terrain.getOpacity(tippoint.x, tippoint.z) > .2 ? 128. : 32.));
787 if (terrain.getOpacity(tippoint.x, tippoint.z) < .2) {
789 terrainlight = terrain.getLighting(tippoint.x, tippoint.z);
790 if (environment == snowyenvironment) {
791 if (distsq(&tippoint, &viewer) < viewdistance * viewdistance / 4) {
792 Sprite::MakeSprite(cloudsprite, tippoint, tipvelocity, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
794 } else if (environment == grassyenvironment) {
795 if (distsq(&tippoint, &viewer) < viewdistance * viewdistance / 4) {
796 Sprite::MakeSprite(cloudsprite, tippoint, tipvelocity, terrainlight.x * 90 / 255, terrainlight.y * 70 / 255, terrainlight.z * 8 / 255, .5, .5);
798 } else if (environment == desertenvironment) {
799 if (distsq(&tippoint, &viewer) < viewdistance * viewdistance / 4) {
800 Sprite::MakeSprite(cloudsprite, tippoint, tipvelocity, terrainlight.x * 190 / 255, terrainlight.y * 170 / 255, terrainlight.z * 108 / 255, .5, .7);
808 mid = position + tippoint;
810 mid += (position - mid) / 20;
812 if (mid.y < terrain.getHeight(mid.x, mid.z)) {
814 mid.y = terrain.getHeight(mid.x, mid.z);
816 terrainnormal = terrain.getNormal(mid.x, mid.z);
817 ReflectVector(&velocity, &terrainnormal);
818 //mid+=terrainnormal*.002;
819 bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal)));
820 if (findLengthfast(&velocity) < findLengthfast(&bounceness)) {
823 frictionness = abs(normaldotproduct(velocity, terrainnormal));
824 velocity -= bounceness;
825 if (1 - friction * frictionness > 0) {
826 velocity *= 1 - friction * frictionness;
830 if (terrain.getOpacity(mid.x, mid.z) < .2) {
831 velocity += bounceness * elasticity * .3;
833 velocity += bounceness * elasticity;
836 if (findLengthfast(&bounceness) > 1) {
838 if (terrain.getOpacity(mid.x, mid.z) > .2) {
840 whichsound = footstepsound3 + abs(Random() % 2);
843 whichsound = clank1sound + abs(Random() % 4);
846 whichsound = footstepsound + abs(Random() % 2);
848 emit_sound_at(whichsound, mid,
849 findLengthfast(&bounceness) * (terrain.getOpacity(position.x, position.z) > .2
853 position += (mid - oldmid) * 20;
856 mid = position + tippoint;
858 mid += (tippoint - mid) / 20;
860 if (mid.y < terrain.getHeight(mid.x, mid.z)) {
862 mid.y = terrain.getHeight(mid.x, mid.z);
864 terrainnormal = terrain.getNormal(mid.x, mid.z);
865 ReflectVector(&tipvelocity, &terrainnormal);
866 //mid+=terrainnormal*.002;
867 bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal)));
868 if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness)) {
871 frictionness = abs(normaldotproduct(tipvelocity, terrainnormal));
872 tipvelocity -= bounceness;
873 if (1 - friction * frictionness > 0) {
874 tipvelocity *= 1 - friction * frictionness;
878 if (terrain.getOpacity(mid.x, mid.z) < .2) {
879 tipvelocity += bounceness * elasticity * .3;
881 tipvelocity += bounceness * elasticity;
884 if (findLengthfast(&bounceness) > 1) {
886 if (terrain.getOpacity(mid.x, mid.z) > .2) {
888 whichsound = footstepsound3 + abs(Random() % 2);
891 whichsound = clank1sound + abs(Random() % 4);
894 whichsound = footstepsound + abs(Random() % 2);
896 emit_sound_at(whichsound, mid,
897 findLengthfast(&bounceness) * (terrain.getOpacity(position.x, position.z) > .2
901 tippoint += (mid - oldmid) * 20;
904 velocity.y += gravity * multiplier;
905 tipvelocity.y += gravity * multiplier;
908 XYZ temppoint1, temppoint2;
911 temppoint1 = position;
912 temppoint2 = tippoint;
913 distance = findDistance(&temppoint1, &temppoint2);
914 rotation2 = asin((temppoint1.y - temppoint2.y) / distance);
915 rotation2 *= 360 / 6.28;
918 rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2));
919 rotation1 *= 360 / 6.28;
926 if (temppoint1.x > temppoint2.x) {
927 rotation1 = 360 - rotation1;
931 if (findLengthfast(&velocity) < .3 && findLengthfast(&tipvelocity) < .3 && hitsomething) {
932 freetime += multiplier;
942 multiplier = tempmult;
943 if (blooddrip && bloody) {
944 blooddripdelay -= blooddrip * multiplier / 2;
945 blooddrip -= multiplier;
952 if (blooddripdelay < 0 && bloodtoggle) {
956 bloodloc = position + (tippoint - position) * .7;
960 Sprite::MakeSprite(bloodsprite, bloodloc, bloodvel, 1, 1, 1, .03, 1);
965 flamedelay -= multiplier;
966 if (onfire && flamedelay <= 0) {
968 flamedelay -= multiplier;
971 normalrot = Person::players[owner]->velocity;
975 if (Person::players[owner]->onterrain) {
979 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);
980 Sprite::setLastSpriteSpeed(4);
981 Sprite::setLastSpriteAlivetime(.3);
985 if (!onfire && owner == -1 && type != staff) {
986 flamedelay -= multiplier;
987 if (flamedelay <= 0) {
989 flamedelay -= multiplier;
991 if (Random() % 50 == 0 && distsq(&position, &viewer) > 80) {
993 shinepoint = position + (tippoint - position) * (((float)abs(Random() % 100)) / 100);
994 Sprite::MakeSprite(weaponshinesprite, shinepoint, normalrot, 1, 1, 1, (.1 + (float)abs(Random() % 100) / 200 - .25) * 1 / 3 * fast_sqrt(findDistance(&shinepoint, &viewer)), 1);
995 Sprite::setLastSpriteSpeed(4);
996 Sprite::setLastSpriteAlivetime(.3);
1002 void Weapons::DoStuff()
1006 for (std::vector<Weapon>::iterator weapon = begin(); weapon != end(); ++weapon) {
1007 weapon->doStuff(i++);
1013 static XYZ terrainlight;
1014 static GLfloat M[16];
1016 if ((frustum.SphereInFrustum(position.x, position.y, position.z, 1) &&
1017 distsq(&viewer, &position) < viewdistance * viewdistance)) {
1021 if (velocity.x && !physics) {
1027 if (Person::players[owner]->occluded < 25) {
1028 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) {
1033 (Person::players[owner]->animTarget == knifeslashstartanim ||
1034 Person::players[owner]->animTarget == swordsneakattackanim ||
1035 (Person::players[owner]->animCurrent == staffhitanim && Person::players[owner]->frameCurrent > 1) ||
1036 (Person::players[owner]->animCurrent == staffhitreversedanim && Person::players[owner]->frameCurrent > 1) ||
1037 (Person::players[owner]->animCurrent == staffspinhitanim && Person::players[owner]->frameCurrent > 1) ||
1038 (Person::players[owner]->animCurrent == staffspinhitreversedanim && Person::players[owner]->frameCurrent > 1) ||
1039 (Person::players[owner]->animCurrent == staffgroundsmashanim && Person::players[owner]->frameCurrent > 1) ||
1040 (Person::players[owner]->animTarget == swordslashanim && Person::players[owner]->frameTarget < 7) ||
1041 Person::players[owner]->animTarget == crouchstabanim ||
1042 Person::players[owner]->animTarget == swordslashreversalanim ||
1043 Person::players[owner]->animTarget == swordslashreversedanim ||
1044 Person::players[owner]->animTarget == knifefollowanim ||
1045 Person::players[owner]->animTarget == swordgroundstabanim ||
1046 Person::players[owner]->animTarget == knifethrowanim) &&
1047 Person::players[owner]->animTarget == lastdrawnanim &&
1048 !Person::players[owner]->skeleton.free) {
1053 if (Person::players[owner]->animTarget == swordgroundstabanim) {
1054 lastdrawnrotation1 = rotation1;
1055 lastdrawnrotation2 = rotation2;
1056 lastdrawnrotation3 = rotation3;
1057 lastdrawnbigrotation = bigrotation;
1058 lastdrawnbigtilt = bigtilt;
1059 lastdrawnbigtilt2 = bigtilt2;
1060 lastdrawnsmallrotation = smallrotation;
1061 lastdrawnsmallrotation2 = smallrotation2;
1065 terrainlight = terrain.getLighting(position.x, position.z);
1066 if (drawhowmany > 0) {
1067 glAlphaFunc(GL_GREATER, 0.01);
1069 for (int j = drawhowmany; j > 0; j--) {
1070 glMatrixMode(GL_MODELVIEW);
1072 glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, j / drawhowmany);
1074 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));
1076 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));
1078 glRotatef(bigrotation * (((float)(j)) / drawhowmany) + lastdrawnbigrotation * (1 - ((float)(j)) / drawhowmany), 0, 1, 0);
1079 glRotatef(bigtilt2 * (((float)(j)) / drawhowmany) + lastdrawnbigtilt2 * (1 - ((float)(j)) / drawhowmany), 1, 0, 0);
1080 glRotatef(bigtilt * (((float)(j)) / drawhowmany) + lastdrawnbigtilt * (1 - ((float)(j)) / drawhowmany), 0, 0, 1);
1081 glRotatef(-rotation1 * (((float)(j)) / drawhowmany) - lastdrawnrotation1 * (1 - ((float)(j)) / drawhowmany) + 90, 0, 1, 0);
1082 glRotatef(-rotation2 * (((float)(j)) / drawhowmany) - lastdrawnrotation2 * (1 - ((float)(j)) / drawhowmany) + 90, 0, 0, 1);
1083 glRotatef(-rotation3 * (((float)(j)) / drawhowmany) - lastdrawnrotation3 * (1 - ((float)(j)) / drawhowmany), 0, 1, 0);
1084 glRotatef(smallrotation * (((float)(j)) / drawhowmany) + lastdrawnsmallrotation * (1 - ((float)(j)) / drawhowmany), 1, 0, 0);
1085 glRotatef(smallrotation2 * (((float)(j)) / drawhowmany) + lastdrawnsmallrotation2 * (1 - ((float)(j)) / drawhowmany), 0, 1, 0);
1088 if (Person::players[owner]->animTarget == staffhitanim || Person::players[owner]->animCurrent == staffhitanim || Person::players[owner]->animTarget == staffhitreversedanim || Person::players[owner]->animCurrent == staffhitreversedanim) {
1089 glTranslatef(0, 0, -.3);
1091 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) {
1092 glTranslatef(0, 0, -.1);
1096 glEnable(GL_LIGHTING);
1099 if (!bloody || !bloodtoggle) {
1100 throwingknifemodel.drawdifftex(knifetextureptr);
1104 throwingknifemodel.drawdifftex(lightbloodknifetextureptr);
1107 throwingknifemodel.drawdifftex(bloodknifetextureptr);
1112 if (!bloody || !bloodtoggle) {
1113 swordmodel.drawdifftex(swordtextureptr);
1117 swordmodel.drawdifftex(lightbloodswordtextureptr);
1120 swordmodel.drawdifftex(bloodswordtextureptr);
1125 staffmodel.drawdifftex(stafftextureptr);
1132 lastdrawnposition = position;
1133 lastdrawntippoint = tippoint;
1134 lastdrawnrotation1 = rotation1;
1135 lastdrawnrotation2 = rotation2;
1136 lastdrawnrotation3 = rotation3;
1137 lastdrawnbigrotation = bigrotation;
1138 lastdrawnbigtilt = bigtilt;
1139 lastdrawnbigtilt2 = bigtilt2;
1140 lastdrawnsmallrotation = smallrotation;
1141 lastdrawnsmallrotation2 = smallrotation2;
1143 lastdrawnanim = Person::players[owner]->animCurrent;
1147 glMatrixMode(GL_MODELVIEW);
1150 glTranslatef(position.x, position.y - .02, position.z);
1151 glRotatef(bigrotation, 0, 1, 0);
1152 glRotatef(bigtilt2, 1, 0, 0);
1153 glRotatef(bigtilt, 0, 0, 1);
1154 glRotatef(-rotation1 + 90, 0, 1, 0);
1155 glRotatef(-rotation2 + 90, 0, 0, 1);
1156 glRotatef(-rotation3, 0, 1, 0);
1157 glRotatef(smallrotation, 1, 0, 0);
1158 glRotatef(smallrotation2, 0, 1, 0);
1159 glTranslatef(0, 0, length);
1160 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1169 void Weapon::drop(XYZ v, XYZ tv, bool sethitsomething)
1175 if (sethitsomething) {
1183 void Weapon::thrown(XYZ v, bool sethitsomething)
1185 drop(v, v, sethitsomething);
1192 glAlphaFunc(GL_GREATER, 0.9);
1193 glEnable(GL_TEXTURE_2D);
1195 glEnable(GL_CULL_FACE);
1196 glCullFace(GL_FRONT);
1199 for (std::vector<Weapon>::iterator weapon = begin(); weapon != end(); ++weapon) {