]> git.jsancho.org Git - lugaru.git/blob - Source/GameTick.cpp
Moved Weapon model and texture loading in Weapon class
[lugaru.git] / Source / GameTick.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 // Enable full math definitions
22 #define _USE_MATH_DEFINES
23
24 #include "Game.hpp"
25
26 #include "Animation/Animation.hpp"
27 #include "Audio/openal_wrapper.hpp"
28 #include "Devtools/ConsoleCmds.hpp"
29 #include "Level/Awards.hpp"
30 #include "Level/Campaign.hpp"
31 #include "Level/Dialog.hpp"
32 #include "Level/Hotspot.hpp"
33 #include "Menu/Menu.hpp"
34 #include "User/Settings.hpp"
35 #include "Utils/Folders.hpp"
36 #include "Utils/Input.hpp"
37 #include "Tutorial.hpp"
38
39 #if PLATFORM_UNIX
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43 #else
44 #include <direct.h>
45 #endif
46
47 #include <algorithm>
48 #include <cmath>
49 #include <ctime>
50 #include <dirent.h>
51 #include <limits>
52 #include <set>
53
54 using namespace std;
55 using namespace Game;
56
57 // Added more evilness needed for MSVC
58 #ifdef _MSC_VER
59 #define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n)
60 #define snprintf(buf, size, format, ...) _sprintf_p(buf, size, format)
61 #endif
62
63
64 extern float multiplier;
65 extern XYZ viewer;
66 extern int environment;
67 extern Terrain terrain;
68 extern float screenwidth, screenheight;
69 extern float gravity;
70 extern int detail;
71 extern float texdetail;
72 extern int slomo;
73 extern float slomodelay;
74 extern bool floatjump;
75 extern float volume;
76 extern Light light;
77 extern float camerashake;
78 extern float woozy;
79 extern float blackout;
80 extern bool cellophane;
81 extern bool musictoggle;
82 extern int difficulty;
83 extern int bloodtoggle;
84 extern bool invertmouse;
85 extern float windvar;
86 extern float precipdelay;
87 extern XYZ viewerfacing;
88 extern bool ambientsound;
89 extern bool mousejump;
90 extern float viewdistance;
91 extern bool freeze;
92 extern XYZ windvector;
93 extern bool devtools;
94 int leveltheme;
95 extern int mainmenu;
96 extern int oldmainmenu;
97 extern bool visibleloading;
98 extern XYZ envsound[30];
99 extern float envsoundvol[30];
100 extern int numenvsounds;
101 extern float envsoundlife[30];
102 extern float usermousesensitivity;
103 extern bool ismotionblur;
104 extern bool showdamagebar; // (des)activate the damage bar
105 extern bool decalstoggle;
106 extern bool skyboxtexture;
107 extern float skyboxr;
108 extern float skyboxg;
109 extern float skyboxb;
110 extern float skyboxlightr;
111 extern float skyboxlightg;
112 extern float skyboxlightb;
113 extern float fadestart;
114 extern float slomospeed;
115 extern float slomofreq;
116 extern float smoketex;
117 extern bool againbonus;
118 extern bool reversaltrain;
119 extern bool canattack;
120 extern bool cananger;
121 extern float damagedealt;
122 extern int maptype;
123 extern int editoractive;
124 extern int editorpathtype;
125
126 extern float hostiletime;
127
128 extern bool gamestarted;
129
130 extern int hostile;
131
132 extern bool stillloading;
133 extern bool winfreeze;
134
135 extern bool campaign;
136
137 extern void toggleFullscreen();
138
139 bool won = false;
140 int whichchoice = 0;
141 bool winhotspot = false;
142 bool windialogue = false;
143 bool realthreat = 0;
144 XYZ cameraloc;
145 float cameradist = 0;
146 bool oldattackkey = 0;
147 int whichlevel = 0;
148 float musicvolume[4] = {};
149 float oldmusicvolume[4] = {};
150 int musicselected = 0;
151
152 const char *rabbitskin[] = {
153     "Textures/Fur3.jpg",
154     "Textures/Fur.jpg",
155     "Textures/Fur2.jpg",
156     "Textures/Lynx.jpg",
157     "Textures/Otter.jpg",
158     "Textures/Opal.jpg",
159     "Textures/Sable.jpg",
160     "Textures/Chocolate.jpg",
161     "Textures/BW2.jpg",
162     "Textures/WB2.jpg"
163 };
164
165 const char *wolfskin[] = {
166     "Textures/Wolf.jpg",
167     "Textures/DarkWolf.jpg",
168     "Textures/SnowWolf.jpg"
169 };
170
171 const char **creatureskin[] = {rabbitskin, wolfskin};
172
173 #define STATIC_ASSERT(x) extern int s_a_dummy[2 * (!!(x)) - 1];
174 STATIC_ASSERT (rabbittype == 0 && wolftype == 1)
175
176 // utility functions
177
178 // TODO: this is slightly incorrect
179 float roughDirection(XYZ vec)
180 {
181     Normalise(&vec);
182     float angle = -asin(-vec.x) * 180 / M_PI;
183     if (vec.z < 0)
184         angle = 180 - angle;
185     return angle;
186 }
187 float roughDirectionTo(XYZ start, XYZ end)
188 {
189     return roughDirection(end - start);
190 }
191 inline float pitchOf(XYZ vec)
192 {
193     Normalise(&vec);
194     return -asin(vec.y) * 180 / M_PI;
195 }
196 float pitchTo(XYZ start, XYZ end)
197 {
198     return pitchOf(end - start);
199 }
200 float sq(float n)
201 {
202     return n * n;
203 }
204 inline float stepTowardf(float from, float to, float by)
205 {
206     if (fabs(from - to) < by)
207         return to;
208     else if (from > to)
209         return from - by;
210     else
211         return from + by;
212 }
213
214 void Game::playdialoguescenesound()
215 {
216     XYZ temppos;
217     temppos = Person::players[Dialog::currentScene().participantfocus]->coords;
218     temppos = temppos - viewer;
219     Normalise(&temppos);
220     temppos += viewer;
221
222     int sound = -1;
223     switch (Dialog::currentScene().sound) {
224     case -6:
225         sound = alarmsound;
226         break;
227     case -4:
228         sound = consolefailsound;
229         break;
230     case -3:
231         sound = consolesuccesssound;
232         break;
233     case -2:
234         sound = firestartsound;
235         break;
236     case -1:
237         sound = fireendsound;
238         break;
239     case 1:
240         sound = rabbitchitter;
241         break;
242     case 2:
243         sound = rabbitchitter2;
244         break;
245     case 3:
246         sound = rabbitpainsound;
247         break;
248     case 4:
249         sound = rabbitpain1sound;
250         break;
251     case 5:
252         sound = rabbitattacksound;
253         break;
254     case 6:
255         sound = rabbitattack2sound;
256         break;
257     case 7:
258         sound = rabbitattack3sound;
259         break;
260     case 8:
261         sound = rabbitattack4sound;
262         break;
263     case 9:
264         sound = growlsound;
265         break;
266     case 10:
267         sound = growl2sound;
268         break;
269     case 11:
270         sound = snarlsound;
271         break;
272     case 12:
273         sound = snarl2sound;
274         break;
275     case 13:
276         sound = barksound;
277         break;
278     case 14:
279         sound = bark2sound;
280         break;
281     case 15:
282         sound = bark3sound;
283         break;
284     case 16:
285         sound = barkgrowlsound;
286         break;
287     default:
288         break;
289     }
290     if (sound != -1)
291         emit_sound_at(sound, temppos);
292 }
293
294 // ================================================================
295
296 int Game::findClosestPlayer()
297 {
298     int closest = -1;
299     float closestdist = std::numeric_limits<float>::max();
300
301     for (unsigned i = 1; i < Person::players.size(); i++) {
302         float distance = distsq(&Person::players[i]->coords, &Person::players[0]->coords);
303         if (distance < closestdist) {
304             closestdist = distance;
305             closest = i;
306         }
307     }
308     return closest;
309 }
310
311 static int findClosestObject()
312 {
313     int closest = -1;
314     float closestdist = std::numeric_limits<float>::max();
315
316     for (unsigned int i = 0; i < Object::objects.size(); i++) {
317         float distance = distsq(&Object::objects[i]->position,
318                                 &Person::players[0]->coords);
319         if (distance < closestdist) {
320             closestdist = distance;
321             closest = (int) i;
322         }
323     }
324     return closest;
325 }
326
327 static void cmd_dispatch(const string cmd)
328 {
329     int i, n_cmds = sizeof(cmd_names) / sizeof(cmd_names[0]);
330
331     for (i = 0; i < n_cmds; i++)
332         if (cmd.substr(0, cmd.find(' ')) == string(cmd_names[i])) {
333             cout << "|" << cmd.substr(cmd.find(' ') + 1) << "|" << endl;
334             cmd_handlers[i](cmd.substr(cmd.find(' ') + 1).c_str());
335             break;
336         }
337     emit_sound_np(i < n_cmds ? consolesuccesssound : consolefailsound);
338 }
339
340 /********************> Tick() <*****/
341 extern bool save_screenshot(const char * fname);
342 void Screenshot (void)
343 {
344     char filename[1024];
345     time_t t = time(NULL);
346     struct tm *tme = localtime(&t);
347     sprintf(filename, "Screenshot-%04d%02d%02d-%02d%02d%02d.png",
348             tme->tm_year + 1900, tme->tm_mon + 1, tme->tm_mday, tme->tm_hour, tme->tm_min, tme->tm_sec);
349
350     save_screenshot(filename);
351 }
352
353 void Game::SetUpLighting()
354 {
355     if (environment == snowyenvironment)
356         light.setColors(.65, .65, .7, .4, .4, .44);
357     if (environment == desertenvironment)
358         light.setColors(.95, .95, .95, .4, .35, .3);
359     if (environment == grassyenvironment)
360         light.setColors(.95, .95, 1, .4, .4, .44);
361     if (!skyboxtexture)
362         light.setColors(1, 1, 1, .4, .4, .4);
363     float average;
364     average = (skyboxlightr + skyboxlightg + skyboxlightb) / 3;
365     light.color[0] *= (skyboxlightr + average) / 2;
366     light.color[1] *= (skyboxlightg + average) / 2;
367     light.color[2] *= (skyboxlightb + average) / 2;
368     light.ambient[0] *= (skyboxlightr + average) / 2;
369     light.ambient[1] *= (skyboxlightg + average) / 2;
370     light.ambient[2] *= (skyboxlightb + average) / 2;
371 }
372
373 void Setenvironment(int which)
374 {
375     LOGFUNC;
376
377     LOG(" Setting environment...");
378
379     float temptexdetail;
380     environment = which;
381
382     pause_sound(stream_snowtheme);
383     pause_sound(stream_grasstheme);
384     pause_sound(stream_deserttheme);
385     pause_sound(stream_wind);
386     pause_sound(stream_desertambient);
387
388
389     if (environment == snowyenvironment) {
390         windvector = 0;
391         windvector.z = 3;
392         if (ambientsound)
393             emit_stream_np(stream_wind);
394
395         Object::treetextureptr.load("Textures/SnowTree.png", 0);
396         Object::bushtextureptr.load("Textures/BushSnow.png", 0);
397         Object::rocktextureptr.load("Textures/BoulderSnow.jpg", 1);
398         Object::boxtextureptr.load("Textures/SnowBox.jpg", 1);
399
400         footstepsound = footstepsn1;
401         footstepsound2 = footstepsn2;
402         footstepsound3 = footstepst1;
403         footstepsound4 = footstepst2;
404
405         terraintexture.load("Textures/Snow.jpg", 1);
406         terraintexture2.load("Textures/Rock.jpg", 1);
407
408
409         temptexdetail = texdetail;
410         if (texdetail > 1)
411             texdetail = 4;
412         skybox->load(   "Textures/Skybox(snow)/Front.jpg",
413                         "Textures/Skybox(snow)/Left.jpg",
414                         "Textures/Skybox(snow)/Back.jpg",
415                         "Textures/Skybox(snow)/Right.jpg",
416                         "Textures/Skybox(snow)/Up.jpg",
417                         "Textures/Skybox(snow)/Down.jpg");
418
419
420         texdetail = temptexdetail;
421     } else if (environment == desertenvironment) {
422         windvector = 0;
423         windvector.z = 2;
424         Object::treetextureptr.load("Textures/DesertTree.png", 0);
425         Object::bushtextureptr.load("Textures/BushDesert.png", 0);
426         Object::rocktextureptr.load("Textures/BoulderDesert.jpg", 1);
427         Object::boxtextureptr.load("Textures/DesertBox.jpg", 1);
428
429
430         if (ambientsound)
431             emit_stream_np(stream_desertambient);
432
433         footstepsound = footstepsn1;
434         footstepsound2 = footstepsn2;
435         footstepsound3 = footstepsn1;
436         footstepsound4 = footstepsn2;
437
438         terraintexture.load("Textures/Sand.jpg", 1);
439         terraintexture2.load("Textures/SandSlope.jpg", 1);
440
441
442         temptexdetail = texdetail;
443         if (texdetail > 1)
444             texdetail = 4;
445         skybox->load(   "Textures/Skybox(sand)/Front.jpg",
446                         "Textures/Skybox(sand)/Left.jpg",
447                         "Textures/Skybox(sand)/Back.jpg",
448                         "Textures/Skybox(sand)/Right.jpg",
449                         "Textures/Skybox(sand)/Up.jpg",
450                         "Textures/Skybox(sand)/Down.jpg");
451
452
453         texdetail = temptexdetail;
454     } else if (environment == grassyenvironment) {
455         windvector = 0;
456         windvector.z = 2;
457         Object::treetextureptr.load("Textures/Tree.png", 0);
458         Object::bushtextureptr.load("Textures/Bush.png", 0);
459         Object::rocktextureptr.load("Textures/Boulder.jpg", 1);
460         Object::boxtextureptr.load("Textures/GrassBox.jpg", 1);
461
462         if (ambientsound)
463             emit_stream_np(stream_wind, 100.);
464
465         footstepsound = footstepgr1;
466         footstepsound2 = footstepgr2;
467         footstepsound3 = footstepst1;
468         footstepsound4 = footstepst2;
469
470         terraintexture.load("Textures/GrassDirt.jpg", 1);
471         terraintexture2.load("Textures/MossRock.jpg", 1);
472
473
474         temptexdetail = texdetail;
475         if (texdetail > 1)
476             texdetail = 4;
477         skybox->load(   "Textures/Skybox(grass)/Front.jpg",
478                         "Textures/Skybox(grass)/Left.jpg",
479                         "Textures/Skybox(grass)/Back.jpg",
480                         "Textures/Skybox(grass)/Right.jpg",
481                         "Textures/Skybox(grass)/Up.jpg",
482                         "Textures/Skybox(grass)/Down.jpg");
483
484
485         texdetail = temptexdetail;
486     }
487     temptexdetail = texdetail;
488     texdetail = 1;
489     terrain.load("Textures/HeightMap.png");
490
491     texdetail = temptexdetail;
492 }
493
494 void Game::Loadlevel(int which)
495 {
496     stealthloading = 0;
497     whichlevel = which;
498
499     if (which == -1) {
500         Loadlevel("tutorial", true);
501     } else if (which >= 0 && which <= 15) {
502         char buf[32];
503         snprintf(buf, 32, "map%d", which + 1); // challenges
504         Loadlevel(buf);
505     } else {
506         Loadlevel("mapsave");
507     }
508 }
509
510 void Game::Loadlevel(const std::string& name, bool tutorial)
511 {
512     int indemo; // FIXME this should be removed
513     int templength;
514     float lamefloat;
515
516     LOGFUNC;
517
518     LOG(std::string("Loading level...") + name);
519
520     if (!gameon)
521         visibleloading = true;
522     if (stealthloading)
523         visibleloading = false;
524     if (!stillloading)
525         loadtime = 0;
526     gamestarted = 1;
527
528     numenvsounds = 0;
529
530     Tutorial::active = tutorial;
531
532     if (Tutorial::active) {
533         Tutorial::stage = 0;
534     }
535     if (Tutorial::stage == 0) {
536         Tutorial::stagetime = 0;
537         Tutorial::maxtime = 1;
538     }
539     pause_sound(whooshsound);
540     pause_sound(stream_firesound);
541
542     int mapvers;
543     FILE *tfile;
544     errno = 0;
545     tfile = Folders::openMandatoryFile( Folders::getResourcePath("Maps/"+name), "rb" );
546
547     pause_sound(stream_firesound);
548     scoreadded = 0;
549     windialogue = false;
550     hostiletime = 0;
551     won = 0;
552
553     //~ Animation::animations[bounceidleanim].Load("Idle", middleheight, neutral);
554
555     Dialog::dialogs.clear();
556
557     Dialog::indialogue = -1;
558     cameramode = 0;
559
560     damagedealt = 0;
561     damagetaken = 0;
562
563     if (Account::hasActive()) {
564         difficulty = Account::active().getDifficulty();
565     }
566
567     Hotspot::hotspots.clear();
568     Hotspot::current = -1;
569     bonustime = 1;
570
571     skyboxtexture = 1;
572     skyboxr = 1;
573     skyboxg = 1;
574     skyboxb = 1;
575
576     freeze = 0;
577     winfreeze = 0;
578
579     for (unsigned char i = 0; i < 100; i++)
580         bonusnum[i] = 0;
581
582     numfalls = 0;
583     numflipfail = 0;
584     numseen = 0;
585     numstaffattack = 0;
586     numswordattack = 0;
587     numknifeattack = 0;
588     numunarmedattack = 0;
589     numescaped = 0;
590     numflipped = 0;
591     numwallflipped = 0;
592     numthrowkill = 0;
593     numafterkill = 0;
594     numreversals = 0;
595     numattacks = 0;
596     maxalarmed = 0;
597     numresponded = 0;
598
599     bonustotal = startbonustotal;
600     bonus = 0;
601     gameon = 1;
602     changedelay = 0;
603     if (console) {
604         emit_sound_np(consolesuccesssound);
605         freeze = 0;
606         console = false;
607     }
608
609     if (!stealthloading) {
610         terrain.decals.clear();
611         Sprite::deleteSprites();
612
613         for (int i = 0; i < subdivision; i++) {
614             for (int j = 0; j < subdivision; j++) {
615                 terrain.patchobjectnum[i][j] = 0;
616             }
617         }
618         Game::LoadingScreen();
619     }
620
621     weapons.clear();
622     Person::players.resize(1);
623
624     funpackf(tfile, "Bi", &mapvers);
625     if (mapvers < 12) {
626         cerr << name << " has obsolete map version " << mapvers << endl;
627     }
628     if (mapvers >= 15)
629         funpackf(tfile, "Bi", &indemo);
630     else
631         indemo = 0;
632     if (mapvers >= 5)
633         funpackf(tfile, "Bi", &maptype);
634     else
635         maptype = mapkilleveryone;
636     if (mapvers >= 6)
637         funpackf(tfile, "Bi", &hostile);
638     else
639         hostile = 1;
640     if (mapvers >= 4)
641         funpackf(tfile, "Bf Bf", &viewdistance, &fadestart);
642     else {
643         viewdistance = 100;
644         fadestart = .6;
645     }
646     if (mapvers >= 2)
647         funpackf(tfile, "Bb Bf Bf Bf", &skyboxtexture, &skyboxr, &skyboxg, &skyboxb);
648     else {
649         skyboxtexture = 1;
650         skyboxr = 1;
651         skyboxg = 1;
652         skyboxb = 1;
653     }
654     if (mapvers >= 10)
655         funpackf(tfile, "Bf Bf Bf", &skyboxlightr, &skyboxlightg, &skyboxlightb);
656     else {
657         skyboxlightr = skyboxr;
658         skyboxlightg = skyboxg;
659         skyboxlightb = skyboxb;
660     }
661     /* TODO - This should be done in an other way so that we can rebuild main player as well (so coords would need to be copied from old ones after rebuilding) */
662     if (stealthloading) {
663         funpackf(tfile, "Bf Bf Bf Bf Bf Bi", &lamefloat, &lamefloat, &lamefloat, &lamefloat, &lamefloat, &Person::players[0]->num_weapons);
664     } else {
665         funpackf(tfile, "Bf Bf Bf Bf Bf Bi", &Person::players[0]->coords.x, &Person::players[0]->coords.y, &Person::players[0]->coords.z, &Person::players[0]->yaw, &Person::players[0]->targetyaw, &Person::players[0]->num_weapons);
666     }
667     if (Person::players[0]->num_weapons > 0 && Person::players[0]->num_weapons < 5)
668         for (int j = 0; j < Person::players[0]->num_weapons; j++) {
669             Person::players[0]->weaponids[j] = weapons.size();
670             int type;
671             funpackf(tfile, "Bi", &type);
672             weapons.push_back(Weapon(type, 0));
673         }
674
675     Game::LoadingScreen();
676
677     funpackf(tfile, "Bf Bf Bf", &Person::players[0]->armorhead, &Person::players[0]->armorhigh, &Person::players[0]->armorlow);
678     funpackf(tfile, "Bf Bf Bf", &Person::players[0]->protectionhead, &Person::players[0]->protectionhigh, &Person::players[0]->protectionlow);
679     funpackf(tfile, "Bf Bf Bf", &Person::players[0]->metalhead, &Person::players[0]->metalhigh, &Person::players[0]->metallow);
680     funpackf(tfile, "Bf Bf", &Person::players[0]->power, &Person::players[0]->speedmult);
681
682     funpackf(tfile, "Bi", &Person::players[0]->numclothes);
683
684     if (mapvers >= 9)
685         funpackf(tfile, "Bi Bi", &Person::players[0]->whichskin, &Person::players[0]->creature);
686     else {
687         Person::players[0]->whichskin = 0;
688         Person::players[0]->creature = rabbittype;
689     }
690
691     Person::players[0]->lastattack = -1;
692     Person::players[0]->lastattack2 = -1;
693     Person::players[0]->lastattack3 = -1;
694
695     //dialogues
696     if (mapvers >= 8) {
697         Dialog::loadDialogs(tfile);
698     }
699
700     for (int k = 0; k < Person::players[0]->numclothes; k++) {
701         funpackf(tfile, "Bi", &templength);
702         for (int l = 0; l < templength; l++)
703             funpackf(tfile, "Bb", &Person::players[0]->clothes[k][l]);
704         Person::players[0]->clothes[k][templength] = '\0';
705         funpackf(tfile, "Bf Bf Bf", &Person::players[0]->clothestintr[k], &Person::players[0]->clothestintg[k], &Person::players[0]->clothestintb[k]);
706     }
707
708     funpackf(tfile, "Bi", &environment);
709
710     if (environment != oldenvironment)
711         Setenvironment(environment);
712     oldenvironment = environment;
713
714     Object::LoadObjectsFromFile(tfile, stealthloading);
715
716     if (mapvers >= 7) {
717         int numhotspots;
718         funpackf(tfile, "Bi", &numhotspots);
719         if (numhotspots < 0) {
720             cerr << "Map " << name << " have an invalid number of hotspots" << endl;
721             numhotspots = 0;
722         }
723         Hotspot::hotspots.resize(numhotspots);
724         for (unsigned i = 0; i < Hotspot::hotspots.size(); i++) {
725             funpackf(tfile, "Bi Bf Bf Bf Bf", &Hotspot::hotspots[i].type, &Hotspot::hotspots[i].size, &Hotspot::hotspots[i].position.x, &Hotspot::hotspots[i].position.y, &Hotspot::hotspots[i].position.z);
726             funpackf(tfile, "Bi", &templength);
727             if (templength)
728                 for (int l = 0; l < templength; l++)
729                     funpackf(tfile, "Bb", &Hotspot::hotspots[i].text[l]);
730             Hotspot::hotspots[i].text[templength] = '\0';
731             if (Hotspot::hotspots[i].type == -111)
732                 indemo = 1;
733         }
734     } else {
735         Hotspot::hotspots.clear();
736     }
737
738     Game::LoadingScreen();
739
740     if (!stealthloading) {
741         Object::ComputeCenter();
742         Object::ComputeRadius();
743     }
744
745     Game::LoadingScreen();
746
747     int numplayers;
748     funpackf(tfile, "Bi", &numplayers);
749     if (numplayers > maxplayers) {
750         cout << "Warning: this level contains more players than allowed" << endl;
751     }
752     unsigned j = 1;
753     for (int i = 1; i < numplayers; i++) {
754         try {
755             Person::players.push_back(shared_ptr<Person>(new Person(tfile, mapvers, j)));
756             j++;
757         } catch (InvalidPersonException e) {
758             cerr << "Invalid Person found in " << name << endl;
759         }
760     }
761     Game::LoadingScreen();
762
763     funpackf(tfile, "Bi", &numpathpoints);
764     if (numpathpoints > 30 || numpathpoints < 0)
765         numpathpoints = 0;
766     for (int j = 0; j < numpathpoints; j++) {
767         funpackf(tfile, "Bf Bf Bf Bi", &pathpoint[j].x, &pathpoint[j].y, &pathpoint[j].z, &numpathpointconnect[j]);
768         for (int k = 0; k < numpathpointconnect[j]; k++) {
769             funpackf(tfile, "Bi", &pathpointconnect[j][k]);
770         }
771     }
772     Game::LoadingScreen();
773
774     funpackf(tfile, "Bf Bf Bf Bf", &mapcenter.x, &mapcenter.y, &mapcenter.z, &mapradius);
775
776     SetUpLighting();
777
778     if (!stealthloading) {
779         Object::AddObjectsToTerrain();
780         terrain.DoShadows();
781         Game::LoadingScreen();
782         Object::DoShadows();
783         Game::LoadingScreen();
784     }
785
786     fclose(tfile);
787
788     for (unsigned i = 0; i < Person::players.size(); i++) {
789         Game::LoadingScreen();
790         if (i == 0) {
791             Person::players[i]->burnt = 0;
792             Person::players[i]->bled = 0;
793             Person::players[i]->onfire = 0;
794             Person::players[i]->scale = .2;
795             if (mapvers < 9) {
796                 Person::players[i]->creature = rabbittype;
797             }
798         }
799         Person::players[i]->skeleton.free = 0;
800
801         Person::players[i]->skeletonLoad();
802
803         Person::players[i]->addClothes();
804
805         if (i == 0) {
806             Person::players[i]->animCurrent = bounceidleanim;
807             Person::players[i]->animTarget = bounceidleanim;
808             Person::players[i]->frameCurrent = 0;
809             Person::players[i]->frameTarget = 1;
810             Person::players[i]->target = 0;
811         }
812         Person::players[i]->speed = 1 + (float)(Random() % 100) / 1000;
813         if (difficulty == 0)
814             Person::players[i]->speed -= .2;
815         if (difficulty == 1)
816             Person::players[i]->speed -= .1;
817
818         if (i == 0) {
819             Person::players[i]->velocity = 0;
820             Person::players[i]->oldcoords = Person::players[i]->coords;
821             Person::players[i]->realoldcoords = Person::players[i]->coords;
822
823             Person::players[i]->id = i;
824             Person::players[i]->updatedelay = 0;
825             Person::players[i]->normalsupdatedelay = 0;
826
827             Person::players[i]->proportionhead = 1.2;
828             Person::players[i]->proportionbody = 1.05;
829             Person::players[i]->proportionarms = 1.00;
830             Person::players[i]->proportionlegs = 1.1;
831             Person::players[i]->proportionlegs.y = 1.05;
832             Person::players[i]->headless = 0;
833             Person::players[i]->currentoffset = 0;
834             Person::players[i]->targetoffset = 0;
835             if (Person::players[i]->creature == wolftype) {
836                 Person::players[i]->scale = .23;
837                 Person::players[i]->damagetolerance = 300;
838             } else {
839                 Person::players[i]->damagetolerance = 200;
840             }
841         }
842
843
844         Game::LoadingScreen();
845
846         if (cellophane) {
847             Person::players[i]->proportionhead.z = 0;
848             Person::players[i]->proportionbody.z = 0;
849             Person::players[i]->proportionarms.z = 0;
850             Person::players[i]->proportionlegs.z = 0;
851         }
852
853         Person::players[i]->tempanimation = Animation("Tempanim", lowheight, neutral);
854
855         if (i == 0) {
856             Person::players[i]->headmorphness = 0;
857             Person::players[i]->targetheadmorphness = 1;
858             Person::players[i]->headmorphstart = 0;
859             Person::players[i]->headmorphend = 0;
860
861             Person::players[i]->pausetime = 0;
862
863             Person::players[i]->dead = 0;
864             Person::players[i]->jumppower = 5;
865             Person::players[i]->damage = 0;
866             Person::players[i]->permanentdamage = 0;
867             Person::players[i]->superpermanentdamage = 0;
868
869             Person::players[i]->forwardkeydown = 0;
870             Person::players[i]->leftkeydown = 0;
871             Person::players[i]->backkeydown = 0;
872             Person::players[i]->rightkeydown = 0;
873             Person::players[i]->jumpkeydown = 0;
874             Person::players[i]->crouchkeydown = 0;
875             Person::players[i]->throwkeydown = 0;
876
877             Person::players[i]->collided = -10;
878             Person::players[i]->loaded = 1;
879             Person::players[i]->bloodloss = 0;
880             Person::players[i]->weaponactive = -1;
881             Person::players[i]->weaponstuck = -1;
882             Person::players[i]->bleeding = 0;
883             Person::players[i]->deathbleeding = 0;
884             Person::players[i]->stunned = 0;
885             Person::players[i]->hasvictim = 0;
886             Person::players[i]->wentforweapon = 0;
887         }
888     }
889
890     Person::players[0]->aitype = playercontrolled;
891
892     if (difficulty == 1) {
893         Person::players[0]->power = 1 / .9;
894         Person::players[0]->damagetolerance = 250;
895     } else if (difficulty == 0) {
896         Person::players[0]->power = 1 / .8;
897         Person::players[0]->damagetolerance = 300;
898         Person::players[0]->armorhead *= 1.5;
899         Person::players[0]->armorhigh *= 1.5;
900         Person::players[0]->armorlow *= 1.5;
901     }
902
903     cameraloc = Person::players[0]->coords;
904     cameraloc.y += 5;
905     yaw = Person::players[0]->yaw;
906
907     hawkcoords = Person::players[0]->coords;
908     hawkcoords.y += 30;
909
910     Game::LoadingScreen();
911
912     LOG("Starting background music...");
913
914     OPENAL_StopSound(OPENAL_ALL);
915     if (ambientsound) {
916         if (environment == snowyenvironment) {
917             emit_stream_np(stream_wind);
918         } else if (environment == desertenvironment) {
919             emit_stream_np(stream_desertambient);
920         } else if (environment == grassyenvironment) {
921             emit_stream_np(stream_wind, 100.);
922         }
923     }
924     oldmusicvolume[0] = 0;
925     oldmusicvolume[1] = 0;
926     oldmusicvolume[2] = 0;
927     oldmusicvolume[3] = 0;
928
929     leveltime = 0;
930     wonleveltime = 0;
931     visibleloading = false;
932 }
933
934 void doDevKeys()
935 {
936     float headprop, bodyprop, armprop, legprop;
937     if (!devtools) {
938         return;
939     }
940
941     if (Input::isKeyDown(SDL_SCANCODE_LALT)) {
942         /* Invicible */
943         if (Input::isKeyPressed(SDL_SCANCODE_H)) {
944             Person::players[0]->damagetolerance = 200000;
945             Person::players[0]->damage = 0;
946             Person::players[0]->burnt = 0;
947             Person::players[0]->permanentdamage = 0;
948             Person::players[0]->superpermanentdamage = 0;
949         }
950
951         /* Change environment */
952         if (Input::isKeyPressed(SDL_SCANCODE_J)) {
953             environment++;
954             if (environment > 2)
955                 environment = 0;
956             Setenvironment(environment);
957         }
958
959         /* Camera mode */
960         if (Input::isKeyPressed(SDL_SCANCODE_C)) {
961             cameramode = !cameramode;
962         }
963
964         /* Toggle Slow motion */
965         if (Input::isKeyPressed(SDL_SCANCODE_B)) {
966             slomo = 1 - slomo;
967             slomodelay = 1000;
968         }
969
970         /* Ragdoll */
971         if (Input::isKeyPressed(SDL_SCANCODE_N)) {
972             Person::players[0]->RagDoll(0);
973
974             emit_sound_at(whooshsound, Person::players[0]->coords, 128.);
975         }
976
977         /* Grow tree leaves?? */
978         if (Input::isKeyPressed(SDL_SCANCODE_Y)) {
979             for (auto& an_object : Object::objects) {
980                 if (an_object->type == treeleavestype) {
981                     an_object->scale *= .9;
982                 }
983             }
984         }
985
986         /* Change (or add) weapon */
987         if (Input::isKeyPressed(SDL_SCANCODE_X)) {
988             int closest = 0;
989             if (!Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
990                 closest = findClosestPlayer();
991             }
992
993             if (closest >= 0) {
994                 if (Person::players[closest]->num_weapons > 0) {
995                     if (weapons[Person::players[closest]->weaponids[0]].getType() == sword)
996                         weapons[Person::players[closest]->weaponids[0]].setType(staff);
997                     else if (weapons[Person::players[closest]->weaponids[0]].getType() == staff)
998                         weapons[Person::players[closest]->weaponids[0]].setType(knife);
999                     else
1000                         weapons[Person::players[closest]->weaponids[0]].setType(sword);
1001                 } else {
1002                     Person::players[closest]->weaponids[0] = weapons.size();
1003
1004                     weapons.push_back(Weapon(knife, closest));
1005
1006                     Person::players[closest]->num_weapons = 1;
1007                 }
1008             }
1009         }
1010
1011         /* Change yaw? */
1012         if (Input::isKeyDown(SDL_SCANCODE_U)) {
1013             int closest = 0;
1014             if (!Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1015                 closest = findClosestPlayer();
1016             }
1017
1018             if (closest >= 0) {
1019                 Person::players[closest]->yaw += multiplier * 50;
1020                 Person::players[closest]->targetyaw = Person::players[closest]->yaw;
1021             }
1022         }
1023
1024         /* Toggle fire */
1025         if (Input::isKeyPressed(SDL_SCANCODE_F)) {
1026             Person::players[0]->onfire = 1 - Person::players[0]->onfire;
1027             if (Person::players[0]->onfire) {
1028                 Person::players[0]->CatchFire();
1029             }
1030             if (!Person::players[0]->onfire) {
1031                 emit_sound_at(fireendsound, Person::players[0]->coords);
1032                 pause_sound(stream_firesound);
1033             }
1034         }
1035
1036         /* Change skin */
1037         if (Input::isKeyPressed(SDL_SCANCODE_O) && !Input::isKeyDown(SDL_SCANCODE_LSHIFT)) {
1038             int closest = 0;
1039             if (!Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1040                 closest = findClosestPlayer();
1041             }
1042
1043             if (closest >= 0) {
1044                 Person::players[closest]->whichskin++;
1045                 if (Person::players[closest]->whichskin > 9)
1046                     Person::players[closest]->whichskin = 0;
1047                 if (Person::players[closest]->whichskin > 2 && Person::players[closest]->creature == wolftype)
1048                     Person::players[closest]->whichskin = 0;
1049
1050                 Person::players[closest]->skeleton.drawmodel.textureptr.load(creatureskin[Person::players[closest]->creature][Person::players[closest]->whichskin], 1,
1051                         &Person::players[closest]->skeleton.skinText[0], &Person::players[closest]->skeleton.skinsize);
1052             }
1053
1054             Person::players[closest]->addClothes();
1055         }
1056
1057         /* Change creature type */
1058         if (Input::isKeyPressed(SDL_SCANCODE_O) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) {
1059             int closest = 0;
1060             if (!Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1061                 closest = findClosestPlayer();
1062             }
1063
1064             if (closest >= 0) {
1065                 if (Person::players[closest]->creature == wolftype) {
1066                     headprop = Person::players[closest]->proportionhead.x / 1.1;
1067                     bodyprop = Person::players[closest]->proportionbody.x / 1.1;
1068                     armprop = Person::players[closest]->proportionarms.x / 1.1;
1069                     legprop = Person::players[closest]->proportionlegs.x / 1.1;
1070                 } else {
1071                     // rabbittype
1072                     headprop = Person::players[closest]->proportionhead.x / 1.2;
1073                     bodyprop = Person::players[closest]->proportionbody.x / 1.05;
1074                     armprop = Person::players[closest]->proportionarms.x / 1.00;
1075                     legprop = Person::players[closest]->proportionlegs.x / 1.1;
1076                 }
1077
1078
1079                 if (Person::players[closest]->creature == rabbittype) {
1080                     Person::players[closest]->creature = wolftype;
1081                     Person::players[closest]->whichskin = 0;
1082                     Person::players[closest]->skeletonLoad();
1083
1084                     Person::players[closest]->proportionhead = 1.1;
1085                     Person::players[closest]->proportionbody = 1.1;
1086                     Person::players[closest]->proportionarms = 1.1;
1087                     Person::players[closest]->proportionlegs = 1.1;
1088                     Person::players[closest]->proportionlegs.y = 1.1;
1089                     Person::players[closest]->scale = .23 * 5 * Person::players[0]->scale;
1090
1091                     Person::players[closest]->damagetolerance = 300;
1092                 } else {
1093                     Person::players[closest]->creature = rabbittype;
1094                     Person::players[closest]->whichskin = 0;
1095                     Person::players[closest]->skeletonLoad(true);
1096
1097                     Person::players[closest]->proportionhead = 1.2;
1098                     Person::players[closest]->proportionbody = 1.05;
1099                     Person::players[closest]->proportionarms = 1.00;
1100                     Person::players[closest]->proportionlegs = 1.1;
1101                     Person::players[closest]->proportionlegs.y = 1.05;
1102                     Person::players[closest]->scale = .2 * 5 * Person::players[0]->scale;
1103
1104                     Person::players[closest]->damagetolerance = 200;
1105                 }
1106
1107                 if (Person::players[closest]->creature == wolftype) {
1108                     Person::players[closest]->proportionhead = 1.1 * headprop;
1109                     Person::players[closest]->proportionbody = 1.1 * bodyprop;
1110                     Person::players[closest]->proportionarms = 1.1 * armprop;
1111                     Person::players[closest]->proportionlegs = 1.1 * legprop;
1112                 }
1113
1114                 if (Person::players[closest]->creature == rabbittype) {
1115                     Person::players[closest]->proportionhead = 1.2 * headprop;
1116                     Person::players[closest]->proportionbody = 1.05 * bodyprop;
1117                     Person::players[closest]->proportionarms = 1.00 * armprop;
1118                     Person::players[closest]->proportionlegs = 1.1 * legprop;
1119                     Person::players[closest]->proportionlegs.y = 1.05 * legprop;
1120                 }
1121
1122             }
1123         }
1124
1125         /* Decapitate */
1126         if (((Input::isKeyPressed(SDL_SCANCODE_I) && !Input::isKeyDown(SDL_SCANCODE_LSHIFT)))) {
1127             int closest = -1;
1128             float closestdist = std::numeric_limits<float>::max();
1129
1130             for (unsigned i = 1; i < Person::players.size(); i++) {
1131                 float distance = distsq(&Person::players[i]->coords, &Person::players[0]->coords);
1132                 if (!Person::players[i]->headless)
1133                     if (distance < closestdist) {
1134                         closestdist = distance;
1135                         closest = i;
1136                     }
1137             }
1138
1139             XYZ flatfacing2, flatvelocity2;
1140             XYZ blah;
1141             if (closest != -1 && distsq(&Person::players[closest]->coords, &Person::players[0]->coords) < 144) {
1142                 blah = Person::players[closest]->coords;
1143                 XYZ headspurtdirection;
1144                 //int i = Person::players[closest]->skeleton.jointlabels[head];
1145                 Joint& headjoint = Person::players[closest]->joint(head);
1146                 for (unsigned k = 0; k < Person::players[closest]->skeleton.joints.size(); k++) {
1147                     if (!Person::players[closest]->skeleton.free)
1148                         flatvelocity2 = Person::players[closest]->velocity;
1149                     if (Person::players[closest]->skeleton.free)
1150                         flatvelocity2 = headjoint.velocity;
1151                     if (!Person::players[closest]->skeleton.free)
1152                         flatfacing2 = DoRotation(DoRotation(DoRotation(headjoint.position, 0, 0, Person::players[closest]->tilt), Person::players[closest]->tilt2, 0, 0), 0, Person::players[closest]->yaw, 0) * Person::players[closest]->scale + Person::players[closest]->coords;
1153                     if (Person::players[closest]->skeleton.free)
1154                         flatfacing2 = headjoint.position * Person::players[closest]->scale + Person::players[closest]->coords;
1155                     flatvelocity2.x += (float)(abs(Random() % 100) - 50) / 10;
1156                     flatvelocity2.y += (float)(abs(Random() % 100) - 50) / 10;
1157                     flatvelocity2.z += (float)(abs(Random() % 100) - 50) / 10;
1158                     headspurtdirection = headjoint.position - Person::players[closest]->jointPos(neck);
1159                     Normalise(&headspurtdirection);
1160                     Sprite::MakeSprite(bloodflamesprite, flatfacing2, flatvelocity2, 1, 1, 1, .6, 1);
1161                     flatvelocity2 += headspurtdirection * 8;
1162                     Sprite::MakeSprite(bloodsprite, flatfacing2, flatvelocity2 / 2, 1, 1, 1, .16, 1);
1163                 }
1164                 Sprite::MakeSprite(cloudsprite, flatfacing2, flatvelocity2 * 0, .6, 0, 0, 1, .5);
1165
1166                 emit_sound_at(splattersound, blah);
1167                 emit_sound_at(breaksound2, blah, 100.);
1168
1169                 if (Person::players[closest]->skeleton.free == 2)
1170                     Person::players[closest]->skeleton.free = 0;
1171                 Person::players[closest]->RagDoll(0);
1172                 Person::players[closest]->dead = 2;
1173                 Person::players[closest]->headless = 1;
1174                 Person::players[closest]->DoBloodBig(3, 165);
1175
1176                 camerashake += .3;
1177             }
1178         }
1179
1180         /* Explode */
1181         if (((Input::isKeyPressed(SDL_SCANCODE_I) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)))) {
1182             int closest = findClosestPlayer();
1183             XYZ flatfacing2, flatvelocity2;
1184             XYZ blah;
1185             if (closest >= 0 && distsq(&Person::players[closest]->coords, &Person::players[0]->coords) < 144) {
1186                 blah = Person::players[closest]->coords;
1187                 emit_sound_at(splattersound, blah);
1188                 emit_sound_at(breaksound2, blah);
1189
1190                 for (unsigned i = 0; i < Person::players[closest]->skeleton.joints.size(); i++) {
1191                     if (!Person::players[closest]->skeleton.free)
1192                         flatvelocity2 = Person::players[closest]->velocity;
1193                     if (Person::players[closest]->skeleton.free)
1194                         flatvelocity2 = Person::players[closest]->skeleton.joints[i].velocity;
1195                     if (!Person::players[closest]->skeleton.free)
1196                         flatfacing2 = DoRotation(DoRotation(DoRotation(Person::players[closest]->skeleton.joints[i].position, 0, 0, Person::players[closest]->tilt), Person::players[closest]->tilt2, 0, 0), 0, Person::players[closest]->yaw, 0) * Person::players[closest]->scale + Person::players[closest]->coords;
1197                     if (Person::players[closest]->skeleton.free)
1198                         flatfacing2 = Person::players[closest]->skeleton.joints[i].position * Person::players[closest]->scale + Person::players[closest]->coords;
1199                     flatvelocity2.x += (float)(abs(Random() % 100) - 50) / 10;
1200                     flatvelocity2.y += (float)(abs(Random() % 100) - 50) / 10;
1201                     flatvelocity2.z += (float)(abs(Random() % 100) - 50) / 10;
1202                     Sprite::MakeSprite(bloodflamesprite, flatfacing2, flatvelocity2, 1, 1, 1, 3, 1);
1203                     Sprite::MakeSprite(bloodsprite, flatfacing2, flatvelocity2, 1, 1, 1, .3, 1);
1204                     Sprite::MakeSprite(cloudsprite, flatfacing2, flatvelocity2 * 0, .6, 0, 0, 1, .5);
1205                 }
1206
1207                 for (unsigned i = 0; i < Person::players[closest]->skeleton.joints.size(); i++) {
1208                     if (!Person::players[closest]->skeleton.free)
1209                         flatvelocity2 = Person::players[closest]->velocity;
1210                     if (Person::players[closest]->skeleton.free)
1211                         flatvelocity2 = Person::players[closest]->skeleton.joints[i].velocity;
1212                     if (!Person::players[closest]->skeleton.free)
1213                         flatfacing2 = DoRotation(DoRotation(DoRotation(Person::players[closest]->skeleton.joints[i].position, 0, 0, Person::players[closest]->tilt), Person::players[closest]->tilt2, 0, 0), 0, Person::players[closest]->yaw, 0) * Person::players[closest]->scale + Person::players[closest]->coords;
1214                     if (Person::players[closest]->skeleton.free)
1215                         flatfacing2 = Person::players[closest]->skeleton.joints[i].position * Person::players[closest]->scale + Person::players[closest]->coords;
1216                     flatvelocity2.x += (float)(abs(Random() % 100) - 50) / 10;
1217                     flatvelocity2.y += (float)(abs(Random() % 100) - 50) / 10;
1218                     flatvelocity2.z += (float)(abs(Random() % 100) - 50) / 10;
1219                     Sprite::MakeSprite(bloodflamesprite, flatfacing2, flatvelocity2, 1, 1, 1, 3, 1);
1220                     Sprite::MakeSprite(bloodsprite, flatfacing2, flatvelocity2, 1, 1, 1, .4, 1);
1221                 }
1222
1223                 for (unsigned i = 0; i < Person::players[closest]->skeleton.joints.size(); i++) {
1224                     if (!Person::players[closest]->skeleton.free)
1225                         flatvelocity2 = Person::players[closest]->velocity;
1226                     if (Person::players[closest]->skeleton.free)
1227                         flatvelocity2 = Person::players[closest]->skeleton.joints[i].velocity;
1228                     if (!Person::players[closest]->skeleton.free)
1229                         flatfacing2 = DoRotation(DoRotation(DoRotation(Person::players[closest]->skeleton.joints[i].position, 0, 0, Person::players[closest]->tilt), Person::players[closest]->tilt2, 0, 0), 0, Person::players[closest]->yaw, 0) * Person::players[closest]->scale + Person::players[closest]->coords;
1230                     if (Person::players[closest]->skeleton.free)
1231                         flatfacing2 = Person::players[closest]->skeleton.joints[i].position * Person::players[closest]->scale + Person::players[closest]->coords;
1232                     flatvelocity2.x += (float)(abs(Random() % 100) - 50) / 10;
1233                     flatvelocity2.y += (float)(abs(Random() % 100) - 50) / 10;
1234                     flatvelocity2.z += (float)(abs(Random() % 100) - 50) / 10;
1235                     Sprite::MakeSprite(bloodflamesprite, flatfacing2, flatvelocity2 * 2, 1, 1, 1, 3, 1);
1236                     Sprite::MakeSprite(bloodsprite, flatfacing2, flatvelocity2 * 2, 1, 1, 1, .4, 1);
1237                 }
1238
1239                 for (unsigned i = 0; i < Person::players[closest]->skeleton.joints.size(); i++) {
1240                     if (!Person::players[closest]->skeleton.free)
1241                         flatvelocity2 = Person::players[closest]->velocity;
1242                     if (Person::players[closest]->skeleton.free)
1243                         flatvelocity2 = Person::players[closest]->skeleton.joints[i].velocity;
1244                     if (!Person::players[closest]->skeleton.free)
1245                         flatfacing2 = DoRotation(DoRotation(DoRotation(Person::players[closest]->skeleton.joints[i].position, 0, 0, Person::players[closest]->tilt), Person::players[closest]->tilt2, 0, 0), 0, Person::players[closest]->yaw, 0) * Person::players[closest]->scale + Person::players[closest]->coords;
1246                     if (Person::players[closest]->skeleton.free)
1247                         flatfacing2 = Person::players[closest]->skeleton.joints[i].position * Person::players[closest]->scale + Person::players[closest]->coords;
1248                     flatvelocity2.x += (float)(abs(Random() % 100) - 50) / 10;
1249                     flatvelocity2.y += (float)(abs(Random() % 100) - 50) / 10;
1250                     flatvelocity2.z += (float)(abs(Random() % 100) - 50) / 10;
1251                     Sprite::MakeSprite(bloodflamesprite, flatfacing2, flatvelocity2 * 2, 1, 1, 1, 3, 1);
1252                     Sprite::MakeSprite(bloodsprite, flatfacing2, flatvelocity2 * 2, 1, 1, 1, .4, 1);
1253                 }
1254
1255                 XYZ temppos;
1256                 for (unsigned j = 0; j < Person::players.size(); j++) {
1257                     if (int(j) != closest) {
1258                         if (distsq(&Person::players[j]->coords, &Person::players[closest]->coords) < 25) {
1259                             Person::players[j]->DoDamage((25 - distsq(&Person::players[j]->coords, &Person::players[closest]->coords)) * 60);
1260                             if (Person::players[j]->skeleton.free == 2)
1261                                 Person::players[j]->skeleton.free = 1;
1262                             Person::players[j]->skeleton.longdead = 0;
1263                             Person::players[j]->RagDoll(0);
1264                             for (unsigned i = 0; i < Person::players[j]->skeleton.joints.size(); i++) {
1265                                 temppos = Person::players[j]->skeleton.joints[i].position + Person::players[j]->coords;
1266                                 if (distsq(&temppos, &Person::players[closest]->coords) < 25) {
1267                                     flatvelocity2 = temppos - Person::players[closest]->coords;
1268                                     Normalise(&flatvelocity2);
1269                                     Person::players[j]->skeleton.joints[i].velocity += flatvelocity2 * ((20 - distsq(&temppos, &Person::players[closest]->coords)) * 20);
1270                                 }
1271                             }
1272                         }
1273                     }
1274                 }
1275
1276                 Person::players[closest]->DoDamage(10000);
1277                 Person::players[closest]->RagDoll(0);
1278                 Person::players[closest]->dead = 2;
1279                 Person::players[closest]->coords = 20;
1280                 Person::players[closest]->skeleton.free = 2;
1281
1282                 camerashake += .6;
1283
1284             }
1285         }
1286
1287         /* Enable editor */
1288         if (Input::isKeyPressed(SDL_SCANCODE_M) && !Input::isKeyDown(SDL_SCANCODE_LSHIFT)) {
1289             editorenabled = !editorenabled;
1290             if (editorenabled) {
1291                 Person::players[0]->damagetolerance = 100000;
1292             } else {
1293                 Person::players[0]->damagetolerance = 200;
1294             }
1295             Person::players[0]->damage = 0; // these lines were in both if and else, but I think they would better fit in the if
1296             Person::players[0]->permanentdamage = 0;
1297             Person::players[0]->superpermanentdamage = 0;
1298             Person::players[0]->bloodloss = 0;
1299             Person::players[0]->deathbleeding = 0;
1300         }
1301     }
1302
1303     //skip level
1304     if (whichlevel != -2 && Input::isKeyPressed(SDL_SCANCODE_K) && Input::isKeyDown(SDL_SCANCODE_LSHIFT) && !editorenabled) {
1305         targetlevel++;
1306         if (targetlevel > numchallengelevels - 1)
1307             targetlevel = 0;
1308         loading = 1;
1309         leveltime = 5;
1310     }
1311
1312     if (editorenabled) {
1313         if (Input::isKeyPressed(SDL_SCANCODE_DELETE) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) {
1314             int closest = findClosestPlayer();
1315             if (closest >= 0) {
1316                 Person::players.erase(Person::players.begin()+closest);
1317             }
1318         }
1319
1320         if (Input::isKeyPressed(SDL_SCANCODE_DELETE) && Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1321             int closest = findClosestObject();
1322             if (closest >= 0) {
1323                 Object::objects[closest]->position.y -= 500;
1324             }
1325         }
1326
1327         if (Input::isKeyPressed(SDL_SCANCODE_M) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) {
1328             if (Object::objects.size() < max_objects - 1) {
1329                 XYZ scenecoords;
1330                 scenecoords.x = Person::players[0]->coords.x;
1331                 scenecoords.z = Person::players[0]->coords.z;
1332                 scenecoords.y = Person::players[0]->coords.y - 3;
1333                 if (editortype == bushtype)
1334                     scenecoords.y = Person::players[0]->coords.y - .5;
1335                 if (editortype == firetype)
1336                     scenecoords.y = Person::players[0]->coords.y - .5;
1337                 float temprotat, temprotat2;
1338                 temprotat = editoryaw;
1339                 temprotat2 = editorpitch;
1340                 if (temprotat < 0 || editortype == bushtype)
1341                     temprotat = Random() % 360;
1342                 if (temprotat2 < 0)
1343                     temprotat2 = Random() % 360;
1344
1345                 Object::MakeObject(editortype, scenecoords, (int)temprotat - ((int)temprotat) % 30, (int)temprotat2, editorsize);
1346                 if (editortype == treetrunktype)
1347                     Object::MakeObject(treeleavestype, scenecoords, Random() % 360 * (temprotat2 < 2) + (int)editoryaw - ((int)editoryaw) % 30, editorpitch, editorsize);
1348             }
1349         }
1350
1351         if (Input::isKeyPressed(SDL_SCANCODE_P) && Input::isKeyDown(SDL_SCANCODE_LSHIFT) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1352             Person::players.push_back(shared_ptr<Person>(new Person()));
1353
1354             Person::players.back()->id = Person::players.size()-1;
1355
1356             Person::players.back()->scale = Person::players[0]->scale;
1357             Person::players.back()->creature = rabbittype;
1358             Person::players.back()->howactive = editoractive;
1359
1360             int k = abs(Random() % 2) + 1;
1361             if (k == 0) {
1362                 Person::players.back()->whichskin = 0;
1363             } else if (k == 1) {
1364                 Person::players.back()->whichskin = 1;
1365             } else {
1366                 Person::players.back()->whichskin = 2;
1367             }
1368
1369             Person::players.back()->skeletonLoad(true);
1370
1371             Person::players.back()->skeleton.drawmodelclothes.textureptr.load("Textures/Belt.png", 1);
1372             Person::players.back()->speed = 1 + (float)(Random() % 100) / 1000;
1373
1374             Person::players.back()->targetyaw = Person::players[0]->targetyaw;
1375             Person::players.back()->yaw = Person::players[0]->yaw;
1376
1377             Person::players.back()->coords = Person::players[0]->coords;
1378             Person::players.back()->oldcoords = Person::players.back()->coords;
1379             Person::players.back()->realoldcoords = Person::players.back()->coords;
1380
1381             if (Person::players[0]->creature == wolftype) {
1382                 headprop = Person::players[0]->proportionhead.x / 1.1;
1383                 bodyprop = Person::players[0]->proportionbody.x / 1.1;
1384                 armprop = Person::players[0]->proportionarms.x / 1.1;
1385                 legprop = Person::players[0]->proportionlegs.x / 1.1;
1386             } else {
1387                 // rabbittype
1388                 headprop = Person::players[0]->proportionhead.x / 1.2;
1389                 bodyprop = Person::players[0]->proportionbody.x / 1.05;
1390                 armprop = Person::players[0]->proportionarms.x / 1.00;
1391                 legprop = Person::players[0]->proportionlegs.x / 1.1;
1392             }
1393
1394             if (Person::players.back()->creature == wolftype) {
1395                 Person::players.back()->proportionhead = 1.1 * headprop;
1396                 Person::players.back()->proportionbody = 1.1 * bodyprop;
1397                 Person::players.back()->proportionarms = 1.1 * armprop;
1398                 Person::players.back()->proportionlegs = 1.1 * legprop;
1399             }
1400
1401             if (Person::players.back()->creature == rabbittype) {
1402                 Person::players.back()->proportionhead = 1.2 * headprop;
1403                 Person::players.back()->proportionbody = 1.05 * bodyprop;
1404                 Person::players.back()->proportionarms = 1.00 * armprop;
1405                 Person::players.back()->proportionlegs = 1.1 * legprop;
1406                 Person::players.back()->proportionlegs.y = 1.05 * legprop;
1407             }
1408
1409             if (cellophane) {
1410                 Person::players.back()->proportionhead.z = 0;
1411                 Person::players.back()->proportionbody.z = 0;
1412                 Person::players.back()->proportionarms.z = 0;
1413                 Person::players.back()->proportionlegs.z = 0;
1414             }
1415
1416             Person::players.back()->tempanimation = Animation("Tempanim", lowheight, neutral);
1417
1418             Person::players.back()->damagetolerance = 200;
1419
1420             Person::players.back()->protectionhead = Person::players[0]->protectionhead;
1421             Person::players.back()->protectionhigh = Person::players[0]->protectionhigh;
1422             Person::players.back()->protectionlow = Person::players[0]->protectionlow;
1423             Person::players.back()->armorhead = Person::players[0]->armorhead;
1424             Person::players.back()->armorhigh = Person::players[0]->armorhigh;
1425             Person::players.back()->armorlow = Person::players[0]->armorlow;
1426             Person::players.back()->metalhead = Person::players[0]->metalhead;
1427             Person::players.back()->metalhigh = Person::players[0]->metalhigh;
1428             Person::players.back()->metallow = Person::players[0]->metallow;
1429
1430             Person::players.back()->immobile = Person::players[0]->immobile;
1431
1432             Person::players.back()->numclothes = Person::players[0]->numclothes;
1433             for (int i = 0; i < Person::players.back()->numclothes; i++) {
1434                 strcpy(Person::players.back()->clothes[i], Person::players[0]->clothes[i]);
1435                 Person::players.back()->clothestintr[i] = Person::players[0]->clothestintr[i];
1436                 Person::players.back()->clothestintg[i] = Person::players[0]->clothestintg[i];
1437                 Person::players.back()->clothestintb[i] = Person::players[0]->clothestintb[i];
1438             }
1439             Person::players.back()->addClothes();
1440
1441             Person::players.back()->power = Person::players[0]->power;
1442             Person::players.back()->speedmult = Person::players[0]->speedmult;
1443
1444             Person::players.back()->loaded = true;
1445         }
1446
1447         if (Input::isKeyPressed(SDL_SCANCODE_P) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) {
1448             if (Person::players.back()->numwaypoints < 90) {
1449                 Person::players.back()->waypoints[Person::players.back()->numwaypoints] = Person::players[0]->coords;
1450                 Person::players.back()->waypointtype[Person::players.back()->numwaypoints] = editorpathtype;
1451                 Person::players.back()->numwaypoints++;
1452             }
1453         }
1454
1455         if (Input::isKeyPressed(SDL_SCANCODE_P) && Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1456             if (numpathpoints < 30) {
1457                 bool connected, alreadyconnected;
1458                 connected = 0;
1459                 if (numpathpoints > 1)
1460                     for (int i = 0; i < numpathpoints; i++) {
1461                         if (distsq(&pathpoint[i], &Person::players[0]->coords) < .5 && i != pathpointselected && !connected) {
1462                             alreadyconnected = 0;
1463                             for (int j = 0; j < numpathpointconnect[pathpointselected]; j++) {
1464                                 if (pathpointconnect[pathpointselected][j] == i)
1465                                     alreadyconnected = 1;
1466                             }
1467                             if (!alreadyconnected) {
1468                                 numpathpointconnect[pathpointselected]++;
1469                                 connected = 1;
1470                                 pathpointconnect[pathpointselected][numpathpointconnect[pathpointselected] - 1] = i;
1471                             }
1472                         }
1473                     }
1474                 if (!connected) {
1475                     numpathpoints++;
1476                     pathpoint[numpathpoints - 1] = Person::players[0]->coords;
1477                     numpathpointconnect[numpathpoints - 1] = 0;
1478                     if (numpathpoints > 1 && pathpointselected != -1) {
1479                         numpathpointconnect[pathpointselected]++;
1480                         pathpointconnect[pathpointselected][numpathpointconnect[pathpointselected] - 1] = numpathpoints - 1;
1481                     }
1482                     pathpointselected = numpathpoints - 1;
1483                 }
1484             }
1485         }
1486
1487         if (Input::isKeyPressed(SDL_SCANCODE_PERIOD)) {
1488             pathpointselected++;
1489             if (pathpointselected >= numpathpoints)
1490                 pathpointselected = -1;
1491         }
1492
1493         if (Input::isKeyPressed(SDL_SCANCODE_COMMA) && !Input::isKeyDown(SDL_SCANCODE_LSHIFT)) {
1494             pathpointselected--;
1495             if (pathpointselected <= -2)
1496                 pathpointselected = numpathpoints - 1;
1497         }
1498
1499         if (Input::isKeyPressed(SDL_SCANCODE_COMMA) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) {
1500             if (pathpointselected != -1) {
1501                 numpathpoints--;
1502                 pathpoint[pathpointselected] = pathpoint[numpathpoints];
1503                 numpathpointconnect[pathpointselected] = numpathpointconnect[numpathpoints];
1504                 for (int i = 0; i < numpathpointconnect[pathpointselected]; i++) {
1505                     pathpointconnect[pathpointselected][i] = pathpointconnect[numpathpoints][i];
1506                 }
1507                 for (int i = 0; i < numpathpoints; i++) {
1508                     for (int j = 0; j < numpathpointconnect[i]; j++) {
1509                         if (pathpointconnect[i][j] == pathpointselected) {
1510                             pathpointconnect[i][j] = pathpointconnect[i][numpathpointconnect[i] - 1];
1511                             numpathpointconnect[i]--;
1512                         }
1513                         if (pathpointconnect[i][j] == numpathpoints) {
1514                             pathpointconnect[i][j] = pathpointselected;
1515                         }
1516                     }
1517                 }
1518                 pathpointselected = numpathpoints - 1;
1519             }
1520         }
1521
1522         if (Input::isKeyPressed(SDL_SCANCODE_LEFT) && Input::isKeyDown(SDL_SCANCODE_LSHIFT) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1523             editortype--;
1524             if (editortype == treeleavestype || editortype == 10)
1525                 editortype--;
1526             if (editortype < 0)
1527                 editortype = firetype;
1528         }
1529
1530         if (Input::isKeyPressed(SDL_SCANCODE_RIGHT) && Input::isKeyDown(SDL_SCANCODE_LSHIFT) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1531             editortype++;
1532             if (editortype == treeleavestype || editortype == 10)
1533                 editortype++;
1534             if (editortype > firetype)
1535                 editortype = 0;
1536         }
1537
1538         if (Input::isKeyDown(SDL_SCANCODE_LEFT) && !Input::isKeyDown(SDL_SCANCODE_LSHIFT) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1539             editoryaw -= multiplier * 100;
1540             if (editoryaw < -.01)
1541                 editoryaw = -.01;
1542         }
1543
1544         if (Input::isKeyDown(SDL_SCANCODE_RIGHT) && !Input::isKeyDown(SDL_SCANCODE_LSHIFT) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1545             editoryaw += multiplier * 100;
1546         }
1547
1548         if (Input::isKeyDown(SDL_SCANCODE_UP) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1549             editorsize += multiplier;
1550         }
1551
1552         if (Input::isKeyDown(SDL_SCANCODE_DOWN) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1553             editorsize -= multiplier;
1554             if (editorsize < .1)
1555                 editorsize = .1;
1556         }
1557
1558
1559         if (Input::isKeyPressed(SDL_SCANCODE_LEFT) && Input::isKeyDown(SDL_SCANCODE_LSHIFT) && Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1560             mapradius -= multiplier * 10;
1561         }
1562
1563         if (Input::isKeyPressed(SDL_SCANCODE_RIGHT) && Input::isKeyDown(SDL_SCANCODE_LSHIFT) && Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1564             mapradius += multiplier * 10;
1565         }
1566
1567         if (Input::isKeyDown(SDL_SCANCODE_UP) && Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1568             editorpitch += multiplier * 100;
1569         }
1570
1571         if (Input::isKeyDown(SDL_SCANCODE_DOWN) && Input::isKeyDown(SDL_SCANCODE_LCTRL)) {
1572             editorpitch -= multiplier * 100;
1573             if (editorpitch < -.01)
1574                 editorpitch = -.01;
1575         }
1576
1577         if (Input::isKeyPressed(SDL_SCANCODE_DELETE) && Object::objects.size() && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) {
1578             int closest = findClosestObject();
1579             if (closest >= 0) {
1580                 Object::DeleteObject(closest);
1581             }
1582         }
1583     }
1584 }
1585
1586 void doJumpReversals()
1587 {
1588     for (unsigned k = 0; k < Person::players.size(); k++) {
1589         for (unsigned i = k + 1; i < Person::players.size(); i++) {
1590             if (     Person::players[k]->skeleton.free == 0 &&
1591                      Person::players[i]->skeleton.oldfree == 0 &&
1592                      (Person::players[i]->animTarget == jumpupanim ||
1593                       Person::players[k]->animTarget == jumpupanim) &&
1594                      (Person::players[i]->aitype == playercontrolled ||
1595                       Person::players[k]->aitype == playercontrolled) &&
1596                      ((Person::players[i]->aitype == attacktypecutoff && Person::players[i]->stunned <= 0) ||
1597                       (Person::players[k]->aitype == attacktypecutoff && Person::players[k]->stunned <= 0))) {
1598                 if (     distsq(&Person::players[i]->coords, &Person::players[k]->coords) < 10 * sq((Person::players[i]->scale + Person::players[k]->scale) * 2.5) &&
1599                          distsqflat(&Person::players[i]->coords, &Person::players[k]->coords) < 2 * sq((Person::players[i]->scale + Person::players[k]->scale) * 2.5)) {
1600                     //TODO: refactor two huge similar ifs
1601                     if (Person::players[i]->animTarget == jumpupanim &&
1602                             Person::players[k]->animTarget != getupfrombackanim &&
1603                             Person::players[k]->animTarget != getupfromfrontanim &&
1604                             Animation::animations[Person::players[k]->animTarget].height == middleheight &&
1605                             normaldotproduct(Person::players[i]->velocity, Person::players[k]->coords - Person::players[i]->coords) < 0 &&
1606                             ((Person::players[k]->aitype == playercontrolled && Person::players[k]->attackkeydown) ||
1607                              Person::players[k]->aitype != playercontrolled)) {
1608                         Person::players[i]->victim = Person::players[k];
1609                         Person::players[i]->velocity = 0;
1610                         Person::players[i]->animCurrent = jumpreversedanim;
1611                         Person::players[i]->animTarget = jumpreversedanim;
1612                         Person::players[i]->frameCurrent = 0;
1613                         Person::players[i]->frameTarget = 1;
1614                         Person::players[i]->targettilt2 = 0;
1615                         Person::players[k]->victim = Person::players[i];
1616                         Person::players[k]->velocity = 0;
1617                         Person::players[k]->animCurrent = jumpreversalanim;
1618                         Person::players[k]->animTarget = jumpreversalanim;
1619                         Person::players[k]->frameCurrent = 0;
1620                         Person::players[k]->frameTarget = 1;
1621                         Person::players[k]->targettilt2 = 0;
1622                         if (Person::players[i]->coords.y < Person::players[k]->coords.y + 1) {
1623                             Person::players[i]->animCurrent = rabbitkickreversedanim;
1624                             Person::players[i]->animTarget = rabbitkickreversedanim;
1625                             Person::players[i]->frameCurrent = 1;
1626                             Person::players[i]->frameTarget = 2;
1627                             Person::players[k]->animCurrent = rabbitkickreversalanim;
1628                             Person::players[k]->animTarget = rabbitkickreversalanim;
1629                             Person::players[k]->frameCurrent = 1;
1630                             Person::players[k]->frameTarget = 2;
1631                         }
1632                         Person::players[i]->target = 0;
1633                         Person::players[k]->oldcoords = Person::players[k]->coords;
1634                         Person::players[i]->coords = Person::players[k]->coords;
1635                         Person::players[k]->targetyaw = Person::players[i]->targetyaw;
1636                         Person::players[k]->yaw = Person::players[i]->targetyaw;
1637                         if (Person::players[k]->aitype == attacktypecutoff)
1638                             Person::players[k]->stunned = .5;
1639                     }
1640                     if (Person::players[k]->animTarget == jumpupanim &&
1641                             Person::players[i]->animTarget != getupfrombackanim &&
1642                             Person::players[i]->animTarget != getupfromfrontanim &&
1643                             Animation::animations[Person::players[i]->animTarget].height == middleheight &&
1644                             normaldotproduct(Person::players[k]->velocity, Person::players[i]->coords - Person::players[k]->coords) < 0 &&
1645                             ((Person::players[i]->aitype == playercontrolled && Person::players[i]->attackkeydown) ||
1646                              Person::players[i]->aitype != playercontrolled)) {
1647                         Person::players[k]->victim = Person::players[i];
1648                         Person::players[k]->velocity = 0;
1649                         Person::players[k]->animCurrent = jumpreversedanim;
1650                         Person::players[k]->animTarget = jumpreversedanim;
1651                         Person::players[k]->frameCurrent = 0;
1652                         Person::players[k]->frameTarget = 1;
1653                         Person::players[k]->targettilt2 = 0;
1654                         Person::players[i]->victim = Person::players[k];
1655                         Person::players[i]->velocity = 0;
1656                         Person::players[i]->animCurrent = jumpreversalanim;
1657                         Person::players[i]->animTarget = jumpreversalanim;
1658                         Person::players[i]->frameCurrent = 0;
1659                         Person::players[i]->frameTarget = 1;
1660                         Person::players[i]->targettilt2 = 0;
1661                         if (Person::players[k]->coords.y < Person::players[i]->coords.y + 1) {
1662                             Person::players[k]->animTarget = rabbitkickreversedanim;
1663                             Person::players[k]->animCurrent = rabbitkickreversedanim;
1664                             Person::players[i]->animCurrent = rabbitkickreversalanim;
1665                             Person::players[i]->animTarget = rabbitkickreversalanim;
1666                             Person::players[k]->frameCurrent = 1;
1667                             Person::players[k]->frameTarget = 2;
1668                             Person::players[i]->frameCurrent = 1;
1669                             Person::players[i]->frameTarget = 2;
1670                         }
1671                         Person::players[k]->target = 0;
1672                         Person::players[i]->oldcoords = Person::players[i]->coords;
1673                         Person::players[k]->coords = Person::players[i]->coords;
1674                         Person::players[i]->targetyaw = Person::players[k]->targetyaw;
1675                         Person::players[i]->yaw = Person::players[k]->targetyaw;
1676                         if (Person::players[i]->aitype == attacktypecutoff)
1677                             Person::players[i]->stunned = .5;
1678                     }
1679                 }
1680             }
1681         }
1682     }
1683 }
1684
1685 void doAerialAcrobatics()
1686 {
1687     static XYZ facing, flatfacing;
1688     for (unsigned k = 0; k < Person::players.size(); k++) {
1689         Person::players[k]->turnspeed = 500;
1690
1691         if ((Person::players[k]->isRun() &&
1692                 ((Person::players[k]->targetyaw != rabbitrunninganim &&
1693                   Person::players[k]->targetyaw != wolfrunninganim) ||
1694                  Person::players[k]->frameTarget == 4)) ||
1695                 Person::players[k]->animTarget == removeknifeanim ||
1696                 Person::players[k]->animTarget == crouchremoveknifeanim ||
1697                 Person::players[k]->animTarget == flipanim ||
1698                 Person::players[k]->animTarget == fightsidestep ||
1699                 Person::players[k]->animTarget == walkanim) {
1700             Person::players[k]->yaw = stepTowardf(Person::players[k]->yaw, Person::players[k]->targetyaw, multiplier * Person::players[k]->turnspeed);
1701         }
1702
1703
1704         if (Person::players[k]->isStop() ||
1705                 Person::players[k]->isLanding() ||
1706                 Person::players[k]->animTarget == staggerbackhighanim ||
1707                 (Person::players[k]->animTarget == sneakanim && Person::players[k]->animCurrent == sneakanim) ||
1708                 Person::players[k]->animTarget == staggerbackhardanim ||
1709                 Person::players[k]->animTarget == backhandspringanim ||
1710                 Person::players[k]->animTarget == dodgebackanim ||
1711                 Person::players[k]->animTarget == rollanim ||
1712                 (Animation::animations[Person::players[k]->animTarget].attack &&
1713                  Person::players[k]->animTarget != rabbitkickanim &&
1714                  (Person::players[k]->animTarget != crouchstabanim || Person::players[k]->hasvictim) &&
1715                  (Person::players[k]->animTarget != swordgroundstabanim || Person::players[k]->hasvictim))) {
1716             Person::players[k]->yaw = stepTowardf(Person::players[k]->yaw, Person::players[k]->targetyaw, multiplier * Person::players[k]->turnspeed * 2);
1717         }
1718
1719         if (Person::players[k]->animTarget == sneakanim && Person::players[k]->animCurrent != sneakanim) {
1720             Person::players[k]->yaw = stepTowardf(Person::players[k]->yaw, Person::players[k]->targetyaw, multiplier * Person::players[k]->turnspeed * 4);
1721         }
1722
1723         Person::players[k]->DoStuff();
1724         if (Person::players[k]->immobile && k != 0)
1725             Person::players[k]->coords = Person::players[k]->realoldcoords;
1726
1727         //if player's position has changed (?)
1728         if (distsq(&Person::players[k]->coords, &Person::players[k]->realoldcoords) > 0 &&
1729                 !Person::players[k]->skeleton.free &&
1730                 Person::players[k]->animTarget != climbanim &&
1731                 Person::players[k]->animTarget != hanganim) {
1732             XYZ lowpoint, lowpointtarget, lowpoint2, lowpointtarget2, lowpoint3, lowpointtarget3, lowpoint4, lowpointtarget4, lowpoint5, lowpointtarget5, lowpoint6, lowpointtarget6, lowpoint7, lowpointtarget7, colpoint, colpoint2;
1733             int whichhit;
1734             bool tempcollide = 0;
1735
1736             if (Person::players[k]->collide < -.3)
1737                 Person::players[k]->collide = -.3;
1738             if (Person::players[k]->collide > 1)
1739                 Person::players[k]->collide = 1;
1740             Person::players[k]->collide -= multiplier * 30;
1741
1742             //clip to terrain
1743             Person::players[k]->coords.y = max(Person::players[k]->coords.y, terrain.getHeight(Person::players[k]->coords.x, Person::players[k]->coords.z));
1744
1745             for (int l = 0; l < terrain.patchobjectnum[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz]; l++) {
1746                 int i = terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz][l];
1747                 if (Object::objects[i]->type != rocktype ||
1748                         Object::objects[i]->scale > .5 && Person::players[k]->aitype == playercontrolled ||
1749                         Object::objects[i]->position.y > Person::players[k]->coords.y) {
1750                     lowpoint = Person::players[k]->coords;
1751                     if (Person::players[k]->animTarget != jumpupanim &&
1752                             Person::players[k]->animTarget != jumpdownanim &&
1753                             !Person::players[k]->isFlip())
1754                         lowpoint.y += 1.25;
1755                     else
1756                         lowpoint.y += 1.3;
1757                     if (     Person::players[k]->coords.y < terrain.getHeight(Person::players[k]->coords.x, Person::players[k]->coords.z) &&
1758                              Person::players[k]->coords.y > terrain.getHeight(Person::players[k]->coords.x, Person::players[k]->coords.z) - .1)
1759                         Person::players[k]->coords.y = terrain.getHeight(Person::players[k]->coords.x, Person::players[k]->coords.z);
1760                     if (Person::players[k]->SphereCheck(&lowpoint, 1.3, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw, &Object::objects[i]->model) != -1) {
1761                         flatfacing = lowpoint - Person::players[k]->coords;
1762                         Person::players[k]->coords = lowpoint;
1763                         Person::players[k]->coords.y -= 1.3;
1764                         Person::players[k]->collide = 1;
1765                         tempcollide = 1;
1766                         //wall jumps
1767                         //TODO: refactor four similar blocks
1768                         if (Person::players[k]->aitype == playercontrolled &&
1769                                 (Person::players[k]->animTarget == jumpupanim ||
1770                                  Person::players[k]->animTarget == jumpdownanim ||
1771                                  Person::players[k]->isFlip()) &&
1772                                 !Person::players[k]->jumptogglekeydown &&
1773                                 Person::players[k]->jumpkeydown) {
1774                             lowpointtarget = lowpoint + DoRotation(Person::players[k]->facing, 0, -90, 0) * 1.5;
1775                             XYZ tempcoords1 = lowpoint;
1776                             whichhit = Object::objects[i]->model.LineCheck(&lowpoint, &lowpointtarget, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw);
1777                             if (whichhit != -1 && fabs(Object::objects[i]->model.Triangles[whichhit].facenormal.y) < .3) {
1778                                 Person::players[k]->setTargetAnimation(walljumpleftanim);
1779                                 emit_sound_at(movewhooshsound, Person::players[k]->coords);
1780                                 if (k == 0)
1781                                     pause_sound(whooshsound);
1782
1783                                 lowpointtarget = DoRotation(Object::objects[i]->model.Triangles[whichhit].facenormal, 0, Object::objects[i]->yaw, 0);
1784                                 Person::players[k]->yaw = -asin(0 - lowpointtarget.x) * 180 / M_PI;
1785                                 if (lowpointtarget.z < 0)
1786                                     Person::players[k]->yaw = 180 - Person::players[k]->yaw;
1787                                 Person::players[k]->targetyaw = Person::players[k]->yaw;
1788                                 Person::players[k]->lowyaw = Person::players[k]->yaw;
1789                                 if (k == 0)
1790                                     numwallflipped++;
1791                             } else {
1792                                 lowpoint = tempcoords1;
1793                                 lowpointtarget = lowpoint + DoRotation(Person::players[k]->facing, 0, 90, 0) * 1.5;
1794                                 whichhit = Object::objects[i]->model.LineCheck(&lowpoint, &lowpointtarget, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw);
1795                                 if (whichhit != -1 && fabs(Object::objects[i]->model.Triangles[whichhit].facenormal.y) < .3) {
1796                                     Person::players[k]->setTargetAnimation(walljumprightanim);
1797                                     emit_sound_at(movewhooshsound, Person::players[k]->coords);
1798                                     if (k == 0)
1799                                         pause_sound(whooshsound);
1800
1801                                     lowpointtarget = DoRotation(Object::objects[i]->model.Triangles[whichhit].facenormal, 0, Object::objects[i]->yaw, 0);
1802                                     Person::players[k]->yaw = -asin(0 - lowpointtarget.x) * 180 / M_PI;
1803                                     if (lowpointtarget.z < 0)
1804                                         Person::players[k]->yaw = 180 - Person::players[k]->yaw;
1805                                     Person::players[k]->targetyaw = Person::players[k]->yaw;
1806                                     Person::players[k]->lowyaw = Person::players[k]->yaw;
1807                                     if (k == 0)
1808                                         numwallflipped++;
1809                                 } else {
1810                                     lowpoint = tempcoords1;
1811                                     lowpointtarget = lowpoint + Person::players[k]->facing * 2;
1812                                     whichhit = Object::objects[i]->model.LineCheck(&lowpoint, &lowpointtarget, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw);
1813                                     if (whichhit != -1 && fabs(Object::objects[i]->model.Triangles[whichhit].facenormal.y) < .3) {
1814                                         Person::players[k]->setTargetAnimation(walljumpbackanim);
1815                                         emit_sound_at(movewhooshsound, Person::players[k]->coords);
1816                                         if (k == 0)
1817                                             pause_sound(whooshsound);
1818
1819                                         lowpointtarget = DoRotation(Object::objects[i]->model.Triangles[whichhit].facenormal, 0, Object::objects[i]->yaw, 0);
1820                                         Person::players[k]->yaw = -asin(0 - lowpointtarget.x) * 180 / M_PI;
1821                                         if (lowpointtarget.z < 0)
1822                                             Person::players[k]->yaw = 180 - Person::players[k]->yaw;
1823                                         Person::players[k]->targetyaw = Person::players[k]->yaw;
1824                                         Person::players[k]->lowyaw = Person::players[k]->yaw;
1825                                         if (k == 0)
1826                                             numwallflipped++;
1827                                     } else {
1828                                         lowpoint = tempcoords1;
1829                                         lowpointtarget = lowpoint - Person::players[k]->facing * 2;
1830                                         whichhit = Object::objects[i]->model.LineCheck(&lowpoint, &lowpointtarget, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw);
1831                                         if (whichhit != -1 && fabs(Object::objects[i]->model.Triangles[whichhit].facenormal.y) < .3) {
1832                                             Person::players[k]->setTargetAnimation(walljumpfrontanim);
1833                                             emit_sound_at(movewhooshsound, Person::players[k]->coords);
1834                                             if (k == 0)
1835                                                 pause_sound(whooshsound);
1836
1837                                             lowpointtarget = DoRotation(Object::objects[i]->model.Triangles[whichhit].facenormal, 0, Object::objects[i]->yaw, 0);
1838                                             Person::players[k]->yaw = -asin(0 - lowpointtarget.x) * 180 / M_PI;
1839                                             if (lowpointtarget.z < 0)
1840                                                 Person::players[k]->yaw = 180 - Person::players[k]->yaw;
1841                                             Person::players[k]->yaw += 180;
1842                                             Person::players[k]->targetyaw = Person::players[k]->yaw;
1843                                             Person::players[k]->lowyaw = Person::players[k]->yaw;
1844                                             if (k == 0)
1845                                                 numwallflipped++;
1846                                         }
1847                                     }
1848                                 }
1849                             }
1850                         }
1851                     }
1852                 } else if (Object::objects[i]->type == rocktype) {
1853                     lowpoint2 = Person::players[k]->coords;
1854                     lowpoint = Person::players[k]->coords;
1855                     lowpoint.y += 2;
1856                     if (Object::objects[i]->model.LineCheck(&lowpoint, &lowpoint2, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw) != -1) {
1857                         Person::players[k]->coords = colpoint;
1858                         Person::players[k]->collide = 1;
1859                         tempcollide = 1;
1860
1861                         if (Person::players[k]->animTarget == jumpdownanim || Person::players[k]->isFlip()) {
1862                             //flipped into a rock
1863                             if (Person::players[k]->isFlip() && Person::players[k]->targetFrame().label == 7)
1864                                 Person::players[k]->RagDoll(0);
1865
1866                             if (Person::players[k]->animTarget == jumpupanim) {
1867                                 Person::players[k]->jumppower = -4;
1868                                 Person::players[k]->animTarget = Person::players[k]->getIdle();
1869                             }
1870                             Person::players[k]->target = 0;
1871                             Person::players[k]->frameTarget = 0;
1872                             Person::players[k]->onterrain = 1;
1873
1874                             if (Person::players[k]->id == 0) {
1875                                 pause_sound(whooshsound);
1876                                 OPENAL_SetVolume(channels[whooshsound], 0);
1877                             }
1878
1879                             //landing
1880                             if ((Person::players[k]->animTarget == jumpdownanim || Person::players[k]->isFlip()) && !Person::players[k]->wasLanding()) {
1881                                 if (Person::players[k]->isFlip())
1882                                     Person::players[k]->jumppower = -4;
1883                                 Person::players[k]->animTarget = Person::players[k]->getLanding();
1884                                 emit_sound_at(landsound, Person::players[k]->coords, 128.);
1885                                 if (k == 0) {
1886                                     addEnvSound(Person::players[k]->coords);
1887                                 }
1888                             }
1889                         }
1890                     }
1891                 }
1892             }
1893
1894             if (tempcollide)
1895                 for (int l = 0; l < terrain.patchobjectnum[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz]; l++) {
1896                     int i = terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz][l];
1897                     lowpoint = Person::players[k]->coords;
1898                     lowpoint.y += 1.35;
1899                     if (Object::objects[i]->type != rocktype)
1900                         if (Person::players[k]->SphereCheck(&lowpoint, 1.33, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw, &Object::objects[i]->model) != -1) {
1901                             if (Person::players[k]->animTarget != jumpupanim &&
1902                                     Person::players[k]->animTarget != jumpdownanim &&
1903                                     Person::players[k]->onterrain)
1904                                 Person::players[k]->avoidcollided = 1;
1905                             Person::players[k]->coords = lowpoint;
1906                             Person::players[k]->coords.y -= 1.35;
1907                             Person::players[k]->collide = 1;
1908
1909                             if ((Person::players[k]->grabdelay <= 0 || Person::players[k]->aitype != playercontrolled) &&
1910                                     (Person::players[k]->animCurrent != climbanim &&
1911                                      Person::players[k]->animCurrent != hanganim &&
1912                                      !Person::players[k]->isWallJump() ||
1913                                      Person::players[k]->animTarget == jumpupanim ||
1914                                      Person::players[k]->animTarget == jumpdownanim)) {
1915                                 lowpoint = Person::players[k]->coords;
1916                                 Object::objects[i]->model.SphereCheckPossible(&lowpoint, 1.5, &Object::objects[i]->position, &Object::objects[i]->yaw);
1917                                 lowpoint = Person::players[k]->coords;
1918                                 lowpoint.y += .05;
1919                                 facing = 0;
1920                                 facing.z = -1;
1921                                 facing = DoRotation(facing, 0, Person::players[k]->targetyaw + 180, 0);
1922                                 lowpointtarget = lowpoint + facing * 1.4;
1923                                 whichhit = Object::objects[i]->model.LineCheckPossible(&lowpoint, &lowpointtarget, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw);
1924                                 if (whichhit != -1) {
1925                                     lowpoint = Person::players[k]->coords;
1926                                     lowpoint.y += .1;
1927                                     lowpointtarget = lowpoint + facing * 1.4;
1928                                     lowpoint2 = lowpoint;
1929                                     lowpointtarget2 = lowpointtarget;
1930                                     lowpoint3 = lowpoint;
1931                                     lowpointtarget3 = lowpointtarget;
1932                                     lowpoint4 = lowpoint;
1933                                     lowpointtarget4 = lowpointtarget;
1934                                     lowpoint5 = lowpoint;
1935                                     lowpointtarget5 = lowpointtarget;
1936                                     lowpoint6 = lowpoint;
1937                                     lowpointtarget6 = lowpointtarget;
1938                                     lowpoint7 = lowpoint;
1939                                     lowpointtarget7 = lowpoint;
1940                                     lowpoint2.x += .1;
1941                                     lowpointtarget2.x += .1;
1942                                     lowpoint3.z += .1;
1943                                     lowpointtarget3.z += .1;
1944                                     lowpoint4.x -= .1;
1945                                     lowpointtarget4.x -= .1;
1946                                     lowpoint5.z -= .1;
1947                                     lowpointtarget5.z -= .1;
1948                                     lowpoint6.y += 45 / 13;
1949                                     lowpointtarget6.y += 45 / 13;
1950                                     lowpointtarget6 += facing * .6;
1951                                     lowpointtarget7.y += 90 / 13;
1952                                     whichhit = Object::objects[i]->model.LineCheckPossible(&lowpoint, &lowpointtarget, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw);
1953                                     if (Object::objects[i]->friction > .5)
1954                                         if (whichhit != -1) {
1955                                             if (Person::players[k]->animTarget != jumpupanim && Person::players[k]->animTarget != jumpdownanim)
1956                                                 Person::players[k]->collided = 1;
1957                                             if (Object::checkcollide(lowpoint7, lowpointtarget7) == -1)
1958                                                 if (Object::checkcollide(lowpoint6, lowpointtarget6) == -1)
1959                                                     if (     Object::objects[i]->model.LineCheckPossible(&lowpoint2, &lowpointtarget2,
1960                                                              &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw) != -1 &&
1961                                                              Object::objects[i]->model.LineCheckPossible(&lowpoint3, &lowpointtarget3,
1962                                                                      &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw) != -1 &&
1963                                                              Object::objects[i]->model.LineCheckPossible(&lowpoint4, &lowpointtarget4,
1964                                                                      &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw) != -1 &&
1965                                                              Object::objects[i]->model.LineCheckPossible(&lowpoint5, &lowpointtarget5,
1966                                                                      &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw) != -1)
1967                                                         for (int j = 0; j < 45; j++) {
1968                                                             lowpoint = Person::players[k]->coords;
1969                                                             lowpoint.y += (float)j / 13;
1970                                                             lowpointtarget = lowpoint + facing * 1.4;
1971                                                             if (Object::objects[i]->model.LineCheckPossible(&lowpoint, &lowpointtarget,
1972                                                                                                    &colpoint2, &Object::objects[i]->position, &Object::objects[i]->yaw) == -1) {
1973                                                                 if (j <= 6 || j <= 25 && Person::players[k]->animTarget == jumpdownanim)
1974                                                                     break;
1975                                                                 if (Person::players[k]->animTarget == jumpupanim || Person::players[k]->animTarget == jumpdownanim) {
1976                                                                     lowpoint = Person::players[k]->coords;
1977                                                                     lowpoint.y += (float)j / 13;
1978                                                                     lowpointtarget = lowpoint + facing * 1.3;
1979                                                                     flatfacing = Person::players[k]->coords;
1980                                                                     Person::players[k]->coords = colpoint - DoRotation(Object::objects[i]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * .01;
1981                                                                     Person::players[k]->coords.y = lowpointtarget.y - .07;
1982                                                                     Person::players[k]->currentoffset = (flatfacing - Person::players[k]->coords) / Person::players[k]->scale;
1983
1984                                                                     if (j > 10 || !Person::players[k]->isRun()) {
1985                                                                         if (Person::players[k]->animTarget == jumpdownanim || Person::players[k]->animTarget == jumpupanim) {
1986                                                                             if (k == 0)
1987                                                                                 pause_sound(whooshsound);
1988                                                                         }
1989                                                                         emit_sound_at(jumpsound, Person::players[k]->coords, 128.);
1990
1991                                                                         lowpointtarget = DoRotation(Object::objects[i]->model.Triangles[whichhit].facenormal, 0, Object::objects[i]->yaw, 0);
1992                                                                         Person::players[k]->yaw = -asin(0 - lowpointtarget.x) * 180 / M_PI;
1993                                                                         if (lowpointtarget.z < 0)
1994                                                                             Person::players[k]->yaw = 180 - Person::players[k]->yaw;
1995                                                                         Person::players[k]->targetyaw = Person::players[k]->yaw;
1996                                                                         Person::players[k]->lowyaw = Person::players[k]->yaw;
1997
1998                                                                         //Person::players[k]->velocity=lowpointtarget*.03;
1999                                                                         Person::players[k]->velocity = 0;
2000
2001                                                                         //climb ledge (?)
2002                                                                         if (Person::players[k]->animTarget == jumpupanim) {
2003                                                                             Person::players[k]->animTarget = climbanim;
2004                                                                             Person::players[k]->jumppower = 0;
2005                                                                             Person::players[k]->jumpclimb = 1;
2006                                                                         }
2007                                                                         Person::players[k]->transspeed = 6;
2008                                                                         Person::players[k]->target = 0;
2009                                                                         Person::players[k]->frameTarget = 1;
2010                                                                         //hang ledge (?)
2011                                                                         if (j > 25) {
2012                                                                             Person::players[k]->setTargetAnimation(hanganim);
2013                                                                             Person::players[k]->jumppower = 0;
2014                                                                         }
2015                                                                     }
2016                                                                     break;
2017                                                                 }
2018                                                             }
2019                                                         }
2020                                         }
2021                                 }
2022                             }
2023                         }
2024                 }
2025             if (Person::players[k]->collide <= 0) {
2026                 //in the air
2027                 if (!Person::players[k]->onterrain &&
2028                         Person::players[k]->animTarget != jumpupanim &&
2029                         Person::players[k]->animTarget != jumpdownanim &&
2030                         Person::players[k]->animTarget != climbanim &&
2031                         Person::players[k]->animTarget != hanganim &&
2032                         !Person::players[k]->isWallJump() &&
2033                         !Person::players[k]->isFlip()) {
2034                     if (Person::players[k]->animCurrent != climbanim &&
2035                             Person::players[k]->animCurrent != tempanim &&
2036                             Person::players[k]->animTarget != backhandspringanim &&
2037                             (Person::players[k]->animTarget != rollanim ||
2038                              Person::players[k]->frameTarget < 2 ||
2039                              Person::players[k]->frameTarget > 6)) {
2040                         //stagger off ledge (?)
2041                         if (Person::players[k]->animTarget == staggerbackhighanim || Person::players[k]->animTarget == staggerbackhardanim)
2042                             Person::players[k]->RagDoll(0);
2043                         Person::players[k]->setTargetAnimation(jumpdownanim);
2044
2045                         if (!k)
2046                             emit_sound_at(whooshsound, Person::players[k]->coords, 128.);
2047                     }
2048                     //gravity
2049                     Person::players[k]->velocity.y += gravity;
2050                 }
2051             }
2052         }
2053         Person::players[k]->realoldcoords = Person::players[k]->coords;
2054     }
2055 }
2056
2057 void doAttacks()
2058 {
2059     static int randattack;
2060     static bool playerrealattackkeydown = 0;
2061
2062     if (!Input::isKeyDown(attackkey))
2063         oldattackkey = 0;
2064     if (oldattackkey)
2065         Person::players[0]->attackkeydown = 0;
2066     if (oldattackkey)
2067         playerrealattackkeydown = 0;
2068     if (!oldattackkey)
2069         playerrealattackkeydown = Input::isKeyDown(attackkey);
2070     if ((Person::players[0]->parriedrecently <= 0 ||
2071             Person::players[0]->weaponactive == -1) &&
2072             (!oldattackkey ||
2073              (realthreat &&
2074               Person::players[0]->lastattack != swordslashanim &&
2075               Person::players[0]->lastattack != knifeslashstartanim &&
2076               Person::players[0]->lastattack != staffhitanim &&
2077               Person::players[0]->lastattack != staffspinhitanim)))
2078         Person::players[0]->attackkeydown = Input::isKeyDown(attackkey);
2079     if (Input::isKeyDown(attackkey) &&
2080             !oldattackkey &&
2081             !Person::players[0]->backkeydown) {
2082         for (unsigned k = 0; k < Person::players.size(); k++) {
2083             if ((Person::players[k]->animTarget == swordslashanim ||
2084                     Person::players[k]->animTarget == staffhitanim ||
2085                     Person::players[k]->animTarget == staffspinhitanim) &&
2086                     Person::players[0]->animCurrent != dodgebackanim &&
2087                     !Person::players[k]->skeleton.free)
2088                 Person::players[k]->Reverse();
2089         }
2090     }
2091
2092     if (!hostile || Dialog::inDialog())
2093         Person::players[0]->attackkeydown = 0;
2094
2095     for (unsigned k = 0; k < Person::players.size(); k++) {
2096         if (Dialog::inDialog())
2097             Person::players[k]->attackkeydown = 0;
2098         if (Person::players[k]->animTarget != rabbitrunninganim && Person::players[k]->animTarget != wolfrunninganim) {
2099             if (Person::players[k]->aitype != playercontrolled)
2100                 Person::players[k]->victim = Person::players[0];
2101             //attack key pressed
2102             if (Person::players[k]->attackkeydown) {
2103                 //dodge backward
2104                 if (Person::players[k]->backkeydown &&
2105                         Person::players[k]->animTarget != backhandspringanim &&
2106                         (Person::players[k]->isIdle() ||
2107                          Person::players[k]->isStop() ||
2108                          Person::players[k]->isRun() ||
2109                          Person::players[k]->animTarget == walkanim)) {
2110                     if (Person::players[k]->jumppower <= 1) {
2111                         Person::players[k]->jumppower -= 2;
2112                     } else {
2113                         for (unsigned i = 0; i < Person::players.size(); i++) {
2114                             if (i == k)
2115                                 continue;
2116                             if (Person::players[i]->animTarget == swordslashanim ||
2117                                     Person::players[i]->animTarget == knifeslashstartanim ||
2118                                     Person::players[i]->animTarget == staffhitanim ||
2119                                     Person::players[i]->animTarget == staffspinhitanim)
2120                                 if (distsq(&Person::players[k]->coords, &Person::players[i]->coords) < 6.5 && !Person::players[i]->skeleton.free) {
2121                                     Person::players[k]->setTargetAnimation(dodgebackanim);
2122                                     Person::players[k]->targetyaw = roughDirectionTo(Person::players[k]->coords, Person::players[i]->coords);
2123                                     Person::players[k]->targettilt2 = pitchTo(Person::players[k]->coords, Person::players[i]->coords);
2124                                 }
2125                         }
2126                         if (Person::players[k]->animTarget != dodgebackanim) {
2127                             if (k == 0)
2128                                 numflipped++;
2129                             Person::players[k]->setTargetAnimation(backhandspringanim);
2130                             Person::players[k]->targetyaw = -yaw + 180;
2131                             if (Person::players[k]->leftkeydown)
2132                                 Person::players[k]->targetyaw -= 45;
2133                             if (Person::players[k]->rightkeydown)
2134                                 Person::players[k]->targetyaw += 45;
2135                             Person::players[k]->yaw = Person::players[k]->targetyaw;
2136                             Person::players[k]->jumppower -= 2;
2137                         }
2138                     }
2139                 }
2140                 //attack
2141                 if (!Animation::animations[Person::players[k]->animTarget].attack &&
2142                         !Person::players[k]->backkeydown &&
2143                         (Person::players[k]->isIdle() ||
2144                          Person::players[k]->isRun() ||
2145                          Person::players[k]->animTarget == walkanim ||
2146                          Person::players[k]->animTarget == sneakanim ||
2147                          Person::players[k]->isCrouch())) {
2148                     const int attackweapon = Person::players[k]->weaponactive == -1 ? 0 : weapons[Person::players[k]->weaponids[Person::players[k]->weaponactive]].getType();
2149                     //normal attacks (?)
2150                     Person::players[k]->hasvictim = 0;
2151                     if (Person::players.size() > 1)
2152                         for (unsigned i = 0; i < Person::players.size(); i++) {
2153                             if (i == k || !(k == 0 || i == 0))
2154                                 continue;
2155                             if (!Person::players[k]->hasvictim)
2156                                 if (Animation::animations[Person::players[k]->animTarget].attack != reversal) {
2157                                     //choose an attack
2158                                     const float distance = distsq(&Person::players[k]->coords, &Person::players[i]->coords);
2159                                     if (distance < 4.5 &&
2160                                             !Person::players[i]->skeleton.free &&
2161                                             Person::players[i]->howactive < typedead1 &&
2162                                             Person::players[i]->animTarget != jumpreversedanim &&
2163                                             Person::players[i]->animTarget != rabbitkickreversedanim &&
2164                                             Person::players[i]->animTarget != rabbitkickanim &&
2165                                             Person::players[k]->animTarget != rabbitkickanim &&
2166                                             Person::players[i]->animTarget != getupfrombackanim &&
2167                                             (Person::players[i]->animTarget != staggerbackhighanim &&
2168                                              (Person::players[i]->animTarget != staggerbackhardanim || Person::players[i]->targetFrame().label == 6)) &&
2169                                             Person::players[i]->animTarget != jumpdownanim &&
2170                                             Person::players[i]->animTarget != jumpupanim &&
2171                                             Person::players[i]->animTarget != getupfromfrontanim) {
2172                                         Person::players[k]->victim = Person::players[i];
2173                                         Person::players[k]->hasvictim = 1;
2174                                         if (Person::players[k]->aitype == playercontrolled) { //human player
2175                                             //sweep
2176                                             if (distance < 2.5 * sq(Person::players[k]->scale * 5) &&
2177                                                     Person::players[k]->crouchkeydown &&
2178                                                     Animation::animations[Person::players[i]->animTarget].height != lowheight)
2179                                                 Person::players[k]->animTarget = sweepanim;
2180                                             //winduppunch
2181                                             else if (distance < 1.5 * sq(Person::players[k]->scale * 5) &&
2182                                                      Animation::animations[Person::players[i]->animTarget].height != lowheight &&
2183                                                      !Person::players[k]->forwardkeydown &&
2184                                                      !Person::players[k]->leftkeydown &&
2185                                                      !Person::players[k]->rightkeydown &&
2186                                                      !Person::players[k]->crouchkeydown &&
2187                                                      !attackweapon &&
2188                                                      !reversaltrain)
2189                                                 Person::players[k]->animTarget = winduppunchanim;
2190                                             //upunch
2191                                             else if (distance < 2.5 * sq(Person::players[k]->scale * 5) &&
2192                                                      Animation::animations[Person::players[i]->animTarget].height != lowheight &&
2193                                                      !Person::players[k]->forwardkeydown &&
2194                                                      !Person::players[k]->leftkeydown &&
2195                                                      !Person::players[k]->rightkeydown &&
2196                                                      !Person::players[k]->crouchkeydown &&
2197                                                      !attackweapon)
2198                                                 Person::players[k]->animTarget = upunchanim;
2199                                             //knifefollow
2200                                             else if (distance < 2.5 * sq(Person::players[k]->scale * 5) &&
2201                                                      Person::players[i]->staggerdelay > 0 &&
2202                                                      attackweapon == knife &&
2203                                                      Person::players[i]->bloodloss > Person::players[i]->damagetolerance / 2)
2204                                                 Person::players[k]->animTarget = knifefollowanim;
2205                                             //knifeslashstart
2206                                             else if (distance < 2.5 * sq(Person::players[k]->scale * 5) &&
2207                                                      Animation::animations[Person::players[i]->animTarget].height != lowheight &&
2208                                                      !Person::players[k]->forwardkeydown &&
2209                                                      !Person::players[k]->leftkeydown &&
2210                                                      !Person::players[k]->rightkeydown &&
2211                                                      !Person::players[k]->crouchkeydown &&
2212                                                      attackweapon == knife &&
2213                                                      Person::players[k]->weaponmissdelay <= 0)
2214                                                 Person::players[k]->animTarget = knifeslashstartanim;
2215                                             //swordslash
2216                                             else if (distance < 4.5 * sq(Person::players[k]->scale * 5) &&
2217                                                      Animation::animations[Person::players[i]->animTarget].height != lowheight &&
2218                                                      !Person::players[k]->crouchkeydown &&
2219                                                      attackweapon == sword &&
2220                                                      Person::players[k]->weaponmissdelay <= 0)
2221                                                 Person::players[k]->animTarget = swordslashanim;
2222                                             //staffhit
2223                                             else if (distance < 4.5 * sq(Person::players[k]->scale * 5) &&
2224                                                      Animation::animations[Person::players[i]->animTarget].height != lowheight &&
2225                                                      !Person::players[k]->crouchkeydown &&
2226                                                      attackweapon == staff &&
2227                                                      Person::players[k]->weaponmissdelay <= 0 &&
2228                                                      !Person::players[k]->leftkeydown &&
2229                                                      !Person::players[k]->rightkeydown &&
2230                                                      !Person::players[k]->forwardkeydown)
2231                                                 Person::players[k]->animTarget = staffhitanim;
2232                                             //staffspinhit
2233                                             else if (distance < 4.5 * sq(Person::players[k]->scale * 5) &&
2234                                                      Animation::animations[Person::players[i]->animTarget].height != lowheight &&
2235                                                      !Person::players[k]->crouchkeydown &&
2236                                                      attackweapon == staff &&
2237                                                      Person::players[k]->weaponmissdelay <= 0)
2238                                                 Person::players[k]->animTarget = staffspinhitanim;
2239                                             //spinkick
2240                                             else if (distance < 2.5 * sq(Person::players[k]->scale * 5) &&
2241                                                      Animation::animations[Person::players[i]->animTarget].height != lowheight)
2242                                                 Person::players[k]->animTarget = spinkickanim;
2243                                             //lowkick
2244                                             else if (distance < 2.5 * sq(Person::players[k]->scale * 5) &&
2245                                                      Animation::animations[Person::players[i]->animTarget].height == lowheight &&
2246                                                      Animation::animations[Person::players[k]->animTarget].attack != normalattack)
2247                                                 Person::players[k]->animTarget = lowkickanim;
2248                                         } else { //AI player
2249                                             if (distance < 4.5 * sq(Person::players[k]->scale * 5)) {
2250                                                 randattack = abs(Random() % 5);
2251                                                 if (!attackweapon && distance < 2.5 * sq(Person::players[k]->scale * 5)) {
2252                                                     //sweep
2253                                                     if (randattack == 0 && Animation::animations[Person::players[i]->animTarget].height != lowheight)
2254                                                         Person::players[k]->animTarget = sweepanim;
2255                                                     //upunch
2256                                                     else if (randattack == 1 && Animation::animations[Person::players[i]->animTarget].height != lowheight &&
2257                                                              !attackweapon)
2258                                                         Person::players[k]->animTarget = upunchanim;
2259                                                     //spinkick
2260                                                     else if (randattack == 2 && Animation::animations[Person::players[i]->animTarget].height != lowheight)
2261                                                         Person::players[k]->animTarget = spinkickanim;
2262                                                     //lowkick
2263                                                     else if (Animation::animations[Person::players[i]->animTarget].height == lowheight)
2264                                                         Person::players[k]->animTarget = lowkickanim;
2265                                                 }
2266                                                 if (attackweapon) {
2267                                                     //sweep
2268                                                     if ((!Tutorial::active || !attackweapon) &&
2269                                                             distance < 2.5 * sq(Person::players[k]->scale * 5) &&
2270                                                             randattack == 0 &&
2271                                                             Animation::animations[Person::players[i]->animTarget].height != lowheight)
2272                                                         Person::players[k]->animTarget = sweepanim;
2273                                                     //knifeslashstart
2274                                                     else if (distance < 2.5 * sq(Person::players[k]->scale * 5) &&
2275                                                              attackweapon == knife &&
2276                                                              Person::players[k]->weaponmissdelay <= 0)
2277                                                         Person::players[k]->animTarget = knifeslashstartanim;
2278                                                     //swordslash
2279                                                     else if (!(Person::players[0]->victim == Person::players[i] &&
2280                                                                Person::players[0]->hasvictim &&
2281                                                                Person::players[0]->animTarget == swordslashanim) &&
2282                                                              attackweapon == sword &&
2283                                                              Person::players[k]->weaponmissdelay <= 0)
2284                                                         Person::players[k]->animTarget = swordslashanim;
2285                                                     //staffhit
2286                                                     else if (!(Person::players[0]->victim == Person::players[i] &&
2287                                                                Person::players[0]->hasvictim &&
2288                                                                Person::players[0]->animTarget == swordslashanim) &&
2289                                                              attackweapon == staff &&
2290                                                              Person::players[k]->weaponmissdelay <= 0 &&
2291                                                              randattack < 3)
2292                                                         Person::players[k]->animTarget = staffhitanim;
2293                                                     //staffspinhit
2294                                                     else if (!(Person::players[0]->victim == Person::players[i] &&
2295                                                                Person::players[0]->hasvictim &&
2296                                                                Person::players[0]->animTarget == swordslashanim) &&
2297                                                              attackweapon == staff &&
2298                                                              Person::players[k]->weaponmissdelay <= 0 &&
2299                                                              randattack >= 3)
2300                                                         Person::players[k]->animTarget = staffspinhitanim;
2301                                                     //spinkick
2302                                                     else if ((!Tutorial::active || !attackweapon) &&
2303                                                              distance < 2.5 * sq(Person::players[k]->scale * 5) &&
2304                                                              randattack == 1 &&
2305                                                              Animation::animations[Person::players[i]->animTarget].height != lowheight)
2306                                                         Person::players[k]->animTarget = spinkickanim;
2307                                                     //lowkick
2308                                                     else if (distance < 2.5 * sq(Person::players[k]->scale * 5) &&
2309                                                              Animation::animations[Person::players[i]->animTarget].height == lowheight &&
2310                                                              Animation::animations[Person::players[k]->animTarget].attack != normalattack)
2311                                                         Person::players[k]->animTarget = lowkickanim;
2312                                                 }
2313                                             }
2314                                         }
2315                                         //upunch becomes wolfslap
2316                                         if (Person::players[k]->animTarget == upunchanim && Person::players[k]->creature == wolftype)
2317                                             Person::players[k]->animTarget = wolfslapanim;
2318                                     }
2319                                     //sneak attacks
2320                                     if ((k == 0) && (!Tutorial::active || Tutorial::stage == 22) &&
2321                                             Person::players[i]->howactive < typedead1 &&
2322                                             distance < 1.5 * sq(Person::players[k]->scale * 5) &&
2323                                             !Person::players[i]->skeleton.free &&
2324                                             Person::players[i]->animTarget != getupfrombackanim &&
2325                                             Person::players[i]->animTarget != getupfromfrontanim &&
2326                                             (Person::players[i]->surprised > 0 ||
2327                                              Person::players[i]->aitype == passivetype ||
2328                                              attackweapon && Person::players[i]->stunned > 0) &&
2329                                             normaldotproduct(Person::players[i]->facing, Person::players[i]->coords - Person::players[k]->coords) > 0) {
2330                                         //sneakattack
2331                                         if (!attackweapon) {
2332                                             Person::players[k]->animCurrent = sneakattackanim;
2333                                             Person::players[k]->animTarget = sneakattackanim;
2334                                             Person::players[i]->animCurrent = sneakattackedanim;
2335                                             Person::players[i]->animTarget = sneakattackedanim;
2336                                             Person::players[k]->oldcoords = Person::players[k]->coords;
2337                                             Person::players[k]->coords = Person::players[i]->coords;
2338                                         }
2339                                         //knifesneakattack
2340                                         if (attackweapon == knife) {
2341                                             Person::players[k]->animCurrent = knifesneakattackanim;
2342                                             Person::players[k]->animTarget = knifesneakattackanim;
2343                                             Person::players[i]->animCurrent = knifesneakattackedanim;
2344                                             Person::players[i]->animTarget = knifesneakattackedanim;
2345                                             Person::players[i]->oldcoords = Person::players[i]->coords;
2346                                             Person::players[i]->coords = Person::players[k]->coords;
2347                                         }
2348                                         //swordsneakattack
2349                                         if (attackweapon == sword) {
2350                                             Person::players[k]->animCurrent = swordsneakattackanim;
2351                                             Person::players[k]->animTarget = swordsneakattackanim;
2352                                             Person::players[i]->animCurrent = swordsneakattackedanim;
2353                                             Person::players[i]->animTarget = swordsneakattackedanim;
2354                                             Person::players[i]->oldcoords = Person::players[i]->coords;
2355                                             Person::players[i]->coords = Person::players[k]->coords;
2356                                         }
2357                                         if (attackweapon != staff) {
2358                                             Person::players[k]->victim = Person::players[i];
2359                                             Person::players[k]->hasvictim = 1;
2360                                             Person::players[i]->targettilt2 = 0;
2361                                             Person::players[i]->frameTarget = 1;
2362                                             Person::players[i]->frameCurrent = 0;
2363                                             Person::players[i]->target = 0;
2364                                             Person::players[i]->velocity = 0;
2365                                             Person::players[k]->targettilt2 = Person::players[i]->targettilt2;
2366                                             Person::players[k]->frameCurrent = Person::players[i]->frameCurrent;
2367                                             Person::players[k]->frameTarget = Person::players[i]->frameTarget;
2368                                             Person::players[k]->target = Person::players[i]->target;
2369                                             Person::players[k]->velocity = 0;
2370                                             Person::players[k]->targetyaw = Person::players[i]->yaw;
2371                                             Person::players[k]->yaw = Person::players[i]->yaw;
2372                                             Person::players[i]->targetyaw = Person::players[i]->yaw;
2373                                         }
2374                                     }
2375                                     if (Animation::animations[Person::players[k]->animTarget].attack == normalattack &&
2376                                             Person::players[k]->victim == Person::players[i] &&
2377                                             (!Person::players[i]->skeleton.free)) {
2378                                         oldattackkey = 1;
2379                                         Person::players[k]->frameTarget = 0;
2380                                         Person::players[k]->target = 0;
2381
2382                                         Person::players[k]->targetyaw = roughDirectionTo(Person::players[k]->coords, Person::players[i]->coords);
2383                                         Person::players[k]->targettilt2 = pitchTo(Person::players[k]->coords, Person::players[i]->coords);
2384                                         Person::players[k]->lastattack3 = Person::players[k]->lastattack2;
2385                                         Person::players[k]->lastattack2 = Person::players[k]->lastattack;
2386                                         Person::players[k]->lastattack = Person::players[k]->animTarget;
2387                                     }
2388                                     if (Person::players[k]->animTarget == knifefollowanim &&
2389                                             Person::players[k]->victim == Person::players[i]) {
2390                                         oldattackkey = 1;
2391                                         Person::players[k]->targetyaw = roughDirectionTo(Person::players[k]->coords, Person::players[i]->coords);
2392                                         Person::players[k]->targettilt2 = pitchTo(Person::players[k]->coords, Person::players[i]->coords);
2393                                         Person::players[k]->victim = Person::players[i];
2394                                         Person::players[k]->hasvictim = 1;
2395                                         Person::players[i]->animTarget = knifefollowedanim;
2396                                         Person::players[i]->animCurrent = knifefollowedanim;
2397                                         Person::players[i]->targettilt2 = 0;
2398                                         Person::players[i]->targettilt2 = Person::players[k]->targettilt2;
2399                                         Person::players[i]->frameTarget = 1;
2400                                         Person::players[i]->frameCurrent = 0;
2401                                         Person::players[i]->target = 0;
2402                                         Person::players[i]->velocity = 0;
2403                                         Person::players[k]->animCurrent = knifefollowanim;
2404                                         Person::players[k]->animTarget = knifefollowanim;
2405                                         Person::players[k]->targettilt2 = Person::players[i]->targettilt2;
2406                                         Person::players[k]->frameCurrent = Person::players[i]->frameCurrent;
2407                                         Person::players[k]->frameTarget = Person::players[i]->frameTarget;
2408                                         Person::players[k]->target = Person::players[i]->target;
2409                                         Person::players[k]->velocity = 0;
2410                                         Person::players[k]->oldcoords = Person::players[k]->coords;
2411                                         Person::players[i]->coords = Person::players[k]->coords;
2412                                         Person::players[i]->targetyaw = Person::players[k]->targetyaw;
2413                                         Person::players[i]->yaw = Person::players[k]->targetyaw;
2414                                         Person::players[k]->yaw = Person::players[k]->targetyaw;
2415                                         Person::players[i]->yaw = Person::players[k]->targetyaw;
2416                                     }
2417                                 }
2418                         }
2419                     const bool hasstaff = attackweapon == staff;
2420                     if (k == 0 && Person::players.size() > 1)
2421                         for (unsigned i = 0; i < Person::players.size(); i++) {
2422                             if (i == k)
2423                                 continue;
2424                             if ((playerrealattackkeydown || Person::players[i]->dead || !hasstaff) &&
2425                                     Animation::animations[Person::players[k]->animTarget].attack == neutral) {
2426                                 const float distance = distsq(&Person::players[k]->coords, &Person::players[i]->coords);
2427                                 if (!Person::players[i]->dead || !realthreat || (!attackweapon && Person::players[k]->crouchkeydown))
2428                                     if (Person::players[i]->skeleton.free)
2429                                         if (distance < 3.5 * sq(Person::players[k]->scale * 5) &&
2430                                                 (Person::players[i]->dead ||
2431                                                  Person::players[i]->skeleton.longdead > 1000 ||
2432                                                  Person::players[k]->isRun() ||
2433                                                  hasstaff ||
2434                                                  (attackweapon &&
2435                                                   (Person::players[i]->skeleton.longdead > 2000 ||
2436                                                    Person::players[i]->damage > Person::players[i]->damagetolerance / 8 ||
2437                                                    Person::players[i]->bloodloss > Person::players[i]->damagetolerance / 2) &&
2438                                                   distance < 1.5 * sq(Person::players[k]->scale * 5)))) {
2439                                             Person::players[k]->victim = Person::players[i];
2440                                             Person::players[k]->hasvictim = 1;
2441                                             if (attackweapon && !Tutorial::active) {
2442                                                 //crouchstab
2443                                                 if (Person::players[k]->crouchkeydown && attackweapon == knife && distance < 1.5 * sq(Person::players[k]->scale * 5))
2444                                                     Person::players[k]->animTarget = crouchstabanim;
2445                                                 //swordgroundstab
2446                                                 if (Person::players[k]->crouchkeydown && distance < 1.5 * sq(Person::players[k]->scale * 5) && attackweapon == sword)
2447                                                     Person::players[k]->animTarget = swordgroundstabanim;
2448                                                 //staffgroundsmash
2449                                                 if (distance < 3.5 * sq(Person::players[k]->scale * 5) && attackweapon == staff)
2450                                                     Person::players[k]->animTarget = staffgroundsmashanim;
2451                                             }
2452                                             if (distance < 2.5 &&
2453                                                     Person::players[k]->crouchkeydown &&
2454                                                     Person::players[k]->animTarget != crouchstabanim &&
2455                                                     !attackweapon &&
2456                                                     Person::players[i]->dead &&
2457                                                     Person::players[i]->skeleton.free &&
2458                                                     Person::players[i]->skeleton.longdead > 1000) {
2459                                                 Person::players[k]->animTarget = killanim;
2460                                                 terrain.deleteDeadDecals();
2461                                                 for (unsigned int l = 0; l < Object::objects.size(); l++) {
2462                                                     if (Object::objects[l]->model.type == decalstype) {
2463                                                         Object::objects[l]->model.deleteDeadDecals();
2464                                                     }
2465                                                 }
2466                                             }
2467                                             if (!Person::players[i]->dead || musictype != 2)
2468                                                 if (distance < 3.5 &&
2469                                                         (Person::players[k]->isRun() || Person::players[k]->isIdle() && Person::players[k]->attackkeydown) &&
2470                                                         Person::players[k]->staggerdelay <= 0 &&
2471                                                         (Person::players[i]->dead ||
2472                                                          Person::players[i]->skeleton.longdead < 300 &&
2473                                                          Person::players[k]->lastattack != spinkickanim &&
2474                                                          Person::players[i]->skeleton.free) &&
2475                                                         (!Person::players[i]->dead || musictype != stream_fighttheme)) {
2476                                                     Person::players[k]->animTarget = dropkickanim;
2477                                                     terrain.deleteDeadDecals();
2478                                                     for (unsigned int l = 0; l < Object::objects.size(); l++) {
2479                                                         if (Object::objects[l]->model.type == decalstype) {
2480                                                             Object::objects[l]->model.deleteDeadDecals();
2481                                                         }
2482                                                     }
2483                                                 }
2484                                         }
2485                                 if (Animation::animations[Person::players[k]->animTarget].attack == normalattack &&
2486                                         Person::players[k]->victim == Person::players[i] &&
2487                                         (!Person::players[i]->skeleton.free ||
2488                                          Person::players[k]->animTarget == killanim ||
2489                                          Person::players[k]->animTarget == crouchstabanim ||
2490                                          Person::players[k]->animTarget == swordgroundstabanim ||
2491                                          Person::players[k]->animTarget == staffgroundsmashanim ||
2492                                          Person::players[k]->animTarget == dropkickanim)) {
2493                                     oldattackkey = 1;
2494                                     Person::players[k]->frameTarget = 0;
2495                                     Person::players[k]->target = 0;
2496
2497                                     XYZ targetpoint = Person::players[i]->coords;
2498                                     if (Person::players[k]->animTarget == crouchstabanim ||
2499                                             Person::players[k]->animTarget == swordgroundstabanim ||
2500                                             Person::players[k]->animTarget == staffgroundsmashanim) {
2501                                         targetpoint += (Person::players[i]->jointPos(abdomen) +
2502                                                         Person::players[i]->jointPos(neck)) / 2 *
2503                                                        Person::players[i]->scale;
2504                                     }
2505                                     Person::players[k]->targetyaw = roughDirectionTo(Person::players[k]->coords, targetpoint);
2506                                     Person::players[k]->targettilt2 = pitchTo(Person::players[k]->coords, targetpoint);
2507
2508                                     if (Person::players[k]->animTarget == crouchstabanim || Person::players[k]->animTarget == swordgroundstabanim) {
2509                                         Person::players[k]->targetyaw += (float)(abs(Random() % 100) - 50) / 4;
2510                                     }
2511
2512                                     if (Person::players[k]->animTarget == staffgroundsmashanim)
2513                                         Person::players[k]->targettilt2 += 10;
2514
2515                                     Person::players[k]->lastattack3 = Person::players[k]->lastattack2;
2516                                     Person::players[k]->lastattack2 = Person::players[k]->lastattack;
2517                                     Person::players[k]->lastattack = Person::players[k]->animTarget;
2518
2519                                     if (Person::players[k]->animTarget == swordgroundstabanim) {
2520                                         Person::players[k]->targetyaw += 30;
2521                                     }
2522                                 }
2523                             }
2524                         }
2525                     if (!Person::players[k]->hasvictim) {
2526                         //find victim
2527                         for (unsigned i = 0; i < Person::players.size(); i++) {
2528                             if (i == k || !(i == 0 || k == 0))
2529                                 continue;
2530                             if (!Person::players[i]->skeleton.free) {
2531                                 if (Person::players[k]->hasvictim) {
2532                                     if (distsq(&Person::players[k]->coords, &Person::players[i]->coords) <
2533                                             distsq(&Person::players[k]->coords, &Person::players[k]->victim->coords))
2534                                         Person::players[k]->victim = Person::players[i];
2535                                 } else {
2536                                     Person::players[k]->victim = Person::players[i];
2537                                     Person::players[k]->hasvictim = 1;
2538                                 }
2539                             }
2540                         }
2541                     }
2542                     if (Person::players[k]->aitype == playercontrolled)
2543                         //rabbit kick
2544                         if (Person::players[k]->attackkeydown &&
2545                                 Person::players[k]->isRun() &&
2546                                 Person::players[k]->wasRun() &&
2547                                 ((Person::players[k]->hasvictim &&
2548                                   distsq(&Person::players[k]->coords, &Person::players[k]->victim->coords) < 12 * sq(Person::players[k]->scale * 5) &&
2549                                   distsq(&Person::players[k]->coords, &Person::players[k]->victim->coords) > 7 * sq(Person::players[k]->scale * 5) &&
2550                                   !Person::players[k]->victim->skeleton.free &&
2551                                   Person::players[k]->victim->animTarget != getupfrombackanim &&
2552                                   Person::players[k]->victim->animTarget != getupfromfrontanim &&
2553                                   Animation::animations[Person::players[k]->victim->animTarget].height != lowheight &&
2554                                   Person::players[k]->aitype != playercontrolled && //wat???
2555                                   normaldotproduct(Person::players[k]->facing, Person::players[k]->victim->coords - Person::players[k]->coords) > 0 &&
2556                                   Person::players[k]->rabbitkickenabled) ||
2557                                  Person::players[k]->jumpkeydown)) {
2558                             oldattackkey = 1;
2559                             Person::players[k]->setTargetAnimation(rabbitkickanim);
2560                         }
2561                     //update counts
2562                     if (Animation::animations[Person::players[k]->animTarget].attack && k == 0) {
2563                         numattacks++;
2564                         switch (attackweapon) {
2565                         case 0:
2566                             numunarmedattack++;
2567                             break;
2568                         case knife:
2569                             numknifeattack++;
2570                             break;
2571                         case sword:
2572                             numswordattack++;
2573                             break;
2574                         case staff:
2575                             numstaffattack++;
2576                             break;
2577                         }
2578                     }
2579                 }
2580             }
2581         }
2582     }
2583 }
2584
2585 void doPlayerCollisions()
2586 {
2587     static XYZ rotatetarget;
2588     static float collisionradius;
2589     if (Person::players.size() > 1)
2590         for (unsigned k = 0; k < Person::players.size(); k++)
2591             for (unsigned i = k + 1; i < Person::players.size(); i++) {
2592                 //neither player is part of a reversal
2593                 if ((Animation::animations[Person::players[i]->animTarget].attack != reversed &&
2594                         Animation::animations[Person::players[i]->animTarget].attack != reversal &&
2595                         Animation::animations[Person::players[k]->animTarget].attack != reversed &&
2596                         Animation::animations[Person::players[k]->animTarget].attack != reversal) || (i != 0 && k != 0))
2597                     if ((Animation::animations[Person::players[i]->animCurrent].attack != reversed &&
2598                             Animation::animations[Person::players[i]->animCurrent].attack != reversal &&
2599                             Animation::animations[Person::players[k]->animCurrent].attack != reversed &&
2600                             Animation::animations[Person::players[k]->animCurrent].attack != reversal) || (i != 0 && k != 0))
2601                         //neither is sleeping
2602                         if (Person::players[i]->howactive <= typesleeping && Person::players[k]->howactive <= typesleeping)
2603                             if (Person::players[i]->howactive != typesittingwall && Person::players[k]->howactive != typesittingwall)
2604                                 //in same patch, neither is climbing
2605                                 if (Person::players[i]->whichpatchx == Person::players[k]->whichpatchx &&
2606                                         Person::players[i]->whichpatchz == Person::players[k]->whichpatchz &&
2607                                         Person::players[k]->skeleton.oldfree == Person::players[k]->skeleton.free &&
2608                                         Person::players[i]->skeleton.oldfree == Person::players[i]->skeleton.free &&
2609                                         Person::players[i]->animTarget != climbanim &&
2610                                         Person::players[i]->animTarget != hanganim &&
2611                                         Person::players[k]->animTarget != climbanim &&
2612                                         Person::players[k]->animTarget != hanganim)
2613                                     //players are close (bounding box test)
2614                                     if (Person::players[i]->coords.y > Person::players[k]->coords.y - 3)
2615                                         if (Person::players[i]->coords.y < Person::players[k]->coords.y + 3)
2616                                             if (Person::players[i]->coords.x > Person::players[k]->coords.x - 3)
2617                                                 if (Person::players[i]->coords.x < Person::players[k]->coords.x + 3)
2618                                                     if (Person::players[i]->coords.z > Person::players[k]->coords.z - 3)
2619                                                         if (Person::players[i]->coords.z < Person::players[k]->coords.z + 3) {
2620                                                             //spread fire from player to player
2621                                                             if (distsq(&Person::players[i]->coords, &Person::players[k]->coords)
2622                                                                     < 3 * sq((Person::players[i]->scale + Person::players[k]->scale) * 2.5)) {
2623                                                                 if (Person::players[i]->onfire || Person::players[k]->onfire) {
2624                                                                     if (!Person::players[i]->onfire)
2625                                                                         Person::players[i]->CatchFire();
2626                                                                     if (!Person::players[k]->onfire)
2627                                                                         Person::players[k]->CatchFire();
2628                                                                 }
2629                                                             }
2630
2631                                                             XYZ tempcoords1 = Person::players[i]->coords;
2632                                                             XYZ tempcoords2 = Person::players[k]->coords;
2633                                                             if (!Person::players[i]->skeleton.oldfree)
2634                                                                 tempcoords1.y += Person::players[i]->jointPos(abdomen).y * Person::players[i]->scale;
2635                                                             if (!Person::players[k]->skeleton.oldfree)
2636                                                                 tempcoords2.y += Person::players[k]->jointPos(abdomen).y * Person::players[k]->scale;
2637                                                             collisionradius = 1.2 * sq((Person::players[i]->scale + Person::players[k]->scale) * 2.5);
2638                                                             if (Person::players[0]->hasvictim)
2639                                                                 if (Person::players[0]->animTarget == rabbitkickanim && (k == 0 || i == 0) && !Person::players[0]->victim->skeleton.free)
2640                                                                     collisionradius = 3;
2641                                                             if ((!Person::players[i]->skeleton.oldfree || !Person::players[k]->skeleton.oldfree) &&
2642                                                                     (distsq(&tempcoords1, &tempcoords2) < collisionradius ||
2643                                                                      distsq(&Person::players[i]->coords, &Person::players[k]->coords) < collisionradius)) {
2644                                                                 //jump down on a dead body
2645                                                                 if (k == 0 || i == 0) {
2646                                                                     int l = i ? i : k;
2647                                                                     if (Person::players[0]->animTarget == jumpdownanim &&
2648                                                                             !Person::players[0]->skeleton.oldfree &&
2649                                                                             !Person::players[0]->skeleton.free &&
2650                                                                             Person::players[l]->skeleton.oldfree &&
2651                                                                             Person::players[l]->skeleton.free &&
2652                                                                             Person::players[l]->dead &&
2653                                                                             Person::players[0]->lastcollide <= 0 &&
2654                                                                             fabs(Person::players[l]->coords.y - Person::players[0]->coords.y) < .2 &&
2655                                                                             distsq(&Person::players[0]->coords, &Person::players[l]->coords) < .7 * sq((Person::players[l]->scale + Person::players[0]->scale) * 2.5)) {
2656                                                                         Person::players[0]->coords.y = Person::players[l]->coords.y;
2657                                                                         Person::players[l]->velocity = Person::players[0]->velocity;
2658                                                                         Person::players[l]->skeleton.free = 0;
2659                                                                         Person::players[l]->yaw = 0;
2660                                                                         Person::players[l]->RagDoll(0);
2661                                                                         Person::players[l]->DoDamage(20);
2662                                                                         camerashake += .3;
2663                                                                         Person::players[l]->skeleton.longdead = 0;
2664                                                                         Person::players[0]->lastcollide = 1;
2665                                                                     }
2666                                                                 }
2667
2668                                                                 if (     (Person::players[i]->skeleton.oldfree == 1 && findLengthfast(&Person::players[i]->velocity) > 1) ||
2669                                                                          (Person::players[k]->skeleton.oldfree == 1 && findLengthfast(&Person::players[k]->velocity) > 1) ||
2670                                                                          (Person::players[i]->skeleton.oldfree == 0 && Person::players[k]->skeleton.oldfree == 0)) {
2671                                                                     rotatetarget = Person::players[k]->velocity - Person::players[i]->velocity;
2672                                                                     if ((Person::players[i]->animTarget != getupfrombackanim && Person::players[i]->animTarget != getupfromfrontanim ||
2673                                                                             Person::players[i]->skeleton.free) &&
2674                                                                             (Person::players[k]->animTarget != getupfrombackanim && Person::players[k]->animTarget != getupfromfrontanim ||
2675                                                                              Person::players[k]->skeleton.free))
2676                                                                         if ((((k != 0 && findLengthfast(&rotatetarget) > 150 ||
2677                                                                                 k == 0 && findLengthfast(&rotatetarget) > 50 && Person::players[0]->rabbitkickragdoll) &&
2678                                                                                 normaldotproduct(rotatetarget, Person::players[k]->coords - Person::players[i]->coords) > 0) &&
2679                                                                                 (k == 0 ||
2680                                                                                  k != 0 && Person::players[i]->skeleton.oldfree == 1 && Animation::animations[Person::players[k]->animCurrent].attack == neutral ||
2681                                                                                  /*i!=0&&*/Person::players[k]->skeleton.oldfree == 1 && Animation::animations[Person::players[i]->animCurrent].attack == neutral)) ||
2682                                                                                 (Person::players[i]->animTarget == jumpupanim || Person::players[i]->animTarget == jumpdownanim || Person::players[i]->isFlip()) &&
2683                                                                                 (Person::players[k]->animTarget == jumpupanim || Person::players[k]->animTarget == jumpdownanim || Person::players[k]->isFlip()) &&
2684                                                                                 k == 0 && !Person::players[i]->skeleton.oldfree && !Person::players[k]->skeleton.oldfree) {
2685                                                                             //If hit by body
2686                                                                             if (     (i != 0 || Person::players[i]->skeleton.free) &&
2687                                                                                      (k != 0 || Person::players[k]->skeleton.free) ||
2688                                                                                      (Animation::animations[Person::players[i]->animTarget].height == highheight &&
2689                                                                                       Animation::animations[Person::players[k]->animTarget].height == highheight)) {
2690                                                                                 if (!Tutorial::active) {
2691                                                                                     emit_sound_at(heavyimpactsound, Person::players[i]->coords);
2692                                                                                 }
2693
2694                                                                                 Person::players[i]->RagDoll(0);
2695                                                                                 if (Person::players[i]->damage > Person::players[i]->damagetolerance - findLengthfast(&rotatetarget) / 4 && !Person::players[i]->dead) {
2696                                                                                     award_bonus(0, aimbonus);
2697                                                                                 }
2698                                                                                 Person::players[i]->DoDamage(findLengthfast(&rotatetarget) / 4);
2699                                                                                 Person::players[k]->RagDoll(0);
2700                                                                                 if (Person::players[k]->damage > Person::players[k]->damagetolerance - findLengthfast(&rotatetarget) / 4 && !Person::players[k]->dead) {
2701                                                                                     award_bonus(0, aimbonus); // Huh, again?
2702                                                                                 }
2703                                                                                 Person::players[k]->DoDamage(findLengthfast(&rotatetarget) / 4);
2704
2705                                                                                 for (unsigned j = 0; j < Person::players[i]->skeleton.joints.size(); j++) {
2706                                                                                     Person::players[i]->skeleton.joints[j].velocity = Person::players[i]->skeleton.joints[j].velocity / 5 + Person::players[k]->velocity;
2707                                                                                 }
2708                                                                                 for (unsigned j = 0; j < Person::players[k]->skeleton.joints.size(); j++) {
2709                                                                                     Person::players[k]->skeleton.joints[j].velocity = Person::players[k]->skeleton.joints[j].velocity / 5 + Person::players[i]->velocity;
2710                                                                                 }
2711
2712                                                                             }
2713                                                                         }
2714                                                                     if (     (Animation::animations[Person::players[i]->animTarget].attack == neutral ||
2715                                                                               Animation::animations[Person::players[i]->animTarget].attack == normalattack) &&
2716                                                                              (Animation::animations[Person::players[k]->animTarget].attack == neutral ||
2717                                                                               Animation::animations[Person::players[k]->animTarget].attack == normalattack)) {
2718                                                                         //If bumped
2719                                                                         if (Person::players[i]->skeleton.oldfree == 0 && Person::players[k]->skeleton.oldfree == 0) {
2720                                                                             if (distsq(&Person::players[k]->coords, &Person::players[i]->coords) < .5 * sq((Person::players[i]->scale + Person::players[k]->scale) * 2.5)) {
2721                                                                                 rotatetarget = Person::players[k]->coords - Person::players[i]->coords;
2722                                                                                 Normalise(&rotatetarget);
2723                                                                                 Person::players[k]->coords = (Person::players[k]->coords + Person::players[i]->coords) / 2;
2724                                                                                 Person::players[i]->coords = Person::players[k]->coords - rotatetarget * fast_sqrt(.6) / 2
2725                                                                                                    * sq((Person::players[i]->scale + Person::players[k]->scale) * 2.5);
2726                                                                                 Person::players[k]->coords += rotatetarget * fast_sqrt(.6) / 2 * sq((Person::players[i]->scale + Person::players[k]->scale) * 2.5);
2727                                                                                 if (Person::players[k]->howactive == typeactive || hostile)
2728                                                                                     if (Person::players[k]->isIdle()) {
2729                                                                                         if (Person::players[k]->howactive < typesleeping)
2730                                                                                             Person::players[k]->setTargetAnimation(Person::players[k]->getStop());
2731                                                                                         else if (Person::players[k]->howactive == typesleeping)
2732                                                                                             Person::players[k]->setTargetAnimation(getupfromfrontanim);
2733                                                                                         if (!editorenabled)
2734                                                                                             Person::players[k]->howactive = typeactive;
2735                                                                                     }
2736                                                                                 if (Person::players[i]->howactive == typeactive || hostile)
2737                                                                                     if (Person::players[i]->isIdle()) {
2738                                                                                         if (Person::players[i]->howactive < typesleeping)
2739                                                                                             Person::players[i]->setTargetAnimation(Person::players[k]->getStop());
2740                                                                                         else
2741                                                                                             Person::players[i]->setTargetAnimation(getupfromfrontanim);
2742                                                                                         if (!editorenabled)
2743                                                                                             Person::players[i]->howactive = typeactive;
2744                                                                                     }
2745                                                                             }
2746                                                                             //jump down on player
2747                                                                             if (hostile) {
2748                                                                                 if (k == 0 && i != 0 && Person::players[k]->animTarget == jumpdownanim &&
2749                                                                                         !Person::players[i]->isCrouch() &&
2750                                                                                         Person::players[i]->animTarget != rollanim &&
2751                                                                                         !Person::players[k]->skeleton.oldfree && !
2752                                                                                         Person::players[k]->skeleton.free &&
2753                                                                                         Person::players[k]->lastcollide <= 0 &&
2754                                                                                         Person::players[k]->velocity.y < -10) {
2755                                                                                     Person::players[i]->velocity = Person::players[k]->velocity;
2756                                                                                     Person::players[k]->velocity = Person::players[k]->velocity * -.5;
2757                                                                                     Person::players[k]->velocity.y = Person::players[i]->velocity.y;
2758                                                                                     Person::players[i]->DoDamage(20);
2759                                                                                     Person::players[i]->RagDoll(0);
2760                                                                                     Person::players[k]->lastcollide = 1;
2761                                                                                     award_bonus(k, AboveBonus);
2762                                                                                 }
2763                                                                                 if (i == 0 && k != 0 && Person::players[i]->animTarget == jumpdownanim &&
2764                                                                                         !Person::players[k]->isCrouch() &&
2765                                                                                         Person::players[k]->animTarget != rollanim &&
2766                                                                                         !Person::players[i]->skeleton.oldfree &&
2767                                                                                         !Person::players[i]->skeleton.free &&
2768                                                                                         Person::players[i]->lastcollide <= 0 &&
2769                                                                                         Person::players[i]->velocity.y < -10) {
2770                                                                                     Person::players[k]->velocity = Person::players[i]->velocity;
2771                                                                                     Person::players[i]->velocity = Person::players[i]->velocity * -.3;
2772                                                                                     Person::players[i]->velocity.y = Person::players[k]->velocity.y;
2773                                                                                     Person::players[k]->DoDamage(20);
2774                                                                                     Person::players[k]->RagDoll(0);
2775                                                                                     Person::players[i]->lastcollide = 1;
2776                                                                                     award_bonus(i, AboveBonus);
2777                                                                                 }
2778                                                                             }
2779                                                                         }
2780                                                                     }
2781                                                                 }
2782                                                                 Person::players[i]->CheckKick();
2783                                                                 Person::players[k]->CheckKick();
2784                                                             }
2785                                                         }
2786             }
2787 }
2788
2789 void Game::Tick()
2790 {
2791     static XYZ facing, flatfacing;
2792     static int target;
2793
2794     Input::Tick();
2795
2796     if (Input::isKeyPressed(SDL_SCANCODE_F6)) {
2797         if (Input::isKeyDown(SDL_SCANCODE_LSHIFT))
2798             stereoreverse = true;
2799         else
2800             stereoreverse = false;
2801
2802         if (stereoreverse)
2803             printf("Stereo reversed\n");
2804         else
2805             printf("Stereo unreversed\n");
2806     }
2807
2808     if (Input::isKeyDown(SDL_SCANCODE_F7)) {
2809         if (Input::isKeyDown(SDL_SCANCODE_LSHIFT))
2810             stereoseparation -= 0.001;
2811         else
2812             stereoseparation -= 0.010;
2813         printf("Stereo decreased increased to %f\n", stereoseparation);
2814     }
2815
2816     if (Input::isKeyDown(SDL_SCANCODE_F8)) {
2817         if (Input::isKeyDown(SDL_SCANCODE_LSHIFT))
2818             stereoseparation += 0.001;
2819         else
2820             stereoseparation += 0.010;
2821         printf("Stereo separation increased to %f\n", stereoseparation);
2822     }
2823
2824
2825     if (Input::isKeyPressed(SDL_SCANCODE_TAB) && Tutorial::active) {
2826         if (Tutorial::stage != 51) {
2827             Tutorial::stagetime = Tutorial::maxtime;
2828         }
2829         emit_sound_np(consolefailsound, 128.);
2830     }
2831
2832     /*
2833     Values of mainmenu :
2834     1 Main menu
2835     2 Menu pause (resume/end game)
2836     3 Option menu
2837     4 Controls configuration menu
2838     5 Main game menu (choose level or challenge)
2839     6 Deleting user menu
2840     7 User managment menu (select/add)
2841     8 Choose difficulty menu
2842     9 Challenge level selection menu
2843     10 End of the campaign congratulation (is that really a menu?)
2844     11 Same that 9 ??? => unused
2845     18 stereo configuration
2846     */
2847
2848     if (!console) {
2849         //campaign over?
2850         if (mainmenu && endgame == 1)
2851             mainmenu = 10;
2852         //go to level select after completing a campaign level
2853         if (campaign && winfreeze && mainmenu == 0 && campaignlevels[actuallevel].choosenext == 1) {
2854             mainmenu = 5;
2855             gameon = 0;
2856             winfreeze = 0;
2857             fireSound();
2858             flash();
2859             if (musictoggle) {
2860                 OPENAL_SetFrequency(OPENAL_ALL);
2861                 emit_stream_np(stream_menutheme);
2862                 pause_sound(leveltheme);
2863             }
2864             Menu::Load();
2865         }
2866         //escape key pressed
2867         if (Input::isKeyPressed(SDL_SCANCODE_ESCAPE) &&
2868                 (gameon || mainmenu == 0)) {
2869             selected = -1;
2870             if (mainmenu == 0 && !winfreeze)
2871                 mainmenu = 2; //pause
2872             else if (mainmenu == 1 || mainmenu == 2) {
2873                 mainmenu = 0; //unpause
2874             }
2875             //play menu theme
2876             if (musictoggle && (mainmenu == 1 || mainmenu == 2)) {
2877                 OPENAL_SetFrequency(OPENAL_ALL);
2878                 emit_stream_np(stream_menutheme);
2879                 pause_sound(leveltheme);
2880             }
2881             //on resume, play level music
2882             if (!mainmenu) {
2883                 pause_sound(stream_menutheme);
2884                 resume_stream(leveltheme);
2885             }
2886         }
2887     }
2888
2889     if (mainmenu) {
2890         Menu::Tick();
2891     }
2892
2893     if (!mainmenu) {
2894         if (hostile == 1)
2895             hostiletime += multiplier;
2896         else
2897             hostiletime = 0;
2898         if (!winfreeze)
2899             leveltime += multiplier;
2900
2901         //keys
2902         if (Input::isKeyDown(SDL_SCANCODE_LALT) && Input::isKeyPressed(SDL_SCANCODE_V) && devtools) {
2903             freeze = !freeze;
2904             if (freeze) {
2905                 OPENAL_SetFrequency(OPENAL_ALL);
2906             }
2907         }
2908
2909         if (Input::isKeyPressed(consolekey) && devtools) {
2910             console = !console;
2911             if (console) {
2912                 OPENAL_SetFrequency(OPENAL_ALL);
2913             } else {
2914                 freeze = 0;
2915                 waiting = false;
2916             }
2917         }
2918
2919         if (console)
2920             freeze = 1;
2921         if (console && !Input::isKeyDown(SDL_SCANCODE_LGUI)) {
2922             inputText(consoletext[0], &consoleselected);
2923             if (!waiting) {
2924                 if (!consoletext[0].empty()) {
2925                     cmd_dispatch(consoletext[0]);
2926                     for (int k = 14; k >= 1; k--) {
2927                         consoletext[k] = consoletext[k - 1];
2928                     }
2929                     consoletext[0].clear();
2930                     consoleselected = 0;
2931                 }
2932             }
2933
2934             consoleblinkdelay -= multiplier;
2935             if (consoleblinkdelay <= 0) {
2936                 consoleblinkdelay = .3;
2937                 consoleblink = !consoleblink;
2938             }
2939         }
2940
2941         static int oldwinfreeze;
2942         if (winfreeze && !oldwinfreeze) {
2943             OPENAL_SetFrequency(OPENAL_ALL);
2944             emit_sound_np(consolesuccesssound);
2945         }
2946         if (winfreeze == 0)
2947             oldwinfreeze = winfreeze;
2948         else
2949             oldwinfreeze++;
2950
2951         if ((Input::isKeyPressed(jumpkey) || Input::isKeyPressed(SDL_SCANCODE_SPACE)) && !campaign)
2952             if (winfreeze)
2953                 winfreeze = 0;
2954         if ((Input::isKeyDown(SDL_SCANCODE_ESCAPE)) && !campaign && gameon) {
2955             if (console) {
2956                 console = false;
2957                 freeze = 0;
2958             } else if (winfreeze) {
2959                 mainmenu = 9;
2960                 gameon = 0;
2961             }
2962         }
2963
2964
2965
2966         if (!freeze && !winfreeze && !(mainmenu && gameon) && (gameon || !gamestarted)) {
2967
2968             //dialogues
2969             static float talkdelay = 0;
2970
2971             if (Dialog::inDialog())
2972                 talkdelay = 1;
2973             talkdelay -= multiplier;
2974
2975             if (talkdelay <= 0 && !Dialog::inDialog() && Animation::animations[Person::players[0]->animTarget].height != highheight) {
2976                 for (unsigned i = 0; i < Dialog::dialogs.size(); i++) {
2977                     Dialog::dialogs[i].tick(i);
2978                 }
2979             }
2980
2981             windvar += multiplier;
2982             smoketex += multiplier;
2983             Tutorial::stagetime += multiplier;
2984
2985             //hotspots
2986             static float hotspotvisual[40];
2987             if (Hotspot::hotspots.size()) {
2988                 XYZ hotspotsprite;
2989                 if (editorenabled) {
2990                     for (unsigned i = 0; i < Hotspot::hotspots.size(); i++) {
2991                         hotspotvisual[i] -= multiplier / 320;
2992                     }
2993                 }
2994
2995                 for (unsigned i = 0; i < Hotspot::hotspots.size(); i++) {
2996                     while (hotspotvisual[i] < 0) {
2997                         hotspotsprite = 0;
2998                         hotspotsprite.x = float(abs(Random() % 100000)) / 100000 * Hotspot::hotspots[i].size;
2999                         hotspotsprite = DoRotation(hotspotsprite, 0, 0, Random() % 360);
3000                         hotspotsprite = DoRotation(hotspotsprite, 0, Random() % 360, 0);
3001                         hotspotsprite += Hotspot::hotspots[i].position;
3002                         Sprite::MakeSprite(breathsprite, hotspotsprite, hotspotsprite * 0, 1, 0.5, 0, 7, 0.4);
3003                         hotspotvisual[i] += 0.1 / Hotspot::hotspots[i].size / Hotspot::hotspots[i].size / Hotspot::hotspots[i].size;
3004                     }
3005                 }
3006
3007                 for (unsigned i = 0; i < Hotspot::hotspots.size(); i++) {
3008                     if (Hotspot::hotspots[i].type <= 10 && Hotspot::hotspots[i].type > 0) {
3009                         Hotspot::hotspots[i].position = Person::players[Hotspot::hotspots[i].type]->coords;
3010                     }
3011                 }
3012             }
3013
3014             //Tutorial
3015             if (Tutorial::active) {
3016                 Tutorial::Do(multiplier);
3017             }
3018
3019             //bonuses
3020             if (!Tutorial::active) {
3021                 if (bonustime == 0 &&
3022                         bonus != solidhit &&
3023                         bonus != spinecrusher &&
3024                         bonus != tracheotomy &&
3025                         bonus != backstab &&
3026                         bonusvalue > 10) {
3027                     emit_sound_np(consolesuccesssound);
3028                 }
3029             } else if (bonustime == 0) {
3030                 emit_sound_np(fireendsound);
3031             }
3032             if (bonustime == 0) {
3033                 if (bonus != solidhit &&
3034                         bonus != twoxcombo &&
3035                         bonus != threexcombo &&
3036                         bonus != fourxcombo &&
3037                         bonus != megacombo)
3038                     bonusnum[bonus]++;
3039                 else
3040                     bonusnum[bonus] += 0.15;
3041                 if (Tutorial::active)
3042                     bonusvalue = 0;
3043                 bonusvalue /= bonusnum[bonus];
3044                 bonustotal += bonusvalue;
3045             }
3046             bonustime += multiplier;
3047
3048             //snow effects
3049             if (environment == snowyenvironment) {
3050                 precipdelay -= multiplier;
3051                 while (precipdelay < 0) {
3052                     precipdelay += .04;
3053                     if (!detail)
3054                         precipdelay += .04;
3055                     XYZ footvel, footpoint;
3056
3057                     footvel = 0;
3058                     footpoint = viewer + viewerfacing * 6;
3059                     footpoint.y += ((float)abs(Random() % 1200)) / 100 - 6;
3060                     footpoint.x += ((float)abs(Random() % 1200)) / 100 - 6;
3061                     footpoint.z += ((float)abs(Random() % 1200)) / 100 - 6;
3062                     Sprite::MakeSprite(snowsprite, footpoint, footvel, 1, 1, 1, .1, 1);
3063                 }
3064             }
3065
3066
3067             doAerialAcrobatics();
3068
3069
3070             static XYZ oldviewer;
3071
3072             //control keys
3073             if (!Dialog::inDialog()) {
3074                 Person::players[0]->forwardkeydown = Input::isKeyDown(forwardkey);
3075                 Person::players[0]->leftkeydown = Input::isKeyDown(leftkey);
3076                 Person::players[0]->backkeydown = Input::isKeyDown(backkey);
3077                 Person::players[0]->rightkeydown = Input::isKeyDown(rightkey);
3078                 Person::players[0]->jumpkeydown = Input::isKeyDown(jumpkey);
3079                 Person::players[0]->crouchkeydown = Input::isKeyDown(crouchkey);
3080                 Person::players[0]->drawkeydown = Input::isKeyDown(drawkey);
3081                 Person::players[0]->throwkeydown = Input::isKeyDown(throwkey);
3082             } else {
3083                 Person::players[0]->forwardkeydown = 0;
3084                 Person::players[0]->leftkeydown = 0;
3085                 Person::players[0]->backkeydown = 0;
3086                 Person::players[0]->rightkeydown = 0;
3087                 Person::players[0]->jumpkeydown = 0;
3088                 Person::players[0]->crouchkeydown = 0;
3089                 Person::players[0]->drawkeydown = 0;
3090                 Person::players[0]->throwkeydown = 0;
3091             }
3092
3093             if (!Person::players[0]->jumpkeydown)
3094                 Person::players[0]->jumpclimb = 0;
3095
3096
3097             if (Dialog::inDialog()) {
3098                 cameramode = 1;
3099                 if (Dialog::directing) {
3100                     facing = 0;
3101                     facing.z = -1;
3102
3103                     facing = DoRotation(facing, -pitch, 0, 0);
3104                     facing = DoRotation(facing, 0, 0 - yaw, 0);
3105
3106                     flatfacing = 0;
3107                     flatfacing.z = -1;
3108
3109                     flatfacing = DoRotation(flatfacing, 0, -yaw, 0);
3110
3111                     if (Input::isKeyDown(forwardkey))
3112                         viewer += facing * multiplier * 4;
3113                     if (Input::isKeyDown(backkey))
3114                         viewer -= facing * multiplier * 4;
3115                     if (Input::isKeyDown(leftkey))
3116                         viewer += DoRotation(flatfacing * multiplier, 0, 90, 0) * 4;
3117                     if (Input::isKeyDown(rightkey))
3118                         viewer += DoRotation(flatfacing * multiplier, 0, -90, 0) * 4;
3119                     if (Input::isKeyDown(jumpkey))
3120                         viewer.y += multiplier * 4;
3121                     if (Input::isKeyDown(crouchkey))
3122                         viewer.y -= multiplier * 4;
3123                     if (     Input::isKeyPressed(SDL_SCANCODE_1) ||
3124                              Input::isKeyPressed(SDL_SCANCODE_2) ||
3125                              Input::isKeyPressed(SDL_SCANCODE_3) ||
3126                              Input::isKeyPressed(SDL_SCANCODE_4) ||
3127                              Input::isKeyPressed(SDL_SCANCODE_5) ||
3128                              Input::isKeyPressed(SDL_SCANCODE_6) ||
3129                              Input::isKeyPressed(SDL_SCANCODE_7) ||
3130                              Input::isKeyPressed(SDL_SCANCODE_8) ||
3131                              Input::isKeyPressed(SDL_SCANCODE_9) ||
3132                              Input::isKeyPressed(SDL_SCANCODE_0) ||
3133                              Input::isKeyPressed(SDL_SCANCODE_MINUS)) {
3134                         int whichend;
3135                         if (Input::isKeyPressed(SDL_SCANCODE_1)) whichend = 1;
3136                         if (Input::isKeyPressed(SDL_SCANCODE_2)) whichend = 2;
3137                         if (Input::isKeyPressed(SDL_SCANCODE_3)) whichend = 3;
3138                         if (Input::isKeyPressed(SDL_SCANCODE_4)) whichend = 4;
3139                         if (Input::isKeyPressed(SDL_SCANCODE_5)) whichend = 5;
3140                         if (Input::isKeyPressed(SDL_SCANCODE_6)) whichend = 6;
3141                         if (Input::isKeyPressed(SDL_SCANCODE_7)) whichend = 7;
3142                         if (Input::isKeyPressed(SDL_SCANCODE_8)) whichend = 8;
3143                         if (Input::isKeyPressed(SDL_SCANCODE_9)) whichend = 9;
3144                         if (Input::isKeyPressed(SDL_SCANCODE_0)) whichend = 0;
3145                         if (Input::isKeyPressed(SDL_SCANCODE_MINUS))
3146                             whichend = -1;
3147                         if (whichend != -1) {
3148                             Dialog::currentScene().participantfocus = whichend;
3149                             Dialog::currentDialog().participantlocation[whichend] = Person::players[whichend]->coords;
3150                             Dialog::currentDialog().participantyaw[whichend] = Person::players[whichend]->yaw;
3151                         }
3152                         if (whichend == -1) {
3153                             Dialog::currentScene().participantfocus = -1;
3154                         }
3155                         /* FIXME: potentially accessing -1 in Person::players! */
3156                         if (Person::players[Dialog::currentScene().participantfocus]->dead) {
3157                             Dialog::indialogue = -1;
3158                             Dialog::directing = false;
3159                             cameramode = 0;
3160                         }
3161                         Dialog::currentScene().camera = viewer;
3162                         Dialog::currentScene().camerayaw = yaw;
3163                         Dialog::currentScene().camerapitch = pitch;
3164                         Dialog::indialogue++;
3165                         if (Dialog::indialogue < int(Dialog::currentDialog().scenes.size())) {
3166                             if (Dialog::currentScene().sound != 0) {
3167                                 playdialoguescenesound();
3168                             }
3169                         }
3170
3171                         for (unsigned j = 0; j < Person::players.size(); j++) {
3172                             Dialog::currentScene().participantfacing[j] = Dialog::currentDialog().scenes[Dialog::indialogue - 1].participantfacing[j];
3173                         }
3174                     }
3175                     //TODO: should these be KeyDown or KeyPressed?
3176                     if (     Input::isKeyDown(SDL_SCANCODE_KP_1) ||
3177                              Input::isKeyDown(SDL_SCANCODE_KP_2) ||
3178                              Input::isKeyDown(SDL_SCANCODE_KP_3) ||
3179                              Input::isKeyDown(SDL_SCANCODE_KP_4) ||
3180                              Input::isKeyDown(SDL_SCANCODE_KP_5) ||
3181                              Input::isKeyDown(SDL_SCANCODE_KP_6) ||
3182                              Input::isKeyDown(SDL_SCANCODE_KP_7) ||
3183                              Input::isKeyDown(SDL_SCANCODE_KP_8) ||
3184                              Input::isKeyDown(SDL_SCANCODE_KP_9) ||
3185                              Input::isKeyDown(SDL_SCANCODE_KP_0)) {
3186                         int whichend;
3187                         if (Input::isKeyDown(SDL_SCANCODE_KP_1)) whichend = 1;
3188                         if (Input::isKeyDown(SDL_SCANCODE_KP_2)) whichend = 2;
3189                         if (Input::isKeyDown(SDL_SCANCODE_KP_3)) whichend = 3;
3190                         if (Input::isKeyDown(SDL_SCANCODE_KP_4)) whichend = 4;
3191                         if (Input::isKeyDown(SDL_SCANCODE_KP_5)) whichend = 5;
3192                         if (Input::isKeyDown(SDL_SCANCODE_KP_6)) whichend = 6;
3193                         if (Input::isKeyDown(SDL_SCANCODE_KP_7)) whichend = 7;
3194                         if (Input::isKeyDown(SDL_SCANCODE_KP_8)) whichend = 8;
3195                         if (Input::isKeyDown(SDL_SCANCODE_KP_9)) whichend = 9;
3196                         if (Input::isKeyDown(SDL_SCANCODE_KP_0)) whichend = 0;
3197                         Dialog::currentScene().participantfacing[whichend] = facing;
3198                     }
3199                     if (Dialog::indialogue >= int(Dialog::currentDialog().scenes.size())) {
3200                         Dialog::indialogue = -1;
3201                         Dialog::directing = false;
3202                         cameramode = 0;
3203                     }
3204                 }
3205                 if (!Dialog::directing) {
3206                     pause_sound(whooshsound);
3207                     viewer = Dialog::currentScene().camera;
3208                     viewer.y = max((double)viewer.y, terrain.getHeight(viewer.x, viewer.z) + .1);
3209                     yaw = Dialog::currentScene().camerayaw;
3210                     pitch = Dialog::currentScene().camerapitch;
3211                     if (Dialog::dialoguetime > 0.5) {
3212                         if (Input::isKeyPressed(attackkey)) {
3213                             Dialog::indialogue++;
3214                             if (Dialog::indialogue < int(Dialog::currentDialog().scenes.size())) {
3215                                 if (Dialog::currentScene().sound != 0) {
3216                                     playdialoguescenesound();
3217                                     if (Dialog::currentScene().sound == -5) {
3218                                         Hotspot::hotspots.emplace_back(Person::players[0]->coords, -1, 10);
3219                                     }
3220                                     if (Dialog::currentScene().sound == -6) {
3221                                         hostile = 1;
3222                                     }
3223
3224                                     if (Person::players[Dialog::currentScene().participantfocus]->dead) {
3225                                         Dialog::indialogue = -1;
3226                                         Dialog::directing = false;
3227                                         cameramode = 0;
3228                                     }
3229                                 }
3230                             }
3231                         }
3232                     }
3233                     if (Dialog::indialogue >= int(Dialog::currentDialog().scenes.size())) {
3234                         Dialog::indialogue = -1;
3235                         Dialog::directing = false;
3236                         cameramode = 0;
3237                         if (Dialog::currentDialog().type > 19 && Dialog::currentDialog().type < 30) {
3238                             hostile = 1;
3239                         }
3240                         if (Dialog::currentDialog().type > 29 && Dialog::currentDialog().type < 40) {
3241                             windialogue = true;
3242                         }
3243                         if (Dialog::currentDialog().type > 49 && Dialog::currentDialog().type < 60) {
3244                             hostile = 1;
3245                             for (unsigned i = 1; i < Person::players.size(); i++) {
3246                                 Person::players[i]->aitype = attacktypecutoff;
3247                             }
3248                         }
3249                     }
3250                 }
3251             }
3252
3253             if (!Person::players[0]->jumpkeydown) {
3254                 Person::players[0]->jumptogglekeydown = 0;
3255             }
3256             if (Person::players[0]->jumpkeydown &&
3257                     Person::players[0]->animTarget != jumpupanim &&
3258                     Person::players[0]->animTarget != jumpdownanim &&
3259                     !Person::players[0]->isFlip())
3260                 Person::players[0]->jumptogglekeydown = 1;
3261
3262
3263             Dialog::dialoguetime += multiplier;
3264             hawkyaw += multiplier * 25;
3265             realhawkcoords = 0;
3266             realhawkcoords.x = 25;
3267             realhawkcoords = DoRotation(realhawkcoords, 0, hawkyaw, 0) + hawkcoords;
3268             hawkcalldelay -= multiplier / 2;
3269
3270             if (hawkcalldelay <= 0) {
3271                 emit_sound_at(hawksound, realhawkcoords);
3272
3273                 hawkcalldelay = 16 + abs(Random() % 8);
3274             }
3275
3276             doDevKeys();
3277
3278             doAttacks();
3279
3280             doPlayerCollisions();
3281
3282             doJumpReversals();
3283
3284             for (unsigned k = 0; k < Person::players.size(); k++)
3285                 if (k != 0 && Person::players[k]->immobile)
3286                     Person::players[k]->coords = Person::players[k]->realoldcoords;
3287
3288             for (unsigned k = 0; k < Person::players.size(); k++) {
3289                 if (!isnormal(Person::players[k]->coords.x) || !isnormal(Person::players[k]->coords.y) || !isnormal(Person::players[k]->coords.z)) {
3290                     if (!isnormal(Person::players[k]->coords.x) || !isnormal(Person::players[k]->coords.y) || !isnormal(Person::players[k]->coords.z)) {
3291                         Person::players[k]->DoDamage(1000);
3292                     }
3293                 }
3294             }
3295
3296             //respawn
3297             static bool respawnkeydown;
3298             if (!editorenabled &&
3299                     (whichlevel != -2 &&
3300                      (Input::isKeyDown(SDL_SCANCODE_Z) &&
3301                       Input::isKeyDown(SDL_SCANCODE_LGUI) &&
3302                       devtools) ||
3303                      (Input::isKeyDown(jumpkey) &&
3304                       !respawnkeydown &&
3305                       !oldattackkey &&
3306                       Person::players[0]->dead))) {
3307                 targetlevel = whichlevel;
3308                 loading = 1;
3309                 leveltime = 5;
3310             }
3311             respawnkeydown = Input::isKeyDown(jumpkey);
3312
3313
3314             static bool movekey;
3315
3316             //?
3317             for (unsigned i = 0; i < Person::players.size(); i++) {
3318                 static float oldtargetyaw;
3319                 if (!Person::players[i]->skeleton.free) {
3320                     oldtargetyaw = Person::players[i]->targetyaw;
3321                     if (i == 0 && !Dialog::inDialog()) {
3322                         //TODO: refactor repetitive code
3323                         if (!Animation::animations[Person::players[0]->animTarget].attack &&
3324                                 Person::players[0]->animTarget != staggerbackhighanim &&
3325                                 Person::players[0]->animTarget != staggerbackhardanim &&
3326                                 Person::players[0]->animTarget != crouchremoveknifeanim &&
3327                                 Person::players[0]->animTarget != removeknifeanim &&
3328                                 Person::players[0]->animTarget != backhandspringanim &&
3329                                 Person::players[0]->animTarget != dodgebackanim &&
3330                                 Person::players[0]->animTarget != walljumprightkickanim &&
3331                                 Person::players[0]->animTarget != walljumpleftkickanim) {
3332                             if (cameramode)
3333                                 Person::players[0]->targetyaw = 0;
3334                             else
3335                                 Person::players[0]->targetyaw = -yaw + 180;
3336                         }
3337
3338                         facing = 0;
3339                         facing.z = -1;
3340
3341                         flatfacing = DoRotation(facing, 0, Person::players[i]->yaw + 180, 0);
3342                         if (cameramode) {
3343                             facing = flatfacing;
3344                         } else {
3345                             facing = DoRotation(facing, -pitch, 0, 0);
3346                             facing = DoRotation(facing, 0, 0 - yaw, 0);
3347                         }
3348
3349                         Person::players[0]->lookyaw = -yaw;
3350
3351                         Person::players[i]->targetheadyaw = yaw;
3352                         Person::players[i]->targetheadpitch = pitch;
3353                     }
3354                     if (i != 0 && Person::players[i]->aitype == playercontrolled && !Dialog::inDialog()) {
3355                         if (!Animation::animations[Person::players[i]->animTarget].attack &&
3356                                 Person::players[i]->animTarget != staggerbackhighanim &&
3357                                 Person::players[i]->animTarget != staggerbackhardanim &&
3358                                 Person::players[i]->animTarget != crouchremoveknifeanim &&
3359                                 Person::players[i]->animTarget != removeknifeanim &&
3360                                 Person::players[i]->animTarget != backhandspringanim &&
3361                                 Person::players[i]->animTarget != dodgebackanim &&
3362                                 Person::players[i]->animTarget != walljumprightkickanim &&
3363                                 Person::players[i]->animTarget != walljumpleftkickanim) {
3364                             Person::players[i]->targetyaw = -Person::players[i]->lookyaw + 180;
3365                         }
3366
3367                         facing = 0;
3368                         facing.z = -1;
3369
3370                         flatfacing = DoRotation(facing, 0, Person::players[i]->yaw + 180, 0);
3371
3372                         facing = DoRotation(facing, -Person::players[i]->lookpitch, 0, 0);
3373                         facing = DoRotation(facing, 0, 0 - Person::players[i]->lookyaw, 0);
3374
3375                         Person::players[i]->targetheadyaw = Person::players[i]->lookyaw;
3376                         Person::players[i]->targetheadpitch = Person::players[i]->lookpitch;
3377                     }
3378                     if (Dialog::inDialog()) {
3379                         Person::players[i]->targetheadyaw = 180 - roughDirection(Dialog::currentScene().participantfacing[i]);
3380                         Person::players[i]->targetheadpitch = pitchOf(Dialog::currentScene().participantfacing[i]);
3381                     }
3382
3383                     if (leveltime < .5)
3384                         numenvsounds = 0;
3385
3386                     Person::players[i]->avoidsomething = 0;
3387
3388                     //avoid flaming things
3389                     for (unsigned int j = 0; j < Object::objects.size(); j++)
3390                         if (Object::objects[j]->onfire)
3391                             if (distsq(&Person::players[i]->coords, &Object::objects[j]->position) < sq(Object::objects[j]->scale) * 200)
3392                                 if (     distsq(&Person::players[i]->coords, &Object::objects[j]->position) <
3393                                          distsq(&Person::players[i]->coords, &Person::players[0]->coords)) {
3394                                     Person::players[i]->collided = 0;
3395                                     Person::players[i]->avoidcollided = 1;
3396                                     if (Person::players[i]->avoidsomething == 0 ||
3397                                             distsq(&Person::players[i]->coords, &Object::objects[j]->position) <
3398                                             distsq(&Person::players[i]->coords, &Person::players[i]->avoidwhere)) {
3399                                         Person::players[i]->avoidwhere = Object::objects[j]->position;
3400                                         Person::players[i]->avoidsomething = 1;
3401                                     }
3402                                 }
3403
3404                     //avoid flaming players
3405                     for (unsigned j = 0; j < Person::players.size(); j++)
3406                         if (Person::players[j]->onfire)
3407                             if (distsq(&Person::players[j]->coords, &Person::players[i]->coords) < sq(0.3) * 200)
3408                                 if (     distsq(&Person::players[i]->coords, &Person::players[j]->coords) <
3409                                          distsq(&Person::players[i]->coords, &Person::players[0]->coords)) {
3410                                     Person::players[i]->collided = 0;
3411                                     Person::players[i]->avoidcollided = 1;
3412                                     if (Person::players[i]->avoidsomething == 0 ||
3413                                             distsq(&Person::players[i]->coords, &Person::players[j]->coords) <
3414                                             distsq(&Person::players[i]->coords, &Person::players[i]->avoidwhere)) {
3415                                         Person::players[i]->avoidwhere = Person::players[j]->coords;
3416                                         Person::players[i]->avoidsomething = 1;
3417                                     }
3418                                 }
3419
3420                     if (Person::players[i]->collided > .8)
3421                         Person::players[i]->avoidcollided = 0;
3422
3423                     Person::players[i]->doAI();
3424
3425                     if (Animation::animations[Person::players[i]->animTarget].attack == reversed) {
3426                         //Person::players[i]->targetyaw=Person::players[i]->yaw;
3427                         Person::players[i]->forwardkeydown = 0;
3428                         Person::players[i]->leftkeydown = 0;
3429                         Person::players[i]->backkeydown = 0;
3430                         Person::players[i]->rightkeydown = 0;
3431                         Person::players[i]->jumpkeydown = 0;
3432                         Person::players[i]->attackkeydown = 0;
3433                         //Person::players[i]->crouchkeydown=0;
3434                         Person::players[i]->throwkeydown = 0;
3435                     }
3436
3437                     if (Dialog::inDialog()) {
3438                         Person::players[i]->forwardkeydown = 0;
3439                         Person::players[i]->leftkeydown = 0;
3440                         Person::players[i]->backkeydown = 0;
3441                         Person::players[i]->rightkeydown = 0;
3442                         Person::players[i]->jumpkeydown = 0;
3443                         Person::players[i]->crouchkeydown = 0;
3444                         Person::players[i]->drawkeydown = 0;
3445                         Person::players[i]->throwkeydown = 0;
3446                     }
3447
3448                     if (Person::players[i]->collided < -.3)
3449                         Person::players[i]->collided = -.3;
3450                     if (Person::players[i]->collided > 1)
3451                         Person::players[i]->collided = 1;
3452                     Person::players[i]->collided -= multiplier * 4;
3453                     Person::players[i]->whichdirectiondelay -= multiplier;
3454                     if (Person::players[i]->avoidcollided < -.3 || Person::players[i]->whichdirectiondelay <= 0) {
3455                         Person::players[i]->avoidcollided = -.3;
3456                         Person::players[i]->whichdirection = abs(Random() % 2);
3457                         Person::players[i]->whichdirectiondelay = .4;
3458                     }
3459                     if (Person::players[i]->avoidcollided > 1)
3460                         Person::players[i]->avoidcollided = 1;
3461                     Person::players[i]->avoidcollided -= multiplier / 4;
3462                     if (!Person::players[i]->skeleton.free) {
3463                         Person::players[i]->stunned -= multiplier;
3464                         Person::players[i]->surprised -= multiplier;
3465                     }
3466                     if (i != 0 && Person::players[i]->surprised <= 0 &&
3467                             Person::players[i]->aitype == attacktypecutoff &&
3468                             !Person::players[i]->dead &&
3469                             !Person::players[i]->skeleton.free &&
3470                             Animation::animations[Person::players[i]->animTarget].attack == neutral)
3471                         numresponded = 1;
3472
3473                     if (!Person::players[i]->throwkeydown)
3474                         Person::players[i]->throwtogglekeydown = 0;
3475
3476                     //pick up weapon
3477                     if (Person::players[i]->throwkeydown && !Person::players[i]->throwtogglekeydown) {
3478                         if (Person::players[i]->weaponactive == -1 &&
3479                                 Person::players[i]->num_weapons < 2 &&
3480                                 (Person::players[i]->isIdle() ||
3481                                  Person::players[i]->isCrouch() ||
3482                                  Person::players[i]->animTarget == sneakanim ||
3483                                  Person::players[i]->animTarget == rollanim ||
3484                                  Person::players[i]->animTarget == backhandspringanim ||
3485                                  Person::players[i]->isFlip() ||
3486                                  Person::players[i]->aitype != playercontrolled)) {
3487                             for (unsigned j = 0; j < weapons.size(); j++) {
3488                                 if ((weapons[j].velocity.x == 0 && weapons[j].velocity.y == 0 && weapons[j].velocity.z == 0 ||
3489                                         Person::players[i]->aitype == playercontrolled) &&
3490                                         weapons[j].owner == -1 &&
3491                                         Person::players[i]->weaponactive == -1)
3492                                     if (distsqflat(&Person::players[i]->coords, &weapons[j].position) < 2) {
3493                                         if (distsq(&Person::players[i]->coords, &weapons[j].position) < 2) {
3494                                             if (Person::players[i]->isCrouch() ||
3495                                                     Person::players[i]->animTarget == sneakanim ||
3496                                                     Person::players[i]->isRun() ||
3497                                                     Person::players[i]->isIdle() ||
3498                                                     Person::players[i]->aitype != playercontrolled) {
3499                                                 Person::players[i]->throwtogglekeydown = 1;
3500                                                 Person::players[i]->setTargetAnimation(crouchremoveknifeanim);
3501                                                 Person::players[i]->targetyaw = roughDirectionTo(Person::players[i]->coords, weapons[j].position);
3502                                                 Person::players[i]->hasvictim = 0;
3503                                             }
3504                                             if (Person::players[i]->animTarget == rollanim || Person::players[i]->animTarget == backhandspringanim) {
3505                                                 Person::players[i]->throwtogglekeydown = 1;
3506                                                 Person::players[i]->hasvictim = 0;
3507
3508                                                 if ((weapons[j].velocity.x == 0 && weapons[j].velocity.y == 0 && weapons[j].velocity.z == 0 ||
3509                                                         Person::players[i]->aitype == playercontrolled) &&
3510                                                         weapons[j].owner == -1 ||
3511                                                         Person::players[i]->victim &&
3512                                                         weapons[j].owner == int(Person::players[i]->victim->id))
3513                                                     if (distsqflat(&Person::players[i]->coords, &weapons[j].position) < 2 && Person::players[i]->weaponactive == -1)
3514                                                         if (distsq(&Person::players[i]->coords, &weapons[j].position) < 1 || Person::players[i]->victim) {
3515                                                             if (weapons[j].getType() != staff)
3516                                                                 emit_sound_at(knifedrawsound, Person::players[i]->coords, 128.);
3517
3518                                                             Person::players[i]->takeWeapon(j);
3519                                                         }
3520                                             }
3521                                         } else if ((Person::players[i]->isIdle() ||
3522                                                     Person::players[i]->isFlip() ||
3523                                                     Person::players[i]->aitype != playercontrolled) &&
3524                                                    distsq(&Person::players[i]->coords, &weapons[j].position) < 5 &&
3525                                                    Person::players[i]->coords.y < weapons[j].position.y) {
3526                                             if (!Person::players[i]->isFlip()) {
3527                                                 Person::players[i]->throwtogglekeydown = 1;
3528                                                 Person::players[i]->setTargetAnimation(removeknifeanim);
3529                                                 Person::players[i]->targetyaw = roughDirectionTo(Person::players[i]->coords, weapons[j].position);
3530                                             }
3531                                             if (Person::players[i]->isFlip()) {
3532                                                 Person::players[i]->throwtogglekeydown = 1;
3533                                                 Person::players[i]->hasvictim = 0;
3534
3535                                                 for (unsigned k = 0; k < weapons.size(); k++) {
3536                                                     if (Person::players[i]->weaponactive == -1)
3537                                                         if ((weapons[k].velocity.x == 0 && weapons[k].velocity.y == 0 && weapons[k].velocity.z == 0 ||
3538                                                                 Person::players[i]->aitype == playercontrolled) &&
3539                                                                 weapons[k].owner == -1 ||
3540                                                                 Person::players[i]->victim &&
3541                                                                 weapons[k].owner == int(Person::players[i]->victim->id))
3542                                                             if (distsqflat(&Person::players[i]->coords, &weapons[k].position) < 3 &&
3543                                                                     Person::players[i]->weaponactive == -1) {
3544                                                                 if (weapons[k].getType() != staff)
3545                                                                     emit_sound_at(knifedrawsound, Person::players[i]->coords, 128.);
3546
3547                                                                 Person::players[i]->takeWeapon(k);
3548                                                             }
3549                                                 }
3550                                             }
3551                                         }
3552                                     }
3553                             }
3554                             if (Person::players[i]->isCrouch() ||
3555                                     Person::players[i]->animTarget == sneakanim ||
3556                                     Person::players[i]->isRun() ||
3557                                     Person::players[i]->isIdle() || Person::players[i]->animTarget == rollanim ||
3558                                     Person::players[i]->animTarget == backhandspringanim) {
3559                                 if (Person::players.size() > 1)
3560                                     for (unsigned j = 0; j < Person::players.size(); j++) {
3561                                         if (Person::players[i]->weaponactive == -1)
3562                                             if (j != i)
3563                                                 if (Person::players[j]->num_weapons &&
3564                                                         Person::players[j]->skeleton.free &&
3565                                                         distsq(&Person::players[i]->coords, &Person::players[j]->coords) < 2/*&&Person::players[j]->dead*/ &&
3566                                                         (((Person::players[j]->skeleton.forward.y < 0 &&
3567                                                            Person::players[j]->weaponstuckwhere == 0) ||
3568                                                           (Person::players[j]->skeleton.forward.y > 0 &&
3569                                                            Person::players[j]->weaponstuckwhere == 1)) ||
3570                                                          Person::players[j]->weaponstuck == -1 ||
3571                                                          Person::players[j]->num_weapons > 1)) {
3572                                                     if (Person::players[i]->animTarget != rollanim && Person::players[i]->animTarget != backhandspringanim) {
3573                                                         Person::players[i]->throwtogglekeydown = 1;
3574                                                         Person::players[i]->victim = Person::players[j];
3575                                                         Person::players[i]->hasvictim = 1;
3576                                                         Person::players[i]->setTargetAnimation(crouchremoveknifeanim);
3577                                                         Person::players[i]->targetyaw = roughDirectionTo(Person::players[i]->coords, Person::players[j]->coords);
3578                                                     }
3579                                                     if (Person::players[i]->animTarget == rollanim || Person::players[i]->animTarget == backhandspringanim) {
3580                                                         Person::players[i]->throwtogglekeydown = 1;
3581                                                         Person::players[i]->victim = Person::players[j];
3582                                                         Person::players[i]->hasvictim = 1;
3583                                                         int k = Person::players[j]->weaponids[0];
3584                                                         if (Person::players[i]->hasvictim) {
3585                                                             bool fleshstuck;
3586                                                             fleshstuck = 0;
3587                                                             if (Person::players[i]->victim->weaponstuck != -1) {
3588                                                                 if (Person::players[i]->victim->weaponids[Person::players[i]->victim->weaponstuck] == k) {
3589                                                                     fleshstuck = 1;
3590                                                                 }
3591                                                             }
3592                                                             if (!fleshstuck) {
3593                                                                 if (weapons[k].getType() != staff)
3594                                                                     emit_sound_at(knifedrawsound, Person::players[i]->coords, 128.);
3595                                                             }
3596                                                             if (fleshstuck)
3597                                                                 emit_sound_at(fleshstabremovesound, Person::players[i]->coords, 128.);
3598
3599                                                             if (weapons[k].owner != -1) {
3600                                                                 if (Person::players[i]->victim->num_weapons == 1)
3601                                                                     Person::players[i]->victim->num_weapons = 0;
3602                                                                 else
3603                                                                     Person::players[i]->victim->num_weapons = 1;
3604
3605                                                                 Person::players[i]->victim->skeleton.longdead = 0;
3606                                                                 Person::players[i]->victim->skeleton.free = 1;
3607                                                                 Person::players[i]->victim->skeleton.broken = 0;
3608
3609                                                                 for (unsigned l = 0; l < Person::players[i]->victim->skeleton.joints.size(); l++) {
3610                                                                     Person::players[i]->victim->skeleton.joints[l].velchange = 0;
3611                                                                     Person::players[i]->victim->skeleton.joints[l].locked = 0;
3612                                                                 }
3613
3614                                                                 XYZ relative;
3615                                                                 relative = 0;
3616                                                                 relative.y = 10;
3617                                                                 Normalise(&relative);
3618                                                                 XYZ footvel, footpoint;
3619                                                                 footvel = 0;
3620                                                                 footpoint = weapons[k].position;
3621                                                                 if (Person::players[i]->victim->weaponstuck != -1) {
3622                                                                     if (Person::players[i]->victim->weaponids[Person::players[i]->victim->weaponstuck] == k) {
3623                                                                         if (bloodtoggle)
3624                                                                             Sprite::MakeSprite(cloudimpactsprite, footpoint, footvel, 1, 0, 0, .8, .3);
3625                                                                         weapons[k].bloody = 2;
3626                                                                         weapons[k].blooddrip = 5;
3627                                                                         Person::players[i]->victim->weaponstuck = -1;
3628                                                                         Person::players[i]->victim->bloodloss += 2000;
3629                                                                         Person::players[i]->victim->DoDamage(2000);
3630                                                                     }
3631                                                                 }
3632                                                                 if (Person::players[i]->victim->num_weapons > 0) {
3633                                                                     if (Person::players[i]->victim->weaponstuck != 0 && Person::players[i]->victim->weaponstuck != -1)
3634                                                                         Person::players[i]->victim->weaponstuck = 0;
3635                                                                     if (Person::players[i]->victim->weaponids[0] == k)
3636                                                                         Person::players[i]->victim->weaponids[0] = Person::players[i]->victim->weaponids[Person::players[i]->victim->num_weapons];
3637                                                                 }
3638
3639                                                                 Person::players[i]->victim->weaponactive = -1;
3640
3641                                                                 Person::players[i]->victim->jointVel(abdomen) += relative * 6;
3642                                                                 Person::players[i]->victim->jointVel(neck) += relative * 6;
3643                                                                 Person::players[i]->victim->jointVel(rightshoulder) += relative * 6;
3644                                                                 Person::players[i]->victim->jointVel(leftshoulder) += relative * 6;
3645                                                             }
3646                                                             Person::players[i]->takeWeapon(k);
3647                                                         }
3648                                                     }
3649                                                 }
3650                                     }
3651                             }
3652                         }
3653                         if (Person::players[i]->weaponactive != -1 && Person::players[i]->aitype == playercontrolled) {
3654                             if (weapons[Person::players[i]->weaponids[0]].getType() == knife) {
3655                                 if (Person::players[i]->isIdle() ||
3656                                         Person::players[i]->isRun() ||
3657                                         Person::players[i]->isCrouch() ||
3658                                         Person::players[i]->animTarget == sneakanim ||
3659                                         Person::players[i]->isFlip())
3660                                     if (Person::players.size() > 1)
3661                                         for (unsigned j = 0; j < Person::players.size(); j++) {
3662                                             if (i != j)
3663                                                 if (!Tutorial::active || Tutorial::stage == 49)
3664                                                     if (hostile)
3665                                                         if (normaldotproduct(Person::players[i]->facing, Person::players[i]->coords - Person::players[j]->coords) < 0 &&
3666                                                                 distsq(&Person::players[i]->coords, &Person::players[j]->coords) < 100 &&
3667                                                                 distsq(&Person::players[i]->coords, &Person::players[j]->coords) > 1.5 &&
3668                                                                 !Person::players[j]->skeleton.free &&
3669                                                                 -1 == Object::checkcollide(DoRotation(Person::players[j]->jointPos(head), 0, Person::players[j]->yaw, 0)*Person::players[j]->scale + Person::players[j]->coords, DoRotation(Person::players[i]->jointPos(head), 0, Person::players[i]->yaw, 0)*Person::players[i]->scale + Person::players[i]->coords)) {
3670                                                             if (!Person::players[i]->isFlip()) {
3671                                                                 Person::players[i]->throwtogglekeydown = 1;
3672                                                                 Person::players[i]->victim = Person::players[j];
3673                                                                 Person::players[i]->setTargetAnimation(knifethrowanim);
3674                                                                 Person::players[i]->targetyaw = roughDirectionTo(Person::players[i]->coords, Person::players[j]->coords);
3675                                                                 Person::players[i]->targettilt2 = pitchTo(Person::players[i]->coords, Person::players[j]->coords);
3676                                                             }
3677                                                             if (Person::players[i]->isFlip()) {
3678                                                                 if (Person::players[i]->weaponactive != -1) {
3679                                                                     Person::players[i]->throwtogglekeydown = 1;
3680                                                                     Person::players[i]->victim = Person::players[j];
3681                                                                     XYZ aim;
3682                                                                     aim = Person::players[i]->victim->coords + DoRotation(Person::players[i]->victim->jointPos(abdomen), 0, Person::players[i]->victim->yaw, 0) * Person::players[i]->victim->scale + Person::players[i]->victim->velocity * findDistance(&Person::players[i]->victim->coords, &Person::players[i]->coords) / 50 - (Person::players[i]->coords + DoRotation(Person::players[i]->jointPos(righthand), 0, Person::players[i]->yaw, 0) * Person::players[i]->scale);
3683                                                                     Normalise(&aim);
3684
3685                                                                     aim = DoRotation(aim, (float)abs(Random() % 30) - 15, (float)abs(Random() % 30) - 15, 0);
3686
3687                                                                     weapons[Person::players[i]->weaponids[0]].thrown(aim * 50, false);
3688                                                                     Person::players[i]->num_weapons--;
3689                                                                     if (Person::players[i]->num_weapons) {
3690                                                                         Person::players[i]->weaponids[0] = Person::players[i]->weaponids[Person::players[i]->num_weapons];
3691                                                                     }
3692                                                                     Person::players[i]->weaponactive = -1;
3693                                                                 }
3694                                                             }
3695                                                         }
3696                                         }
3697                             }
3698                         }
3699                         if (Person::players[i]->weaponactive != -1 && Person::players[i]->aitype == playercontrolled) {
3700                             if (Person::players[i]->isCrouch() || Person::players[i]->animTarget == sneakanim) {
3701                                 Person::players[i]->throwtogglekeydown = 1;
3702                                 XYZ tempVelocity = Person::players[i]->velocity * .2;
3703                                 if (tempVelocity.x == 0)
3704                                     tempVelocity.x = .1;
3705                                 weapons[Person::players[i]->weaponids[0]].drop(tempVelocity, tempVelocity, false);
3706                                 Person::players[i]->num_weapons--;
3707                                 if (Person::players[i]->num_weapons) {
3708                                     Person::players[i]->weaponids[0] = Person::players[i]->weaponids[Person::players[i]->num_weapons];
3709                                     if (Person::players[i]->weaponstuck == Person::players[i]->num_weapons)
3710                                         Person::players[i]->weaponstuck = 0;
3711                                 }
3712
3713                                 Person::players[i]->weaponactive = -1;
3714                                 for (unsigned j = 0; j < Person::players.size(); j++) {
3715                                     Person::players[j]->wentforweapon = 0;
3716                                 }
3717                             }
3718                         }
3719
3720                     }
3721
3722                     //draw weapon
3723                     if (i == 0 || !Person::players[0]->dead || (Person::players[i]->weaponactive != -1)) {
3724                         if (Person::players[i]->drawkeydown && !Person::players[i]->drawtogglekeydown ||
3725                                 (Person::players[i]->num_weapons == 2) &&
3726                                 (Person::players[i]->weaponactive == -1) &&
3727                                 Person::players[i]->isIdle() ||
3728                                 Person::players[0]->dead &&
3729                                 (Person::players[i]->weaponactive != -1) &&
3730                                 i != 0) {
3731                             bool isgood = true;
3732                             if (Person::players[i]->weaponactive != -1)
3733                                 if (weapons[Person::players[i]->weaponids[Person::players[i]->weaponactive]].getType() == staff)
3734                                     isgood = false;
3735                             if (isgood && Person::players[i]->creature != wolftype) {
3736                                 if (Person::players[i]->isIdle() && Person::players[i]->num_weapons && weapons[Person::players[i]->weaponids[0]].getType() == knife) {
3737                                     Person::players[i]->setTargetAnimation(drawrightanim);
3738                                     Person::players[i]->drawtogglekeydown = 1;
3739                                 }
3740                                 if ((Person::players[i]->isIdle() ||
3741                                         (Person::players[i]->aitype != playercontrolled &&
3742                                          Person::players[0]->weaponactive != -1 &&
3743                                          Person::players[i]->isRun())) &&
3744                                         Person::players[i]->num_weapons &&
3745                                         weapons[Person::players[i]->weaponids[0]].getType() == sword) {
3746                                     Person::players[i]->setTargetAnimation(drawleftanim);
3747                                     Person::players[i]->drawtogglekeydown = 1;
3748                                 }
3749                                 if (Person::players[i]->isCrouch() && Person::players[i]->num_weapons && weapons[Person::players[i]->weaponids[0]].getType() == knife) {
3750                                     Person::players[i]->setTargetAnimation(crouchdrawrightanim);
3751                                     Person::players[i]->drawtogglekeydown = 1;
3752                                 }
3753                             }
3754                         }
3755                     }
3756
3757                     //clean weapon
3758                     if (Person::players[i]->weaponactive != -1) {
3759                         if (Person::players[i]->isCrouch() &&
3760                                 weapons[Person::players[i]->weaponids[Person::players[i]->weaponactive]].bloody &&
3761                                 bloodtoggle &&
3762                                 Person::players[i]->onterrain &&
3763                                 Person::players[i]->num_weapons &&
3764                                 Person::players[i]->attackkeydown &&
3765                                 musictype != stream_fighttheme) {
3766                             if (weapons[Person::players[i]->weaponids[Person::players[i]->weaponactive]].getType() == knife)
3767                                 Person::players[i]->setTargetAnimation(crouchstabanim);
3768                             if (weapons[Person::players[i]->weaponids[Person::players[i]->weaponactive]].getType() == sword)
3769                                 Person::players[i]->setTargetAnimation(swordgroundstabanim);
3770                             Person::players[i]->hasvictim = 0;
3771                         }
3772                     }
3773
3774                     if (!Person::players[i]->drawkeydown)
3775                         Person::players[i]->drawtogglekeydown = 0;
3776
3777                     XYZ absflatfacing;
3778                     if (i == 0) {
3779                         absflatfacing = 0;
3780                         absflatfacing.z = -1;
3781
3782                         absflatfacing = DoRotation(absflatfacing, 0, -yaw, 0);
3783                     } else
3784                         absflatfacing = flatfacing;
3785
3786                     if (Dialog::inDialog()) {
3787                         Person::players[i]->forwardkeydown = 0;
3788                         Person::players[i]->leftkeydown = 0;
3789                         Person::players[i]->backkeydown = 0;
3790                         Person::players[i]->rightkeydown = 0;
3791                         Person::players[i]->jumpkeydown = 0;
3792                         Person::players[i]->crouchkeydown = 0;
3793                         Person::players[i]->drawkeydown = 0;
3794                         Person::players[i]->throwkeydown = 0;
3795                     }
3796                     movekey = 0;
3797                     //Do controls
3798                     if (!Animation::animations[Person::players[i]->animTarget].attack &&
3799                             Person::players[i]->animTarget != staggerbackhighanim &&
3800                             Person::players[i]->animTarget != staggerbackhardanim &&
3801                             Person::players[i]->animTarget != backhandspringanim &&
3802                             Person::players[i]->animTarget != dodgebackanim) {
3803                         if (!Person::players[i]->forwardkeydown)
3804                             Person::players[i]->forwardstogglekeydown = 0;
3805                         if (Person::players[i]->crouchkeydown) {
3806                             //Crouch
3807                             target = -2;
3808                             if (i == 0) {
3809                                 Person::players[i]->superruntoggle = 1;
3810                                 if (Person::players.size() > 1)
3811                                     for (unsigned j = 0; j < Person::players.size(); j++)
3812                                         if (j != i && !Person::players[j]->skeleton.free && Person::players[j]->aitype == passivetype)
3813                                             if (distsq(&Person::players[j]->coords, &Person::players[i]->coords) < 16)
3814                                                 Person::players[i]->superruntoggle = 0;
3815                             }
3816
3817                             if (Person::players.size() > 1)
3818                                 for (unsigned j = 0; j < Person::players.size(); j++) {
3819                                     if (j != i && !Person::players[j]->skeleton.free && Person::players[j]->victim && Person::players[i]->lowreversaldelay <= 0) {
3820                                         if (distsq(&Person::players[j]->coords, &Person::players[j]->victim->coords) < 3 &&
3821                                                 Person::players[j]->victim == Person::players[i] &&
3822                                                 (Person::players[j]->animTarget == sweepanim ||
3823                                                  Person::players[j]->animTarget == upunchanim ||
3824                                                  Person::players[j]->animTarget == wolfslapanim ||
3825                                                  ((Person::players[j]->animTarget == swordslashanim ||
3826                                                    Person::players[j]->animTarget == knifeslashstartanim ||
3827                                                    Person::players[j]->animTarget == staffhitanim ||
3828                                                    Person::players[j]->animTarget == staffspinhitanim) &&
3829                                                   distsq(&Person::players[j]->coords, &Person::players[i]->coords) < 2))) {
3830                                             if (target >= 0)
3831                                                 target = -1;
3832                                             else
3833                                                 target = j;
3834                                         }
3835                                     }
3836                                 }
3837                             if (target >= 0)
3838                                 Person::players[target]->Reverse();
3839                             Person::players[i]->lowreversaldelay = .5;
3840
3841                             if (Person::players[i]->isIdle()) {
3842                                 Person::players[i]->setTargetAnimation(Person::players[i]->getCrouch());
3843                                 Person::players[i]->transspeed = 10;
3844                             }
3845                             if (Person::players[i]->isRun() ||
3846                                     (Person::players[i]->isStop() &&
3847                                      (Person::players[i]->leftkeydown ||
3848                                       Person::players[i]->rightkeydown ||
3849                                       Person::players[i]->forwardkeydown ||
3850                                       Person::players[i]->backkeydown))) {
3851                                 Person::players[i]->setTargetAnimation(rollanim);
3852                                 Person::players[i]->transspeed = 20;
3853                             }
3854                         }
3855                         if (!Person::players[i]->crouchkeydown) {
3856                             //Uncrouch
3857                             if (!Person::players[i]->isRun() && Person::players[i]->animTarget != sneakanim && i == 0)
3858                                 Person::players[i]->superruntoggle = 0;
3859                             target = -2;
3860                             if (Person::players[i]->isCrouch()) {
3861                                 if (Person::players.size() > 1)
3862                                     for (unsigned j = 0; j < Person::players.size(); j++) {
3863                                         if (j != i &&
3864                                                 !Person::players[j]->skeleton.free &&
3865                                                 Person::players[j]->victim &&
3866                                                 Person::players[i]->highreversaldelay <= 0) {
3867                                             if (distsq(&Person::players[j]->coords, &Person::players[j]->victim->coords) < 3 &&
3868                                                     Person::players[j]->victim == Person::players[i] &&
3869                                                     (Person::players[j]->animTarget == spinkickanim) &&
3870                                                     Person::players[i]->isCrouch()) {
3871                                                 if (target >= 0)
3872                                                     target = -1;
3873                                                 else
3874                                                     target = j;
3875                                             }
3876                                         }
3877                                     }
3878                                 if (target >= 0)
3879                                     Person::players[target]->Reverse();
3880                                 Person::players[i]->highreversaldelay = .5;
3881
3882                                 if (Person::players[i]->isCrouch()) {
3883                                     if (!Person::players[i]->wasCrouch()) {
3884                                         Person::players[i]->animCurrent = Person::players[i]->getCrouch();
3885                                         Person::players[i]->frameCurrent = 0;
3886                                     }
3887                                     Person::players[i]->setTargetAnimation(Person::players[i]->getIdle());
3888                                     Person::players[i]->transspeed = 10;
3889                                 }
3890                             }
3891                             if (Person::players[i]->animTarget == sneakanim) {
3892                                 Person::players[i]->setTargetAnimation(Person::players[i]->getIdle());
3893                                 Person::players[i]->transspeed = 10;
3894                             }
3895                         }
3896                         if (Person::players[i]->forwardkeydown) {
3897                             if (Person::players[i]->isIdle() ||
3898                                     (Person::players[i]->isStop() &&
3899                                      Person::players[i]->targetyaw == Person::players[i]->yaw) ||
3900                                     (Person::players[i]->isLanding() &&
3901                                      Person::players[i]->frameTarget > 0 &&
3902                                      !Person::players[i]->jumpkeydown) ||
3903                                     (Person::players[i]->isLandhard() &&
3904                                      Person::players[i]->frameTarget > 0 &&
3905                                      !Person::players[i]->jumpkeydown &&
3906                                      Person::players[i]->crouchkeydown)) {
3907                                 if (Person::players[i]->aitype == passivetype)
3908                                     Person::players[i]->setTargetAnimation(walkanim);
3909                                 else
3910                                     Person::players[i]->setTargetAnimation(Person::players[i]->getRun());
3911                             }
3912                             if (Person::players[i]->isCrouch()) {
3913                                 Person::players[i]->animTarget = sneakanim;
3914                                 if (Person::players[i]->wasCrouch())
3915                                     Person::players[i]->target = 0;
3916                                 Person::players[i]->frameTarget = 0;
3917                             }
3918                             if (Person::players[i]->animTarget == hanganim/*&&(!Person::players[i]->forwardstogglekeydown||Person::players[i]->aitype!=playercontrolled)*/) {
3919                                 Person::players[i]->setTargetAnimation(climbanim);
3920                                 Person::players[i]->frameTarget = 1;
3921                                 Person::players[i]->jumpclimb = 1;
3922                             }
3923                             if (Person::players[i]->animTarget == jumpupanim || Person::players[i]->animTarget == jumpdownanim || Person::players[i]->isFlip()) {
3924                                 Person::players[i]->velocity += absflatfacing * 5 * multiplier;
3925                             }
3926                             Person::players[i]->forwardstogglekeydown = 1;
3927                             movekey = 1;
3928                         }
3929                         if (Person::players[i]->rightkeydown) {
3930                             if (Person::players[i]->isIdle() ||
3931                                     (Person::players[i]->isStop() &&
3932                                      Person::players[i]->targetyaw == Person::players[i]->yaw) ||
3933                                     (Person::players[i]->isLanding() &&
3934                                      Person::players[i]->frameTarget > 0 &&
3935                                      !Person::players[i]->jumpkeydown) ||
3936                                     (Person::players[i]->isLandhard() &&
3937                                      Person::players[i]->frameTarget > 0 &&
3938                                      !Person::players[i]->jumpkeydown &&
3939                                      Person::players[i]->crouchkeydown)) {
3940                                 Person::players[i]->setTargetAnimation(Person::players[i]->getRun());
3941                             }
3942                             if (Person::players[i]->isCrouch()) {
3943                                 Person::players[i]->animTarget = sneakanim;
3944                                 if (Person::players[i]->wasCrouch())
3945                                     Person::players[i]->target = 0;
3946                                 Person::players[i]->frameTarget = 0;
3947                             }
3948                             if (Person::players[i]->animTarget == jumpupanim || Person::players[i]->animTarget == jumpdownanim || Person::players[i]->isFlip()) {
3949                                 Person::players[i]->velocity += DoRotation(absflatfacing * 5 * multiplier, 0, -90, 0);
3950                             }
3951                             Person::players[i]->targetyaw -= 90;
3952                             if (Person::players[i]->forwardkeydown)
3953                                 Person::players[i]->targetyaw += 45;
3954                             if (Person::players[i]->backkeydown)
3955                                 Person::players[i]->targetyaw -= 45;
3956                             movekey = 1;
3957                         }
3958                         if ( Person::players[i]->leftkeydown) {
3959                             if (Person::players[i]->isIdle() ||
3960                                     (Person::players[i]->isStop() &&
3961                                      Person::players[i]->targetyaw == Person::players[i]->yaw) ||
3962                                     (Person::players[i]->isLanding() &&
3963                                      Person::players[i]->frameTarget > 0 &&
3964                                      !Person::players[i]->jumpkeydown) ||
3965                                     (Person::players[i]->isLandhard() &&
3966                                      Person::players[i]->frameTarget > 0 &&
3967                                      !Person::players[i]->jumpkeydown &&
3968                                      Person::players[i]->crouchkeydown)) {
3969                                 Person::players[i]->setTargetAnimation(Person::players[i]->getRun());
3970                             }
3971                             if (Person::players[i]->isCrouch()) {
3972                                 Person::players[i]->animTarget = sneakanim;
3973                                 if (Person::players[i]->wasCrouch())
3974                                     Person::players[i]->target = 0;
3975                                 Person::players[i]->frameTarget = 0;
3976                             }
3977                             if (Person::players[i]->animTarget == jumpupanim || Person::players[i]->animTarget == jumpdownanim || Person::players[i]->isFlip()) {
3978                                 Person::players[i]->velocity -= DoRotation(absflatfacing * 5 * multiplier, 0, -90, 0);
3979                             }
3980                             Person::players[i]->targetyaw += 90;
3981                             if (Person::players[i]->forwardkeydown)
3982                                 Person::players[i]->targetyaw -= 45;
3983                             if (Person::players[i]->backkeydown)
3984                                 Person::players[i]->targetyaw += 45;
3985                             movekey = 1;
3986                         }
3987                         if (Person::players[i]->backkeydown) {
3988                             if (Person::players[i]->isIdle() ||
3989                                     (Person::players[i]->isStop() &&
3990                                      Person::players[i]->targetyaw == Person::players[i]->yaw) ||
3991                                     (Person::players[i]->isLanding() &&
3992                                      Person::players[i]->frameTarget > 0 &&
3993                                      !Person::players[i]->jumpkeydown) ||
3994                                     (Person::players[i]->isLandhard() &&
3995                                      Person::players[i]->frameTarget > 0 &&
3996                                      !Person::players[i]->jumpkeydown &&
3997                                      Person::players[i]->crouchkeydown)) {
3998                                 Person::players[i]->setTargetAnimation(Person::players[i]->getRun());
3999                             }
4000                             if (Person::players[i]->isCrouch()) {
4001                                 Person::players[i]->animTarget = sneakanim;
4002                                 if (Person::players[i]->wasCrouch())
4003                                     Person::players[i]->target = 0;
4004                                 Person::players[i]->frameTarget = 0;
4005                             }
4006                             if (Person::players[i]->animTarget == jumpupanim || Person::players[i]->animTarget == jumpdownanim || Person::players[i]->isFlip()) {
4007                                 Person::players[i]->velocity -= absflatfacing * 5 * multiplier;
4008                             }
4009                             if (Person::players[i]->animTarget == hanganim) {
4010                                 Person::players[i]->animCurrent = jumpdownanim;
4011                                 Person::players[i]->animTarget = jumpdownanim;
4012                                 Person::players[i]->target = 0;
4013                                 Person::players[i]->frameCurrent = 0;
4014                                 Person::players[i]->frameTarget = 1;
4015                                 Person::players[i]->velocity = 0;
4016                                 Person::players[i]->velocity.y += gravity;
4017                                 Person::players[i]->coords.y -= 1.4;
4018                                 Person::players[i]->grabdelay = 1;
4019                             }
4020                             if ( !Person::players[i]->leftkeydown && !Person::players[i]->rightkeydown)
4021                                 Person::players[i]->targetyaw += 180;
4022                             movekey = 1;
4023                         }
4024                         if ((Person::players[i]->jumpkeydown && !Person::players[i]->jumpclimb) || Person::players[i]->jumpstart) {
4025                             if ((((Person::players[i]->isLanding() && Person::players[i]->frameTarget >= 3) ||
4026                                     Person::players[i]->isRun() ||
4027                                     Person::players[i]->animTarget == walkanim ||
4028                                     Person::players[i]->isCrouch() ||
4029                                     Person::players[i]->animTarget == sneakanim) &&
4030                                     Person::players[i]->jumppower > 1) &&
4031                                     ((Person::players[i]->animTarget != rabbitrunninganim &&
4032                                       Person::players[i]->animTarget != wolfrunninganim) || i != 0)) {
4033                                 Person::players[i]->jumpstart = 0;
4034                                 Person::players[i]->setTargetAnimation(jumpupanim);
4035                                 Person::players[i]->yaw = Person::players[i]->targetyaw;
4036                                 Person::players[i]->transspeed = 20;
4037                                 Person::players[i]->FootLand(leftfoot, 1);
4038                                 Person::players[i]->FootLand(rightfoot, 1);
4039
4040                                 facing = 0;
4041                                 facing.z = -1;
4042                                 flatfacing = DoRotation(facing, 0, Person::players[i]->targetyaw + 180, 0);
4043
4044                                 if (movekey)
4045                                     Person::players[i]->velocity = flatfacing * Person::players[i]->speed * 45 * Person::players[i]->scale;
4046                                 if (!movekey)
4047                                     Person::players[i]->velocity = 0;
4048
4049                                 //Dodge sweep?
4050                                 target = -2;
4051                                 if (Person::players.size() > 1)
4052                                     for (unsigned j = 0; j < Person::players.size(); j++) {
4053                                         if (j != i && !Person::players[j]->skeleton.free && Person::players[j]->victim) {
4054                                             if (distsq(&Person::players[j]->coords, &Person::players[j]->victim->coords) < 3 &&
4055                                                     (Person::players[j]->victim == Person::players[i]) &&
4056                                                     (Person::players[j]->animTarget == sweepanim)) {
4057                                                 if (target >= 0)
4058                                                     target = -1;
4059                                                 else
4060                                                     target = j;
4061                                             }
4062                                         }
4063                                     }
4064                                 if (target >= 0)
4065                                     Person::players[i]->velocity.y = 1;
4066                                 else
4067                                     if (Person::players[i]->crouchkeydown || Person::players[i]->aitype != playercontrolled) {
4068                                     Person::players[i]->velocity.y = 7;
4069                                     Person::players[i]->crouchtogglekeydown = 1;
4070                                 } else Person::players[i]->velocity.y = 5;
4071
4072                                 if (mousejump && i == 0 && devtools) {
4073                                     if (!Person::players[i]->isLanding())
4074                                         Person::players[i]->tempdeltav = deltav;
4075                                     if (Person::players[i]->tempdeltav < 0)
4076                                         Person::players[i]->velocity.y -= (float)(Person::players[i]->tempdeltav) / multiplier / 1000;
4077                                 }
4078
4079                                 Person::players[i]->coords.y += .2;
4080                                 Person::players[i]->jumppower -= 1;
4081
4082                                 if (!i)
4083                                     emit_sound_at(whooshsound, Person::players[i]->coords, 128.);
4084
4085                                 emit_sound_at(jumpsound, Person::players[i]->coords, 128.);
4086                             }
4087                             if ((Person::players[i]->isIdle()) && Person::players[i]->jumppower > 1) {
4088                                 Person::players[i]->setTargetAnimation(Person::players[i]->getLanding());
4089                                 Person::players[i]->frameTarget = 2;
4090                                 Person::players[i]->landhard = 0;
4091                                 Person::players[i]->jumpstart = 1;
4092                                 Person::players[i]->tempdeltav = deltav;
4093                             }
4094                             if (Person::players[i]->animTarget == jumpupanim &&
4095                                     (((!floatjump &&
4096                                        !editorenabled) ||
4097                                       !devtools) ||
4098                                      Person::players[i]->aitype != playercontrolled)) {
4099                                 if (Person::players[i]->jumppower > multiplier * 6) {
4100                                     Person::players[i]->velocity.y += multiplier * 6;
4101                                     Person::players[i]->jumppower -= multiplier * 6;
4102                                 }
4103                                 if (Person::players[i]->jumppower <= multiplier * 6) {
4104                                     Person::players[i]->velocity.y += Person::players[i]->jumppower;
4105                                     Person::players[i]->jumppower = 0;
4106                                 }
4107                             }
4108                             if (((floatjump || editorenabled) && devtools) && i == 0)
4109                                 Person::players[i]->velocity.y += multiplier * 30;
4110                         }
4111
4112                         if (!movekey) {
4113                             if (Person::players[i]->isRun() || Person::players[i]->animTarget == walkanim) {
4114                                 Person::players[i]->setTargetAnimation(Person::players[i]->getStop());
4115                             }
4116                             if (Person::players[i]->animTarget == sneakanim) {
4117                                 Person::players[i]->animTarget = Person::players[i]->getCrouch();
4118                                 if (Person::players[i]->animCurrent == sneakanim) {
4119                                     Person::players[i]->target = 0;
4120                                 }
4121                                 Person::players[i]->frameTarget = 0;
4122                             }
4123                         }
4124                         if (Person::players[i]->animTarget == walkanim &&
4125                                 (Person::players[i]->aitype == attacktypecutoff ||
4126                                  Person::players[i]->aitype == searchtype ||
4127                                  (Person::players[i]->aitype == passivetype &&
4128                                   Person::players[i]->numwaypoints <= 1)))
4129                             Person::players[i]->setTargetAnimation(Person::players[i]->getStop());
4130                         if (Person::players[i]->isRun() && (Person::players[i]->aitype == passivetype))
4131                             Person::players[i]->setTargetAnimation(Person::players[i]->getStop());
4132                     }
4133                 }
4134                 if (Person::players[i]->animTarget == rollanim)
4135                     Person::players[i]->targetyaw = oldtargetyaw;
4136             }
4137
4138             //Rotation
4139             for (unsigned k = 0; k < Person::players.size(); k++) {
4140                 if (fabs(Person::players[k]->yaw - Person::players[k]->targetyaw) > 180) {
4141                     if (Person::players[k]->yaw > Person::players[k]->targetyaw)
4142                         Person::players[k]->yaw -= 360;
4143                     else
4144                         Person::players[k]->yaw += 360;
4145                 }
4146
4147                 //stop to turn in right direction
4148                 if (fabs(Person::players[k]->yaw - Person::players[k]->targetyaw) > 90 && (Person::players[k]->isRun() || Person::players[k]->animTarget == walkanim))
4149                     Person::players[k]->setTargetAnimation(Person::players[k]->getStop());
4150
4151                 if (Person::players[k]->animTarget == backhandspringanim || Person::players[k]->animTarget == dodgebackanim)
4152                     Person::players[k]->targettilt = 0;
4153
4154                 if (Person::players[k]->animTarget != jumpupanim &&
4155                         Person::players[k]->animTarget != backhandspringanim &&
4156                         Person::players[k]->animTarget != jumpdownanim &&
4157                         !Person::players[k]->isFlip()) {
4158                     Person::players[k]->targettilt = 0;
4159                     if (Person::players[k]->jumppower < 0 && !Person::players[k]->jumpkeydown)
4160                         Person::players[k]->jumppower = 0;
4161                     Person::players[k]->jumppower += multiplier * 7;
4162                     if (Person::players[k]->isCrouch())
4163                         Person::players[k]->jumppower += multiplier * 7;
4164                     if (Person::players[k]->jumppower > 5)
4165                         Person::players[k]->jumppower = 5;
4166                 }
4167
4168                 if (Person::players[k]->isRun())
4169                     Person::players[k]->targettilt = (Person::players[k]->yaw - Person::players[k]->targetyaw) / 4;
4170
4171                 Person::players[k]->tilt = stepTowardf(Person::players[k]->tilt, Person::players[k]->targettilt, multiplier * 150);
4172                 Person::players[k]->grabdelay -= multiplier;
4173             }
4174
4175             //do animations
4176             for (unsigned k = 0; k < Person::players.size(); k++) {
4177                 Person::players[k]->DoAnimations();
4178                 Person::players[k]->whichpatchx = Person::players[k]->coords.x / (terrain.size / subdivision * terrain.scale);
4179                 Person::players[k]->whichpatchz = Person::players[k]->coords.z / (terrain.size / subdivision * terrain.scale);
4180             }
4181
4182             //do stuff
4183             Object::DoStuff();
4184
4185             for (int j = numenvsounds - 1; j >= 0; j--) {
4186                 envsoundlife[j] -= multiplier;
4187                 if (envsoundlife[j] < 0) {
4188                     numenvsounds--;
4189                     envsoundlife[j] = envsoundlife[numenvsounds];
4190                     envsound[j] = envsound[numenvsounds];
4191                 }
4192             }
4193             OPENAL_SetFrequency(OPENAL_ALL, slomo);
4194
4195             if (Tutorial::active) {
4196                 Tutorial::DoStuff(multiplier);
4197             }
4198
4199
4200             //3d sound
4201             static float gLoc[3];
4202             gLoc[0] = viewer.x;
4203             gLoc[1] = viewer.y;
4204             gLoc[2] = viewer.z;
4205             static float vel[3];
4206             vel[0] = (viewer.x - oldviewer.x) / multiplier;
4207             vel[1] = (viewer.y - oldviewer.y) / multiplier;
4208             vel[2] = (viewer.z - oldviewer.z) / multiplier;
4209
4210             //Set orientation with forward and up vectors
4211             static XYZ upvector;
4212             upvector = 0;
4213             upvector.z = -1;
4214
4215             upvector = DoRotation(upvector, -pitch + 90, 0, 0);
4216             upvector = DoRotation(upvector, 0, 0 - yaw, 0);
4217
4218             facing = 0;
4219             facing.z = -1;
4220
4221             facing = DoRotation(facing, -pitch, 0, 0);
4222             facing = DoRotation(facing, 0, 0 - yaw, 0);
4223
4224
4225             static float ori[6];
4226             ori[0] = -facing.x;
4227             ori[1] = facing.y;
4228             ori[2] = -facing.z;
4229             ori[3] = -upvector.x;
4230             ori[4] = upvector.y;
4231             ori[5] = -upvector.z;
4232
4233             OPENAL_3D_Listener_SetAttributes(&gLoc[0], &vel[0], ori[0], ori[1], ori[2], ori[3], ori[4], ori[5]);
4234             OPENAL_Update();
4235
4236             oldviewer = viewer;
4237         }
4238     }
4239
4240     if (Input::isKeyPressed(SDL_SCANCODE_F1))
4241         Screenshot();
4242 }
4243
4244 void Game::TickOnce()
4245 {
4246     if (mainmenu) {
4247         yaw += multiplier * 5;
4248     } else if (Dialog::directing || !Dialog::inDialog()) {
4249         yaw += deltah * .7;
4250         if (invertmouse) {
4251             pitch -= deltav * .7;
4252         } else {
4253             pitch += deltav * .7;
4254         }
4255         if (pitch > 90)
4256             pitch = 90;
4257         if (pitch < -70)
4258             pitch = -70;
4259     }
4260 }
4261
4262 void Game::TickOnceAfter()
4263 {
4264     static XYZ colviewer;
4265     static XYZ coltarget;
4266     static XYZ target;
4267     static XYZ col;
4268     static XYZ facing;
4269     static float changedelay;
4270     static bool alldead;
4271     static float unseendelay;
4272     static float cameraspeed;
4273
4274     if (!mainmenu) {
4275         static int oldmusictype = musictype;
4276
4277         if (environment == snowyenvironment)
4278             leveltheme = stream_snowtheme;
4279         if (environment == grassyenvironment)
4280             leveltheme = stream_grasstheme;
4281         if (environment == desertenvironment)
4282             leveltheme = stream_deserttheme;
4283
4284         realthreat = 0;
4285
4286         musictype = leveltheme;
4287         for (unsigned i = 0; i < Person::players.size(); i++) {
4288             if ((Person::players[i]->aitype == attacktypecutoff ||
4289                     Person::players[i]->aitype == getweapontype ||
4290                     Person::players[i]->aitype == gethelptype ||
4291                     Person::players[i]->aitype == searchtype) &&
4292                     !Person::players[i]->dead &&
4293                     (Person::players[i]->animTarget != sneakattackedanim &&
4294                      Person::players[i]->animTarget != knifesneakattackedanim &&
4295                      Person::players[i]->animTarget != swordsneakattackedanim)) {
4296                 musictype = stream_fighttheme;
4297                 realthreat = 1;
4298             }
4299         }
4300         if (Person::players[0]->dead)
4301             musictype = stream_menutheme;
4302
4303
4304         if (musictype == stream_fighttheme)
4305             unseendelay = 1;
4306
4307         if (oldmusictype == stream_fighttheme && musictype != stream_fighttheme) {
4308             unseendelay -= multiplier;
4309             if (unseendelay > 0)
4310                 musictype = stream_fighttheme;
4311         }
4312
4313
4314         if (loading == 2) {
4315             musictype = stream_menutheme;
4316             musicvolume[2] = 512;
4317             musicvolume[0] = 0;
4318             musicvolume[1] = 0;
4319             musicvolume[3] = 0;
4320         }
4321
4322         if (musictoggle)
4323             if (musictype != oldmusictype && musictype == stream_fighttheme)
4324                 emit_sound_np(alarmsound);
4325         musicselected = musictype;
4326
4327         if (musicselected == leveltheme)
4328             musicvolume[0] += multiplier * 450;
4329         else
4330             musicvolume[0] -= multiplier * 450;
4331         if (musicselected == stream_fighttheme)
4332             musicvolume[1] += multiplier * 450;
4333         else
4334             musicvolume[1] -= multiplier * 450;
4335         if (musicselected == stream_menutheme)
4336             musicvolume[2] += multiplier * 450;
4337         else
4338             musicvolume[2] -= multiplier * 450;
4339
4340         for (int i = 0; i < 3; i++) {
4341             if (musicvolume[i] < 0)
4342                 musicvolume[i] = 0;
4343             if (musicvolume[i] > 512)
4344                 musicvolume[i] = 512;
4345         }
4346
4347         if (musicvolume[2] > 128 && !loading && !mainmenu)
4348             musicvolume[2] = 128;
4349
4350         if (musictoggle) {
4351             if (musicvolume[0] > 0 && oldmusicvolume[0] <= 0)
4352                 emit_stream_np(leveltheme, musicvolume[0]);
4353             if (musicvolume[1] > 0 && oldmusicvolume[1] <= 0)
4354                 emit_stream_np(stream_fighttheme, musicvolume[1]);
4355             if (musicvolume[2] > 0 && oldmusicvolume[2] <= 0)
4356                 emit_stream_np(stream_menutheme, musicvolume[2]);
4357             if (musicvolume[0] <= 0 && oldmusicvolume[0] > 0)
4358                 pause_sound(leveltheme);
4359             if (musicvolume[1] <= 0 && oldmusicvolume[1] > 0)
4360                 pause_sound(stream_fighttheme);
4361             if (musicvolume[2] <= 0 && oldmusicvolume[2] > 0)
4362                 pause_sound(stream_menutheme);
4363
4364             if (musicvolume[0] != oldmusicvolume[0])
4365                 OPENAL_SetVolume(channels[leveltheme], musicvolume[0]);
4366             if (musicvolume[1] != oldmusicvolume[1])
4367                 OPENAL_SetVolume(channels[stream_fighttheme], musicvolume[1]);
4368             if (musicvolume[2] != oldmusicvolume[2])
4369                 OPENAL_SetVolume(channels[stream_menutheme], musicvolume[2]);
4370
4371             for (int i = 0; i < 3; i++)
4372                 oldmusicvolume[i] = musicvolume[i];
4373         } else {
4374             pause_sound(leveltheme);
4375             pause_sound(stream_fighttheme);
4376             pause_sound(stream_menutheme);
4377
4378             for (int i = 0; i < 4; i++) {
4379                 oldmusicvolume[i] = 0;
4380                 musicvolume[i] = 0;
4381             }
4382         }
4383
4384         Hotspot::killhotspot = 2;
4385         for (unsigned i = 0; i < Hotspot::hotspots.size(); i++) {
4386             if (Hotspot::hotspots[i].type > 10 && Hotspot::hotspots[i].type < 20) {
4387                 if (Person::players[Hotspot::hotspots[i].type - 10]->dead == 0)
4388                     Hotspot::killhotspot = 0;
4389                 else if (Hotspot::killhotspot == 2)
4390                     Hotspot::killhotspot = 1;
4391             }
4392         }
4393         if (Hotspot::killhotspot == 2)
4394             Hotspot::killhotspot = 0;
4395
4396
4397         winhotspot = false;
4398         for (unsigned i = 0; i < Hotspot::hotspots.size(); i++) {
4399             if (Hotspot::hotspots[i].type == -1) {
4400                 if (distsq(&Person::players[0]->coords, &Hotspot::hotspots[i].position) < Hotspot::hotspots[i].size) {
4401                     winhotspot = true;
4402                 }
4403             }
4404         }
4405
4406         int numalarmed = 0;
4407         for (unsigned i = 1; i < Person::players.size(); i++) {
4408             if (!Person::players[i]->dead && Person::players[i]->aitype == attacktypecutoff && Person::players[i]->surprised <= 0) {
4409                 numalarmed++;
4410             }
4411         }
4412         if (numalarmed > maxalarmed) {
4413             maxalarmed = numalarmed;
4414         }
4415
4416         if (changedelay <= 0 && !loading && !editorenabled && gameon && !Tutorial::active && changedelay != -999 && !won) {
4417             if (Person::players[0]->dead) {
4418                 changedelay = 1;
4419                 targetlevel = whichlevel;
4420             }
4421             alldead = true;
4422             for (unsigned i = 1; i < Person::players.size(); i++) {
4423                 if (!Person::players[i]->dead && Person::players[i]->howactive < typedead1) {
4424                     alldead = false;
4425                     break;
4426                 }
4427             }
4428
4429
4430             if (alldead && !Person::players[0]->dead && maptype == mapkilleveryone) {
4431                 changedelay = 1;
4432                 targetlevel = whichlevel + 1;
4433                 if (targetlevel > numchallengelevels - 1)
4434                     targetlevel = 0;
4435             }
4436             if (winhotspot || windialogue) {
4437                 changedelay = 0.1;
4438                 targetlevel = whichlevel + 1;
4439                 if (targetlevel > numchallengelevels - 1)
4440                     targetlevel = 0;
4441             }
4442
4443
4444             if (Hotspot::killhotspot) {
4445                 changedelay = 1;
4446                 targetlevel = whichlevel + 1;
4447                 if (targetlevel > numchallengelevels - 1)
4448                     targetlevel = 0;
4449             }
4450
4451             if (changedelay > 0 && !Person::players[0]->dead && !won) {
4452                 //high scores, awards, win
4453                 if (campaign) {
4454                     Account::active().winCampaignLevel(whichchoice, bonustotal, leveltime);
4455                     scoreadded = 1;
4456                 } else {
4457                     wonleveltime = leveltime;
4458                     Account::active().winLevel(whichlevel, bonustotal - startbonustotal, leveltime);
4459                 }
4460                 won = 1;
4461                 Account::saveFile(Folders::getUserSavePath());
4462             }
4463         }
4464
4465         if (!winfreeze) {
4466
4467             if (leveltime < 1) {
4468                 loading = 0;
4469                 changedelay = .1;
4470                 alldead = false;
4471                 winhotspot = false;
4472                 Hotspot::killhotspot = 0;
4473             }
4474
4475             if (!editorenabled && gameon && !mainmenu) {
4476                 if (changedelay != -999)
4477                     changedelay -= multiplier / 7;
4478                 if (Person::players[0]->dead)
4479                     targetlevel = whichlevel;
4480                 if (loading == 2 && !campaign) {
4481                     flash();
4482
4483                     fireSound(firestartsound);
4484
4485                     if (!Person::players[0]->dead && targetlevel != whichlevel)
4486                         startbonustotal = bonustotal;
4487                     if (Person::players[0]->dead)
4488                         Loadlevel(whichlevel);
4489                     else
4490                         Loadlevel(targetlevel);
4491
4492                     fireSound();
4493
4494                     loading = 3;
4495                 }
4496                 if (loading == 2 && targetlevel == whichlevel) {
4497                     flash();
4498                     loadtime = 0;
4499
4500                     fireSound(firestartsound);
4501
4502                     Loadlevel(campaignlevels[Account::active().getCampaignChoicesMade()].mapname.c_str());
4503
4504                     fireSound();
4505
4506                     loading = 3;
4507                 }
4508                 if (changedelay <= -999 &&
4509                         whichlevel != -2 &&
4510                         !loading &&
4511                         (Person::players[0]->dead ||
4512                          (alldead && maptype == mapkilleveryone) ||
4513                          (winhotspot) ||
4514                          (Hotspot::killhotspot)))
4515                     loading = 1;
4516                 if ((Person::players[0]->dead ||
4517                         (alldead && maptype == mapkilleveryone) ||
4518                         (winhotspot) ||
4519                         (windialogue) ||
4520                         (Hotspot::killhotspot)) &&
4521                         changedelay <= 0) {
4522                     if (whichlevel != -2 && !loading && !Person::players[0]->dead) {
4523                         winfreeze = true;
4524                         changedelay = -999;
4525                     }
4526                     if (Person::players[0]->dead)
4527                         loading = 1;
4528                 }
4529             }
4530
4531             if (campaign) {
4532                 // campaignchoosenext determines what to do when the level is complete:
4533                 // 0 = load next level
4534                 // 1 = go back to level select screen
4535                 // 2 = stealthload next level
4536                 if (mainmenu == 0 && winfreeze && (campaignlevels[actuallevel].choosenext) == 1) {
4537                     if (campaignlevels[actuallevel].nextlevel.empty())
4538                         endgame = 1;
4539                 } else if (mainmenu == 0 && winfreeze) {
4540                     stealthloading = (campaignlevels[actuallevel].choosenext == 2);
4541
4542                     if (!stealthloading) {
4543                         fireSound(firestartsound);
4544
4545                         flash();
4546                     }
4547
4548                     startbonustotal = 0;
4549
4550                     LoadCampaign();
4551
4552                     loading = 2;
4553                     loadtime = 0;
4554                     targetlevel = 7;
4555                     if (!firstLoadDone) {
4556                         LoadStuff();
4557                     }
4558                     whichchoice = 0;
4559                     actuallevel = campaignlevels[actuallevel].nextlevel.front();
4560                     visibleloading = true;
4561                     stillloading = 1;
4562                     Loadlevel(campaignlevels[actuallevel].mapname.c_str());
4563                     campaign = 1;
4564                     mainmenu = 0;
4565                     gameon = 1;
4566                     pause_sound(stream_menutheme);
4567
4568                     stealthloading = 0;
4569                 }
4570             }
4571
4572             if (loading == 3)
4573                 loading = 0;
4574
4575         }
4576
4577         oldmusictype = musictype;
4578     }
4579
4580     facing = 0;
4581     facing.z = -1;
4582
4583     facing = DoRotation(facing, -pitch, 0, 0);
4584     facing = DoRotation(facing, 0, 0 - yaw, 0);
4585     viewerfacing = facing;
4586
4587     if (!cameramode) {
4588         if ((Animation::animations[Person::players[0]->animTarget].attack != 3 && Animation::animations[Person::players[0]->animCurrent].attack != 3) || Person::players[0]->skeleton.free)
4589             target = Person::players[0]->coords + Person::players[0]->currentoffset * (1 - Person::players[0]->target) * Person::players[0]->scale + Person::players[0]->targetoffset * Person::players[0]->target * Person::players[0]->scale - Person::players[0]->facing * .05;
4590         else
4591             target = Person::players[0]->oldcoords + Person::players[0]->currentoffset * (1 - Person::players[0]->target) * Person::players[0]->scale + Person::players[0]->targetoffset * Person::players[0]->target * Person::players[0]->scale - Person::players[0]->facing * .05;
4592         target.y += .1;
4593         if (Person::players[0]->skeleton.free) {
4594             for (unsigned i = 0; i < Person::players[0]->skeleton.joints.size(); i++) {
4595                 if (Person::players[0]->skeleton.joints[i].position.y * Person::players[0]->scale + Person::players[0]->coords.y > target.y)
4596                     target.y = Person::players[0]->skeleton.joints[i].position.y * Person::players[0]->scale + Person::players[0]->coords.y;
4597             }
4598             target.y += .1;
4599         }
4600         if (Person::players[0]->skeleton.free != 2) {
4601             cameraspeed = 20;
4602             if (findLengthfast(&Person::players[0]->velocity) > 400) {
4603                 cameraspeed = 20 + (findLength(&Person::players[0]->velocity) - 20) * .96;
4604             }
4605             if (Person::players[0]->skeleton.free == 0 && Person::players[0]->animTarget != hanganim && Person::players[0]->animTarget != climbanim)
4606                 target.y += 1.4;
4607             coltarget = target - cameraloc;
4608             if (findLengthfast(&coltarget) < multiplier * multiplier * 400)
4609                 cameraloc = target;
4610             else {
4611                 Normalise(&coltarget);
4612                 if (Person::players[0]->animTarget != hanganim && Person::players[0]->animTarget != climbanim && Person::players[0]->animCurrent != climbanim && Person::players[0]->currentoffset.x == 0)
4613                     cameraloc = cameraloc + coltarget * multiplier * cameraspeed;
4614                 else
4615                     cameraloc = cameraloc + coltarget * multiplier * 8;
4616             }
4617             if (editorenabled)
4618                 cameraloc = target;
4619             cameradist += multiplier * 5;
4620             if (cameradist > 2.3)
4621                 cameradist = 2.3;
4622             viewer = cameraloc - facing * cameradist;
4623             colviewer = viewer;
4624             coltarget = cameraloc;
4625             Object::SphereCheckPossible(&colviewer, findDistance(&colviewer, &coltarget));
4626             if (terrain.patchobjectnum[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz])
4627                 for (int j = 0; j < terrain.patchobjectnum[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz]; j++) {
4628                     int i = terrain.patchobjects[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz][j];
4629                     colviewer = viewer;
4630                     coltarget = cameraloc;
4631                     if (Object::objects[i]->model.LineCheckPossible(&colviewer, &coltarget, &col, &Object::objects[i]->position, &Object::objects[i]->yaw) != -1)
4632                         viewer = col;
4633                 }
4634             if (terrain.patchobjectnum[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz])
4635                 for (int j = 0; j < terrain.patchobjectnum[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz]; j++) {
4636                     int i = terrain.patchobjects[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz][j];
4637                     colviewer = viewer;
4638                     if (Object::objects[i]->model.SphereCheck(&colviewer, .15, &col, &Object::objects[i]->position, &Object::objects[i]->yaw) != -1) {
4639                         viewer = colviewer;
4640                     }
4641                 }
4642             cameradist = findDistance(&viewer, &target);
4643             viewer.y = max((double)viewer.y, terrain.getHeight(viewer.x, viewer.z) + .6);
4644             if (cameraloc.y < terrain.getHeight(cameraloc.x, cameraloc.z)) {
4645                 cameraloc.y = terrain.getHeight(cameraloc.x, cameraloc.z);
4646             }
4647         }
4648         if (camerashake > .8)
4649             camerashake = .8;
4650         woozy += multiplier;
4651         if (Person::players[0]->dead)
4652             camerashake = 0;
4653         if (Person::players[0]->dead)
4654             woozy = 0;
4655         camerashake -= multiplier * 2;
4656         blackout -= multiplier * 2;
4657         if (camerashake < 0)
4658             camerashake = 0;
4659         if (blackout < 0)
4660             blackout = 0;
4661         if (camerashake) {
4662             viewer.x += (float)(Random() % 100) * .0005 * camerashake;
4663             viewer.y += (float)(Random() % 100) * .0005 * camerashake;
4664             viewer.z += (float)(Random() % 100) * .0005 * camerashake;
4665         }
4666     }
4667 }
4668