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