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