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