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