]> git.jsancho.org Git - lugaru.git/blob - Source/Person.cpp
Refactored weapon taking in Person::takeWeapon
[lugaru.git] / Source / Person.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /**> HEADER FILES <**/
22 #include "Person.h"
23 #include "openal_wrapper.h"
24 #include "Animation.h"
25 #include "Sounds.h"
26 #include "Awards.h"
27 #include "Game.h"
28
29 extern float multiplier;
30 extern Terrain terrain;
31 extern float gravity;
32 extern int environment;
33 extern int detail;
34 extern FRUSTUM frustum;
35 extern XYZ viewer;
36 extern float realmultiplier;
37 extern int slomo;
38 extern float slomodelay;
39 extern bool cellophane;
40 extern float texdetail;
41 extern float realtexdetail;
42 extern GLubyte bloodText[512 * 512 * 3];
43 extern GLubyte wolfbloodText[512 * 512 * 3];
44 extern int bloodtoggle;
45 extern Objects objects;
46 extern bool autoslomo;
47 extern float camerashake;
48 extern float woozy;
49 extern float viewdistance;
50 extern float blackout;
51 extern int difficulty;
52 extern bool decals;
53 extern float fadestart;
54 extern bool freeze;
55 extern bool winfreeze;
56 extern float flashamount, flashr, flashg, flashb;
57 extern int flashdelay;
58 extern bool showpoints;
59 extern bool immediate;
60 extern int test;
61 extern bool tilt2weird;
62 extern bool tiltweird;
63 extern bool midweird;
64 extern bool proportionweird;
65 extern bool vertexweird[6];
66 extern XYZ envsound[30];
67 extern float envsoundvol[30];
68 extern float envsoundlife[30];
69 extern int numenvsounds;
70 extern int tutoriallevel;
71 extern float smoketex;
72 extern int tutorialstage;
73 extern bool reversaltrain;
74 extern bool canattack;
75 extern bool cananger;
76 extern float damagedealt;
77 extern int hostile;
78 extern float hostiletime;
79
80 extern int indialogue;
81
82 extern bool gamestarted;
83
84 std::vector<std::shared_ptr<Person>> Person::players(1, std::shared_ptr<Person>(new Person()));
85
86 /* EFFECT
87  *
88  * USES:
89  * GameTick/doPlayerCollisions
90  */
91 void Person::CheckKick()
92 {
93     if (!(hasvictim
94             && (animTarget == rabbitkickanim
95                 && victim
96                 && victim != this->shared_from_this()
97                 && frameCurrent >= 2
98                 && animCurrent == rabbitkickanim)
99             && distsq(&coords, &victim->coords) < 1.2
100             && !victim->skeleton.free))
101         return;
102
103     if (animation[victim->animTarget].height != lowheight) {
104         float damagemult = (creature == wolftype ? 2.5 : 1.) * power * power;
105         XYZ relative = velocity;
106         relative.y = 0;
107         Normalise(&relative);
108
109         victim->spurt = 1;
110         DoBlood(.2, 250);
111         if (tutoriallevel != 1)
112             emit_sound_at(heavyimpactsound, victim->coords);
113         victim->RagDoll(0);
114         for (int i = 0; i < victim->skeleton.num_joints; i++) {
115             victim->skeleton.joints[i].velocity += relative * 120 * damagemult;
116         }
117         victim->Puff(neck);
118         victim->DoDamage(100 * damagemult / victim->protectionhigh);
119         if (id == 0)
120             camerashake += .4;
121
122         target = 0;
123         frameCurrent = 3;
124         animTarget = backflipanim;
125         frameTarget = 4;
126         velocity = facing * -10;
127         velocity.y = 5;
128         skeleton.free = 0;
129         if (id == 0)
130             resume_stream(whooshsound);
131
132         award_bonus(id, cannon);
133     } else if (victim->isCrouch()) {
134         animTarget = rabbitkickreversedanim;
135         animCurrent = rabbitkickreversedanim;
136         victim->animCurrent = rabbitkickreversalanim;
137         victim->animTarget = rabbitkickreversalanim;
138         targettilt2 = 0;
139         frameCurrent = 0;
140         frameTarget = 1;
141         target = 0;
142         velocity = 0;
143         victim->oldcoords = victim->coords;
144         coords = victim->coords;
145         victim->targetyaw = targetyaw;
146         victim->victim = this->shared_from_this();
147     }
148 }
149
150 /* EFFECT
151  *
152  * USES:
153  * GameTick/doPlayerCollisions - spread fire between players
154  * GameTick/doDebugKeys - press f to ignite
155  * Person::DoStuff - spread fire from lit campfires and bushes
156  */
157 void Person::CatchFire()
158 {
159     XYZ flatfacing, flatvelocity;
160     int howmany;
161     for (int i = 0; i < 10; i++) {
162         howmany = abs(Random() % (skeleton.num_joints));
163         if (!skeleton.free)
164             flatvelocity = velocity;
165         if (skeleton.free)
166             flatvelocity = skeleton.joints[howmany].velocity;
167         if (!skeleton.free)
168             flatfacing = DoRotation(DoRotation(DoRotation(skeleton.joints[howmany].position, 0, 0, tilt), tilt2, 0, 0), 0, yaw, 0) * scale + coords;
169         if (skeleton.free)
170             flatfacing = skeleton.joints[howmany].position * scale + coords;
171         Sprite::MakeSprite(flamesprite, flatfacing, flatvelocity, 1, 1, 1, 2, 1);
172     }
173
174     onfiredelay = 0.5;
175
176     emit_sound_at(firestartsound, coords);
177
178     emit_stream_at(stream_firesound, coords);
179
180     flamedelay = 0;
181
182     onfire = 1;
183 }
184
185 /* FUNCTION
186  * idle animation for this creature (depending on status)
187  */
188 int Person::getIdle()
189 {
190     if (indialogue != -1 && howactive == typeactive && creature == rabbittype)
191         return talkidleanim;
192     if (hasvictim && (victim != this->shared_from_this())/*||(id==0&&attackkeydown)*/)
193         if (/*(id==0&&attackkeydown)||*/(!victim->dead && victim->aitype != passivetype &&
194             victim->aitype != searchtype && aitype != passivetype && aitype != searchtype &&
195             victim->id < Person::players.size())) {
196             if ((aitype == playercontrolled && stunned <= 0 && weaponactive == -1) || pause) {
197                 if (creature == rabbittype)
198                     return fightidleanim;
199                 if (creature == wolftype)
200                     return wolfidle;
201             }
202             if (aitype == playercontrolled && stunned <= 0 && weaponactive != -1) {
203                 if (weapons[weaponids[weaponactive]].getType() == knife)
204                     return knifefightidleanim;
205                 if (weapons[weaponids[weaponactive]].getType() == sword && victim->weaponactive != -1)
206                     return swordfightidlebothanim;
207                 if (weapons[weaponids[weaponactive]].getType() == sword)
208                     return swordfightidleanim;
209                 if (weapons[weaponids[weaponactive]].getType() == staff)
210                     return swordfightidleanim;
211             }
212             if (aitype != playercontrolled && stunned <= 0 && creature != wolftype && !pause)
213                 return fightsidestep;
214         }
215     if ((damage > permanentdamage || damage > damagetolerance * .8 || deathbleeding > 0) && creature != wolftype)
216         return hurtidleanim;
217     if (howactive == typesitting) return sitanim;
218     if (howactive == typesittingwall) return sitwallanim;
219     if (howactive == typesleeping) return sleepanim;
220     if (howactive == typedead1) return dead1anim;
221     if (howactive == typedead2) return dead2anim;
222     if (howactive == typedead3) return dead3anim;
223     if (howactive == typedead4) return dead4anim;
224     if (creature == rabbittype) return bounceidleanim;
225     if (creature == wolftype) return wolfidle;
226     return 0;
227 }
228
229 /* FUNCTION
230  * crouch animation for this creature
231  */
232 int Person::getCrouch()
233 {
234     if (creature == rabbittype)
235         return crouchanim;
236     if (creature == wolftype)
237         return wolfcrouchanim;
238     return 0;
239 }
240
241 /* FUNCTION
242  * running animation for this creature (can be upright or all fours)
243  */
244 int Person::getRun()
245 {
246     if (creature == rabbittype && (!superruntoggle || weaponactive != -1))
247         return runanim;
248     if (creature == wolftype && (!superruntoggle))
249         return wolfrunanim;
250
251     if (creature == rabbittype && (superruntoggle && weaponactive == -1))
252         return rabbitrunninganim;
253     if (creature == wolftype && (superruntoggle))
254         return wolfrunninganim;
255     return 0;
256 }
257
258 /* FUNCTION
259  */
260 int Person::getStop()
261 {
262     if (creature == rabbittype)
263         return stopanim;
264     if (creature == wolftype)
265         return wolfstopanim;
266     return 0;
267 }
268
269 /* FUNCTION
270  */
271 int Person::getLanding()
272 {
273     if (creature == rabbittype)
274         return landanim;
275     if (creature == wolftype)
276         return wolflandanim;
277     return 0;
278 }
279
280 /* FUNCTION
281  */
282 int Person::getLandhard()
283 {
284     if (creature == rabbittype)
285         return landhardanim;
286     if (creature == wolftype)
287         return wolflandhardanim;
288     return 0;
289 }
290
291 /* EFFECT
292  *
293  * USES:
294  * Person::DoAnimations
295  */
296 static void
297 SolidHitBonus(int playerid)
298 {
299     if (bonustime < 1.5 && bonus >= solidhit && bonus <= megacombo)
300         award_bonus(playerid, bonus == megacombo ? bonus : bonus + 1);
301     else
302         award_bonus(playerid, solidhit);
303 }
304
305 /* EFFECT
306  * spawns blood effects
307  */
308 void Person::DoBlood(float howmuch, int which)
309 {
310     // FIXME: should abstract out inputs
311     static int bleedxint, bleedyint;
312     static XYZ bloodvel;
313     if (bloodtoggle && tutoriallevel != 1) {
314         if (bleeding <= 0 && spurt) {
315             spurt = 0;
316             for (int i = 0; i < 3; i++) {
317                 // emit blood particles
318                 bloodvel = 0;
319                 if (!skeleton.free) {
320                     bloodvel.z = 10;
321                     bloodvel = DoRotation(bloodvel, ((float)(Random() % 100)) / 4, yaw + ((float)(Random() % 100)) / 4, 0) * scale;
322                 }
323                 if (skeleton.free) {
324                     bloodvel -= DoRotation(skeleton.forward * 10 * scale, ((float)(Random() % 100)) / 4, ((float)(Random() % 100)) / 4, 0);
325                 }
326                 if (skeleton.free)
327                     bloodvel += DoRotation(jointVel(head), ((float)(Random() % 100)) / 4, yaw + ((float)(Random() % 100)) / 4, 0) * scale;
328                 if (!skeleton.free)
329                     bloodvel += DoRotation(velocity, ((float)(Random() % 100)) / 4, ((float)(Random() % 100)) / 4, 0) * scale;
330                 if (skeleton.free) {
331                     Sprite::MakeSprite(bloodsprite, jointPos(head) * scale + coords, bloodvel, 1, 1, 1, .05, 1);
332                     Sprite::MakeSprite(bloodflamesprite, jointPos(head) * scale + coords, bloodvel, 1, 1, 1, .3, 1);
333                 }
334                 if (!skeleton.free) {
335                     Sprite::MakeSprite(bloodsprite, DoRotation((jointPos(head) + jointPos(neck)) / 2, 0, yaw, 0)*scale + coords, bloodvel, 1, 1, 1, .05, 1);
336                     Sprite::MakeSprite(bloodflamesprite, DoRotation((jointPos(head) + jointPos(neck)) / 2, 0, yaw, 0)*scale + coords, bloodvel, 1, 1, 1, .3, 1);
337                 }
338             }
339             if (Random() % 2 == 0) // 50% chance
340                 for (int i = 0; i < 3; i++) {
341                     if (Random() % 2 != 0) {
342                         // emit teeth particles
343                         bloodvel = 0;
344                         if (skeleton.free) {
345                             bloodvel -= DoRotation(skeleton.forward * 10 * scale, ((float)(Random() % 100)) / 4, ((float)(Random() % 100)) / 4, 0);
346                             bloodvel += DoRotation(jointVel(head), ((float)(Random() % 100)) / 4, yaw + ((float)(Random() % 100)) / 4, 0) * scale;
347                         } else {
348                             bloodvel.z = 10;
349                             bloodvel = DoRotation(bloodvel, ((float)(Random() % 100)) / 4, yaw + ((float)(Random() % 100)) / 4, 0) * scale;
350                             bloodvel += DoRotation(velocity, ((float)(Random() % 100)) / 4, ((float)(Random() % 100)) / 4, 0) * scale;
351                         }
352                         bloodvel *= .2;
353                         if (skeleton.free) {
354                             Sprite::MakeSprite(splintersprite, jointPos(head) * scale + coords, bloodvel, 1, 1, 1, .05, 1);
355                         } else {
356                             Sprite::MakeSprite(splintersprite, DoRotation((jointPos(head) + jointPos(neck)) / 2, 0, yaw, 0)*scale + coords, bloodvel, 1, 1, 1, .05, 1);
357                         }
358                         Sprite::setLastSpriteSpecial(3); // sets it to teeth
359                     }
360                 }
361         }
362         if (decals) {
363             // FIXME: manipulating attributes
364             bleeding = howmuch + (float)abs(Random() % 100) / 200 - .25;
365             bleedxint = 0;
366             bleedyint = 0;
367             if (creature == rabbittype)
368                 while (bloodText[bleedxint * 512 * 3 + bleedyint * 3 + 0] > which + 4 || bloodText[bleedxint * 512 * 3 + bleedyint * 3 + 0] < which - 4 || bleedxint < 10 || bleedyint < 10 || bleedxint > 500 || bleedyint > 500) {
369                     bleedxint = abs(Random() % 512);
370                     bleedyint = abs(Random() % 512);
371                 }
372             if (creature == wolftype)
373                 while (wolfbloodText[bleedxint * 512 * 3 + bleedyint * 3 + 0] > which + 4 || wolfbloodText[bleedxint * 512 * 3 + bleedyint * 3 + 0] < which - 4 || bleedxint < 10 || bleedyint < 10 || bleedxint > 500 || bleedyint > 500) {
374                     bleedxint = abs(Random() % 512);
375                     bleedyint = abs(Random() % 512);
376                 }
377             bleedy = bleedxint;
378             bleedx = bleedyint;
379             bleedy /= realtexdetail;
380             bleedx /= realtexdetail;
381             direction = abs(Random() % 2) * 2 - 1;
382         }
383
384     }
385     if (bleeding > 2)
386         bleeding = 2;
387 }
388
389 /* EFFECT
390  * spawns big blood effects and ???
391  * modifies character's skin texture
392  */
393 void Person::DoBloodBig(float howmuch, int which)
394 {
395     static int bleedxint, bleedyint, i, j;
396     static XYZ bloodvel;
397     if (howmuch && id == 0)
398         blooddimamount = 1;
399
400     if (tutoriallevel != 1 || id == 0)
401         if (aitype != playercontrolled && howmuch > 0) {
402             // play pain sounds
403             int whichsound = -1;
404
405             // FIXME: seems to be spawning sounds by manipulating attributes... MESSY!
406             if (creature == wolftype) {
407                 int i = abs(Random() % 2);
408                 if (i == 0)
409                     whichsound = snarlsound;
410                 if (i == 1)
411                     whichsound = snarl2sound;
412                 envsound[numenvsounds] = coords;
413                 envsoundvol[numenvsounds] = 16;
414                 envsoundlife[numenvsounds] = .4;
415                 numenvsounds++;
416             }
417             if (creature == rabbittype) {
418                 int i = abs(Random() % 2);
419                 if (i == 0)
420                     whichsound = rabbitpainsound;
421                 if (i == 1 && howmuch >= 2)
422                     whichsound = rabbitpain1sound;
423                 envsound[numenvsounds] = coords;
424                 envsoundvol[numenvsounds] = 16;
425                 envsoundlife[numenvsounds] = .4;
426                 numenvsounds++;
427             }
428
429             if (whichsound != -1)
430                 emit_sound_at(whichsound, coords);
431         }
432
433     if (id == 0 && howmuch > 0) {
434         // FIXME: manipulating attributes
435         flashamount = .5;
436         flashr = 1;
437         flashg = 0;
438         flashb = 0;
439         flashdelay = 0;
440     }
441
442     if (bloodtoggle && decals && tutoriallevel != 1) {
443         if (bleeding <= 0 && spurt) {
444             spurt = 0;
445             for (int i = 0; i < 3; i++) {
446                 // emit blood particles
447                 // FIXME: copypaste from above
448                 bloodvel = 0;
449                 if (!skeleton.free) {
450                     bloodvel.z = 10;
451                     bloodvel = DoRotation(bloodvel, ((float)(Random() % 100)) / 4, yaw + ((float)(Random() % 100)) / 4, 0) * scale;
452                 }
453                 if (skeleton.free) {
454                     bloodvel -= DoRotation(skeleton.forward * 10 * scale, ((float)(Random() % 100)) / 4, ((float)(Random() % 100)) / 4, 0);
455                 }
456                 if (skeleton.free)
457                     bloodvel += DoRotation(jointVel(head), ((float)(Random() % 100)) / 4, yaw + ((float)(Random() % 100)) / 4, 0) * scale;
458                 if (!skeleton.free)
459                     bloodvel += DoRotation(velocity, ((float)(Random() % 100)) / 4, ((float)(Random() % 100)) / 4, 0) * scale;
460                 if (skeleton.free) {
461                     Sprite::MakeSprite(bloodsprite, jointPos(head) * scale + coords, bloodvel, 1, 1, 1, .05, 1);
462                     Sprite::MakeSprite(bloodflamesprite, jointPos(head) * scale + coords, bloodvel, 1, 1, 1, .3, 1);
463                 }
464                 if (!skeleton.free) {
465                     Sprite::MakeSprite(bloodsprite, DoRotation((jointPos(head) + jointPos(neck)) / 2, 0, yaw, 0)*scale + coords, bloodvel, 1, 1, 1, .05, 1);
466                     Sprite::MakeSprite(bloodflamesprite, DoRotation((jointPos(head) + jointPos(neck)) / 2, 0, yaw, 0)*scale + coords, bloodvel, 1, 1, 1, .3, 1);
467                 }
468             }
469         }
470
471         // weird texture manipulation code follows.
472         // looks like this is painting blood onto the character's skin texture
473         // FIXME: surely there's a better way
474
475         int offsetx = 0, offsety = 0;
476         if (which == 225) {
477             offsety = Random() % 40;
478             offsetx = abs(Random() % 60);
479         }
480         if (which == 190 || which == 185) {
481             offsety = Random() % 40;
482             offsetx = abs(Random() % 100) - 20;
483         }
484         if (which == 175) {
485             offsety = Random() % 10;
486             offsetx = Random() % 10;
487         }
488         if (which == 170) {
489             offsety = Random() % 20;
490             offsetx = Random() % 20;
491         }
492         if (which == 220 || which == 215) {
493             offsetx = 20;
494         }
495
496
497         int startx = 512;
498         int starty = 512;
499         int endx = 0;
500         int endy = 0;
501         GLubyte color;
502         if (creature == rabbittype)
503             for (i = 0; i < 512; i++) {
504                 for (j = 0; j < 512; j++) {
505                     if (bloodText[i * 512 * 3 + j * 3 + 0] <= which + 4 && bloodText[i * 512 * 3 + j * 3 + 0] >= which - 4) {
506                         if (i < startx) startx = i;
507                         if (j < starty) starty = j;
508                         if (i > endx) endx = i;
509                         if (j > endy) endy = j;
510                     }
511                 }
512             }
513         if (creature == wolftype)
514             for (i = 0; i < 512; i++) {
515                 for (j = 0; j < 512; j++) {
516                     if (wolfbloodText[i * 512 * 3 + j * 3 + 0] <= which + 4 && wolfbloodText[i * 512 * 3 + j * 3 + 0] >= which - 4) {
517                         if (i < startx) startx = i;
518                         if (j < starty) starty = j;
519                         if (i > endx) endx = i;
520                         if (j > endy) endy = j;
521                     }
522                 }
523             }
524
525         startx += offsetx;
526         endx += offsetx;
527         starty += offsety;
528         endy += offsety;
529
530         if (startx < 0) startx = 0;
531         if (starty < 0) starty = 0;
532         if (endx > 512 - 1) endx = 512 - 1;
533         if (endy > 512 - 1) endy = 512 - 1;
534         if (endx < startx) endx = startx;
535         if (endy < starty) endy = starty;
536
537         startx /= realtexdetail;
538         starty /= realtexdetail;
539         endx /= realtexdetail;
540         endy /= realtexdetail;
541
542         int texdetailint = realtexdetail;
543         int where;
544         if (creature == rabbittype)
545             for (i = startx; i < endx; i++) {
546                 for (j = starty; j < endy; j++) {
547                     if (bloodText[(i * texdetailint - offsetx) * 512 * 3 + (j * texdetailint - offsety) * 3 + 0] <= which + 4 && bloodText[(i * texdetailint - offsetx) * 512 * 3 + (j * texdetailint - offsety) * 3 + 0] >= which - 4) {
548                         color = Random() % 85 + 170;
549                         where = i * skeleton.skinsize * 3 + j * 3;
550                         if (skeleton.skinText[where + 0] > color / 2)
551                             skeleton.skinText[where + 0] = color / 2;
552                         skeleton.skinText[where + 1] = 0;
553                         skeleton.skinText[where + 2] = 0;
554                     }
555                 }
556             }
557         if (creature == wolftype)
558             for (i = startx; i < endx; i++) {
559                 for (j = starty; j < endy; j++) {
560                     if (wolfbloodText[(i * texdetailint - offsetx) * 512 * 3 + (j * texdetailint - offsety) * 3 + 0] <= which + 4 && wolfbloodText[(i * texdetailint - offsetx) * 512 * 3 + (j * texdetailint - offsety) * 3 + 0] >= which - 4) {
561                         color = Random() % 85 + 170;
562                         where = i * skeleton.skinsize * 3 + j * 3;
563                         if (skeleton.skinText[where + 0] > color / 2)
564                             skeleton.skinText[where + 0] = color / 2;
565                         skeleton.skinText[where + 1] = 0;
566                         skeleton.skinText[where + 2] = 0;
567                     }
568                 }
569             }
570         skeleton.drawmodel.textureptr.bind();
571         DoMipmaps();
572
573         bleedxint = 0;
574         bleedyint = 0;
575         if (creature == rabbittype)
576             while (bloodText[bleedxint * 512 * 3 + bleedyint * 3 + 0] > which + 4 || bloodText[bleedxint * 512 * 3 + bleedyint * 3 + 0] < which - 4 || bleedxint < 10 || bleedyint < 10 || bleedxint > 500 || bleedyint > 500) {
577                 bleedxint = abs(Random() % 512);
578                 bleedyint = abs(Random() % 512);
579             }
580         if (creature == wolftype)
581             while (wolfbloodText[bleedxint * 512 * 3 + bleedyint * 3 + 0] > which + 4 || wolfbloodText[bleedxint * 512 * 3 + bleedyint * 3 + 0] < which - 4 || bleedxint < 10 || bleedyint < 10 || bleedxint > 500 || bleedyint > 500) {
582                 bleedxint = abs(Random() % 512);
583                 bleedyint = abs(Random() % 512);
584             }
585         bleedy = bleedxint + offsetx;
586         bleedx = bleedyint + offsety;
587         bleedy /= realtexdetail;
588         bleedx /= realtexdetail;
589         if (bleedx < 0)
590             bleedx = 0;
591         if (bleedy < 0)
592             bleedy = 0;
593         if (bleedx > skeleton.skinsize - 1)
594             bleedx = skeleton.skinsize - 1;
595         if (bleedy > skeleton.skinsize - 1)
596             bleedy = skeleton.skinsize - 1;
597         direction = abs(Random() % 2) * 2 - 1;
598
599     }
600     bleeding = howmuch + (float)abs(Random() % 100) / 200 - .25;
601     deathbleeding += bleeding;
602     bloodloss += bleeding * 3;
603
604     if (tutoriallevel != 1 && aitype != playercontrolled && bloodloss > damagetolerance * 2 / 3 && bloodloss < damagetolerance && creature == rabbittype) {
605         if (abs(Random() % 2) == 0) {
606             aitype = gethelptype;
607             lastseentime = 12;
608         } else
609             aitype = attacktypecutoff;
610         ally = 0;
611     }
612     if (bleeding > 2)
613         bleeding = 2;
614 }
615
616 /* EFFECT
617  * similar to DoBloodBig
618  */
619 bool Person::DoBloodBigWhere(float howmuch, int which, XYZ where)
620 {
621     static int i, j;
622     static XYZ bloodvel;
623     static XYZ startpoint, endpoint, colpoint, movepoint;
624     static float rotationpoint;
625     static int whichtri;
626     static XYZ p1, p2, p3, p0;
627     static XYZ N, temp;
628     XYZ bary;
629     XYZ gxx, gyy;
630     float coordsx, coordsy;
631     float total;
632
633     if (bloodtoggle && decals && tutoriallevel != 1) {
634         where -= coords;
635         if (!skeleton.free)
636             where = DoRotation(where, 0, -yaw, 0);
637         //where=scale;
638         startpoint = where;
639         startpoint.y += 100;
640         endpoint = where;
641         endpoint.y -= 100;
642         movepoint = 0;
643         rotationpoint = 0;
644         // ray testing for a tri in the character model
645         whichtri = skeleton.drawmodel.LineCheck(&startpoint, &endpoint, &colpoint, &movepoint, &rotationpoint);
646         if (whichtri != -1) {
647             // low level geometry math
648             p0 = colpoint;
649             p1 = skeleton.drawmodel.vertex[skeleton.drawmodel.Triangles[whichtri].vertex[0]];
650             p2 = skeleton.drawmodel.vertex[skeleton.drawmodel.Triangles[whichtri].vertex[1]];
651             p3 = skeleton.drawmodel.vertex[skeleton.drawmodel.Triangles[whichtri].vertex[2]];
652             /*
653             CrossProduct(p2-p1,p3-p1,&N);
654             CrossProduct(p0-p1,p3-p1,&temp);
655             s =  dotproduct(&temp,&N)/findLength(&N);
656             CrossProduct(p2-p1,p1-p0,&temp);
657             t = dotproduct(&temp,&N)/findLength(&N);
658             r = 1 - (s + t);*/
659
660             bary.x = distsq(&p0, &p1);
661             bary.y = distsq(&p0, &p2);
662             bary.z = distsq(&p0, &p3);
663
664             total = bary.x + bary.y + bary.z;
665             bary.x /= total;
666             bary.y /= total;
667             bary.z /= total;
668
669             bary.x = 1 - bary.x;
670             bary.y = 1 - bary.y;
671             bary.z = 1 - bary.z;
672
673             total = bary.x + bary.y + bary.z;
674             bary.x /= total;
675             bary.y /= total;
676             bary.z /= total;
677
678
679             gxx.x = skeleton.drawmodel.Triangles[whichtri].gx[0];
680             gxx.y = skeleton.drawmodel.Triangles[whichtri].gx[1];
681             gxx.z = skeleton.drawmodel.Triangles[whichtri].gx[2];
682             gyy.x = skeleton.drawmodel.Triangles[whichtri].gy[0];
683             gyy.y = skeleton.drawmodel.Triangles[whichtri].gy[1];
684             gyy.z = skeleton.drawmodel.Triangles[whichtri].gy[2];
685             coordsx = skeleton.drawmodel.Triangles[whichtri].gx[0] * bary.x + skeleton.drawmodel.Triangles[whichtri].gx[1] * bary.y + skeleton.drawmodel.Triangles[whichtri].gx[2] * bary.z;
686             coordsy = skeleton.drawmodel.Triangles[whichtri].gy[0] * bary.x + skeleton.drawmodel.Triangles[whichtri].gy[1] * bary.y + skeleton.drawmodel.Triangles[whichtri].gy[2] * bary.z;
687
688             //coordsx=skeleton.drawmodel.Triangles[whichtri].gx[1];
689             //coordsy=skeleton.drawmodel.Triangles[whichtri].gy[1];
690
691             if (bleeding <= 0 && spurt) {
692                 spurt = 0;
693                 for (int i = 0; i < 3; i++) {
694                     // emit blood particles
695                     // FIXME: more copypaste code
696                     bloodvel = 0;
697                     if (!skeleton.free) {
698                         bloodvel.z = 10;
699                         bloodvel = DoRotation(bloodvel, ((float)(Random() % 100)) / 4, yaw + ((float)(Random() % 100)) / 4, 0) * scale;
700                     }
701                     if (skeleton.free) {
702                         bloodvel -= DoRotation(skeleton.forward * 10 * scale, ((float)(Random() % 100)) / 4, ((float)(Random() % 100)) / 4, 0);
703                     }
704                     if (skeleton.free)
705                         bloodvel += DoRotation(jointVel(head), ((float)(Random() % 100)) / 4, yaw + ((float)(Random() % 100)) / 4, 0) * scale;
706                     if (!skeleton.free)
707                         bloodvel += DoRotation(velocity, ((float)(Random() % 100)) / 4, ((float)(Random() % 100)) / 4, 0) * scale;
708                     if (skeleton.free) {
709                         Sprite::MakeSprite(bloodsprite, jointPos(head) * scale + coords, bloodvel, 1, 1, 1, .05, 1);
710                         Sprite::MakeSprite(bloodflamesprite, jointPos(head) * scale + coords, bloodvel, 1, 1, 1, .3, 1);
711                     }
712                     if (!skeleton.free) {
713                         Sprite::MakeSprite(bloodsprite, DoRotation((jointPos(head) + jointPos(neck)) / 2, 0, yaw, 0)*scale + coords, bloodvel, 1, 1, 1, .05, 1);
714                         Sprite::MakeSprite(bloodflamesprite, DoRotation((jointPos(head) + jointPos(neck)) / 2, 0, yaw, 0)*scale + coords, bloodvel, 1, 1, 1, .3, 1);
715                     }
716                 }
717             }
718
719             // texture manipulation follows
720
721             int offsetx = 0, offsety = 0;
722             offsetx = (1 + coordsy) * 512 - 291;
723             offsety = coordsx * 512 - 437;
724
725             int startx = 512;
726             int starty = 512;
727             int endx = 0;
728             int endy = 0;
729             GLubyte color;
730             if (creature == rabbittype)
731                 for (i = 0; i < 512; i++) {
732                     for (j = 0; j < 512; j++) {
733                         if (bloodText[i * 512 * 3 + j * 3 + 0] <= which + 4 && bloodText[i * 512 * 3 + j * 3 + 0] >= which - 4) {
734                             if (i < startx) startx = i;
735                             if (j < starty) starty = j;
736                             if (i > endx) endx = i;
737                             if (j > endy) endy = j;
738                         }
739                     }
740                 }
741             if (creature == wolftype)
742                 for (i = 0; i < 512; i++) {
743                     for (j = 0; j < 512; j++) {
744                         if (wolfbloodText[i * 512 * 3 + j * 3 + 0] <= which + 4 && wolfbloodText[i * 512 * 3 + j * 3 + 0] >= which - 4) {
745                             if (i < startx) startx = i;
746                             if (j < starty) starty = j;
747                             if (i > endx) endx = i;
748                             if (j > endy) endy = j;
749                         }
750                     }
751                 }
752             startx += offsetx;
753             endx += offsetx;
754             starty += offsety;
755             endy += offsety;
756
757             if (startx < 0) startx = 0;
758             if (starty < 0) starty = 0;
759             if (endx > 512 - 1) endx = 512 - 1;
760             if (endy > 512 - 1) endy = 512 - 1;
761             if (endx < startx) endx = startx;
762             if (endy < starty) endy = starty;
763
764             startx /= realtexdetail;
765             starty /= realtexdetail;
766             endx /= realtexdetail;
767             endy /= realtexdetail;
768
769             int texdetailint = realtexdetail;
770             int where;
771             if (creature == rabbittype)
772                 for (i = startx; i < endx; i++) {
773                     for (j = starty; j < endy; j++) {
774                         if (bloodText[(i * texdetailint - offsetx) * 512 * 3 + (j * texdetailint - offsety) * 3 + 0] <= which + 4 && bloodText[(i * texdetailint - offsetx) * 512 * 3 + (j * texdetailint - offsety) * 3 + 0] >= which - 4) {
775                             color = Random() % 85 + 170;
776                             where = i * skeleton.skinsize * 3 + j * 3;
777                             if (skeleton.skinText[where + 0] > color / 2)
778                                 skeleton.skinText[where + 0] = color / 2;
779                             skeleton.skinText[where + 1] = 0;
780                             skeleton.skinText[where + 2] = 0;
781                         } else if (bloodText[(i * texdetailint - offsetx) * 512 * 3 + (j * texdetailint - offsety) * 3 + 0] <= 160 + 4 && bloodText[(i * texdetailint - offsetx) * 512 * 3 + (j * texdetailint - offsety) * 3 + 0] >= 160 - 4) {
782                             color = Random() % 85 + 170;
783                             where = i * skeleton.skinsize * 3 + j * 3;
784                             if (skeleton.skinText[where + 0] > color / 2)
785                                 skeleton.skinText[where + 0] = color / 2;
786                             skeleton.skinText[where + 1] = 0;
787                             skeleton.skinText[where + 2] = 0;
788                         }
789                     }
790                 }
791             if (creature == wolftype)
792                 for (i = startx; i < endx; i++) {
793                     for (j = starty; j < endy; j++) {
794                         if (wolfbloodText[(i * texdetailint - offsetx) * 512 * 3 + (j * texdetailint - offsety) * 3 + 0] <= which + 4 && wolfbloodText[(i * texdetailint - offsetx) * 512 * 3 + (j * texdetailint - offsety) * 3 + 0] >= which - 4) {
795                             color = Random() % 85 + 170;
796                             where = i * skeleton.skinsize * 3 + j * 3;
797                             if (skeleton.skinText[where + 0] > color / 2)
798                                 skeleton.skinText[where + 0] = color / 2;
799                             skeleton.skinText[where + 1] = 0;
800                             skeleton.skinText[where + 2] = 0;
801                         } else if (wolfbloodText[(i * texdetailint - offsetx) * 512 * 3 + (j * texdetailint - offsety) * 3 + 0] <= 160 + 4 && wolfbloodText[(i * texdetailint - offsetx) * 512 * 3 + (j * texdetailint - offsety) * 3 + 0] >= 160 - 4) {
802                             color = Random() % 85 + 170;
803                             where = i * skeleton.skinsize * 3 + j * 3;
804                             if (skeleton.skinText[where + 0] > color / 2)
805                                 skeleton.skinText[where + 0] = color / 2;
806                             skeleton.skinText[where + 1] = 0;
807                             skeleton.skinText[where + 2] = 0;
808                         }
809                     }
810                 }
811             skeleton.drawmodel.textureptr.bind();
812             DoMipmaps();
813
814             bleedy = (1 + coordsy) * 512;
815             bleedx = coordsx * 512;
816             bleedy /= realtexdetail;
817             bleedx /= realtexdetail;
818             if (bleedx < 0)
819                 bleedx = 0;
820             if (bleedy < 0)
821                 bleedy = 0;
822             if (bleedx > skeleton.skinsize - 1)
823                 bleedx = skeleton.skinsize - 1;
824             if (bleedy > skeleton.skinsize - 1)
825                 bleedy = skeleton.skinsize - 1;
826             direction = abs(Random() % 2) * 2 - 1;
827         }
828         if (whichtri == -1)
829             return 0;
830     }
831     bleeding = howmuch + (float)abs(Random() % 100) / 200 - .25;
832     deathbleeding += bleeding;
833     bloodloss += bleeding * 3;
834
835     if (tutoriallevel != 1 && aitype != playercontrolled && bloodloss > damagetolerance * 2 / 3 && bloodloss < damagetolerance && creature == rabbittype) {
836         if (abs(Random() % 2) == 0) {
837             aitype = gethelptype;
838             lastseentime = 12;
839         } else
840             aitype = attacktypecutoff;
841         ally = 0;
842     }
843     if (bleeding > 2)
844         bleeding = 2;
845     return 1;
846 }
847
848
849
850 /* EFFECT
851  * guessing this performs a reversal
852  */
853 void Person::Reverse()
854 {
855     if (!((victim->aitype == playercontrolled
856             || hostiletime > 1
857             || staggerdelay <= 0)
858             && victim->animTarget != jumpupanim
859             && victim->animTarget != jumpdownanim
860             && (tutoriallevel != 1 || cananger)
861             && hostile))
862         return;
863
864     if (normaldotproduct (victim->facing, victim->coords - coords) > 0
865             && (victim->id != 0 || difficulty >= 2)
866             && (creature != wolftype || victim->creature == wolftype))
867         return;
868
869     if (animTarget == sweepanim) {
870         animTarget = sweepreversedanim;
871         animCurrent = sweepreversedanim;
872         victim->animCurrent = sweepreversalanim;
873         victim->animTarget = sweepreversalanim;
874     }
875     if (animTarget == spinkickanim) {
876         animTarget = spinkickreversedanim;
877         animCurrent = spinkickreversedanim;
878         victim->animCurrent = spinkickreversalanim;
879         victim->animTarget = spinkickreversalanim;
880     }
881     if (animTarget == upunchanim || animTarget == rabbittacklinganim) {
882         if (animTarget == rabbittacklinganim) {
883             frameCurrent = 6;
884             frameTarget = 7;
885             victim->frameCurrent = 6;
886             victim->frameTarget = 7;
887         }
888         animTarget = upunchreversedanim;
889         animCurrent = upunchreversedanim;
890         victim->animCurrent = upunchreversalanim;
891         victim->animTarget = upunchreversalanim;
892     }
893     if (animTarget == staffhitanim && distsq(&victim->coords, &coords) < 2 && ((victim->id == 0 && victim->crouchkeydown) || Random() % 4 == 0)) {
894         if (victim->weaponactive != -1) {
895             victim->throwtogglekeydown = 1;
896             XYZ tempVelocity = victim->velocity * .2;
897             if (tempVelocity.x == 0)
898                 tempVelocity.x = .1;
899             weapons[victim->weaponids[0]].drop(tempVelocity, tempVelocity, false);
900             victim->num_weapons--;
901             if (victim->num_weapons) {
902                 victim->weaponids[0] = victim->weaponids[victim->num_weapons];
903                 if (victim->weaponstuck == victim->num_weapons)
904                     victim->weaponstuck = 0;
905             }
906
907             victim->weaponactive = -1;
908             for (unsigned j = 0; j < Person::players.size(); j++) {
909                 Person::players[j]->wentforweapon = 0;
910             }
911         }
912
913         animTarget = staffhitreversedanim;
914         animCurrent = staffhitreversedanim;
915         victim->animCurrent = staffhitreversalanim;
916         victim->animTarget = staffhitreversalanim;
917     }
918     if (animTarget == staffspinhitanim && distsq(&victim->coords, &coords) < 2 && ((victim->id == 0 && victim->crouchkeydown) || Random() % 2 == 0)) {
919         if (victim->weaponactive != -1) {
920             victim->throwtogglekeydown = 1;
921             XYZ tempVelocity = victim->velocity * .2;
922             if (tempVelocity.x == 0)
923                 tempVelocity.x = .1;
924             weapons[victim->weaponids[0]].drop(tempVelocity, tempVelocity, false);
925             victim->num_weapons--;
926             if (victim->num_weapons) {
927                 victim->weaponids[0] = victim->weaponids[victim->num_weapons];
928                 if (victim->weaponstuck == victim->num_weapons)
929                     victim->weaponstuck = 0;
930             }
931
932             victim->weaponactive = -1;
933             for (unsigned j = 0; j < Person::players.size(); j++) {
934                 Person::players[j]->wentforweapon = 0;
935             }
936         }
937         animTarget = staffspinhitreversedanim;
938         animCurrent = staffspinhitreversedanim;
939         victim->animCurrent = staffspinhitreversalanim;
940         victim->animTarget = staffspinhitreversalanim;
941     }
942     if (animTarget == swordslashanim && distsq(&victim->coords, &coords) < 2 && ((victim->id == 0 && victim->crouchkeydown) || Random() % 4 == 0)) {
943         if (victim->weaponactive != -1) {
944             victim->throwtogglekeydown = 1;
945             XYZ tempVelocity = victim->velocity * .2;
946             if (tempVelocity.x == 0)
947                 tempVelocity.x = .1;
948             weapons[victim->weaponids[0]].drop(tempVelocity, tempVelocity, false);
949             victim->num_weapons--;
950             if (victim->num_weapons) {
951                 victim->weaponids[0] = victim->weaponids[victim->num_weapons];
952                 if (victim->weaponstuck == victim->num_weapons)
953                     victim->weaponstuck = 0;
954             }
955
956             victim->weaponactive = -1;
957             for (unsigned j = 0; j < Person::players.size(); j++) {
958                 Person::players[j]->wentforweapon = 0;
959             }
960         }
961         animTarget = swordslashreversedanim;
962         animCurrent = swordslashreversedanim;
963         victim->animCurrent = swordslashreversalanim;
964         victim->animTarget = swordslashreversalanim;
965     }
966     if (animTarget == knifeslashstartanim && distsq(&victim->coords, &coords) < 2 && (victim->id == 0 || Random() % 4 == 0)) {
967         if (victim->weaponactive != -1) {
968             victim->throwtogglekeydown = 1;
969             XYZ tempVelocity = victim->velocity * .2;
970             if (tempVelocity.x == 0)
971                 tempVelocity.x = .1;
972             weapons[victim->weaponids[0]].drop(tempVelocity, tempVelocity, false);
973             victim->num_weapons--;
974             if (victim->num_weapons) {
975                 victim->weaponids[0] = victim->weaponids[victim->num_weapons];
976                 if (victim->weaponstuck == victim->num_weapons)
977                     victim->weaponstuck = 0;
978             }
979
980             victim->weaponactive = -1;
981             for (unsigned j = 0; j < Person::players.size(); j++) {
982                 Person::players[j]->wentforweapon = 0;
983             }
984         }
985         animTarget = knifeslashreversedanim;
986         animCurrent = knifeslashreversedanim;
987         victim->animCurrent = knifeslashreversalanim;
988         victim->animTarget = knifeslashreversalanim;
989     }
990     if (animTarget != knifeslashstartanim && animTarget != staffhitanim && animTarget != staffspinhitanim && animTarget != winduppunchanim && animTarget != wolfslapanim && animTarget != swordslashanim) {
991         victim->targettilt2 = targettilt2;
992         victim->frameCurrent = frameCurrent;
993         victim->frameTarget = frameTarget;
994         victim->target = target;
995         victim->velocity = 0;
996         victim->oldcoords = victim->coords;
997         victim->coords = coords;
998         victim->targetyaw = targetyaw;
999         victim->yaw = targetyaw;
1000         victim->victim = this->shared_from_this();
1001     }
1002     if (animTarget == winduppunchanim) {
1003         animTarget = winduppunchblockedanim;
1004         victim->animTarget = blockhighleftanim;
1005         victim->frameTarget = 1;
1006         victim->target = .5;
1007         victim->victim = this->shared_from_this();
1008         victim->targetyaw = targetyaw + 180;
1009     }
1010     if (animTarget == wolfslapanim) {
1011         animTarget = winduppunchblockedanim;
1012         victim->animTarget = blockhighleftanim;
1013         victim->frameTarget = 1;
1014         victim->target = .5;
1015         victim->victim = this->shared_from_this();
1016         victim->targetyaw = targetyaw + 180;
1017     }
1018     if ((animTarget == swordslashanim || animTarget == staffhitanim || animTarget == staffspinhitanim) && victim->weaponactive != -1) {
1019         animTarget = swordslashparriedanim;
1020         parriedrecently = .4;
1021         victim->parriedrecently = 0;
1022         victim->animTarget = swordslashparryanim;
1023         victim->frameTarget = 1;
1024         victim->target = .5;
1025         victim->victim = this->shared_from_this();
1026         victim->targetyaw = targetyaw + 180;
1027
1028         if (abs(Random() % 20) == 0 || weapons[victim->weaponids[victim->weaponactive]].getType() == knife) {
1029             if (victim->weaponactive != -1) {
1030                 if (weapons[victim->weaponids[0]].getType() == staff || weapons[weaponids[0]].getType() == staff) {
1031                     if (weapons[victim->weaponids[0]].getType() == staff)
1032                         weapons[victim->weaponids[0]].damage += .2 + float(abs(Random() % 100) - 50) / 250;
1033                     if (weapons[weaponids[0]].getType() == staff)
1034                         weapons[weaponids[0]].damage += .2 + float(abs(Random() % 100) - 50) / 250;
1035                     emit_sound_at(swordstaffsound, victim->coords);
1036                 } else {
1037                     emit_sound_at(metalhitsound, victim->coords);
1038                 }
1039             }
1040             XYZ aim;
1041             victim->Puff(righthand);
1042             victim->target = 0;
1043             victim->frameTarget = 0;
1044             victim->animTarget = staggerbackhighanim;
1045             victim->targetyaw = targetyaw + 180;
1046             victim->target = 0;
1047             aim = DoRotation(facing, 0, 90, 0) * 21;
1048             aim.y += 7;
1049             weapons[victim->weaponids[0]].drop(aim * -.2, aim);
1050             victim->num_weapons--;
1051             if (victim->num_weapons) {
1052                 victim->weaponids[0] = victim->weaponids[num_weapons];
1053                 if (victim->weaponstuck == victim->num_weapons)
1054                     victim->weaponstuck = 0;
1055             }
1056             victim->weaponactive = -1;
1057             for (unsigned i = 0; i < Person::players.size(); i++) {
1058                 Person::players[i]->wentforweapon = 0;
1059             }
1060         }
1061
1062         if (abs(Random() % 20) == 0) {
1063             if (weaponactive != -1) {
1064                 if (weapons[victim->weaponids[0]].getType() == staff || weapons[weaponids[0]].getType() == staff) {
1065                     if (weapons[victim->weaponids[0]].getType() == staff)
1066                         weapons[victim->weaponids[0]].damage += .2 + float(abs(Random() % 100) - 50) / 250;
1067                     if (weapons[weaponids[0]].getType() == staff)
1068                         weapons[weaponids[0]].damage += .2 + float(abs(Random() % 100) - 50) / 250;
1069
1070                     emit_sound_at(swordstaffsound, coords);
1071                 } else {
1072                     emit_sound_at(metalhitsound, coords);
1073                 }
1074             }
1075
1076             XYZ aim;
1077             Puff(righthand);
1078             target = 0;
1079             frameTarget = 0;
1080             animTarget = staggerbackhighanim;
1081             targetyaw = targetyaw + 180;
1082             target = 0;
1083             aim = DoRotation(facing, 0, 90, 0) * 21;
1084             aim.y += 7;
1085             weapons[victim->weaponids[0]].drop(aim * -.2, aim);
1086             num_weapons--;
1087             if (num_weapons) {
1088                 weaponids[0] = weaponids[num_weapons];
1089                 if (weaponstuck == num_weapons)
1090                     weaponstuck = 0;
1091             }
1092             weaponactive = -1;
1093             for (unsigned i = 0; i < Person::players.size(); i++) {
1094                 Person::players[i]->wentforweapon = 0;
1095             }
1096
1097
1098         }
1099     }
1100     if (hasvictim)
1101         if (animTarget == knifeslashstartanim || animTarget == swordslashanim || animTarget == staffhitanim || animTarget == staffspinhitanim) {
1102             if ((animTarget != staffhitanim && animTarget != staffspinhitanim) || distsq(&coords, &victim->coords) > .2) {
1103                 victim->animTarget = dodgebackanim;
1104                 victim->frameTarget = 0;
1105                 victim->target = 0;
1106
1107                 XYZ rotatetarget;
1108                 rotatetarget = coords - victim->coords;
1109                 Normalise(&rotatetarget);
1110                 victim->targetyaw = -asin(0 - rotatetarget.x);
1111                 victim->targetyaw *= 360 / 6.28;
1112                 if (rotatetarget.z < 0)
1113                     victim->targetyaw = 180 - victim->targetyaw;
1114
1115                 victim->targettilt2 = -asin(rotatetarget.y) * 360 / 6.28; //*-70;
1116
1117                 victim->lastattack3 = victim->lastattack2;
1118                 victim->lastattack2 = victim->lastattack;
1119                 victim->lastattack = victim->animTarget;
1120             } else {
1121                 victim->animTarget = sweepanim;
1122                 victim->frameTarget = 0;
1123                 victim->target = 0;
1124
1125                 XYZ rotatetarget;
1126                 rotatetarget = coords - victim->coords;
1127                 Normalise(&rotatetarget);
1128                 victim->targetyaw = -asin(0 - rotatetarget.x);
1129                 victim->targetyaw *= 360 / 6.28;
1130                 if (rotatetarget.z < 0)
1131                     victim->targetyaw = 180 - victim->targetyaw;
1132
1133                 victim->targettilt2 = -asin(rotatetarget.y) * 360 / 6.28; //*-70;
1134
1135                 victim->lastattack3 = victim->lastattack2;
1136                 victim->lastattack2 = victim->lastattack;
1137                 victim->lastattack = victim->animTarget;
1138             }
1139         }
1140
1141     velocity = 0;
1142     victim->velocity = 0;
1143
1144     if (aitype != playercontrolled)
1145         feint = 0;
1146     if (aitype != playercontrolled && Random() % 3 == 0 && escapednum < 2 && difficulty == 2)
1147         feint = 1;
1148     if (aitype != playercontrolled && Random() % 5 == 0 && escapednum < 2 && difficulty == 1)
1149         feint = 1;
1150     if (aitype != playercontrolled && Random() % 10 == 0 && escapednum < 2 && difficulty == 0)
1151         feint = 1;
1152
1153     if (victim->id == 0 && animation[victim->animTarget].attack == reversal)
1154         numreversals++;
1155 }
1156
1157 /* EFFECT
1158  * get hurt
1159  */
1160 void Person::DoDamage(float howmuch)
1161 {
1162     // subtract health (temporary?)
1163     if (tutoriallevel != 1)
1164         damage += howmuch / power;
1165     // stats?
1166     if (id != 0)
1167         damagedealt += howmuch / power;
1168     if (id == 0)
1169         damagetaken += howmuch / power;
1170
1171     // reset bonuses
1172     if (id == 0 && (bonus == solidhit || bonus == twoxcombo || bonus == threexcombo || bonus == fourxcombo || bonus == megacombo))
1173         bonus = 0;
1174     // subtract health
1175     if (tutoriallevel != 1)
1176         permanentdamage += howmuch / 2 / power;
1177     if (tutoriallevel != 1)
1178         superpermanentdamage += howmuch / 4 / power;
1179     // visual effects
1180     if (permanentdamage > damagetolerance / 2 && permanentdamage - howmuch < damagetolerance / 2 && Random() % 2)
1181         DoBlood(1, 255);
1182     if ((permanentdamage > damagetolerance * .8 && Random() % 2 && !deathbleeding) || spurt)
1183         DoBlood(1, 255);
1184     spurt = 0;
1185     if (id == 0)
1186         camerashake += howmuch / 100;
1187     if (id == 0 && ((howmuch > 50 && damage > damagetolerance / 2)))
1188         blackout = damage / damagetolerance;
1189     if (blackout > 1)
1190         blackout = 1;
1191
1192     // cancel attack?
1193     if (aitype == passivetype && damage < damagetolerance && ((tutoriallevel != 1 || cananger) && hostile))
1194         aitype = attacktypecutoff;
1195     if (tutoriallevel != 1 && aitype != playercontrolled && damage < damagetolerance && damage > damagetolerance * 2 / 3 && creature == rabbittype) {
1196         if (abs(Random() % 2) == 0) {
1197             aitype = gethelptype;
1198             lastseentime = 12;
1199         } else
1200             aitype = attacktypecutoff;
1201         ally = 0;
1202     }
1203
1204     if (howmuch > damagetolerance * 50 && skeleton.free != 2) {
1205         XYZ flatvelocity2;
1206         XYZ flatfacing2;
1207         for (int i = 0; i < skeleton.num_joints; i++) {
1208             if (!skeleton.free)
1209                 flatvelocity2 = velocity;
1210             if (skeleton.free)
1211                 flatvelocity2 = skeleton.joints[i].velocity;
1212             if (!skeleton.free)
1213                 flatfacing2 = DoRotation(DoRotation(DoRotation(skeleton.joints[i].position, 0, 0, tilt), tilt2, 0, 0), 0, yaw, 0) * scale + coords;
1214             if (skeleton.free)
1215                 flatfacing2 = skeleton.joints[i].position * scale + coords;
1216             flatvelocity2.x += (float)(abs(Random() % 100) - 50) / 10;
1217             flatvelocity2.y += (float)(abs(Random() % 100) - 50) / 10;
1218             flatvelocity2.z += (float)(abs(Random() % 100) - 50) / 10;
1219             Sprite::MakeSprite(bloodflamesprite, flatfacing2, flatvelocity2, 1, 1, 1, 3, 1);
1220             Sprite::MakeSprite(bloodsprite, flatfacing2, flatvelocity2, 1, 1, 1, .4, 1);
1221             Sprite::MakeSprite(cloudsprite, flatfacing2, flatvelocity2 * 0, .6, 0, 0, 1, .5);
1222         }
1223
1224         emit_sound_at(splattersound, coords);
1225
1226         skeleton.free = 2;
1227         DoDamage(10000);
1228         RagDoll(0);
1229         if (!dead && creature == wolftype) {
1230             award_bonus(0, Wolfbonus);
1231         }
1232         dead = 2;
1233         coords = 20;
1234     }
1235
1236     // play sounds
1237     if (tutoriallevel != 1 || id == 0)
1238         if (speechdelay <= 0 && !dead && aitype != playercontrolled) {
1239             int whichsound = -1;
1240
1241             if (creature == wolftype) {
1242                 int i = abs(Random() % 2);
1243                 if (i == 0)
1244                     whichsound = snarlsound;
1245                 if (i == 1)
1246                     whichsound = snarl2sound;
1247                 envsound[numenvsounds] = coords;
1248                 envsoundvol[numenvsounds] = 16;
1249                 envsoundlife[numenvsounds] = .4;
1250                 numenvsounds++;
1251             }
1252             if (creature == rabbittype) {
1253                 int i = abs(Random() % 2);
1254                 if (i == 0)
1255                     whichsound = rabbitpainsound;
1256                 if (i == 1 && damage > damagetolerance)
1257                     whichsound = rabbitpain1sound;
1258                 envsound[numenvsounds] = coords;
1259                 envsoundvol[numenvsounds] = 16;
1260                 envsoundlife[numenvsounds] = .4;
1261                 numenvsounds++;
1262             }
1263
1264             if (whichsound != -1) {
1265                 emit_sound_at(whichsound, coords);
1266             }
1267         }
1268     speechdelay = .3;
1269 }
1270
1271 /* EFFECT
1272  * calculate/animate head facing direction?
1273  */
1274 void Person::DoHead()
1275 {
1276     static XYZ rotatearound;
1277     static XYZ facing;
1278     static float lookspeed = 500;
1279
1280     if (!freeze && !winfreeze) {
1281
1282         //head facing
1283         targetheadyaw = (float)((int)((0 - yaw - targetheadyaw + 180) * 100) % 36000) / 100;
1284         targetheadpitch = (float)((int)(targetheadpitch * 100) % 36000) / 100;
1285
1286         while (targetheadyaw > 180)targetheadyaw -= 360;
1287         while (targetheadyaw < -180)targetheadyaw += 360;
1288
1289         if (targetheadyaw > 160)
1290             targetheadpitch = targetheadpitch * -1;
1291         if (targetheadyaw < -160)
1292             targetheadpitch = targetheadpitch * -1;
1293         if (targetheadyaw > 160)
1294             targetheadyaw = targetheadyaw - 180;
1295         if (targetheadyaw < -160)
1296             targetheadyaw = targetheadyaw + 180;
1297
1298         if (targetheadpitch > 120)
1299             targetheadpitch = 120;
1300         if (targetheadpitch < -120)
1301             targetheadpitch = -120;
1302         if (targetheadyaw > 120)
1303             targetheadyaw = 120;
1304         if (targetheadyaw < -120)
1305             targetheadyaw = -120;
1306
1307         if (!isIdle())
1308             targetheadpitch = 0;
1309         if (isIdle()) {
1310             if (targetheadyaw > 80)
1311                 targetheadyaw = 80;
1312             if (targetheadyaw < -80)
1313                 targetheadyaw = -80;
1314             if (targetheadpitch > 50)
1315                 targetheadpitch = 50;
1316             if (targetheadpitch < -50)
1317                 targetheadpitch = -50;
1318         }
1319
1320         if (abs(headyaw - targetheadyaw) < multiplier * lookspeed)
1321             headyaw = targetheadyaw;
1322         else if (headyaw > targetheadyaw) {
1323             headyaw -= multiplier * lookspeed;
1324         } else if (headyaw < targetheadyaw) {
1325             headyaw += multiplier * lookspeed;
1326         }
1327
1328         if (abs(headpitch - targetheadpitch) < multiplier * lookspeed / 2)
1329             headpitch = targetheadpitch;
1330         else if (headpitch > targetheadpitch) {
1331             headpitch -= multiplier * lookspeed / 2;
1332         } else if (headpitch < targetheadpitch) {
1333             headpitch += multiplier * lookspeed / 2;
1334         }
1335
1336         rotatearound = jointPos(neck);
1337         jointPos(head) = rotatearound + DoRotation(jointPos(head) - rotatearound, headpitch, 0, 0);
1338
1339         facing = 0;
1340         facing.z = -1;
1341         if (animTarget != bounceidleanim && animTarget != fightidleanim && animTarget != wolfidle && animTarget != knifefightidleanim && animTarget != drawrightanim && animTarget != drawleftanim && animTarget != walkanim) {
1342             facing = DoRotation(facing, headpitch * .4, 0, 0);
1343             facing = DoRotation(facing, 0, headyaw * .4, 0);
1344         }
1345
1346         if (animTarget == bounceidleanim || animTarget == fightidleanim || animTarget == wolfidle || animTarget == knifefightidleanim || animTarget == drawrightanim || animTarget == drawleftanim) {
1347             facing = DoRotation(facing, headpitch * .8, 0, 0);
1348             facing = DoRotation(facing, 0, headyaw * .8, 0);
1349         }
1350
1351         if (animTarget == walkanim) {
1352             facing = DoRotation(facing, headpitch * .6, 0, 0);
1353             facing = DoRotation(facing, 0, headyaw * .6, 0);
1354         }
1355
1356         skeleton.specialforward[0] = facing;
1357         //skeleton.specialforward[0]=DoRotation(facing,0,yaw,0);
1358         for (int i = 0; i < skeleton.num_muscles; i++) {
1359             if (skeleton.muscles[i].visible && (skeleton.muscles[i].parent1->label == head || skeleton.muscles[i].parent2->label == head)) {
1360                 skeleton.FindRotationMuscle(i, animTarget);
1361             }
1362         }
1363     }
1364 }
1365
1366 /* EFFECT
1367  * ragdolls character?
1368  */
1369 void Person::RagDoll(bool checkcollision)
1370 {
1371     static XYZ change;
1372     static int l, i, j;
1373     static float speed;
1374     if (!skeleton.free) {
1375         if (id == 0)
1376             numfalls++;
1377         if (id == 0 && isFlip())
1378             numflipfail++;
1379
1380         escapednum = 0;
1381
1382         facing = 0;
1383         facing.z = 1;
1384         facing = DoRotation(facing, 0, yaw, 0);
1385
1386         skeleton.freetime = 0;
1387
1388         skeleton.longdead = 0;
1389
1390         skeleton.free = 1;
1391         skeleton.broken = 0;
1392         skeleton.spinny = 1;
1393         freefall = 1;
1394         skeleton.freefall = 1;
1395
1396         if (!isnormal(velocity.x)) velocity.x = 0;
1397         if (!isnormal(velocity.y)) velocity.y = 0;
1398         if (!isnormal(velocity.z)) velocity.z = 0;
1399         if (!isnormal(yaw)) yaw = 0;
1400         if (!isnormal(coords.x)) coords = 0;
1401         if (!isnormal(tilt)) tilt = 0;
1402         if (!isnormal(tilt2)) tilt2 = 0;
1403
1404         for (int i = 0; i < skeleton.num_joints; i++) {
1405             skeleton.joints[i].delay = 0;
1406             skeleton.joints[i].locked = 0;
1407             skeleton.joints[i].position = DoRotation(DoRotation(DoRotation(skeleton.joints[i].position, 0, 0, tilt), tilt2, 0, 0), 0, yaw, 0);
1408             if (!isnormal(skeleton.joints[i].position.x)) skeleton.joints[i].position = DoRotation(skeleton.joints[i].position, 0, yaw, 0);
1409             if (!isnormal(skeleton.joints[i].position.x)) skeleton.joints[i].position = coords;
1410             skeleton.joints[i].position.y += .1;
1411             skeleton.joints[i].oldposition = skeleton.joints[i].position;
1412             skeleton.joints[i].realoldposition = skeleton.joints[i].position * scale + coords;
1413         }
1414
1415         for (int i = 0; i < skeleton.num_joints; i++) {
1416             skeleton.joints[i].velocity = 0;
1417             skeleton.joints[i].velchange = 0;
1418         }
1419         skeleton.DoConstraints(&coords, &scale);
1420         if (animation[animCurrent].height == lowheight || animation[animTarget].height == lowheight) {
1421             skeleton.DoConstraints(&coords, &scale);
1422             skeleton.DoConstraints(&coords, &scale);
1423             skeleton.DoConstraints(&coords, &scale);
1424             skeleton.DoConstraints(&coords, &scale);
1425         }
1426
1427         speed = animation[animTarget].speed[frameTarget] * 2;
1428         if (animation[animCurrent].speed[frameCurrent] > animation[animTarget].speed[frameTarget]) {
1429             speed = animation[animCurrent].speed[frameCurrent] * 2;
1430         }
1431         if (transspeed)
1432             speed = transspeed * 2;
1433
1434         speed *= speedmult;
1435
1436         for (int i = 0; i < skeleton.num_joints; i++) {
1437             if ((animation[animCurrent].attack != reversed || animCurrent == swordslashreversedanim) && animCurrent != rabbitkickanim && !isLanding() && !wasLanding() && animation[animCurrent].height == animation[animTarget].height)
1438                 skeleton.joints[i].velocity = velocity / scale + facing * 5 + DoRotation(DoRotation(DoRotation((animation[animTarget].position[i][frameTarget] - animation[animCurrent].position[i][frameCurrent]) * speed, 0, 0, tilt), tilt2, 0, 0), 0, yaw, 0);
1439             else
1440                 skeleton.joints[i].velocity = velocity / scale + facing * 5;
1441             change.x = (float)(Random() % 100) / 100;
1442             change.y = (float)(Random() % 100) / 100;
1443             change.z = (float)(Random() % 100) / 100;
1444             skeleton.joints[i].velocity += change;
1445             skeleton.joints[abs(Random() % skeleton.num_joints)].velocity -= change;
1446
1447             change.x = (float)(Random() % 100) / 100;
1448             change.y = (float)(Random() % 100) / 100;
1449             change.z = (float)(Random() % 100) / 100;
1450             skeleton.joints[i].velchange += change;
1451             skeleton.joints[abs(Random() % skeleton.num_joints)].velchange -= change;
1452         }
1453
1454         if (checkcollision) {
1455             XYZ average;
1456             XYZ lowpoint;
1457             XYZ colpoint;
1458             int howmany;
1459             average = 0;
1460             howmany = 0;
1461             for (j = 0; j < skeleton.num_joints; j++) {
1462                 average += skeleton.joints[j].position;
1463                 howmany++;
1464             }
1465             average /= howmany;
1466             coords += average * scale;
1467             for (j = 0; j < skeleton.num_joints; j++) {
1468                 skeleton.joints[j].position -= average;
1469             }
1470
1471             whichpatchx = coords.x / (terrain.size / subdivision * terrain.scale);
1472             whichpatchz = coords.z / (terrain.size / subdivision * terrain.scale);
1473             if (terrain.patchobjectnum[whichpatchx][whichpatchz])
1474                 for (l = 0; l < terrain.patchobjectnum[whichpatchx][whichpatchz]; l++) {
1475                     i = terrain.patchobjects[whichpatchx][whichpatchz][l];
1476                     lowpoint = coords;
1477                     lowpoint.y += 1;
1478                     if (SphereCheck(&lowpoint, 3, &colpoint, &objects.position[i], &objects.yaw[i], &objects.model[i]) != -1) {
1479                         coords.x = lowpoint.x;
1480                         coords.z = lowpoint.z;
1481                     }
1482                 }
1483         }
1484
1485         yaw = 0;
1486         updatedelay = 0;
1487
1488         velocity = 0;
1489         for (int i = 0; i < skeleton.num_joints; i++) {
1490             velocity += skeleton.joints[i].velocity * scale;
1491         }
1492         velocity /= skeleton.num_joints;
1493
1494         // drop weapon
1495         if (Random() % 2 == 0) {
1496             if (weaponactive != -1 && animTarget != rabbitkickanim && num_weapons > 0) {
1497                 weapons[weaponids[0]].drop(jointVel(righthand) * scale * -.3, jointVel(righthand) * scale);
1498                 weapons[weaponids[0]].velocity.x += .01;
1499                 num_weapons--;
1500                 if (num_weapons) {
1501                     weaponids[0] = weaponids[num_weapons];
1502                     if (weaponstuck == num_weapons)
1503                         weaponstuck = 0;
1504                 }
1505                 weaponactive = -1;
1506                 for (unsigned i = 0; i < Person::players.size(); i++) {
1507                     Person::players[i]->wentforweapon = 0;
1508                 }
1509             }
1510         }
1511
1512         animTarget = bounceidleanim;
1513         animCurrent = bounceidleanim;
1514         frameTarget = 0;
1515         frameCurrent = 0;
1516     }
1517 }
1518
1519
1520
1521 /* EFFECT
1522  */
1523 void Person::FootLand(int which, float opacity)
1524 {
1525     static XYZ terrainlight;
1526     static XYZ footvel, footpoint;
1527     if (opacity >= 1 || skiddelay <= 0)
1528         if (opacity > 1) {
1529             footvel = 0;
1530             if (which == 0)
1531                 footpoint = DoRotation(jointPos(leftfoot), 0, yaw, 0) * scale + coords;
1532             if (which == 1)
1533                 footpoint = DoRotation(jointPos(rightfoot), 0, yaw, 0) * scale + coords;
1534             //footpoint.y=coords.y;
1535             if (distsq(&footpoint, &viewer))
1536                 Sprite::MakeSprite(cloudsprite, footpoint, footvel, 1, 1, 1, .5, .2 * opacity);
1537         } else if (environment == snowyenvironment && onterrain && terrain.getOpacity(coords.x, coords.z) < .2) {
1538             footvel = velocity / 5;
1539             if (footvel.y < .8)
1540                 footvel.y = .8;
1541             if (which == 0)
1542                 footpoint = DoRotation(jointPos(leftfoot), 0, yaw, 0) * scale + coords;
1543             if (which == 1)
1544                 footpoint = DoRotation(jointPos(rightfoot), 0, yaw, 0) * scale + coords;
1545             footpoint.y = terrain.getHeight(footpoint.x, footpoint.z);
1546             terrainlight = terrain.getLighting(footpoint.x, footpoint.z);
1547             if (distsq(&footpoint, &viewer) < viewdistance * viewdistance / 4)
1548                 Sprite::MakeSprite(cloudsprite, footpoint, footvel * .6, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7 * opacity);
1549             if (opacity >= 1 || detail == 2)
1550                 if (detail == 2)
1551                     if (distsq(&footpoint, &viewer) < viewdistance * viewdistance / 4)
1552                         terrain.MakeDecal(footprintdecal, footpoint, .2, 1 * opacity, yaw);
1553         } else if (environment == grassyenvironment && onterrain && terrain.getOpacity(coords.x, coords.z) < .2) {
1554             footvel = velocity / 5;
1555             if (footvel.y < .8)
1556                 footvel.y = .8;
1557             if (which == 0)
1558                 footpoint = DoRotation(jointPos(leftfoot), 0, yaw, 0) * scale + coords;
1559             if (which == 1)
1560                 footpoint = DoRotation(jointPos(rightfoot), 0, yaw, 0) * scale + coords;
1561             footpoint.y = terrain.getHeight(footpoint.x, footpoint.z);
1562             terrainlight = terrain.getLighting(footpoint.x, footpoint.z);
1563             if (distsq(&footpoint, &viewer) < viewdistance * viewdistance / 4)
1564                 Sprite::MakeSprite(cloudsprite, footpoint, footvel * .6, terrainlight.x * 90 / 255, terrainlight.y * 70 / 255, terrainlight.z * 8 / 255, .5, .5 * opacity);
1565         } else if (environment == desertenvironment && onterrain && terrain.getOpacity(coords.x, coords.z) < .2) {
1566             footvel = velocity / 5;
1567             if (footvel.y < .8)
1568                 footvel.y = .8;
1569             if (which == 0)
1570                 footpoint = DoRotation(jointPos(leftfoot), 0, yaw, 0) * scale + coords;
1571             if (which == 1)
1572                 footpoint = DoRotation(jointPos(rightfoot), 0, yaw, 0) * scale + coords;
1573             footpoint.y = terrain.getHeight(footpoint.x, footpoint.z);
1574             terrainlight = terrain.getLighting(footpoint.x, footpoint.z);
1575             if (distsq(&footpoint, &viewer) < viewdistance * viewdistance / 4)
1576                 Sprite::MakeSprite(cloudsprite, footpoint, footvel * .6, terrainlight.x * 190 / 255, terrainlight.y * 170 / 255, terrainlight.z * 108 / 255, .5, .7 * opacity);
1577             if (opacity >= 1 || detail == 2)
1578                 if (detail == 2)
1579                     if (distsq(&footpoint, &viewer) < viewdistance * viewdistance / 4)
1580                         terrain.MakeDecal(footprintdecal, footpoint, .2, .25 * opacity, yaw);
1581         } else if (isLanding() || animTarget == jumpupanim || isLandhard()) {
1582             footvel = velocity / 5;
1583             if (footvel.y < .8)
1584                 footvel.y = .8;
1585             if (which == 0)
1586                 footpoint = DoRotation(jointPos(leftfoot), 0, yaw, 0) * scale + coords;
1587             if (which == 1)
1588                 footpoint = DoRotation(jointPos(rightfoot), 0, yaw, 0) * scale + coords;
1589             //footpoint.y=coords.y;
1590             if (distsq(&footpoint, &viewer) < viewdistance * viewdistance / 4)
1591                 Sprite::MakeSprite(cloudsprite, footpoint, footvel * .6, 1, 1, 1, .5, .2 * opacity);
1592         }
1593 }
1594
1595 /* EFFECT
1596  * make a puff effect at a body part (dust effect?)
1597  */
1598 void Person::Puff(int whichlabel)
1599 {
1600     static XYZ footvel, footpoint;
1601
1602     footvel = 0;
1603     footpoint = DoRotation(jointPos(whichlabel), 0, yaw, 0) * scale + coords;
1604     Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 1, 1, .9, .3);
1605 }
1606
1607 /* EFFECT
1608  * I think I added this in an attempt to clean up code
1609  */
1610 void Person::setAnimation(int animation)
1611 {
1612     animTarget = animation;
1613     frameTarget = 0;
1614     target = 0;
1615 }
1616
1617 /* EFFECT
1618  * MONSTER
1619  * TODO: ???
1620  */
1621 void Person::DoAnimations()
1622 {
1623     if (!skeleton.free) {
1624         static float oldtarget;
1625
1626         if (isIdle() && animCurrent != getIdle())
1627             normalsupdatedelay = 0;
1628
1629         if (animTarget == tempanim || animCurrent == tempanim) {
1630             animation[tempanim] = tempanimation;
1631         }
1632         if (animTarget == jumpupanim || animTarget == jumpdownanim || isFlip()) {
1633             float gLoc[3];
1634             float vel[3];
1635             gLoc[0] = coords.x;
1636             gLoc[1] = coords.y;
1637             gLoc[2] = coords.z;
1638             vel[0] = velocity.x;
1639             vel[1] = velocity.y;
1640             vel[2] = velocity.z;
1641
1642             if (id == 0) {
1643                 OPENAL_3D_SetAttributes(channels[whooshsound], gLoc, vel);
1644                 OPENAL_SetVolume(channels[whooshsound], 64 * findLength(&velocity) / 5);
1645             }
1646             if (((velocity.y < -15) || (crouchkeydown && velocity.y < -8)) && abs(velocity.y) * 4 > fast_sqrt(velocity.x * velocity.x * velocity.z * velocity.z))
1647                 landhard = 1;
1648             if (!crouchkeydown && velocity.y >= -15)
1649                 landhard = 0;
1650         }
1651         if ((animCurrent == jumpupanim || animTarget == jumpdownanim)/*&&velocity.y<40*/ && !isFlip() && (!isLanding() && !isLandhard()) && ((crouchkeydown && !crouchtogglekeydown))) {
1652             XYZ targfacing;
1653             targfacing = 0;
1654             targfacing.z = 1;
1655
1656             targfacing = DoRotation(targfacing, 0, targetyaw, 0);
1657
1658             if (normaldotproduct(targfacing, velocity) >= -.3)
1659                 animTarget = flipanim;
1660             else
1661                 animTarget = backflipanim;
1662             crouchtogglekeydown = 1;
1663             frameTarget = 0;
1664             target = 0;
1665
1666             if (id == 0)
1667                 numflipped++;
1668         }
1669
1670         if (animation[animTarget].attack != reversed)
1671             feint = 0;
1672         if (!crouchkeydown || (isLanding() || isLandhard()) || (wasLanding() || wasLandhard())) {
1673             crouchtogglekeydown = 0;
1674             if (aitype == playercontrolled)
1675                 feint = 0;
1676         } else {
1677             if (!crouchtogglekeydown && animation[animTarget].attack == reversed && aitype == playercontrolled && (escapednum < 2 || reversaltrain))
1678                 feint = 1;
1679             if (!isFlip())
1680                 crouchtogglekeydown = 1;
1681         }
1682
1683
1684         if (animation[animTarget].attack || animCurrent == getupfrombackanim || animCurrent == getupfromfrontanim) {
1685             if (detail)
1686                 normalsupdatedelay = 0;
1687         }
1688
1689         if (target >= 1) {
1690             if (animTarget == rollanim && frameTarget == 3 && onfire) {
1691                 onfire = 0;
1692                 emit_sound_at(fireendsound, coords);
1693                 pause_sound(stream_firesound);
1694                 deathbleeding = 0;
1695             }
1696
1697             if (animTarget == rabbittacklinganim && frameTarget == 1) {
1698                 if (victim->aitype == attacktypecutoff && victim->stunned <= 0 && victim->surprised <= 0 && victim->id != 0)
1699                     Reverse();
1700                 if (animTarget == rabbittacklinganim && frameTarget == 1 && !victim->isCrouch() && victim->animTarget != backhandspringanim) {
1701                     if (normaldotproduct(victim->facing, facing) > 0)
1702                         victim->animTarget = rabbittackledbackanim;
1703                     else
1704                         victim->animTarget = rabbittackledfrontanim;
1705                     victim->frameTarget = 2;
1706                     victim->target = 0;
1707                     victim->yaw = yaw;
1708                     victim->targetyaw = yaw;
1709                     if (victim->aitype == gethelptype)
1710                         victim->DoDamage(victim->damagetolerance - victim->damage);
1711                     //victim->DoDamage(30);
1712                     if (creature == wolftype) {
1713                         DoBloodBig(0, 255);
1714                         emit_sound_at(clawslicesound, victim->coords);
1715                         victim->spurt = 1;
1716                         victim->DoBloodBig(1 / victim->armorhead, 210);
1717                     }
1718                     award_bonus(id, TackleBonus,
1719                                 victim->aitype == gethelptype ? 50 : 0);
1720                 }
1721             }
1722
1723             if (!drawtogglekeydown && drawkeydown && (weaponactive == -1 || num_weapons == 1) && (animation[animTarget].label[frameTarget] || (animTarget != animCurrent && animCurrent == rollanim)) && num_weapons > 0 && creature != wolftype) {
1724                 if (weapons[weaponids[0]].getType() == knife) {
1725                     if (weaponactive == -1)
1726                         weaponactive = 0;
1727                     else if (weaponactive == 0)
1728                         weaponactive = -1;
1729
1730                     if (weaponactive == -1) {
1731                         emit_sound_at(knifesheathesound, coords);
1732                     }
1733                     if (weaponactive != -1) {
1734                         emit_sound_at(knifedrawsound, coords, 128);
1735                     }
1736                 }
1737                 drawtogglekeydown = 1;
1738             }
1739             //Footstep sounds
1740             if (tutoriallevel != 1 || id == 0)
1741                 if ((animation[animTarget].label[frameTarget] && (animation[animTarget].label[frameTarget] < 5 || animation[animTarget].label[frameTarget] == 8))/*||(animTarget==rollanim&&frameTarget==animation[rollanim].numframes-1)*/) {
1742                     int whichsound;
1743                     if (onterrain) {
1744                         if (terrain.getOpacity(coords.x, coords.z) < .2) {
1745                             if (animation[animTarget].label[frameTarget] == 1)
1746                                 whichsound = footstepsound;
1747                             else
1748                                 whichsound = footstepsound2;
1749                             if (animation[animTarget].label[frameTarget] == 1)
1750                                 FootLand(0, 1);
1751                             if (animation[animTarget].label[frameTarget] == 2)
1752                                 FootLand(1, 1);
1753                             if (animation[animTarget].label[frameTarget] == 3 && isRun()) {
1754                                 FootLand(1, 1);
1755                                 FootLand(0, 1);
1756                             }
1757
1758                         }
1759                         if (terrain.getOpacity(coords.x, coords.z) >= .2) {
1760                             if (animation[animTarget].label[frameTarget] == 1)
1761                                 whichsound = footstepsound3;
1762                             else
1763                                 whichsound = footstepsound4;
1764                         }
1765                     }
1766                     if (!onterrain) {
1767                         if (animation[animTarget].label[frameTarget] == 1)
1768                             whichsound = footstepsound3;
1769                         else
1770                             whichsound = footstepsound4;
1771                     }
1772                     if (animation[animTarget].label[frameTarget] == 4 && (weaponactive == -1 || (animTarget != knifeslashstartanim && animTarget != knifethrowanim && animTarget != crouchstabanim && animTarget != swordgroundstabanim && animTarget != knifefollowanim))) {
1773                         if (animation[animTarget].attack != neutral) {
1774                             unsigned r = abs(Random() % 3);
1775                             if (r == 0)
1776                                 whichsound = lowwhooshsound;
1777                             if (r == 1)
1778                                 whichsound = midwhooshsound;
1779                             if (r == 2)
1780                                 whichsound = highwhooshsound;
1781                         }
1782                         if (animation[animTarget].attack == neutral)
1783                             whichsound = movewhooshsound;
1784                     } else if (animation[animTarget].label[frameTarget] == 4)
1785                         whichsound = knifeswishsound;
1786                     if (animation[animTarget].label[frameTarget] == 8 && tutoriallevel != 1)
1787                         whichsound = landsound2;
1788
1789                     emit_sound_at(whichsound, coords, 256.);
1790
1791                     if (id == 0)
1792                         if (whichsound == footstepsound || whichsound == footstepsound2 || whichsound == footstepsound3 || whichsound == footstepsound4) {
1793                             envsound[numenvsounds] = coords;
1794                             if (animTarget == wolfrunninganim || animTarget == rabbitrunninganim)
1795                                 envsoundvol[numenvsounds] = 15;
1796                             else
1797                                 envsoundvol[numenvsounds] = 6;
1798                             envsoundlife[numenvsounds] = .4;
1799                             numenvsounds++;
1800                         }
1801
1802                     if (animation[animTarget].label[frameTarget] == 3) {
1803                         whichsound--;
1804                         emit_sound_at(whichsound, coords, 128.);
1805                     }
1806                 }
1807
1808             //Combat sounds
1809             if (tutoriallevel != 1 || id == 0)
1810                 if (speechdelay <= 0)
1811                     if (animTarget != crouchstabanim && animTarget != swordgroundstabanim && animTarget != staffgroundsmashanim)
1812                         if ((animation[animTarget].label[frameTarget] && (animation[animTarget].label[frameTarget] < 5 || animation[animTarget].label[frameTarget] == 8))/*||(animTarget==rollanim&&frameTarget==animation[rollanim].numframes-1)*/) {
1813                             int whichsound = -1;
1814                             if (animation[animTarget].label[frameTarget] == 4 && aitype != playercontrolled) {
1815                                 if (animation[animTarget].attack != neutral) {
1816                                     unsigned r = abs(Random() % 4);
1817                                     if (creature == rabbittype) {
1818                                         if (r == 0) whichsound = rabbitattacksound;
1819                                         if (r == 1) whichsound = rabbitattack2sound;
1820                                         if (r == 2) whichsound = rabbitattack3sound;
1821                                         if (r == 3) whichsound = rabbitattack4sound;
1822                                     }
1823                                     if (creature == wolftype) {
1824                                         if (r == 0) whichsound = barksound;
1825                                         if (r == 1) whichsound = bark2sound;
1826                                         if (r == 2) whichsound = bark3sound;
1827                                         if (r == 3) whichsound = barkgrowlsound;
1828                                     }
1829                                     speechdelay = .3;
1830                                 }
1831                             }
1832
1833                             if (whichsound != -1) {
1834                                 emit_sound_at(whichsound, coords);
1835                             }
1836                         }
1837
1838
1839
1840             if ((!wasLanding() && !wasLandhard()) && animCurrent != getIdle() && (isLanding() || isLandhard())) {
1841                 FootLand(0, 1);
1842                 FootLand(1, 1);
1843             }
1844
1845             transspeed = 0;
1846             currentoffset = targetoffset;
1847             frameTarget = frameCurrent;
1848             animCurrent = animTarget;
1849             frameTarget++;
1850
1851             if (animTarget == removeknifeanim && animation[animTarget].label[frameCurrent] == 5) {
1852                 for (unsigned i = 0; i < weapons.size(); i++) {
1853                     if (weapons[i].owner == -1)
1854                         if (distsqflat(&coords, &weapons[i].position) < 4 && weaponactive == -1) {
1855                             if (distsq(&coords, &weapons[i].position) >= 1) {
1856                                 if (weapons[i].getType() != staff) {
1857                                     emit_sound_at(knifedrawsound, coords, 128.);
1858                                 }
1859
1860                                 takeWeapon(i);
1861                             }
1862                         }
1863                 }
1864             }
1865
1866             if (animTarget == crouchremoveknifeanim && animation[animTarget].label[frameCurrent] == 5) {
1867                 for (unsigned i = 0; i < weapons.size(); i++) {
1868                     bool willwork = true;
1869                     if (weapons[i].owner != -1)
1870                         if (Person::players[weapons[i].owner]->weaponstuck != -1)
1871                             if (Person::players[weapons[i].owner]->weaponids[Person::players[weapons[i].owner]->weaponstuck] == int(i))
1872                                 if (Person::players[weapons[i].owner]->num_weapons > 1)
1873                                     willwork = 0;
1874                     if ((weapons[i].owner == -1) || (hasvictim && (weapons[i].owner == int(victim->id)) && victim->skeleton.free))
1875                         if (willwork && distsqflat(&coords, &weapons[i].position) < 3 && weaponactive == -1) {
1876                             if (distsq(&coords, &weapons[i].position) < 1 || hasvictim) {
1877                                 bool fleshstuck = false;
1878                                 if (weapons[i].owner != -1)
1879                                     if (victim->weaponstuck != -1) {
1880                                         if (victim->weaponids[victim->weaponstuck] == int(i)) {
1881                                             fleshstuck = true;
1882                                         }
1883                                     }
1884                                 if (fleshstuck) {
1885                                     emit_sound_at(fleshstabremovesound, coords, 128.);
1886                                 } else {
1887                                     if (weapons[i].getType() != staff) {
1888                                         emit_sound_at(knifedrawsound, coords, 128.);
1889                                     }
1890                                 }
1891                                 if (weapons[i].owner != -1) {
1892                                     victim = Person::players[weapons[i].owner];
1893                                     if (victim->num_weapons == 1)
1894                                         victim->num_weapons = 0;
1895                                     else
1896                                         victim->num_weapons = 1;
1897
1898                                     //victim->weaponactive=-1;
1899                                     victim->skeleton.longdead = 0;
1900                                     victim->skeleton.free = 1;
1901                                     victim->skeleton.broken = 0;
1902
1903                                     for (int j = 0; j < victim->skeleton.num_joints; j++) {
1904                                         victim->skeleton.joints[j].velchange = 0;
1905                                         victim->skeleton.joints[j].locked = 0;
1906                                     }
1907
1908                                     XYZ relative;
1909                                     relative = 0;
1910                                     relative.y = 10;
1911                                     Normalise(&relative);
1912                                     XYZ footvel, footpoint;
1913                                     footvel = 0;
1914                                     footpoint = weapons[i].position;
1915                                     if (victim->weaponstuck != -1) {
1916                                         if (victim->weaponids[victim->weaponstuck] == int(i)) {
1917                                             if (bloodtoggle)
1918                                                 Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 0, 0, .8, .3);
1919                                             weapons[i].bloody = 2;
1920                                             weapons[i].blooddrip = 5;
1921                                             victim->weaponstuck = -1;
1922                                         }
1923                                     }
1924                                     if (victim->num_weapons > 0) {
1925                                         if (victim->weaponstuck != 0 && victim->weaponstuck != -1)
1926                                             victim->weaponstuck = 0;
1927                                         if (victim->weaponids[0] == int(i))
1928                                             victim->weaponids[0] = victim->weaponids[victim->num_weapons];
1929                                     }
1930
1931                                     victim->jointVel(abdomen) += relative * 6;
1932                                     victim->jointVel(neck) += relative * 6;
1933                                     victim->jointVel(rightshoulder) += relative * 6;
1934                                     victim->jointVel(leftshoulder) += relative * 6;
1935                                 }
1936                                 takeWeapon(i);
1937                             }
1938                         }
1939                 }
1940             }
1941
1942             if (animCurrent == drawleftanim && animation[animTarget].label[frameCurrent] == 5) {
1943                 if (weaponactive == -1)
1944                     weaponactive = 0;
1945                 else if (weaponactive == 0) {
1946                     weaponactive = -1;
1947                     if (num_weapons == 2) {
1948                         int buffer;
1949                         buffer = weaponids[0];
1950                         weaponids[0] = weaponids[1];
1951                         weaponids[1] = buffer;
1952                     }
1953                 }
1954                 if (weaponactive == -1) {
1955                     emit_sound_at(knifesheathesound, coords, 128.);
1956                 }
1957                 if (weaponactive != -1) {
1958                     emit_sound_at(knifedrawsound, coords, 128.);
1959                 }
1960             }
1961
1962
1963             if ((animCurrent == walljumprightkickanim && animTarget == walljumprightkickanim) || (animCurrent == walljumpleftkickanim && animTarget == walljumpleftkickanim)) {
1964                 XYZ rotatetarget = DoRotation(skeleton.forward, 0, yaw, 0);
1965                 Normalise(&rotatetarget);
1966                 targetyaw = -asin(0 - rotatetarget.x);
1967                 targetyaw *= 360 / 6.28;
1968                 if (rotatetarget.z < 0)
1969                     targetyaw = 180 - targetyaw;
1970
1971                 if (animTarget == walljumprightkickanim)
1972                     targetyaw += 40;
1973                 if (animTarget == walljumpleftkickanim)
1974                     targetyaw -= 40;
1975             }
1976
1977             bool dojumpattack;
1978             dojumpattack = 0;
1979             if ((animTarget == rabbitrunninganim || animTarget == wolfrunninganim) && frameTarget == 3 && (jumpkeydown || attackkeydown || id != 0))
1980                 dojumpattack = 1;
1981             if (hasvictim)
1982                 if (distsq(&victim->coords, &/*Person::players[i]->*/coords) < 5 && victim->aitype == gethelptype && (attackkeydown) && !victim->skeleton.free && victim->isRun() && victim->runninghowlong >= 1)
1983                     dojumpattack = 1;
1984             if (!hostile)
1985                 dojumpattack = 0;
1986             if (dojumpattack) {
1987                 if ((animTarget == rabbitrunninganim || animTarget == wolfrunninganim) && id == 0) {
1988                     animTarget = rabbittackleanim;
1989                     frameTarget = 0;
1990                     emit_sound_at(jumpsound, coords);
1991                 }
1992
1993                 float closestdist;
1994                 closestdist = 0;
1995                 int closestid;
1996                 closestid = -1;
1997                 XYZ targetloc;
1998                 targetloc = velocity;
1999                 Normalise(&targetloc);
2000                 targetloc += coords;
2001                 for (unsigned i = 0; i < Person::players.size(); i++) {
2002                     if (i != id)
2003                         if (distsq(&targetloc, &Person::players[i]->coords) < closestdist || closestdist == 0) {
2004                             closestdist = distsq(&targetloc, &Person::players[i]->coords);
2005                             closestid = i;
2006                         }
2007                 }
2008                 if (closestid != -1)
2009                     if (closestdist < 5 && !Person::players[closestid]->dead && animation[Person::players[closestid]->animTarget].height != lowheight && Person::players[closestid]->animTarget != backhandspringanim) {
2010                         hasvictim = 1;
2011                         victim = Person::players[closestid];
2012                         coords = victim->coords;
2013                         animCurrent = rabbittacklinganim;
2014                         animTarget = rabbittacklinganim;
2015                         frameCurrent = 0;
2016                         frameTarget = 1;
2017                         XYZ rotatetarget;
2018                         if (coords.z != victim->coords.z || coords.x != victim->coords.x) {
2019                             rotatetarget = coords - victim->coords;
2020                             Normalise(&rotatetarget);
2021                             targetyaw = -asin(0 - rotatetarget.x);
2022                             targetyaw *= 360 / 6.28;
2023                             if (rotatetarget.z < 0)
2024                                 targetyaw = 180 - targetyaw;
2025                         }
2026                         if (animTarget != rabbitrunninganim) {
2027                             emit_sound_at(jumpsound, coords, 128.);
2028                         }
2029                     }
2030             }
2031
2032             //Move impacts
2033             float damagemult = 1 * power;
2034             if (creature == wolftype)
2035                 damagemult = 2.5 * power;
2036             if (hasvictim) {
2037                 damagemult /= victim->damagetolerance / 200;
2038             }
2039             if ((animation[animTarget].attack == normalattack || animTarget == walljumprightkickanim || animTarget == walljumpleftkickanim) && (!feint) && (victim->skeleton.free != 2 || animTarget == killanim || animTarget == dropkickanim || animTarget == crouchstabanim || animTarget == swordgroundstabanim || animTarget == staffgroundsmashanim)) {
2040                 if (animTarget == spinkickanim && animation[animTarget].label[frameCurrent] == 5) {
2041                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 3 && 3 && animation[victim->animTarget].height != lowheight) {
2042                         escapednum = 0;
2043                         if (id == 0)
2044                             camerashake += .4;
2045                         if (Random() % 2 || creature == wolftype) {
2046                             victim->spurt = 1;
2047                             DoBlood(.2, 250);
2048                             if (creature == wolftype)
2049                                 DoBloodBig(0, 250);
2050                         }
2051                         if (tutoriallevel != 1) {
2052                             emit_sound_at(heavyimpactsound, victim->coords, 128.);
2053                         }
2054                         if (creature == wolftype) {
2055                             emit_sound_at(clawslicesound, victim->coords, 128.);
2056                             victim->spurt = 1;
2057                             victim->DoBloodBig(2 / victim->armorhead, 175);
2058                         }
2059                         victim->RagDoll(0);
2060                         XYZ relative;
2061                         relative = victim->coords - coords;
2062                         relative.y = 0;
2063                         Normalise(&relative);
2064                         relative = DoRotation(relative, 0, -90, 0);
2065                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2066                             victim->skeleton.joints[i].velocity += relative * damagemult * 40;
2067                         }
2068                         victim->jointVel(head) += relative * damagemult * 200;
2069                         //FootLand(1,2);
2070                         victim->Puff(head);
2071                         victim->DoDamage(damagemult * 100 / victim->protectionhead);
2072
2073                         SolidHitBonus(id);
2074                     }
2075                 }
2076
2077                 if (animTarget == wolfslapanim && animation[animTarget].label[frameCurrent] == 5) {
2078                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 3 && 3 && animation[victim->animTarget].height != lowheight) {
2079                         escapednum = 0;
2080                         if (id == 0)
2081                             camerashake += .4;
2082                         if (Random() % 2 || creature == wolftype) {
2083                             victim->spurt = 1;
2084                             if (creature == wolftype)
2085                                 DoBloodBig(0, 235);
2086                         }
2087                         emit_sound_at(whooshhitsound, victim->coords);
2088                         if (creature == wolftype) {
2089                             emit_sound_at(clawslicesound, victim->coords, 128.);
2090                             victim->spurt = 1;
2091                             victim->DoBloodBig(2, 175);
2092                         }
2093                         victim->RagDoll(0);
2094                         XYZ relative;
2095                         relative = victim->coords - coords;
2096                         relative.y = 0;
2097                         Normalise(&relative);
2098                         relative.y -= 1;
2099                         Normalise(&relative);
2100                         relative = DoRotation(relative, 0, 90, 0);
2101                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2102                             victim->skeleton.joints[i].velocity += relative * damagemult * 20;
2103                         }
2104                         victim->jointVel(head) += relative * damagemult * 100;
2105                         //FootLand(1,2);
2106                         victim->Puff(head);
2107                         victim->DoDamage(damagemult * 50 / victim->protectionhead);
2108                     }
2109                 }
2110
2111                 if (animTarget == walljumprightkickanim && animation[animTarget].label[frameCurrent] == 5) {
2112                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 3 && animation[victim->animTarget].height != lowheight) {
2113                         escapednum = 0;
2114                         if (id == 0)
2115                             camerashake += .4;
2116                         victim->spurt = 1;
2117                         DoBlood(.2, 250);
2118                         if (tutoriallevel != 1) {
2119                             emit_sound_at(heavyimpactsound, victim->coords, 160.);
2120                         }
2121                         if (creature == wolftype) {
2122                             emit_sound_at(clawslicesound, victim->coords, 128.);
2123                             victim->spurt = 1;
2124                             victim->DoBloodBig(2 / victim->armorhead, 175);
2125                         }
2126                         victim->RagDoll(0);
2127                         XYZ relative;
2128                         relative = facing;
2129                         relative.y = 0;
2130                         Normalise(&relative);
2131                         relative = DoRotation(relative, 0, -90, 0);
2132                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2133                             victim->skeleton.joints[i].velocity += relative * damagemult * 40;
2134                         }
2135                         victim->jointVel(head) += relative * damagemult * 200;
2136                         //FootLand(1,2);
2137                         victim->Puff(head);
2138                         victim->DoDamage(damagemult * 150 / victim->protectionhead);
2139
2140                         if (victim->damage > victim->damagetolerance)
2141                             award_bonus(id, style);
2142                         else
2143                             SolidHitBonus(id);
2144                     }
2145                 }
2146
2147                 if (animTarget == walljumpleftkickanim && animation[animTarget].label[frameCurrent] == 5) {
2148                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 3 && animation[victim->animTarget].height != lowheight) {
2149                         escapednum = 0;
2150                         if (id == 0)
2151                             camerashake += .4;
2152                         victim->spurt = 1;
2153                         DoBlood(.2, 250);
2154                         if (tutoriallevel != 1) {
2155                             emit_sound_at(heavyimpactsound, victim->coords, 160.);
2156                         }
2157                         if (creature == wolftype) {
2158                             emit_sound_at(clawslicesound, victim->coords, 128.);
2159                             victim->spurt = 1;
2160                             victim->DoBloodBig(2 / victim->armorhead, 175);
2161                         }
2162                         victim->RagDoll(0);
2163                         XYZ relative;
2164                         relative = facing;
2165                         relative.y = 0;
2166                         Normalise(&relative);
2167                         relative = DoRotation(relative, 0, 90, 0);
2168                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2169                             victim->skeleton.joints[i].velocity += relative * damagemult * 40;
2170                         }
2171                         victim->jointVel(head) += relative * damagemult * 200;
2172                         //FootLand(1,2);
2173                         victim->Puff(head);
2174                         victim->DoDamage(damagemult * 150 / victim->protectionhead);
2175
2176                         if (victim->damage > victim->damagetolerance)
2177                             award_bonus(id, style);
2178                         else
2179                             SolidHitBonus(id);
2180                     }
2181                 }
2182
2183                 if (animTarget == blockhighleftstrikeanim && animation[animTarget].label[frameCurrent] == 5) {
2184                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 3 && animation[victim->animTarget].height != lowheight) {
2185                         escapednum = 0;
2186                         if (id == 0)
2187                             camerashake += .4;
2188                         if (Random() % 2) {
2189                             victim->spurt = 1;
2190                             DoBlood(.2, 235);
2191                         }
2192                         emit_sound_at(whooshhitsound, victim->coords);
2193                         victim->RagDoll(0);
2194                         XYZ relative;
2195                         relative = victim->coords - coords;
2196                         relative.y = 0;
2197                         Normalise(&relative);
2198                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2199                             victim->skeleton.joints[i].velocity += relative * damagemult * 30;
2200                         }
2201                         victim->jointVel(head) += relative * damagemult * 100;
2202                         //FootLand(1,2);
2203                         victim->Puff(head);
2204                         victim->DoDamage(damagemult * 50 / victim->protectionhead);
2205                     }
2206                 }
2207
2208                 if (animTarget == killanim && animation[animTarget].label[frameCurrent] == 8) {
2209                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 3 && victim->dead) {
2210                         escapednum = 0;
2211                         if (id == 0)
2212                             camerashake += .2;
2213                         emit_sound_at(whooshhitsound, victim->coords, 128.);
2214
2215                         victim->skeleton.longdead = 0;
2216                         victim->skeleton.free = 1;
2217                         victim->skeleton.broken = 0;
2218                         victim->skeleton.spinny = 1;
2219
2220                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2221                             victim->skeleton.joints[i].velchange = 0;
2222                             victim->skeleton.joints[i].delay = 0;
2223                             victim->skeleton.joints[i].locked = 0;
2224                             //victim->skeleton.joints[i].velocity=0;
2225                         }
2226
2227                         XYZ relative;
2228                         relative = 0;
2229                         relative.y = 1;
2230                         Normalise(&relative);
2231                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2232                             victim->skeleton.joints[i].velocity.y = relative.y * 10;
2233                             victim->skeleton.joints[i].position.y += relative.y * .3;
2234                             victim->skeleton.joints[i].oldposition.y += relative.y * .3;
2235                             victim->skeleton.joints[i].realoldposition.y += relative.y * .3;
2236                         }
2237                         victim->Puff(abdomen);
2238                         victim->jointVel(abdomen).y = relative.y * 400;
2239                     }
2240                 }
2241
2242                 if (animTarget == killanim && animation[animTarget].label[frameCurrent] == 5) {
2243                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 9 && victim->dead) {
2244                         escapednum = 0;
2245                         if (id == 0)
2246                             camerashake += .4;
2247                         if (tutoriallevel != 1) {
2248                             emit_sound_at(heavyimpactsound, coords, 128.);
2249                         }
2250                         XYZ relative;
2251                         relative = victim->coords - coords;
2252                         relative.y = 0;
2253                         Normalise(&relative);
2254                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2255                             victim->skeleton.joints[i].velocity += relative * damagemult * 90;
2256                         }
2257                         victim->Puff(abdomen);
2258                         if (victim->dead != 2 && victim->permanentdamage > victim->damagetolerance - 250 && autoslomo) {
2259                             slomo = 1;
2260                             slomodelay = .2;
2261                         }
2262                         victim->DoDamage(damagemult * 500 / victim->protectionhigh);
2263                         victim->jointVel(abdomen) += relative * damagemult * 300;
2264                     }
2265                 }
2266
2267                 if (animTarget == dropkickanim && animation[animTarget].label[frameCurrent] == 7) {
2268                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 9 && victim->skeleton.free) {
2269                         escapednum = 0;
2270                         if (id == 0)
2271                             camerashake += .4;
2272                         if (tutoriallevel != 1) {
2273                             emit_sound_at(thudsound, coords);
2274                         }
2275
2276                         victim->skeleton.longdead = 0;
2277                         victim->skeleton.free = 1;
2278                         victim->skeleton.broken = 0;
2279                         victim->skeleton.spinny = 1;
2280
2281                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2282                             victim->skeleton.joints[i].velchange = 0;
2283                             //victim->skeleton.joints[i].delay=0;
2284                             victim->skeleton.joints[i].locked = 0;
2285                         }
2286                         XYZ relative;
2287                         relative = victim->coords - coords;
2288                         Normalise(&relative);
2289                         relative.y += .3;
2290                         Normalise(&relative);
2291                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2292                             victim->skeleton.joints[i].velocity += relative * damagemult * 20;
2293                         }
2294                         if (!victim->dead)
2295                             SolidHitBonus(id);
2296
2297                         victim->Puff(abdomen);
2298                         victim->DoDamage(damagemult * 20 / victim->protectionhigh);
2299                         victim->jointVel(abdomen) += relative * damagemult * 200;
2300                         staggerdelay = .5;
2301                         if (!victim->dead)
2302                             staggerdelay = 1.2;
2303
2304
2305                     }
2306                 }
2307
2308                 if ((animTarget == crouchstabanim || animTarget == swordgroundstabanim) && animation[animTarget].label[frameCurrent] == 5) {
2309
2310                     if (hasvictim)
2311                         if (!victim->skeleton.free)
2312                             hasvictim = 0;
2313
2314                     if (!hasvictim) {
2315                         terrain.MakeDecal(blooddecalfast, (weapons[weaponids[weaponactive]].tippoint * .8 + weapons[weaponids[weaponactive]].position * .2), .08, .6, Random() % 360);
2316                         emit_sound_at(knifesheathesound, coords, 128.);
2317                     }
2318
2319                     if (victim && hasvictim) {
2320                         if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 3) {
2321
2322                             XYZ where, startpoint, endpoint, movepoint, colpoint;
2323                             float rotationpoint;
2324                             int whichtri;
2325                             if (weapons[weaponids[weaponactive]].getType() == knife) {
2326                                 where = (weapons[weaponids[weaponactive]].tippoint * .6 + weapons[weaponids[weaponactive]].position * .4);
2327                                 where -= victim->coords;
2328                                 if (!victim->skeleton.free)
2329                                     where = DoRotation(where, 0, -victim->yaw, 0);
2330                                 //where=scale;
2331                                 startpoint = where;
2332                                 startpoint.y += 100;
2333                                 endpoint = where;
2334                                 endpoint.y -= 100;
2335                             }
2336                             if (weapons[weaponids[weaponactive]].getType() == sword) {
2337                                 where = weapons[weaponids[weaponactive]].position;
2338                                 where -= victim->coords;
2339                                 if (!victim->skeleton.free)
2340                                     where = DoRotation(where, 0, -victim->yaw, 0);
2341                                 startpoint = where;
2342                                 where = weapons[weaponids[weaponactive]].tippoint;
2343                                 where -= victim->coords;
2344                                 if (!victim->skeleton.free)
2345                                     where = DoRotation(where, 0, -victim->yaw, 0);
2346                                 endpoint = where;
2347                             }
2348                             if (weapons[weaponids[weaponactive]].getType() == staff) {
2349                                 where = weapons[weaponids[weaponactive]].position;
2350                                 where -= victim->coords;
2351                                 if (!victim->skeleton.free)
2352                                     where = DoRotation(where, 0, -victim->yaw, 0);
2353                                 startpoint = where;
2354                                 where = weapons[weaponids[weaponactive]].tippoint;
2355                                 where -= victim->coords;
2356                                 if (!victim->skeleton.free)
2357                                     where = DoRotation(where, 0, -victim->yaw, 0);
2358                                 endpoint = where;
2359                             }
2360                             movepoint = 0;
2361                             rotationpoint = 0;
2362                             whichtri = victim->skeleton.drawmodel.LineCheck(&startpoint, &endpoint, &colpoint, &movepoint, &rotationpoint);
2363
2364                             if (whichtri != -1) {
2365                                 if (victim->dead != 2) {
2366                                     victim->DoDamage(abs((victim->damagetolerance - victim->permanentdamage) * 2));
2367                                     if (!victim->dead)
2368                                         award_bonus(id, FinishedBonus);
2369                                 }
2370                                 if (bloodtoggle)
2371                                     weapons[weaponids[weaponactive]].bloody = 2;
2372
2373                                 victim->skeleton.longdead = 0;
2374                                 victim->skeleton.free = 1;
2375                                 victim->skeleton.broken = 0;
2376
2377                                 for (int i = 0; i < victim->skeleton.num_joints; i++) {
2378                                     victim->skeleton.joints[i].velchange = 0;
2379                                     victim->skeleton.joints[i].locked = 0;
2380                                     //victim->skeleton.joints[i].velocity=0;
2381                                 }
2382                                 emit_sound_at(fleshstabsound, coords, 128);
2383
2384                             }
2385                             if (whichtri != -1 || weapons[weaponids[weaponactive]].bloody) {
2386                                 weapons[weaponids[weaponactive]].blooddrip += 5;
2387                                 weapons[weaponids[weaponactive]].blooddripdelay = 0;
2388                             }
2389                             if (whichtri == -1) {
2390                                 hasvictim = 0;
2391                                 emit_sound_at(knifesheathesound, coords, 128.);
2392                             }
2393                         }
2394                     }
2395                 }
2396
2397                 if ((animTarget == crouchstabanim || animTarget == swordgroundstabanim) && animation[animTarget].label[frameCurrent] == 6) {
2398                     if (!hasvictim) {
2399                         emit_sound_at(knifedrawsound, coords, 128);
2400                     }
2401
2402                     if (victim && hasvictim) {
2403                         XYZ footvel, footpoint;
2404
2405                         emit_sound_at(fleshstabremovesound, coords, 128.);
2406
2407                         footvel = 0;
2408                         footpoint = (weapons[weaponids[weaponactive]].tippoint * .8 + weapons[weaponids[weaponactive]].position * .2);
2409
2410                         if (weapons[weaponids[weaponactive]].getType() == sword) {
2411                             XYZ where, startpoint, endpoint, movepoint;
2412                             float rotationpoint;
2413                             int whichtri;
2414
2415                             where = weapons[weaponids[weaponactive]].position;
2416                             where -= victim->coords;
2417                             if (!victim->skeleton.free)
2418                                 where = DoRotation(where, 0, -victim->yaw, 0);
2419                             startpoint = where;
2420                             where = weapons[weaponids[weaponactive]].tippoint;
2421                             where -= victim->coords;
2422                             if (!victim->skeleton.free)
2423                                 where = DoRotation(where, 0, -victim->yaw, 0);
2424                             endpoint = where;
2425
2426                             movepoint = 0;
2427                             rotationpoint = 0;
2428                             whichtri = victim->skeleton.drawmodel.LineCheck(&startpoint, &endpoint, &footpoint, &movepoint, &rotationpoint);
2429                             footpoint += victim->coords;
2430
2431                             if (whichtri == -1) {
2432                                 footpoint = (weapons[weaponids[weaponactive]].tippoint * .8 + weapons[weaponids[weaponactive]].position * .2);
2433                             }
2434                         }
2435                         if (weapons[weaponids[weaponactive]].getType() == staff) {
2436                             XYZ where, startpoint, endpoint, movepoint;
2437                             float rotationpoint;
2438                             int whichtri;
2439
2440                             where = weapons[weaponids[weaponactive]].position;
2441                             where -= victim->coords;
2442                             if (!victim->skeleton.free)
2443                                 where = DoRotation(where, 0, -victim->yaw, 0);
2444                             startpoint = where;
2445                             where = weapons[weaponids[weaponactive]].tippoint;
2446                             where -= victim->coords;
2447                             if (!victim->skeleton.free)
2448                                 where = DoRotation(where, 0, -victim->yaw, 0);
2449                             endpoint = where;
2450
2451                             movepoint = 0;
2452                             rotationpoint = 0;
2453                             whichtri = victim->skeleton.drawmodel.LineCheck(&startpoint, &endpoint, &footpoint, &movepoint, &rotationpoint);
2454                             footpoint += victim->coords;
2455
2456                             if (whichtri == -1) {
2457                                 footpoint = (weapons[weaponids[weaponactive]].tippoint * .8 + weapons[weaponids[weaponactive]].position * .2);
2458                             }
2459                         }
2460                         hasvictim = victim->DoBloodBigWhere(2, 220, footpoint);
2461                         if (hasvictim) {
2462                             if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 3) {
2463                                 victim->skeleton.longdead = 0;
2464                                 victim->skeleton.free = 1;
2465                                 victim->skeleton.broken = 0;
2466
2467                                 for (int i = 0; i < victim->skeleton.num_joints; i++) {
2468                                     victim->skeleton.joints[i].velchange = 0;
2469                                     victim->skeleton.joints[i].locked = 0;
2470                                     //victim->skeleton.joints[i].velocity=0;
2471                                 }
2472
2473                                 XYZ relative;
2474                                 relative = 0;
2475                                 relative.y = 10;
2476                                 Normalise(&relative);
2477                                 //victim->Puff(abdomen);
2478                                 if (bloodtoggle)
2479                                     Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 0, 0, .8, .3);
2480
2481                                 if (victim->bloodloss < victim->damagetolerance) {
2482                                     victim->bloodloss += 1000;
2483                                     victim->bled = 0;
2484                                 }
2485
2486                                 victim->jointVel(abdomen) += relative * damagemult * 20;
2487                             }
2488                         }
2489                     }
2490                     if (!hasvictim && onterrain) {
2491                         weapons[weaponids[weaponactive]].bloody = 0;
2492                         weapons[weaponids[weaponactive]].blooddrip = 0;
2493                     }
2494                 }
2495
2496                 if (animTarget == upunchanim && animation[animTarget].label[frameCurrent] == 5) {
2497                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 3) {
2498                         escapednum = 0;
2499                         if (id == 0)
2500                             camerashake += .4;
2501                         if (Random() % 2) {
2502                             victim->spurt = 1;
2503                             DoBlood(.2, 235);
2504                         }
2505                         if (tutoriallevel != 1) {
2506                             emit_sound_at(heavyimpactsound, victim->coords, 128);
2507                         }
2508
2509                         victim->RagDoll(0);
2510                         XYZ relative;
2511                         relative = victim->coords - coords;
2512                         relative.y = 0;
2513                         Normalise(&relative);
2514                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2515                             victim->skeleton.joints[i].velocity = relative * 30;
2516                         }
2517                         victim->jointVel(head) += relative * damagemult * 150;
2518
2519                         victim->frameTarget = 0;
2520                         victim->animTarget = staggerbackhardanim;
2521                         victim->targetyaw = targetyaw + 180;
2522                         victim->target = 0;
2523                         victim->stunned = 1;
2524
2525                         victim->Puff(head);
2526                         victim->Puff(abdomen);
2527                         victim->DoDamage(damagemult * 60 / victim->protectionhigh);
2528
2529                         SolidHitBonus(id);
2530                     }
2531                 }
2532
2533
2534                 if (animTarget == winduppunchanim && animation[animTarget].label[frameCurrent] == 5) {
2535                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 2) {
2536                         escapednum = 0;
2537                         if (id == 0)
2538                             camerashake += .4;
2539                         if (victim->damage <= victim->damagetolerance - 60 && normaldotproduct(victim->facing, victim->coords - coords) < (scale * 5) * (scale * 5) * 0 && animation[victim->animTarget].height != lowheight) {
2540                             if (tutoriallevel != 1) {
2541                                 emit_sound_at(thudsound, victim->coords);
2542                             }
2543                         } else if (victim->damage <= victim->damagetolerance - 60 && normaldotproduct(victim->facing, victim->coords - coords) < (scale * 5) * (scale * 5) * 0 && animation[victim->animTarget].height == lowheight) {
2544                             if (tutoriallevel != 1) {
2545                                 emit_sound_at(whooshhitsound, victim->coords);
2546                             }
2547                         } else {
2548                             if (tutoriallevel != 1) {
2549                                 emit_sound_at(heavyimpactsound, victim->coords);
2550                             }
2551                         }
2552
2553                         if (victim->damage > victim->damagetolerance - 60 || normaldotproduct(victim->facing, victim->coords - coords) > 0 || animation[victim->animTarget].height == lowheight)
2554                             victim->RagDoll(0);
2555                         XYZ relative;
2556                         relative = victim->coords - coords;
2557                         relative.y = 0;
2558                         Normalise(&relative);
2559                         relative.y = .3;
2560                         Normalise(&relative);
2561                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2562                             victim->skeleton.joints[i].velocity = relative * 5;
2563                         }
2564                         victim->jointVel(abdomen) += relative * damagemult * 400;
2565
2566                         victim->frameTarget = 0;
2567                         victim->animTarget = staggerbackhardanim;
2568                         victim->targetyaw = targetyaw + 180;
2569                         victim->target = 0;
2570                         victim->stunned = 1;
2571
2572                         victim->Puff(abdomen);
2573                         victim->DoDamage(damagemult * 60 / victim->protectionhigh);
2574
2575                         SolidHitBonus(id);
2576                     }
2577                 }
2578
2579                 if (animTarget == blockhighleftanim && animation[animTarget].label[frameCurrent] == 5) {
2580                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 4) {
2581                         if (victim->id == 0)
2582                             camerashake += .4;
2583                         emit_sound_at(landsound2, victim->coords);
2584
2585                         Puff(righthand);
2586                     }
2587                 }
2588
2589                 if (animTarget == swordslashparryanim && animation[animTarget].label[frameCurrent] == 5) {
2590                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 4) {
2591                         if (victim->id == 0)
2592                             camerashake += .4;
2593
2594                         if (weaponactive != -1) {
2595                             if (weapons[victim->weaponids[0]].getType() == staff || weapons[weaponids[0]].getType() == staff) {
2596                                 if (weapons[victim->weaponids[0]].getType() == staff)
2597                                     weapons[victim->weaponids[0]].damage += .2 + float(abs(Random() % 100) - 50) / 250;
2598                                 if (weapons[weaponids[0]].getType() == staff)
2599                                     weapons[weaponids[0]].damage += .2 + float(abs(Random() % 100) - 50) / 250;
2600
2601                                 emit_sound_at(swordstaffsound, victim->coords);
2602                             } else {
2603                                 emit_sound_at(metalhitsound, victim->coords);
2604                             }
2605                         }
2606
2607                         //Puff(righthand);
2608                     }
2609                 }
2610
2611                 if (animTarget == knifethrowanim && animation[animTarget].label[frameCurrent] == 5) {
2612                     if (weaponactive != -1) {
2613                         escapednum = 0;
2614                         XYZ aim;
2615                         aim = victim->coords + DoRotation(victim->jointPos(abdomen), 0, victim->yaw, 0) * victim->scale + victim->velocity * findDistance(&victim->coords, &coords) / 50 - (coords + DoRotation(jointPos(righthand), 0, yaw, 0) * scale);
2616                         Normalise(&aim);
2617                         weapons[weaponids[0]].thrown(aim * 50);
2618                         num_weapons--;
2619                         if (num_weapons) {
2620                             weaponids[0] = weaponids[num_weapons];
2621                         }
2622                         weaponactive = -1;
2623                     }
2624                 }
2625
2626                 if (animTarget == knifeslashstartanim && animation[animTarget].label[frameCurrent] == 5) {
2627                     if (hasvictim)
2628                         if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 4.5 &&/*animation[victim->animTarget].height!=lowheight&&*/victim->animTarget != dodgebackanim && victim->animTarget != rollanim) {
2629                             escapednum = 0;
2630                             if (tutoriallevel != 1)
2631                                 victim->DoBloodBig(1.5 / victim->armorhigh, 225);
2632
2633                             award_bonus(id, Slicebonus);
2634                             if (tutoriallevel != 1) {
2635                                 emit_sound_at(knifeslicesound, victim->coords);
2636                             }
2637                             //victim->jointVel(abdomen)+=relative*damagemult*200;
2638                             if (animation[victim->animTarget].attack && (victim->aitype != playercontrolled || victim->animTarget == knifeslashstartanim) && (victim->creature == rabbittype || victim->deathbleeding <= 0)) {
2639                                 if (victim->id != 0 || difficulty == 2) {
2640                                     victim->frameTarget = 0;
2641                                     victim->animTarget = staggerbackhardanim;
2642                                     victim->targetyaw = targetyaw + 180;
2643                                     victim->target = 0;
2644                                 }
2645                             }
2646                             victim->lowreversaldelay = 0;
2647                             victim->highreversaldelay = 0;
2648                             if (aitype != playercontrolled)
2649                                 weaponmissdelay = .6;
2650
2651                             if (tutoriallevel != 1)
2652                                 if (bloodtoggle && !weapons[weaponids[weaponactive]].bloody)
2653                                     weapons[weaponids[weaponactive]].bloody = 1;
2654                             if (tutoriallevel != 1)
2655                                 weapons[weaponids[weaponactive]].blooddrip += 3;
2656
2657                             XYZ footvel, footpoint;
2658                             footvel = 0;
2659                             if (skeleton.free) {
2660                                 footpoint = (victim->jointPos(abdomen) + victim->jointPos(neck)) / 2 * victim->scale + victim->coords;
2661                             }
2662                             if (!skeleton.free) {
2663                                 footpoint = DoRotation((victim->jointPos(abdomen) + victim->jointPos(neck)) / 2, 0, victim->yaw, 0) * victim->scale + victim->coords;
2664                             }
2665                             if (tutoriallevel != 1) {
2666                                 if (bloodtoggle)
2667                                     Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 0, 0, .6, .3);
2668                                 footvel = DoRotation(facing, 0, 90, 0) * .8;
2669                                 //footvel.y-=.3;
2670                                 Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 7, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
2671                                 Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 3, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
2672                                 Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * 5, 1, 1, 1, .2, 1);
2673                                 Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * 2, 1, 1, 1, .2, 1);
2674                             }
2675                             if (tutoriallevel == 1) {
2676                                 Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 1, 1, .6, .3);
2677                             }
2678                             victim->DoDamage(damagemult * 0);
2679                         }
2680                 }
2681                 if (animTarget == swordslashanim && animation[animTarget].label[frameCurrent] == 5 && victim->animTarget != rollanim) {
2682                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 6.5 && victim->animTarget != dodgebackanim) {
2683                         if (victim->weaponactive == -1 || normaldotproduct(victim->facing, victim->coords - coords) > 0 || (Random() % 2 == 0)) {
2684                             award_bonus(id, Slashbonus);
2685                             escapednum = 0;
2686                             if (tutoriallevel != 1) {
2687                                 if (normaldotproduct(victim->facing, victim->coords - coords) < 0)
2688                                     victim->DoBloodBig(2 / victim->armorhigh, 190);
2689                                 else
2690                                     victim->DoBloodBig(2 / victim->armorhigh, 185);
2691                                 victim->deathbleeding = 1;
2692                                 emit_sound_at(swordslicesound, victim->coords);
2693                             }
2694                             //victim->jointVel(abdomen)+=relative*damagemult*200;
2695                             if (tutoriallevel != 1) {
2696                                 victim->frameTarget = 0;
2697                                 victim->animTarget = staggerbackhardanim;
2698                                 victim->targetyaw = targetyaw + 180;
2699                                 victim->target = 0;
2700                             }
2701
2702                             if (tutoriallevel != 1) {
2703                                 if (bloodtoggle && !weapons[weaponids[weaponactive]].bloody)
2704                                     weapons[weaponids[weaponactive]].bloody = 1;
2705                                 weapons[weaponids[weaponactive]].blooddrip += 3;
2706
2707                                 float bloodlossamount;
2708                                 bloodlossamount = 200 + abs((float)(Random() % 40)) - 20;
2709                                 victim->bloodloss += bloodlossamount / victim->armorhigh;
2710                                 //victim->bloodloss+=100*(6.5-distsq(&coords,&victim->coords));
2711                                 victim->DoDamage(damagemult * 0);
2712
2713                                 XYZ footvel, footpoint;
2714                                 footvel = 0;
2715                                 if (skeleton.free) {
2716                                     footpoint = (victim->jointPos(abdomen) + victim->jointPos(neck)) / 2 * victim->scale + victim->coords;
2717                                 }
2718                                 if (!skeleton.free) {
2719                                     footpoint = DoRotation((victim->jointPos(abdomen) + victim->jointPos(neck)) / 2, 0, victim->yaw, 0) * victim->scale + victim->coords;
2720                                 }
2721                                 if (bloodtoggle)
2722                                     Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 0, 0, .9, .3);
2723                                 footvel = DoRotation(facing, 0, 90, 0) * .8;
2724                                 footvel.y -= .3;
2725                                 Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 7, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
2726                                 Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 3, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
2727                                 Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * 5, 1, 1, 1, .3, 1);
2728                                 Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * 2, 1, 1, 1, .3, 1);
2729                             }
2730                         } else {
2731                             if (victim->weaponactive != -1) {
2732                                 if (weapons[victim->weaponids[0]].getType() == staff || weapons[weaponids[0]].getType() == staff) {
2733                                     if (weapons[victim->weaponids[0]].getType() == staff)
2734                                         weapons[victim->weaponids[0]].damage += .2 + float(abs(Random() % 100) - 50) / 250;
2735                                     if (weapons[weaponids[0]].getType() == staff)
2736                                         weapons[weaponids[0]].damage += .2 + float(abs(Random() % 100) - 50) / 250;
2737
2738                                     emit_sound_at(swordstaffsound, victim->coords);
2739                                 } else {
2740                                     emit_sound_at(metalhitsound, victim->coords);
2741                                 }
2742                             }
2743
2744
2745                             XYZ aim;
2746                             victim->Puff(righthand);
2747                             victim->target = 0;
2748                             victim->frameTarget = 0;
2749                             victim->animTarget = staggerbackhighanim;
2750                             victim->targetyaw = targetyaw + 180;
2751                             victim->target = 0;
2752                             aim = DoRotation(facing, 0, 90, 0) * 21;
2753                             aim.y += 7;
2754                             weapons[victim->weaponids[0]].drop(aim * -.2, aim);
2755                             victim->num_weapons--;
2756                             if (victim->num_weapons) {
2757                                 victim->weaponids[0] = victim->weaponids[num_weapons];
2758                                 if (victim->weaponstuck == victim->num_weapons)
2759                                     victim->weaponstuck = 0;
2760                             }
2761                             victim->weaponactive = -1;
2762                             for (unsigned i = 0; i < Person::players.size(); i++) {
2763                                 Person::players[i]->wentforweapon = 0;
2764                             }
2765
2766                         }
2767                     }
2768                 }
2769
2770                 if (animTarget == staffhitanim && animation[animTarget].label[frameCurrent] == 5 && victim->animTarget != rollanim) {
2771                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 6.5 && victim->animTarget != dodgebackanim && victim->animTarget != sweepanim) {
2772                         if (tutoriallevel != 1) {
2773                             weapons[weaponids[0]].damage += .4 + float(abs(Random() % 100) - 50) / 250;
2774                             escapednum = 0;
2775                             if (id == 0)
2776                                 camerashake += .4;
2777                             if (Random() % 2 || creature == wolftype) {
2778                                 victim->spurt = 1;
2779                             }
2780                             emit_sound_at(staffheadsound, victim->coords);
2781                         }
2782                         victim->RagDoll(0);
2783                         XYZ relative;
2784                         relative = victim->coords - coords;
2785                         relative.y = 0;
2786                         Normalise(&relative);
2787                         relative = DoRotation(relative, 0, 90, 0);
2788                         relative.y -= 1;
2789                         Normalise(&relative);
2790                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2791                             victim->skeleton.joints[i].velocity += relative * damagemult * 60;
2792                         }
2793                         victim->jointVel(head) += relative * damagemult * 230;
2794                         victim->jointVel(neck) += relative * damagemult * 230;
2795                         //FootLand(1,2);
2796                         victim->Puff(head);
2797                         if (tutoriallevel != 1) {
2798                             victim->DoDamage(damagemult * 120 / victim->protectionhigh);
2799
2800                             award_bonus(id, solidhit, 30);
2801                         }
2802                     }
2803                 }
2804
2805                 if (animTarget == staffspinhitanim && animation[animTarget].label[frameCurrent] == 5 && victim->animTarget != rollanim) {
2806                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 6.5 && victim->animTarget != dodgebackanim && victim->animTarget != sweepanim) {
2807                         if (tutoriallevel != 1) {
2808                             weapons[weaponids[0]].damage += .6 + float(abs(Random() % 100) - 50) / 250;
2809                             escapednum = 0;
2810                             if (id == 0)
2811                                 camerashake += .4;
2812                             if (Random() % 2 || creature == wolftype) {
2813                                 victim->spurt = 1;
2814                             }
2815                             emit_sound_at(staffheadsound, victim->coords);
2816                         }
2817                         victim->RagDoll(0);
2818                         XYZ relative;
2819                         relative = victim->coords - coords;
2820                         relative.y = 0;
2821                         Normalise(&relative);
2822                         relative = DoRotation(relative, 0, -90, 0);
2823                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2824                             victim->skeleton.joints[i].velocity += relative * damagemult * 40;
2825                         }
2826                         victim->jointVel(head) += relative * damagemult * 220;
2827                         victim->jointVel(neck) += relative * damagemult * 220;
2828                         //FootLand(1,2);
2829                         victim->Puff(head);
2830                         if (tutoriallevel != 1) {
2831                             victim->DoDamage(damagemult * 350 / victim->protectionhead);
2832
2833                             award_bonus(id, solidhit, 60);
2834                         }
2835                     }
2836                 }
2837
2838                 if (animTarget == staffgroundsmashanim && animation[animTarget].label[frameCurrent] == 5) {
2839                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 6.5) {
2840                         escapednum = 0;
2841                         if (tutoriallevel != 1) {
2842                             if (!victim->dead)
2843                                 weapons[weaponids[0]].damage += .4 + float(abs(Random() % 100) - 50) / 500;
2844                             if (id == 0)
2845                                 camerashake += .4;
2846                             if (Random() % 2 || creature == wolftype) {
2847                                 victim->spurt = 1;
2848                             }
2849                             emit_sound_at(staffbodysound, victim->coords);
2850                         }
2851                         victim->skeleton.longdead = 0;
2852                         victim->skeleton.free = 1;
2853                         victim->skeleton.broken = 0;
2854
2855                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
2856                             victim->skeleton.joints[i].velchange = 0;
2857                             victim->skeleton.joints[i].locked = 0;
2858                             //victim->skeleton.joints[i].velocity=0;
2859                         }
2860
2861                         victim->RagDoll(0);
2862                         XYZ relative;
2863                         relative = 0;
2864                         /*relative=victim->coords-coords;
2865                         relative.y=0;
2866                         Normalise(&relative);
2867                         relative=DoRotation(relative,0,90,0);*/
2868                         relative.y = -1;
2869                         Normalise(&relative);
2870                         if (!victim->dead) {
2871                             for (int i = 0; i < victim->skeleton.num_joints; i++) {
2872                                 victim->skeleton.joints[i].velocity = relative * damagemult * 40;
2873                             }
2874                             //FootLand(1,2);
2875                             victim->jointVel(abdomen) += relative * damagemult * 40;
2876                         }
2877                         if (victim->dead) {
2878                             for (int i = 0; i < victim->skeleton.num_joints; i++) {
2879                                 victim->skeleton.joints[i].velocity = relative * damagemult * abs(Random() % 20);
2880                             }
2881                             //FootLand(1,2);
2882                             //victim->jointVel(abdomen)+=relative*damagemult*20;
2883                         }
2884                         victim->Puff(abdomen);
2885                         if (tutoriallevel != 1) {
2886                             victim->DoDamage(damagemult * 100 / victim->protectionhigh);
2887
2888                             if (!victim->dead) {
2889                                 award_bonus(id, solidhit, 40);
2890                             }
2891                         }
2892                     }
2893                 }
2894
2895                 if (animTarget == lowkickanim && animation[animTarget].label[frameCurrent] == 5) {
2896                     if (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 3 && animation[victim->animTarget].height != highheight) {
2897                         escapednum = 0;
2898                         if (id == 0)
2899                             camerashake += .4;
2900                         XYZ relative;
2901                         relative = victim->coords - coords;
2902                         relative.y = 0;
2903                         Normalise(&relative);
2904
2905                         SolidHitBonus(id);
2906
2907                         if (animation[victim->animTarget].height == lowheight) {
2908                             if (Random() % 2) {
2909                                 victim->spurt = 1;
2910                                 DoBlood(.2, 250);
2911                             }
2912                             victim->RagDoll(0);
2913                             for (int i = 0; i < victim->skeleton.num_joints; i++) {
2914                                 victim->skeleton.joints[i].velocity += relative * damagemult * 40;
2915                             }
2916                             victim->jointVel(head) += relative * damagemult * 200;
2917                             if (tutoriallevel != 1) {
2918                                 emit_sound_at(heavyimpactsound, victim->coords, 128.);
2919                             }
2920                             victim->Puff(head);
2921                             victim->DoDamage(damagemult * 100 / victim->protectionhead);
2922                             if (victim->howactive == typesleeping)
2923                                 victim->DoDamage(damagemult * 150 / victim->protectionhead);
2924                             if (creature == wolftype) {
2925                                 emit_sound_at(clawslicesound, victim->coords, 128.);
2926                                 victim->spurt = 1;
2927                                 victim->DoBloodBig(2 / victim->armorhead, 175);
2928                             }
2929                         } else {
2930                             if (victim->damage >= victim->damagetolerance)
2931                                 victim->RagDoll(0);
2932                             for (int i = 0; i < victim->skeleton.num_joints; i++) {
2933                                 victim->skeleton.joints[i].velocity += relative * damagemult * 10;
2934                             }
2935                             victim->jointVel(abdomen) += relative * damagemult * 200;
2936                             victim->frameTarget = 0;
2937                             victim->animTarget = staggerbackhighanim;
2938                             victim->targetyaw = targetyaw + 180;
2939                             victim->target = 0;
2940                             if (tutoriallevel != 1) {
2941                                 emit_sound_at(landsound2, victim->coords, 128.);
2942                             }
2943                             victim->Puff(abdomen);
2944                             victim->DoDamage(damagemult * 30 / victim->protectionhigh);
2945                             if (creature == wolftype) {
2946                                 emit_sound_at(clawslicesound, victim->coords, 128.);
2947                                 victim->spurt = 1;
2948                                 victim->DoBloodBig(2 / victim->armorhigh, 170);
2949                             }
2950                         }
2951
2952                     }
2953                 }
2954
2955                 if (animTarget == sweepanim && animation[animTarget].label[frameCurrent] == 5) {
2956                     if ((victim->animTarget != jumpupanim) &&
2957                         (distsq(&coords, &victim->coords) < (scale * 5) * (scale * 5) * 3) &&
2958                         (victim != this->shared_from_this())) {
2959                         escapednum = 0;
2960                         if (id == 0)
2961                             camerashake += .2;
2962                         if (tutoriallevel != 1) {
2963                             emit_sound_at(landsound2, victim->coords, 128.);
2964                         }
2965                         XYZ relative;
2966                         relative = victim->coords - coords;
2967                         relative.y = 0;
2968                         Normalise(&relative);
2969
2970                         if (animation[victim->animTarget].height == middleheight || animation[victim->animCurrent].height == middleheight || victim->damage >= victim->damagetolerance - 40) {
2971                             victim->RagDoll(0);
2972
2973                             for (int i = 0; i < victim->skeleton.num_joints; i++) {
2974                                 victim->skeleton.joints[i].velocity += relative * damagemult * 15;
2975                             }
2976                             relative = DoRotation(relative, 0, -90, 0);
2977                             relative.y += .1;
2978                             for (int i = 0; i < victim->skeleton.num_joints; i++) {
2979                                 if (victim->skeleton.joints[i].label == leftfoot || victim->skeleton.joints[i].label == rightfoot || victim->skeleton.joints[i].label == leftankle || victim->skeleton.joints[i].label == rightankle)
2980                                     victim->skeleton.joints[i].velocity = relative * 80;
2981                             }
2982                             victim->Puff(rightankle);
2983                             victim->Puff(leftankle);
2984                             victim->DoDamage(damagemult * 40 / victim->protectionlow);
2985                         } else {
2986                             if (victim->damage >= victim->damagetolerance)
2987                                 victim->RagDoll(0);
2988                             for (int i = 0; i < victim->skeleton.num_joints; i++) {
2989                                 victim->skeleton.joints[i].velocity += relative * damagemult * 10;
2990                             }
2991                             relative = DoRotation(relative, 0, -90, 0);
2992                             for (int i = 0; i < victim->skeleton.num_joints; i++) {
2993                                 if (victim->skeleton.joints[i].label == leftfoot || victim->skeleton.joints[i].label == rightfoot || victim->skeleton.joints[i].label == leftankle || victim->skeleton.joints[i].label == rightankle)
2994                                     victim->skeleton.joints[i].velocity += relative * damagemult * 80;
2995                             }
2996                             victim->jointVel(abdomen) += relative * damagemult * 200;
2997                             victim->frameTarget = 0;
2998                             victim->animTarget = staggerbackhighanim;
2999                             victim->targetyaw = targetyaw + 180;
3000                             victim->target = 0;
3001                             if (tutoriallevel != 1) {
3002                                 emit_sound_at(landsound2, victim->coords, 128.);
3003                             }
3004                             victim->Puff(abdomen);
3005                             victim->DoDamage(damagemult * 30 / victim->protectionlow);
3006                         }
3007
3008                         SolidHitBonus(id);
3009
3010                     }
3011                 }
3012             }
3013             if (animation[animTarget].attack == reversal && (!victim->feint || (victim->lastattack == victim->lastattack2 && victim->lastattack2 == victim->lastattack3 && Random() % 2) || animTarget == knifefollowanim)) {
3014                 if (animTarget == spinkickreversalanim && animation[animTarget].label[frameCurrent] == 7) {
3015                     escapednum = 0;
3016                     if (id == 0)
3017                         camerashake += .4;
3018                     if (Random() % 2) {
3019                         victim->spurt = 1;
3020                         DoBlood(.2, 230);
3021                     }
3022                     if (tutoriallevel != 1) {
3023                         emit_sound_at(heavyimpactsound, victim->coords, 128.);
3024                     }
3025                     if (creature == wolftype) {
3026                         emit_sound_at(clawslicesound, victim->coords, 128);
3027                         victim->spurt = 1;
3028                         victim->DoBloodBig(2 / victim->armorhigh, 170);
3029                     }
3030                     victim->RagDoll(0);
3031                     XYZ relative;
3032                     relative = victim->coords - oldcoords;
3033                     relative.y = 0;
3034                     Normalise(&relative);
3035                     //relative=DoRotation(relative,0,-90,0);
3036                     for (int i = 0; i < victim->skeleton.num_joints; i++) {
3037                         victim->skeleton.joints[i].velocity += relative * damagemult * 40;
3038                     }
3039                     victim->jointVel(abdomen) += relative * damagemult * 200;
3040                     //FootLand(1,2);
3041                     victim->Puff(abdomen);
3042                     victim->DoDamage(damagemult * 150 / victim->protectionhigh);
3043
3044                     award_bonus(id, Reversal);
3045                 }
3046
3047                 if ((animTarget == swordslashreversalanim || animTarget == knifeslashreversalanim || animTarget == staffhitreversalanim || animTarget == staffspinhitreversalanim) && animation[animTarget].label[frameCurrent] == 5) {
3048                     if (victim->weaponactive != -1 && victim->num_weapons > 0) {
3049                         if (weapons[victim->weaponids[victim->weaponactive]].owner == int(victim->id)) {
3050                             takeWeapon(victim->weaponids[victim->weaponactive]);
3051                             victim->num_weapons--;
3052                             if (victim->num_weapons > 0) {
3053                                 victim->weaponids[victim->weaponactive] = victim->weaponids[victim->num_weapons];
3054                             }
3055                             victim->weaponactive = -1;
3056                         }
3057                     }
3058                 }
3059
3060                 if (animTarget == staffhitreversalanim && animation[animTarget].label[frameCurrent] == 5) {
3061                     escapednum = 0;
3062                     if (id == 0)
3063                         camerashake += .4;
3064                     if (Random() % 2) {
3065                         victim->spurt = 1;
3066                         DoBlood(.2, 230);
3067                     }
3068                     emit_sound_at(whooshhitsound, victim->coords, 128.);
3069                     victim->RagDoll(0);
3070                     XYZ relative;
3071                     relative = victim->coords - oldcoords;
3072                     relative.y = 0;
3073                     Normalise(&relative);
3074                     for (int i = 0; i < victim->skeleton.num_joints; i++) {
3075                         victim->skeleton.joints[i].velocity += relative * damagemult * 30;
3076                     }
3077                     victim->jointVel(abdomen) += relative * damagemult * 200;
3078                     victim->Puff(head);
3079                     victim->DoDamage(damagemult * 70 / victim->protectionhigh);
3080                 }
3081
3082                 if (animTarget == staffspinhitreversalanim && animation[animTarget].label[frameCurrent] == 7) {
3083                     escapednum = 0;
3084                     if (id == 0)
3085                         camerashake += .4;
3086                     if (Random() % 2) {
3087                         victim->spurt = 1;
3088                         DoBlood(.2, 230);
3089                     }
3090
3091                     award_bonus(id, staffreversebonus);
3092
3093                     if (tutoriallevel != 1) {
3094                         emit_sound_at(heavyimpactsound, victim->coords, 128.);
3095                     }
3096                     victim->RagDoll(0);
3097                     award_bonus(id, staffreversebonus); // Huh, again?
3098
3099                     XYZ relative;
3100                     relative = victim->coords - oldcoords;
3101                     relative.y = 0;
3102                     Normalise(&relative);
3103                     for (int i = 0; i < victim->skeleton.num_joints; i++) {
3104                         victim->skeleton.joints[i].velocity += relative * damagemult * 30;
3105                     }
3106                     victim->jointVel(abdomen) += relative * damagemult * 200;
3107                     victim->Puff(head);
3108                     victim->DoDamage(damagemult * 70 / victim->protectionhigh);
3109                 }
3110
3111                 if (animTarget == upunchreversalanim && animation[animTarget].label[frameCurrent] == 7) {
3112                     escapednum = 0;
3113                     victim->RagDoll(1);
3114                     XYZ relative;
3115                     relative = facing;
3116                     relative.y = 0;
3117                     Normalise(&relative);
3118                     relative.y -= .1;
3119                     for (int i = 0; i < victim->skeleton.num_joints; i++) {
3120                         victim->skeleton.joints[i].velocity += relative * damagemult * 70;
3121                     }
3122                     victim->jointVel(lefthand) *= .1;
3123                     victim->jointVel(leftwrist) *= .2;
3124                     victim->jointVel(leftelbow) *= .5;
3125                     victim->jointVel(leftshoulder) *= .7;
3126                     victim->jointVel(righthand) *= .1;
3127                     victim->jointVel(rightwrist) *= .2;
3128                     victim->jointVel(rightelbow) *= .5;
3129                     victim->jointVel(rightshoulder) *= .7;
3130
3131                     victim->Puff(abdomen);
3132                     victim->DoDamage(damagemult * 90 / victim->protectionhigh);
3133
3134                     award_bonus(id, Reversal);
3135
3136                     bool doslice;
3137                     doslice = 0;
3138                     if (weaponactive != -1 || creature == wolftype)
3139                         doslice = 1;
3140                     if (creature == rabbittype && weaponactive != -1)
3141                         if (weapons[weaponids[0]].getType() == staff)
3142                             doslice = 0;
3143                     if (doslice) {
3144                         if (weaponactive != -1) {
3145                             victim->DoBloodBig(2 / victim->armorhigh, 225);
3146                             emit_sound_at(knifeslicesound, victim->coords);
3147                             if (bloodtoggle && !weapons[weaponids[weaponactive]].bloody)
3148                                 weapons[weaponids[weaponactive]].bloody = 1;
3149                             weapons[weaponids[weaponactive]].blooddrip += 3;
3150                         }
3151                         if (weaponactive == -1 && creature == wolftype) {
3152                             ;
3153                             emit_sound_at(clawslicesound, victim->coords, 128.);
3154                             victim->spurt = 1;
3155                             victim->DoBloodBig(2 / victim->armorhigh, 175);
3156                         }
3157                     }
3158                 }
3159
3160
3161
3162                 if (animTarget == swordslashreversalanim && animation[animTarget].label[frameCurrent] == 7) {
3163                     escapednum = 0;
3164                     victim->RagDoll(1);
3165                     XYZ relative;
3166                     relative = facing;
3167                     relative.y = 0;
3168                     Normalise(&relative);
3169                     relative.y -= .1;
3170                     for (int i = 0; i < victim->skeleton.num_joints; i++) {
3171                         victim->skeleton.joints[i].velocity += relative * damagemult * 70;
3172                     }
3173                     victim->jointVel(lefthand) *= .1 - 1;
3174                     victim->jointVel(leftwrist) *= .2 - 1;
3175                     victim->jointVel(leftelbow) *= .5 - 1;
3176                     victim->jointVel(leftshoulder) *= .7 - 1;
3177                     victim->jointVel(righthand) *= .1 - 1;
3178                     victim->jointVel(rightwrist) *= .2 - 1;
3179                     victim->jointVel(rightelbow) *= .5 - 1;
3180                     victim->jointVel(rightshoulder) *= .7 - 1;
3181
3182                     award_bonus(id, swordreversebonus);
3183                 }
3184
3185                 if (hasvictim && animTarget == knifeslashreversalanim && animation[animTarget].label[frameCurrent] == 7) {
3186                     escapednum = 0;
3187                     if (id == 0)
3188                         camerashake += .4;
3189                     if (Random() % 2) {
3190                         victim->spurt = 1;
3191                         DoBlood(.2, 230);
3192                     }
3193                     if (tutoriallevel != 1) {
3194                         emit_sound_at(heavyimpactsound, victim->coords, 128.);
3195                     }
3196                     victim->RagDoll(0);
3197                     XYZ relative;
3198                     relative = victim->coords - oldcoords;
3199                     relative.y = 0;
3200                     Normalise(&relative);
3201                     relative = DoRotation(relative, 0, -90, 0);
3202                     for (int i = 0; i < victim->skeleton.num_joints; i++) {
3203                         victim->skeleton.joints[i].velocity += relative * damagemult * 40;
3204                     }
3205                     victim->jointVel(abdomen) += relative * damagemult * 200;
3206                     victim->Puff(abdomen);
3207                     victim->DoDamage(damagemult * 30 / victim->protectionhigh);
3208
3209                     award_bonus(id, Reversal);
3210                 }
3211
3212                 if (hasvictim && animTarget == sneakattackanim && animation[animTarget].label[frameCurrent] == 7) {
3213                     escapednum = 0;
3214                     victim->RagDoll(0);
3215                     victim->skeleton.spinny = 0;
3216                     XYZ relative;
3217                     relative = facing * -1;
3218                     relative.y = -3;
3219                     Normalise(&relative);
3220                     if (victim->id == 0)
3221                         relative /= 30;
3222                     for (int i = 0; i < victim->skeleton.num_joints; i++) {
3223                         victim->skeleton.joints[i].velocity += relative * damagemult * 40;
3224                     }
3225                     victim->damage = victim->damagetolerance;
3226                     victim->permanentdamage = victim->damagetolerance - 1;
3227                     bool doslice;
3228                     doslice = 0;
3229                     if (weaponactive != -1 || creature == wolftype)
3230                         doslice = 1;
3231                     if (creature == rabbittype && weaponactive != -1)
3232                         if (weapons[weaponids[0]].getType() == staff)
3233                             doslice = 0;
3234                     if (doslice) {
3235                         if (weaponactive != -1) {
3236                             victim->DoBloodBig(200, 225);
3237                             emit_sound_at(knifeslicesound, victim->coords);
3238                             if (bloodtoggle)
3239                                 weapons[weaponids[weaponactive]].bloody = 2;
3240                             weapons[weaponids[weaponactive]].blooddrip += 5;
3241                         }
3242
3243                         if (creature == wolftype && weaponactive == -1) {
3244                             emit_sound_at(clawslicesound, victim->coords, 128.);
3245                             victim->spurt = 1;
3246                             victim->DoBloodBig(2, 175);
3247                         }
3248                     }
3249                     award_bonus(id, spinecrusher);
3250                 }
3251
3252                 if (hasvictim && (animTarget == knifefollowanim || animTarget == knifesneakattackanim) && animation[animTarget].label[frameCurrent] == 5) {
3253                     if (weaponactive != -1 && victim->bloodloss < victim->damagetolerance) {
3254                         escapednum = 0;
3255                         if (animTarget == knifefollowanim)
3256                             victim->DoBloodBig(200, 210);
3257                         if (animTarget == knifesneakattackanim) {
3258                             XYZ footvel, footpoint;
3259                             footvel = 0;
3260                             footpoint = weapons[weaponids[0]].tippoint;
3261                             if (bloodtoggle)
3262                                 Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 0, 0, .9, .3);
3263                             footvel = (weapons[weaponids[0]].tippoint - weapons[weaponids[0]].position);
3264                             Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 7, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
3265                             Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 3, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
3266                             Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * 5, 1, 1, 1, .3, 1);
3267                             Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * 2, 1, 1, 1, .3, 1);
3268                             victim->DoBloodBig(200, 195);
3269                             award_bonus(id, tracheotomy);
3270                         }
3271                         if (animTarget == knifefollowanim) {
3272                             award_bonus(id, Stabbonus);
3273                             XYZ footvel, footpoint;
3274                             footvel = 0;
3275                             footpoint = weapons[weaponids[0]].tippoint;
3276                             if (bloodtoggle)
3277                                 Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 0, 0, .9, .3);
3278                             footvel = (weapons[weaponids[0]].tippoint - weapons[weaponids[0]].position) * -1;
3279                             Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 7, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
3280                             Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 3, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
3281                             Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * 5, 1, 1, 1, .2, 1);
3282                             Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * 2, 1, 1, 1, .2, 1);
3283
3284                         }
3285                         victim->bloodloss += 10000;
3286                         victim->velocity = 0;
3287                         emit_sound_at(fleshstabsound, victim->coords);
3288                         if (bloodtoggle)
3289                             weapons[weaponids[weaponactive]].bloody = 2;
3290                         weapons[weaponids[weaponactive]].blooddrip += 5;
3291                     }
3292                 }
3293
3294                 if (hasvictim && (animTarget == knifefollowanim || animTarget == knifesneakattackanim) && animation[animTarget].label[frameCurrent] == 6) {
3295                     escapednum = 0;
3296                     victim->velocity = 0;
3297                     for (int i = 0; i < victim->skeleton.num_joints; i++) {
3298                         victim->skeleton.joints[i].velocity = 0;
3299                     }
3300                     if (animTarget == knifefollowanim) {
3301                         victim->RagDoll(0);
3302                         for (int i = 0; i < victim->skeleton.num_joints; i++) {
3303                             victim->skeleton.joints[i].velocity = 0;
3304                         }
3305                     }
3306                     if (weaponactive != -1 && animation[victim->animTarget].attack != reversal) {
3307                         emit_sound_at(fleshstabremovesound, victim->coords);
3308                         if (bloodtoggle)
3309                             weapons[weaponids[weaponactive]].bloody = 2;
3310                         weapons[weaponids[weaponactive]].blooddrip += 5;
3311
3312                         XYZ footvel, footpoint;
3313                         footvel = 0;
3314                         footpoint = weapons[weaponids[0]].tippoint;
3315                         if (bloodtoggle)
3316                             Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 0, 0, .9, .3);
3317                         footvel = (weapons[weaponids[0]].tippoint - weapons[weaponids[0]].position) * -1;
3318                         Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 7, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
3319                         Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 3, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
3320                         Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * 5, 1, 1, 1, .3, 1);
3321                         Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * 2, 1, 1, 1, .3, 1);
3322                     }
3323                 }
3324
3325                 if (hasvictim && (animTarget == swordsneakattackanim) && animation[animTarget].label[frameCurrent] == 5) {
3326                     if (weaponactive != -1 && victim->bloodloss < victim->damagetolerance) {
3327                         award_bonus(id, backstab);
3328
3329                         escapednum = 0;
3330
3331                         XYZ footvel, footpoint;
3332                         footvel = 0;
3333                         footpoint = (weapons[weaponids[0]].tippoint + weapons[weaponids[0]].position) / 2;
3334                         if (bloodtoggle)
3335                             Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 0, 0, .9, .3);
3336                         footvel = (weapons[weaponids[0]].tippoint - weapons[weaponids[0]].position);
3337                         Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 7, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
3338                         Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 3, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
3339                         Sprite::MakeSprite(bloodflamesprite, footpoint, DoRotation(footvel * 5, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .3, 1);
3340                         Sprite::MakeSprite(bloodflamesprite, footpoint, DoRotation(footvel * 3, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .3, 1);
3341                         victim->DoBloodBig(200, 180);
3342                         victim->DoBloodBig(200, 215);
3343                         victim->bloodloss += 10000;
3344                         victim->velocity = 0;
3345                         emit_sound_at(fleshstabsound, victim->coords);
3346                         if (bloodtoggle)
3347                             weapons[weaponids[weaponactive]].bloody = 2;
3348                         weapons[weaponids[weaponactive]].blooddrip += 5;
3349                     }
3350                 }
3351
3352                 if (hasvictim && animTarget == swordsneakattackanim && animation[animTarget].label[frameCurrent] == 6) {
3353                     escapednum = 0;
3354                     victim->velocity = 0;
3355                     for (int i = 0; i < victim->skeleton.num_joints; i++) {
3356                         victim->skeleton.joints[i].velocity = 0;
3357                     }
3358                     if (weaponactive != -1) {
3359                         emit_sound_at(fleshstabremovesound, victim->coords);
3360                         if (bloodtoggle)
3361                             weapons[weaponids[weaponactive]].bloody = 2;
3362                         weapons[weaponids[weaponactive]].blooddrip += 5;
3363
3364                         XYZ footvel, footpoint;
3365                         footvel = 0;
3366                         footpoint = weapons[weaponids[0]].tippoint;
3367                         if (bloodtoggle)
3368                             Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 0, 0, .9, .3);
3369                         footvel = (weapons[weaponids[0]].tippoint - weapons[weaponids[0]].position) * -1;
3370                         Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 7, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
3371                         Sprite::MakeSprite(bloodsprite, footpoint, DoRotation(footvel * 3, (float)(Random() % 20), (float)(Random() % 20), 0), 1, 1, 1, .05, .9);
3372                         Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * 5, 1, 1, 1, .3, 1);
3373                         Sprite::MakeSprite(bloodflamesprite, footpoint, footvel * 2, 1, 1, 1, .3, 1);
3374                     }
3375                 }
3376
3377                 if (animTarget == sweepreversalanim && animation[animTarget].label[frameCurrent] == 7) {
3378                     escapednum = 0;
3379                     if (id == 0)
3380                         camerashake += .4;
3381                     if (Random() % 2) {
3382                         victim->spurt = 1;
3383                         DoBlood(.2, 240);
3384                     }
3385                     if (weaponactive == -1) {
3386                         if (tutoriallevel != 1) {
3387                             emit_sound_at(heavyimpactsound, victim->coords, 128.);
3388                         }
3389                     }
3390                     bool doslice;
3391                     doslice = 0;
3392                     if (weaponactive != -1 || creature == wolftype)
3393                         doslice = 1;
3394                     if (creature == rabbittype && weaponactive != -1)
3395                         if (weapons[weaponids[0]].getType() == staff)
3396                             doslice = 0;
3397                     if (doslice) {
3398                         if (weaponactive != -1) {
3399                             victim->DoBloodBig(2 / victim->armorhead, 225);
3400                             emit_sound_at(knifeslicesound, victim->coords);
3401                             if (bloodtoggle && !weapons[weaponids[weaponactive]].bloody)
3402                                 weapons[weaponids[weaponactive]].bloody = 1;
3403                             weapons[weaponids[weaponactive]].blooddrip += 3;
3404                         }
3405                         if (weaponactive == -1 && creature == wolftype) {
3406                             emit_sound_at(clawslicesound, victim->coords, 128.);
3407                             victim->spurt = 1;
3408                             victim->DoBloodBig(2 / victim->armorhead, 175);
3409                         }
3410                     }
3411
3412                     award_bonus(id, Reversal);
3413
3414                     victim->Puff(neck);
3415
3416                     XYZ relative;
3417                     relative = facing * -1;
3418                     relative.y = 0;
3419                     Normalise(&relative);
3420                     relative = DoRotation(relative, 0, 90, 0);
3421                     relative.y = .5;
3422                     Normalise(&relative);
3423                     for (int i = 0; i < victim->skeleton.num_joints; i++) {
3424                         victim->skeleton.joints[i].velocity += relative * damagemult * 20;
3425                     }
3426                     victim->jointVel(head) += relative * damagemult * 200;
3427                     if (victim->damage < victim->damagetolerance - 100)
3428                         victim->velocity = relative * 200;
3429                     victim->DoDamage(damagemult * 100 / victim->protectionhead);
3430                     victim->velocity = 0;
3431                 }
3432
3433                 if (animTarget == sweepreversalanim && ((animation[animTarget].label[frameCurrent] == 9 && victim->damage < victim->damagetolerance) || (animation[animTarget].label[frameCurrent] == 7 && victim->damage > victim->damagetolerance))) {
3434                     escapednum = 0;
3435                     victim->RagDoll(0);
3436                     XYZ relative;
3437                     relative = facing * -1;
3438                     relative.y = 0;
3439                     Normalise(&relative);
3440                     relative = DoRotation(relative, 0, 90, 0);
3441                     relative.y = .5;
3442                     Normalise(&relative);
3443                     for (int i = 0; i < victim->skeleton.num_joints; i++) {
3444                         victim->skeleton.joints[i].velocity += relative * damagemult * 20;
3445                     }
3446                     victim->jointVel(head) += relative * damagemult * 200;
3447                 }
3448
3449                 if (hasvictim && (animTarget == spinkickreversalanim || animTarget == sweepreversalanim || animTarget == rabbitkickreversalanim || animTarget == upunchreversalanim || animTarget == jumpreversalanim || animTarget == swordslashreversalanim || animTarget == knifeslashreversalanim || animTarget == rabbittacklereversal || animTarget == wolftacklereversal || animTarget == staffhitreversalanim || animTarget == staffspinhitreversalanim))
3450                     if (victim->damage > victim->damagetolerance && bonus != reverseko) {
3451                         award_bonus(id, reverseko);
3452                     }
3453             }
3454
3455
3456             //Animation end
3457             if (frameTarget > animation[animCurrent].numframes - 1) {
3458                 frameTarget = 0;
3459                 if (wasStop()) {
3460                     animTarget = getIdle();
3461                     FootLand(0, 1);
3462                     FootLand(1, 1);
3463                 }
3464                 if (animCurrent == rabbittackleanim || animCurrent == rabbittacklinganim) {
3465                     animTarget = rollanim;
3466                     frameTarget = 3;
3467                     emit_sound_at(movewhooshsound, coords, 128.);
3468                 }
3469                 if (animCurrent == staggerbackhighanim) {
3470                     animTarget = getIdle();
3471                 }
3472                 if (animCurrent == staggerbackhardanim) {
3473                     animTarget = getIdle();
3474                 }
3475                 if (animCurrent == removeknifeanim) {
3476                     animTarget = getIdle();
3477                 }
3478                 if (animCurrent == crouchremoveknifeanim) {
3479                     animTarget = getCrouch();
3480                 }
3481                 if (animCurrent == backhandspringanim) {
3482                     animTarget = getIdle();
3483                 }
3484                 if (animCurrent == dodgebackanim) {
3485                     animTarget = getIdle();
3486                 }
3487                 if (animCurrent == drawleftanim) {
3488                     animTarget = getIdle();
3489                 }
3490                 if (animCurrent == drawrightanim || animCurrent == crouchdrawrightanim) {
3491                     animTarget = getIdle();
3492                     if (animCurrent == crouchdrawrightanim) {
3493                         animTarget = getCrouch();
3494                     }
3495                     if (weaponactive == -1)
3496                         weaponactive = 0;
3497                     else if (weaponactive == 0) {
3498                         weaponactive = -1;
3499                         if (num_weapons == 2) {
3500                             int buffer;
3501                             buffer = weaponids[0];
3502                             weaponids[0] = weaponids[1];
3503                             weaponids[1] = buffer;
3504                         }
3505                     }
3506
3507                     if (weaponactive == -1) {
3508                         emit_sound_at(knifesheathesound, coords, 128.);
3509                     }
3510                     if (weaponactive != -1) {
3511                         emit_sound_at(knifedrawsound, coords, 128.);
3512                     }
3513                 }
3514                 if (animCurrent == rollanim) {
3515                     animTarget = getCrouch();
3516                     FootLand(0, 1);
3517                     FootLand(1, 1);
3518                 }
3519                 if (isFlip()) {
3520                     if (animTarget == walljumprightkickanim) {
3521                         targetrot = -190;
3522                     }
3523                     if (animTarget == walljumpleftkickanim) {
3524                         targetrot = 190;
3525                     }
3526                     animTarget = jumpdownanim;
3527                 }
3528                 if (animCurrent == climbanim) {
3529                     animTarget = getCrouch();
3530                     frameTarget = 1;
3531                     coords += facing * .1;
3532                     if (!isnormal(coords.x))
3533                         coords = oldcoords;
3534                     oldcoords = coords;
3535                     collided = 0;
3536                     targetoffset = 0;
3537                     currentoffset = 0;
3538                     grabdelay = 1;
3539                     velocity = 0;
3540                     collided = 0;
3541                     avoidcollided = 0;
3542                 }
3543                 if (animTarget == rabbitkickreversalanim) {
3544                     animTarget = getCrouch();
3545                     lastfeint = 0;
3546                 }
3547                 if (animTarget == jumpreversalanim) {
3548                     animTarget = getCrouch();
3549                     lastfeint = 0;
3550                 }
3551                 if (animTarget == walljumprightanim || animTarget == walljumpbackanim || animTarget == walljumpfrontanim) {
3552                     if (attackkeydown && animTarget != walljumpfrontanim) {
3553                         int closest = -1;
3554                         float closestdist = -1;
3555                         float distance;
3556                         if (Person::players.size() > 1)
3557                             for (unsigned i = 0; i < Person::players.size(); i++) {
3558                                 if (id != i && Person::players[i]->coords.y < coords.y && !Person::players[i]->skeleton.free) {
3559                                     distance = distsq(&Person::players[i]->coords, &coords);
3560                                     if (closestdist == -1 || distance < closestdist) {
3561                                         closestdist = distance;
3562                                         closest = i;
3563                                     }
3564                                 }
3565                             }
3566                         if (closestdist > 0 && closest >= 0 && closestdist < 16) {
3567                             victim = Person::players[closest];
3568                             animTarget = walljumprightkickanim;
3569                             frameTarget = 0;
3570                             XYZ rotatetarget = victim->coords - coords;
3571                             Normalise(&rotatetarget);
3572                             yaw = -asin(0 - rotatetarget.x);
3573                             yaw *= 360 / 6.28;
3574                             if (rotatetarget.z < 0)
3575                                 yaw = 180 - yaw;
3576                             targettilt2 = -asin(rotatetarget.y) * 360 / 6.28;
3577                             velocity = (victim->coords - coords) * 4;
3578                             velocity.y += 2;
3579                             transspeed = 40;
3580                         }
3581                     }
3582                     if (animTarget == walljumpbackanim) {
3583                         animTarget = backflipanim;
3584                         frameTarget = 3;
3585                         velocity = facing * -8;
3586                         velocity.y = 4;
3587                         if (id == 0)
3588                             resume_stream(whooshsound);
3589                     }
3590                     if (animTarget == walljumprightanim) {
3591                         animTarget = rightflipanim;
3592                         frameTarget = 4;
3593                         targetyaw -= 90;
3594                         yaw -= 90;
3595                         velocity = DoRotation(facing, 0, 30, 0) * -8;
3596                         velocity.y = 4;
3597                     }
3598                     if (animTarget == walljumpfrontanim) {
3599                         animTarget = frontflipanim;
3600                         frameTarget = 2;
3601                         //targetyaw-=180;
3602                         ////yaw-=180;
3603                         velocity = facing * 8;
3604                         velocity.y = 4;
3605                     }
3606                     if (id == 0)
3607                         resume_stream(whooshsound);
3608                 }
3609                 if (animTarget == walljumpleftanim) {
3610                     if (attackkeydown) {
3611                         int closest = -1;
3612                         float closestdist = -1;
3613                         float distance;
3614                         if (Person::players.size() > 1)
3615                             for (unsigned i = 0; i < Person::players.size(); i++) {
3616                                 if (id != i && Person::players[i]->coords.y < coords.y && !Person::players[i]->skeleton.free) {
3617                                     distance = distsq(&Person::players[i]->coords, &coords);
3618                                     if (closestdist == -1 || distance < closestdist) {
3619                                         closestdist = distance;
3620                                         closest = i;
3621                                     }
3622                                 }
3623                             }
3624                         if (closestdist > 0 && closest >= 0 && closestdist < 16) {
3625                             victim = Person::players[closest];
3626                             animTarget = walljumpleftkickanim;
3627                             frameTarget = 0;
3628                             XYZ rotatetarget = victim->coords - coords;
3629                             Normalise(&rotatetarget);
3630                             yaw = -asin(0 - rotatetarget.x);
3631                             yaw *= 360 / 6.28;
3632                             if (rotatetarget.z < 0)
3633                                 yaw = 180 - yaw;
3634                             targettilt2 = -asin(rotatetarget.y) * 360 / 6.28;
3635                             velocity = (victim->coords - coords) * 4;
3636                             velocity.y += 2;
3637                             transspeed = 40;
3638                         }
3639                     }
3640                     if (animTarget != walljumpleftkickanim) {
3641                         animTarget = leftflipanim;
3642                         frameTarget = 4;
3643                         targetyaw += 90;
3644                         yaw += 90;
3645                         velocity = DoRotation(facing, 0, -30, 0) * -8;
3646                         velocity.y = 4;
3647                     }
3648                     if (id == 0)
3649                         resume_stream(whooshsound);
3650                 }
3651                 if (animTarget == sneakattackanim) {
3652                     animCurrent = getCrouch();
3653                     animTarget = getCrouch();
3654                     frameTarget = 1;
3655                     frameCurrent = 0;
3656                     targetyaw += 180;
3657                     yaw += 180;
3658                     targettilt2 *= -1;
3659                     tilt2 *= -1;
3660                     transspeed = 1000000;
3661                     targetheadyaw += 180;
3662                     coords -= facing * .7;
3663                     if (onterrain)
3664                         coords.y = terrain.getHeight(coords.x, coords.z);
3665
3666                     lastfeint = 0;
3667                 }
3668                 if (animTarget == knifesneakattackanim || animTarget == swordsneakattackanim) {
3669                     animTarget = getIdle();
3670                     frameTarget = 0;
3671                     if (onterrain)
3672                         coords.y = terrain.getHeight(coords.x, coords.z);
3673
3674                     lastfeint = 0;
3675                 }
3676                 if (animCurrent == knifefollowanim) {
3677                     animTarget = getIdle();
3678                     lastfeint = 0;
3679                 }
3680                 if (animation[animTarget].attack == reversal && animCurrent != sneakattackanim && animCurrent != knifesneakattackanim && animCurrent != swordsneakattackanim && animCurrent != knifefollowanim) {
3681                     float ycoords = oldcoords.y;
3682                     animTarget = getStop();
3683                     targetyaw += 180;
3684                     yaw += 180;
3685                     targettilt2 *= -1;
3686                     tilt2 *= -1;
3687                     transspeed = 1000000;
3688                     targetheadyaw += 180;
3689                     if (!isnormal(coords.x))
3690                         coords = oldcoords;
3691                     if (animCurrent == spinkickreversalanim || animCurrent == swordslashreversalanim)
3692                         oldcoords = coords + facing * .5;
3693                     else if (animCurrent == sweepreversalanim)
3694                         oldcoords = coords + facing * 1.1;
3695                     else if (animCurrent == upunchreversalanim) {
3696                         oldcoords = coords + facing * 1.5;
3697                         targetyaw += 180;
3698                         yaw += 180;
3699                         targetheadyaw += 180;
3700                         targettilt2 *= -1;
3701                         tilt2 *= -1;
3702                     } else if (animCurrent == knifeslashreversalanim) {
3703                         oldcoords = coords + facing * .5;
3704                         targetyaw += 90;
3705                         yaw += 90;
3706                         targetheadyaw += 90;
3707                         targettilt2 = 0;
3708                         tilt2 = 0;
3709                     } else if (animCurrent == staffspinhitreversalanim) {
3710                         targetyaw += 180;
3711                         yaw += 180;
3712                         targetheadyaw += 180;
3713                         targettilt2 = 0;
3714                         tilt2 = 0;
3715                     }
3716                     if (onterrain)
3717                         oldcoords.y = terrain.getHeight(oldcoords.x, oldcoords.z);
3718                     else
3719                         oldcoords.y = ycoords;
3720                     currentoffset = coords - oldcoords;
3721                     targetoffset = 0;
3722                     coords = oldcoords;
3723
3724                     lastfeint = 0;
3725                 }
3726                 if (animCurrent == knifesneakattackedanim || animCurrent == swordsneakattackedanim) {
3727                     velocity = 0;
3728                     velocity.y = -5;
3729                     RagDoll(0);
3730                 }
3731                 if (animation[animTarget].attack == reversed) {
3732                     escapednum++;
3733                     if (animTarget == sweepreversedanim)
3734                         targetyaw += 90;
3735                     animTarget = backhandspringanim;
3736                     frameTarget = 2;
3737                     emit_sound_at(landsound, coords, 128);
3738
3739                     if (animCurrent == upunchreversedanim || animCurrent == swordslashreversedanim) {
3740                         animTarget = rollanim;
3741                         frameTarget = 5;
3742                         oldcoords = coords;
3743                         coords += (DoRotation(jointPos(leftfoot), 0, yaw, 0) + DoRotation(jointPos(rightfoot), 0, yaw, 0)) / 2 * scale;
3744                         coords.y = oldcoords.y;
3745                     }
3746                     if (animCurrent == knifeslashreversedanim) {
3747                         animTarget = rollanim;
3748                         frameTarget = 0;
3749                         targetyaw += 90;
3750                         yaw += 90;
3751                         oldcoords = coords;
3752                         coords += (DoRotation(jointPos(leftfoot), 0, yaw, 0) + DoRotation(jointPos(rightfoot), 0, yaw, 0)) / 2 * scale;
3753                         coords.y = oldcoords.y;
3754                     }
3755                 }
3756                 if (wasFlip()) {
3757                     animTarget = jumpdownanim;
3758                 }
3759                 if (wasLanding())
3760                     animTarget = getIdle();
3761                 if (wasLandhard())
3762                     animTarget = getIdle();
3763                 if (animCurrent == spinkickanim || animCurrent == getupfrombackanim || animCurrent == getupfromfrontanim || animCurrent == lowkickanim) {
3764                     animTarget = getIdle();
3765                     oldcoords = coords;
3766                     coords += (DoRotation(jointPos(leftfoot), 0, yaw, 0) + DoRotation(jointPos(rightfoot), 0, yaw, 0)) / 2 * scale;
3767                     coords.y = oldcoords.y;
3768                     //coords+=DoRotation(animation[animCurrent].offset,0,yaw,0)*scale;
3769                     targetoffset.y = coords.y;
3770                     if (onterrain)
3771                         targetoffset.y = terrain.getHeight(coords.x, coords.z);
3772                     currentoffset = DoRotation(animation[animCurrent].offset * -1, 0, yaw, 0) * scale;
3773                     currentoffset.y -= (coords.y - targetoffset.y);
3774                     coords.y = targetoffset.y;
3775                     targetoffset = 0;
3776                     normalsupdatedelay = 0;
3777                 }
3778                 if (animCurrent == upunchanim) {
3779                     animTarget = getStop();
3780                     normalsupdatedelay = 0;
3781                     lastfeint = 0;
3782                 }
3783                 if (animCurrent == rabbitkickanim && animTarget != backflipanim) {
3784                     targetyaw = yaw;
3785                     bool hasstaff;
3786                     hasstaff = 0;
3787                     if (num_weapons > 0)
3788                         if (weapons[0].getType() == staff)
3789                             hasstaff = 1;
3790                     if (!hasstaff)
3791                         DoDamage(35);
3792                     RagDoll(0);
3793                     lastfeint = 0;
3794                     rabbitkickragdoll = 1;
3795                 }
3796                 if (animCurrent == rabbitkickreversedanim) {
3797                     if (!feint) {
3798                         velocity = 0;
3799                         velocity.y = -10;
3800                         //DoDamage(100);
3801                         RagDoll(0);
3802                         skeleton.spinny = 0;
3803                         SolidHitBonus(!id); // FIXME: tricky id
3804                     }
3805                     if (feint) {
3806                         escapednum++;
3807                         animTarget = rollanim;
3808                         coords += facing;
3809                         if (id == 0)
3810                             pause_sound(whooshsound);
3811                     }
3812                     lastfeint = 0;
3813                 }
3814                 if (animCurrent == rabbittackledbackanim || animCurrent == rabbittackledfrontanim) {
3815                     velocity = 0;
3816                     velocity.y = -10;
3817                     RagDoll(0);
3818                     skeleton.spinny = 0;
3819                 }
3820                 if (animCurrent == jumpreversedanim) {
3821                     if (!feint) {
3822                         velocity = 0;
3823                         velocity.y = -10;
3824                         //DoDamage(100);
3825                         RagDoll(0);
3826                         skeleton.spinny = 0;
3827                         SolidHitBonus(!id); // FIXME: tricky id
3828                     }
3829                     if (feint) {
3830                         escapednum++;
3831                         animTarget = rollanim;
3832                         coords += facing * 2;
3833                         if (id == 0)
3834                             pause_sound(whooshsound);
3835                     }
3836                     lastfeint = 0;
3837                 }
3838
3839                 if (animation[animCurrent].attack == normalattack && !victim->skeleton.free && victim->animTarget != staggerbackhighanim && victim->animTarget != staggerbackhardanim && animTarget != winduppunchblockedanim && animTarget != blockhighleftanim && animTarget != swordslashparryanim && animTarget != swordslashparriedanim && animTarget != crouchstabanim && animTarget != swordgroundstabanim) {
3840                     animTarget = getupfromfrontanim;
3841                     lastfeint = 0;
3842                 } else if (animation[animCurrent].attack == normalattack) {
3843                     animTarget = getIdle();
3844                     lastfeint = 0;
3845                 }
3846                 if (animCurrent == blockhighleftanim && aitype != playercontrolled) {
3847                     animTarget = blockhighleftstrikeanim;
3848                 }
3849                 if (animCurrent == knifeslashstartanim || animCurrent == knifethrowanim || animCurrent == swordslashanim || animCurrent == staffhitanim || animCurrent == staffgroundsmashanim || animCurrent == staffspinhitanim) {
3850                     animTarget = getIdle();
3851                     lastfeint = 0;
3852                 }
3853                 if (animCurrent == spinkickanim && victim->skeleton.free) {
3854                     if (creature == rabbittype)
3855                         animTarget = fightidleanim;
3856                 }
3857             }
3858             target = 0;
3859
3860             if (isIdle() && !wasIdle())
3861                 normalsupdatedelay = 0;
3862
3863             if (animCurrent == jumpupanim && velocity.y < 0 && !isFlip()) {
3864                 animTarget = jumpdownanim;
3865             }
3866         }
3867         if (!skeleton.free) {
3868             oldtarget = target;
3869             if (!transspeed && animation[animTarget].attack != 2 && animation[animTarget].attack != 3) {
3870                 if (!isRun() || !wasRun()) {
3871                     if (animation[animTarget].speed[frameTarget] > animation[animCurrent].speed[frameCurrent])
3872                         target += multiplier * animation[animTarget].speed[frameTarget] * speed * 2;
3873                     if (animation[animTarget].speed[frameTarget] <= animation[animCurrent].speed[frameCurrent])
3874                         target += multiplier * animation[animCurrent].speed[frameCurrent] * speed * 2;
3875                 }
3876                 if (isRun() && wasRun()) {
3877                     float tempspeed;
3878                     tempspeed = velspeed;
3879                     if (tempspeed < 10 * speedmult)
3880                         tempspeed = 10 * speedmult;
3881                     target += multiplier * animation[animTarget].speed[frameCurrent] * speed * 1.7 * tempspeed / (speed * 45 * scale);
3882                 }
3883             } else if (transspeed)
3884                 target += multiplier * transspeed * speed * 2;
3885             else {
3886                 if (!isRun() || !wasRun()) {
3887                     if (animation[animTarget].speed[frameTarget] > animation[animCurrent].speed[frameCurrent])
3888                         target += multiplier * animation[animTarget].speed[frameTarget] * 2;
3889                     if (animation[animTarget].speed[frameTarget] <= animation[animCurrent].speed[frameCurrent])
3890                         target += multiplier * animation[animCurrent].speed[frameCurrent] * 2;
3891                 }
3892             }
3893
3894             if (animCurrent != animTarget)
3895                 target = (target + oldtarget) / 2;
3896
3897             if (target > 1) {
3898                 frameCurrent = frameTarget;
3899                 target = 1;
3900             }
3901             oldrot = rot;
3902             rot = targetrot * target;
3903             yaw += rot - oldrot;
3904             if (target == 1) {
3905                 rot = 0;
3906                 oldrot = 0;
3907                 targetrot = 0;
3908             }
3909             if (animCurrent != oldanimCurrent || animTarget != oldanimTarget || ((frameCurrent != oldframeCurrent || frameTarget != oldframeTarget) && !calcrot)) {
3910                 //Old rotates
3911                 for (int i = 0; i < skeleton.num_joints; i++) {
3912                     skeleton.joints[i].position = animation[animCurrent].position[i][frameCurrent];
3913                 }
3914
3915                 skeleton.FindForwards();
3916
3917                 for (int i = 0; i < skeleton.num_muscles; i++) {
3918                     if (skeleton.muscles[i].visible) {
3919                         skeleton.FindRotationMuscle(i, animTarget);
3920                     }
3921                 }
3922                 for (int i = 0; i < skeleton.num_muscles; i++) {
3923                     if (skeleton.muscles[i].visible) {
3924                         if (isnormal((float)((int)(skeleton.muscles[i].rotate1 * 100) % 36000) / 100))
3925                             skeleton.muscles[i].oldrotate1 = (float)((int)(skeleton.muscles[i].rotate1 * 100) % 36000) / 100;
3926                         if (isnormal((float)((int)(skeleton.muscles[i].rotate2 * 100) % 36000) / 100))
3927                             skeleton.muscles[i].oldrotate2 = (float)((int)(skeleton.muscles[i].rotate2 * 100) % 36000) / 100;
3928                         if (isnormal((float)((int)(skeleton.muscles[i].rotate3 * 100) % 36000) / 100))
3929                             skeleton.muscles[i].oldrotate3 = (float)((int)(skeleton.muscles[i].rotate3 * 100) % 36000) / 100;
3930                     }
3931                 }
3932
3933                 //New rotates
3934                 for (int i = 0; i < skeleton.num_joints; i++) {
3935                     skeleton.joints[i].position = animation[animTarget].position[i][frameTarget];
3936                 }
3937
3938                 skeleton.FindForwards();
3939
3940                 for (int i = 0; i < skeleton.num_muscles; i++) {
3941                     if (skeleton.muscles[i].visible) {
3942                         skeleton.FindRotationMuscle(i, animTarget);
3943                     }
3944                 }
3945                 for (int i = 0; i < skeleton.num_muscles; i++) {
3946                     if (skeleton.muscles[i].visible) {
3947                         if (isnormal((float)((int)(skeleton.muscles[i].rotate1 * 100) % 36000) / 100))
3948                             skeleton.muscles[i].newrotate1 = (float)((int)(skeleton.muscles[i].rotate1 * 100) % 36000) / 100;
3949                         if (isnormal((float)((int)(skeleton.muscles[i].rotate2 * 100) % 36000) / 100))
3950                             skeleton.muscles[i].newrotate2 = (float)((int)(skeleton.muscles[i].rotate2 * 100) % 36000) / 100;
3951                         if (isnormal((float)((int)(skeleton.muscles[i].rotate3 * 100) % 36000) / 100))
3952                             skeleton.muscles[i].newrotate3 = (float)((int)(skeleton.muscles[i].rotate3 * 100) % 36000) / 100;
3953                         if (skeleton.muscles[i].newrotate3 > skeleton.muscles[i].oldrotate3 + 180) skeleton.muscles[i].newrotate3 -= 360;
3954                         if (skeleton.muscles[i].newrotate3 < skeleton.muscles[i].oldrotate3 - 180) skeleton.muscles[i].newrotate3 += 360;
3955                         if (skeleton.muscles[i].newrotate2 > skeleton.muscles[i].oldrotate2 + 180) skeleton.muscles[i].newrotate2 -= 360;
3956                         if (skeleton.muscles[i].newrotate2 < skeleton.muscles[i].oldrotate2 - 180) skeleton.muscles[i].newrotate2 += 360;
3957                         if (skeleton.muscles[i].newrotate1 > skeleton.muscles[i].oldrotate1 + 180) skeleton.muscles[i].newrotate1 -= 360;
3958                         if (skeleton.muscles[i].newrotate1 < skeleton.muscles[i].oldrotate1 - 180) skeleton.muscles[i].newrotate1 += 360;
3959                     }
3960                 }
3961             }
3962             if (frameCurrent >= animation[animCurrent].numframes)
3963                 frameCurrent = animation[animCurrent].numframes - 1;
3964
3965             oldanimCurrent = animCurrent;
3966             oldanimTarget = animTarget;
3967             oldframeTarget = frameTarget;
3968             oldframeCurrent = frameCurrent;
3969
3970             for (int i = 0; i < skeleton.num_joints; i++) {
3971                 skeleton.joints[i].velocity = (animation[animCurrent].position[i][frameCurrent] * (1 - target) + animation[animTarget].position[i][frameTarget] * (target) - skeleton.joints[i].position) / multiplier;
3972                 skeleton.joints[i].position = animation[animCurrent].position[i][frameCurrent] * (1 - target) + animation[animTarget].position[i][frameTarget] * (target);
3973             }
3974             offset = currentoffset * (1 - target) + targetoffset * target;
3975             for (int i = 0; i < skeleton.num_muscles; i++) {
3976                 if (skeleton.muscles[i].visible) {
3977                     skeleton.muscles[i].rotate1 = skeleton.muscles[i].oldrotate1 * (1 - target) + skeleton.muscles[i].newrotate1 * (target);
3978                     skeleton.muscles[i].rotate2 = skeleton.muscles[i].oldrotate2 * (1 - target) + skeleton.muscles[i].newrotate2 * (target);
3979                     skeleton.muscles[i].rotate3 = skeleton.muscles[i].oldrotate3 * (1 - target) + skeleton.muscles[i].newrotate3 * (target);
3980                 }
3981             }
3982         }
3983
3984         if (isLanding() && landhard) {
3985             if (id == 0)
3986                 camerashake += .4;
3987             animTarget = getLandhard();
3988             frameTarget = 0;
3989             target = 0;
3990             landhard = 0;
3991             transspeed = 15;
3992         }
3993     }
3994 }
3995
3996 /* EFFECT
3997  * MONSTER
3998  * TODO
3999  */
4000 void Person::DoStuff()
4001 {
4002     static XYZ terrainnormal;
4003     static XYZ flatfacing;
4004     static XYZ flatvelocity;
4005     static float flatvelspeed;
4006     static int i, j, l;
4007     static XYZ average;
4008     static int howmany;
4009     static int bloodsize;
4010     static int startx, starty, endx, endy;
4011     static GLubyte color;
4012     static XYZ bloodvel;
4013
4014     onfiredelay -= multiplier;
4015     if (onfiredelay < 0 && onfire) {
4016         if (Random() % 2 == 0) {
4017             crouchkeydown = 1;
4018         }
4019         onfiredelay = 0.3;
4020     }
4021
4022     crouchkeydowntime += multiplier;
4023     if (!crouchkeydown)
4024         crouchkeydowntime = 0;
4025     jumpkeydowntime += multiplier;
4026     if (!jumpkeydown && skeleton.free)
4027         jumpkeydowntime = 0;
4028
4029     if (hostile || damage > 0 || bloodloss > 0)
4030         immobile = 0;
4031
4032     if (isIdle() || isRun())
4033         targetoffset = 0;
4034
4035     if (num_weapons == 1 && weaponactive != -1)
4036         weaponstuck = -1;
4037
4038     if (id == 0)
4039         blooddimamount -= multiplier * .3;
4040     speechdelay -= multiplier;
4041     texupdatedelay -= multiplier;
4042     interestdelay -= multiplier;
4043     flamedelay -= multiplier;
4044     parriedrecently -= multiplier;
4045     if (!victim) {
4046         victim = this->shared_from_this();
4047         hasvictim = 0;
4048     }
4049
4050     if (id == 0)
4051         speed = 1.1 * speedmult;
4052     else
4053         speed = 1.0 * speedmult;
4054     if (!skeleton.free)
4055         rabbitkickragdoll = 0;
4056
4057     speed *= speedmult;
4058
4059     if (id != 0 && (creature == rabbittype || difficulty != 2))
4060         superruntoggle = 0;
4061     if (id != 0 && creature == wolftype && difficulty == 2) {
4062         superruntoggle = 0;
4063         if (aitype != passivetype) {
4064             superruntoggle = 1;
4065             if (aitype == attacktypecutoff && (Person::players[0]->isIdle() || Person::players[0]->isCrouch() || Person::players[0]->skeleton.free || Person::players[0]->animTarget == getupfrombackanim || Person::players[0]->animTarget == getupfromfrontanim || Person::players[0]->animTarget == sneakanim) && distsq(&coords, &Person::players[0]->coords) < 16) {
4066                 superruntoggle = 0;
4067             }
4068         }
4069         if (scale < 0.2)
4070             superruntoggle = 0;
4071         if (animTarget == wolfrunninganim && !superruntoggle) {
4072             animTarget = getRun();
4073             frameTarget = 0;
4074         }
4075     }
4076     if (weaponactive == -1 && num_weapons > 0) {
4077         if (weapons[weaponids[0]].getType() == staff) {
4078             weaponactive = 0;
4079         }
4080     }
4081
4082     if (onfire) {
4083         burnt += multiplier;
4084         deathbleeding = 1;
4085         if (burnt > .6)
4086             burnt = .6;
4087         OPENAL_SetVolume(channels[stream_firesound], 256 + 256 * findLength(&velocity) / 3);
4088
4089         if (animTarget == jumpupanim || animTarget == jumpdownanim || isFlip()) {
4090             float gLoc[3];
4091             float vel[3];
4092             gLoc[0] = coords.x;
4093             gLoc[1] = coords.y;
4094             gLoc[2] = coords.z;
4095             vel[0] = velocity.x;
4096             vel[1] = velocity.y;
4097             vel[2] = velocity.z;
4098
4099             if (id == 0) {
4100                 OPENAL_3D_SetAttributes(channels[whooshsound], gLoc, vel);
4101                 OPENAL_SetVolume(channels[whooshsound], 64 * findLength(&velocity) / 5);
4102             }
4103         }
4104     }
4105     while (flamedelay < 0 && onfire) {
4106         flamedelay += .006;
4107         howmany = abs(Random() % (skeleton.num_joints));
4108         if (!skeleton.free)
4109             flatvelocity = (coords - oldcoords) / multiplier / 2; //velocity/2;
4110         if (skeleton.free)
4111             flatvelocity = skeleton.joints[howmany].velocity * scale / 2;
4112         if (!skeleton.free)
4113             flatfacing = DoRotation(DoRotation(DoRotation(skeleton.joints[howmany].position, 0, 0, tilt), tilt2, 0, 0), 0, yaw, 0) * scale + coords;
4114         if (skeleton.free)
4115             flatfacing = skeleton.joints[howmany].position * scale + coords;
4116         Sprite::MakeSprite(flamesprite, flatfacing, flatvelocity, 1, 1, 1, .6 + (float)abs(Random() % 100) / 200 - .25, 1);
4117     }
4118
4119     while (flamedelay < 0 && !onfire && tutoriallevel == 1 && id != 0) {
4120         flamedelay += .05;
4121         howmany = abs(Random() % (skeleton.num_joints));
4122         if (!skeleton.free)
4123             flatvelocity = (coords - oldcoords) / multiplier / 2; //velocity/2;
4124         if (skeleton.free)
4125             flatvelocity = skeleton.joints[howmany].velocity * scale / 2;
4126         if (!skeleton.free)
4127             flatfacing = DoRotation(DoRotation(DoRotation(skeleton.joints[howmany].position, 0, 0, tilt), tilt2, 0, 0), 0, yaw, 0) * scale + coords;
4128         if (skeleton.free)
4129             flatfacing = skeleton.joints[howmany].position * scale + coords;
4130         Sprite::MakeSprite(breathsprite, flatfacing, flatvelocity, 1, 1, 1, .6 + (float)abs(Random() % 100) / 200 - .25, .3);
4131     }
4132
4133     if (bleeding > 0) {
4134         bleeding -= multiplier * .3;
4135         if (bloodtoggle == 2) {
4136             skeleton.drawmodel.textureptr.bind();
4137             if ((bleeding <= 0) && (detail != 2))
4138                 DoMipmaps();
4139         }
4140     }
4141
4142     if (neckspurtamount > 0) {
4143         neckspurtamount -= multiplier;
4144         neckspurtdelay -= multiplier * 3;
4145         neckspurtparticledelay -= multiplier * 3;
4146         if (neckspurtparticledelay < 0 && neckspurtdelay > 2) {
4147             spurt = 0;
4148             bloodvel = 0;
4149             if (!skeleton.free) {
4150                 bloodvel.z = 5 * neckspurtamount;
4151                 bloodvel = DoRotation(bloodvel, ((float)(Random() % 100)) / 40, yaw + ((float)(Random() % 100)) / 40, 0) * scale;
4152             }
4153             if (skeleton.free) {
4154                 bloodvel -= DoRotation(skeleton.forward * 10 * scale, ((float)(Random() % 100)) / 40, ((float)(Random() % 100)) / 40, 0);
4155             }
4156             if (skeleton.free)
4157                 bloodvel += DoRotation(jointVel(head), ((float)(Random() % 100)) / 40, yaw + ((float)(Random() % 100)) / 40, 0) * scale;
4158             if (!skeleton.free)
4159                 bloodvel += DoRotation(velocity, ((float)(Random() % 100)) / 40, ((float)(Random() % 100)) / 40, 0) * scale;
4160             if (skeleton.free)
4161                 Sprite::MakeSprite(bloodsprite, (jointPos(neck) + (jointPos(neck) - jointPos(head)) / 5)*scale + coords, bloodvel, 1, 1, 1, .05, .9);
4162             if (!skeleton.free)
4163                 Sprite::MakeSprite(bloodsprite, DoRotation(jointPos(neck) + (jointPos(neck) - jointPos(head)) / 5, 0, yaw, 0)*scale + coords, bloodvel, 1, 1, 1, .05, .9);
4164             neckspurtparticledelay = .05;
4165         }
4166         if (neckspurtdelay < 0) {
4167             neckspurtdelay = 3;
4168         }
4169     }
4170
4171     if (deathbleeding > 0 && dead != 2) {
4172         if (deathbleeding < 5)
4173             bleeddelay -= deathbleeding * multiplier / 4;
4174         else
4175             bleeddelay -= 5 * multiplier / 4;
4176         if (bleeddelay < 0 && bloodtoggle) {
4177             bleeddelay = 1;
4178             XYZ bloodvel;
4179             if (bloodtoggle) {
4180                 bloodvel = 0;
4181                 if (skeleton.free)
4182                     bloodvel += DoRotation(jointVel(abdomen), ((float)(Random() % 100)) / 4, yaw + ((float)(Random() % 100)) / 4, 0) * scale;
4183                 if (!skeleton.free)
4184                     bloodvel += DoRotation(velocity, ((float)(Random() % 100)) / 4, ((float)(Random() % 100)) / 4, 0) * scale;
4185                 if (skeleton.free)
4186                     Sprite::MakeSprite(bloodsprite, jointPos(abdomen) * scale + coords, bloodvel, 1, 1, 1, .05, 1);
4187                 if (!skeleton.free)
4188                     Sprite::MakeSprite(bloodsprite, DoRotation((jointPos(abdomen) + jointPos(abdomen)) / 2, 0, yaw, 0)*scale + coords, bloodvel, 1, 1, 1, .05, 1);
4189             }
4190         }
4191         bloodloss += deathbleeding * multiplier * 80;
4192         deathbleeding -= multiplier * 1.6;
4193         if (deathbleeding < 0)
4194             deathbleeding = 0;
4195         if (bloodloss > damagetolerance && animation[animTarget].attack == neutral) {
4196             if (weaponactive != -1) {
4197                 weapons[weaponids[0]].drop(velocity * scale * -.3, velocity * scale);
4198                 weapons[weaponids[0]].velocity.x += .01;
4199                 num_weapons--;
4200                 if (num_weapons) {
4201                     weaponids[0] = weaponids[num_weapons];
4202                     if (weaponstuck == num_weapons)
4203                         weaponstuck = 0;
4204                 }
4205                 weaponactive = -1;
4206                 for (unsigned i = 0; i < Person::players.size(); i++) {
4207                     Person::players[i]->wentforweapon = 0;
4208                 }
4209
4210                 if (id == 0) {
4211                     flashamount = .5;
4212                     flashr = 1;
4213                     flashg = 0;
4214                     flashb = 0;
4215                     flashdelay = 0;
4216                 }
4217             }
4218
4219             if (!dead && creature == wolftype) {
4220                 award_bonus(0, Wolfbonus);
4221             }
4222             dead = 2;
4223             if (animTarget == knifefollowedanim && !skeleton.free) {
4224                 for (int i = 0; i < skeleton.num_joints; i++) {
4225                     skeleton.joints[i].velocity = 0;
4226                     skeleton.joints[i].velocity.y = -2;
4227                 }
4228             }
4229             if (id != 0 && unconscioustime > .1) {
4230                 numafterkill++;
4231             }
4232
4233             RagDoll(0);
4234         }
4235     }
4236
4237     if (texupdatedelay < 0 && bleeding > 0 && bloodtoggle == 2 && distsq(&viewer, &coords) < 9) {
4238         texupdatedelay = .12;
4239
4240         bloodsize = 5 - realtexdetail;
4241
4242         startx = 0;
4243         starty = 0;
4244         startx = bleedy; //abs(Random()%(skeleton.skinsize-bloodsize-1));
4245         starty = bleedx; //abs(Random()%(skeleton.skinsize-bloodsize-1));
4246         endx = startx + bloodsize;
4247         endy = starty + bloodsize;
4248
4249         if (startx < 0) {
4250             startx = 0;
4251             bleeding = 0;
4252         }
4253         if (starty < 0) {
4254             starty = 0;
4255             bleeding = 0;
4256         }
4257         if (endx > skeleton.skinsize - 1) {
4258             endx = skeleton.skinsize - 1;
4259             bleeding = 0;
4260         }
4261         if (endy > skeleton.skinsize - 1) {
4262             endy = skeleton.skinsize - 1;
4263             bleeding = 0;
4264         }
4265         if (endx < startx)
4266             endx = startx;
4267         if (endy < starty)
4268             endy = starty;
4269
4270         for (i = startx; i < endx; i++) {
4271             for (j = starty; j < endy; j++) {
4272                 if (Random() % 2 == 0) {
4273                     color = Random() % 85 + 170;
4274                     if (skeleton.skinText[i * skeleton.skinsize * 3 + j * 3 + 0] > color / 2)
4275                         skeleton.skinText[i * skeleton.skinsize * 3 + j * 3 + 0] = color / 2;
4276                     skeleton.skinText[i * skeleton.skinsize * 3 + j * 3 + 1] = 0;
4277                     skeleton.skinText[i * skeleton.skinsize * 3 + j * 3 + 2] = 0;
4278                 }
4279             }
4280         }
4281         if (detail > 1) {
4282             skeleton.drawmodel.textureptr.bind();
4283             DoMipmaps();
4284         }
4285
4286         if (!skeleton.free) {
4287             bleedy -= 4 / realtexdetail;
4288             if (detail == 2)
4289                 bleedx += (abs(Random() % 3) - 1) * 2 / realtexdetail;
4290             else
4291                 bleedx += (abs(Random() % 3) - 1) * 4 / realtexdetail;
4292         }
4293         if (skeleton.free) {
4294             bleedx += 4 * direction / realtexdetail;
4295             if (detail == 2)
4296                 bleedy += (abs(Random() % 3) - 1) * 2 / realtexdetail;
4297             else
4298                 bleedy += (abs(Random() % 3) - 1) * 4 / realtexdetail;
4299         }
4300     }
4301
4302     if (abs(righthandmorphness - targetrighthandmorphness) < multiplier * 4) {
4303         righthandmorphness = targetrighthandmorphness;
4304         righthandmorphstart = righthandmorphend;
4305     } else if (righthandmorphness > targetrighthandmorphness) {
4306         righthandmorphness -= multiplier * 4;
4307     } else if (righthandmorphness < targetrighthandmorphness) {
4308         righthandmorphness += multiplier * 4;
4309     }
4310
4311     if (abs(lefthandmorphness - targetlefthandmorphness) < multiplier * 4) {
4312         lefthandmorphness = targetlefthandmorphness;
4313         lefthandmorphstart = lefthandmorphend;
4314     } else if (lefthandmorphness > targetlefthandmorphness) {
4315         lefthandmorphness -= multiplier * 4;
4316     } else if (lefthandmorphness < targetlefthandmorphness) {
4317         lefthandmorphness += multiplier * 4;
4318     }
4319
4320     if (creature == rabbittype || targettailmorphness == 5 || targettailmorphness == 0) {
4321         if (abs(tailmorphness - targettailmorphness) < multiplier * 10) {
4322             tailmorphness = targettailmorphness;
4323             tailmorphstart = tailmorphend;
4324         } else if (tailmorphness > targettailmorphness) {
4325             tailmorphness -= multiplier * 10;
4326         } else if (tailmorphness < targettailmorphness) {
4327             tailmorphness += multiplier * 10;
4328         }
4329     }
4330
4331     if (creature == wolftype) {
4332         if (abs(tailmorphness - targettailmorphness) < multiplier * 4) {
4333             tailmorphness = targettailmorphness;
4334             tailmorphstart = tailmorphend;
4335         } else if (tailmorphness > targettailmorphness) {
4336             tailmorphness -= multiplier * 2;
4337         } else if (tailmorphness < targettailmorphness) {
4338             tailmorphness += multiplier * 2;
4339         }
4340     }
4341
4342     if (headmorphend == 3 || headmorphstart == 3) {
4343         if (abs(headmorphness - targetheadmorphness) < multiplier * 7) {
4344             headmorphness = targetheadmorphness;
4345             headmorphstart = headmorphend;
4346         } else if (headmorphness > targetheadmorphness) {
4347             headmorphness -= multiplier * 7;
4348         } else if (headmorphness < targetheadmorphness) {
4349             headmorphness += multiplier * 7;
4350         }
4351     } else if (headmorphend == 5 || headmorphstart == 5) {
4352         if (abs(headmorphness - targetheadmorphness) < multiplier * 10) {
4353             headmorphness = targetheadmorphness;
4354             headmorphstart = headmorphend;
4355         } else if (headmorphness > targetheadmorphness) {
4356             headmorphness -= multiplier * 10;
4357         } else if (headmorphness < targetheadmorphness) {
4358             headmorphness += multiplier * 10;
4359         }
4360     } else {
4361         if (abs(headmorphness - targetheadmorphness) < multiplier * 4) {
4362             headmorphness = targetheadmorphness;
4363             headmorphstart = headmorphend;
4364         } else if (headmorphness > targetheadmorphness) {
4365             headmorphness -= multiplier * 4;
4366         } else if (headmorphness < targetheadmorphness) {
4367             headmorphness += multiplier * 4;
4368         }
4369     }
4370
4371     if (abs(chestmorphness - targetchestmorphness) < multiplier) {
4372         chestmorphness = targetchestmorphness;
4373         chestmorphstart = chestmorphend;
4374     } else if (chestmorphness > targetchestmorphness) {
4375         chestmorphness -= multiplier;
4376     } else if (chestmorphness < targetchestmorphness) {
4377         chestmorphness += multiplier;
4378     }
4379
4380     if (dead != 2 && howactive <= typesleeping) {
4381         if (chestmorphstart == 0 && chestmorphend == 0) {
4382             chestmorphness = 0;
4383             targetchestmorphness = 1;
4384             chestmorphend = 3;
4385         }
4386         if (chestmorphstart != 0 && chestmorphend != 0) {
4387             chestmorphness = 0;
4388             targetchestmorphness = 1;
4389             chestmorphend = 0;
4390             if (environment == snowyenvironment) {
4391                 XYZ footpoint;
4392                 XYZ footvel;
4393                 if (!skeleton.free)
4394                     footvel = DoRotation(skeleton.specialforward[0], 0, yaw, 0) * -1;
4395                 if (skeleton.free)
4396                     footvel = skeleton.specialforward[0] * -1;
4397                 if (!skeleton.free)
4398                     footpoint = DoRotation((jointPos(head) + jointPos(neck)) / 2, 0, yaw, 0) * scale + coords;
4399                 if (skeleton.free)
4400                     footpoint = ((jointPos(head) + jointPos(neck)) / 2) * scale + coords;
4401                 if (animTarget == sleepanim)
4402                     footvel = DoRotation(footvel, 0, 90, 0);
4403                 Sprite::MakeSprite(breathsprite, footpoint + footvel * .2, footvel * .4, 1, 1, 1, .4, .3);
4404             }
4405         }
4406
4407         if (!dead && howactive < typesleeping) {
4408             blinkdelay -= multiplier * 2;
4409             if (headmorphstart == 0 && headmorphend == 0 && blinkdelay <= 0) {
4410                 headmorphness = 0;
4411                 targetheadmorphness = 1;
4412                 headmorphend = 3;
4413                 blinkdelay = (float)(abs(Random() % 40)) / 5;
4414             }
4415             if (headmorphstart == 3 && headmorphend == 3) {
4416                 headmorphness = 0;
4417                 targetheadmorphness = 1;
4418                 headmorphend = 0;
4419             }
4420         }
4421         if (!dead) {
4422             twitchdelay -= multiplier * 1.5;
4423             if (animTarget != hurtidleanim) {
4424                 if (headmorphstart == 0 && headmorphend == 0 && twitchdelay <= 0) {
4425                     headmorphness = 0;
4426                     targetheadmorphness = 1;
4427                     headmorphend = 5;
4428                     twitchdelay = (float)(abs(Random() % 40)) / 5;
4429                 }
4430                 if (headmorphstart == 5 && headmorphend == 5) {
4431                     headmorphness = 0;
4432                     targetheadmorphness = 1;
4433                     headmorphend = 0;
4434                 }
4435             }
4436             if ((isIdle() || isCrouch()) && animTarget != hurtidleanim) {
4437                 twitchdelay3 -= multiplier * 1;
4438                 if (Random() % 2 == 0) {
4439                     if (righthandmorphstart == 0 && righthandmorphend == 0 && twitchdelay3 <= 0) {
4440                         righthandmorphness = 0;
4441                         targetrighthandmorphness = 1;
4442                         righthandmorphend = 1;
4443                         if (Random() % 2 == 0)twitchdelay3 = (float)(abs(Random() % 40)) / 5;
4444                     }
4445                     if (righthandmorphstart == 1 && righthandmorphend == 1) {
4446                         righthandmorphness = 0;
4447                         targetrighthandmorphness = 1;
4448                         righthandmorphend = 0;
4449                     }
4450                 }
4451                 if (Random() % 2 == 0) {
4452                     if (lefthandmorphstart == 0 && lefthandmorphend == 0 && twitchdelay3 <= 0) {
4453                         lefthandmorphness = 0;
4454                         targetlefthandmorphness = 1;
4455                         lefthandmorphend = 1;
4456                         twitchdelay3 = (float)(abs(Random() % 40)) / 5;
4457                     }
4458                     if (lefthandmorphstart == 1 && lefthandmorphend == 1) {
4459                         lefthandmorphness = 0;
4460                         targetlefthandmorphness = 1;
4461                         lefthandmorphend = 0;
4462                     }
4463                 }
4464             }
4465         }
4466         if (!dead) {
4467             if (creature == rabbittype) {
4468                 if (howactive < typesleeping)
4469                     twitchdelay2 -= multiplier * 1.5;
4470                 else
4471                     twitchdelay2 -= multiplier * 0.5;
4472                 if (howactive <= typesleeping) {
4473                     if (tailmorphstart == 0 && tailmorphend == 0 && twitchdelay2 <= 0) {
4474                         tailmorphness = 0;
4475                         targettailmorphness = 1;
4476                         tailmorphend = 1;
4477                         twitchdelay2 = (float)(abs(Random() % 40)) / 5;
4478                     }
4479                     if (tailmorphstart == 1 && tailmorphend == 1) {
4480                         tailmorphness = 0;
4481                         targettailmorphness = 1;
4482                         tailmorphend = 2;
4483                     }
4484                     if (tailmorphstart == 2 && tailmorphend == 2) {
4485                         tailmorphness = 0;
4486                         targettailmorphness = 1;
4487                         tailmorphend = 0;
4488                     }
4489                 }
4490             }
4491         }
4492     }
4493     if (creature == wolftype) {
4494         twitchdelay2 -= multiplier * 1.5;
4495         if (tailmorphend != 0)
4496             if ((isRun() || animTarget == jumpupanim || animTarget == jumpdownanim || animTarget == backflipanim) && !skeleton.free) {
4497                 tailmorphness = 0;
4498                 targettailmorphness = 1;
4499                 tailmorphend = 0;
4500                 twitchdelay2 = .1;
4501             }
4502         if (tailmorphend != 5)
4503             if (animTarget == flipanim || animTarget == frontflipanim || animTarget == rollanim || skeleton.free) {
4504                 tailmorphness = 0;
4505                 targettailmorphness = 1;
4506                 tailmorphend = 5;
4507                 twitchdelay2 = .1;
4508             }
4509         if (twitchdelay2 <= 0) {
4510             if (((tailmorphstart == 0 && tailmorphend == 0) || (tailmorphstart == 5 && tailmorphend == 5))) {
4511                 tailmorphness = 0;
4512                 targettailmorphness = 1;
4513                 tailmorphend = 1;
4514             }
4515             if (tailmorphstart == 1 && tailmorphend == 1) {
4516                 tailmorphness = 0;
4517                 targettailmorphness = 1;
4518                 tailmorphend = 2;
4519             }
4520             if (tailmorphstart == 2 && tailmorphend == 2) {
4521                 tailmorphness = 0;
4522                 targettailmorphness = 1;
4523                 tailmorphend = 3;
4524             }
4525             if (tailmorphstart == 3 && tailmorphend == 3) {
4526                 tailmorphness = 0;
4527                 targettailmorphness = 1;
4528                 tailmorphend = 4;
4529             }
4530             if (tailmorphstart == 4 && tailmorphend == 4) {
4531                 tailmorphness = 0;
4532                 targettailmorphness = 1;
4533                 tailmorphend = 1;
4534             }
4535         }
4536     }
4537
4538     if (dead != 1)
4539         unconscioustime = 0;
4540
4541     if (dead == 1 || howactive == typesleeping) {
4542         unconscioustime += multiplier;
4543         //If unconscious, close eyes and mouth
4544         if (righthandmorphend != 0)
4545             righthandmorphness = 0;
4546         righthandmorphend = 0;
4547         targetrighthandmorphness = 1;
4548
4549         if (lefthandmorphend != 0)
4550             lefthandmorphness = 0;
4551         lefthandmorphend = 0;
4552         targetlefthandmorphness = 1;
4553
4554         if (headmorphend != 3 && headmorphend != 5)
4555             headmorphness = 0;
4556         headmorphend = 3;
4557         targetheadmorphness = 1;
4558     }
4559
4560
4561     if (howactive > typesleeping) {
4562         XYZ headpoint;
4563         headpoint = coords;
4564         if (bloodtoggle && !bled) {
4565             terrain.MakeDecal(blooddecalslow, headpoint, .8, .5, 0);
4566         }
4567         if (bloodtoggle && !bled)
4568             for (l = 0; l < terrain.patchobjectnum[whichpatchx][whichpatchz]; l++) {
4569                 j = terrain.patchobjects[whichpatchx][whichpatchz][l];
4570                 XYZ point = DoRotation(headpoint - objects.position[j], 0, -objects.yaw[j], 0);
4571                 float size = .8;
4572                 float opacity = .6;
4573                 float yaw = 0;
4574                 objects.model[j].MakeDecal(blooddecalslow, &point, &size, &opacity, &yaw);
4575             }
4576         bled = 1;
4577     }
4578
4579     if (dead == 2 || howactive > typesleeping) {
4580         //If dead, open mouth and hands
4581         if (righthandmorphend != 0)
4582             righthandmorphness = 0;
4583         righthandmorphend = 0;
4584         targetrighthandmorphness = 1;
4585
4586         if (lefthandmorphend != 0)
4587             lefthandmorphness = 0;
4588         lefthandmorphend = 0;
4589         targetlefthandmorphness = 1;
4590
4591         if (headmorphend != 2)
4592             headmorphness = 0;
4593         headmorphend = 2;
4594         targetheadmorphness = 1;
4595     }
4596
4597     if (stunned > 0 && !dead && headmorphend != 2) {
4598         if (headmorphend != 4)
4599             headmorphness = 0;
4600         headmorphend = 4;
4601         targetheadmorphness = 1;
4602     }
4603
4604     if (damage > damagetolerance && !dead) {
4605
4606         dead = 1;
4607         unconscioustime = 0;
4608
4609         if (creature == wolftype) {
4610             award_bonus(0, Wolfbonus);
4611         }
4612
4613         RagDoll(0);
4614
4615         if (weaponactive != -1) {
4616             weapons[weaponids[0]].drop(velocity * scale * -.3, velocity * scale);
4617             weapons[weaponids[0]].velocity.x += .01;
4618             num_weapons--;
4619             if (num_weapons) {
4620                 weaponids[0] = weaponids[num_weapons];
4621                 if (weaponstuck == num_weapons)
4622                     weaponstuck = 0;
4623             }
4624             weaponactive = -1;
4625             for (unsigned i = 0; i < Person::players.size(); i++) {
4626                 Person::players[i]->wentforweapon = 0;
4627             }
4628         }
4629
4630
4631
4632         if ((id == 0 || distsq(&coords, &viewer) < 50) && autoslomo) {
4633             slomo = 1;
4634             slomodelay = .2;
4635         }
4636
4637         damage += 20;
4638     }
4639
4640     if (!dead)
4641         damage -= multiplier * 13;
4642     if (!dead)
4643         permanentdamage -= multiplier * 4;
4644     if (isIdle() || isCrouch()) {
4645         if (!dead)
4646             permanentdamage -= multiplier * 4;
4647     }
4648     if (damage < 0)
4649         damage = 0;
4650     if (permanentdamage < 0)
4651         permanentdamage = 0;
4652     if (superpermanentdamage < 0)
4653         superpermanentdamage = 0;
4654     if (permanentdamage < superpermanentdamage) {
4655         permanentdamage = superpermanentdamage;
4656     }
4657     if (damage < permanentdamage) {
4658         damage = permanentdamage;
4659     }
4660     if (dead == 1 && damage < damagetolerance) {
4661         dead = 0;
4662         skeleton.free = 1;
4663         damage -= 20;
4664         for (int i = 0; i < skeleton.num_joints; i++) {
4665             skeleton.joints[i].velocity = 0;
4666         }
4667     }
4668     if (permanentdamage > damagetolerance && dead != 2) {
4669         DoBlood(1, 255);
4670
4671         if (weaponactive != -1) {
4672             weapons[weaponids[0]].drop(velocity * scale * -.3, velocity * scale);
4673             weapons[weaponids[0]].velocity.x += .01;
4674             num_weapons--;
4675             if (num_weapons) {
4676                 weaponids[0] = weaponids[num_weapons];
4677                 if (weaponstuck == num_weapons)
4678                     weaponstuck = 0;
4679             }
4680             weaponactive = -1;
4681             for (unsigned i = 0; i < Person::players.size(); i++) {
4682                 Person::players[i]->wentforweapon = 0;
4683             }
4684         }
4685
4686         bled = 0;
4687
4688         if (!dead && creature == wolftype) {
4689             award_bonus(0, Wolfbonus);
4690         }
4691
4692         if (unconscioustime < .1 && (bonus != spinecrusher || bonustime > 1) && (bonus != FinishedBonus || bonustime > 1) && bloodloss < damagetolerance)
4693             award_bonus(id, touchofdeath);
4694         if (id != 0 && unconscioustime > .1) {
4695             numafterkill++;
4696         }
4697
4698         dead = 2;
4699
4700         skeleton.free = 1;
4701
4702         emit_sound_at(breaksound, coords);
4703     }
4704
4705     if (skeleton.free == 1) {
4706         if (id == 0)
4707             pause_sound(whooshsound);
4708
4709         if (!dead) {
4710             //If knocked over, open hands and close mouth
4711             if (righthandmorphend != 0)
4712                 righthandmorphness = 0;
4713             righthandmorphend = 0;
4714             targetrighthandmorphness = 1;
4715
4716             if (lefthandmorphend != 0)
4717                 lefthandmorphness = 0;
4718             lefthandmorphend = 0;
4719             targetlefthandmorphness = 1;
4720
4721             if (headmorphend != 3 && headmorphend != 5 && headmorphstart != 3 && headmorphstart != 5) {
4722                 if (headmorphend != 0)
4723                     headmorphness = 0;
4724                 headmorphend = 0;
4725                 targetheadmorphness = 1;
4726             }
4727         }
4728
4729         skeleton.DoGravity(&scale);
4730         float damageamount;
4731         damageamount = skeleton.DoConstraints(&coords, &scale) * 5;
4732         if (damage > damagetolerance - damageamount && !dead && (bonus != spinecrusher || bonustime > 1) && (bonus != style || bonustime > 1) && (bonus != cannon || bonustime > 1))
4733             award_bonus(id, deepimpact);
4734         DoDamage(damageamount / ((protectionhigh + protectionhead + protectionlow) / 3));
4735
4736         average = 0;
4737         howmany = 0;
4738         for (j = 0; j < skeleton.num_joints; j++) {
4739             average += skeleton.joints[j].position;
4740             howmany++;
4741         }
4742         average /= howmany;
4743         coords += average * scale;
4744         for (j = 0; j < skeleton.num_joints; j++) {
4745             skeleton.joints[j].position -= average;
4746         }
4747         average /= multiplier;
4748
4749         //velocity=jointVel(groin)*scale;
4750         velocity = 0;
4751         for (int i = 0; i < skeleton.num_joints; i++) {
4752             velocity += skeleton.joints[i].velocity * scale;
4753         }
4754         velocity /= skeleton.num_joints;
4755
4756         if (!isnormal(velocity.x) && velocity.x) {
4757             velocity = 0;
4758         }
4759
4760         if (findLength(&average) < 10 && dead && skeleton.free) {
4761             skeleton.longdead += (2000 - findLength(&average)) * multiplier + multiplier;
4762             if (skeleton.longdead > 2000) {
4763                 if (skeleton.longdead > 6000) {
4764                     if (id == 0)
4765                         pause_sound(whooshsound);
4766                     skeleton.free = 3;
4767                     DrawSkeleton();
4768                     skeleton.free = 2;
4769                 }
4770                 if (dead == 2 && bloodloss < damagetolerance) {
4771                     XYZ headpoint;
4772                     headpoint = (jointPos(head) + jointPos(neck)) / 2 * scale + coords;
4773                     DoBlood(1, 255);
4774                     if (bloodtoggle && !bled) {
4775                         terrain.MakeDecal(blooddecal, headpoint, .2 * 1.2, .5, 0);
4776                     }
4777                     if (bloodtoggle && !bled)
4778                         for (l = 0; l < terrain.patchobjectnum[whichpatchx][whichpatchz]; l++) {
4779                             j = terrain.patchobjects[whichpatchx][whichpatchz][l];
4780                             XYZ point = DoRotation(headpoint - objects.position[j], 0, -objects.yaw[j], 0);
4781                             float size = .2 * 1.2;
4782                             float opacity = .6;
4783                             float yaw = 0;
4784                             objects.model[j].MakeDecal(blooddecal, &point, &size, &opacity, &yaw);
4785                         }
4786                     bled = 1;
4787                 }
4788                 if (dead == 2 && bloodloss >= damagetolerance) {
4789                     XYZ headpoint;
4790                     headpoint = (jointPos(abdomen) + jointPos(neck)) / 2 * scale + coords;
4791                     if (bleeding <= 0)
4792                         DoBlood(1, 255);
4793                     if (bloodtoggle && !bled) {
4794                         terrain.MakeDecal(blooddecalslow, headpoint, .8, .5, 0);
4795                     }
4796                     if (bloodtoggle && !bled)
4797                         for (l = 0; l < terrain.patchobjectnum[whichpatchx][whichpatchz]; l++) {
4798                             j = terrain.patchobjects[whichpatchx][whichpatchz][l];
4799                             XYZ point = DoRotation(headpoint - objects.position[j], 0, -objects.yaw[j], 0);
4800                             float size = .8;
4801                             float opacity = .6;
4802                             float yaw = 0;
4803                             objects.model[j].MakeDecal(blooddecalslow, &point, &size, &opacity, &yaw);
4804                         }
4805                     bled = 1;
4806                 }
4807             }
4808         }
4809
4810         if (!dead && crouchkeydown && skeleton.freetime > .5 && id == 0 && skeleton.free) {
4811             bool canrecover = 1;
4812             XYZ startpoint, endpoint, colpoint, colviewer, coltarget;
4813             startpoint = coords;
4814             endpoint = coords;
4815             endpoint.y -= .7;
4816             if (terrain.lineTerrain(startpoint, endpoint, &colpoint) != -1)
4817                 canrecover = 0;
4818             if (velocity.y < -30)
4819                 canrecover = 0;
4820             for (i = 0; i < objects.numobjects; i++) {
4821                 if (objects.type[i] != treeleavestype && objects.type[i] != bushtype && objects.type[i] != firetype) {
4822                     colviewer = startpoint;
4823                     coltarget = endpoint;
4824                     if (objects.model[i].LineCheck(&colviewer, &coltarget, &colpoint, &objects.position[i], &objects.yaw[i]) != -1)
4825                         canrecover = 0;
4826                 }
4827             }
4828             if (canrecover) {
4829                 skeleton.free = 0;
4830                 XYZ middle;
4831                 middle = 0;
4832
4833                 terrainnormal = jointPos(groin) - jointPos(abdomen);
4834                 if (joint(groin).locked && joint(abdomen).locked) {
4835                     terrainnormal = jointPos(groin) - jointPos(abdomen);
4836                     middle = (jointPos(groin) + jointPos(abdomen)) / 2;
4837                 }
4838                 if (joint(abdomen).locked && joint(neck).locked) {
4839                     terrainnormal = jointPos(abdomen) - jointPos(neck);
4840                     middle = (jointPos(neck) + jointPos(abdomen)) / 2;
4841                 }
4842                 if (joint(groin).locked && joint(neck).locked) {
4843                     terrainnormal = jointPos(groin) - jointPos(neck);
4844                     middle = (jointPos(groin) + jointPos(neck)) / 2;
4845                 }
4846                 Normalise(&terrainnormal);
4847
4848                 targetyaw = -asin(0 - terrainnormal.x);
4849                 targetyaw *= 360 / 6.28;
4850                 if (terrainnormal.z < 0)
4851                     targetyaw = 180 - targetyaw;
4852                 yaw = targetyaw;
4853
4854                 frameTarget = 0;
4855                 animTarget = flipanim;
4856                 crouchtogglekeydown = 1;
4857                 target = 0;
4858                 tilt2 = 0;
4859                 targettilt2 = 0;
4860
4861                 animCurrent = tempanim;
4862                 frameCurrent = 0;
4863                 target = 0;
4864
4865                 for (int i = 0; i < skeleton.num_joints; i++) {
4866                     tempanimation.position[i][0] = skeleton.joints[i].position;
4867                     tempanimation.position[i][0] = DoRotation(tempanimation.position[i][0], 0, -yaw, 0);
4868                 }
4869             }
4870         }
4871
4872         if (findLength(&average) < 10 && !dead && skeleton.free) {
4873             skeleton.longdead += (2000 - findLength(&average)) * multiplier + multiplier;
4874             if (skeleton.longdead > (damage + 500) * 1.5) {
4875                 if (id == 0)
4876                     pause_sound(whooshsound);
4877                 skeleton.free = 0;
4878                 velocity = 0;
4879                 XYZ middle;
4880                 middle = 0;
4881
4882                 terrainnormal = jointPos(groin) - jointPos(abdomen);
4883                 if (joint(groin).locked && joint(abdomen).locked) {
4884                     terrainnormal = jointPos(groin) - jointPos(abdomen);
4885                     middle = (jointPos(groin) + jointPos(abdomen)) / 2;
4886                 }
4887                 if (joint(abdomen).locked && joint(neck).locked) {
4888                     terrainnormal = jointPos(abdomen) - jointPos(neck);
4889                     middle = (jointPos(neck) + jointPos(abdomen)) / 2;
4890                 }
4891                 if (joint(groin).locked && joint(neck).locked) {
4892                     terrainnormal = jointPos(groin) - jointPos(neck);
4893                     middle = (jointPos(groin) + jointPos(neck)) / 2;
4894                 }
4895                 Normalise(&terrainnormal);
4896
4897                 targetyaw = -asin(0 - terrainnormal.x);
4898                 targetyaw *= 360 / 6.28;
4899                 if (terrainnormal.z < 0)
4900                     targetyaw = 180 - targetyaw;
4901                 yaw = targetyaw;
4902
4903                 targettilt2 = asin(terrainnormal.y) * 180 / 3.14 * -1;
4904
4905
4906                 if (skeleton.forward.y < 0) {
4907                     animTarget = getupfrombackanim;
4908                     frameTarget = 0;
4909                     targettilt2 = 0;
4910                 }
4911                 if (skeleton.forward.y > -.3) {
4912                     animTarget = getupfromfrontanim;
4913                     yaw += 180;
4914                     targetyaw += 180;
4915                     targettilt2 *= -1;
4916                     frameTarget = 0;
4917                     targettilt2 = 0;
4918                 }
4919
4920                 if ((Random() % 8 == 0 && id != 0 && creature == rabbittype) || (Random() % 2 == 0 && id != 0 && creature == wolftype) || (id == 0 && crouchkeydown && (forwardkeydown || backkeydown || leftkeydown || rightkeydown))) {
4921                     animTarget = rollanim;
4922                     targetyaw = lookyaw;
4923                     if (id == 0) {
4924                         if (rightkeydown) {
4925                             targetyaw -= 90;
4926                             if (forwardkeydown)
4927                                 targetyaw += 45;
4928                             if (backkeydown)
4929                                 targetyaw -= 45;
4930                         }
4931                         if (leftkeydown) {
4932                             targetyaw += 90;
4933                             if (forwardkeydown)
4934                                 targetyaw -= 45;
4935                             if (backkeydown)
4936                                 targetyaw += 45;
4937                         }
4938                         if (backkeydown) {
4939                             if ( !leftkeydown && !rightkeydown)
4940                                 targetyaw += 180;
4941                         }
4942                         targetyaw += 180;
4943                     }
4944                 }
4945
4946                 if (abs(targettilt2) > 50)
4947                     targettilt2 = 0;
4948                 animCurrent = tempanim;
4949                 frameCurrent = 0;
4950                 target = 0;
4951                 tilt2 = targettilt2;
4952
4953                 if (middle.y > 0 && animTarget != rollanim)
4954                     targetoffset.y = middle.y + 1;
4955
4956                 for (int i = 0; i < skeleton.num_joints; i++) {
4957                     tempanimation.position[i][0] = skeleton.joints[i].position;
4958                     tempanimation.position[i][0] = DoRotation(tempanimation.position[i][0], 0, -yaw, 0);
4959                 }
4960             }
4961         }
4962
4963         bool hasstaff;
4964         hasstaff = 0;
4965         if (num_weapons > 0)
4966             if (weapons[0].getType() == staff)
4967                 hasstaff = 1;
4968         if (!skeleton.freefall && freefall && ((jumpkeydown && jumpkeydowntime < .2) || (hasstaff && rabbitkickragdoll)) && !dead) {
4969             if (velocity.y > -30) {
4970                 XYZ tempvelocity;
4971                 tempvelocity = velocity;
4972                 Normalise(&tempvelocity);
4973                 targetyaw = -asin(0 - tempvelocity.x);
4974                 targetyaw *= 360 / 6.28;
4975                 if (velocity.z < 0)
4976                     targetyaw = 180 - targetyaw;
4977                 //targetyaw+=180;
4978
4979                 skeleton.free = 0;
4980                 if (dotproduct(&skeleton.forward, &tempvelocity) < 0) {
4981                     animTarget = rollanim;
4982                     frameTarget = 2;
4983                 } else {
4984                     animTarget = backhandspringanim;
4985                     targetyaw += 180;
4986                     frameTarget = 6;
4987                 }
4988                 target = 0;
4989
4990                 emit_sound_at(movewhooshsound, coords, 128.);
4991
4992                 animCurrent = animTarget;
4993                 frameCurrent = frameTarget - 1;
4994                 target = 0;
4995
4996                 velocity = 0;
4997
4998                 yaw = targetyaw;
4999                 tilt = 0;
5000                 targettilt = 0;
5001                 tilt2 = 0;
5002                 targettilt2 = 0;
5003             }
5004         }
5005         if (skeleton.freefall == 0)
5006             freefall = 0;
5007
5008     }
5009
5010     if (aitype != passivetype || skeleton.free == 1)
5011         if (findLengthfast(&velocity) > .1)
5012             for (i = 0; i < objects.numobjects; i++) {
5013                 if (objects.type[i] == firetype)
5014                     if (distsqflat(&coords, &objects.position[i]) < objects.scale[i]*objects.scale[i] * 12 && distsq(&coords, &objects.position[i]) < objects.scale[i]*objects.scale[i] * 49) {
5015                         if (onfire) {
5016                             if (!objects.onfire[i]) {
5017                                 emit_sound_at(firestartsound, objects.position[i]);
5018                             }
5019                             objects.onfire[i] = 1;
5020                         }
5021                         if (!onfire) {
5022                             if (objects.onfire[i]) {
5023                                 CatchFire();
5024                             }
5025                         }
5026                     }
5027                 if (objects.type[i] == bushtype)
5028                     if (distsqflat(&coords, &objects.position[i]) < objects.scale[i]*objects.scale[i] * 12 && distsq(&coords, &objects.position[i]) < objects.scale[i]*objects.scale[i] * 49) {
5029                         if (onfire) {
5030                             if (!objects.onfire[i]) {
5031                                 emit_sound_at(firestartsound, objects.position[i]);
5032                             }
5033                             objects.onfire[i] = 1;
5034                         }
5035
5036                         if (!onfire) {
5037                             if (objects.onfire[i]) {
5038                                 CatchFire();
5039                             }
5040                         }
5041                         if (objects.messedwith[i] <= 0) {
5042                             XYZ tempvel;
5043                             XYZ pos;
5044
5045                             emit_sound_at(bushrustle, coords, 40 * findLength(&velocity));
5046
5047                             if (id == 0) {
5048                                 envsound[numenvsounds] = coords;
5049                                 envsoundvol[numenvsounds] = 4 * findLength(&velocity);
5050                                 envsoundlife[numenvsounds] = .4;
5051                                 numenvsounds++;
5052                             }
5053
5054                             int howmany;
5055                             if (environment == grassyenvironment)
5056                                 howmany = findLength(&velocity) * 4;
5057                             if (environment == snowyenvironment)
5058                                 howmany = findLength(&velocity) * 2;
5059                             if (detail == 2)
5060                                 if (environment != desertenvironment)
5061                                     for (j = 0; j < howmany; j++) {
5062                                         tempvel.x = float(abs(Random() % 100) - 50) / 20;
5063                                         tempvel.y = float(abs(Random() % 100) - 50) / 20;
5064                                         tempvel.z = float(abs(Random() % 100) - 50) / 20;
5065                                         pos = coords;
5066                                         pos.y += 1;
5067                                         pos.x += float(abs(Random() % 100) - 50) / 200;
5068                                         pos.y += float(abs(Random() % 100) - 50) / 200;
5069                                         pos.z += float(abs(Random() % 100) - 50) / 200;
5070                                         Sprite::MakeSprite(splintersprite, pos, tempvel * .5 + velocity * float(abs(Random() % 100)) / 100, 165 / 255 + float(abs(Random() % 100) - 50) / 400, 0, 0, .2 + float(abs(Random() % 100) - 50) / 1300, 1);
5071                                         Sprite::setLastSpriteSpecial(1);
5072                                     }
5073                             howmany = findLength(&velocity) * 4;
5074                             if (detail == 2)
5075                                 if (environment == snowyenvironment)
5076                                     for (j = 0; j < howmany; j++) {
5077                                         tempvel.x = float(abs(Random() % 100) - 50) / 20;
5078                                         tempvel.y = float(abs(Random() % 100) - 50) / 20;
5079                                         tempvel.z = float(abs(Random() % 100) - 50) / 20;
5080                                         pos = coords;
5081                                         pos.y += 1;
5082                                         pos.x += float(abs(Random() % 100) - 50) / 200;
5083                                         pos.y += float(abs(Random() % 100) - 50) / 200;
5084                                         pos.z += float(abs(Random() % 100) - 50) / 200;
5085                                         Sprite::MakeSprite(splintersprite, pos, tempvel * .3 + velocity * float(abs(Random() % 100)) / 100 / 2, 1, 1, 1, .1, 1);
5086                                         Sprite::setLastSpriteSpecial(2);
5087                                     }
5088                         }
5089                         objects.rotx[i] += velocity.x * multiplier * 6;
5090                         objects.roty[i] += velocity.z * multiplier * 6;
5091                         objects.messedwith[i] = .5;
5092                     }
5093                 XYZ tempcoord;
5094                 if (objects.type[i] == treeleavestype && environment != desertenvironment) {
5095                     if (objects.pitch[i] == 0)
5096                         tempcoord = coords;
5097                     else {
5098                         tempcoord = coords - objects.position[i];
5099                         tempcoord = DoRotation(tempcoord, 0, -objects.yaw[i], 0);
5100                         tempcoord = DoRotation(tempcoord, -objects.pitch[i], 0, 0);
5101                         tempcoord += objects.position[i];
5102                     }
5103                     if (distsqflat(&tempcoord, &objects.position[i]) < objects.scale[i]*objects.scale[i] * 8 && distsq(&tempcoord, &objects.position[i]) < objects.scale[i]*objects.scale[i] * 300 && tempcoord.y > objects.position[i].y + 3 * objects.scale[i]) {
5104                         if (objects.messedwith[i] <= 0) {
5105                             XYZ tempvel;
5106                             XYZ pos;
5107
5108                             emit_sound_at(bushrustle, coords, 40 * findLength(&velocity));
5109
5110                             if (id == 0) {
5111                                 envsound[numenvsounds] = coords;
5112                                 envsoundvol[numenvsounds] = 4 * findLength(&velocity);
5113                                 envsoundlife[numenvsounds] = .4;
5114                                 numenvsounds++;
5115                             }
5116
5117                             int howmany;
5118                             if (environment == grassyenvironment)
5119                                 howmany = findLength(&velocity) * 4;
5120                             if (environment == snowyenvironment)
5121                                 howmany = findLength(&velocity) * 2;
5122                             if (detail == 2)
5123                                 if (environment != desertenvironment)
5124                                     for (j = 0; j < howmany; j++) {
5125                                         tempvel.x = float(abs(Random() % 100) - 50) / 20;
5126                                         tempvel.y = float(abs(Random() % 100) - 50) / 20;
5127                                         tempvel.z = float(abs(Random() % 100) - 50) / 20;
5128                                         pos = coords;
5129                                         pos += velocity * .1;
5130                                         pos.y += 1;
5131                                         pos.x += float(abs(Random() % 100) - 50) / 150;
5132                                         pos.y += float(abs(Random() % 100) - 50) / 150;
5133                                         pos.z += float(abs(Random() % 100) - 50) / 150;
5134                                         Sprite::MakeSprite(splintersprite, pos, tempvel * .5 + velocity * float(abs(Random() % 100)) / 100, 165 / 255 + float(abs(Random() % 100) - 50) / 400, 0, 0, .2 + float(abs(Random() % 100) - 50) / 1300, 1);
5135                                         Sprite::setLastSpriteSpecial(1);
5136                                     }
5137                             howmany = findLength(&velocity) * 4;
5138                             if (detail == 2)
5139                                 if (environment == snowyenvironment)
5140                                     for (j = 0; j < howmany; j++) {
5141                                         tempvel.x = float(abs(Random() % 100) - 50) / 20;
5142                                         tempvel.y = float(abs(Random() % 100) - 50) / 20;
5143                                         tempvel.z = float(abs(Random() % 100) - 50) / 20;
5144                                         pos = coords;
5145                                         pos += velocity * .1;
5146                                         pos.y += 1;
5147                                         pos.x += float(abs(Random() % 100) - 50) / 150;
5148                                         pos.y += float(abs(Random() % 100) - 50) / 150;
5149                                         pos.z += float(abs(Random() % 100) - 50) / 150;
5150                                         Sprite::MakeSprite(splintersprite, pos, tempvel * .3 + velocity * float(abs(Random() % 100)) / 100 / 2, 1, 1, 1, .1, 1);
5151                                         Sprite::setLastSpriteSpecial(2);
5152                                     }
5153                         }
5154                         objects.messedwith[i] = .5;
5155                     }
5156                 }
5157             }
5158
5159     if (!skeleton.free) {
5160         bool play;
5161         play = 0;
5162         if ((stunned > 0 || surprised > 0) && Person::players.size() > 2 && aitype != passivetype)
5163             play = 1;
5164         if (hasvictim)
5165             if (aitype != passivetype && victim->skeleton.free && !victim->dead)
5166                 play = 1;
5167         if (tutoriallevel == 1 && id != 0)
5168             play = 0;
5169         if (play && aitype != playercontrolled) {
5170             int whichsound = -1;
5171             i = abs(Random() % 4);
5172             if (speechdelay <= 0) {
5173                 if (creature == rabbittype) {
5174                     if (i == 0)
5175                         whichsound = rabbitchitter;
5176                     if (i == 1)
5177                         whichsound = rabbitchitter2;
5178                 }
5179                 if (creature == wolftype) {
5180                     if (i == 0)
5181                         whichsound = growlsound;
5182                     if (i == 1)
5183                         whichsound = growl2sound;
5184                 }
5185             }
5186             speechdelay = .3;
5187
5188             if (whichsound != -1) {
5189                 emit_sound_at(whichsound, coords);
5190             }
5191         }
5192
5193         if (animTarget == staggerbackhighanim)
5194             staggerdelay = 1;
5195         if (animTarget == staggerbackhardanim)
5196             staggerdelay = 1;
5197         staggerdelay -= multiplier;
5198         if (animTarget != crouchstabanim && animTarget != swordgroundstabanim && animTarget != staffgroundsmashanim)
5199             hasvictim = 1;
5200         if (velocity.y < -30 && animTarget == jumpdownanim)
5201             RagDoll(0);
5202         if (animCurrent != getIdle() && wasIdle() && animTarget != getIdle() && isIdle()) {
5203             animTarget = getIdle();
5204             frameTarget = 0;
5205             target = 0;
5206         }
5207         weaponmissdelay -= multiplier;
5208         highreversaldelay -= multiplier;
5209         lowreversaldelay -= multiplier;
5210         lastcollide -= multiplier;
5211         skiddelay -= multiplier;
5212         if (!isnormal(velocity.x) && velocity.x) {
5213             velocity = 0;
5214         }
5215         if (!isnormal(targettilt) && targettilt) {
5216             targettilt = 0;
5217         }
5218         if (!isnormal(targettilt2) && targettilt2) {
5219             targettilt2 = 0;
5220         }
5221         if (!isnormal(targetyaw) && targetyaw) {
5222             targetyaw = 0;
5223         }
5224
5225         if (animTarget == bounceidleanim || animTarget == wolfidle || animTarget == walkanim || animTarget == drawrightanim || animTarget == crouchdrawrightanim || animTarget == drawleftanim || animTarget == fightidleanim || animTarget == fightsidestep || animTarget == hanganim || isCrouch() || animTarget == backhandspringanim) {
5226             //open hands and close mouth
5227             if (righthandmorphend != 0 && righthandmorphness == targetrighthandmorphness) {
5228                 righthandmorphness = 0;
5229                 righthandmorphend = 0;
5230                 targetrighthandmorphness = 1;
5231             }
5232
5233             if (lefthandmorphend != 0 && lefthandmorphness == targetlefthandmorphness) {
5234                 lefthandmorphness = 0;
5235                 lefthandmorphend = 0;
5236                 targetlefthandmorphness = 1;
5237             }
5238
5239             if (headmorphend != 3 && headmorphend != 5 && headmorphstart != 3 && headmorphstart != 5 && headmorphend != 0 && headmorphness == targetheadmorphness) {
5240                 headmorphness = 0;
5241                 headmorphend = 0;
5242                 targetheadmorphness = 1;
5243             }
5244         }
5245
5246         if (animTarget == rollanim || animTarget == dodgebackanim || animTarget == removeknifeanim || animTarget == knifefightidleanim || animTarget == swordfightidleanim || animTarget == blockhighleftstrikeanim || animTarget == crouchremoveknifeanim || animTarget == sneakanim || animTarget == sweepanim || animTarget == spinkickreversedanim || animTarget == jumpdownanim || isWallJump() || isFlip() || animTarget == climbanim || isRun() || animTarget == getupfrombackanim || animTarget == getupfromfrontanim) {
5247             //open hands and mouth
5248             if (righthandmorphend != 0 && righthandmorphness == targetrighthandmorphness) {
5249                 righthandmorphness = 0;
5250                 righthandmorphend = 0;
5251                 targetrighthandmorphness = 1;
5252             }
5253
5254             if (lefthandmorphend != 0 && lefthandmorphness == targetlefthandmorphness) {
5255                 lefthandmorphness = 0;
5256                 lefthandmorphend = 0;
5257                 targetlefthandmorphness = 1;
5258             }
5259
5260             if (headmorphend != 1 && headmorphness == targetheadmorphness) {
5261                 headmorphness = 0;
5262                 headmorphend = 1;
5263                 targetheadmorphness = 1;
5264             }
5265         }
5266
5267         if (animTarget == jumpupanim || animTarget == crouchstabanim || animTarget == swordgroundstabanim || animTarget == swordfightidlebothanim || animTarget == blockhighleftanim) {
5268             //close hands and mouth
5269             if (righthandmorphend != 1 && righthandmorphness == targetrighthandmorphness) {
5270                 righthandmorphness = 0;
5271                 righthandmorphend = 1;
5272                 targetrighthandmorphness = 1;
5273             }
5274
5275             if (lefthandmorphend != 1 && lefthandmorphness == targetlefthandmorphness) {
5276                 lefthandmorphness = 0;
5277                 lefthandmorphend = 1;
5278                 targetlefthandmorphness = 1;
5279             }
5280
5281             if (headmorphend != 0 && headmorphness == targetheadmorphness) {
5282                 headmorphness = 0;
5283                 headmorphend = 0;
5284                 targetheadmorphness = 1;
5285             }
5286         }
5287
5288         if (animTarget == spinkickanim || animTarget == staffspinhitreversalanim || animTarget == staffspinhitreversedanim || animTarget == staffhitreversalanim || animTarget == staffhitreversedanim || animTarget == hurtidleanim || animTarget == winduppunchanim || animTarget == swordslashreversalanim || animTarget == swordslashreversedanim || animTarget == knifeslashreversalanim || animTarget == knifeslashreversedanim || animTarget == knifethrowanim || animTarget == knifefollowanim || animTarget == knifefollowedanim || animTarget == killanim || animTarget == dropkickanim || animTarget == upunchanim || animTarget == knifeslashstartanim || animTarget == swordslashanim || animTarget == staffhitanim || animTarget == staffspinhitanim || animTarget == staffgroundsmashanim || animTarget == spinkickreversalanim || animTarget == sweepreversalanim || animTarget == lowkickanim || animTarget == sweepreversedanim || animTarget == rabbitkickreversalanim || animTarget == rabbitkickreversedanim || animTarget == jumpreversalanim || animTarget == jumpreversedanim) {
5289             //close hands and yell
5290             if (righthandmorphend != 1 && righthandmorphness == targetrighthandmorphness) {
5291                 righthandmorphness = 0;
5292                 righthandmorphend = 1;
5293                 targetrighthandmorphness = 1;
5294             }
5295
5296             if (lefthandmorphend != 1 && lefthandmorphness == targetlefthandmorphness) {
5297                 lefthandmorphness = 0;
5298                 lefthandmorphend = 1;
5299                 targetlefthandmorphness = 1;
5300             }
5301
5302             if (headmorphend != 2 && headmorphness == targetheadmorphness) {
5303                 headmorphness = 1;
5304                 headmorphend = 2;
5305                 targetheadmorphness = 1;
5306             }
5307         }
5308
5309         bool behind;
5310         behind = 0;
5311         if (hasvictim) {
5312             if ((victim != this->shared_from_this()) && !victim->dead && (victim->aitype != passivetype) &&
5313                 (victim->aitype != searchtype) && (aitype != passivetype) &&
5314                 (aitype != searchtype) && (victim->id < Person::players.size()) && (aitype != passivetype)) {
5315                 behind = (normaldotproduct(facing, coords - victim->coords) > 0);
5316             }
5317         }
5318
5319         if (!dead && animTarget != hurtidleanim)
5320             if (behind || animTarget == killanim || animTarget == knifethrowanim || animTarget == knifefollowanim || animTarget == spinkickreversalanim || animTarget == rabbitkickreversedanim || animTarget == jumpreversedanim) {
5321                 if (headmorphend != 4 || headmorphness == targetheadmorphness) {
5322                     headmorphend = 4;
5323                     //headmorphness=1;
5324                     targetheadmorphness = 1;
5325                 }
5326             }
5327
5328         if (weaponactive != -1) {
5329             if (weapons[weaponids[weaponactive]].getType() != staff) {
5330                 righthandmorphstart = 1;
5331                 righthandmorphend = 1;
5332             }
5333             if (weapons[weaponids[weaponactive]].getType() == staff) {
5334                 righthandmorphstart = 2;
5335                 righthandmorphend = 2;
5336             }
5337             targetrighthandmorphness = 1;
5338         }
5339
5340         terrainnormal = terrain.getNormal(coords.x, coords.z);
5341
5342         if (animation[animTarget].attack != reversal) {
5343             if (!isnormal(coords.x))
5344                 coords = oldcoords;
5345             oldcoords = coords;
5346         }
5347
5348         flatfacing = 0;
5349         flatfacing.z = 1;
5350
5351         flatfacing = DoRotation(flatfacing, 0, yaw, 0);
5352         facing = flatfacing;
5353         ReflectVector(&facing, terrainnormal);
5354         Normalise(&facing);
5355
5356         if (isRun() || animTarget == sneakanim || animTarget == rollanim || animTarget == walkanim) {
5357             if (onterrain)
5358                 targettilt2 = -facing.y * 20;
5359             else
5360                 targettilt2 = 0;
5361         }
5362         onterrain = 0;
5363         if (!isRun() && !animation[animTarget].attack && animTarget != getupfromfrontanim && animTarget != getupfrombackanim && animTarget != sneakanim)
5364             targettilt2 = 0;
5365         if (animTarget == jumpupanim || animTarget == jumpdownanim || isFlip()) {
5366             flatvelocity = velocity;
5367             flatvelocity.y = 0;
5368             flatvelspeed = findLength(&flatvelocity);
5369             targettilt = flatvelspeed * fast_sqrt(abs(velocity.y) * .7) * normaldotproduct(DoRotation(flatfacing, 0, -90, 0), flatvelocity);
5370             targettilt2 = flatvelspeed * fast_sqrt(abs(velocity.y) * .7) * normaldotproduct(flatfacing, flatvelocity);
5371             if (velocity.y < 0)
5372                 targettilt2 *= -1;
5373             if (velocity.y < 0)
5374                 targettilt *= -1;
5375             if (targettilt > 25)
5376                 targettilt = 25;
5377             if (targettilt < -25)
5378                 targettilt = -25;
5379         }
5380
5381         if (targettilt2 > 45)
5382             targettilt2 = 45;
5383         if (targettilt2 < -45)
5384             targettilt2 = -45;
5385         if (abs(tilt2 - targettilt2) < multiplier * 400)
5386             tilt2 = targettilt2;
5387         else if (tilt2 > targettilt2) {
5388             tilt2 -= multiplier * 400;
5389         } else if (tilt2 < targettilt2) {
5390             tilt2 += multiplier * 400;
5391         }
5392         if (!animation[animTarget].attack && animTarget != getupfrombackanim && animTarget != getupfromfrontanim) {
5393             if (tilt2 > 25)
5394                 tilt2 = 25;
5395             if (tilt2 < -25)
5396                 tilt2 = -25;
5397         }
5398
5399         if (!isnormal(targettilt) && targettilt) {
5400             targettilt = 0;
5401         }
5402         if (!isnormal(targettilt2) && targettilt2) {
5403             targettilt2 = 0;
5404         }
5405
5406         //Running velocity
5407         if (animTarget == rabbittackleanim) {
5408             velocity += facing * multiplier * speed * 700 * scale;
5409             velspeed = findLength(&velocity);
5410             if (velspeed > speed * 65 * scale) {
5411                 velocity /= velspeed;
5412                 velspeed = speed * 65 * scale;
5413                 velocity *= velspeed;
5414             }
5415             velocity.y += gravity * multiplier * 20;
5416             ReflectVector(&velocity, terrain.getNormal(coords.x, coords.z));
5417             velspeed = findLength(&velocity);
5418             velocity = flatfacing * velspeed;
5419         }
5420         if (animTarget != rabbitrunninganim && animTarget != wolfrunninganim) {
5421             if (isRun() || animTarget == rabbitkickanim) {
5422                 velocity += facing * multiplier * speed * 700 * scale;
5423                 velspeed = findLength(&velocity);
5424                 if (velspeed > speed * 45 * scale) {
5425                     velocity /= velspeed;
5426                     velspeed = speed * 45 * scale;
5427                     velocity *= velspeed;
5428                 }
5429                 velocity.y += gravity * multiplier * 20;
5430                 ReflectVector(&velocity, terrain.getNormal(coords.x, coords.z));
5431                 velspeed = findLength(&velocity);
5432                 if (velspeed < speed * 30 * scale)
5433                     velspeed = speed * 30 * scale;
5434                 velocity = flatfacing * velspeed;
5435             }
5436         } else if (isRun()) {
5437             velocity += facing * multiplier * speed * 700 * scale;
5438             velspeed = findLength(&velocity);
5439             if (creature == rabbittype) {
5440                 if (velspeed > speed * 55 * scale) {
5441                     velocity /= velspeed;
5442                     velspeed = speed * 55 * scale;
5443                     velocity *= velspeed;
5444                 }
5445             }
5446             if (creature == wolftype) {
5447                 if (velspeed > speed * 75 * scale) {
5448                     velocity /= velspeed;
5449                     velspeed = speed * 75 * scale;
5450                     velocity *= velspeed;
5451                 }
5452             }
5453             velocity.y += gravity * multiplier * 20;
5454             ReflectVector(&velocity, terrain.getNormal(coords.x, coords.z));
5455             velspeed = findLength(&velocity);
5456             velocity = flatfacing * velspeed;
5457         }
5458
5459         if (animTarget == rollanim && animation[animTarget].label[frameTarget] != 6) {
5460             velocity += facing * multiplier * speed * 700 * scale;
5461             velspeed = findLength(&velocity);
5462             if (velspeed > speed * 45 * scale) {
5463                 velocity /= velspeed;
5464                 velspeed = speed * 45 * scale;
5465                 velocity *= velspeed;
5466             }
5467             velocity.y += gravity * multiplier * 20;
5468             ReflectVector(&velocity, terrain.getNormal(coords.x, coords.z));
5469             velspeed = findLength(&velocity);
5470             velocity = flatfacing * velspeed;
5471         }
5472
5473         if (animTarget == sneakanim || animTarget == walkanim) {
5474             velocity += facing * multiplier * speed * 700 * scale;
5475             velspeed = findLength(&velocity);
5476             if (velspeed > speed * 12 * scale) {
5477                 velocity /= velspeed;
5478                 velspeed = speed * 12 * scale;
5479                 velocity *= velspeed;
5480             }
5481             velocity.y += gravity * multiplier * 20;
5482             ReflectVector(&velocity, terrain.getNormal(coords.x, coords.z));
5483             velspeed = findLength(&velocity);
5484             velocity = flatfacing * velspeed;
5485         }
5486
5487         if ((animTarget == fightidleanim || animTarget == knifefightidleanim) && (animCurrent == bounceidleanim || animCurrent == hurtidleanim)) {
5488             velocity += facing * multiplier * speed * 700 * scale;
5489             velspeed = findLength(&velocity);
5490             if (velspeed > speed * 2 * scale) {
5491                 velocity /= velspeed;
5492                 velspeed = speed * 2 * scale;
5493                 velocity *= velspeed;
5494             }
5495             velocity.y += gravity * multiplier * 20;
5496             ReflectVector(&velocity, terrain.getNormal(coords.x, coords.z));
5497             velspeed = findLength(&velocity);
5498             velocity = flatfacing * velspeed;
5499         }
5500
5501
5502         if ((animTarget == bounceidleanim || animCurrent == hurtidleanim) && (animCurrent == fightidleanim || animCurrent == knifefightidleanim)) {
5503             velocity -= facing * multiplier * speed * 700 * scale;
5504             velspeed = findLength(&velocity);
5505             if (velspeed > speed * 2 * scale) {
5506                 velocity /= velspeed;
5507                 velspeed = speed * 2 * scale;
5508                 velocity *= velspeed;
5509             }
5510             velocity.y += gravity * multiplier * 20;
5511             ReflectVector(&velocity, terrain.getNormal(coords.x, coords.z));
5512             velspeed = findLength(&velocity);
5513             velocity = flatfacing * velspeed * -1;
5514         }
5515
5516         if (animTarget == fightsidestep) {
5517             velocity += DoRotation(facing * multiplier * speed * 700 * scale, 0, -90, 0);
5518             velspeed = findLength(&velocity);
5519             if (velspeed > speed * 12 * scale) {
5520                 velocity /= velspeed;
5521                 velspeed = speed * 12 * scale;
5522                 velocity *= velspeed;
5523             }
5524             velocity.y += gravity * multiplier * 20;
5525             ReflectVector(&velocity, terrain.getNormal(coords.x, coords.z));
5526             velspeed = findLength(&velocity);
5527             velocity = DoRotation(flatfacing * velspeed, 0, -90, 0);
5528         }
5529
5530         if (animTarget == staggerbackhighanim) {
5531             coords -= facing * multiplier * speed * 16 * scale;
5532             velocity = 0;
5533         }
5534         if (animTarget == staggerbackhardanim && animation[staggerbackhardanim].label[frameTarget] != 6) {
5535             coords -= facing * multiplier * speed * 20 * scale;
5536             velocity = 0;
5537         }
5538
5539         if (animTarget == backhandspringanim) {
5540             //coords-=facing*multiplier*50*scale;
5541             velocity += facing * multiplier * speed * 700 * scale * -1;
5542             velspeed = findLength(&velocity);
5543             if (velspeed > speed * 50 * scale) {
5544                 velocity /= velspeed;
5545                 velspeed = speed * 50 * scale;
5546                 velocity *= velspeed;
5547             }
5548             velocity.y += gravity * multiplier * 20;
5549             ReflectVector(&velocity, terrain.getNormal(coords.x, coords.z));
5550             velspeed = findLength(&velocity);
5551             velocity = flatfacing * velspeed * -1;
5552         }
5553         if (animTarget == dodgebackanim) {
5554             //coords-=facing*multiplier*50*scale;
5555             velocity += facing * multiplier * speed * 700 * scale * -1;
5556             velspeed = findLength(&velocity);
5557             if (velspeed > speed * 60 * scale) {
5558                 velocity /= velspeed;
5559                 velspeed = speed * 60 * scale;
5560                 velocity *= velspeed;
5561             }
5562             velocity.y += gravity * multiplier * 20;
5563             ReflectVector(&velocity, terrain.getNormal(coords.x, coords.z));
5564             velspeed = findLength(&velocity);
5565             velocity = flatfacing * velspeed * -1;
5566         }
5567
5568         if (animTarget == jumpupanim || animTarget == jumpdownanim || isFlip()) {
5569             velspeed = findLength(&velocity);
5570         }
5571
5572
5573         if (animTarget == jumpupanim || animTarget == jumpdownanim || isFlip()) {
5574             velocity.y += gravity * multiplier;
5575         }
5576
5577         if (animTarget != climbanim && animTarget != hanganim && !isWallJump())
5578             coords += velocity * multiplier;
5579
5580         if (coords.y < terrain.getHeight(coords.x, coords.z) && (animTarget == jumpdownanim || animTarget == jumpupanim || isFlip())) {
5581             if (isFlip() && animation[animTarget].label[frameTarget] == 7)
5582                 RagDoll(0);
5583
5584             if (animTarget == jumpupanim) {
5585                 jumppower = -4;
5586                 animTarget = getIdle();
5587             }
5588             target = 0;
5589             frameTarget = 0;
5590             onterrain = 1;
5591
5592             if (id == 0) {
5593                 pause_sound(whooshsound);
5594                 OPENAL_SetVolume(channels[whooshsound], 0);
5595             }
5596
5597             if (animTarget == jumpdownanim || isFlip()) {
5598                 if (isFlip())jumppower = -4;
5599                 animTarget = getLanding();
5600                 emit_sound_at(landsound, coords, 128.);
5601
5602                 if (id == 0) {
5603                     envsound[numenvsounds] = coords;
5604                     envsoundvol[numenvsounds] = 16;
5605                     envsoundlife[numenvsounds] = .4;
5606                     numenvsounds++;
5607                 }
5608             }
5609         }
5610
5611         if (animTarget != jumpupanim && animTarget != jumpdownanim && !isFlip() && animTarget != climbanim && animTarget != hanganim && !isWallJump())
5612             coords.y += gravity * multiplier * 2;
5613         if (animTarget != jumpupanim && animTarget != jumpdownanim && !isFlip() && coords.y < terrain.getHeight(coords.x, coords.z)) {
5614             coords.y = terrain.getHeight(coords.x, coords.z);
5615             onterrain = 1;
5616         }
5617
5618
5619         if (isIdle() || animTarget == drawrightanim || animTarget == drawleftanim || animTarget == crouchdrawrightanim || animTarget == crouchstabanim || animTarget == swordgroundstabanim || isStop() || animTarget == removeknifeanim || animTarget == crouchremoveknifeanim || isLanding() || isCrouch() || animation[animTarget].attack || (animTarget == rollanim && animation[animTarget].label[frameTarget] == 6)) {
5620             velspeed = findLength(&velocity);
5621             velocity.y = 0;
5622             if (velspeed < multiplier * 300 * scale) {
5623                 velocity = 0;
5624             } else
5625                 velocity -= velocity / velspeed * multiplier * 300 * scale;
5626             if (velspeed > 5 && (isLanding() || isLandhard())) {
5627                 skiddingdelay += multiplier;
5628                 if (skiddelay <= 0) {
5629                     FootLand(0, .5);
5630                     FootLand(1, .5);
5631                     skiddelay = .02;
5632                 }
5633             } else
5634                 skiddingdelay = 0;
5635         }
5636
5637         if (isLandhard()) {
5638             velspeed = findLength(&velocity);
5639             velocity.y = 0;
5640             if (velspeed < multiplier * 600 * scale) {
5641                 velocity = 0;
5642             } else
5643                 velocity -= velocity / velspeed * multiplier * 600 * scale;
5644             velocity = 0;
5645             if (velspeed > 5 && (isLanding() || isLandhard())) {
5646                 skiddingdelay += multiplier;
5647                 if (skiddelay <= 0) {
5648                     FootLand(0, .5);
5649                     FootLand(1, .5);
5650                     skiddelay = .02;
5651                 }
5652             } else
5653                 skiddingdelay = 0;
5654         }
5655
5656         if (skiddingdelay < 0)
5657             skiddingdelay += multiplier;
5658         if (skiddingdelay > .02 && !forwardkeydown && !backkeydown && !leftkeydown && !rightkeydown && !jumpkeydown && isLanding() && !landhard) {
5659             skiddingdelay = -1;
5660             if (!onterrain || environment == grassyenvironment) {
5661                 emit_sound_at(skidsound, coords, 128 * velspeed / 10);
5662             } else {
5663                 emit_sound_at(snowskidsound, coords, 128 * velspeed / 10);
5664             }
5665         }
5666
5667         if (animation[animTarget].attack == normalattack && animTarget != rabbitkickanim && !victim->skeleton.free) {
5668             terrainnormal = victim->coords - coords;
5669             Normalise(&terrainnormal);
5670             targetyaw = -asin(0 - terrainnormal.x);
5671             targetyaw *= 360 / 6.28;
5672             if (terrainnormal.z < 0)
5673                 targetyaw = 180 - targetyaw;
5674             targettilt2 = -asin(terrainnormal.y) * 360 / 6.28; //*-70;
5675         }
5676
5677         if (animation[animTarget].attack == reversal && animTarget != rabbittacklinganim) {
5678             targetyaw = victim->targetyaw;
5679         }
5680         if (animTarget == rabbittacklinganim) {
5681             coords = victim->coords;
5682         }
5683     }
5684     skeleton.oldfree = skeleton.free;
5685
5686     XYZ midterrain;
5687     midterrain = 0;
5688     midterrain.x = terrain.size * terrain.scale / 2;
5689     midterrain.z = terrain.size * terrain.scale / 2;
5690     if (distsqflat(&coords, &midterrain) > (terrain.size * terrain.scale / 2 - viewdistance) * (terrain.size * terrain.scale / 2 - viewdistance)) {
5691         XYZ tempposit;
5692         tempposit = coords - midterrain;
5693         tempposit.y = 0;
5694         Normalise(&tempposit);
5695         tempposit *= (terrain.size * terrain.scale / 2 - viewdistance);
5696         coords.x = tempposit.x + midterrain.x;
5697         coords.z = tempposit.z + midterrain.z;
5698     }
5699 }
5700
5701
5702 /* EFFECT
5703  * inverse kinematics helper function
5704  */
5705 void IKHelper(Person *p, float interp)
5706 {
5707     XYZ point, newpoint, change, change2;
5708     float heightleft, heightright;
5709
5710     // TODO: implement localToWorld and worldToLocal
5711     //       but keep in mind it won't be the same math if player is ragdolled or something
5712     //       - localToWorldStanding / worldToLocalStanding (or crouching or...?)
5713     //       then comb through code for places where to use it
5714
5715     // point = localToWorld(jointPos(leftfoot))
5716     point = DoRotation(p->jointPos(leftfoot), 0, p->yaw, 0) * p->scale + p->coords;
5717     // adjust height of foot
5718     heightleft = terrain.getHeight(point.x, point.z) + .04;
5719     point.y = heightleft;
5720     change = p->jointPos(leftankle) - p->jointPos(leftfoot);
5721     change2 = p->jointPos(leftknee) - p->jointPos(leftfoot);
5722     // jointPos(leftfoot) = interpolate(worldToLocal(point), jointPos(leftfoot), interp)
5723     p->jointPos(leftfoot) = DoRotation((point - p->coords) / p->scale, 0, -p->yaw, 0) * interp + p->jointPos(leftfoot) * (1 - interp);
5724     // move ankle along with foot
5725     p->jointPos(leftankle) = p->jointPos(leftfoot) + change;
5726     // average knee pos between old and new pos
5727     p->jointPos(leftknee) = (p->jointPos(leftfoot) + change2) / 2 + (p->jointPos(leftknee)) / 2;
5728
5729     // do same as above for right leg
5730     point = DoRotation(p->jointPos(rightfoot), 0, p->yaw, 0) * p->scale + p->coords;
5731     heightright = terrain.getHeight(point.x, point.z) + .04;
5732     point.y = heightright;
5733     change = p->jointPos(rightankle) - p->jointPos(rightfoot);
5734     change2 = p->jointPos(rightknee) - p->jointPos(rightfoot);
5735     p->jointPos(rightfoot) = DoRotation((point - p->coords) / p->scale, 0, -p->yaw, 0) * interp + p->jointPos(rightfoot) * (1 - interp);
5736     p->jointPos(rightankle) = p->jointPos(rightfoot) + change;
5737     p->jointPos(rightknee) = (p->jointPos(rightfoot) + change2) / 2 + (p->jointPos(rightknee)) / 2;
5738
5739     // fix up skeleton now that we've moved body parts?
5740     p->skeleton.DoConstraints(&p->coords, &p->scale);
5741 }
5742
5743 /* EFFECT
5744  * MONSTER
5745  * TODO: ???
5746  */
5747 int Person::DrawSkeleton()
5748 {
5749     int oldplayerdetail;
5750     if ((frustum.SphereInFrustum(coords.x, coords.y + scale * 3, coords.z, scale * 8) && distsq(&viewer, &coords) < viewdistance * viewdistance) || skeleton.free == 3) {
5751         if (onterrain && (isIdle() || isCrouch() || wasIdle() || wasCrouch()) && !skeleton.free) {
5752             calcrot = 1;
5753         }
5754
5755         if (headless) {
5756             headmorphness = 0;
5757             headmorphstart = 6;
5758             headmorphend = 6;
5759         }
5760
5761         glAlphaFunc(GL_GREATER, 0.0001);
5762         XYZ terrainlight;
5763         float terrainheight;
5764         float distance;
5765         if (!isnormal(yaw))
5766             yaw = 0;
5767         if (!isnormal(tilt))
5768             tilt = 0;
5769         if (!isnormal(tilt2))
5770             tilt2 = 0;
5771         oldplayerdetail = playerdetail;
5772         playerdetail = 0;
5773         if (distsq(&viewer, &coords) < viewdistance * viewdistance / 32 && detail == 2) {
5774             playerdetail = 1;
5775         }
5776         if (distsq(&viewer, &coords) < viewdistance * viewdistance / 128 && detail == 1) {
5777             playerdetail = 1;
5778         }
5779         if (distsq(&viewer, &coords) < viewdistance * viewdistance / 256 && (detail != 1 && detail != 2)) {
5780             playerdetail = 1;
5781         }
5782         if (id == 0)
5783             playerdetail = 1;
5784         if (playerdetail != oldplayerdetail) {
5785             updatedelay = 0;
5786             normalsupdatedelay = 0;
5787         }
5788         static float updatedelaychange;
5789         static float morphness;
5790         static float framemult;
5791         if (calcrot) {
5792             skeleton.FindForwards();
5793             if (howactive == typesittingwall) {
5794                 skeleton.specialforward[1] = 0;
5795                 skeleton.specialforward[1].z = 1;
5796             }
5797         }
5798         static XYZ mid;
5799         static float M[16];
5800         static int i, j, k;
5801         static int weaponattachmuscle;
5802         static int weaponrotatemuscle;
5803         static XYZ weaponpoint;
5804         static int start, endthing;
5805         if ((dead != 2 || skeleton.free != 2) && updatedelay <= 0) {
5806             if (!isSleeping() && !isSitting()) {
5807                 // TODO: give these meaningful names
5808                 const bool cond1 = (isIdle() || isCrouch() || isLanding() || isLandhard()
5809                                     || animTarget == drawrightanim || animTarget == drawleftanim || animTarget == crouchdrawrightanim);
5810                 const bool cond2 = (wasIdle() || wasCrouch() || wasLanding() || wasLandhard()
5811                                     || animCurrent == drawrightanim || animCurrent == drawleftanim || animCurrent == crouchdrawrightanim);
5812
5813                 if (onterrain && (cond1 && cond2) && !skeleton.free) {
5814                     IKHelper(this, 1);
5815                     if (creature == wolftype)
5816                         IKHelper(this, 1);
5817                 }
5818
5819                 if (onterrain && (cond1 && !cond2) && !skeleton.free) {
5820                     IKHelper(this, target);
5821                     if (creature == wolftype)
5822                         IKHelper(this, target);
5823                 }
5824
5825                 if (onterrain && (!cond1 && cond2) && !skeleton.free) {
5826                     IKHelper(this, 1 - target);
5827                     if (creature == wolftype)
5828                         IKHelper(this, 1 - target);
5829                 }
5830             }
5831
5832             if (!skeleton.free && (!animation[animTarget].attack && animTarget != getupfrombackanim && ((animTarget != rollanim && !isFlip()) || animation[animTarget].label[frameTarget] == 6) && animTarget != getupfromfrontanim && animTarget != wolfrunninganim && animTarget != rabbitrunninganim && animTarget != backhandspringanim && animTarget != walljumpfrontanim && animTarget != hurtidleanim && !isLandhard() && !isSleeping()))
5833                 DoHead();
5834             else {
5835                 targetheadyaw = -targetyaw;
5836                 targetheadpitch = 0;
5837                 if (animation[animTarget].attack == 3)
5838                     targetheadyaw += 180;
5839             }
5840             for (i = 0; i < skeleton.drawmodel.vertexNum; i++) {
5841                 skeleton.drawmodel.vertex[i] = 0;
5842                 skeleton.drawmodel.vertex[i].y = 999;
5843             }
5844             for (i = 0; i < skeleton.drawmodellow.vertexNum; i++) {
5845                 skeleton.drawmodellow.vertex[i] = 0;
5846                 skeleton.drawmodellow.vertex[i].y = 999;
5847             }
5848             for (i = 0; i < skeleton.drawmodelclothes.vertexNum; i++) {
5849                 skeleton.drawmodelclothes.vertex[i] = 0;
5850                 skeleton.drawmodelclothes.vertex[i].y = 999;
5851             }
5852             for (int i = 0; i < skeleton.num_muscles; i++) {
5853                 // convenience renames
5854                 const int p1 = skeleton.muscles[i].parent1->label;
5855                 const int p2 = skeleton.muscles[i].parent2->label;
5856
5857                 if ((skeleton.muscles[i].numvertices > 0 && playerdetail) || (skeleton.muscles[i].numverticeslow > 0 && !playerdetail)) {
5858                     morphness = 0;
5859                     start = 0;
5860                     endthing = 0;
5861
5862                     if (p1 == righthand || p2 == righthand) {
5863                         morphness = righthandmorphness;
5864                         start = righthandmorphstart;
5865                         endthing = righthandmorphend;
5866                     }
5867                     if (p1 == lefthand || p2 == lefthand) {
5868                         morphness = lefthandmorphness;
5869                         start = lefthandmorphstart;
5870                         endthing = lefthandmorphend;
5871                     }
5872                     if (p1 == head || p2 == head) {
5873                         morphness = headmorphness;
5874                         start = headmorphstart;
5875                         endthing = headmorphend;
5876                     }
5877                     if ((p1 == neck && p2 == abdomen) || (p2 == neck && p1 == abdomen)) {
5878                         morphness = chestmorphness;
5879                         start = chestmorphstart;
5880                         endthing = chestmorphend;
5881                     }
5882                     if ((p1 == groin && p2 == abdomen) || (p2 == groin && p1 == abdomen)) {
5883                         morphness = tailmorphness;
5884                         start = tailmorphstart;
5885                         endthing = tailmorphend;
5886                     }
5887                     if (calcrot)
5888                         skeleton.FindRotationMuscle(i, animTarget);
5889                     mid = (skeleton.muscles[i].parent1->position + skeleton.muscles[i].parent2->position) / 2;
5890                     glMatrixMode(GL_MODELVIEW);
5891                     glPushMatrix();
5892                     glLoadIdentity();
5893                     if (!skeleton.free)
5894                         glRotatef(tilt2, 1, 0, 0);
5895                     if (!skeleton.free)
5896                         glRotatef(tilt, 0, 0, 1);
5897
5898
5899                     glTranslatef(mid.x, mid.y, mid.z);
5900
5901                     skeleton.muscles[i].lastrotate1 = skeleton.muscles[i].rotate1;
5902                     glRotatef(-skeleton.muscles[i].lastrotate1 + 90, 0, 1, 0);
5903
5904                     skeleton.muscles[i].lastrotate2 = skeleton.muscles[i].rotate2;
5905                     glRotatef(-skeleton.muscles[i].lastrotate2 + 90, 0, 0, 1);
5906
5907                     skeleton.muscles[i].lastrotate3 = skeleton.muscles[i].rotate3;
5908                     glRotatef(-skeleton.muscles[i].lastrotate3, 0, 1, 0);
5909
5910                     if (playerdetail || skeleton.free == 3) {
5911                         for (j = 0; j < skeleton.muscles[i].numvertices; j++) {
5912                             XYZ &v0 = skeleton.model[start].vertex[skeleton.muscles[i].vertices[j]];
5913                             XYZ &v1 = skeleton.model[endthing].vertex[skeleton.muscles[i].vertices[j]];
5914                             glMatrixMode(GL_MODELVIEW);
5915                             glPushMatrix();
5916                             if (p1 == abdomen || p2 == abdomen)
5917                                 glTranslatef((v0.x * (1 - morphness) + v1.x * morphness) * proportionbody.x,
5918                                              (v0.y * (1 - morphness) + v1.y * morphness) * proportionbody.y,
5919                                              (v0.z * (1 - morphness) + v1.z * morphness) * proportionbody.z);
5920                             if (p1 == lefthand || p1 == righthand || p1 == leftwrist || p1 == rightwrist || p1 == leftelbow || p1 == rightelbow || p2 == leftelbow || p2 == rightelbow)
5921                                 glTranslatef((v0.x * (1 - morphness) + v1.x * morphness) * proportionarms.x,
5922                                              (v0.y * (1 - morphness) + v1.y * morphness) * proportionarms.y,
5923                                              (v0.z * (1 - morphness) + v1.z * morphness) * proportionarms.z);
5924                             if (p1 == leftfoot || p1 == rightfoot || p1 == leftankle || p1 == rightankle || p1 == leftknee || p1 == rightknee || p2 == leftknee || p2 == rightknee)
5925                                 glTranslatef((v0.x * (1 - morphness) + v1.x * morphness) * proportionlegs.x,
5926                                              (v0.y * (1 - morphness) + v1.y * morphness) * proportionlegs.y,
5927                                              (v0.z * (1 - morphness) + v1.z * morphness) * proportionlegs.z);
5928                             if (p1 == head || p2 == head)
5929                                 glTranslatef((v0.x * (1 - morphness) + v1.x * morphness) * proportionhead.x,
5930                                              (v0.y * (1 - morphness) + v1.y * morphness) * proportionhead.y,
5931                                              (v0.z * (1 - morphness) + v1.z * morphness) * proportionhead.z);
5932                             glGetFloatv(GL_MODELVIEW_MATRIX, M);
5933                             skeleton.drawmodel.vertex[skeleton.muscles[i].vertices[j]].x = M[12] * scale;
5934                             skeleton.drawmodel.vertex[skeleton.muscles[i].vertices[j]].y = M[13] * scale;
5935                             skeleton.drawmodel.vertex[skeleton.muscles[i].vertices[j]].z = M[14] * scale;
5936                             glPopMatrix();
5937                         }
5938                     }
5939                     if (!playerdetail || skeleton.free == 3) {
5940                         for (j = 0; j < skeleton.muscles[i].numverticeslow; j++) {
5941                             XYZ &v0 = skeleton.modellow.vertex[skeleton.muscles[i].verticeslow[j]];
5942                             glMatrixMode(GL_MODELVIEW);
5943                             glPushMatrix();
5944                             if (p1 == abdomen || p2 == abdomen)
5945                                 glTranslatef(v0.x * proportionbody.x,
5946                                              v0.y * proportionbody.y,
5947                                              v0.z * proportionbody.z);
5948                             if (p1 == lefthand || p1 == righthand || p1 == leftwrist || p1 == rightwrist || p1 == leftelbow || p1 == rightelbow || p2 == leftelbow || p2 == rightelbow)
5949                                 glTranslatef(v0.x * proportionarms.x,
5950                                              v0.y * proportionarms.y,
5951                                              v0.z * proportionarms.z);
5952                             if (p1 == leftfoot || p1 == rightfoot || p1 == leftankle || p1 == rightankle || p1 == leftknee || p1 == rightknee || p2 == leftknee || p2 == rightknee)
5953                                 glTranslatef(v0.x * proportionlegs.x,
5954                                              v0.y * proportionlegs.y,
5955                                              v0.z * proportionlegs.z);
5956                             if (p1 == head || p2 == head)
5957                                 glTranslatef(v0.x * proportionhead.x,
5958                                              v0.y * proportionhead.y,
5959                                              v0.z * proportionhead.z);
5960
5961                             glGetFloatv(GL_MODELVIEW_MATRIX, M);
5962                             skeleton.drawmodellow.vertex[skeleton.muscles[i].verticeslow[j]].x = M[12] * scale;
5963                             skeleton.drawmodellow.vertex[skeleton.muscles[i].verticeslow[j]].y = M[13] * scale;
5964                             skeleton.drawmodellow.vertex[skeleton.muscles[i].verticeslow[j]].z = M[14] * scale;
5965                             glPopMatrix();
5966                         }
5967                     }
5968                     glPopMatrix();
5969                 }
5970                 if (skeleton.clothes && skeleton.muscles[i].numverticesclothes > 0) {
5971                     mid = (skeleton.muscles[i].parent1->position + skeleton.muscles[i].parent2->position) / 2;
5972
5973                     glMatrixMode(GL_MODELVIEW);
5974                     glPushMatrix();
5975                     glLoadIdentity();
5976                     if (!skeleton.free)
5977                         glRotatef(tilt2, 1, 0, 0);
5978                     if (!skeleton.free)
5979                         glRotatef(tilt, 0, 0, 1);
5980                     glTranslatef(mid.x, mid.y, mid.z);
5981                     skeleton.muscles[i].lastrotate1 = skeleton.muscles[i].rotate1;
5982                     glRotatef(-skeleton.muscles[i].lastrotate1 + 90, 0, 1, 0);
5983
5984                     skeleton.muscles[i].lastrotate2 = skeleton.muscles[i].rotate2;
5985                     glRotatef(-skeleton.muscles[i].lastrotate2 + 90, 0, 0, 1);
5986
5987                     skeleton.muscles[i].lastrotate3 = skeleton.muscles[i].rotate3;
5988                     glRotatef(-skeleton.muscles[i].lastrotate3, 0, 1, 0);
5989
5990                     for (j = 0; j < skeleton.muscles[i].numverticesclothes; j++) {
5991                         XYZ &v0 = skeleton.modelclothes.vertex[skeleton.muscles[i].verticesclothes[j]];
5992                         glMatrixMode(GL_MODELVIEW);
5993                         glPushMatrix();
5994                         if (p1 == abdomen || p2 == abdomen)
5995                             glTranslatef(v0.x * proportionbody.x,
5996                                          v0.y * proportionbody.y,
5997                                          v0.z * proportionbody.z);
5998                         if (p1 == lefthand || p1 == righthand || p1 == leftwrist || p1 == rightwrist || p1 == leftelbow || p1 == rightelbow || p2 == leftelbow || p2 == rightelbow)
5999                             glTranslatef(v0.x * proportionarms.x,
6000                                          v0.y * proportionarms.y,
6001                                          v0.z * proportionarms.z);
6002                         if (p1 == leftfoot || p1 == rightfoot || p1 == leftankle || p1 == rightankle || p1 == leftknee || p1 == rightknee || p2 == leftknee || p2 == rightknee)
6003                             glTranslatef(v0.x * proportionlegs.x,
6004                                          v0.y * proportionlegs.y,
6005                                          v0.z * proportionlegs.z);
6006                         if (p1 == head || p2 == head)
6007                             glTranslatef(v0.x * proportionhead.x,
6008                                          v0.y * proportionhead.y,
6009                                          v0.z * proportionhead.z);
6010                         glGetFloatv(GL_MODELVIEW_MATRIX, M);
6011                         skeleton.drawmodelclothes.vertex[skeleton.muscles[i].verticesclothes[j]].x = M[12] * scale;
6012                         skeleton.drawmodelclothes.vertex[skeleton.muscles[i].verticesclothes[j]].y = M[13] * scale;
6013                         skeleton.drawmodelclothes.vertex[skeleton.muscles[i].verticesclothes[j]].z = M[14] * scale;
6014                         glPopMatrix();
6015                     }
6016                     glPopMatrix();
6017                 }
6018                 updatedelay = 1 + (float)(Random() % 100) / 1000;
6019             }
6020             if (skeleton.free != 2 && (skeleton.free == 1 || skeleton.free == 3 || id == 0 || (normalsupdatedelay <= 0) || animTarget == getupfromfrontanim || animTarget == getupfrombackanim || animCurrent == getupfromfrontanim || animCurrent == getupfrombackanim)) {
6021                 normalsupdatedelay = 1;
6022                 if (playerdetail || skeleton.free == 3)
6023                     skeleton.drawmodel.CalculateNormals(0);
6024                 if (!playerdetail || skeleton.free == 3)
6025                     skeleton.drawmodellow.CalculateNormals(0);
6026                 if (skeleton.clothes)
6027                     skeleton.drawmodelclothes.CalculateNormals(0);
6028             } else {
6029                 if (playerdetail || skeleton.free == 3)
6030                     skeleton.drawmodel.UpdateVertexArrayNoTexNoNorm();
6031                 if (!playerdetail || skeleton.free == 3)
6032                     skeleton.drawmodellow.UpdateVertexArrayNoTexNoNorm();
6033                 if (skeleton.clothes) {
6034                     skeleton.drawmodelclothes.UpdateVertexArrayNoTexNoNorm();
6035                 }
6036             }
6037         }
6038         framemult = .01;
6039         updatedelaychange = -framemult * 4 * (45 - findDistance(&viewer, &coords) * 1);
6040         if (updatedelaychange > -realmultiplier * 30)
6041             updatedelaychange = -realmultiplier * 30;
6042         if (updatedelaychange > -framemult * 4)
6043             updatedelaychange = -framemult * 4;
6044         if (skeleton.free == 1)
6045             updatedelaychange *= 6;
6046         if (id == 0)
6047             updatedelaychange *= 8;
6048         updatedelay += updatedelaychange;
6049
6050         glMatrixMode(GL_MODELVIEW);
6051         glPushMatrix();
6052         if (!skeleton.free)
6053             glTranslatef(coords.x, coords.y - .02, coords.z);
6054         if (skeleton.free)
6055             glTranslatef(coords.x, coords.y - .02, coords.z);
6056         if (!skeleton.free)
6057             glTranslatef(offset.x * scale, offset.y * scale, offset.z * scale);
6058         if (!skeleton.free)
6059             glRotatef(yaw, 0, 1, 0);
6060         if (showpoints) {
6061             glPointSize(5);
6062             glColor4f(.4, 1, .4, 1);
6063             glDisable(GL_LIGHTING);
6064             glDisable(GL_TEXTURE_2D);
6065             glBegin(GL_POINTS);
6066             if (playerdetail)
6067                 for (i = 0; i < skeleton.drawmodel.vertexNum; i++) {
6068                     XYZ &v0 = skeleton.drawmodel.vertex[i];
6069                     glVertex3f(v0.x, v0.y, v0.z);
6070                 }
6071             glEnd();
6072             glBegin(GL_LINES);
6073
6074             if (playerdetail)
6075                 for (i = 0; i < skeleton.drawmodel.TriangleNum; i++) {
6076                     XYZ &v0 = skeleton.drawmodel.vertex[skeleton.drawmodel.Triangles[i].vertex[0]];
6077                     XYZ &v1 = skeleton.drawmodel.vertex[skeleton.drawmodel.Triangles[i].vertex[1]];
6078                     XYZ &v2 = skeleton.drawmodel.vertex[skeleton.drawmodel.Triangles[i].vertex[2]];
6079                     glVertex3f(v0.x, v0.y, v0.z);
6080                     glVertex3f(v1.x, v1.y, v1.z);
6081                     glVertex3f(v1.x, v1.y, v1.z);
6082                     glVertex3f(v2.x, v2.y, v2.z);
6083                     glVertex3f(v2.x, v2.y, v2.z);
6084                     glVertex3f(v0.x, v0.y, v0.z);
6085                 }
6086
6087             glEnd();
6088         }
6089
6090         terrainlight = terrain.getLighting(coords.x, coords.z);
6091         distance = distsq(&viewer, &coords);
6092         distance = (viewdistance * viewdistance - (distance - (viewdistance * viewdistance * fadestart)) * (1 / (1 - fadestart))) / viewdistance / viewdistance;
6093         if (distance > 1)
6094             distance = 1;
6095         if (distance > 0) {
6096             terrainheight = (coords.y - terrain.getHeight(coords.x, coords.z)) / 3 + 1;
6097             if (terrainheight < 1)
6098                 terrainheight = 1;
6099             if (terrainheight > 1.7)
6100                 terrainheight = 1.7;
6101
6102             //burnt=0;
6103             glColor4f((1 - (1 - terrainlight.x) / terrainheight) - burnt, (1 - (1 - terrainlight.y) / terrainheight) - burnt, (1 - (1 - terrainlight.z) / terrainheight) - burnt, distance);
6104             glDisable(GL_BLEND);
6105             glAlphaFunc(GL_GREATER, 0.0001);
6106             glEnable(GL_TEXTURE_2D);
6107             if (cellophane) {
6108                 glDisable(GL_TEXTURE_2D);
6109                 glColor4f(.7, .35, 0, .5);
6110                 glDepthMask(0);
6111                 glEnable(GL_LIGHTING);
6112                 glEnable(GL_BLEND);
6113             }
6114             if (tutoriallevel && id != 0) {
6115                 glColor4f(.7, .7, .7, 0.6);
6116                 glDepthMask(0);
6117                 glEnable(GL_LIGHTING);
6118                 glEnable(GL_BLEND);
6119                 if (canattack && cananger)
6120                     if (animation[animTarget].attack == normalattack || animation[animTarget].attack == reversed) {
6121                         glDisable(GL_TEXTURE_2D);
6122                         glColor4f(1, 0, 0, 0.8);
6123                     }
6124                 glMatrixMode(GL_TEXTURE);
6125                 glPushMatrix();
6126                 glTranslatef(0, -smoketex, 0);
6127                 glTranslatef(-smoketex, 0, 0);
6128             }
6129             if (playerdetail) {
6130                 if (!showpoints) {
6131                     if ((tutoriallevel && id != 0))
6132                         skeleton.drawmodel.drawdifftex(Sprite::cloudimpacttexture);
6133                     else
6134                         skeleton.drawmodel.draw();
6135                 }
6136             }
6137             if (!playerdetail) {
6138                 if ((tutoriallevel && id != 0))
6139                     skeleton.drawmodellow.drawdifftex(Sprite::cloudimpacttexture);
6140                 else
6141                     skeleton.drawmodellow.drawdifftex(skeleton.drawmodel.textureptr);
6142             }
6143
6144             if (!(animation[animTarget].attack == normalattack || animation[animTarget].attack == reversed))
6145                 if (tutoriallevel && id != 0) {
6146                     glPopMatrix();
6147                     glMatrixMode(GL_MODELVIEW);
6148                     glEnable(GL_TEXTURE_2D);
6149                     glColor4f(.7, .7, .7, 0.6);
6150                     glDepthMask(0);
6151                     glEnable(GL_LIGHTING);
6152                     glEnable(GL_BLEND);
6153                     if (canattack && cananger)
6154                         if (animation[animTarget].attack == normalattack || animation[animTarget].attack == reversed) {
6155                             glDisable(GL_TEXTURE_2D);
6156                             glColor4f(1, 0, 0, 0.8);
6157                         }
6158                     glMatrixMode(GL_TEXTURE);
6159                     glPushMatrix();
6160                     glTranslatef(0, -smoketex * .6, 0);
6161                     glTranslatef(smoketex * .6, 0, 0);
6162                     if (playerdetail) {
6163                         if (!showpoints) {
6164                             if ((tutoriallevel && id != 0))
6165                                 skeleton.drawmodel.drawdifftex(Sprite::cloudimpacttexture);
6166                             else
6167                                 skeleton.drawmodel.draw();
6168                         }
6169                     }
6170                     if (!playerdetail) {
6171                         if ((tutoriallevel && id != 0))
6172                             skeleton.drawmodellow.drawdifftex(Sprite::cloudimpacttexture);
6173                         else
6174                             skeleton.drawmodellow.drawdifftex(skeleton.drawmodel.textureptr);
6175                     }
6176                 }
6177
6178
6179             if (tutoriallevel && id != 0) {
6180                 glPopMatrix();
6181                 glMatrixMode(GL_MODELVIEW);
6182                 glEnable(GL_TEXTURE_2D);
6183             }
6184             if (skeleton.clothes) {
6185                 glDepthMask(0);
6186                 glEnable(GL_BLEND);
6187                 if (!immediate)
6188                     skeleton.drawmodelclothes.draw();
6189                 if (immediate)
6190                     skeleton.drawmodelclothes.drawimmediate();
6191                 glDepthMask(1);
6192             }
6193         }
6194         glPopMatrix();
6195
6196         if (num_weapons > 0) {
6197             for (k = 0; k < num_weapons; k++) {
6198                 i = weaponids[k];
6199                 if (weaponactive == k) {
6200                     if (weapons[i].getType() != staff) {
6201                         for (j = 0; j < skeleton.num_muscles; j++) {
6202                             if ((skeleton.muscles[j].parent1->label == righthand || skeleton.muscles[j].parent2->label == righthand) && skeleton.muscles[j].numvertices > 0) {
6203                                 weaponattachmuscle = j;
6204                             }
6205                         }
6206                         for (j = 0; j < skeleton.num_muscles; j++) {
6207                             if ((skeleton.muscles[j].parent1->label == rightwrist || skeleton.muscles[j].parent2->label == rightwrist) && (skeleton.muscles[j].parent1->label != righthand && skeleton.muscles[j].parent2->label != righthand) && skeleton.muscles[j].numvertices > 0) {
6208                                 weaponrotatemuscle = j;
6209                             }
6210                         }
6211                         weaponpoint = (skeleton.muscles[weaponattachmuscle].parent1->position + skeleton.muscles[weaponattachmuscle].parent2->position) / 2;
6212                         if (creature == wolftype)
6213                             weaponpoint = (jointPos(rightwrist) * .7 + jointPos(righthand) * .3);
6214                     }
6215                     if (weapons[i].getType() == staff) {
6216                         for (j = 0; j < skeleton.num_muscles; j++) {
6217                             if ((skeleton.muscles[j].parent1->label == righthand || skeleton.muscles[j].parent2->label == righthand) && skeleton.muscles[j].numvertices > 0) {
6218                                 weaponattachmuscle = j;
6219                             }
6220                         }
6221                         for (j = 0; j < skeleton.num_muscles; j++) {
6222                             if ((skeleton.muscles[j].parent1->label == rightelbow || skeleton.muscles[j].parent2->label == rightelbow) && (skeleton.muscles[j].parent1->label != rightshoulder && skeleton.muscles[j].parent2->label != rightshoulder) && skeleton.muscles[j].numvertices > 0) {
6223                                 weaponrotatemuscle = j;
6224                             }
6225                         }
6226                         //weaponpoint=jointPos(rightwrist);
6227                         weaponpoint = (skeleton.muscles[weaponattachmuscle].parent1->position + skeleton.muscles[weaponattachmuscle].parent2->position) / 2;
6228                         //weaponpoint+=skeleton.specialforward[1]*.1+(jointPos(rightwrist)-jointPos(rightelbow));
6229                         XYZ tempnormthing, vec1, vec2;
6230                         vec1 = (jointPos(rightwrist) - jointPos(rightelbow));
6231                         vec2 = (jointPos(rightwrist) - jointPos(rightshoulder));
6232                         CrossProduct(&vec1, &vec2, &tempnormthing);
6233                         Normalise(&tempnormthing);
6234                         if (animTarget != staffhitanim && animCurrent != staffhitanim && animTarget != staffgroundsmashanim && animCurrent != staffgroundsmashanim && animTarget != staffspinhitanim && animCurrent != staffspinhitanim)
6235                             weaponpoint += tempnormthing * .1 - skeleton.specialforward[1] * .3 + (jointPos(rightwrist) - jointPos(rightelbow));
6236                     }
6237                 }
6238                 if (weaponactive != k && weaponstuck != k) {
6239                     if (weapons[i].getType() == knife)
6240                         weaponpoint = jointPos(abdomen) + (jointPos(righthip) - jointPos(lefthip)) * .1 + (jointPos(rightshoulder) - jointPos(leftshoulder)) * .35;
6241                     if (weapons[i].getType() == sword)
6242                         weaponpoint = jointPos(abdomen) + (jointPos(lefthip) - jointPos(righthip)) * .09 + (jointPos(leftshoulder) - jointPos(rightshoulder)) * .33;
6243                     if (weapons[i].getType() == staff)
6244                         weaponpoint = jointPos(abdomen) + (jointPos(lefthip) - jointPos(righthip)) * .09 + (jointPos(leftshoulder) - jointPos(rightshoulder)) * .33;
6245                     for (j = 0; j < skeleton.num_muscles; j++) {
6246                         if ((skeleton.muscles[j].parent1->label == abdomen || skeleton.muscles[j].parent2->label == abdomen) && (skeleton.muscles[j].parent1->label == neck || skeleton.muscles[j].parent2->label == neck) && skeleton.muscles[j].numvertices > 0) {
6247                             weaponrotatemuscle = j;
6248                         }
6249                     }
6250                 }
6251                 if (weaponstuck == k) {
6252                     if (weaponstuckwhere == 0)
6253                         weaponpoint = jointPos(abdomen) * .5 + jointPos(neck) * .5 - skeleton.forward * .8;
6254                     else
6255                         weaponpoint = jointPos(abdomen) * .5 + jointPos(neck) * .5 + skeleton.forward * .8;
6256                     for (j = 0; j < skeleton.num_muscles; j++) {
6257                         if ((skeleton.muscles[j].parent1->label == abdomen || skeleton.muscles[j].parent2->label == abdomen) && (skeleton.muscles[j].parent1->label == neck || skeleton.muscles[j].parent2->label == neck) && skeleton.muscles[j].numvertices > 0) {
6258                             weaponrotatemuscle = j;
6259                         }
6260                     }
6261                 }
6262                 if (skeleton.free) {
6263                     weapons[i].position = weaponpoint * scale + coords;
6264                     weapons[i].bigrotation = 0;
6265                     weapons[i].bigtilt = 0;
6266                     weapons[i].bigtilt2 = 0;
6267                 } else {
6268                     weapons[i].position = DoRotation(DoRotation(DoRotation(weaponpoint, 0, 0, tilt), tilt2, 0, 0), 0, yaw, 0) * scale + coords + currentoffset * (1 - target) * scale + targetoffset * target * scale;
6269                     weapons[i].bigrotation = yaw;
6270                     weapons[i].bigtilt = tilt;
6271                     weapons[i].bigtilt2 = tilt2;
6272                 }
6273                 weapons[i].rotation1 = skeleton.muscles[weaponrotatemuscle].lastrotate1;
6274                 weapons[i].rotation2 = skeleton.muscles[weaponrotatemuscle].lastrotate2;
6275                 weapons[i].rotation3 = skeleton.muscles[weaponrotatemuscle].lastrotate3;
6276                 if (weaponactive == k) {
6277                     if (weapons[i].getType() == knife) {
6278                         weapons[i].smallrotation = 180;
6279                         weapons[i].smallrotation2 = 0;
6280                         if (isCrouch() || wasCrouch()) {
6281                             weapons[i].smallrotation2 = 20;
6282                         }
6283                         if (animTarget == hurtidleanim) {
6284                             weapons[i].smallrotation2 = 50;
6285                         }
6286                         if ((animCurrent == crouchstabanim && animTarget == crouchstabanim) || (animCurrent == backhandspringanim && animTarget == backhandspringanim)) {
6287                             XYZ temppoint1, temppoint2, tempforward;
6288                             float distance;
6289
6290                             temppoint1 = jointPos(righthand);
6291                             temppoint2 = animation[animCurrent].weapontarget[frameCurrent] * (1 - target) + animation[animTarget].weapontarget[frameTarget] * (target);
6292                             distance = findDistance(&temppoint1, &temppoint2);
6293                             weapons[i].rotation2 = asin((temppoint1.y - temppoint2.y) / distance);
6294                             weapons[i].rotation2 *= 360 / 6.28;
6295                             temppoint1.y = 0;
6296                             temppoint2.y = 0;
6297                             weapons[i].rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2));
6298                             weapons[i].rotation1 *= 360 / 6.28;
6299                             weapons[i].rotation3 = 0;
6300                             weapons[i].smallrotation = -90;
6301                             weapons[i].smallrotation2 = 0;
6302                             if (temppoint1.x > temppoint2.x)
6303                                 weapons[i].rotation1 = 360 - weapons[i].rotation1;
6304                         }
6305                         if ((animCurrent == knifeslashreversalanim && animTarget == knifeslashreversalanim) || (animCurrent == knifeslashreversedanim && animTarget == knifeslashreversedanim)) {
6306                             XYZ temppoint1, temppoint2, tempforward;
6307                             float distance;
6308
6309                             temppoint1 = jointPos(righthand);
6310                             temppoint2 = animation[animCurrent].weapontarget[frameCurrent] * (1 - target) + animation[animTarget].weapontarget[frameTarget] * (target);
6311                             distance = findDistance(&temppoint1, &temppoint2);
6312                             weapons[i].rotation2 = asin((temppoint1.y - temppoint2.y) / distance);
6313                             weapons[i].rotation2 *= 360 / 6.28;
6314                             temppoint1.y = 0;
6315                             temppoint2.y = 0;
6316                             weapons[i].rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2));
6317                             weapons[i].rotation1 *= 360 / 6.28;
6318                             weapons[i].rotation3 = 0;
6319                             weapons[i].smallrotation = 90;
6320                             weapons[i].smallrotation2 = 0;
6321                             if (temppoint1.x > temppoint2.x)
6322                                 weapons[i].rotation1 = 360 - weapons[i].rotation1;
6323                         }
6324                         if (animTarget == knifethrowanim) {
6325                             weapons[i].smallrotation = 90;
6326                             //weapons[i].smallrotation2=-90;
6327                             weapons[i].smallrotation2 = 0;
6328                             weapons[i].rotation1 = 0;
6329                             weapons[i].rotation2 = 0;
6330                             weapons[i].rotation3 = 0;
6331                         }
6332                         if (animTarget == knifesneakattackanim && frameTarget < 5) {
6333                             weapons[i].smallrotation = -90;
6334                             weapons[i].rotation1 = 0;
6335                             weapons[i].rotation2 = 0;
6336                             weapons[i].rotation3 = 0;
6337                         }
6338                     }
6339                     if (weapons[i].getType() == sword) {
6340                         weapons[i].smallrotation = 0;
6341                         weapons[i].smallrotation2 = 0;
6342                         if (animTarget == knifethrowanim) {
6343                             weapons[i].smallrotation = -90;
6344                             weapons[i].smallrotation2 = 0;
6345                             weapons[i].rotation1 = 0;
6346                             weapons[i].rotation2 = 0;
6347                             weapons[i].rotation3 = 0;
6348                         }
6349                         if ((animTarget == swordgroundstabanim && animCurrent == swordgroundstabanim) || (animTarget == swordsneakattackanim && animCurrent == swordsneakattackanim) || (animTarget == swordslashparryanim && animCurrent == swordslashparryanim) || (animTarget == swordslashparriedanim && animCurrent == swordslashparriedanim) || (animTarget == swordslashreversalanim && animCurrent == swordslashreversalanim) || (animTarget == swordslashreversedanim && animCurrent == swordslashreversedanim) || (animTarget == knifeslashreversalanim && animCurrent == knifeslashreversalanim) || (animTarget == knifeslashreversedanim && animCurrent == knifeslashreversedanim) || (animTarget == swordslashanim && animCurrent == swordslashanim) || (animTarget == drawleftanim && animCurrent == drawleftanim) || (animCurrent == backhandspringanim && animTarget == backhandspringanim)) {
6350                             XYZ temppoint1, temppoint2, tempforward;
6351                             float distance;
6352
6353                             temppoint1 = animation[animCurrent].position[skeleton.jointlabels[righthand]][frameCurrent] * (1 - target) + animation[animTarget].position[skeleton.jointlabels[righthand]][frameTarget] * (target); //jointPos(righthand);
6354                             temppoint2 = animation[animCurrent].weapontarget[frameCurrent] * (1 - target) + animation[animTarget].weapontarget[frameTarget] * (target);
6355                             distance = findDistance(&temppoint1, &temppoint2);
6356                             weapons[i].rotation2 = asin((temppoint1.y - temppoint2.y) / distance);
6357                             weapons[i].rotation2 *= 360 / 6.28;
6358                             temppoint1.y = 0;
6359                             temppoint2.y = 0;
6360                             weapons[i].rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2));
6361                             weapons[i].rotation1 *= 360 / 6.28;
6362                             weapons[i].rotation3 = 0;
6363                             weapons[i].smallrotation = 90;
6364                             weapons[i].smallrotation2 = 0;
6365                             if (temppoint1.x > temppoint2.x)
6366                                 weapons[i].rotation1 = 360 - weapons[i].rotation1;
6367                         }
6368                     }
6369                     if (weapons[i].getType() == staff) {
6370                         weapons[i].smallrotation = 100;
6371                         weapons[i].smallrotation2 = 0;
6372                         if ((animTarget == staffhitanim && animCurrent == staffhitanim) || (animTarget == staffhitreversedanim && animCurrent == staffhitreversedanim) || (animTarget == staffspinhitreversedanim && animCurrent == staffspinhitreversedanim) || (animTarget == staffgroundsmashanim && animCurrent == staffgroundsmashanim) || (animTarget == staffspinhitanim && animCurrent == staffspinhitanim)) {
6373                             XYZ temppoint1, temppoint2, tempforward;
6374                             float distance;
6375
6376                             temppoint1 = animation[animCurrent].position[skeleton.jointlabels[righthand]][frameCurrent] * (1 - target) + animation[animTarget].position[skeleton.jointlabels[righthand]][frameTarget] * (target); //jointPos(righthand);
6377                             temppoint2 = animation[animCurrent].weapontarget[frameCurrent] * (1 - target) + animation[animTarget].weapontarget[frameTarget] * (target);
6378                             distance = findDistance(&temppoint1, &temppoint2);
6379                             weapons[i].rotation2 = asin((temppoint1.y - temppoint2.y) / distance);
6380                             weapons[i].rotation2 *= 360 / 6.28;
6381                             temppoint1.y = 0;
6382                             temppoint2.y = 0;
6383                             weapons[i].rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2));
6384                             weapons[i].rotation1 *= 360 / 6.28;
6385                             weapons[i].rotation3 = 0;
6386                             weapons[i].smallrotation = 90;
6387                             weapons[i].smallrotation2 = 0;
6388                             if (temppoint1.x > temppoint2.x)
6389                                 weapons[i].rotation1 = 360 - weapons[i].rotation1;
6390                         }
6391                     }
6392                 }
6393                 if (weaponactive != k && weaponstuck != k) {
6394                     if (weapons[i].getType() == knife) {
6395                         weapons[i].smallrotation = -70;
6396                         weapons[i].smallrotation2 = 10;
6397                     }
6398                     if (weapons[i].getType() == sword) {
6399                         weapons[i].smallrotation = -100;
6400                         weapons[i].smallrotation2 = -8;
6401                     }
6402                     if (weapons[i].getType() == staff) {
6403                         weapons[i].smallrotation = -100;
6404                         weapons[i].smallrotation2 = -8;
6405                     }
6406                 }
6407                 if (weaponstuck == k) {
6408                     if (weaponstuckwhere == 0)
6409                         weapons[i].smallrotation = 180;
6410                     else
6411                         weapons[i].smallrotation = 0;
6412                     weapons[i].smallrotation2 = 10;
6413                 }
6414             }
6415         }
6416     }
6417
6418     calcrot = 0;
6419     if (skeleton.free)
6420         calcrot = 1;
6421     if (animation[animTarget].attack || isRun() || animTarget == staggerbackhardanim || isFlip() || animTarget == climbanim || animTarget == sneakanim || animTarget == rollanim || animTarget == walkanim || animTarget == backhandspringanim || isFlip() || isWallJump())
6422         calcrot = 1;
6423     if (animCurrent != animTarget)
6424         calcrot = 1;
6425     if (skeleton.free == 2)
6426         calcrot = 0;
6427
6428     return 0;
6429 }
6430
6431
6432 /* FUNCTION?
6433  */
6434 int Person::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate, Model *model)
6435 {
6436     static int i, j;
6437     static float distance;
6438     static float olddistance;
6439     static int intersecting;
6440     static int firstintersecting;
6441     static XYZ point;
6442     static XYZ oldp1;
6443     static XYZ start, end;
6444     static float slopethreshold = -.4;
6445
6446     firstintersecting = -1;
6447
6448     oldp1 = *p1;
6449     *p1 = *p1 - *move;
6450     if (distsq(p1, &model->boundingspherecenter) > radius * radius + model->boundingsphereradius * model->boundingsphereradius)
6451         return -1;
6452     if (*rotate)
6453         *p1 = DoRotation(*p1, 0, -*rotate, 0);
6454     for (i = 0; i < 4; i++) {
6455         for (j = 0; j < model->TriangleNum; j++) {
6456             if (model->facenormals[j].y <= slopethreshold) {
6457                 intersecting = 0;
6458                 distance = abs((model->facenormals[j].x * p1->x) + (model->facenormals[j].y * p1->y) + (model->facenormals[j].z * p1->z) - ((model->facenormals[j].x * model->vertex[model->Triangles[j].vertex[0]].x) + (model->facenormals[j].y * model->vertex[model->Triangles[j].vertex[0]].y) + (model->facenormals[j].z * model->vertex[model->Triangles[j].vertex[0]].z)));
6459                 if (distance < radius) {
6460                     point = *p1 - model->facenormals[j] * distance;
6461                     if (PointInTriangle( &point, model->facenormals[j], &model->vertex[model->Triangles[j].vertex[0]], &model->vertex[model->Triangles[j].vertex[1]], &model->vertex[model->Triangles[j].vertex[2]]))
6462                         intersecting = 1;
6463                     if (!intersecting)
6464                         intersecting = sphere_line_intersection(&model->vertex[model->Triangles[j].vertex[0]],
6465                                                                 &model->vertex[model->Triangles[j].vertex[1]],
6466                                                                 p1, &radius);
6467                     if (!intersecting)
6468                         intersecting = sphere_line_intersection(&model->vertex[model->Triangles[j].vertex[1]],
6469                                                                 &model->vertex[model->Triangles[j].vertex[2]],
6470                                                                 p1, &radius);
6471                     if (!intersecting)
6472                         intersecting = sphere_line_intersection(&model->vertex[model->Triangles[j].vertex[0]],
6473                                                                 &model->vertex[model->Triangles[j].vertex[2]],
6474                                                                 p1, &radius);
6475                     end = *p1 - point;
6476                     if (dotproduct(&model->facenormals[j], &end) > 0 && intersecting) {
6477                         start = *p1;
6478                         end = *p1;
6479                         end.y -= radius;
6480                         if (LineFacetd(&start, &end, &model->vertex[model->Triangles[j].vertex[0]], &model->vertex[model->Triangles[j].vertex[1]], &model->vertex[model->Triangles[j].vertex[2]], &model->facenormals[j], &point)) {
6481                             p1->y = point.y + radius;
6482                             if ((animTarget == jumpdownanim || isFlip())) {
6483                                 if (isFlip() && (frameTarget < 5 || animation[animTarget].label[frameTarget] == 7 || animation[animTarget].label[frameTarget] == 4))
6484                                     RagDoll(0);
6485
6486                                 if (animTarget == jumpupanim) {
6487                                     jumppower = -4;
6488                                     animTarget = getIdle();
6489                                 }
6490                                 target = 0;
6491                                 frameTarget = 0;
6492                                 onterrain = 1;
6493
6494                                 if (id == 0) {
6495                                     pause_sound(whooshsound);
6496                                     OPENAL_SetVolume(channels[whooshsound], 0);
6497                                 }
6498
6499                                 if ((animTarget == jumpdownanim || isFlip()) && !wasLanding() && !wasLandhard()) {
6500                                     if (isFlip())
6501                                         jumppower = -4;
6502                                     animTarget = getLanding();
6503                                     emit_sound_at(landsound, coords, 128.);
6504
6505                                     if (id == 0) {
6506                                         envsound[numenvsounds] = coords;
6507                                         envsoundvol[numenvsounds] = 16;
6508                                         envsoundlife[numenvsounds] = .4;
6509                                         numenvsounds++;
6510                                     }
6511                                 }
6512                             }
6513                         }
6514                     }
6515                 }
6516                 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
6517                     olddistance = distance;
6518                     firstintersecting = j;
6519                     *p = point;
6520                 }
6521             }
6522         }
6523         for (j = 0; j < model->TriangleNum; j++) {
6524             if (model->facenormals[j].y > slopethreshold) {
6525                 intersecting = 0;
6526                 start = *p1;
6527                 start.y -= radius / 4;
6528                 XYZ &v0 = model->vertex[model->Triangles[j].vertex[0]];
6529                 XYZ &v1 = model->vertex[model->Triangles[j].vertex[1]];
6530                 XYZ &v2 = model->vertex[model->Triangles[j].vertex[2]];
6531                 distance = abs((model->facenormals[j].x * start.x)
6532                                + (model->facenormals[j].y * start.y)
6533                                + (model->facenormals[j].z * start.z)
6534                                - ((model->facenormals[j].x * v0.x)
6535                                   + (model->facenormals[j].y * v0.y)
6536                                   + (model->facenormals[j].z * v0.z)));
6537                 if (distance < radius * .5) {
6538                     point = start - model->facenormals[j] * distance;
6539                     if (PointInTriangle( &point, model->facenormals[j], &v0, &v1, &v2))
6540                         intersecting = 1;
6541                     if (!intersecting)
6542                         intersecting = sphere_line_intersection(v0.x, v0.y, v0.z, v1.x, v1.y, v1.z, p1->x, p1->y, p1->z, radius / 2);
6543                     if (!intersecting)
6544                         intersecting = sphere_line_intersection(v1.x, v1.y, v1.z, v2.x, v2.y, v2.z, p1->x, p1->y, p1->z, radius / 2);
6545                     if (!intersecting)
6546                         intersecting = sphere_line_intersection(v0.x, v0.y, v0.z, v2.x, v2.y, v2.z, p1->x, p1->y, p1->z, radius / 2);
6547                     end = *p1 - point;
6548                     if (dotproduct(&model->facenormals[j], &end) > 0 && intersecting) {
6549                         if ((animTarget == jumpdownanim || animTarget == jumpupanim || isFlip())) {
6550                             start = velocity;
6551                             velocity -= DoRotation(model->facenormals[j], 0, *rotate, 0) * findLength(&velocity) * abs(normaldotproduct(velocity, DoRotation(model->facenormals[j], 0, *rotate, 0))); //(distance-radius*.5)/multiplier;
6552                             if (findLengthfast(&start) < findLengthfast(&velocity))
6553                                 velocity = start;
6554                         }
6555                         *p1 += model->facenormals[j] * (distance - radius * .5);
6556                     }
6557                 }
6558                 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
6559                     olddistance = distance;
6560                     firstintersecting = j;
6561                     *p = point;
6562                 }
6563             }
6564         }
6565     }
6566     if (*rotate)
6567         *p = DoRotation(*p, 0, *rotate, 0);
6568     *p = *p + *move;
6569     if (*rotate)
6570         *p1 = DoRotation(*p1, 0, *rotate, 0);
6571     *p1 += *move;
6572     return firstintersecting;
6573 }
6574
6575 void Person::takeWeapon(int weaponId)
6576 {
6577     weaponactive = 0;
6578     weapons[weaponId].owner = id;
6579     if (num_weapons > 0) {
6580         weaponids[num_weapons] = weaponids[0];
6581     }
6582     num_weapons++;
6583     weaponids[0] = weaponId;
6584 }
6585