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/openal_wrapper.hpp"
27 #include "Audio/Sounds.hpp"
29 #include "Level/Awards.hpp"
31 extern float multiplier;
32 extern Terrain terrain;
34 extern int environment;
36 extern FRUSTUM frustum;
38 extern float realmultiplier;
40 extern float slomodelay;
41 extern bool cellophane;
42 extern float texdetail;
43 extern GLubyte bloodText[512 * 512 * 3];
44 extern int bloodtoggle;
45 extern Objects objects;
46 extern bool autoslomo;
47 extern float camerashake;
49 extern float viewdistance;
50 extern float blackout;
52 extern int tutoriallevel;
53 extern int numthrowkill;
55 Model Weapon::throwingknifemodel;
56 Texture Weapon::knifetextureptr;
57 Texture Weapon::lightbloodknifetextureptr;
58 Texture Weapon::bloodknifetextureptr;
60 Model Weapon::swordmodel;
61 Texture Weapon::swordtextureptr;
62 Texture Weapon::lightbloodswordtextureptr;
63 Texture Weapon::bloodswordtextureptr;
65 Model Weapon::staffmodel;
66 Texture Weapon::stafftextureptr;
68 Weapon::Weapon(int t, int o) : owner(o)
81 void Weapon::setType(int t)
101 void Weapon::DoStuff(int i)
103 static int whichpatchx, whichpatchz, whichhit;
104 static XYZ start, end, colpoint, normalrot, footvel, footpoint;
105 static XYZ terrainnormal;
108 static XYZ newpoint1, newpoint2;
109 static float friction = 3.5;
110 static float elasticity = .4;
111 static XYZ bounceness;
112 static float frictionness;
113 static float closestdistance;
114 static float distance;
116 static XYZ closestpoint;
117 static XYZ closestswordpoint;
118 static float tempmult;
120 if (multiplier <= 0) {
127 if (damage >= 2 && type == staff && owner != -1) { // the staff breaks
128 emit_sound_at(staffbreaksound, tippoint);
130 for (int j = 0; j < 40; j++) {
131 tempvel.x = float(abs(Random() % 100) - 50) / 20;
132 tempvel.y = float(abs(Random() % 100) - 50) / 20;
133 tempvel.z = float(abs(Random() % 100) - 50) / 20;
134 Sprite::MakeSprite(splintersprite, position + (tippoint - position) * ((float)j - 8) / 32, tempvel * .5, 115 / 255, 73 / 255, 12 / 255, .1, 1);
137 Person::players[owner]->weaponactive = -1;
138 Person::players[owner]->num_weapons--;
139 if (Person::players[owner]->num_weapons) {
140 Person::players[owner]->weaponids[0] = Person::players[owner]->weaponids[Person::players[owner]->num_weapons];
141 if (Person::players[owner]->weaponstuck == Person::players[owner]->num_weapons)
142 Person::players[owner]->weaponstuck = 0;
153 oldposition = position;
154 oldtippoint = tippoint;
155 if (owner == -1 && (velocity.x || velocity.y || velocity.z) && !physics) { // if the weapon is flying
156 position += velocity * multiplier;
157 tippoint += velocity * multiplier;
158 whichpatchx = position.x / (terrain.size / subdivision * terrain.scale);
159 whichpatchz = position.z / (terrain.size / subdivision * terrain.scale);
160 if (whichpatchx > 0 && whichpatchz > 0 && whichpatchx < subdivision && whichpatchz < subdivision) {
161 if (terrain.patchobjectnum[whichpatchx][whichpatchz]) { // if there are objects where the weapon is
162 for (int j = 0; j < terrain.patchobjectnum[whichpatchx][whichpatchz]; j++) { // check for collision
163 int k = terrain.patchobjects[whichpatchx][whichpatchz][j];
166 whichhit = objects.model[k].LineCheck(&start, &end, &colpoint, &objects.position[k], &objects.yaw[k]);
167 if (whichhit != -1) {
168 if (objects.type[k] == treetrunktype) {
169 objects.model[k].MakeDecal(breakdecal, DoRotation(colpoint - objects.position[k], 0, -objects.yaw[k], 0), .1, 1, Random() % 360);
170 normalrot = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0);
173 position = colpoint - normalrot * .1;
174 else if (type == sword)
175 position = colpoint - normalrot * .2;
176 else if (type == staff)
177 position = colpoint - normalrot * .2;
178 XYZ temppoint1, temppoint2;
182 temppoint2 = normalrot;
183 distance = findDistance(&temppoint1, &temppoint2);
184 rotation2 = asin((temppoint1.y - temppoint2.y) / distance);
185 rotation2 *= 360 / 6.28;
188 rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2));
189 rotation1 *= 360 / 6.28;
190 if (temppoint1.x > temppoint2.x)
191 rotation1 = 360 - rotation1;
200 emit_sound_at(knifesheathesound, position, 128.);
204 Sprite::MakeSprite(cloudimpactsprite, position, velocity, 1, 1, 1, .8, .3);
208 position -= velocity * multiplier;
209 tippoint -= velocity * multiplier;
210 tipvelocity = velocity;
217 if (velocity.x || velocity.y || velocity.z) {
218 for (unsigned j = 0; j < Person::players.size(); j++) {
220 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;
221 if (owner == -1 && distsqflat(&position, &Person::players[j]->coords) < 1.5 &&
222 distsq(&position, &Person::players[j]->coords) < 4 && Person::players[j]->weaponstuck == -1 &&
223 !Person::players[j]->skeleton.free && (int(j) != oldowner)) {
224 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) {
225 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)) ||
226 (Person::players[j]->creature == rabbittype && Random() % 2 == 0 && Person::players[j]->aitype == attacktypecutoff && Person::players[j]->weaponactive == -1)) {
227 emit_sound_at(knifedrawsound, Person::players[j]->coords, 128.);
229 Person::players[j]->animTarget = removeknifeanim;
230 Person::players[j]->frameTarget = 1;
231 Person::players[j]->target = 1;
232 Person::players[j]->takeWeapon(i);
234 Person::players[j]->aitype = attacktypecutoff;
238 Person::players[j]->num_weapons++;
239 Person::players[j]->weaponstuck = Person::players[j]->num_weapons - 1;
240 if (normaldotproduct(Person::players[j]->facing, velocity) > 0)
241 Person::players[j]->weaponstuckwhere = 1;
243 Person::players[j]->weaponstuckwhere = 0;
245 Person::players[j]->weaponids[Person::players[j]->num_weapons - 1] = i;
247 Person::players[j]->RagDoll(0);
248 Person::players[j]->jointVel(abdomen) += velocity * 2;
249 Person::players[j]->jointVel(neck) += velocity * 2;
250 Person::players[j]->jointVel(rightshoulder) += velocity * 2;
251 Person::players[j]->jointVel(leftshoulder) += velocity * 2;
252 if (bloodtoggle && tutoriallevel != 1)
253 Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 0, 0, .8, .3);
254 if (tutoriallevel == 1)
255 Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 1, 1, .8, .3);
256 footvel = tippoint - position;
258 if (bloodtoggle && tutoriallevel != 1)
259 Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * -1, 1, 0, 0, .6, 1);
261 if (tutoriallevel != 1) {
262 if (Person::players[j]->weaponstuckwhere == 0)
263 Person::players[j]->DoBloodBig(2, 205);
264 if (Person::players[j]->weaponstuckwhere == 1)
265 Person::players[j]->DoBloodBig(2, 200);
266 Person::players[j]->damage += 200 / Person::players[j]->armorhigh;
267 Person::players[j]->deathbleeding = 1;
268 Person::players[j]->bloodloss += (200 + abs((float)(Random() % 40)) - 20) / Person::players[j]->armorhigh;
274 emit_sound_at(fleshstabsound, position, 128.);
276 if (Animation::animations[Person::players[0]->animTarget].height == highheight)
277 award_bonus(0, ninja);
279 award_bonus(0, Bullseyebonus);
287 if (position.y < terrain.getHeight(position.x, position.z)) {
288 if (terrain.getOpacity(position.x, position.z) < .2) {
290 if (terrain.lineTerrain(oldposition, position, &colpoint) != -1) {
291 position = colpoint * terrain.scale;
293 position.y = terrain.getHeight(position.x, position.z);
296 terrain.MakeDecal(shadowdecalpermanent, position, .06, .5, 0);
297 normalrot = terrain.getNormal(position.x, position.z) * -1;
299 glMatrixMode(GL_MODELVIEW);
303 glRotatef(bigrotation, 0, 1, 0);
304 glRotatef(bigtilt2, 1, 0, 0);
305 glRotatef(bigtilt, 0, 0, 1);
306 glRotatef(-rotation1 + 90, 0, 1, 0);
307 glRotatef(-rotation2 + 90, 0, 0, 1);
308 glRotatef(-rotation3, 0, 1, 0);
309 glRotatef(smallrotation, 1, 0, 0);
310 glRotatef(smallrotation2, 0, 1, 0);
311 glTranslatef(0, 0, 1);
312 glGetFloatv(GL_MODELVIEW_MATRIX, M);
317 position -= tippoint * .15;
318 XYZ temppoint1, temppoint2;
327 emit_sound_at(knifesheathesound, position, 128.);
330 terrainlight = terrain.getLighting(position.x, position.z);
331 if (environment == snowyenvironment) {
332 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4)
333 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
334 } else if (environment == grassyenvironment) {
335 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4)
336 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x * 90 / 255, terrainlight.y * 70 / 255, terrainlight.z * 8 / 255, .5, .5);
337 } else if (environment == desertenvironment) {
338 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4)
339 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x * 190 / 255, terrainlight.y * 170 / 255, terrainlight.z * 108 / 255, .5, .7);
346 position -= velocity * multiplier;
347 tippoint -= velocity * multiplier;
348 tipvelocity = velocity;
351 if (velocity.x != 0 || velocity.z != 0 || velocity.y != 0) {
352 velocity.y += gravity * multiplier;
354 XYZ temppoint1, temppoint2;
358 temppoint2 = velocity;
359 distance = findDistance(&temppoint1, &temppoint2);
360 rotation2 = asin((temppoint1.y - temppoint2.y) / distance);
361 rotation2 *= 360 / 6.28;
364 rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2));
365 rotation1 *= 360 / 6.28;
372 if (temppoint1.x > temppoint2.x)
373 rotation1 = 360 - rotation1;
383 tempmult = multiplier;
385 for (int l = 0; l < 10; l++) {
386 if (owner == -1 && (velocity.x || velocity.y || velocity.z) && physics) {
388 position += velocity * multiplier;
389 tippoint += tipvelocity * multiplier;
392 midp = (position * mass + tippoint * tipmass) / (mass + tipmass);
393 vel = tippoint - midp;
395 newpoint1 = midp - vel * length * (tipmass / (mass + tipmass));
396 newpoint2 = midp + vel * length * (mass / (mass + tipmass));
398 if (freetime > .04) {
399 velocity = velocity + (newpoint1 - position) / multiplier;
400 tipvelocity = tipvelocity + (newpoint2 - tippoint) / multiplier;
403 position = newpoint1;
404 tippoint = newpoint2;
408 whichpatchx = (position.x) / (terrain.size / subdivision * terrain.scale);
409 whichpatchz = (position.z) / (terrain.size / subdivision * terrain.scale);
410 if (whichpatchx > 0 && whichpatchz > 0 && whichpatchx < subdivision && whichpatchz < subdivision)
411 if (terrain.patchobjectnum[whichpatchx][whichpatchz]) {
412 for (int j = 0; j < terrain.patchobjectnum[whichpatchx][whichpatchz]; j++) {
413 int k = terrain.patchobjects[whichpatchx][whichpatchz][j];
417 start = tippoint - (position - tippoint) / 5;
418 end = position + (position - tippoint) / 30;
419 whichhit = objects.model[k].LineCheck(&start, &end, &colpoint, &objects.position[k], &objects.yaw[k]);
420 if (whichhit != -1) {
422 diff = (colpoint - position);
426 tippoint += (colpoint - position) + diff * .05;
427 position = colpoint + diff * .05;
428 oldtippoint = tippoint;
429 oldposition = tippoint;
432 start = position - (tippoint - position) / 5;
433 end = tippoint + (tippoint - position) / 30;
434 whichhit = objects.model[k].LineCheck(&start, &end, &colpoint, &objects.position[k], &objects.yaw[k]);
435 if (whichhit != -1) {
437 diff = (colpoint - tippoint);
441 position += (colpoint - tippoint) + diff * .05;
442 tippoint = colpoint + diff * .05;
443 oldposition = position;
444 oldtippoint = tippoint;
451 whichhit = objects.model[k].LineCheck(&start, &end, &colpoint, &objects.position[k], &objects.yaw[k]);
452 if (whichhit != -1) {
455 terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1;
456 ReflectVector(&velocity, &terrainnormal);
457 position += terrainnormal * .002;
459 bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal)));
460 if (findLengthfast(&velocity) < findLengthfast(&bounceness))
462 frictionness = abs(normaldotproduct(velocity, terrainnormal));
463 velocity -= bounceness;
464 if (1 - friction * frictionness > 0)
465 velocity *= 1 - friction * frictionness;
468 velocity += bounceness * elasticity;
470 if (findLengthfast(&bounceness) > 1) {
473 whichsound = footstepsound3 + abs(Random() % 2);
475 whichsound = clank1sound + abs(Random() % 4);
476 emit_sound_at(whichsound, position, 128 * findLengthfast(&bounceness));
481 whichhit = objects.model[k].LineCheck(&start, &end, &colpoint, &objects.position[k], &objects.yaw[k]);
482 if (whichhit != -1) {
485 terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1;
486 ReflectVector(&tipvelocity, &terrainnormal);
487 tippoint += terrainnormal * .002;
489 bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal)));
490 if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness))
492 frictionness = abs(normaldotproduct(tipvelocity, terrainnormal));
493 tipvelocity -= bounceness;
494 if (1 - friction * frictionness > 0)
495 tipvelocity *= 1 - friction * frictionness;
498 tipvelocity += bounceness * elasticity;
500 if (findLengthfast(&bounceness) > 1) {
503 whichsound = footstepsound3 + abs(Random() % 2);
505 whichsound = clank1sound + abs(Random() % 4);
506 emit_sound_at(whichsound, position, 128 * findLengthfast(&bounceness));
510 if ((objects.type[k] != boxtype && objects.type[k] != platformtype && objects.type[k] != walltype && objects.type[k] != weirdtype) || objects.pitch[k] != 0)
511 for (int m = 0; m < 2; m++) {
512 mid = (position * (21 + (float)m * 10) + tippoint * (19 - (float)m * 10)) / 40;
514 oldmid = (oldposition * (21 + (float)m * 10) + oldtippoint * (19 - (float)m * 10)) / 40;
518 whichhit = objects.model[k].LineCheck(&start, &end, &colpoint, &objects.position[k], &objects.yaw[k]);
519 if (whichhit != -1) {
522 terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1;
523 ReflectVector(&velocity, &terrainnormal);
525 bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal)));
526 if (findLengthfast(&velocity) < findLengthfast(&bounceness))
528 frictionness = abs(normaldotproduct(velocity, terrainnormal));
529 velocity -= bounceness;
530 if (1 - friction * frictionness > 0)
531 velocity *= 1 - friction * frictionness;
534 velocity += bounceness * elasticity;
536 if (findLengthfast(&bounceness) > 1) {
539 whichsound = footstepsound3 + abs(Random() % 2);
541 whichsound = clank1sound + abs(Random() % 4);
542 emit_sound_at(whichsound, mid, 128 * findLengthfast(&bounceness));
544 position += (mid - oldmid2) * (20 / (1 + (float)m * 10));
547 mid = (position * (19 - (float)m * 10) + tippoint * (21 + (float)m * 10)) / 40;
549 oldmid = (oldposition * (19 - (float)m * 10) + oldtippoint * (21 + (float)m * 10)) / 40;
553 whichhit = objects.model[k].LineCheck(&start, &end, &colpoint, &objects.position[k], &objects.yaw[k]);
554 if (whichhit != -1) {
557 terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1;
558 ReflectVector(&tipvelocity, &terrainnormal);
560 bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal)));
561 if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness))
563 frictionness = abs(normaldotproduct(tipvelocity, terrainnormal));
564 tipvelocity -= bounceness;
565 if (1 - friction * frictionness > 0)
566 tipvelocity *= 1 - friction * frictionness;
569 tipvelocity += bounceness * elasticity;
571 if (findLengthfast(&bounceness) > 1) {
574 whichsound = footstepsound3 + abs(Random() % 2);
576 whichsound = clank1sound + abs(Random() % 4);
577 emit_sound_at(whichsound, mid, 128 * findLengthfast(&bounceness));
579 tippoint += (mid - oldmid2) * (20 / (1 + (float)m * 10));
585 whichhit = objects.model[k].LineCheck(&start, &end, &colpoint, &objects.position[k], &objects.yaw[k]);
586 if (whichhit != -1) {
588 closestdistance = -1;
589 closestswordpoint = colpoint; //(position+tippoint)/2;
590 point[0] = DoRotation(objects.model[k].vertex[objects.model[k].Triangles[whichhit].vertex[0]], 0, objects.yaw[k], 0) + objects.position[k];
591 point[1] = DoRotation(objects.model[k].vertex[objects.model[k].Triangles[whichhit].vertex[1]], 0, objects.yaw[k], 0) + objects.position[k];
592 point[2] = DoRotation(objects.model[k].vertex[objects.model[k].Triangles[whichhit].vertex[2]], 0, objects.yaw[k], 0) + objects.position[k];
593 if (DistancePointLine(&closestswordpoint, &point[0], &point[1], &distance, &colpoint )) {
594 if (distance < closestdistance || closestdistance == -1) {
595 closestpoint = colpoint;
596 closestdistance = distance;
599 if (DistancePointLine(&closestswordpoint, &point[1], &point[2], &distance, &colpoint )) {
600 if (distance < closestdistance || closestdistance == -1) {
601 closestpoint = colpoint;
602 closestdistance = distance;
605 if (DistancePointLine(&closestswordpoint, &point[2], &point[0], &distance, &colpoint )) {
606 if (distance < closestdistance || closestdistance == -1) {
607 closestpoint = colpoint;
608 closestdistance = distance;
611 if (closestdistance != -1 && isnormal(closestdistance)) {
612 if (DistancePointLine(&closestpoint, &position, &tippoint, &distance, &colpoint )) {
613 closestswordpoint = colpoint;
614 velocity += (closestpoint - closestswordpoint);
615 tipvelocity += (closestpoint - closestswordpoint);
616 position += (closestpoint - closestswordpoint);
617 tippoint += (closestpoint - closestswordpoint);
625 whichhit = terrain.lineTerrain(oldposition, position, &colpoint);
626 if (whichhit != -1 || position.y < terrain.getHeight(position.x, position.z)) {
629 position = colpoint * terrain.scale;
631 position.y = terrain.getHeight(position.x, position.z);
633 terrainnormal = terrain.getNormal(position.x, position.z);
634 ReflectVector(&velocity, &terrainnormal);
635 position += terrainnormal * .002;
636 bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal)));
637 if (findLengthfast(&velocity) < findLengthfast(&bounceness))
639 frictionness = abs(normaldotproduct(velocity, terrainnormal));
640 velocity -= bounceness;
641 if (1 - friction * frictionness > 0)
642 velocity *= 1 - friction * frictionness;
645 if (terrain.getOpacity(position.x, position.z) < .2)
646 velocity += bounceness * elasticity * .3;
648 velocity += bounceness * elasticity;
650 if (findLengthfast(&bounceness) > 1) {
652 if (terrain.getOpacity(position.x, position.z) > .2) {
654 whichsound = footstepsound3 + abs(Random() % 2);
656 whichsound = clank1sound + abs(Random() % 4);
658 whichsound = footstepsound + abs(Random() % 2);
660 emit_sound_at(whichsound, position,
661 findLengthfast(&bounceness)
662 * (terrain.getOpacity(position.x, position.z) > .2 ? 128. : 32.));
664 if (terrain.getOpacity(position.x, position.z) < .2) {
666 terrainlight = terrain.getLighting(position.x, position.z);
667 if (environment == snowyenvironment) {
668 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4)
669 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
670 } else if (environment == grassyenvironment) {
671 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4)
672 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x * 90 / 255, terrainlight.y * 70 / 255, terrainlight.z * 8 / 255, .5, .5);
673 } else if (environment == desertenvironment) {
674 if (distsq(&position, &viewer) < viewdistance * viewdistance / 4)
675 Sprite::MakeSprite(cloudsprite, position, velocity, terrainlight.x * 190 / 255, terrainlight.y * 170 / 255, terrainlight.z * 108 / 255, .5, .7);
680 whichhit = terrain.lineTerrain(oldtippoint, tippoint, &colpoint);
681 if (whichhit != -1 || tippoint.y < terrain.getHeight(tippoint.x, tippoint.z)) {
683 tippoint = colpoint * terrain.scale;
685 tippoint.y = terrain.getHeight(tippoint.x, tippoint.z);
687 terrainnormal = terrain.getNormal(tippoint.x, tippoint.z);
688 ReflectVector(&tipvelocity, &terrainnormal);
689 tippoint += terrainnormal * .002;
690 bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal)));
691 if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness))
693 frictionness = abs(normaldotproduct(tipvelocity, terrainnormal));
694 tipvelocity -= bounceness;
695 if (1 - friction * frictionness > 0)
696 tipvelocity *= 1 - friction * frictionness;
699 if (terrain.getOpacity(tippoint.x, tippoint.z) < .2)
700 tipvelocity += bounceness * elasticity * .3;
702 tipvelocity += bounceness * elasticity;
704 if (findLengthfast(&bounceness) > 1) {
706 if (terrain.getOpacity(tippoint.x, tippoint.z) > .2) {
708 whichsound = footstepsound3 + abs(Random() % 2);
710 whichsound = clank1sound + abs(Random() % 4);
712 whichsound = footstepsound + abs(Random() % 2);
714 emit_sound_at(whichsound, tippoint,
715 findLengthfast(&bounceness)
716 * (terrain.getOpacity(tippoint.x, tippoint.z) > .2 ? 128. : 32.));
718 if (terrain.getOpacity(tippoint.x, tippoint.z) < .2) {
720 terrainlight = terrain.getLighting(tippoint.x, tippoint.z);
721 if (environment == snowyenvironment) {
722 if (distsq(&tippoint, &viewer) < viewdistance * viewdistance / 4)
723 Sprite::MakeSprite(cloudsprite, tippoint, tipvelocity, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
724 } else if (environment == grassyenvironment) {
725 if (distsq(&tippoint, &viewer) < viewdistance * viewdistance / 4)
726 Sprite::MakeSprite(cloudsprite, tippoint, tipvelocity, terrainlight.x * 90 / 255, terrainlight.y * 70 / 255, terrainlight.z * 8 / 255, .5, .5);
727 } else if (environment == desertenvironment) {
728 if (distsq(&tippoint, &viewer) < viewdistance * viewdistance / 4)
729 Sprite::MakeSprite(cloudsprite, tippoint, tipvelocity, terrainlight.x * 190 / 255, terrainlight.y * 170 / 255, terrainlight.z * 108 / 255, .5, .7);
736 mid = position + tippoint;
738 mid += (position - mid) / 20;
740 if (mid.y < terrain.getHeight(mid.x, mid.z)) {
742 mid.y = terrain.getHeight(mid.x, mid.z);
744 terrainnormal = terrain.getNormal(mid.x, mid.z);
745 ReflectVector(&velocity, &terrainnormal);
746 //mid+=terrainnormal*.002;
747 bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal)));
748 if (findLengthfast(&velocity) < findLengthfast(&bounceness))
750 frictionness = abs(normaldotproduct(velocity, terrainnormal));
751 velocity -= bounceness;
752 if (1 - friction * frictionness > 0)
753 velocity *= 1 - friction * frictionness;
756 if (terrain.getOpacity(mid.x, mid.z) < .2)
757 velocity += bounceness * elasticity * .3;
759 velocity += bounceness * elasticity;
761 if (findLengthfast(&bounceness) > 1) {
763 if (terrain.getOpacity(mid.x, mid.z) > .2) {
765 whichsound = footstepsound3 + abs(Random() % 2);
767 whichsound = clank1sound + abs(Random() % 4);
769 whichsound = footstepsound + abs(Random() % 2);
771 emit_sound_at(whichsound, mid,
772 findLengthfast(&bounceness)
773 * (terrain.getOpacity(position.x, position.z) > .2
777 position += (mid - oldmid) * 20;
780 mid = position + tippoint;
782 mid += (tippoint - mid) / 20;
784 if (mid.y < terrain.getHeight(mid.x, mid.z)) {
786 mid.y = terrain.getHeight(mid.x, mid.z);
788 terrainnormal = terrain.getNormal(mid.x, mid.z);
789 ReflectVector(&tipvelocity, &terrainnormal);
790 //mid+=terrainnormal*.002;
791 bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal)));
792 if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness))
794 frictionness = abs(normaldotproduct(tipvelocity, terrainnormal));
795 tipvelocity -= bounceness;
796 if (1 - friction * frictionness > 0)
797 tipvelocity *= 1 - friction * frictionness;
800 if (terrain.getOpacity(mid.x, mid.z) < .2)
801 tipvelocity += bounceness * elasticity * .3;
803 tipvelocity += bounceness * elasticity;
805 if (findLengthfast(&bounceness) > 1) {
807 if (terrain.getOpacity(mid.x, mid.z) > .2) {
809 whichsound = footstepsound3 + abs(Random() % 2);
811 whichsound = clank1sound + abs(Random() % 4);
813 whichsound = footstepsound + abs(Random() % 2);
815 emit_sound_at(whichsound, mid,
816 findLengthfast(&bounceness)
817 * (terrain.getOpacity(position.x, position.z) > .2
821 tippoint += (mid - oldmid) * 20;
824 velocity.y += gravity * multiplier;
825 tipvelocity.y += gravity * multiplier;
828 XYZ temppoint1, temppoint2;
831 temppoint1 = position;
832 temppoint2 = tippoint;
833 distance = findDistance(&temppoint1, &temppoint2);
834 rotation2 = asin((temppoint1.y - temppoint2.y) / distance);
835 rotation2 *= 360 / 6.28;
838 rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2));
839 rotation1 *= 360 / 6.28;
846 if (temppoint1.x > temppoint2.x)
847 rotation1 = 360 - rotation1;
850 if (findLengthfast(&velocity) < .3 && findLengthfast(&tipvelocity) < .3 && hitsomething) {
851 freetime += multiplier;
861 multiplier = tempmult;
862 if (blooddrip && bloody) {
863 blooddripdelay -= blooddrip * multiplier / 2;
864 blooddrip -= multiplier;
869 if (blooddripdelay < 0 && bloodtoggle) {
873 bloodloc = position + (tippoint - position) * .7;
877 Sprite::MakeSprite(bloodsprite, bloodloc, bloodvel, 1, 1, 1, .03, 1);
882 flamedelay -= multiplier;
883 if (onfire && flamedelay <= 0) {
885 flamedelay -= multiplier;
888 normalrot = Person::players[owner]->velocity;
892 if (Person::players[owner]->onterrain) {
896 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);
897 Sprite::setLastSpriteSpeed(4);
898 Sprite::setLastSpriteAlivetime(.3);
902 if (!onfire && owner == -1 && type != staff) {
903 flamedelay -= multiplier;
904 if (flamedelay <= 0) {
906 flamedelay -= multiplier;
908 if (Random() % 50 == 0 && distsq(&position, &viewer) > 80) {
910 shinepoint = position + (tippoint - position) * (((float)abs(Random() % 100)) / 100);
911 Sprite::MakeSprite(weaponshinesprite, shinepoint, normalrot, 1, 1, 1, (.1 + (float)abs(Random() % 100) / 200 - .25) * 1 / 3 * fast_sqrt(findDistance(&shinepoint, &viewer)), 1);
912 Sprite::setLastSpriteSpeed(4);
913 Sprite::setLastSpriteAlivetime(.3);
919 void Weapons::DoStuff()
923 for (std::vector<Weapon>::iterator weapon = begin(); weapon != end(); ++weapon) {
924 weapon->DoStuff(i++);
930 static XYZ terrainlight;
931 static GLfloat M[16];
933 if ((frustum.SphereInFrustum(position.x, position.y, position.z, 1) &&
934 distsq(&viewer, &position) < viewdistance * viewdistance)) {
938 if (velocity.x && !physics)
943 if (Person::players[owner]->occluded < 25)
944 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)
947 (Person::players[owner]->animTarget == knifeslashstartanim ||
948 Person::players[owner]->animTarget == swordsneakattackanim ||
949 (Person::players[owner]->animCurrent == staffhitanim && Person::players[owner]->frameCurrent > 1) ||
950 (Person::players[owner]->animCurrent == staffhitreversedanim && Person::players[owner]->frameCurrent > 1) ||
951 (Person::players[owner]->animCurrent == staffspinhitanim && Person::players[owner]->frameCurrent > 1) ||
952 (Person::players[owner]->animCurrent == staffspinhitreversedanim && Person::players[owner]->frameCurrent > 1) ||
953 (Person::players[owner]->animCurrent == staffgroundsmashanim && Person::players[owner]->frameCurrent > 1) ||
954 (Person::players[owner]->animTarget == swordslashanim && Person::players[owner]->frameTarget < 7) ||
955 Person::players[owner]->animTarget == crouchstabanim ||
956 Person::players[owner]->animTarget == swordslashreversalanim ||
957 Person::players[owner]->animTarget == swordslashreversedanim ||
958 Person::players[owner]->animTarget == knifefollowanim ||
959 Person::players[owner]->animTarget == swordgroundstabanim ||
960 Person::players[owner]->animTarget == knifethrowanim) &&
961 Person::players[owner]->animTarget == lastdrawnanim &&
962 !Person::players[owner]->skeleton.free
968 if (Person::players[owner]->animTarget == swordgroundstabanim) {
969 lastdrawnrotation1 = rotation1;
970 lastdrawnrotation2 = rotation2;
971 lastdrawnrotation3 = rotation3;
972 lastdrawnbigrotation = bigrotation;
973 lastdrawnbigtilt = bigtilt;
974 lastdrawnbigtilt2 = bigtilt2;
975 lastdrawnsmallrotation = smallrotation;
976 lastdrawnsmallrotation2 = smallrotation2;
980 terrainlight = terrain.getLighting(position.x, position.z);
981 if (drawhowmany > 0) {
982 glAlphaFunc(GL_GREATER, 0.01);
984 for (int j = drawhowmany; j > 0; j--) {
985 glMatrixMode(GL_MODELVIEW);
987 glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, j / drawhowmany);
989 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));
991 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));
992 glRotatef(bigrotation * (((float)(j)) / drawhowmany) + lastdrawnbigrotation * (1 - ((float)(j)) / drawhowmany), 0, 1, 0);
993 glRotatef(bigtilt2 * (((float)(j)) / drawhowmany) + lastdrawnbigtilt2 * (1 - ((float)(j)) / drawhowmany), 1, 0, 0);
994 glRotatef(bigtilt * (((float)(j)) / drawhowmany) + lastdrawnbigtilt * (1 - ((float)(j)) / drawhowmany), 0, 0, 1);
995 glRotatef(-rotation1 * (((float)(j)) / drawhowmany) - lastdrawnrotation1 * (1 - ((float)(j)) / drawhowmany) + 90, 0, 1, 0);
996 glRotatef(-rotation2 * (((float)(j)) / drawhowmany) - lastdrawnrotation2 * (1 - ((float)(j)) / drawhowmany) + 90, 0, 0, 1);
997 glRotatef(-rotation3 * (((float)(j)) / drawhowmany) - lastdrawnrotation3 * (1 - ((float)(j)) / drawhowmany), 0, 1, 0);
998 glRotatef(smallrotation * (((float)(j)) / drawhowmany) + lastdrawnsmallrotation * (1 - ((float)(j)) / drawhowmany), 1, 0, 0);
999 glRotatef(smallrotation2 * (((float)(j)) / drawhowmany) + lastdrawnsmallrotation2 * (1 - ((float)(j)) / drawhowmany), 0, 1, 0);
1002 if (Person::players[owner]->animTarget == staffhitanim || Person::players[owner]->animCurrent == staffhitanim || Person::players[owner]->animTarget == staffhitreversedanim || Person::players[owner]->animCurrent == staffhitreversedanim) {
1003 glTranslatef(0, 0, -.3);
1005 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) {
1006 glTranslatef(0, 0, -.1);
1010 glEnable(GL_LIGHTING);
1013 if (!bloody || !bloodtoggle)
1014 throwingknifemodel.drawdifftex(knifetextureptr);
1017 throwingknifemodel.drawdifftex(lightbloodknifetextureptr);
1019 throwingknifemodel.drawdifftex(bloodknifetextureptr);
1023 if (!bloody || !bloodtoggle)
1024 swordmodel.drawdifftex(swordtextureptr);
1027 swordmodel.drawdifftex(lightbloodswordtextureptr);
1029 swordmodel.drawdifftex(bloodswordtextureptr);
1033 staffmodel.drawdifftex(stafftextureptr);
1040 lastdrawnposition = position;
1041 lastdrawntippoint = tippoint;
1042 lastdrawnrotation1 = rotation1;
1043 lastdrawnrotation2 = rotation2;
1044 lastdrawnrotation3 = rotation3;
1045 lastdrawnbigrotation = bigrotation;
1046 lastdrawnbigtilt = bigtilt;
1047 lastdrawnbigtilt2 = bigtilt2;
1048 lastdrawnsmallrotation = smallrotation;
1049 lastdrawnsmallrotation2 = smallrotation2;
1051 lastdrawnanim = Person::players[owner]->animCurrent;
1054 glMatrixMode(GL_MODELVIEW);
1057 glTranslatef(position.x, position.y - .02, position.z);
1058 glRotatef(bigrotation, 0, 1, 0);
1059 glRotatef(bigtilt2, 1, 0, 0);
1060 glRotatef(bigtilt, 0, 0, 1);
1061 glRotatef(-rotation1 + 90, 0, 1, 0);
1062 glRotatef(-rotation2 + 90, 0, 0, 1);
1063 glRotatef(-rotation3, 0, 1, 0);
1064 glRotatef(smallrotation, 1, 0, 0);
1065 glRotatef(smallrotation2, 0, 1, 0);
1066 glTranslatef(0, 0, length);
1067 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1076 void Weapon::drop(XYZ v, XYZ tv, bool sethitsomething)
1082 if (sethitsomething) {
1090 void Weapon::thrown(XYZ v, bool sethitsomething)
1092 drop(v, v, sethitsomething);
1099 glAlphaFunc(GL_GREATER, 0.9);
1100 glEnable(GL_TEXTURE_2D);
1102 glEnable(GL_CULL_FACE);
1103 glCullFace(GL_FRONT);
1106 for (std::vector<Weapon>::iterator weapon = begin(); weapon != end(); ++weapon) {
1118 Weapon::stafftextureptr.destroy();
1119 Weapon::knifetextureptr.destroy();
1120 Weapon::lightbloodknifetextureptr.destroy();
1121 Weapon::bloodknifetextureptr.destroy();
1122 Weapon::swordtextureptr.destroy();
1123 Weapon::lightbloodswordtextureptr.destroy();
1124 Weapon::bloodswordtextureptr.destroy();