]> git.jsancho.org Git - lugaru.git/blob - Source/GameTick.cpp
convert Game class to namespace
[lugaru.git] / Source / GameTick.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15 See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21
22 #if PLATFORM_UNIX
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #else
27 #include <direct.h>
28 #endif
29
30 #include <limits>
31 #include <ctime>
32 #include "Game.h"
33 #include "openal_wrapper.h"
34 #include "Settings.h"
35 #include "Input.h"
36 #include "Animation.h"
37 #include "Awards.h"
38
39 #include <algorithm>
40
41 using namespace std;
42 using namespace Game;
43
44 // Added more evilness needed for MSVC
45 #ifdef _MSC_VER
46         #define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n)
47         #define snprintf(buf, size, format, ...) _sprintf_p(buf, size, format)
48 #endif
49
50
51 extern float multiplier;
52 extern XYZ viewer;
53 extern int environment;
54 extern Terrain terrain;
55 extern float screenwidth,screenheight;
56 extern float gravity;
57 extern int detail;
58 extern float texdetail;
59 extern Objects objects;
60 extern int slomo;
61 extern float slomodelay;
62 extern bool floatjump;
63 extern float volume;
64 extern Light light;
65 extern float camerashake;
66 extern float woozy;
67 extern float blackout;
68 extern bool cellophane;
69 extern bool musictoggle;
70 extern int difficulty;
71 extern int bloodtoggle;
72 extern bool invertmouse;
73 extern float windvar;
74 extern float precipdelay;
75 extern XYZ viewerfacing;
76 extern bool ambientsound;
77 extern bool mousejump;
78 extern float viewdistance;
79 extern bool freeze;
80 extern bool keyboardfrozen;
81 extern bool loadingstuff;
82 extern XYZ windvector;
83 extern bool debugmode;
84 static int leveltheme;
85 extern int mainmenu;
86 extern bool visibleloading;
87 extern XYZ envsound[30];
88 extern float envsoundvol[30];
89 extern int numenvsounds;
90 extern float envsoundlife[30];
91 extern float usermousesensitivity;
92 extern bool ismotionblur;
93 extern bool showdamagebar; // (des)activate the damage bar
94 extern bool decals;
95 extern float tintr,tintg,tintb;
96 extern bool skyboxtexture;
97 extern float skyboxr;
98 extern float skyboxg;
99 extern float skyboxb;
100 extern float skyboxlightr;
101 extern float skyboxlightg;
102 extern float skyboxlightb;
103 extern float fadestart;
104 extern float slomospeed;
105 extern float slomofreq;
106 extern int tutoriallevel;
107 extern float smoketex;
108 extern float tutorialstagetime;
109 extern int tutorialstage;
110 extern float tutorialmaxtime;
111 extern float tutorialsuccess;
112 extern bool againbonus;
113 extern bool reversaltrain;
114 extern bool canattack;
115 extern bool cananger;
116 extern float damagedealt;
117 extern int maptype;
118 extern int editoractive;
119 extern int editorpathtype;
120
121 extern float hostiletime;
122
123 extern bool gamestarted;
124
125 extern int numhotspots;
126 extern int killhotspot;
127 extern XYZ hotspot[40];
128 extern int hotspottype[40];
129 extern float hotspotsize[40];
130 extern char hotspottext[40][256];
131 extern int currenthotspot;
132
133 extern int hostile;
134
135 extern bool stillloading;
136 extern bool winfreeze;
137
138 extern bool campaign;
139
140 static const char *rabbitskin[] = {
141 ":Data:Textures:Fur3.jpg",
142 ":Data:Textures:Fur.jpg",
143 ":Data:Textures:Fur2.jpg",
144 ":Data:Textures:Lynx.jpg",
145 ":Data:Textures:Otter.jpg",
146 ":Data:Textures:Opal.jpg",
147 ":Data:Textures:Sable.jpg",
148 ":Data:Textures:Chocolate.jpg",
149 ":Data:Textures:BW2.jpg",
150 ":Data:Textures:WB2.jpg"
151 };
152
153 static const char *wolfskin[] = {
154 ":Data:Textures:Wolf.jpg",
155 ":Data:Textures:Darkwolf.jpg",
156 ":Data:Textures:Snowwolf.jpg"
157 };
158
159 #define STATIC_ASSERT(x) extern int s_a_dummy[2 * (!!(x)) - 1];
160 STATIC_ASSERT (rabbittype == 0 && wolftype == 1)
161
162 static const char **creatureskin[] = {rabbitskin, wolfskin};
163
164 /* Return true if PFX is a prefix of STR (case-insensitive).  */
165 static bool stripfx(const char *str, const char *pfx)
166 {
167   return !strncasecmp(str, pfx, strlen(pfx));
168 }
169
170 static const char *cmd_names[] = {
171 #define DECLARE_COMMAND(cmd) #cmd,
172 #include "ConsoleCmds.h"
173 #undef  DECLARE_COMMAND
174 };
175
176 typedef void (*console_handler)(const char *args);
177
178 #define DECLARE_COMMAND(cmd) static void ch_##cmd(const char *args);
179 #include "ConsoleCmds.h"
180 #undef  DECLARE_COMMAND
181
182 static console_handler cmd_handlers[] = {
183 #define DECLARE_COMMAND(cmd) ch_##cmd,
184 #include "ConsoleCmds.h"
185 #undef  DECLARE_COMMAND
186 };
187
188
189
190 // added utility functions -sf17k =============================================================
191
192 //TODO: this is incorrect but I'm afraid to change it and break something,
193 //probably causes quirky behavior that I might want to preserve
194 inline float roughDirection(XYZ vec){
195     Normalise(&vec);
196     float angle=-asin(-vec.x)*180/M_PI;
197     if(vec.z<0)
198         angle=180-angle;
199     return angle;
200 }
201 inline float roughDirectionTo(XYZ start, XYZ end){
202     return roughDirection(end-start);
203 }
204
205 //TODO: gotta be a better way
206 inline float pitch(XYZ vec){
207     Normalise(&vec);
208     return -asin(vec.y)*180/M_PI;
209 }
210 inline float pitchTo(XYZ start, XYZ end){
211     return pitch(end-start);
212 }
213
214 inline float sq(float n) { return n*n; }
215
216 inline float stepTowardf(float from, float to, float by){
217     if(fabs(from-to)<by) return to;
218     else if(from>to) return from-by;
219     else return from+by;
220 }
221
222 void playdialogueboxsound(){
223     XYZ temppos;
224     temppos=player[participantfocus[whichdialogue][indialogue]].coords;
225     temppos=temppos-viewer;
226     Normalise(&temppos);
227     temppos+=viewer;
228
229     int sound=-1;
230     switch(dialogueboxsound[whichdialogue][indialogue]){
231         case -6: sound=alarmsound; break;
232         case -4: sound=consolefailsound; break;
233         case -3: sound=consolesuccesssound; break;
234         case -2: sound=firestartsound; break;
235         case -1: sound=fireendsound; break;
236         case 1: sound=rabbitchitter; break;
237         case 2: sound=rabbitchitter2; break;
238         case 3: sound=rabbitpainsound; break;
239         case 4: sound=rabbitpain1sound; break;
240         case 5: sound=rabbitattacksound; break;
241         case 6: sound=rabbitattack2sound; break;
242         case 7: sound=rabbitattack3sound; break;
243         case 8: sound=rabbitattack4sound; break;
244         case 9: sound=growlsound; break;
245         case 10: sound=growl2sound; break;
246         case 11: sound=snarlsound; break;
247         case 12: sound=snarl2sound; break;
248         case 13: sound=barksound; break;
249         case 14: sound=bark2sound; break;
250         case 15: sound=bark3sound; break;
251         case 16: sound=barkgrowlsound; break;
252         default: break;
253     }
254     if(sound!=-1)
255         emit_sound_at(sound, temppos);
256 }
257
258 // end added utility functions ================================================================
259
260
261
262 static void ch_quit(const char *args)
263 {
264   tryquit = 1;
265 }
266
267 static void ch_map(const char *args)
268 {
269   Loadlevel(args);
270   whichlevel = -2;
271   campaign = 0;
272 }
273
274 static void ch_save(const char *args){
275     char buf[64];
276     snprintf(buf, 63, ":Data:Maps:%s", args);
277
278     int mapvers = 12;
279
280     FILE *tfile;
281     tfile=fopen( ConvertFileName(buf), "wb" );
282     fpackf(tfile, "Bi", mapvers);
283     fpackf(tfile, "Bi", maptype);
284     fpackf(tfile, "Bi", hostile);
285     fpackf(tfile, "Bf Bf", viewdistance, fadestart);
286     fpackf(tfile, "Bb Bf Bf Bf", skyboxtexture, skyboxr, skyboxg, skyboxb);
287     fpackf(tfile, "Bf Bf Bf", skyboxlightr, skyboxlightg, skyboxlightb);
288     fpackf(tfile, "Bf Bf Bf Bf Bf Bi", player[0].coords.x, player[0].coords.y, player[0].coords.z,
289             player[0].rotation, player[0].targetrotation, player[0].num_weapons);
290     if(player[0].num_weapons>0&&player[0].num_weapons<5)
291         for(int j=0;j<player[0].num_weapons;j++)
292           fpackf(tfile, "Bi", weapons[player[0].weaponids[j]].getType());
293
294     fpackf(tfile, "Bf Bf Bf", player[0].armorhead, player[0].armorhigh, player[0].armorlow);
295     fpackf(tfile, "Bf Bf Bf", player[0].protectionhead, player[0].protectionhigh, player[0].protectionlow);
296     fpackf(tfile, "Bf Bf Bf", player[0].metalhead, player[0].metalhigh, player[0].metallow);
297     fpackf(tfile, "Bf Bf", player[0].power, player[0].speedmult);
298
299     fpackf(tfile, "Bi", player[0].numclothes);
300
301     fpackf(tfile, "Bi Bi", player[0].whichskin, player[0].creature);
302
303     fpackf(tfile, "Bi", numdialogues);
304
305         for(int k=0;k<numdialogues;k++){
306                 fpackf(tfile, "Bi", numdialogueboxes[k]);
307                 fpackf(tfile, "Bi", dialoguetype[k]);
308                 for(int l=0;l<10;l++){
309                         fpackf(tfile, "Bf Bf Bf", participantlocation[k][l].x, participantlocation[k][l].y, participantlocation[k][l].z);
310                         fpackf(tfile, "Bf", participantrotation[k][l]);
311                 }
312                 for(int l=0;l<numdialogueboxes[k];l++){
313                         fpackf(tfile, "Bi", dialogueboxlocation[k][l]);
314                         fpackf(tfile, "Bf", dialogueboxcolor[k][l][0]);
315                         fpackf(tfile, "Bf", dialogueboxcolor[k][l][1]);
316                         fpackf(tfile, "Bf", dialogueboxcolor[k][l][2]);
317                         fpackf(tfile, "Bi", dialogueboxsound[k][l]);
318
319                         int templength=strlen(dialoguetext[k][l]);
320                         fpackf(tfile, "Bi",(templength));
321                         for(int m=0;m<templength;m++){
322                                 fpackf(tfile, "Bb", dialoguetext[k][l][m]);
323                                 if(dialoguetext[k][l][m]=='\0')
324                     break;
325                         }
326
327                         templength=strlen(dialoguename[k][l]);
328                         fpackf(tfile, "Bi",templength);
329                         for(int m=0;m<templength;m++){
330                                 fpackf(tfile, "Bb", dialoguename[k][l][m]);
331                                 if(dialoguename[k][l][m]=='\0')
332                     break;
333                         }
334
335                         fpackf(tfile, "Bf Bf Bf", dialoguecamera[k][l].x, dialoguecamera[k][l].y, dialoguecamera[k][l].z);
336                         fpackf(tfile, "Bi", participantfocus[k][l]);
337                         fpackf(tfile, "Bi", participantaction[k][l]);
338
339                         for(int m=0;m<10;m++)
340                                 fpackf(tfile, "Bf Bf Bf", participantfacing[k][l][m].x, participantfacing[k][l][m].y, participantfacing[k][l][m].z);
341
342                         fpackf(tfile, "Bf Bf",dialoguecamerarotation[k][l],dialoguecamerarotation2[k][l]);
343                 }
344         }
345
346         for(int k=0;k<player[0].numclothes;k++){
347                 int templength=strlen(player[0].clothes[k]);
348                 fpackf(tfile, "Bi", templength);
349                 for(int l=0;l<templength;l++)
350                         fpackf(tfile, "Bb", player[0].clothes[k][l]);
351                 fpackf(tfile, "Bf Bf Bf", player[0].clothestintr[k], player[0].clothestintg[k], player[0].clothestintb[k]);
352         }
353
354     fpackf(tfile, "Bi", environment);
355
356     fpackf(tfile, "Bi", objects.numobjects);
357
358     for(int k=0;k<objects.numobjects;k++)
359         fpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", objects.type[k], objects.rotation[k], objects.rotation2[k],
360             objects.position[k].x, objects.position[k].y, objects.position[k].z, objects.scale[k]);
361
362     fpackf(tfile, "Bi", numhotspots);
363         for(int i=0;i<numhotspots;i++){
364                 fpackf(tfile, "Bi Bf Bf Bf Bf", hotspottype[i],hotspotsize[i],hotspot[i].x,hotspot[i].y,hotspot[i].z);
365                 int templength=strlen(hotspottext[i]);
366                 fpackf(tfile, "Bi",templength);
367                 for(int l=0;l<templength;l++)
368                         fpackf(tfile, "Bb", hotspottext[i][l]);
369         }
370
371     fpackf(tfile, "Bi", numplayers);
372     if(numplayers<maxplayers)
373         for(int j=1;j<numplayers;j++){
374             fpackf(tfile, "Bi Bi Bf Bf Bf Bi Bi Bf Bb Bf", player[j].whichskin, player[j].creature,
375                     player[j].coords.x, player[j].coords.y, player[j].coords.z,
376                     player[j].num_weapons, player[j].howactive, player[j].scale, player[j].immobile, player[j].rotation);
377             if(player[j].num_weapons<5)
378                 for(int k=0;k<player[j].num_weapons;k++)
379                     fpackf(tfile, "Bi", weapons[player[j].weaponids[k]].getType());
380             if(player[j].numwaypoints<30){
381                 fpackf(tfile, "Bi", player[j].numwaypoints);
382                 for(int k=0;k<player[j].numwaypoints;k++){
383                     fpackf(tfile, "Bf", player[j].waypoints[k].x);
384                     fpackf(tfile, "Bf", player[j].waypoints[k].y);
385                     fpackf(tfile, "Bf", player[j].waypoints[k].z);
386                     fpackf(tfile, "Bi", player[j].waypointtype[k]);
387                 }
388                 fpackf(tfile, "Bi", player[j].waypoint);
389             }else{
390                 player[j].numwaypoints=0;
391                 player[j].waypoint=0;
392                 fpackf(tfile, "Bi Bi Bi", player[j].numwaypoints, player[j].waypoint, player[j].waypoint);
393             }
394
395             fpackf(tfile, "Bf Bf Bf", player[j].armorhead, player[j].armorhigh, player[j].armorlow);
396             fpackf(tfile, "Bf Bf Bf", player[j].protectionhead, player[j].protectionhigh, player[j].protectionlow);
397             fpackf(tfile, "Bf Bf Bf", player[j].metalhead, player[j].metalhigh, player[j].metallow);
398             fpackf(tfile, "Bf Bf", player[j].power, player[j].speedmult);
399
400             float headprop, bodyprop, armprop, legprop;
401             if(player[j].creature==wolftype){
402                 headprop=player[j].proportionhead.x/1.1;
403                 bodyprop=player[j].proportionbody.x/1.1;
404                 armprop=player[j].proportionarms.x/1.1;
405                 legprop=player[j].proportionlegs.x/1.1;
406             }else if(player[j].creature==rabbittype){
407                 headprop=player[j].proportionhead.x/1.2;
408                 bodyprop=player[j].proportionbody.x/1.05;
409                 armprop=player[j].proportionarms.x/1.00;
410                 legprop=player[j].proportionlegs.x/1.1;
411             }
412
413             fpackf(tfile, "Bf Bf Bf Bf", headprop, bodyprop, armprop, legprop);
414
415             fpackf(tfile, "Bi", player[j].numclothes);
416             if(player[j].numclothes)
417                 for(int k=0;k<player[j].numclothes;k++){
418                     int templength;
419                     templength=strlen(player[j].clothes[k]);
420                     fpackf(tfile, "Bi", templength);
421                     for(int l=0;l<templength;l++)
422                         fpackf(tfile, "Bb", player[j].clothes[k][l]);
423                     fpackf(tfile, "Bf Bf Bf", player[j].clothestintr[k], player[j].clothestintg[k], player[j].clothestintb[k]);
424                 }
425         }
426
427     fpackf(tfile, "Bi", numpathpoints);
428         for(int j=0;j<numpathpoints;j++){
429                 fpackf(tfile, "Bf Bf Bf Bi", pathpoint[j].x, pathpoint[j].y, pathpoint[j].z, numpathpointconnect[j]);
430                 for(int k=0;k<numpathpointconnect[j];k++)
431                         fpackf(tfile, "Bi", pathpointconnect[j][k]);
432         }
433
434     fpackf(tfile, "Bf Bf Bf Bf", mapcenter.x, mapcenter.y, mapcenter.z, mapradius);
435
436     fclose(tfile);
437 }
438
439 static void ch_cellar(const char *args)
440 {
441   LoadTextureSave(":Data:Textures:Furdarko.jpg",&player[0].skeleton.drawmodel.textureptr,1,&player[0].skeleton.skinText[0],&player[0].skeleton.skinsize);
442 }
443
444 static void ch_tint(const char *args)
445 {
446   sscanf(args, "%f%f%f", &tintr, &tintg, &tintb);
447 }
448
449 static void ch_tintr(const char *args)
450 {
451   tintr = atof(args);
452 }
453
454 static void ch_tintg(const char *args)
455 {
456   tintg = atof(args);
457 }
458
459 static void ch_tintb(const char *args)
460 {
461   tintb = atof(args);
462 }
463
464 static void ch_speed(const char *args)
465 {
466   player[0].speedmult = atof(args);
467 }
468
469 static void ch_strength(const char *args)
470 {
471   player[0].power = atof(args);
472 }
473
474 static void ch_power(const char *args)
475 {
476   player[0].power = atof(args);
477 }
478
479 static void ch_size(const char *args)
480 {
481   player[0].scale = atof(args) * .2;
482 }
483
484 static int find_closest()
485 {
486   int closest = 0;
487   float closestdist = std::numeric_limits<float>::max();
488
489   for (int i = 1; i < numplayers; i++) {
490     float distance;
491     distance = findDistancefast(&player[i].coords,&player[0].coords);
492     if (distance < closestdist) {
493       closestdist = distance;
494       closest = i;
495     }
496   }
497   return closest;
498 }
499
500 static void ch_sizenear(const char *args)
501 {
502   int closest = find_closest();
503
504   if (closest)
505     player[closest].scale = atof(args) * .2;
506 }
507
508 static void set_proportion(int pnum, const char *args)
509 {
510   float headprop,bodyprop,armprop,legprop;
511
512   sscanf(args, "%f%f%f%f", &headprop, &bodyprop, &armprop, &legprop);
513
514   if(player[pnum].creature==wolftype){
515     player[pnum].proportionhead=1.1*headprop;
516     player[pnum].proportionbody=1.1*bodyprop;
517     player[pnum].proportionarms=1.1*armprop;
518     player[pnum].proportionlegs=1.1*legprop;
519   } else if(player[pnum].creature==rabbittype){
520     player[pnum].proportionhead=1.2*headprop;
521     player[pnum].proportionbody=1.05*bodyprop;
522     player[pnum].proportionarms=1.00*armprop;
523     player[pnum].proportionlegs=1.1*legprop;
524     player[pnum].proportionlegs.y=1.05*legprop;
525   }
526 }
527
528 static void ch_proportion(const char *args)
529 {
530   set_proportion(0, args);
531 }
532
533 static void ch_proportionnear(const char *args)
534 {
535   int closest = find_closest();
536   if (closest)
537     set_proportion(closest, args);
538 }
539
540 static void set_protection(int pnum, const char *args)
541 {
542   float head, high, low;
543   sscanf(args, "%f%f%f", &head, &high, &low);
544
545   player[pnum].protectionhead = head;
546   player[pnum].protectionhigh = high;
547   player[pnum].protectionlow  = low;
548 }
549
550 static void ch_protection(const char *args)
551 {
552   set_protection(0, args);
553 }
554
555 static void ch_protectionnear(const char *args)
556 {
557   int closest = find_closest();
558   if (closest)
559     set_protection(closest, args);
560 }
561
562 static void set_armor(int pnum, const char *args)
563 {
564   float head, high, low;
565   sscanf(args, "%f%f%f", &head, &high, &low);
566
567   player[pnum].armorhead = head;
568   player[pnum].armorhigh = high;
569   player[pnum].armorlow  = low;
570 }
571
572 static void ch_armor(const char *args)
573 {
574   set_armor(0, args);
575 }
576
577 static void ch_armornear(const char *args)
578 {
579   int closest = find_closest();
580   if (closest)
581     set_armor(closest, args);
582 }
583
584 static void ch_protectionreset(const char *args)
585 {
586   set_protection(0, "1 1 1");
587   set_armor(0, "1 1 1");
588 }
589
590 static void set_metal(int pnum, const char *args)
591 {
592   float head, high, low;
593   sscanf(args, "%f%f%f", &head, &high, &low);
594
595   player[pnum].metalhead = head;
596   player[pnum].metalhigh = high;
597   player[pnum].metallow  = low;
598 }
599
600 static void ch_metal(const char *args)
601 {
602   set_metal(0, args);
603 }
604
605 static void set_noclothes(int pnum, const char *args)
606 {
607   player[pnum].numclothes = 0;
608   LoadTextureSave(creatureskin[player[pnum].creature][player[pnum].whichskin],
609                         &player[pnum].skeleton.drawmodel.textureptr,1,
610                         &player[pnum].skeleton.skinText[0],&player[pnum].skeleton.skinsize);
611 }
612
613 static void ch_noclothes(const char *args)
614 {
615   set_noclothes(0, args);
616 }
617
618 static void ch_noclothesnear(const char *args)
619 {
620   int closest = find_closest();
621   if (closest)
622     set_noclothes(closest, args);
623 }
624
625
626 static void set_clothes(int pnum, const char *args)
627 {
628   char buf[64];
629   snprintf(buf, 63, ":Data:Textures:%s.png", args);
630
631   if (!AddClothes(buf,&player[pnum].skeleton.skinText[pnum]))
632     return;
633
634   player[pnum].DoMipmaps();
635   strcpy(player[pnum].clothes[player[pnum].numclothes],buf);
636   player[pnum].clothestintr[player[pnum].numclothes]=tintr;
637   player[pnum].clothestintg[player[pnum].numclothes]=tintg;
638   player[pnum].clothestintb[player[pnum].numclothes]=tintb;
639   player[pnum].numclothes++;
640 }
641
642 static void ch_clothes(const char *args)
643 {
644   set_clothes(0, args);
645 }
646
647 static void ch_clothesnear(const char *args)
648 {
649   int closest = find_closest();
650   if (closest)
651     set_clothes(closest, args);
652 }
653
654 static void ch_belt(const char *args)
655 {
656   player[0].skeleton.clothes = !player[0].skeleton.clothes;
657 }
658
659
660 static void ch_cellophane(const char *args)
661 {
662   cellophane = !cellophane;
663   float mul = cellophane ? 0 : 1;
664
665   for (int i = 0; i < numplayers; i++) {
666     player[i].proportionhead.z = player[i].proportionhead.x * mul;
667     player[i].proportionbody.z = player[i].proportionbody.x * mul;
668     player[i].proportionarms.z = player[i].proportionarms.x * mul;
669     player[i].proportionlegs.z = player[i].proportionlegs.x * mul;
670   }
671 }
672
673 static void ch_funnybunny(const char *args)
674 {
675   player[0].skeleton.id=0;
676   player[0].skeleton.Load(":Data:Skeleton:Basic Figure",":Data:Skeleton:Basic Figurelow",
677                           ":Data:Skeleton:Rabbitbelt",":Data:Models:Body.solid",
678                           ":Data:Models:Body2.solid",":Data:Models:Body3.solid",
679                           ":Data:Models:Body4.solid",":Data:Models:Body5.solid",
680                           ":Data:Models:Body6.solid",":Data:Models:Body7.solid",
681                           ":Data:Models:Bodylow.solid",":Data:Models:Belt.solid",1);
682   LoadTextureSave(":Data:Textures:fur3.jpg",&player[0].skeleton.drawmodel.textureptr,1,
683                         &player[0].skeleton.skinText[0],&player[0].skeleton.skinsize);
684   player[0].creature=rabbittype;
685   player[0].scale=.2;
686   player[0].headless=0;
687   player[0].damagetolerance=200;
688   set_proportion(0, "1 1 1 1");
689 }
690
691 static void ch_wolfie(const char *args)
692 {
693   player[0].skeleton.id=0;
694   player[0].skeleton.Load(":Data:Skeleton:Basic Figure Wolf",":Data:Skeleton:Basic Figure Wolf Low",
695                           ":Data:Skeleton:Rabbitbelt",":Data:Models:Wolf.solid",
696                           ":Data:Models:Wolf2.solid",":Data:Models:Wolf3.solid",
697                           ":Data:Models:Wolf4.solid",":Data:Models:Wolf5.solid",
698                           ":Data:Models:Wolf6.solid",":Data:Models:Wolf7.solid",
699                           ":Data:Models:Wolflow.solid",":Data:Models:Belt.solid",0);
700   LoadTextureSave(":Data:Textures:Wolf.jpg",&player[0].skeleton.drawmodel.textureptr,1,
701                         &player[0].skeleton.skinText[0],&player[0].skeleton.skinsize);
702   player[0].creature=wolftype;
703   player[0].damagetolerance=300;
704   set_proportion(0, "1 1 1 1");
705 }
706
707 static void ch_wolfieisgod(const char *args)
708 {
709   ch_wolfie(args);
710 }
711
712 static void ch_wolf(const char *args)
713 {
714   LoadTextureSave(":Data:Textures:Wolf.jpg",&player[0].skeleton.drawmodel.textureptr,1,
715                         &player[0].skeleton.skinText[0],&player[0].skeleton.skinsize);
716 }
717
718 static void ch_snowwolf(const char *args)
719 {
720   LoadTextureSave(":Data:Textures:SnowWolf.jpg",&player[0].skeleton.drawmodel.textureptr,1,
721                         &player[0].skeleton.skinText[0],&player[0].skeleton.skinsize);
722 }
723
724 static void ch_darkwolf(const char *args)
725 {
726   LoadTextureSave(":Data:Textures:DarkWolf.jpg",&player[0].skeleton.drawmodel.textureptr,1,
727                         &player[0].skeleton.skinText[0],&player[0].skeleton.skinsize);
728 }
729
730 static void ch_lizardwolf(const char *args)
731 {
732   LoadTextureSave(":Data:Textures:Lizardwolf.jpg",&player[0].skeleton.drawmodel.textureptr,1,
733                         &player[0].skeleton.skinText[0],&player[0].skeleton.skinsize);
734 }
735
736 static void ch_white(const char *args)
737 {
738   LoadTextureSave(":Data:Textures:fur.jpg",&player[0].skeleton.drawmodel.textureptr,1,
739                         &player[0].skeleton.skinText[0],&player[0].skeleton.skinsize);
740 }
741
742 static void ch_brown(const char *args)
743 {
744   LoadTextureSave(":Data:Textures:fur3.jpg",&player[0].skeleton.drawmodel.textureptr,1,
745                         &player[0].skeleton.skinText[0],&player[0].skeleton.skinsize);
746 }
747
748 static void ch_black(const char *args)
749 {
750   LoadTextureSave(":Data:Textures:fur2.jpg",&player[0].skeleton.drawmodel.textureptr,1,
751                         &player[0].skeleton.skinText[0],&player[0].skeleton.skinsize);
752 }
753
754 static void ch_sizemin(const char *args)
755 {
756   for (int i = 1; i < numplayers; i++)
757     if (player[i].scale < 0.8 * 0.2)
758       player[i].scale = 0.8 * 0.2;
759 }
760
761 static void ch_tutorial(const char *args)
762 {
763   tutoriallevel = atoi(args);
764 }
765
766 static void ch_hostile(const char *args)
767 {
768   hostile = atoi(args);
769 }
770
771 static void ch_indemo(const char *args)
772 {
773   indemo=1;
774   hotspot[numhotspots]=player[0].coords;
775   hotspotsize[numhotspots]=0;
776   hotspottype[numhotspots]=-111;
777   strcpy(hotspottext[numhotspots],"mapname");
778   numhotspots++;
779 }
780
781 static void ch_notindemo(const char *args)
782 {
783   indemo=0;
784   numhotspots--;
785 }
786
787 static void ch_type(const char *args)
788 {
789   int n = sizeof(editortypenames) / sizeof(editortypenames[0]);
790         for (int i = 0; i < n; i++)
791                 if (stripfx(args, editortypenames[i])) {
792                         editoractive = i;
793                         break;
794                 }
795 }
796
797 static void ch_path(const char *args)
798 {
799   int n = sizeof(pathtypenames) / sizeof(pathtypenames[0]);
800   for (int i = 0; i < n; i++)
801     if (stripfx(args, pathtypenames[i])) {
802                 editorpathtype = i;
803                 break;
804     }
805 }
806
807 static void ch_hs(const char *args)
808 {
809   hotspot[numhotspots]=player[0].coords;
810
811   float size;
812   int type, shift;
813   sscanf(args, "%f%d %n", &size, &type, &shift);
814
815   hotspotsize[numhotspots] = size;
816   hotspottype[numhotspots] = type;
817
818   strcpy(hotspottext[numhotspots], args + shift);
819   strcat(hotspottext[numhotspots], "\n");
820
821   numhotspots++;
822 }
823
824 static void ch_dialogue(const char *args)
825 {
826   int dlg;
827   char buf1[32], buf2[64];
828
829   sscanf(args, "%d %31s", &dlg, buf1);
830   snprintf(buf2, 63, ":Data:Dialogues:%s.txt", buf1);
831
832   dialoguetype[numdialogues] = dlg;
833
834   memset(dialoguetext[numdialogues], 0, sizeof(dialoguetext[numdialogues]));
835   memset(dialoguename[numdialogues], 0, sizeof(dialoguename[numdialogues]));
836
837   ifstream ipstream(ConvertFileName(buf2));
838   ipstream.ignore(256,':');
839   ipstream >> numdialogueboxes[numdialogues];
840   for(int i=0;i<numdialogueboxes[numdialogues];i++){
841     ipstream.ignore(256,':');
842     ipstream.ignore(256,':');
843     ipstream.ignore(256,' ');
844     ipstream >> dialogueboxlocation[numdialogues][i];
845     ipstream.ignore(256,':');
846     ipstream >> dialogueboxcolor[numdialogues][i][0];
847     ipstream >> dialogueboxcolor[numdialogues][i][1];
848     ipstream >> dialogueboxcolor[numdialogues][i][2];
849     ipstream.ignore(256,':');
850     ipstream.getline(dialoguename[numdialogues][i],64);
851     ipstream.ignore(256,':');
852     ipstream.ignore(256,' ');
853     ipstream.getline(dialoguetext[numdialogues][i],128);
854     for(int j=0;j<128;j++){
855       if(dialoguetext[numdialogues][i][j]=='\\')dialoguetext[numdialogues][i][j]='\n';
856     }
857     ipstream.ignore(256,':');
858     ipstream >> dialogueboxsound[numdialogues][i];
859   }
860
861   for(int i=0;i<numdialogueboxes[numdialogues];i++){
862     for(int j=0;j<numplayers;j++){
863       participantfacing[numdialogues][i][j]=player[j].facing;
864     }
865   }
866   ipstream.close();
867
868   directing=1;
869   indialogue=0;
870   whichdialogue=numdialogues;
871
872   numdialogues++;
873 }
874
875 static void ch_fixdialogue(const char *args)
876 {
877   char buf1[32], buf2[64];
878   int whichdi;
879
880   sscanf(args, "%d %31s", &whichdi, buf1);
881   snprintf(buf2, 63, ":Data:Dialogues:%s.txt", buf1);
882
883   memset(dialoguetext[whichdi], 0, sizeof(dialoguetext[whichdi]));
884   memset(dialoguename[whichdi], 0, sizeof(dialoguename[whichdi]));
885
886   ifstream ipstream(ConvertFileName(buf2));
887   ipstream.ignore(256,':');
888   ipstream >> numdialogueboxes[whichdi];
889   for(int i=0;i<numdialogueboxes[whichdi];i++){
890     ipstream.ignore(256,':');
891     ipstream.ignore(256,':');
892     ipstream.ignore(256,' ');
893     ipstream >> dialogueboxlocation[whichdi][i];
894     ipstream.ignore(256,':');
895     ipstream >> dialogueboxcolor[whichdi][i][0];
896     ipstream >> dialogueboxcolor[whichdi][i][1];
897     ipstream >> dialogueboxcolor[whichdi][i][2];
898     ipstream.ignore(256,':');
899     ipstream.getline(dialoguename[whichdi][i],64);
900     ipstream.ignore(256,':');
901     ipstream.ignore(256,' ');
902     ipstream.getline(dialoguetext[whichdi][i],128);
903     for(int j=0;j<128;j++){
904       if(dialoguetext[whichdi][i][j]=='\\')dialoguetext[whichdi][i][j]='\n';
905     }
906     ipstream.ignore(256,':');
907     ipstream >> dialogueboxsound[whichdi][i];
908   }
909
910   ipstream.close();
911 }
912
913 static void ch_fixtype(const char *args)
914 {
915   int dlg;
916   sscanf(args, "%d", &dlg);
917   dialoguetype[0] = dlg;
918 }
919
920 static void ch_fixrotation(const char *args)
921 {
922   participantrotation[whichdialogue][participantfocus[whichdialogue][indialogue]]=player[participantfocus[whichdialogue][indialogue]].rotation;
923 }
924
925 static void ch_ddialogue(const char *args)
926 {
927   if (numdialogues)
928     numdialogues--;
929 }
930
931 static void ch_dhs(const char *args)
932 {
933   if (numhotspots)
934     numhotspots--;
935 }
936
937 static void ch_immobile(const char *args)
938 {
939   player[0].immobile = 1;
940 }
941
942 static void ch_allimmobile(const char *args)
943 {
944   for (int i = 1; i < numplayers; i++)
945     player[i].immobile = 1;
946 }
947
948 static void ch_mobile(const char *args)
949 {
950   player[0].immobile = 0;
951 }
952
953 static void ch_default(const char *args)
954 {
955   player[0].armorhead=1;
956   player[0].armorhigh=1;
957   player[0].armorlow=1;
958   player[0].protectionhead=1;
959   player[0].protectionhigh=1;
960   player[0].protectionlow=1;
961   player[0].metalhead=1;
962   player[0].metalhigh=1;
963   player[0].metallow=1;
964   player[0].power=1;
965   player[0].speedmult=1;
966   player[0].scale=1;
967
968   if(player[0].creature==wolftype){
969     player[0].proportionhead=1.1;
970     player[0].proportionbody=1.1;
971     player[0].proportionarms=1.1;
972     player[0].proportionlegs=1.1;
973   } else if(player[0].creature==rabbittype){
974     player[0].proportionhead=1.2;
975     player[0].proportionbody=1.05;
976     player[0].proportionarms=1.00;
977     player[0].proportionlegs=1.1;
978     player[0].proportionlegs.y=1.05;
979   }
980
981   player[0].numclothes=0;
982   LoadTextureSave(creatureskin[player[0].creature][player[0].whichskin],
983                         &player[0].skeleton.drawmodel.textureptr,1,&player[0].skeleton.skinText[0],
984                         &player[0].skeleton.skinsize);
985
986   editoractive=typeactive;
987   player[0].immobile=0;
988 }
989
990 static void ch_play(const char *args)
991 {
992   int dlg;
993   sscanf(args, "%d", &dlg);
994   whichdialogue = dlg;
995
996   if (whichdialogue >= numdialogues)
997     return;
998
999   for(int i=0;i<numdialogueboxes[whichdialogue];i++){
1000     player[participantfocus[whichdialogue][i]].coords=participantlocation[whichdialogue][participantfocus[whichdialogue][i]];
1001     player[participantfocus[whichdialogue][i]].rotation=participantrotation[whichdialogue][participantfocus[whichdialogue][i]];
1002     player[participantfocus[whichdialogue][i]].targetrotation=participantrotation[whichdialogue][participantfocus[whichdialogue][i]];
1003     player[participantfocus[whichdialogue][i]].velocity=0;
1004     player[participantfocus[whichdialogue][i]].targetanimation=player[participantfocus[whichdialogue][i]].getIdle();
1005     player[participantfocus[whichdialogue][i]].targetframe=0;
1006   }
1007
1008   directing=0;
1009   indialogue=0;
1010
1011   playdialogueboxsound();
1012 }
1013
1014 static void ch_mapkilleveryone(const char *args)
1015 {
1016   maptype = mapkilleveryone;
1017 }
1018
1019 static void ch_mapkillmost(const char *args)
1020 {
1021   maptype = mapkillmost;
1022 }
1023
1024 static void ch_mapkillsomeone(const char *args)
1025 {
1026   maptype = mapkillsomeone;
1027 }
1028
1029 static void ch_mapgosomewhere(const char *args)
1030 {
1031   maptype = mapgosomewhere;
1032 }
1033
1034 static void ch_viewdistance(const char *args)
1035 {
1036   viewdistance = atof(args)*100;
1037 }
1038
1039 static void ch_fadestart(const char *args)
1040 {
1041   fadestart = atof(args);
1042 }
1043
1044 static void ch_slomo(const char *args)
1045 {
1046   slomospeed = atof(args);
1047   slomo = !slomo;
1048   slomodelay = 1000;
1049 }
1050
1051 static void ch_slofreq(const char *args)
1052 {
1053   slomofreq = atof(args);
1054 }
1055
1056 static void ch_skytint(const char *args)
1057 {
1058   sscanf(args, "%f%f%f", &skyboxr, &skyboxg, &skyboxb);
1059
1060   skyboxlightr=skyboxr;
1061   skyboxlightg=skyboxg;
1062   skyboxlightb=skyboxb;
1063
1064   SetUpLighting();
1065
1066   terrain.DoShadows();
1067   objects.DoShadows();
1068 }
1069
1070 static void ch_skylight(const char *args)
1071 {
1072   sscanf(args, "%f%f%f", &skyboxlightr, &skyboxlightg, &skyboxlightb);
1073
1074   SetUpLighting();
1075
1076   terrain.DoShadows();
1077   objects.DoShadows();
1078 }
1079
1080 static void ch_skybox(const char *args)
1081 {
1082   skyboxtexture = !skyboxtexture;
1083
1084   SetUpLighting();
1085
1086   terrain.DoShadows();
1087   objects.DoShadows();
1088 }
1089
1090 static void cmd_dispatch(const string cmd)
1091 {
1092   int i, n_cmds = sizeof(cmd_names) / sizeof(cmd_names[0]);
1093
1094   for (i = 0; i < n_cmds; i++)
1095     if (cmd.substr(0,cmd.find(' '))==string(cmd_names[i]))
1096       {
1097                   cout << "|" << cmd.substr(cmd.find(' ')+1) << "|" << endl;
1098                 cmd_handlers[i](cmd.substr(cmd.find(' ')+1).c_str());
1099                 break;
1100       }
1101   emit_sound_np(i < n_cmds ? consolesuccesssound : consolefailsound);
1102 }
1103
1104 /********************> Tick() <*****/
1105 extern bool save_image(const char * fname);
1106 void Screenshot (void)
1107 {
1108         char temp[1024];
1109         time_t  t = time(NULL);
1110         struct  tm *tme = localtime(&t);
1111         sprintf(temp, "Screenshots/Screenshot_%04d_%02d_%02d--%02d_%02d_%02d.png", tme->tm_year + 1900, tme->tm_mon + 1, tme->tm_mday, tme->tm_hour, tme->tm_min, tme->tm_sec);
1112
1113         #if defined(_WIN32)
1114         mkdir("Screenshots");
1115         #else
1116         mkdir("Screenshots", S_IRWXU);
1117         #endif
1118         
1119         save_image(temp);
1120 }
1121
1122 void Game::SetUpLighting(){
1123         if(environment==snowyenvironment)
1124         light.setColors(.65,.65,.7,.4,.4,.44);
1125         if(environment==desertenvironment)
1126         light.setColors(.95,.95,.95,.4,.35,.3);
1127         if(environment==grassyenvironment)
1128         light.setColors(.95,.95,1,.4,.4,.44);
1129         if(!skyboxtexture)
1130         light.setColors(1,1,1,.4,.4,.4);
1131         float average;
1132         average=(skyboxlightr+skyboxlightg+skyboxlightb)/3;
1133         light.color[0]*=(skyboxlightr+average)/2;
1134         light.color[1]*=(skyboxlightg+average)/2;
1135         light.color[2]*=(skyboxlightb+average)/2;
1136         light.ambient[0]*=(skyboxlightr+average)/2;
1137         light.ambient[1]*=(skyboxlightg+average)/2;
1138         light.ambient[2]*=(skyboxlightb+average)/2;
1139 }
1140
1141 int Game::findPathDist(int start,int end){
1142         int smallestcount,count,connected;
1143         int last,last2,last3,last4;
1144         int closest;
1145
1146         smallestcount=1000;
1147         for(int i=0;i<50;i++){
1148                 count=0;
1149                 last=start;
1150                 last2=-1;
1151                 last3=-1;
1152                 last4=-1;
1153                 while(last!=end&&count<30){
1154                         closest=-1;
1155                         for(int j=0;j<numpathpoints;j++){
1156                                 if(j!=last&&j!=last2&&j!=last3&&j!=last4)
1157                                 {
1158                                         connected=0;
1159                                         if(numpathpointconnect[j])
1160                                                 for(int k=0;k<numpathpointconnect[j];k++){
1161                                                         if(pathpointconnect[j][k]==last)connected=1;
1162                                                 }
1163                     if(!connected)
1164                         if(numpathpointconnect[last])
1165                             for(int k=0;k<numpathpointconnect[last];k++){
1166                                 if(pathpointconnect[last][k]==j)connected=1;
1167                             }
1168                     if(connected)
1169                         if(closest==-1||Random()%2==0){
1170                             closest=j;
1171                         }
1172                                 }
1173                         }
1174                         last4=last3;
1175                         last3=last2;
1176                         last2=last;
1177                         last=closest;
1178                         count++;
1179                 }
1180                 if(count<smallestcount)smallestcount=count;
1181         }
1182         return smallestcount;
1183 }
1184
1185 int Game::checkcollide(XYZ startpoint,XYZ endpoint){
1186         static XYZ colpoint,colviewer,coltarget;
1187         static float minx,minz,maxx,maxz,miny,maxy;
1188
1189     minx=min(startpoint.x,endpoint.x)-1;
1190     miny=min(startpoint.y,endpoint.y)-1;
1191     minz=min(startpoint.z,endpoint.z)-1;
1192     maxx=max(startpoint.x,endpoint.x)+1;
1193     maxy=max(startpoint.y,endpoint.y)+1;
1194     maxz=max(startpoint.z,endpoint.z)+1;
1195
1196         for(int i=0;i<objects.numobjects;i++){
1197                 if(     objects.position[i].x>minx-objects.model[i].boundingsphereradius&&
1198                 objects.position[i].x<maxx+objects.model[i].boundingsphereradius&&
1199                 objects.position[i].y>miny-objects.model[i].boundingsphereradius&&
1200                 objects.position[i].y<maxy+objects.model[i].boundingsphereradius&&
1201                 objects.position[i].z>minz-objects.model[i].boundingsphereradius&&
1202                 objects.position[i].z<maxz+objects.model[i].boundingsphereradius){
1203                         if(     objects.type[i]!=treeleavestype&&
1204                     objects.type[i]!=bushtype&&
1205                     objects.type[i]!=firetype){
1206                                 colviewer=startpoint;
1207                                 coltarget=endpoint;
1208                                 if(objects.model[i].LineCheck(&colviewer,&coltarget,&colpoint,&objects.position[i],&objects.rotation[i])!=-1)return i;
1209                         }
1210                 }
1211         }
1212
1213         //if(terrain.lineTerrain(startpoint,endpoint,&colpoint)!=-1)return 1000;
1214
1215         return -1;
1216 }
1217
1218 int Game::checkcollide(XYZ startpoint,XYZ endpoint,int what){
1219         static XYZ colpoint,colviewer,coltarget;
1220         static float minx,minz,maxx,maxz,miny,maxy;
1221         static int i; //FIXME: see below
1222
1223     minx=min(startpoint.x,endpoint.x)-1;
1224     miny=min(startpoint.y,endpoint.y)-1;
1225     minz=min(startpoint.z,endpoint.z)-1;
1226     maxx=max(startpoint.x,endpoint.x)+1;
1227     maxy=max(startpoint.y,endpoint.y)+1;
1228     maxz=max(startpoint.z,endpoint.z)+1;
1229
1230         if(what!=1000){
1231                 if(     objects.position[what].x>minx-objects.model[what].boundingsphereradius&&
1232                 objects.position[what].x<maxx+objects.model[what].boundingsphereradius&&
1233                 objects.position[what].y>miny-objects.model[what].boundingsphereradius&&
1234                 objects.position[what].y<maxy+objects.model[what].boundingsphereradius&&
1235                 objects.position[what].z>minz-objects.model[what].boundingsphereradius&&
1236                 objects.position[what].z<maxz+objects.model[what].boundingsphereradius){
1237                         if(     objects.type[what]!=treeleavestype&&
1238                     objects.type[what]!=bushtype&&
1239                     objects.type[what]!=firetype){
1240                                 colviewer=startpoint;
1241                                 coltarget=endpoint;
1242                 //FIXME: i/what
1243                                 if(objects.model[what].LineCheck(&colviewer,&coltarget,&colpoint,&objects.position[what],&objects.rotation[what])!=-1)return i;
1244                         }
1245                 }
1246         }
1247
1248         if(what==1000)if(terrain.lineTerrain(startpoint,endpoint,&colpoint)!=-1)return 1000;
1249
1250         return -1;
1251 }
1252
1253 void Game::Setenvironment(int which)
1254 {
1255         LOGFUNC;
1256
1257         LOG(" Setting environment...");
1258
1259         float temptexdetail;
1260         environment=which;
1261
1262         pause_sound(stream_snowtheme);
1263         pause_sound(stream_grasstheme);
1264         pause_sound(stream_deserttheme);
1265         pause_sound(stream_wind);
1266         pause_sound(stream_desertambient);
1267
1268
1269         if(environment==snowyenvironment){
1270                 windvector=0;
1271                 windvector.z=3;
1272                 if(ambientsound)
1273                   emit_stream_np(stream_wind);
1274
1275                 LoadTexture(":Data:Textures:snowtree.png",&objects.treetextureptr,0,1);
1276                 LoadTexture(":Data:Textures:bushsnow.png",&objects.bushtextureptr,0,1);
1277                 LoadTexture(":Data:Textures:bouldersnow.jpg",&objects.rocktextureptr,1,0);
1278                 LoadTexture(":Data:Textures:snowbox.jpg",&objects.boxtextureptr,1,0);
1279
1280                 footstepsound = footstepsn1;
1281                 footstepsound2 = footstepsn2;
1282                 footstepsound3 = footstepst1;
1283                 footstepsound4 = footstepst2;
1284
1285                 LoadTexture(":Data:Textures:snow.jpg",&terraintexture,1,0);
1286
1287                 LoadTexture(":Data:Textures:rock.jpg",&terraintexture2,1,0);
1288
1289                 //LoadTexture(":Data:Textures:detailgrain.png",&terraintexture3,1);
1290
1291
1292
1293
1294                 temptexdetail=texdetail;
1295                 if(texdetail>1)texdetail=4;
1296                 skybox.load(    ":Data:Textures:Skybox(snow):Front.jpg",
1297                         ":Data:Textures:Skybox(snow):Left.jpg",
1298                         ":Data:Textures:Skybox(snow):Back.jpg",
1299                         ":Data:Textures:Skybox(snow):Right.jpg",
1300                         ":Data:Textures:Skybox(snow):Up.jpg",
1301                         ":Data:Textures:Skybox(snow):Down.jpg");
1302
1303
1304
1305
1306                 texdetail=temptexdetail;
1307         } else if(environment==desertenvironment){
1308                 windvector=0;
1309                 windvector.z=2;
1310                 LoadTexture(":Data:Textures:deserttree.png",&objects.treetextureptr,0,1);
1311                 LoadTexture(":Data:Textures:bushdesert.png",&objects.bushtextureptr,0,1);
1312                 LoadTexture(":Data:Textures:boulderdesert.jpg",&objects.rocktextureptr,1,0);
1313                 LoadTexture(":Data:Textures:desertbox.jpg",&objects.boxtextureptr,1,0);
1314
1315
1316                 if(ambientsound)
1317                   emit_stream_np(stream_desertambient);
1318
1319                 footstepsound = footstepsn1;
1320                 footstepsound2 = footstepsn2;
1321                 footstepsound3 = footstepsn1;
1322                 footstepsound4 = footstepsn2;
1323
1324                 LoadTexture(":Data:Textures:sand.jpg",&terraintexture,1,0);
1325
1326                 LoadTexture(":Data:Textures:sandslope.jpg",&terraintexture2,1,0);
1327
1328                 //LoadTexture(":Data:Textures:detailgrain.png",&terraintexture3,1);
1329
1330
1331
1332                 temptexdetail=texdetail;
1333                 if(texdetail>1)texdetail=4;
1334                 skybox.load(    ":Data:Textures:Skybox(sand):Front.jpg",
1335                         ":Data:Textures:Skybox(sand):Left.jpg",
1336                         ":Data:Textures:Skybox(sand):Back.jpg",
1337                         ":Data:Textures:Skybox(sand):Right.jpg",
1338                         ":Data:Textures:Skybox(sand):Up.jpg",
1339                         ":Data:Textures:Skybox(sand):Down.jpg");
1340
1341
1342
1343
1344                 texdetail=temptexdetail;
1345         } else if(environment==grassyenvironment){
1346                 windvector=0;
1347                 windvector.z=2;
1348                 LoadTexture(":Data:Textures:tree.png",&objects.treetextureptr,0,1);
1349                 LoadTexture(":Data:Textures:bush.png",&objects.bushtextureptr,0,1);
1350                 LoadTexture(":Data:Textures:boulder.jpg",&objects.rocktextureptr,1,0);
1351                 LoadTexture(":Data:Textures:grassbox.jpg",&objects.boxtextureptr,1,0);
1352
1353                 if(ambientsound)
1354                   emit_stream_np(stream_wind, 100.);
1355
1356                 footstepsound = footstepgr1;
1357                 footstepsound2 = footstepgr2;
1358                 footstepsound3 = footstepst1;
1359                 footstepsound4 = footstepst2;
1360
1361                 LoadTexture(":Data:Textures:grassdirt.jpg",&terraintexture,1,0);
1362
1363                 LoadTexture(":Data:Textures:mossrock.jpg",&terraintexture2,1,0);
1364
1365                 //LoadTexture(":Data:Textures:detail.png",&terraintexture3,1);
1366
1367
1368
1369                 temptexdetail=texdetail;
1370                 if(texdetail>1)texdetail=4;
1371                 skybox.load(    ":Data:Textures:Skybox(grass):Front.jpg",
1372                         ":Data:Textures:Skybox(grass):Left.jpg",
1373                         ":Data:Textures:Skybox(grass):Back.jpg",
1374                         ":Data:Textures:Skybox(grass):Right.jpg",
1375                         ":Data:Textures:Skybox(grass):Up.jpg",
1376                         ":Data:Textures:Skybox(grass):Down.jpg");
1377
1378
1379
1380                 texdetail=temptexdetail;
1381         }
1382         temptexdetail=texdetail;
1383         texdetail=1;
1384         terrain.load(":Data:Textures:heightmap.png");
1385
1386         texdetail=temptexdetail;
1387 }
1388
1389 void Game::Loadlevel(int which) {
1390         stealthloading=0;
1391         whichlevel=which;
1392
1393         if(which == -1) {
1394             tutoriallevel = -1;
1395             Loadlevel("tutorial");
1396         } else if(which >= 0 && which <= 15) {
1397             char buf[32];
1398             snprintf(buf, 32, "map%d", which + 1); // challenges
1399             Loadlevel(buf);
1400         } else
1401             Loadlevel("mapsave");
1402 }
1403
1404 void Game::Loadlevel(const char *name) {
1405         int templength;
1406         float lamefloat;
1407         static const char *pfx = ":Data:Maps:";
1408         char *buf;
1409
1410         float headprop,legprop,armprop,bodyprop;
1411
1412         LOGFUNC;
1413
1414         LOG(std::string("Loading level...") + name);
1415
1416         if(!gameon)
1417         visibleloading=1;
1418         if(stealthloading)
1419         visibleloading=0;
1420         if(!stillloading)
1421         loadtime=0;
1422         gamestarted=1;
1423
1424         numenvsounds=0;
1425
1426         if(tutoriallevel!=-1)
1427         tutoriallevel=0;
1428         else
1429         tutoriallevel=1;
1430
1431         if(tutoriallevel==1)
1432         tutorialstage=0;
1433         if(tutorialstage==0) {
1434                 tutorialstagetime=0;
1435                 tutorialmaxtime=1;
1436         }
1437         loadingstuff=1;
1438         pause_sound(whooshsound);
1439         pause_sound(stream_firesound);
1440
1441         // Change the map filename into something that is os specific
1442         buf = (char*) alloca(strlen(pfx) + strlen(name) + 1);
1443         sprintf(buf, "%s%s", pfx, name);
1444         const char *FixedFN = ConvertFileName(buf);
1445
1446         int mapvers;
1447         FILE *tfile;
1448         //~ char* buff=getcwd(NULL,0);
1449         //~ cout << buff << " " << FixedFN << endl;
1450         //~ free(buff);
1451         tfile=fopen( FixedFN, "rb" );
1452         if(tfile) {
1453                 pause_sound(stream_firesound);
1454                 scoreadded=0;
1455                 windialogue=false;
1456                 hostiletime=0;
1457                 won=0;
1458
1459                 animation[bounceidleanim].Load((char *)"Idle",middleheight,neutral);
1460
1461                 numdialogues=0;
1462
1463                 for(int i=0;i<20;i++)
1464                         dialoguegonethrough[i]=0;
1465
1466                 indialogue=-1;
1467                 cameramode=0;
1468
1469                 damagedealt=0;
1470                 damagetaken=0;
1471
1472                 if(accountactive)
1473             difficulty=accountactive->getDifficulty();
1474
1475                 if(difficulty!=2)
1476             minimap=1;
1477                 else
1478             minimap=0;
1479
1480                 numhotspots=0;
1481                 currenthotspot=-1;
1482                 bonustime=1;
1483
1484                 skyboxtexture=1;
1485                 skyboxr=1;
1486                 skyboxg=1;
1487                 skyboxb=1;
1488
1489                 freeze=0;
1490                 winfreeze=0;
1491
1492                 for(int i=0;i<100;i++)
1493                         bonusnum[i]=0;
1494
1495                 numfalls=0;
1496                 numflipfail=0;
1497                 numseen=0;
1498                 numstaffattack=0;
1499                 numswordattack=0;
1500                 numknifeattack=0;
1501                 numunarmedattack=0;
1502                 numescaped=0;
1503                 numflipped=0;
1504                 numwallflipped=0;
1505                 numthrowkill=0;
1506                 numafterkill=0;
1507                 numreversals=0;
1508                 numattacks=0;
1509                 maxalarmed=0;
1510                 numresponded=0;
1511
1512                 bonustotal=startbonustotal;
1513                 bonus=0;
1514                 gameon=1;
1515                 changedelay=0;
1516                 if(console){
1517                         emit_sound_np(consolesuccesssound);
1518                         freeze=0;
1519                         console=false;
1520                 }
1521
1522                 if(!stealthloading){
1523                         terrain.numdecals=0;
1524                         Sprite::deleteSprites();
1525                         for(int i=0;i<objects.numobjects;i++)
1526                                 objects.model[i].numdecals=0;
1527
1528                         int j=objects.numobjects;
1529                         for(int i=0;i<j;i++){
1530                                 objects.DeleteObject(0);
1531                                 if(visibleloading)
1532                     LoadingScreen();
1533                         }
1534
1535                         for(int i=0;i<subdivision;i++)
1536                                 for(int j=0;j<subdivision;j++)
1537                                         terrain.patchobjectnum[i][j]=0;
1538                         if(visibleloading)
1539                 LoadingScreen();
1540                 }
1541
1542                 weapons.clear();
1543
1544                 funpackf(tfile, "Bi", &mapvers);
1545                 if(mapvers>=15)
1546             funpackf(tfile, "Bi", &indemo);
1547                 else
1548             indemo=0;
1549                 if(mapvers>=5)
1550             funpackf(tfile, "Bi", &maptype);
1551                 else
1552             maptype=mapkilleveryone;
1553                 if(mapvers>=6)
1554             funpackf(tfile, "Bi", &hostile);
1555                 else
1556             hostile=1;
1557                 if(mapvers>=4)
1558             funpackf(tfile, "Bf Bf", &viewdistance, &fadestart);
1559                 else{
1560                         viewdistance=100;
1561                         fadestart=.6;
1562                 }
1563                 if(mapvers>=2)
1564             funpackf(tfile, "Bb Bf Bf Bf", &skyboxtexture, &skyboxr, &skyboxg, &skyboxb);
1565                 else{
1566                         skyboxtexture=1;
1567                         skyboxr=1;
1568                         skyboxg=1;
1569                         skyboxb=1;
1570                 }
1571                 if(mapvers>=10)
1572             funpackf(tfile, "Bf Bf Bf", &skyboxlightr, &skyboxlightg, &skyboxlightb);
1573                 else{
1574                         skyboxlightr=skyboxr;
1575                         skyboxlightg=skyboxg;
1576                         skyboxlightb=skyboxb;
1577                 }
1578                 if(!stealthloading)
1579             funpackf(tfile, "Bf Bf Bf Bf Bf Bi", &player[0].coords.x,&player[0].coords.y,&player[0].coords.z,&player[0].rotation,&player[0].targetrotation, &player[0].num_weapons);
1580                 if(stealthloading)
1581             funpackf(tfile, "Bf Bf Bf Bf Bf Bi", &lamefloat,&lamefloat,&lamefloat,&lamefloat,&lamefloat, &player[0].num_weapons);
1582                 player[0].originalcoords=player[0].coords;
1583                 if(player[0].num_weapons>0&&player[0].num_weapons<5)
1584                         for(int j=0;j<player[0].num_weapons;j++){
1585                                 player[0].weaponids[j]=weapons.size();
1586                                 int type;
1587                                 funpackf(tfile, "Bi", &type);
1588                                 weapons.push_back(Weapon(type,0));
1589                         }
1590
1591                 if(visibleloading)
1592             LoadingScreen();
1593
1594                 funpackf(tfile, "Bf Bf Bf", &player[0].armorhead, &player[0].armorhigh, &player[0].armorlow);
1595                 funpackf(tfile, "Bf Bf Bf", &player[0].protectionhead, &player[0].protectionhigh, &player[0].protectionlow);
1596                 funpackf(tfile, "Bf Bf Bf", &player[0].metalhead, &player[0].metalhigh, &player[0].metallow);
1597                 funpackf(tfile, "Bf Bf", &player[0].power, &player[0].speedmult);
1598
1599                 funpackf(tfile, "Bi", &player[0].numclothes);
1600
1601                 if(mapvers>=9)
1602                         funpackf(tfile, "Bi Bi", &player[0].whichskin, &player[0].creature);
1603                 else{
1604                         player[0].whichskin=0;
1605                         player[0].creature=rabbittype;
1606                 }
1607
1608                 player[0].lastattack=-1;
1609                 player[0].lastattack2=-1;
1610                 player[0].lastattack3=-1;
1611
1612         //dialogues
1613                 if(mapvers>=8){
1614                         funpackf(tfile, "Bi", &numdialogues);
1615             for(int k=0;k<numdialogues;k++){
1616                 funpackf(tfile, "Bi", &numdialogueboxes[k]);
1617                 funpackf(tfile, "Bi", &dialoguetype[k]);
1618                 for(int l=0;l<10;l++){
1619                     funpackf(tfile, "Bf Bf Bf", &participantlocation[k][l].x, &participantlocation[k][l].y, &participantlocation[k][l].z);
1620                     funpackf(tfile, "Bf", &participantrotation[k][l]);
1621                 }
1622                 for(int l=0;l<numdialogueboxes[k];l++){
1623                     funpackf(tfile, "Bi", &dialogueboxlocation[k][l]);
1624                     funpackf(tfile, "Bf", &dialogueboxcolor[k][l][0]);
1625                     funpackf(tfile, "Bf", &dialogueboxcolor[k][l][1]);
1626                     funpackf(tfile, "Bf", &dialogueboxcolor[k][l][2]);
1627                     funpackf(tfile, "Bi", &dialogueboxsound[k][l]);
1628
1629                     funpackf(tfile, "Bi",&templength);
1630                     if(templength>128||templength<=0)
1631                         templength=128;
1632                     int m;
1633                     for(m=0;m<templength;m++){
1634                         funpackf(tfile, "Bb", &dialoguetext[k][l][m]);
1635                         if(dialoguetext[k][l][m]=='\0')
1636                             break;
1637                     }
1638                     dialoguetext[k][l][m] = 0;
1639
1640                     funpackf(tfile, "Bi",&templength);
1641                     if(templength>64||templength<=0)templength=64;
1642                     for(m=0;m<templength;m++){
1643                         funpackf(tfile, "Bb", &dialoguename[k][l][m]);
1644                         if(dialoguename[k][l][m]=='\0')
1645                             break;
1646                     }
1647                     dialoguename[k][l][m] = 0;
1648                     funpackf(tfile, "Bf Bf Bf", &dialoguecamera[k][l].x, &dialoguecamera[k][l].y, &dialoguecamera[k][l].z);
1649                     funpackf(tfile, "Bi", &participantfocus[k][l]);
1650                     funpackf(tfile, "Bi", &participantaction[k][l]);
1651
1652                     for(m=0;m<10;m++)
1653                         funpackf(tfile, "Bf Bf Bf", &participantfacing[k][l][m].x, &participantfacing[k][l][m].y, &participantfacing[k][l][m].z);
1654
1655                     funpackf(tfile, "Bf Bf",&dialoguecamerarotation[k][l],&dialoguecamerarotation2[k][l]);
1656                 }
1657             }
1658                 }else
1659             numdialogues=0;
1660
1661         for(int k=0;k<player[0].numclothes;k++){
1662             funpackf(tfile, "Bi", &templength);
1663             for(int l=0;l<templength;l++)
1664                 funpackf(tfile, "Bb", &player[0].clothes[k][l]);
1665             player[0].clothes[k][templength]='\0';
1666             funpackf(tfile, "Bf Bf Bf", &player[0].clothestintr[k], &player[0].clothestintg[k], &player[0].clothestintb[k]);
1667         }
1668
1669                 funpackf(tfile, "Bi", &environment);
1670
1671                 funpackf(tfile, "Bi", &objects.numobjects);
1672         for(int i=0;i<objects.numobjects;i++){
1673             funpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", &objects.type[i],&objects.rotation[i],&objects.rotation2[i], &objects.position[i].x, &objects.position[i].y, &objects.position[i].z,&objects.scale[i]);
1674             if(objects.type[i]==treeleavestype)
1675                 objects.scale[i]=objects.scale[i-1];
1676         }
1677
1678                 if(mapvers>=7){
1679                         funpackf(tfile, "Bi", &numhotspots);
1680             for(int i=0;i<numhotspots;i++){
1681                 funpackf(tfile, "Bi Bf Bf Bf Bf", &hotspottype[i],&hotspotsize[i],&hotspot[i].x,&hotspot[i].y,&hotspot[i].z);
1682                 funpackf(tfile, "Bi", &templength);
1683                 if(templength)
1684                     for(int l=0;l<templength;l++)
1685                         funpackf(tfile, "Bb", &hotspottext[i][l]);
1686                 hotspottext[i][templength]='\0';
1687                 if(hotspottype[i]==-111)
1688                     indemo=1;
1689             }
1690                 }else
1691             numhotspots=0;
1692
1693                 if(visibleloading)
1694             LoadingScreen();
1695
1696                 if(!stealthloading){
1697                         objects.center=0;
1698                         for(int i=0;i<objects.numobjects;i++)
1699                                 objects.center+=objects.position[i];
1700                         objects.center/=objects.numobjects;
1701
1702
1703                         if(visibleloading)
1704                 LoadingScreen();
1705
1706                         float maxdistance=0;
1707                         float tempdist;
1708                         //~ int whichclosest;
1709                         for(int i=0;i<objects.numobjects;i++){
1710                                 tempdist=findDistancefast(&objects.center,&objects.position[i]);
1711                                 if(tempdist>maxdistance){
1712                                         //~ whichclosest=i;
1713                                         maxdistance=tempdist;
1714                                 }
1715                         }
1716                         objects.radius=fast_sqrt(maxdistance);
1717                 }
1718
1719                 if(visibleloading)
1720             LoadingScreen();
1721                 //mapcenter=objects.center;
1722                 //mapradius=objects.radius;
1723
1724                 funpackf(tfile, "Bi", &numplayers);
1725                 int howmanyremoved=0;
1726                 bool removeanother=0;
1727                 if(numplayers>1&&numplayers<maxplayers){
1728                         for(int i=1;i<numplayers;i++){
1729                                 if(visibleloading)
1730                     LoadingScreen();
1731                                 removeanother=0;
1732
1733                                 funpackf(tfile, "Bi Bi Bf Bf Bf Bi",&player[i-howmanyremoved].whichskin,&player[i-howmanyremoved].creature, &player[i-howmanyremoved].coords.x,&player[i-howmanyremoved].coords.y,&player[i-howmanyremoved].coords.z,&player[i-howmanyremoved].num_weapons);
1734                                 if(mapvers>=5)
1735                     funpackf(tfile, "Bi", &player[i-howmanyremoved].howactive);
1736                                 else
1737                     player[i-howmanyremoved].howactive=typeactive;
1738                                 if(mapvers>=3)
1739                     funpackf(tfile, "Bf",&player[i-howmanyremoved].scale);
1740                                 else
1741                     player[i-howmanyremoved].scale=-1;
1742                                 if(mapvers>=11)
1743                     funpackf(tfile, "Bb",&player[i-howmanyremoved].immobile);
1744                                 else
1745                     player[i-howmanyremoved].immobile=0;
1746                                 if(mapvers>=12)
1747                     funpackf(tfile, "Bf",&player[i-howmanyremoved].rotation);
1748                                 else
1749                     player[i-howmanyremoved].rotation=0;
1750                                 player[i-howmanyremoved].targetrotation=player[i-howmanyremoved].rotation;
1751                                 if(player[i-howmanyremoved].num_weapons<0||player[i-howmanyremoved].num_weapons>5){
1752                                         removeanother=1;
1753                                         howmanyremoved++;
1754                                 }
1755                                 if(!removeanother){
1756                                         if(player[i-howmanyremoved].num_weapons>0&&player[i-howmanyremoved].num_weapons<5){
1757                                                 for(int j=0;j<player[i-howmanyremoved].num_weapons;j++){
1758                                                         player[i-howmanyremoved].weaponids[j]=weapons.size();
1759                                                         int type;
1760                                                         funpackf(tfile, "Bi", &type);
1761                                                         weapons.push_back(Weapon(type,i));
1762                                                 }
1763                                         }
1764                                         funpackf(tfile, "Bi", &player[i-howmanyremoved].numwaypoints);
1765                                         //player[i-howmanyremoved].numwaypoints=10;
1766                                         for(int j=0;j<player[i-howmanyremoved].numwaypoints;j++){
1767                                                 funpackf(tfile, "Bf", &player[i-howmanyremoved].waypoints[j].x);
1768                                                 funpackf(tfile, "Bf", &player[i-howmanyremoved].waypoints[j].y);
1769                                                 funpackf(tfile, "Bf", &player[i-howmanyremoved].waypoints[j].z);
1770                                                 if(mapvers>=5)
1771                             funpackf(tfile, "Bi", &player[i-howmanyremoved].waypointtype[j]);
1772                                                 else
1773                             player[i-howmanyremoved].waypointtype[j] = wpkeepwalking;
1774                                         }
1775
1776                                         funpackf(tfile, "Bi", &player[i-howmanyremoved].waypoint);
1777                                         if(player[i-howmanyremoved].waypoint>player[i-howmanyremoved].numwaypoints-1)
1778                         player[i-howmanyremoved].waypoint=0;
1779
1780                                         funpackf(tfile, "Bf Bf Bf", &player[i-howmanyremoved].armorhead, &player[i-howmanyremoved].armorhigh, &player[i-howmanyremoved].armorlow);
1781                                         funpackf(tfile, "Bf Bf Bf", &player[i-howmanyremoved].protectionhead, &player[i-howmanyremoved].protectionhigh, &player[i-howmanyremoved].protectionlow);
1782                                         funpackf(tfile, "Bf Bf Bf", &player[i-howmanyremoved].metalhead, &player[i-howmanyremoved].metalhigh, &player[i-howmanyremoved].metallow);
1783                                         funpackf(tfile, "Bf Bf", &player[i-howmanyremoved].power, &player[i-howmanyremoved].speedmult);
1784
1785                                         if(mapvers>=4)
1786                         funpackf(tfile, "Bf Bf Bf Bf", &headprop, &bodyprop, &armprop, &legprop);
1787                                         else{
1788                                                 headprop=1;
1789                                                 bodyprop=1;
1790                                                 armprop=1;
1791                                                 legprop=1;
1792                                         }
1793                                         if(player[i-howmanyremoved].creature==wolftype){
1794                                                 player[i-howmanyremoved].proportionhead=1.1*headprop;
1795                                                 player[i-howmanyremoved].proportionbody=1.1*bodyprop;
1796                                                 player[i-howmanyremoved].proportionarms=1.1*armprop;
1797                                                 player[i-howmanyremoved].proportionlegs=1.1*legprop;
1798                                         }
1799
1800                                         if(player[i-howmanyremoved].creature==rabbittype){
1801                                                 player[i-howmanyremoved].proportionhead=1.2*headprop;
1802                                                 player[i-howmanyremoved].proportionbody=1.05*bodyprop;
1803                                                 player[i-howmanyremoved].proportionarms=1.00*armprop;
1804                                                 player[i-howmanyremoved].proportionlegs=1.1*legprop;
1805                                                 player[i-howmanyremoved].proportionlegs.y=1.05*legprop;
1806                                         }
1807
1808                                         funpackf(tfile, "Bi", &player[i-howmanyremoved].numclothes);
1809                                         if(player[i-howmanyremoved].numclothes){
1810                                                 for(int k=0;k<player[i-howmanyremoved].numclothes;k++){
1811                                                         int templength;
1812                                                         funpackf(tfile, "Bi", &templength);
1813                                                         for(int l=0;l<templength;l++)
1814                                                                 funpackf(tfile, "Bb", &player[i-howmanyremoved].clothes[k][l]);
1815                                                         player[i-howmanyremoved].clothes[k][templength]='\0';
1816                                                         funpackf(tfile, "Bf Bf Bf", &player[i-howmanyremoved].clothestintr[k], &player[i-howmanyremoved].clothestintg[k], &player[i-howmanyremoved].clothestintb[k]);
1817                                                 }
1818                                         }
1819                                 }
1820                         }
1821                 }
1822                 if(visibleloading)
1823             LoadingScreen();
1824
1825                 numplayers-=howmanyremoved;
1826                 funpackf(tfile, "Bi", &numpathpoints);
1827                 if(numpathpoints>30||numpathpoints<0)
1828                         numpathpoints=0;
1829         for(int j=0;j<numpathpoints;j++){
1830             funpackf(tfile, "Bf Bf Bf Bi", &pathpoint[j].x,&pathpoint[j].y,&pathpoint[j].z,&numpathpointconnect[j]);
1831             for(int k=0;k<numpathpointconnect[j];k++){
1832                 funpackf(tfile, "Bi", &pathpointconnect[j][k]);
1833             }
1834         }
1835                 if(visibleloading)
1836             LoadingScreen();
1837
1838                 funpackf(tfile, "Bf Bf Bf Bf", &mapcenter.x,&mapcenter.y,&mapcenter.z,&mapradius);
1839
1840                 SetUpLighting();
1841                 if(environment!=oldenvironment)
1842             Setenvironment(environment);
1843                 oldenvironment=environment;
1844
1845                 if(!stealthloading){
1846                         int j=objects.numobjects;
1847                         objects.numobjects=0;
1848                         for(int i=0;i<j;i++){
1849                                 objects.MakeObject(objects.type[i],objects.position[i],objects.rotation[i],objects.rotation2[i],objects.scale[i]);
1850                                 if(visibleloading)
1851                     LoadingScreen();
1852                         }
1853
1854                         terrain.DoShadows();
1855                         if(visibleloading)
1856                 LoadingScreen();
1857                         objects.DoShadows();
1858                         if(visibleloading)
1859                 LoadingScreen();
1860                 }
1861
1862                 fclose(tfile);
1863
1864                 if(numplayers>maxplayers-1)
1865             numplayers=maxplayers-1;
1866                 for(int i=0;i<numplayers;i++){
1867                         if(visibleloading)
1868                 LoadingScreen();
1869                         player[i].burnt=0;
1870                         player[i].bled=0;
1871                         player[i].onfire=0;
1872                         if(i==0||player[i].scale<0)
1873                 player[i].scale=.2;
1874                         player[i].skeleton.free=0;
1875                         player[i].skeleton.id=i;
1876                         if(i==0&&mapvers<9)
1877                 player[i].creature=rabbittype;
1878                         if(player[i].creature!=wolftype){
1879                 player[i].skeleton.Load(
1880                     (char *)":Data:Skeleton:Basic Figure",
1881                     (char *)":Data:Skeleton:Basic Figurelow",
1882                     (char *)":Data:Skeleton:Rabbitbelt",
1883                     (char *)":Data:Models:Body.solid",
1884                     (char *)":Data:Models:Body2.solid",
1885                     (char *)":Data:Models:Body3.solid",
1886                     (char *)":Data:Models:Body4.solid",
1887                     (char *)":Data:Models:Body5.solid",
1888                     (char *)":Data:Models:Body6.solid",
1889                     (char *)":Data:Models:Body7.solid",
1890                     (char *)":Data:Models:Bodylow.solid",
1891                     (char *)":Data:Models:Belt.solid",0);
1892             }else{
1893                                 if(player[i].creature!=wolftype){
1894                                         player[i].skeleton.Load(
1895                             (char *)":Data:Skeleton:Basic Figure",
1896                             (char *)":Data:Skeleton:Basic Figurelow",
1897                             (char *)":Data:Skeleton:Rabbitbelt",
1898                             (char *)":Data:Models:Body.solid",
1899                             (char *)":Data:Models:Body2.solid",
1900                             (char *)":Data:Models:Body3.solid",
1901                             (char *)":Data:Models:Body4.solid",
1902                             (char *)":Data:Models:Body5.solid",
1903                             (char *)":Data:Models:Body6.solid",
1904                             (char *)":Data:Models:Body7.solid",
1905                             (char *)":Data:Models:Bodylow.solid",
1906                             (char *)":Data:Models:Belt.solid",1);
1907                                         LoadTexture(":Data:Textures:Belt.png",&player[i].skeleton.drawmodelclothes.textureptr,1,1);
1908                                 }
1909                                 if(player[i].creature==wolftype){
1910                                         player[i].skeleton.Load(
1911                             (char *)":Data:Skeleton:Basic Figure Wolf",
1912                             (char *)":Data:Skeleton:Basic Figure Wolf Low",
1913                             (char *)":Data:Skeleton:Rabbitbelt",
1914                             (char *)":Data:Models:Wolf.solid",
1915                             (char *)":Data:Models:Wolf2.solid",
1916                             (char *)":Data:Models:Wolf3.solid",
1917                             (char *)":Data:Models:Wolf4.solid",
1918                             (char *)":Data:Models:Wolf5.solid",
1919                             (char *)":Data:Models:Wolf6.solid",
1920                             (char *)":Data:Models:Wolf7.solid",
1921                             (char *)":Data:Models:Wolflow.solid",
1922                             (char *)":Data:Models:Belt.solid",0);
1923                                 }
1924                         }
1925
1926
1927                         //~ int texsize;
1928                         //~ texsize=512*512*3/texdetail/texdetail;
1929
1930                         LoadTextureSave(creatureskin[player[i].creature][player[i].whichskin],&player[i].skeleton.drawmodel.textureptr,1,&player[i].skeleton.skinText[0],&player[i].skeleton.skinsize);
1931
1932                         if(player[i].numclothes){
1933                                 for(int j=0;j<player[i].numclothes;j++){
1934                                         tintr=player[i].clothestintr[j];
1935                                         tintg=player[i].clothestintg[j];
1936                                         tintb=player[i].clothestintb[j];
1937                                         AddClothes((char *)player[i].clothes[j],&player[i].skeleton.skinText[0]);
1938                                 }
1939                                 player[i].DoMipmaps();
1940                         }
1941
1942                         player[i].currentanimation=bounceidleanim;
1943                         player[i].targetanimation=bounceidleanim;
1944                         player[i].currentframe=0;
1945                         player[i].targetframe=1;
1946                         player[i].target=0;
1947                         player[i].speed=1+(float)(Random()%100)/1000;
1948                         if(difficulty==0)
1949                 player[i].speed-=.2;
1950                         if(difficulty==1)
1951                 player[i].speed-=.1;
1952
1953                         player[i].velocity=0;
1954                         player[i].oldcoords=player[i].coords;
1955                         player[i].realoldcoords=player[i].coords;
1956
1957                         player[i].id=i;
1958                         player[i].skeleton.id=i;
1959                         player[i].updatedelay=0;
1960                         player[i].normalsupdatedelay=0;
1961
1962                         player[i].aitype=passivetype;
1963                         player[i].madskills=0;
1964
1965                         if(i==0){
1966                                 player[i].proportionhead=1.2;
1967                                 player[i].proportionbody=1.05;
1968                                 player[i].proportionarms=1.00;
1969                                 player[i].proportionlegs=1.1;
1970                                 player[i].proportionlegs.y=1.05;
1971                         }
1972                         player[i].headless=0;
1973                         player[i].currentoffset=0;
1974                         player[i].targetoffset=0;
1975
1976                         player[i].damagetolerance=200;
1977
1978                         if(player[i].creature==wolftype){
1979                                 if(i==0||player[i].scale<0)
1980                     player[i].scale=.23;
1981                                 player[i].damagetolerance=300;
1982                         }
1983
1984                         if(visibleloading)
1985                 LoadingScreen();
1986                         if(cellophane){
1987                                 player[i].proportionhead.z=0;
1988                                 player[i].proportionbody.z=0;
1989                                 player[i].proportionarms.z=0;
1990                                 player[i].proportionlegs.z=0;
1991                         }
1992
1993                         player[i].tempanimation.Load((char *)"Tempanim",0,0);
1994
1995                         player[i].headmorphness=0;
1996                         player[i].targetheadmorphness=1;
1997                         player[i].headmorphstart=0;
1998                         player[i].headmorphend=0;
1999
2000                         player[i].pausetime=0;
2001
2002                         player[i].dead=0;
2003                         player[i].jumppower=5;
2004                         player[i].damage=0;
2005                         player[i].permanentdamage=0;
2006                         player[i].superpermanentdamage=0;
2007
2008                         player[i].forwardkeydown=0;
2009                         player[i].leftkeydown=0;
2010                         player[i].backkeydown=0;
2011                         player[i].rightkeydown=0;
2012                         player[i].jumpkeydown=0;
2013                         player[i].crouchkeydown=0;
2014                         player[i].throwkeydown=0;
2015
2016                         player[i].collided=-10;
2017                         player[i].loaded=1;
2018                         player[i].bloodloss=0;
2019                         player[i].weaponactive=-1;
2020                         player[i].weaponstuck=-1;
2021                         player[i].bleeding=0;
2022                         player[i].deathbleeding=0;
2023                         player[i].stunned=0;
2024                         player[i].hasvictim=0;
2025                         player[i].wentforweapon=0;
2026                 }
2027
2028                 player[0].aitype=playercontrolled;
2029                 player[0].weaponactive=-1;
2030
2031                 if(difficulty==1)
2032                         player[0].power=1/.9;
2033
2034                 if(difficulty==0)
2035                         player[0].power=1/.8;
2036
2037                 if(difficulty==1)
2038             player[0].damagetolerance=250;
2039                 if(difficulty==0)
2040             player[0].damagetolerance=300;
2041                 if(difficulty==0)
2042             player[0].armorhead*=1.5;
2043                 if(difficulty==0)
2044             player[0].armorhigh*=1.5;
2045                 if(difficulty==0)
2046             player[0].armorlow*=1.5;
2047                 cameraloc=player[0].coords;
2048                 cameraloc.y+=5;
2049                 rotation=player[0].rotation;
2050
2051                 hawkcoords=player[0].coords;
2052                 hawkcoords.y+=30;
2053
2054                 if(visibleloading)
2055             LoadingScreen();
2056                 //~ for(int i=0;i<weapons.size();i++){
2057                 //~ }
2058                 
2059                 LOG("Starting background music...");
2060
2061                 OPENAL_StopSound(OPENAL_ALL);
2062                 if(environment==snowyenvironment){
2063                         if(ambientsound)
2064                           emit_stream_np(stream_wind);
2065                 }else if(environment==desertenvironment){
2066                         if(ambientsound)
2067                           emit_stream_np(stream_desertambient);
2068                 }else if(environment==grassyenvironment){
2069                         if(ambientsound)
2070                           emit_stream_np(stream_wind, 100.);
2071                 }
2072                 oldmusicvolume[0]=0;
2073                 oldmusicvolume[1]=0;
2074                 oldmusicvolume[2]=0;
2075                 oldmusicvolume[3]=0;
2076
2077                 if(!firstload)
2078                         firstload=1;
2079         } else {
2080                 perror("Problem");
2081         }
2082         leveltime=0;
2083         loadingstuff=0;
2084         visibleloading=0;
2085 }
2086
2087 void Game::doTutorial(){
2088     if(tutorialstagetime>tutorialmaxtime){
2089         tutorialstage++;
2090         tutorialsuccess=0;
2091         if(tutorialstage<=1){
2092             canattack=0;
2093             cananger=0;
2094             reversaltrain=0;
2095         }
2096         switch(tutorialstage){
2097             case 1:
2098                 tutorialmaxtime=5;
2099             break; case 2:
2100                 tutorialmaxtime=2;
2101             break; case 3:
2102                 tutorialmaxtime=600;
2103             break; case 4:
2104                 tutorialmaxtime=1000;
2105             break; case 5:
2106                 tutorialmaxtime=600;
2107             break; case 6:
2108                 tutorialmaxtime=600;
2109             break; case 7:
2110                 tutorialmaxtime=600;
2111             break; case 8:
2112                 tutorialmaxtime=600;
2113             break; case 9:
2114                 tutorialmaxtime=600;
2115             break; case 10:
2116                 tutorialmaxtime=2;
2117             break; case 11:
2118                 tutorialmaxtime=1000;
2119             break; case 12:
2120                 tutorialmaxtime=1000;
2121             break; case 13:
2122                 tutorialmaxtime=2;
2123             break; case 14: {
2124                 tutorialmaxtime=3;
2125
2126                 XYZ temp,temp2;
2127
2128                 temp.x=1011;
2129                 temp.y=84;
2130                 temp.z=491;
2131                 temp2.x=1025;
2132                 temp2.y=75;
2133                 temp2.z=447;
2134
2135                 player[1].coords=(temp+temp2)/2;
2136
2137                 emit_sound_at(fireendsound, player[1].coords);
2138
2139                 for(int i=0;i<player[1].skeleton.num_joints;i++){
2140                     if(Random()%2==0){
2141                         if(!player[1].skeleton.free)temp2=(player[1].coords-player[1].oldcoords)/multiplier/2;//velocity/2;
2142                         if(player[1].skeleton.free)temp2=player[1].skeleton.joints[i].velocity*player[1].scale/2;
2143                         if(!player[1].skeleton.free)temp=DoRotation(DoRotation(DoRotation(player[1].skeleton.joints[i].position,0,0,player[1].tilt),player[1].tilt2,0,0),0,player[1].rotation,0)*player[1].scale+player[1].coords;
2144                         if(player[1].skeleton.free)temp=player[1].skeleton.joints[i].position*player[1].scale+player[1].coords;
2145                         Sprite::MakeSprite(breathsprite, temp,temp2, 1,1,1, .6+(float)abs(Random()%100)/200-.25, 1);
2146                     }
2147                 }
2148             }
2149             break; case 15:
2150                 tutorialmaxtime=500;
2151             break; case 16:
2152                 tutorialmaxtime=500;
2153             break; case 17:
2154                 tutorialmaxtime=500;
2155             break; case 18:
2156                 tutorialmaxtime=500;
2157             break; case 19:
2158                 tutorialstage=20;
2159                 //tutorialmaxtime=500;
2160             break; case 20:
2161                 tutorialmaxtime=500;
2162             break; case 21:
2163                 tutorialmaxtime=500;
2164                 if(bonus==cannon){
2165                     bonus=Slicebonus;
2166                     againbonus=1;
2167                 }
2168                 else againbonus=0;
2169             break; case 22:
2170                 tutorialmaxtime=500;
2171             break; case 23:
2172                 tutorialmaxtime=500;
2173             break; case 24:
2174                 tutorialmaxtime=500;
2175             break; case 25:
2176                 tutorialmaxtime=500;
2177             break; case 26:
2178                 tutorialmaxtime=2;
2179             break; case 27:
2180                 tutorialmaxtime=4;
2181                 reversaltrain=1;
2182                 cananger=1;
2183                 player[1].aitype=attacktypecutoff;
2184             break; case 28:
2185                 tutorialmaxtime=400;
2186             break; case 29:
2187                 tutorialmaxtime=400;
2188                 player[0].escapednum=0;
2189             break; case 30:
2190                 tutorialmaxtime=4;
2191                 reversaltrain=0;
2192                 cananger=0;
2193                 player[1].aitype=passivetype;
2194             break; case 31:
2195                 tutorialmaxtime=13;
2196             break; case 32:
2197                 tutorialmaxtime=8;
2198             break; case 33:
2199                 tutorialmaxtime=400;
2200                 cananger=1;
2201                 canattack=1;
2202                 player[1].aitype=attacktypecutoff;
2203             break; case 34:
2204                 tutorialmaxtime=400;
2205             break; case 35:
2206                 tutorialmaxtime=400;
2207             break; case 36:
2208                 tutorialmaxtime=2;
2209                 reversaltrain=0;
2210                 cananger=0;
2211                 player[1].aitype=passivetype;
2212             break; case 37:
2213                 damagedealt=0;
2214                 damagetaken=0;
2215                 tutorialmaxtime=50;
2216                 cananger=1;
2217                 canattack=1;
2218                 player[1].aitype=attacktypecutoff;
2219             break; case 38:
2220                 tutorialmaxtime=4;
2221                 canattack=0;
2222                 cananger=0;
2223                 player[1].aitype=passivetype;
2224             break; case 39: {
2225                 XYZ temp,temp2;
2226
2227                 temp.x=1011;
2228                 temp.y=84;
2229                 temp.z=491;
2230                 temp2.x=1025;
2231                 temp2.y=75;
2232                 temp2.z=447;
2233
2234                                 Weapon w(knife,-1);
2235                 w.position=(temp+temp2)/2;
2236                 w.tippoint=(temp+temp2)/2;
2237
2238                 w.velocity=0.1;
2239                 w.tipvelocity=0.1;
2240                 w.missed=1;
2241                 w.hitsomething=0;
2242                 w.freetime=0;
2243                 w.firstfree=1;
2244                 w.physics=1;
2245
2246                 weapons.push_back(w);
2247             }
2248             break; case 40:
2249                 tutorialmaxtime=300;
2250             break; case 41:
2251                 tutorialmaxtime=300;
2252             break; case 42:
2253                 tutorialmaxtime=8;
2254             break; case 43:
2255                 tutorialmaxtime=300;
2256             break; case 44:
2257                 weapons[0].owner=1;
2258                 player[0].weaponactive=-1;
2259                 player[0].num_weapons=0;
2260                 player[1].weaponactive=0;
2261                 player[1].num_weapons=1;
2262                 player[1].weaponids[0]=0;
2263
2264                 cananger=1;
2265                 canattack=1;
2266                 player[1].aitype=attacktypecutoff;
2267
2268                 tutorialmaxtime=300;
2269             break; case 45:
2270                 weapons[0].owner=1;
2271                 player[0].weaponactive=-1;
2272                 player[0].num_weapons=0;
2273                 player[1].weaponactive=0;
2274                 player[1].num_weapons=1;
2275                 player[1].weaponids[0]=0;
2276
2277                 tutorialmaxtime=300;
2278             break; case 46:
2279                 weapons[0].owner=1;
2280                 player[0].weaponactive=-1;
2281                 player[0].num_weapons=0;
2282                 player[1].weaponactive=0;
2283                 player[1].num_weapons=1;
2284                 player[1].weaponids[0]=0;
2285
2286                 weapons[0].setType(sword);
2287
2288                 tutorialmaxtime=300;
2289             break; case 47: {
2290                 tutorialmaxtime=10;
2291
2292                 XYZ temp,temp2;
2293
2294                 temp.x=1011;
2295                 temp.y=84;
2296                 temp.z=491;
2297                 temp2.x=1025;
2298                 temp2.y=75;
2299                 temp2.z=447;
2300
2301                                 Weapon w(sword,-1);
2302                 w.position=(temp+temp2)/2;
2303                 w.tippoint=(temp+temp2)/2;
2304
2305                 w.velocity=0.1;
2306                 w.tipvelocity=0.1;
2307                 w.missed=1;
2308                 w.hitsomething=0;
2309                 w.freetime=0;
2310                 w.firstfree=1;
2311                 w.physics=1;
2312                 
2313                 weapons.push_back(w);
2314
2315                 weapons[0].owner=1;
2316                 weapons[1].owner=0;
2317                 player[0].weaponactive=0;
2318                 player[0].num_weapons=1;
2319                 player[0].weaponids[0]=1;
2320                 player[1].weaponactive=0;
2321                 player[1].num_weapons=1;
2322                 player[1].weaponids[0]=0;
2323
2324             }
2325             break; case 48:
2326                 canattack=0;
2327                 cananger=0;
2328                 player[1].aitype=passivetype;
2329
2330                 tutorialmaxtime=15;
2331
2332                 weapons[0].owner=1;
2333                 weapons[1].owner=0;
2334                 player[0].weaponactive=0;
2335                 player[0].num_weapons=1;
2336                 player[0].weaponids[0]=1;
2337                 player[1].weaponactive=0;
2338                 player[1].num_weapons=1;
2339                 player[1].weaponids[0]=0;
2340
2341                 if(player[0].weaponactive!=-1)
2342                                         weapons[player[0].weaponids[player[0].weaponactive]].setType(staff);
2343                 else 
2344                                         weapons[0].setType(staff);
2345             break; case 49:
2346                 canattack=0;
2347                 cananger=0;
2348                 player[1].aitype=passivetype;
2349
2350                 tutorialmaxtime=200;
2351
2352                 weapons[1].position=1000;
2353                 weapons[1].tippoint=1000;
2354
2355                 weapons[0].setType(knife);
2356
2357                 weapons[0].owner=0;
2358                 player[1].weaponactive=-1;
2359                 player[1].num_weapons=0;
2360                 player[0].weaponactive=0;
2361                 player[0].num_weapons=1;
2362                 player[0].weaponids[0]=0;
2363
2364             break; case 50: {
2365                 tutorialmaxtime=8;
2366
2367                 XYZ temp,temp2;
2368                 emit_sound_at(fireendsound, player[1].coords);
2369
2370                 for(int i=0;i<player[1].skeleton.num_joints;i++){
2371                     if(Random()%2==0){
2372                         if(!player[1].skeleton.free)temp2=(player[1].coords-player[1].oldcoords)/multiplier/2;//velocity/2;
2373                         if(player[1].skeleton.free)temp2=player[1].skeleton.joints[i].velocity*player[1].scale/2;
2374                         if(!player[1].skeleton.free)temp=DoRotation(DoRotation(DoRotation(player[1].skeleton.joints[i].position,0,0,player[1].tilt),player[1].tilt2,0,0),0,player[1].rotation,0)*player[1].scale+player[1].coords;
2375                         if(player[1].skeleton.free)temp=player[1].skeleton.joints[i].position*player[1].scale+player[1].coords;
2376                         Sprite::MakeSprite(breathsprite, temp,temp2, 1,1,1, .6+(float)abs(Random()%100)/200-.25, 1);
2377                     }
2378                 }
2379
2380                 player[1].num_weapons=0;
2381                 player[1].weaponstuck=-1;
2382                 player[1].weaponactive=-1;
2383
2384                 weapons.clear();
2385             }
2386             break; case 51:
2387                 tutorialmaxtime=80000;
2388             break; default: break;
2389         }
2390         if(tutorialstage<=51)tutorialstagetime=0;
2391     }
2392
2393     //Tutorial success
2394     if(tutorialstagetime<tutorialmaxtime-3){
2395         switch(tutorialstage){
2396             case 3: if(deltah||deltav)tutorialsuccess+=multiplier;
2397             break; case 4: if(player[0].forwardkeydown||player[0].backkeydown||player[0].leftkeydown||player[0].rightkeydown)tutorialsuccess+=multiplier;
2398             break; case 5: if(player[0].jumpkeydown)tutorialsuccess=1;
2399             break; case 6: if(player[0].isCrouch())tutorialsuccess=1;
2400             break; case 7: if(player[0].targetanimation==rollanim)tutorialsuccess=1;
2401             break; case 8: if(player[0].targetanimation==sneakanim)tutorialsuccess+=multiplier;
2402             break; case 9: if(player[0].targetanimation==rabbitrunninganim||player[0].targetanimation==wolfrunninganim)tutorialsuccess+=multiplier;
2403             break; case 11: if(player[0].isWallJump())tutorialsuccess=1;
2404             break; case 12: if(player[0].targetanimation==flipanim)tutorialsuccess=1;
2405             break; case 15: if(player[0].targetanimation==upunchanim||player[0].targetanimation==winduppunchanim)tutorialsuccess=1;
2406             break; case 16: if(player[0].targetanimation==winduppunchanim)tutorialsuccess=1;
2407             break; case 17: if(player[0].targetanimation==spinkickanim)tutorialsuccess=1;
2408             break; case 18: if(player[0].targetanimation==sweepanim)tutorialsuccess=1;
2409             break; case 19: if(player[0].targetanimation==dropkickanim)tutorialsuccess=1;
2410             break; case 20: if(player[0].targetanimation==rabbitkickanim)tutorialsuccess=1;
2411             break; case 21: if(bonus==cannon)tutorialsuccess=1;
2412             break; case 22: if(bonus==spinecrusher)tutorialsuccess=1;
2413             break; case 23: if(player[0].targetanimation==walljumprightkickanim||player[0].targetanimation==walljumpleftkickanim)tutorialsuccess=1;
2414             break; case 24: if(player[0].targetanimation==rabbittacklinganim)tutorialsuccess=1;
2415             break; case 25: if(player[0].targetanimation==backhandspringanim)tutorialsuccess=1;
2416             break; case 28: if(animation[player[0].targetanimation].attack==reversed&&player[0].feint)tutorialsuccess=1;
2417             break; case 29:
2418                 if(player[0].escapednum==2) {
2419                     tutorialsuccess=1;
2420                     reversaltrain=0;
2421                     cananger=0;
2422                     player[1].aitype=passivetype;
2423                 }
2424             break; case 33: if(animation[player[0].targetanimation].attack==reversal)tutorialsuccess=1;
2425             break; case 34: if(animation[player[0].targetanimation].attack==reversal)tutorialsuccess=1;
2426             break; case 35:
2427                 if(animation[player[0].targetanimation].attack==reversal){
2428                     tutorialsuccess=1;
2429                     reversaltrain=0;
2430                     cananger=0;
2431                     player[1].aitype=passivetype;
2432                 }
2433             break; case 40: if(player[0].num_weapons>0)tutorialsuccess=1;
2434             break; case 41: if(player[0].weaponactive==-1&&player[0].num_weapons>0)tutorialsuccess=1;
2435             break; case 43: if(player[0].targetanimation==knifeslashstartanim)tutorialsuccess=1;
2436             break; case 44: if(animation[player[0].targetanimation].attack==reversal)tutorialsuccess=1;
2437             break; case 45: if(animation[player[0].targetanimation].attack==reversal)tutorialsuccess=1;
2438             break; case 46: if(animation[player[0].targetanimation].attack==reversal)tutorialsuccess=1;
2439             break; case 49: if(player[1].weaponstuck!=-1)tutorialsuccess=1;
2440             break; default: break;
2441         }
2442         if(tutorialsuccess>=1)tutorialstagetime=tutorialmaxtime-3;
2443
2444
2445         if(tutorialstagetime==tutorialmaxtime-3){
2446             emit_sound_np(consolesuccesssound);
2447         }
2448
2449         if(tutorialsuccess>=1){
2450             if(tutorialstage==34||tutorialstage==35)
2451                 tutorialstagetime=tutorialmaxtime-1;
2452         }
2453     }
2454
2455     if(tutorialstage<14||tutorialstage>=50){
2456         player[1].coords.y=300;
2457         player[1].velocity=0;
2458     }
2459 }
2460
2461 void Game::doDebugKeys(){
2462         float headprop,bodyprop,armprop,legprop;
2463     if(debugmode){
2464         if(Input::isKeyPressed(SDLK_h)){
2465             player[0].damagetolerance=200000;
2466             player[0].damage=0;
2467             player[0].burnt=0;
2468             player[0].permanentdamage=0;
2469             player[0].superpermanentdamage=0;
2470         }
2471
2472         if(Input::isKeyPressed(SDLK_j)){
2473             environment++;
2474             if(environment>2)
2475                 environment=0;
2476             Setenvironment(environment);
2477         }
2478
2479         if(Input::isKeyPressed(SDLK_c)){
2480             cameramode=1-cameramode;
2481         }
2482
2483         if(Input::isKeyPressed(SDLK_x)&&!Input::isKeyDown(SDLK_LSHIFT)){
2484             if(player[0].num_weapons>0){
2485                 if(weapons[player[0].weaponids[0]].getType()==sword)
2486                                         weapons[player[0].weaponids[0]].setType(staff);
2487                 else if(weapons[player[0].weaponids[0]].getType()==staff)
2488                                         weapons[player[0].weaponids[0]].setType(knife);
2489                 else
2490                                         weapons[player[0].weaponids[0]].setType(sword);
2491             }
2492         }
2493
2494         if(Input::isKeyPressed(SDLK_x)&&Input::isKeyDown(SDLK_LSHIFT)){
2495             int closest=-1;
2496             float closestdist=-1;
2497             float distance;
2498             if(numplayers>1)
2499                 for(int i=1;i<numplayers;i++){
2500                     distance=findDistancefast(&player[i].coords,&player[0].coords);
2501                     if(closestdist==-1||distance<closestdist){
2502                         closestdist=distance;
2503                         closest=i;
2504                     }
2505                 }
2506             if(closest!=-1){
2507                 if(player[closest].num_weapons){
2508                     if(weapons[player[closest].weaponids[0]].getType()==sword)
2509                         weapons[player[closest].weaponids[0]].setType(staff);
2510                     else if(weapons[player[closest].weaponids[0]].getType()==staff)
2511                         weapons[player[closest].weaponids[0]].setType(knife);
2512                     else
2513                                                 weapons[player[closest].weaponids[0]].setType(sword);
2514                 }
2515                 if(!player[closest].num_weapons){
2516                     player[closest].weaponids[0]=weapons.size();
2517                     
2518                     weapons.push_back(Weapon(knife,closest));
2519                     
2520                     player[closest].num_weapons=1;
2521                 }
2522             }
2523         }
2524
2525         if(Input::isKeyDown(SDLK_u)){
2526             int closest=-1;
2527             float closestdist=-1;
2528             float distance;
2529             if(numplayers>1)
2530                 for(int i=1;i<numplayers;i++){
2531                     distance=findDistancefast(&player[i].coords,&player[0].coords);
2532                     if(closestdist==-1||distance<closestdist){
2533                         closestdist=distance;
2534                         closest=i;
2535                     }
2536                 }
2537             player[closest].rotation+=multiplier*50;
2538             player[closest].targetrotation=player[closest].rotation;
2539         }
2540
2541
2542         if(Input::isKeyPressed(SDLK_o)&&!Input::isKeyDown(SDLK_LSHIFT)){
2543             int closest=-1;
2544             float closestdist=-1;
2545             float distance;
2546             if(numplayers>1)
2547                 for(int i=1;i<numplayers;i++){
2548                     distance=findDistancefast(&player[i].coords,&player[0].coords);
2549                     if(closestdist==-1||distance<closestdist){
2550                         closestdist=distance;
2551                         closest=i;
2552                     }
2553                 }
2554             if(Input::isKeyDown(SDLK_LCTRL))closest=0;
2555
2556             if(closest!=-1){
2557                 player[closest].whichskin++;
2558                 if(player[closest].whichskin>9)
2559                     player[closest].whichskin=0;
2560                 if(player[closest].whichskin>2&&player[closest].creature==wolftype)
2561                     player[closest].whichskin=0;
2562
2563                 LoadTextureSave(creatureskin[player[closest].creature][player[closest].whichskin],
2564                         &player[closest].skeleton.drawmodel.textureptr,1,&player[closest].skeleton.skinText[0],&player[closest].skeleton.skinsize);
2565             }
2566
2567             if(player[closest].numclothes){
2568                 for(int i=0;i<player[closest].numclothes;i++){
2569                     tintr=player[closest].clothestintr[i];
2570                     tintg=player[closest].clothestintg[i];
2571                     tintb=player[closest].clothestintb[i];
2572                     AddClothes((char *)player[closest].clothes[i],&player[closest].skeleton.skinText[0]);
2573                 }
2574                 player[closest].DoMipmaps();
2575             }
2576         }
2577
2578         if(Input::isKeyPressed(SDLK_o)&&Input::isKeyDown(SDLK_LSHIFT)){
2579             int closest=-1;
2580             float closestdist=-1;
2581             float distance;
2582             if(numplayers>1)
2583                 for(int i=1;i<numplayers;i++){
2584                     distance=findDistancefast(&player[i].coords,&player[0].coords);
2585                     if(closestdist==-1||distance<closestdist){
2586                         closestdist=distance;
2587                         closest=i;
2588                     }
2589                 }
2590             if(closest!=-1){
2591                 if(player[closest].creature==wolftype){
2592                     headprop=player[closest].proportionhead.x/1.1;
2593                     bodyprop=player[closest].proportionbody.x/1.1;
2594                     armprop=player[closest].proportionarms.x/1.1;
2595                     legprop=player[closest].proportionlegs.x/1.1;
2596                 }
2597
2598                 if(player[closest].creature==rabbittype){
2599                     headprop=player[closest].proportionhead.x/1.2;
2600                     bodyprop=player[closest].proportionbody.x/1.05;
2601                     armprop=player[closest].proportionarms.x/1.00;
2602                     legprop=player[closest].proportionlegs.x/1.1;
2603                 }
2604
2605
2606                 if(player[closest].creature==rabbittype){
2607                     player[closest].skeleton.id=closest;
2608                     player[closest].skeleton.Load((char *)":Data:Skeleton:Basic Figure Wolf",(char *)":Data:Skeleton:Basic Figure Wolf Low",(char *)":Data:Skeleton:Rabbitbelt",(char *)":Data:Models:Wolf.solid",(char *)":Data:Models:Wolf2.solid",(char *)":Data:Models:Wolf3.solid",(char *)":Data:Models:Wolf4.solid",(char *)":Data:Models:Wolf5.solid",(char *)":Data:Models:Wolf6.solid",(char *)":Data:Models:Wolf7.solid",(char *)":Data:Models:Wolflow.solid",(char *)":Data:Models:Belt.solid",0);
2609                     LoadTextureSave(":Data:Textures:Wolf.jpg",&player[closest].skeleton.drawmodel.textureptr,1,&player[closest].skeleton.skinText[closest],&player[closest].skeleton.skinsize);
2610                     player[closest].whichskin=0;
2611                     player[closest].creature=wolftype;
2612
2613                     player[closest].proportionhead=1.1;
2614                     player[closest].proportionbody=1.1;
2615                     player[closest].proportionarms=1.1;
2616                     player[closest].proportionlegs=1.1;
2617                     player[closest].proportionlegs.y=1.1;
2618                     player[closest].scale=.23*5*player[0].scale;
2619
2620                     player[closest].damagetolerance=300;
2621                 }
2622                 else
2623                 {
2624                     player[closest].skeleton.id=closest;
2625                     player[closest].skeleton.Load((char *)":Data:Skeleton:Basic Figure",(char *)":Data:Skeleton:Basic Figurelow",(char *)":Data:Skeleton:Rabbitbelt",(char *)":Data:Models:Body.solid",(char *)":Data:Models:Body2.solid",(char *)":Data:Models:Body3.solid",(char *)":Data:Models:Body4.solid",(char *)":Data:Models:Body5.solid",(char *)":Data:Models:Body6.solid",(char *)":Data:Models:Body7.solid",(char *)":Data:Models:Bodylow.solid",(char *)":Data:Models:Belt.solid",1);
2626                     LoadTextureSave(":Data:Textures:Fur3.jpg",&player[closest].skeleton.drawmodel.textureptr,1,&player[closest].skeleton.skinText[0],&player[closest].skeleton.skinsize);
2627                     player[closest].whichskin=0;
2628                     player[closest].creature=rabbittype;
2629
2630                     player[closest].proportionhead=1.2;
2631                     player[closest].proportionbody=1.05;
2632                     player[closest].proportionarms=1.00;
2633                     player[closest].proportionlegs=1.1;
2634                     player[closest].proportionlegs.y=1.05;
2635                     player[closest].scale=.2*5*player[0].scale;
2636
2637                     player[closest].damagetolerance=200;
2638                 }
2639
2640                 if(player[closest].creature==wolftype){
2641                     player[closest].proportionhead=1.1*headprop;
2642                     player[closest].proportionbody=1.1*bodyprop;
2643                     player[closest].proportionarms=1.1*armprop;
2644                     player[closest].proportionlegs=1.1*legprop;
2645                 }
2646
2647                 if(player[closest].creature==rabbittype){
2648                     player[closest].proportionhead=1.2*headprop;
2649                     player[closest].proportionbody=1.05*bodyprop;
2650                     player[closest].proportionarms=1.00*armprop;
2651                     player[closest].proportionlegs=1.1*legprop;
2652                     player[closest].proportionlegs.y=1.05*legprop;
2653                 }
2654
2655             }
2656         }
2657
2658         if(Input::isKeyPressed(SDLK_b)&&!Input::isKeyDown(SDLK_LSHIFT)){
2659             slomo=1-slomo;
2660             slomodelay=1000;
2661         }
2662
2663
2664         if(((Input::isKeyPressed(SDLK_i)&&!Input::isKeyDown(SDLK_LSHIFT)))){
2665             int closest=-1;
2666             float closestdist=-1;
2667             float distance;
2668             XYZ flatfacing2,flatvelocity2;
2669             XYZ blah;
2670             if(numplayers>1)
2671                 for(int i=1;i<numplayers;i++){
2672                     distance=findDistancefast(&player[i].coords,&player[0].coords);
2673                     if(distance<144&&!player[i].headless)
2674                         if(closestdist==-1||distance<closestdist){
2675                             closestdist=distance;
2676                             closest=i;
2677                             blah = player[i].coords;
2678                         }
2679                 }
2680
2681             if(closest!=-1){
2682                 XYZ headspurtdirection;
2683                 //int i = player[closest].skeleton.jointlabels[head];
2684                 Joint& headjoint= player[closest].getJointFor(head);
2685                 for(int k=0;k<player[closest].skeleton.num_joints; k++){
2686                     if(!player[closest].skeleton.free)
2687                         flatvelocity2=player[closest].velocity;
2688                     if(player[closest].skeleton.free)
2689                         flatvelocity2=headjoint.velocity;
2690                     if(!player[closest].skeleton.free)
2691                         flatfacing2=DoRotation(DoRotation(DoRotation(headjoint.position,0,0,player[closest].tilt),player[closest].tilt2,0,0),0,player[closest].rotation,0)*player[closest].scale+player[closest].coords;
2692                     if(player[closest].skeleton.free)
2693                         flatfacing2=headjoint.position*player[closest].scale+player[closest].coords;
2694                     flatvelocity2.x+=(float)(abs(Random()%100)-50)/10;
2695                     flatvelocity2.y+=(float)(abs(Random()%100)-50)/10;
2696                     flatvelocity2.z+=(float)(abs(Random()%100)-50)/10;
2697                     headspurtdirection=headjoint.position-player[closest].getJointFor(neck).position;
2698                     Normalise(&headspurtdirection);
2699                     Sprite::MakeSprite(bloodflamesprite, flatfacing2,flatvelocity2, 1,1,1, .6, 1);
2700                     flatvelocity2+=headspurtdirection*8;
2701                     Sprite::MakeSprite(bloodsprite, flatfacing2,flatvelocity2/2, 1,1,1, .16, 1);
2702                 }
2703                 Sprite::MakeSprite(cloudsprite, flatfacing2,flatvelocity2*0, .6,0,0, 1, .5);
2704
2705                 emit_sound_at(splattersound, blah);
2706                 emit_sound_at(breaksound2, blah, 100.);
2707
2708                 if(player[closest].skeleton.free==2)player[closest].skeleton.free=0;
2709                 player[closest].RagDoll(0);
2710                 player[closest].dead=2;
2711                 player[closest].headless=1;
2712                 player[closest].DoBloodBig(3,165);
2713
2714                 camerashake+=.3;
2715             }
2716         }
2717
2718         if(((Input::isKeyPressed(SDLK_i)&&Input::isKeyDown(SDLK_LSHIFT)))){
2719             int closest=-1;
2720             float closestdist=-1;
2721             float distance;
2722             XYZ flatfacing2,flatvelocity2;
2723             XYZ blah;
2724             if(numplayers>1)
2725                 for(int i=1;i<numplayers;i++){
2726                     distance=findDistancefast(&player[i].coords,&player[0].coords);
2727                     if(distance<144)
2728                         if(closestdist==-1||distance<closestdist){
2729                             closestdist=distance;
2730                             closest=i;
2731                             blah=player[i].coords;
2732                         }
2733                 }
2734
2735             if(closest!=-1){
2736                 emit_sound_at(splattersound, blah);
2737
2738                 emit_sound_at(breaksound2, blah);
2739
2740                 for(int i=0;i<player[closest].skeleton.num_joints; i++){
2741                     if(!player[closest].skeleton.free)flatvelocity2=player[closest].velocity;
2742                     if(player[closest].skeleton.free)flatvelocity2=player[closest].skeleton.joints[i].velocity;
2743                     if(!player[closest].skeleton.free)flatfacing2=DoRotation(DoRotation(DoRotation(player[closest].skeleton.joints[i].position,0,0,player[closest].tilt),player[closest].tilt2,0,0),0,player[closest].rotation,0)*player[closest].scale+player[closest].coords;
2744                     if(player[closest].skeleton.free)flatfacing2=player[closest].skeleton.joints[i].position*player[closest].scale+player[closest].coords;
2745                     flatvelocity2.x+=(float)(abs(Random()%100)-50)/10;
2746                     flatvelocity2.y+=(float)(abs(Random()%100)-50)/10;
2747                     flatvelocity2.z+=(float)(abs(Random()%100)-50)/10;
2748                     Sprite::MakeSprite(bloodflamesprite, flatfacing2,flatvelocity2, 1,1,1, 3, 1);
2749                     Sprite::MakeSprite(bloodsprite, flatfacing2,flatvelocity2, 1,1,1, .3, 1);
2750                     Sprite::MakeSprite(cloudsprite, flatfacing2,flatvelocity2*0, .6,0,0, 1, .5);
2751                 }
2752
2753                 for(int i=0;i<player[closest].skeleton.num_joints; i++){
2754                     if(!player[closest].skeleton.free)flatvelocity2=player[closest].velocity;
2755                     if(player[closest].skeleton.free)flatvelocity2=player[closest].skeleton.joints[i].velocity;
2756                     if(!player[closest].skeleton.free)flatfacing2=DoRotation(DoRotation(DoRotation(player[closest].skeleton.joints[i].position,0,0,player[closest].tilt),player[closest].tilt2,0,0),0,player[closest].rotation,0)*player[closest].scale+player[closest].coords;
2757                     if(player[closest].skeleton.free)flatfacing2=player[closest].skeleton.joints[i].position*player[closest].scale+player[closest].coords;
2758                     flatvelocity2.x+=(float)(abs(Random()%100)-50)/10;
2759                     flatvelocity2.y+=(float)(abs(Random()%100)-50)/10;
2760                     flatvelocity2.z+=(float)(abs(Random()%100)-50)/10;
2761                     Sprite::MakeSprite(bloodflamesprite, flatfacing2,flatvelocity2, 1,1,1, 3, 1);
2762                     Sprite::MakeSprite(bloodsprite, flatfacing2,flatvelocity2, 1,1,1, .4, 1);
2763                 }
2764
2765                 for(int i=0;i<player[closest].skeleton.num_joints; i++){
2766                     if(!player[closest].skeleton.free)flatvelocity2=player[closest].velocity;
2767                     if(player[closest].skeleton.free)flatvelocity2=player[closest].skeleton.joints[i].velocity;
2768                     if(!player[closest].skeleton.free)flatfacing2=DoRotation(DoRotation(DoRotation(player[closest].skeleton.joints[i].position,0,0,player[closest].tilt),player[closest].tilt2,0,0),0,player[closest].rotation,0)*player[closest].scale+player[closest].coords;
2769                     if(player[closest].skeleton.free)flatfacing2=player[closest].skeleton.joints[i].position*player[closest].scale+player[closest].coords;
2770                     flatvelocity2.x+=(float)(abs(Random()%100)-50)/10;
2771                     flatvelocity2.y+=(float)(abs(Random()%100)-50)/10;
2772                     flatvelocity2.z+=(float)(abs(Random()%100)-50)/10;
2773                     Sprite::MakeSprite(bloodflamesprite, flatfacing2,flatvelocity2*2, 1,1,1, 3, 1);
2774                     Sprite::MakeSprite(bloodsprite, flatfacing2,flatvelocity2*2, 1,1,1, .4, 1);
2775                 }
2776
2777                 for(int i=0;i<player[closest].skeleton.num_joints; i++){
2778                     if(!player[closest].skeleton.free)flatvelocity2=player[closest].velocity;
2779                     if(player[closest].skeleton.free)flatvelocity2=player[closest].skeleton.joints[i].velocity;
2780                     if(!player[closest].skeleton.free)flatfacing2=DoRotation(DoRotation(DoRotation(player[closest].skeleton.joints[i].position,0,0,player[closest].tilt),player[closest].tilt2,0,0),0,player[closest].rotation,0)*player[closest].scale+player[closest].coords;
2781                     if(player[closest].skeleton.free)flatfacing2=player[closest].skeleton.joints[i].position*player[closest].scale+player[closest].coords;
2782                     flatvelocity2.x+=(float)(abs(Random()%100)-50)/10;
2783                     flatvelocity2.y+=(float)(abs(Random()%100)-50)/10;
2784                     flatvelocity2.z+=(float)(abs(Random()%100)-50)/10;
2785                     Sprite::MakeSprite(bloodflamesprite, flatfacing2,flatvelocity2*2, 1,1,1, 3, 1);
2786                     Sprite::MakeSprite(bloodsprite, flatfacing2,flatvelocity2*2, 1,1,1, .4, 1);
2787                 }
2788
2789                 XYZ temppos;
2790                 for(int j=0;j<numplayers; j++){
2791                     if(j!=closest){
2792                         if(findDistancefast(&player[j].coords,&player[closest].coords)<25){
2793                             player[j].DoDamage((25-findDistancefast(&player[j].coords,&player[closest].coords))*60);
2794                             if(player[j].skeleton.free==2)
2795                                 player[j].skeleton.free=1;
2796                             player[j].skeleton.longdead=0;
2797                             player[j].RagDoll(0);
2798                             for(int i=0;i<player[j].skeleton.num_joints; i++){
2799                                 temppos=player[j].skeleton.joints[i].position+player[j].coords;
2800                                 if(findDistancefast(&temppos,&player[closest].coords)<25){
2801                                     flatvelocity2=temppos-player[closest].coords;
2802                                     Normalise(&flatvelocity2);
2803                                     player[j].skeleton.joints[i].velocity+=flatvelocity2*((20-findDistancefast(&temppos,&player[closest].coords))*20);
2804                                 }
2805                             }
2806                         }
2807                     }
2808                 }
2809
2810                 player[closest].DoDamage(10000);
2811                 player[closest].RagDoll(0);
2812                 player[closest].dead=2;
2813                 player[closest].coords=20;
2814                 player[closest].skeleton.free=2;
2815
2816                 camerashake+=.6;
2817
2818             }
2819         }
2820
2821         if(Input::isKeyPressed(SDLK_f)){
2822             player[0].onfire=1-player[0].onfire;
2823             if(player[0].onfire){
2824                 player[0].CatchFire();
2825             }
2826             if(!player[0].onfire){
2827                 emit_sound_at(fireendsound, player[0].coords);
2828                 pause_sound(stream_firesound);
2829             }
2830         }
2831
2832         if(Input::isKeyPressed(SDLK_n)&&!Input::isKeyDown(SDLK_LCTRL)){
2833             //if(!player[0].skeleton.free)player[0].damage+=500;
2834             player[0].RagDoll(0);
2835             //player[0].spurt=1;
2836             //player[0].DoDamage(1000);
2837
2838             emit_sound_at(whooshsound, player[0].coords, 128.);
2839         }
2840
2841         if(Input::isKeyPressed(SDLK_n)&&Input::isKeyDown(SDLK_LCTRL)){
2842             for(int i=0;i<objects.numobjects;i++){
2843                 if(objects.type[i]==treeleavestype){
2844                     objects.scale[i]*=.9;
2845                 }
2846             }
2847         }
2848
2849         if(Input::isKeyPressed(SDLK_m)&&Input::isKeyDown(SDLK_LSHIFT)){
2850             editorenabled=1-editorenabled;
2851             if(editorenabled){
2852                 player[0].damagetolerance=100000;
2853             } else {
2854                 player[0].damagetolerance=200;
2855             }
2856             player[0].damage=0; // these lines were in both if and else, but I think they would better fit in the if
2857             player[0].permanentdamage=0;
2858             player[0].superpermanentdamage=0;
2859             player[0].bloodloss=0;
2860             player[0].deathbleeding=0;
2861         }
2862
2863         //skip level
2864         if(whichlevel!=-2&&Input::isKeyPressed(SDLK_k)&&Input::isKeyDown(SDLK_LSHIFT)&&!editorenabled){
2865             targetlevel++;
2866             if(targetlevel>numchallengelevels-1)
2867                 targetlevel=0;
2868             loading=1;
2869             leveltime=5;
2870         }
2871
2872         if(editorenabled){
2873             if(Input::isKeyPressed(SDLK_DELETE)&&Input::isKeyDown(SDLK_LSHIFT)){
2874                 int closest=-1;
2875                 float closestdist=-1;
2876                 float distance;
2877                 if(numplayers>1)
2878                     for(int i=1;i<numplayers;i++){
2879                         distance=findDistancefast(&player[i].coords,&player[0].coords);
2880                         if(closestdist==-1||distance<closestdist){
2881                             closestdist=distance;
2882                             closest=i;
2883                         }
2884                     }
2885                 if(closestdist>0&&closest>=0){
2886                     //player[closest]=player[numplayers-1];
2887                     //player[closest].skeleton=player[numplayers-1].skeleton;
2888                     numplayers--;
2889                 }
2890             }
2891
2892             if(Input::isKeyPressed(SDLK_DELETE)&&Input::isKeyDown(SDLK_LCTRL)){
2893                 int closest=-1;
2894                 float closestdist=-1;
2895                 float distance;
2896                 if(max_objects>1)
2897                     for(int i=1;i<max_objects;i++){
2898                         distance=findDistancefast(&objects.position[i],&player[0].coords);
2899                         if(closestdist==-1||distance<closestdist){
2900                             closestdist=distance;
2901                             closest=i;
2902                         }
2903                     }
2904                 if(closestdist>0&&closest>=0){
2905                     objects.position[closest].y-=500;
2906                 }
2907             }
2908
2909             if(Input::isKeyPressed(SDLK_m)&&Input::isKeyDown(SDLK_LSHIFT)){
2910                 //drawmode++;
2911                 //if(drawmode>2)drawmode=0;
2912                 if(objects.numobjects<max_objects-1){
2913                     XYZ boxcoords;
2914                     boxcoords.x=player[0].coords.x;
2915                     boxcoords.z=player[0].coords.z;
2916                     boxcoords.y=player[0].coords.y-3;
2917                     if(editortype==bushtype)boxcoords.y=player[0].coords.y-.5;
2918                     if(editortype==firetype)boxcoords.y=player[0].coords.y-.5;
2919                     //objects.MakeObject(abs(Random()%3),boxcoords,Random()%360);
2920                     float temprotat,temprotat2;
2921                     temprotat=editorrotation;
2922                     temprotat2=editorrotation2;
2923                     if(temprotat<0||editortype==bushtype)temprotat=Random()%360;
2924                     if(temprotat2<0)temprotat2=Random()%360;
2925
2926                     objects.MakeObject(editortype,boxcoords,(int)temprotat-((int)temprotat)%30,(int)temprotat2,editorsize);
2927                     if(editortype==treetrunktype)
2928                         objects.MakeObject(treeleavestype,boxcoords,Random()%360*(temprotat2<2)+(int)editorrotation-((int)editorrotation)%30,editorrotation2,editorsize);
2929                 }
2930             }
2931
2932             if(Input::isKeyPressed(SDLK_p)&&Input::isKeyDown(SDLK_LSHIFT)&&!Input::isKeyDown(SDLK_LCTRL)){
2933                 if(numplayers<maxplayers-1){
2934                     player[numplayers].scale=.2*5*player[0].scale;
2935                     player[numplayers].creature=rabbittype;
2936                     player[numplayers].howactive=editoractive;
2937                     player[numplayers].skeleton.id=numplayers;
2938                     player[numplayers].skeleton.Load((char *)":Data:Skeleton:Basic Figure",(char *)":Data:Skeleton:Basic Figurelow",(char *)":Data:Skeleton:Rabbitbelt",(char *)":Data:Models:Body.solid",(char *)":Data:Models:Body2.solid",(char *)":Data:Models:Body3.solid",(char *)":Data:Models:Body4.solid",(char *)":Data:Models:Body5.solid",(char *)":Data:Models:Body6.solid",(char *)":Data:Models:Body7.solid",(char *)":Data:Models:Bodylow.solid",(char *)":Data:Models:Belt.solid",1);
2939
2940                     //texsize=512*512*3/texdetail/texdetail;
2941                     //if(!player[numplayers].loaded)player[numplayers].skeleton.skinText = new GLubyte[texsize];
2942                     //player[numplayers].skeleton.skinText.resize(texsize);
2943
2944                     int k=abs(Random()%2)+1;
2945                     if(k==0){
2946                         LoadTextureSave(":Data:Textures:Fur3.jpg",&player[numplayers].skeleton.drawmodel.textureptr,1,&player[numplayers].skeleton.skinText[0],&player[numplayers].skeleton.skinsize);
2947                         player[numplayers].whichskin=0;
2948                     }
2949                     else if(k==1){
2950                         LoadTextureSave(":Data:Textures:Fur.jpg",&player[numplayers].skeleton.drawmodel.textureptr,1,&player[numplayers].skeleton.skinText[0],&player[numplayers].skeleton.skinsize);
2951                         player[numplayers].whichskin=1;
2952                     }
2953                     else {
2954                         LoadTextureSave(":Data:Textures:Fur2.jpg",&player[numplayers].skeleton.drawmodel.textureptr,1,&player[numplayers].skeleton.skinText[0],&player[numplayers].skeleton.skinsize);
2955                         player[numplayers].whichskin=2;
2956                     }
2957
2958                     LoadTexture(":Data:Textures:Belt.png",&player[numplayers].skeleton.drawmodelclothes.textureptr,1,1);
2959                     player[numplayers].power=1;
2960                     player[numplayers].speedmult=1;
2961                     player[numplayers].currentanimation=bounceidleanim;
2962                     player[numplayers].targetanimation=bounceidleanim;
2963                     player[numplayers].currentframe=0;
2964                     player[numplayers].targetframe=1;
2965                     player[numplayers].target=0;
2966                     player[numplayers].bled=0;
2967                     player[numplayers].speed=1+(float)(Random()%100)/1000;
2968
2969                     player[numplayers].targetrotation=player[0].targetrotation;
2970                     player[numplayers].rotation=player[0].rotation;
2971
2972                     player[numplayers].velocity=0;
2973                     player[numplayers].coords=player[0].coords;
2974                     player[numplayers].oldcoords=player[numplayers].coords;
2975                     player[numplayers].realoldcoords=player[numplayers].coords;
2976
2977                     player[numplayers].id=numplayers;
2978                     player[numplayers].skeleton.id=numplayers;
2979                     player[numplayers].updatedelay=0;
2980                     player[numplayers].normalsupdatedelay=0;
2981
2982                     player[numplayers].aitype=passivetype;
2983
2984                     if(player[0].creature==wolftype){
2985                         headprop=player[0].proportionhead.x/1.1;
2986                         bodyprop=player[0].proportionbody.x/1.1;
2987                         armprop=player[0].proportionarms.x/1.1;
2988                         legprop=player[0].proportionlegs.x/1.1;
2989                     }
2990
2991                     if(player[0].creature==rabbittype){
2992                         headprop=player[0].proportionhead.x/1.2;
2993                         bodyprop=player[0].proportionbody.x/1.05;
2994                         armprop=player[0].proportionarms.x/1.00;
2995                         legprop=player[0].proportionlegs.x/1.1;
2996                     }
2997
2998                     if(player[numplayers].creature==wolftype){
2999                         player[numplayers].proportionhead=1.1*headprop;
3000                         player[numplayers].proportionbody=1.1*bodyprop;
3001                         player[numplayers].proportionarms=1.1*armprop;
3002                         player[numplayers].proportionlegs=1.1*legprop;
3003                     }
3004
3005                     if(player[numplayers].creature==rabbittype){
3006                         player[numplayers].proportionhead=1.2*headprop;
3007                         player[numplayers].proportionbody=1.05*bodyprop;
3008                         player[numplayers].proportionarms=1.00*armprop;
3009                         player[numplayers].proportionlegs=1.1*legprop;
3010                         player[numplayers].proportionlegs.y=1.05*legprop;
3011                     }
3012
3013                     player[numplayers].headless=0;
3014                     player[numplayers].onfire=0;
3015
3016                     if(cellophane){
3017                         player[numplayers].proportionhead.z=0;
3018                         player[numplayers].proportionbody.z=0;
3019                         player[numplayers].proportionarms.z=0;
3020                         player[numplayers].proportionlegs.z=0;
3021                     }
3022
3023                     player[numplayers].tempanimation.Load((char *)"Tempanim",0,0);
3024
3025                     player[numplayers].damagetolerance=200;
3026
3027                     player[numplayers].protectionhead=player[0].protectionhead;
3028                     player[numplayers].protectionhigh=player[0].protectionhigh;
3029                     player[numplayers].protectionlow=player[0].protectionlow;
3030                     player[numplayers].armorhead=player[0].armorhead;
3031                     player[numplayers].armorhigh=player[0].armorhigh;
3032                     player[numplayers].armorlow=player[0].armorlow;
3033                     player[numplayers].metalhead=player[0].metalhead;
3034                     player[numplayers].metalhigh=player[0].metalhigh;
3035                     player[numplayers].metallow=player[0].metallow;
3036
3037                     player[numplayers].immobile=player[0].immobile;
3038
3039                     player[numplayers].numclothes=player[0].numclothes;
3040                     if(player[numplayers].numclothes)
3041                         for(int i=0;i<player[numplayers].numclothes;i++){
3042                             strcpy(player[numplayers].clothes[i], player[0].clothes[i]);
3043                             player[numplayers].clothestintr[i]=player[0].clothestintr[i];
3044                             player[numplayers].clothestintg[i]=player[0].clothestintg[i];
3045                             player[numplayers].clothestintb[i]=player[0].clothestintb[i];
3046                             tintr=player[numplayers].clothestintr[i];
3047                             tintg=player[numplayers].clothestintg[i];
3048                             tintb=player[numplayers].clothestintb[i];
3049                             AddClothes((char *)player[numplayers].clothes[i],&player[numplayers].skeleton.skinText[0]);
3050                         }
3051                     if(player[numplayers].numclothes){
3052                         player[numplayers].DoMipmaps();
3053                     }
3054
3055                     player[numplayers].power=player[0].power;
3056                     player[numplayers].speedmult=player[0].speedmult;
3057
3058                     player[numplayers].damage=0;
3059                     player[numplayers].permanentdamage=0;
3060                     player[numplayers].superpermanentdamage=0;
3061                     player[numplayers].deathbleeding=0;
3062                     player[numplayers].bleeding=0;
3063                     player[numplayers].numwaypoints=0;
3064                     player[numplayers].waypoint=0;
3065                     player[numplayers].jumppath=0;
3066                     player[numplayers].weaponstuck=-1;
3067                     player[numplayers].weaponactive=-1;
3068                     player[numplayers].num_weapons=0;
3069                     player[numplayers].bloodloss=0;
3070                     player[numplayers].dead=0;
3071
3072                     player[numplayers].loaded=1;
3073
3074                     numplayers++;
3075                 }
3076             }
3077
3078             if(Input::isKeyPressed(SDLK_p)&&Input::isKeyDown(SDLK_LSHIFT)){
3079                 if(player[numplayers-1].numwaypoints<90){
3080                     player[numplayers-1].waypoints[player[numplayers-1].numwaypoints]=player[0].coords;
3081                     player[numplayers-1].waypointtype[player[numplayers-1].numwaypoints]=editorpathtype;
3082                     player[numplayers-1].numwaypoints++;
3083                 }
3084             }
3085
3086             if(Input::isKeyPressed(SDLK_p)&&Input::isKeyDown(SDLK_LCTRL)){
3087                 if(numpathpoints<30){
3088                     bool connected,alreadyconnected;
3089                     connected=0;
3090                     if(numpathpoints>1)
3091                         for(int i=0;i<numpathpoints;i++){
3092                             if(findDistancefast(&pathpoint[i],&player[0].coords)<.5&&i!=pathpointselected&&!connected){
3093                                 alreadyconnected=0;
3094                                 for(int j=0;j<numpathpointconnect[pathpointselected];j++){
3095                                     if(pathpointconnect[pathpointselected][j]==i)alreadyconnected=1;
3096                                 }
3097                                 if(!alreadyconnected){
3098                                     numpathpointconnect[pathpointselected]++;
3099                                     connected=1;
3100                                     pathpointconnect[pathpointselected][numpathpointconnect[pathpointselected]-1]=i;
3101                                 }
3102                             }
3103                         }
3104                     if(!connected){
3105                         numpathpoints++;
3106                         pathpoint[numpathpoints-1]=player[0].coords;
3107                         numpathpointconnect[numpathpoints-1]=0;
3108                         if(numpathpoints>1&&pathpointselected!=-1){
3109                             numpathpointconnect[pathpointselected]++;
3110                             pathpointconnect[pathpointselected][numpathpointconnect[pathpointselected]-1]=numpathpoints-1;
3111                         }
3112                         pathpointselected=numpathpoints-1;
3113                     }
3114                 }
3115             }
3116
3117             if(Input::isKeyPressed(SDLK_PERIOD)){
3118                 pathpointselected++;
3119                 if(pathpointselected>=numpathpoints)
3120                     pathpointselected=-1;
3121             }
3122             if(Input::isKeyPressed(SDLK_COMMA)&&!Input::isKeyDown(SDLK_LSHIFT)){
3123                 pathpointselected--;
3124                 if(pathpointselected<=-2)
3125                     pathpointselected=numpathpoints-1;
3126             }
3127             if(Input::isKeyPressed(SDLK_COMMA)&&Input::isKeyDown(SDLK_LSHIFT)){
3128                 if(pathpointselected!=-1){
3129                     numpathpoints--;
3130                     pathpoint[pathpointselected]=pathpoint[numpathpoints];
3131                     numpathpointconnect[pathpointselected]=numpathpointconnect[numpathpoints];
3132                     for(int i=0;i<numpathpointconnect[pathpointselected];i++){
3133                         pathpointconnect[pathpointselected][i]=pathpointconnect[numpathpoints][i];
3134                     }
3135                     for(int i=0;i<numpathpoints;i++){
3136                         for(int j=0;j<numpathpointconnect[i];j++){
3137                             if(pathpointconnect[i][j]==pathpointselected){
3138                                 pathpointconnect[i][j]=pathpointconnect[i][numpathpointconnect[i]-1];
3139                                 numpathpointconnect[i]--;
3140                             }
3141                             if(pathpointconnect[i][j]==numpathpoints){
3142                                 pathpointconnect[i][j]=pathpointselected;
3143                             }
3144                         }
3145                     }
3146                     pathpointselected=numpathpoints-1;
3147                 }
3148             }
3149
3150             if(Input::isKeyPressed(SDLK_LEFT)&&Input::isKeyDown(SDLK_LSHIFT)&&!Input::isKeyDown(SDLK_LCTRL)){
3151                 editortype--;
3152                 if(editortype==treeleavestype||editortype==10)editortype--;
3153                 if(editortype<0)editortype=firetype;
3154             }
3155
3156             if(Input::isKeyPressed(SDLK_RIGHT)&&Input::isKeyDown(SDLK_LSHIFT)&&!Input::isKeyDown(SDLK_LCTRL)){
3157                 editortype++;
3158                 if(editortype==treeleavestype||editortype==10)editortype++;
3159                 if(editortype>firetype)editortype=0;
3160             }
3161
3162             if(Input::isKeyDown(SDLK_LEFT)&&!Input::isKeyDown(SDLK_LSHIFT)&&!Input::isKeyDown(SDLK_LCTRL)){
3163                 editorrotation-=multiplier*100;
3164                 if(editorrotation<-.01)editorrotation=-.01;
3165             }
3166
3167             if(Input::isKeyDown(SDLK_RIGHT)&&!Input::isKeyDown(SDLK_LSHIFT)&&!Input::isKeyDown(SDLK_LCTRL)){
3168                 editorrotation+=multiplier*100;
3169             }
3170
3171             if(Input::isKeyDown(SDLK_UP)&&!Input::isKeyDown(SDLK_LCTRL)){
3172                 editorsize+=multiplier;
3173             }
3174
3175             if(Input::isKeyDown(SDLK_DOWN)&&!Input::isKeyDown(SDLK_LCTRL)){
3176                 editorsize-=multiplier;
3177                 if(editorsize<.1)editorsize=.1;
3178             }
3179
3180
3181             if(Input::isKeyPressed(SDLK_LEFT)&&Input::isKeyDown(SDLK_LSHIFT)&&Input::isKeyDown(SDLK_LCTRL)){
3182                 mapradius-=multiplier*10;
3183             }
3184
3185             if(Input::isKeyPressed(SDLK_RIGHT)&&Input::isKeyDown(SDLK_LSHIFT)&&Input::isKeyDown(SDLK_LCTRL)){
3186                 mapradius+=multiplier*10;
3187             }
3188             if(Input::isKeyDown(SDLK_UP)&&Input::isKeyDown(SDLK_LCTRL)){
3189                 editorrotation2+=multiplier*100;
3190             }
3191
3192             if(Input::isKeyDown(SDLK_DOWN)&&Input::isKeyDown(SDLK_LCTRL)){
3193                 editorrotation2-=multiplier*100;
3194                 if(editorrotation2<-.01)editorrotation2=-.01;
3195             }
3196             if(Input::isKeyPressed(SDLK_DELETE)&&objects.numobjects&&Input::isKeyDown(SDLK_LSHIFT)){
3197                 int closest=-1;
3198                 float closestdist=-1;
3199                 float distance;
3200                 for(int i=0;i<objects.numobjects;i++){
3201                     distance=findDistancefast(&objects.position[i],&player[0].coords);
3202                     if(closestdist==-1||distance<closestdist){
3203                         closestdist=distance;
3204                         closest=i;
3205                     }
3206                 }
3207                 if(closestdist>0&&closest>=0)objects.DeleteObject(closest);
3208             }
3209         }
3210     }
3211 }
3212
3213 void Game::doJumpReversals(){
3214     for(int k=0;k<numplayers;k++)
3215         for(int i=k;i<numplayers;i++){
3216             if(i==k)continue;
3217             if(     player[k].skeleton.free==0&&
3218                     player[i].skeleton.oldfree==0&&
3219                     (player[i].targetanimation==jumpupanim||
3220                      player[k].targetanimation==jumpupanim)&&
3221                     (player[i].aitype==playercontrolled||
3222                      player[k].aitype==playercontrolled)&&
3223                     (player[i].aitype==attacktypecutoff&&player[i].stunned<=0||
3224                      player[k].aitype==attacktypecutoff&&player[k].stunned<=0)){
3225                 if(     findDistancefast(&player[i].coords,&player[k].coords)<10*sq((player[i].scale+player[k].scale)*2.5)&&
3226                         findDistancefastflat(&player[i].coords,&player[k].coords)<2*sq((player[i].scale+player[k].scale)*2.5)){
3227                     //TODO: refactor two huge similar ifs
3228                     if(player[i].targetanimation==jumpupanim&&
3229                             player[k].targetanimation!=getupfrombackanim&&
3230                             player[k].targetanimation!=getupfromfrontanim&&
3231                             animation[player[k].targetanimation].height==middleheight&&
3232                             normaldotproduct(player[i].velocity,player[k].coords-player[i].coords)<0&&
3233                             (player[k].aitype==playercontrolled&&player[k].attackkeydown||
3234                              player[k].aitype!=playercontrolled)){
3235                         player[i].victim=&player[k];
3236                         player[i].velocity=0;
3237                         player[i].currentanimation=jumpreversedanim;
3238                         player[i].targetanimation=jumpreversedanim;
3239                         player[i].currentframe=0;
3240                         player[i].targetframe=1;
3241                         player[i].targettilt2=0;
3242                         player[k].victim=&player[i];
3243                         player[k].velocity=0;
3244                         player[k].currentanimation=jumpreversalanim;
3245                         player[k].targetanimation=jumpreversalanim;
3246                         player[k].currentframe=0;
3247                         player[k].targetframe=1;
3248                         player[k].targettilt2=0;
3249                         if(player[i].coords.y<player[k].coords.y+1){
3250                             player[i].currentanimation=rabbitkickreversedanim;
3251                             player[i].targetanimation=rabbitkickreversedanim;
3252                             player[i].currentframe=1;
3253                             player[i].targetframe=2;
3254                             player[k].currentanimation=rabbitkickreversalanim;
3255                             player[k].targetanimation=rabbitkickreversalanim;
3256                             player[k].currentframe=1;
3257                             player[k].targetframe=2;
3258                         }
3259                         player[i].target=0;
3260                         player[k].oldcoords=player[k].coords;
3261                         player[i].coords=player[k].coords;
3262                         player[k].targetrotation=player[i].targetrotation;
3263                         player[k].rotation=player[i].targetrotation;
3264                         if(player[k].aitype==attacktypecutoff)
3265                             player[k].stunned=.5;
3266                     }
3267                     if(player[k].targetanimation==jumpupanim&&
3268                             player[i].targetanimation!=getupfrombackanim&&
3269                             player[i].targetanimation!=getupfromfrontanim&&
3270                             animation[player[i].targetanimation].height==middleheight&&
3271                             normaldotproduct(player[k].velocity,player[i].coords-player[k].coords)<0&&
3272                             ((player[i].aitype==playercontrolled&&player[i].attackkeydown)||
3273                              player[i].aitype!=playercontrolled)){
3274                         player[k].victim=&player[i];
3275                         player[k].velocity=0;
3276                         player[k].currentanimation=jumpreversedanim;
3277                         player[k].targetanimation=jumpreversedanim;
3278                         player[k].currentframe=0;
3279                         player[k].targetframe=1;
3280                         player[k].targettilt2=0;
3281                         player[i].victim=&player[k];
3282                         player[i].velocity=0;
3283                         player[i].currentanimation=jumpreversalanim;
3284                         player[i].targetanimation=jumpreversalanim;
3285                         player[i].currentframe=0;
3286                         player[i].targetframe=1;
3287                         player[i].targettilt2=0;
3288                         if(player[k].coords.y<player[i].coords.y+1){
3289                             player[k].targetanimation=rabbitkickreversedanim;
3290                             player[k].currentanimation=rabbitkickreversedanim;
3291                             player[i].currentanimation=rabbitkickreversalanim;
3292                             player[i].targetanimation=rabbitkickreversalanim;
3293                             player[k].currentframe=1;
3294                             player[k].targetframe=2;
3295                             player[i].currentframe=1;
3296                             player[i].targetframe=2;
3297                         }
3298                         player[k].target=0;
3299                         player[i].oldcoords=player[i].coords;
3300                         player[k].coords=player[i].coords;
3301                         player[i].targetrotation=player[k].targetrotation;
3302                         player[i].rotation=player[k].targetrotation;
3303                         if(player[i].aitype==attacktypecutoff)
3304                             player[i].stunned=.5;
3305                     }
3306                 }
3307             }
3308         }
3309 }
3310
3311 void Game::doAerialAcrobatics(){
3312         static XYZ facing,flatfacing;
3313     for(int k=0;k<numplayers;k++){
3314         player[k].turnspeed=500;
3315
3316         if((player[k].isRun()&&
3317                     ((player[k].targetrotation!=rabbitrunninganim&&
3318                       player[k].targetrotation!=wolfrunninganim)||
3319                      player[k].targetframe==4))||
3320                 player[k].targetanimation==removeknifeanim||
3321                 player[k].targetanimation==crouchremoveknifeanim||
3322                 player[k].targetanimation==flipanim||
3323                 player[k].targetanimation==fightsidestep||
3324                 player[k].targetanimation==walkanim){
3325             player[k].rotation=stepTowardf(player[k].rotation, player[k].targetrotation, multiplier*player[k].turnspeed);
3326         }
3327
3328
3329         if(player[k].isStop()||
3330                 player[k].isLanding()||
3331                 player[k].targetanimation==staggerbackhighanim||
3332                 (player[k].targetanimation==sneakanim&&player[k].currentanimation==sneakanim)||
3333                 player[k].targetanimation==staggerbackhardanim||
3334                 player[k].targetanimation==backhandspringanim||
3335                 player[k].targetanimation==dodgebackanim||
3336                 player[k].targetanimation==rollanim||
3337                 (animation[player[k].targetanimation].attack&&
3338                  player[k].targetanimation!=rabbitkickanim&&
3339                  (player[k].targetanimation!=crouchstabanim||player[k].hasvictim)&&
3340                  (player[k].targetanimation!=swordgroundstabanim||player[k].hasvictim))){
3341             player[k].rotation=stepTowardf(player[k].rotation, player[k].targetrotation, multiplier*player[k].turnspeed*2);
3342         }
3343
3344         if(player[k].targetanimation==sneakanim&&player[k].currentanimation!=sneakanim){
3345             player[k].rotation=stepTowardf(player[k].rotation, player[k].targetrotation, multiplier*player[k].turnspeed*4);
3346         }
3347
3348         /*if(player[k].aitype!=passivetype||(findDistancefast(&player[k].coords,&viewer)<viewdistance*viewdistance))*/
3349         player[k].DoStuff();
3350         if(player[k].immobile&&k!=0)
3351             player[k].coords=player[k].realoldcoords;
3352
3353         //if player's position has changed (?)
3354         if(findDistancefast(&player[k].coords,&player[k].realoldcoords)>0&&
3355                 !player[k].skeleton.free&&
3356                 player[k].targetanimation!=climbanim&&
3357                 player[k].targetanimation!=hanganim){
3358             XYZ lowpoint,lowpointtarget,lowpoint2,lowpointtarget2,lowpoint3,lowpointtarget3,lowpoint4,lowpointtarget4,lowpoint5,lowpointtarget5,lowpoint6,lowpointtarget6,lowpoint7,lowpointtarget7,colpoint,colpoint2;
3359             int whichhit;
3360             bool tempcollide=0;
3361
3362             if(player[k].collide<-.3)
3363                 player[k].collide=-.3;
3364             if(player[k].collide>1)
3365                 player[k].collide=1;
3366             player[k].collide-=multiplier*30;
3367
3368             //clip to terrain
3369             player[k].coords.y=max(player[k].coords.y, terrain.getHeight(player[k].coords.x,player[k].coords.z));
3370
3371             for(int l=0;l<terrain.patchobjectnum[player[k].whichpatchx][player[k].whichpatchz];l++){
3372                 int i=terrain.patchobjects[player[k].whichpatchx][player[k].whichpatchz][l];
3373                 if(objects.type[i]!=rocktype||
3374                         objects.scale[i]>.5&&player[k].aitype==playercontrolled||
3375                         objects.position[i].y>player[k].coords.y){
3376                     lowpoint=player[k].coords;
3377                     if(player[k].targetanimation!=jumpupanim&&
3378                             player[k].targetanimation!=jumpdownanim&&
3379                             !player[k].isFlip())
3380                         lowpoint.y+=1.25;
3381                     else
3382                         lowpoint.y+=1.3;
3383                     if(     player[k].coords.y<terrain.getHeight(player[k].coords.x,player[k].coords.z)&&
3384                             player[k].coords.y>terrain.getHeight(player[k].coords.x,player[k].coords.z)-.1)
3385                         player[k].coords.y=terrain.getHeight(player[k].coords.x,player[k].coords.z);
3386                     if(player[k].SphereCheck(&lowpoint, 1.3, &colpoint, &objects.position[i], &objects.rotation[i], &objects.model[i])!=-1){
3387                         flatfacing=lowpoint-player[k].coords;
3388                         player[k].coords=lowpoint;
3389                         player[k].coords.y-=1.3;
3390                         player[k].collide=1;
3391                         tempcollide=1;
3392                         //wall jumps
3393                         //TODO: refactor four similar blocks
3394                         if(player[k].aitype==playercontrolled&&
3395                                 (player[k].targetanimation==jumpupanim||
3396                                  player[k].targetanimation==jumpdownanim||
3397                                  player[k].isFlip())&&
3398                                 !player[k].jumptogglekeydown&&
3399                                 player[k].jumpkeydown){
3400                             lowpointtarget=lowpoint+DoRotation(player[k].facing,0,-90,0)*1.5;
3401                             XYZ tempcoords1=lowpoint;
3402                             whichhit=objects.model[i].LineCheck(&lowpoint,&lowpointtarget,&colpoint,&objects.position[i],&objects.rotation[i]);
3403                             if(whichhit!=-1&&fabs(objects.model[i].facenormals[whichhit].y)<.3){
3404                                 player[k].setAnimation(walljumpleftanim);
3405                                 emit_sound_at(movewhooshsound, player[k].coords);
3406                                 if(k==0)
3407                                     pause_sound(whooshsound);
3408
3409                                 lowpointtarget=DoRotation(objects.model[i].facenormals[whichhit],0,objects.rotation[i],0);
3410                                 player[k].rotation=-asin(0-lowpointtarget.x)*180/M_PI;
3411                                 if(lowpointtarget.z<0)
3412                                     player[k].rotation=180-player[k].rotation;
3413                                 player[k].targetrotation=player[k].rotation;
3414                                 player[k].lowrotation=player[k].rotation;
3415                                 if(k==0)
3416                                     numwallflipped++;
3417                             }
3418                             else
3419                             {
3420                                 lowpoint=tempcoords1;
3421                                 lowpointtarget=lowpoint+DoRotation(player[k].facing,0,90,0)*1.5;
3422                                 whichhit=objects.model[i].LineCheck(&lowpoint,&lowpointtarget,&colpoint,&objects.position[i],&objects.rotation[i]);
3423                                 if(whichhit!=-1&&fabs(objects.model[i].facenormals[whichhit].y)<.3){
3424                                     player[k].setAnimation(walljumprightanim);
3425                                     emit_sound_at(movewhooshsound, player[k].coords);
3426                                     if(k==0)pause_sound(whooshsound);
3427
3428                                     lowpointtarget=DoRotation(objects.model[i].facenormals[whichhit],0,objects.rotation[i],0);
3429                                     player[k].rotation=-asin(0-lowpointtarget.x)*180/M_PI;
3430                                     if(lowpointtarget.z<0)player[k].rotation=180-player[k].rotation;
3431                                     player[k].targetrotation=player[k].rotation;
3432                                     player[k].lowrotation=player[k].rotation;
3433                                     if(k==0)numwallflipped++;
3434                                 }
3435                                 else
3436                                 {
3437                                     lowpoint=tempcoords1;
3438                                     lowpointtarget=lowpoint+player[k].facing*2;
3439                                     whichhit=objects.model[i].LineCheck(&lowpoint,&lowpointtarget,&colpoint,&objects.position[i],&objects.rotation[i]);
3440                                     if(whichhit!=-1&&fabs(objects.model[i].facenormals[whichhit].y)<.3){
3441                                         player[k].setAnimation(walljumpbackanim);
3442                                         emit_sound_at(movewhooshsound, player[k].coords);
3443                                         if(k==0)pause_sound(whooshsound);
3444
3445                                         lowpointtarget=DoRotation(objects.model[i].facenormals[whichhit],0,objects.rotation[i],0);
3446                                         player[k].rotation=-asin(0-lowpointtarget.x)*180/M_PI;
3447                                         if(lowpointtarget.z<0)player[k].rotation=180-player[k].rotation;
3448                                         player[k].targetrotation=player[k].rotation;
3449                                         player[k].lowrotation=player[k].rotation;
3450                                         if(k==0)numwallflipped++;
3451                                     }
3452                                     else
3453                                     {
3454                                         lowpoint=tempcoords1;
3455                                         lowpointtarget=lowpoint-player[k].facing*2;
3456                                         whichhit=objects.model[i].LineCheck(&lowpoint,&lowpointtarget,&colpoint,&objects.position[i],&objects.rotation[i]);
3457                                         if(whichhit!=-1&&fabs(objects.model[i].facenormals[whichhit].y)<.3){
3458                                             player[k].setAnimation(walljumpfrontanim);
3459                                             emit_sound_at(movewhooshsound, player[k].coords);
3460                                             if(k==0)pause_sound(whooshsound);
3461
3462                                             lowpointtarget=DoRotation(objects.model[i].facenormals[whichhit],0,objects.rotation[i],0);
3463                                             player[k].rotation=-asin(0-lowpointtarget.x)*180/M_PI;
3464                                             if(lowpointtarget.z<0)player[k].rotation=180-player[k].rotation;
3465                                             player[k].rotation+=180;
3466                                             player[k].targetrotation=player[k].rotation;
3467                                             player[k].lowrotation=player[k].rotation;
3468                                             if(k==0)numwallflipped++;
3469                                         }
3470                                     }
3471                                 }
3472                             }
3473                         }
3474                     }
3475                 }
3476                 else if(objects.type[i]==rocktype){
3477                     lowpoint2=player[k].coords;
3478                     lowpoint=player[k].coords;
3479                     lowpoint.y+=2;
3480                     if(objects.model[i].LineCheck(&lowpoint,&lowpoint2,&colpoint,&objects.position[i],&objects.rotation[i])!=-1){
3481                         player[k].coords=colpoint;
3482                         player[k].collide=1;
3483                         tempcollide=1;
3484
3485                         if(player[k].targetanimation==jumpdownanim||player[k].isFlip()){
3486                             //flipped into a rock
3487                             if(player[k].isFlip()&&animation[player[k].targetanimation].label[player[k].targetframe]==7)
3488                                 player[k].RagDoll(0);
3489
3490                             if(player[k].targetanimation==jumpupanim){
3491                                 player[k].jumppower=-4;
3492                                 player[k].targetanimation=player[k].getIdle();
3493                             }
3494                             player[k].target=0;
3495                             player[k].targetframe=0;
3496                             player[k].onterrain=1;
3497
3498                             if(player[k].id==0){
3499                                 pause_sound(whooshsound);
3500                                 OPENAL_SetVolume(channels[whooshsound], 0);
3501                             }
3502
3503                             //landing
3504                             if((player[k].targetanimation==jumpdownanim||player[k].isFlip())&&!player[k].wasLanding()){
3505                                 if(player[k].isFlip())
3506                                     player[k].jumppower=-4;
3507                                 player[k].targetanimation=player[k].getLanding();
3508                                 emit_sound_at(landsound, player[k].coords, 128.);
3509                                 if(k==0){
3510                                     envsound[numenvsounds]=player[k].coords;
3511                                     envsoundvol[numenvsounds]=16;
3512                                     envsoundlife[numenvsounds]=.4;
3513                                     numenvsounds++;
3514                                 }
3515
3516                             }
3517                         }
3518                     }
3519                 }
3520             }
3521
3522             if(tempcollide&&(/*player[k].jumptogglekeydown*/1==1||player[k].aitype!=playercontrolled))
3523                 for(int l=0;l<terrain.patchobjectnum[player[k].whichpatchx][player[k].whichpatchz];l++){
3524                     int i=terrain.patchobjects[player[k].whichpatchx][player[k].whichpatchz][l];
3525                     lowpoint=player[k].coords;
3526                     lowpoint.y+=1.35;
3527                     if(objects.type[i]!=rocktype)
3528                         if(player[k].SphereCheck(&lowpoint,1.33,&colpoint,&objects.position[i],&objects.rotation[i],&objects.model[i])!=-1){
3529                             if(player[k].targetanimation!=jumpupanim&&
3530                                     player[k].targetanimation!=jumpdownanim&&
3531                                     player[k].onterrain)
3532                                 player[k].avoidcollided=1;
3533                             player[k].coords=lowpoint;
3534                             player[k].coords.y-=1.35;
3535                             player[k].collide=1;
3536
3537                             if((player[k].grabdelay<=0||player[k].aitype!=playercontrolled)&&
3538                                     (player[k].currentanimation!=climbanim&&
3539                                      player[k].currentanimation!=hanganim&&
3540                                      !player[k].isWallJump()||
3541                                      player[k].targetanimation==jumpupanim||
3542                                      player[k].targetanimation==jumpdownanim)){
3543                                 lowpoint=player[k].coords;
3544                                 objects.model[i].SphereCheckPossible(&lowpoint, 1.5, &objects.position[i], &objects.rotation[i]);
3545                                 lowpoint=player[k].coords;
3546                                 lowpoint.y+=.05;
3547                                 facing=0;
3548                                 facing.z=-1;
3549                                 facing=DoRotation(facing,0,player[k].targetrotation+180,0);
3550                                 lowpointtarget=lowpoint+facing*1.4;
3551                                 whichhit=objects.model[i].LineCheckPossible(&lowpoint,&lowpointtarget,&colpoint,&objects.position[i],&objects.rotation[i]);
3552                                 if(whichhit!=-1){
3553                                     lowpoint=player[k].coords;
3554                                     lowpoint.y+=.1;
3555                                     lowpointtarget=lowpoint+facing*1.4;
3556                                     lowpoint2=lowpoint;
3557                                     lowpointtarget2=lowpointtarget;
3558                                     lowpoint3=lowpoint;
3559                                     lowpointtarget3=lowpointtarget;
3560                                     lowpoint4=lowpoint;
3561                                     lowpointtarget4=lowpointtarget;
3562                                     lowpoint5=lowpoint;
3563                                     lowpointtarget5=lowpointtarget;
3564                                     lowpoint6=lowpoint;
3565                                     lowpointtarget6=lowpointtarget;
3566                                     lowpoint7=lowpoint;
3567                                     lowpointtarget7=lowpoint;
3568                                     lowpoint2.x+=.1;
3569                                     lowpointtarget2.x+=.1;
3570                                     lowpoint3.z+=.1;
3571                                     lowpointtarget3.z+=.1;
3572                                     lowpoint4.x-=.1;
3573                                     lowpointtarget4.x-=.1;
3574                                     lowpoint5.z-=.1;
3575                                     lowpointtarget5.z-=.1;
3576                                     lowpoint6.y+=45/13;
3577                                     lowpointtarget6.y+=45/13;
3578                                     lowpointtarget6+=facing*.6;
3579                                     lowpointtarget7.y+=90/13;
3580                                     whichhit=objects.model[i].LineCheckPossible(&lowpoint,&lowpointtarget,&colpoint,&objects.position[i],&objects.rotation[i]);
3581                                     if(objects.friction[i]>.5)
3582                                         if(whichhit!=-1){
3583                                             if(whichhit!=-1&&player[k].targetanimation!=jumpupanim&&player[k].targetanimation!=jumpdownanim)
3584                                                 player[k].collided=1;
3585                                             if(checkcollide(lowpoint7,lowpointtarget7)==-1)
3586                                                 if(checkcollide(lowpoint6,lowpointtarget6)==-1)
3587                                                     if(     objects.model[i].LineCheckPossible(&lowpoint2,&lowpointtarget2,
3588                                                                 &colpoint,&objects.position[i],&objects.rotation[i])!=-1&&
3589                                                             objects.model[i].LineCheckPossible(&lowpoint3,&lowpointtarget3,
3590                                                                 &colpoint,&objects.position[i],&objects.rotation[i])!=-1&&
3591                                                             objects.model[i].LineCheckPossible(&lowpoint4,&lowpointtarget4,
3592                                                                 &colpoint,&objects.position[i],&objects.rotation[i])!=-1&&
3593                                                             objects.model[i].LineCheckPossible(&lowpoint5,&lowpointtarget5,
3594                                                                 &colpoint,&objects.position[i],&objects.rotation[i])!=-1)
3595                                                         for(int j=0;j<45;j++){
3596                                                             lowpoint=player[k].coords;
3597                                                             lowpoint.y+=(float)j/13;
3598                                                             lowpointtarget=lowpoint+facing*1.4;
3599                                                             if(objects.model[i].LineCheckPossible(&lowpoint,&lowpointtarget,
3600                                                                         &colpoint2,&objects.position[i],&objects.rotation[i])==-1){
3601                                                                 if(j<=6||j<=25&&player[k].targetanimation==jumpdownanim)
3602                                                                     break;
3603                                                                 if(player[k].targetanimation==jumpupanim||player[k].targetanimation==jumpdownanim){
3604                                                                     lowpoint=DoRotation(objects.model[i].facenormals[whichhit],0,objects.rotation[k],0);
3605                                                                     lowpoint=player[k].coords;
3606                                                                     lowpoint.y+=(float)j/13;
3607                                                                     lowpointtarget=lowpoint+facing*1.3;
3608                                                                     flatfacing=player[k].coords;
3609                                                                     player[k].coords=colpoint-DoRotation(objects.model[i].facenormals[whichhit],0,objects.rotation[k],0)*.01;
3610                                                                     player[k].coords.y=lowpointtarget.y-.07;
3611                                                                     player[k].currentoffset=(flatfacing-player[k].coords)/player[k].scale;
3612
3613                                                                     if(j>10||!player[k].isRun()){
3614                                                                         if(player[k].targetanimation==jumpdownanim||player[k].targetanimation==jumpupanim){
3615                                                                             if(k==0)
3616                                                                                 pause_sound(whooshsound);
3617                                                                         }
3618                                                                         emit_sound_at(jumpsound, player[k].coords, 128.);
3619
3620                                                                         lowpointtarget=DoRotation(objects.model[i].facenormals[whichhit],0,objects.rotation[i],0);
3621                                                                         player[k].rotation=-asin(0-lowpointtarget.x)*180/M_PI;
3622                                                                         if(lowpointtarget.z<0)
3623                                                                             player[k].rotation=180-player[k].rotation;
3624                                                                         player[k].targetrotation=player[k].rotation;
3625                                                                         player[k].lowrotation=player[k].rotation;
3626
3627                                                                         //player[k].velocity=lowpointtarget*.03;
3628                                                                         player[k].velocity=0;
3629
3630                                                                         //climb ledge (?)
3631                                                                         if(player[k].targetanimation==jumpupanim){
3632                                                                             player[k].targetanimation=climbanim;
3633                                                                             player[k].jumppower=0;
3634                                                                             player[k].jumpclimb=1;
3635                                                                         }
3636                                                                         player[k].transspeed=6;
3637                                                                         player[k].target=0;
3638                                                                         player[k].targetframe=1;
3639                                                                         //hang ledge (?)
3640                                                                         if(j>25){
3641                                                                             player[k].setAnimation(hanganim);
3642                                                                             player[k].jumppower=0;
3643                                                                         }
3644                                                                     }
3645                                                                     break;
3646                                                                 }
3647                                                             }
3648                                                         }
3649                                         }
3650                                 }
3651                             }
3652                         }
3653                 }
3654             if(player[k].collide<=0){
3655                 //in the air
3656                 if(!player[k].onterrain&&
3657                         player[k].targetanimation!=jumpupanim&&
3658                         player[k].targetanimation!=jumpdownanim&&
3659                         player[k].targetanimation!=climbanim&&
3660                         player[k].targetanimation!=hanganim&&
3661                         !player[k].isWallJump()&&
3662                         !player[k].isFlip()){
3663                     if(player[k].currentanimation!=climbanim&&
3664                             player[k].currentanimation!=tempanim&&
3665                             player[k].targetanimation!=backhandspringanim&&
3666                             (player[k].targetanimation!=rollanim||
3667                              player[k].targetframe<2||
3668                              player[k].targetframe>6)){
3669                         //stagger off ledge (?)
3670                         if(player[k].targetanimation==staggerbackhighanim||player[k].targetanimation==staggerbackhardanim)
3671                             player[k].RagDoll(0);
3672                         player[k].setAnimation(jumpdownanim);
3673
3674                         if(!k)
3675                           emit_sound_at(whooshsound, player[k].coords, 128.);
3676                     }
3677                     //gravity
3678                     player[k].velocity.y+=gravity;
3679                 }
3680             }
3681         }
3682         player[k].realoldcoords=player[k].coords;
3683     }
3684 }
3685
3686 void Game::doAttacks(){
3687     static XYZ relative;
3688     static int randattack;
3689     static bool playerrealattackkeydown=0;
3690
3691     if(!Input::isKeyDown(attackkey))
3692         oldattackkey=0;
3693     if(oldattackkey)
3694         player[0].attackkeydown=0;
3695     if(oldattackkey)
3696         playerrealattackkeydown=0;
3697     if(!oldattackkey)
3698         playerrealattackkeydown=Input::isKeyDown(attackkey);
3699     if((player[0].parriedrecently<=0||
3700                 player[0].weaponactive==-1)&&
3701             (!oldattackkey||
3702              (realthreat&&
3703               player[0].lastattack!=swordslashanim&&
3704               player[0].lastattack!=knifeslashstartanim&&
3705               player[0].lastattack!=staffhitanim&&
3706               player[0].lastattack!=staffspinhitanim)))
3707         player[0].attackkeydown=Input::isKeyDown(attackkey);
3708     if(Input::isKeyDown(attackkey)&&
3709             !oldattackkey&&
3710             !player[0].backkeydown){
3711         for(int k=0;k<numplayers;k++){
3712             if((player[k].targetanimation==swordslashanim||
3713                         player[k].targetanimation==staffhitanim||
3714                         player[k].targetanimation==staffspinhitanim)&&
3715                     player[0].currentanimation!=dodgebackanim&&
3716                     !player[k].skeleton.free)
3717                 player[k].Reverse();
3718         }
3719     }
3720
3721     if(!hostile||indialogue!=-1)player[0].attackkeydown=0;
3722
3723     for(int k=0;k<numplayers;k++){
3724         if(indialogue!=-1)player[k].attackkeydown=0;
3725         if(player[k].targetanimation!=rabbitrunninganim&&player[k].targetanimation!=wolfrunninganim){
3726             if(player[k].aitype!=playercontrolled)
3727                 player[k].victim=&player[0];
3728             //attack key pressed
3729             if(player[k].attackkeydown){
3730                 //dodge backward
3731                 if(player[k].backkeydown&&
3732                         player[k].targetanimation!=backhandspringanim&&
3733                         (player[k].isIdle()||
3734                          player[k].isStop()||
3735                          player[k].isRun()||
3736                          player[k].targetanimation==walkanim)){
3737                     if(player[k].jumppower<=1){
3738                         player[k].jumppower-=2;
3739                     }else{
3740                         for(int i=0;i<numplayers;i++){
3741                             if(i==k)continue;
3742                             if(player[i].targetanimation==swordslashanim||
3743                                     player[i].targetanimation==knifeslashstartanim||
3744                                     player[i].targetanimation==staffhitanim||
3745                                     player[i].targetanimation==staffspinhitanim)
3746                                 if(findDistancefast(&player[k].coords,&player[i].coords)<6.5&&!player[i].skeleton.free){
3747                                     player[k].setAnimation(dodgebackanim);
3748                                     player[k].targetrotation=roughDirectionTo(player[k].coords,player[i].coords);
3749                                     player[k].targettilt2=pitchTo(player[k].coords,player[i].coords);
3750                                 }
3751                         }
3752                         if(player[k].targetanimation!=dodgebackanim){
3753                             if(k==0)numflipped++;
3754                             player[k].setAnimation(backhandspringanim);
3755                             player[k].targetrotation=-rotation+180;
3756                             if(player[k].leftkeydown)
3757                                 player[k].targetrotation-=45;
3758                             if(player[k].rightkeydown)
3759                                 player[k].targetrotation+=45;
3760                             player[k].rotation=player[k].targetrotation;
3761                             player[k].jumppower-=2;
3762                         }
3763                     }
3764                 }
3765                 //attack
3766                 if(!animation[player[k].targetanimation].attack&&
3767                         !player[k].backkeydown&&
3768                         (player[k].isIdle()||
3769                          player[k].isRun()||
3770                          player[k].targetanimation==walkanim||
3771                          player[k].targetanimation==sneakanim||
3772                          player[k].isCrouch())){
3773                     const int attackweapon=player[k].weaponactive==-1?0:weapons[player[k].weaponids[player[k].weaponactive]].getType();
3774                     //normal attacks (?)
3775                     player[k].hasvictim=0;
3776                     if(numplayers>1)
3777                         for(int i=0;i<numplayers;i++){
3778                             if(i==k||!(k==0||i==0))continue;
3779                             if(!player[k].hasvictim)
3780                                 if(animation[player[k].targetanimation].attack!=reversal){
3781                                     //choose an attack
3782                                     const float distance=findDistancefast(&player[k].coords,&player[i].coords);
3783                                     if(distance<4.5&&
3784                                             !player[i].skeleton.free&&
3785                                             player[i].howactive<typedead1&&
3786                                             player[i].targetanimation!=jumpreversedanim&&
3787                                             player[i].targetanimation!=rabbitkickreversedanim&&
3788                                             player[i].targetanimation!=rabbitkickanim&&
3789                                             player[k].targetanimation!=rabbitkickanim&&
3790                                             player[i].targetanimation!=getupfrombackanim&&
3791                                             (player[i].targetanimation!=staggerbackhighanim&&
3792                                              (player[i].targetanimation!=staggerbackhardanim||
3793                                               animation[staggerbackhardanim].label[player[i].targetframe]==6))&&
3794                                             player[i].targetanimation!=jumpdownanim&&
3795                                             player[i].targetanimation!=jumpupanim&&
3796                                             player[i].targetanimation!=getupfromfrontanim){
3797                                         player[k].victim=&player[i];
3798                                         player[k].hasvictim=1;
3799                                         if(player[k].aitype==playercontrolled){ //human player
3800                                             //sweep
3801                                             if(distance<2.5*sq(player[k].scale*5)&&
3802                                                     player[k].crouchkeydown&&
3803                                                     animation[player[i].targetanimation].height!=lowheight)
3804                                                 player[k].targetanimation=sweepanim;
3805                                             //winduppunch
3806                                             else if(distance<1.5*sq(player[k].scale*5)&&
3807                                                     animation[player[i].targetanimation].height!=lowheight&&
3808                                                     !player[k].forwardkeydown&&
3809                                                     !player[k].leftkeydown&&
3810                                                     !player[k].rightkeydown&&
3811                                                     !player[k].crouchkeydown&&
3812                                                     !attackweapon&&
3813                                                     !reversaltrain)
3814                                                 player[k].targetanimation=winduppunchanim;
3815                                             //upunch
3816                                             else if(distance<2.5*sq(player[k].scale*5)&&
3817                                                     animation[player[i].targetanimation].height!=lowheight&&
3818                                                     !player[k].forwardkeydown&&
3819                                                     !player[k].leftkeydown&&
3820                                                     !player[k].rightkeydown&&
3821                                                     !player[k].crouchkeydown&&
3822                                                     !attackweapon)
3823                                                 player[k].targetanimation=upunchanim;
3824                                             //knifefollow
3825                                             else if(distance<2.5*sq(player[k].scale*5)&&
3826                                                     player[i].staggerdelay>0&&
3827                                                     attackweapon==knife&&
3828                                                     player[i].bloodloss>player[i].damagetolerance/2)
3829                                                 player[k].targetanimation=knifefollowanim;
3830                                             //knifeslashstart
3831                                             else if(distance<2.5*sq(player[k].scale*5)&&
3832                                                     animation[player[i].targetanimation].height!=lowheight&&
3833                                                     !player[k].forwardkeydown&&
3834                                                     !player[k].leftkeydown&&
3835                                                     !player[k].rightkeydown&&
3836                                                     !player[k].crouchkeydown&&
3837                                                     attackweapon==knife&&
3838                                                     player[k].weaponmissdelay<=0)
3839                                                 player[k].targetanimation=knifeslashstartanim;
3840                                             //swordslash
3841                                             else if(distance<4.5*sq(player[k].scale*5)&&
3842                                                     animation[player[i].targetanimation].height!=lowheight&&
3843                                                     !player[k].crouchkeydown&&
3844                                                     attackweapon==sword&&
3845                                                     player[k].weaponmissdelay<=0)
3846                                                 player[k].targetanimation=swordslashanim;
3847                                             //staffhit
3848                                             else if(distance<4.5*sq(player[k].scale*5)&&
3849                                                     animation[player[i].targetanimation].height!=lowheight&&
3850                                                     !player[k].crouchkeydown&&
3851                                                     attackweapon==staff&&
3852                                                     player[k].weaponmissdelay<=0&&
3853                                                     !player[k].leftkeydown&&
3854                                                     !player[k].rightkeydown&&
3855                                                     !player[k].forwardkeydown)
3856                                                 player[k].targetanimation=staffhitanim;
3857                                             //staffspinhit
3858                                             else if(distance<4.5*sq(player[k].scale*5)&&
3859                                                     animation[player[i].targetanimation].height!=lowheight&&
3860                                                     !player[k].crouchkeydown&&
3861                                                     attackweapon==staff&&
3862                                                     player[k].weaponmissdelay<=0)
3863                                                 player[k].targetanimation=staffspinhitanim;
3864                                             //spinkick
3865                                             else if(distance<2.5*sq(player[k].scale*5)&&
3866                                                     animation[player[i].targetanimation].height!=lowheight)
3867                                                 player[k].targetanimation=spinkickanim;
3868                                             //lowkick
3869                                             else if(distance<2.5*sq(player[k].scale*5)&&
3870                                                     animation[player[i].targetanimation].height==lowheight&&
3871                                                     animation[player[k].targetanimation].attack!=normalattack)
3872                                                 player[k].targetanimation=lowkickanim;
3873                                         } else { //AI player
3874                                             if(distance<4.5*sq(player[k].scale*5)){
3875                                                 randattack=abs(Random()%5);
3876                                                 if(!attackweapon&&distance<2.5*sq(player[k].scale*5)){
3877                                                     //sweep
3878                                                     if(randattack==0&&animation[player[i].targetanimation].height!=lowheight)
3879                                                         player[k].targetanimation=sweepanim;
3880                                                     //upunch
3881                                                     else if(randattack==1&&animation[player[i].targetanimation].height!=lowheight&&
3882                                                             !attackweapon)
3883                                                         player[k].targetanimation=upunchanim;
3884                                                     //spinkick
3885                                                     else if(randattack==2&&animation[player[i].targetanimation].height!=lowheight)
3886                                                         player[k].targetanimation=spinkickanim;
3887                                                     //lowkick
3888                                                     else if(animation[player[i].targetanimation].height==lowheight)
3889                                                         player[k].targetanimation=lowkickanim;
3890                                                 }
3891                                                 if(attackweapon){
3892                                                     //sweep
3893                                                     if((tutoriallevel!=1||!attackweapon)&&
3894                                                             distance<2.5*sq(player[k].scale*5)&&
3895                                                             randattack==0&&
3896                                                             animation[player[i].targetanimation].height!=lowheight)
3897                                                         player[k].targetanimation=sweepanim;
3898                                                     //knifeslashstart
3899                                                     else if(distance<2.5*sq(player[k].scale*5)&&
3900                                                             attackweapon==knife&&
3901                                                             player[k].weaponmissdelay<=0)
3902                                                         player[k].targetanimation=knifeslashstartanim;
3903                                                     //swordslash
3904                                                     else if(!(player[0].victim==&player[i]&&
3905                                                                 player[0].hasvictim&&
3906                                                                 player[0].targetanimation==swordslashanim)&&
3907                                                             attackweapon==sword&&
3908                                                             player[k].weaponmissdelay<=0)
3909                                                         player[k].targetanimation=swordslashanim;
3910                                                     //staffhit
3911                                                     else if(!(player[0].victim==&player[i]&&
3912                                                                 player[0].hasvictim&&
3913                                                                 player[0].targetanimation==swordslashanim)&&
3914                                                             attackweapon==staff&&
3915                                                             player[k].weaponmissdelay<=0&&
3916                                                             randattack<3)
3917                                                         player[k].targetanimation=staffhitanim;
3918                                                     //staffspinhit
3919                                                     else if(!(player[0].victim==&player[i]&&
3920                                                                 player[0].hasvictim&&
3921                                                                 player[0].targetanimation==swordslashanim)&&
3922                                                             attackweapon==staff&&
3923                                                             player[k].weaponmissdelay<=0&&
3924                                                             randattack>=3)
3925                                                         player[k].targetanimation=staffspinhitanim;
3926                                                     //spinkick
3927                                                     else if((tutoriallevel!=1||!attackweapon)&&
3928                                                             distance<2.5*sq(player[k].scale*5)&&
3929                                                             randattack==1&&
3930                                                             animation[player[i].targetanimation].height!=lowheight)
3931                                                         player[k].targetanimation=spinkickanim;
3932                                                     //lowkick
3933                                                     else if(distance<2.5*sq(player[k].scale*5)&&
3934                                                             animation[player[i].targetanimation].height==lowheight&&
3935                                                             animation[player[k].targetanimation].attack!=normalattack)
3936                                                         player[k].targetanimation=lowkickanim;
3937                                                 }
3938                                             }
3939                                         }
3940                                         //upunch becomes wolfslap
3941                                         if(player[k].targetanimation==upunchanim&&player[k].creature==wolftype)
3942                                             player[k].targetanimation=wolfslapanim;
3943                                     }
3944                                     //sneak attacks
3945                                     if((k==0)&&(tutoriallevel!=1||tutorialstage==22)&&
3946                                             player[i].howactive<typedead1&&
3947                                             distance<1.5*sq(player[k].scale*5)&&
3948                                             !player[i].skeleton.free&&
3949                                             player[i].targetanimation!=getupfrombackanim&&
3950                                             player[i].targetanimation!=getupfromfrontanim&&
3951                                             (player[i].stunned>0&&player[k].madskills||
3952                                              player[i].surprised>0||
3953                                              player[i].aitype==passivetype||
3954                                              attackweapon&&player[i].stunned>0)&&
3955                                             normaldotproduct(player[i].facing,player[i].coords-player[k].coords)>0){
3956                                         //sneakattack
3957                                         if(!attackweapon){
3958                                             player[k].currentanimation=sneakattackanim;
3959                                             player[k].targetanimation=sneakattackanim;
3960                                             player[i].currentanimation=sneakattackedanim;
3961                                             player[i].targetanimation=sneakattackedanim;
3962                                             player[k].oldcoords=player[k].coords;
3963                                             player[k].coords=player[i].coords;
3964                                         }
3965                                         //knifesneakattack
3966                                         if(attackweapon==knife){
3967                                             player[k].currentanimation=knifesneakattackanim;
3968                                             player[k].targetanimation=knifesneakattackanim;
3969                                             player[i].currentanimation=knifesneakattackedanim;
3970                                             player[i].targetanimation=knifesneakattackedanim;
3971                                             player[i].oldcoords=player[i].coords;
3972                                             player[i].coords=player[k].coords;
3973                                         }
3974                                         //swordsneakattack
3975                                         if(attackweapon==sword){
3976                                             player[k].currentanimation=swordsneakattackanim;
3977                                             player[k].targetanimation=swordsneakattackanim;
3978                                             player[i].currentanimation=swordsneakattackedanim;
3979                                             player[i].targetanimation=swordsneakattackedanim;
3980                                             player[i].oldcoords=player[i].coords;
3981                                             player[i].coords=player[k].coords;
3982                                         }
3983                                         if(attackweapon!=staff){
3984                                             player[k].victim=&player[i];
3985                                             player[k].hasvictim=1;
3986                                             player[i].targettilt2=0;
3987                                             player[i].targetframe=1;
3988                                             player[i].currentframe=0;
3989                                             player[i].target=0;
3990                                             player[i].velocity=0;
3991                                             player[k].targettilt2=player[i].targettilt2;
3992                                             player[k].currentframe=player[i].currentframe;
3993                                             player[k].targetframe=player[i].targetframe;
3994                                             player[k].target=player[i].target;
3995                                             player[k].velocity=0;
3996                                             player[k].targetrotation=player[i].rotation;
3997                                             player[k].rotation=player[i].rotation;
3998                                             player[i].targetrotation=player[i].rotation;
3999                                         }
4000                                     }
4001                                     if(animation[player[k].targetanimation].attack==normalattack&&
4002                                             player[k].victim==&player[i]&&
4003                                             (!player[i].skeleton.free)){
4004                                         oldattackkey=1;
4005                                         player[k].targetframe=0;
4006                                         player[k].target=0;
4007
4008                                         player[k].targetrotation=roughDirectionTo(player[k].coords,player[i].coords);
4009                                         player[k].targettilt2=pitchTo(player[k].coords,player[i].coords);
4010                                         player[k].lastattack3=player[k].lastattack2;
4011                                         player[k].lastattack2=player[k].lastattack;
4012                                         player[k].lastattack=player[k].targetanimation;
4013                                     }
4014                                     if(player[k].targetanimation==knifefollowanim&&
4015                                             player[k].victim==&player[i]){
4016                                         oldattackkey=1;
4017                                         player[k].targetrotation=roughDirectionTo(player[k].coords,player[i].coords);
4018                                         player[k].targettilt2=pitchTo(player[k].coords,player[i].coords);
4019                                         player[k].victim=&player[i];
4020                                         player[k].hasvictim=1;
4021                                         player[i].targetanimation=knifefollowedanim;
4022                                         player[i].currentanimation=knifefollowedanim;
4023                                         player[i].targettilt2=0;
4024                                         player[i].targettilt2=player[k].targettilt2;
4025                                         player[i].targetframe=1;
4026                                         player[i].currentframe=0;
4027                                         player[i].target=0;
4028                                         player[i].velocity=0;
4029                                         player[k].currentanimation=knifefollowanim;
4030                                         player[k].targetanimation=knifefollowanim;
4031                                         player[k].targettilt2=player[i].targettilt2;
4032                                         player[k].currentframe=player[i].currentframe;
4033                                         player[k].targetframe=player[i].targetframe;
4034                                         player[k].target=player[i].target;
4035                                         player[k].velocity=0;
4036                                         player[k].oldcoords=player[k].coords;
4037                                         player[i].coords=player[k].coords;
4038                                         player[i].targetrotation=player[k].targetrotation;
4039                                         player[i].rotation=player[k].targetrotation;
4040                                         player[k].rotation=player[k].targetrotation;
4041                                         player[i].rotation=player[k].targetrotation;
4042                                     }
4043                                 }
4044                         }
4045                     const bool hasstaff=attackweapon==staff;
4046                     if(k==0&&numplayers>1)
4047                         for(int i=0;i<numplayers;i++){
4048                             if(i==k)continue;
4049                             if((playerrealattackkeydown||player[i].dead||!hasstaff)&&
4050                                     animation[player[k].targetanimation].attack==neutral){
4051                                 const float distance=findDistancefast(&player[k].coords,&player[i].coords);
4052                                 if(!player[i].dead||!realthreat||(!attackweapon&&player[k].crouchkeydown))
4053                                     if(player[i].skeleton.free)
4054                                         if(distance<3.5*sq(player[k].scale*5)&&
4055                                                 (player[i].dead||
4056                                                  player[i].skeleton.longdead>1000||
4057                                                  player[k].isRun()||
4058                                                  hasstaff||
4059                                                  (attackweapon&&
4060                                                   (player[i].skeleton.longdead>2000||
4061                                                    player[i].damage>player[i].damagetolerance/8||
4062                                                    player[i].bloodloss>player[i].damagetolerance/2)&&
4063                                                   distance<1.5*sq(player[k].scale*5)))){
4064                                             player[k].victim=&player[i];
4065                                             player[k].hasvictim=1;
4066                                             if(attackweapon&&tutoriallevel!=1){
4067                                                 //crouchstab
4068                                                 if(player[k].crouchkeydown&&attackweapon==knife&&distance<1.5*sq(player[k].scale*5))
4069                                                     player[k].targetanimation=crouchstabanim;
4070                                                 //swordgroundstab
4071                                                 if(player[k].crouchkeydown&&distance<1.5*sq(player[k].scale*5)&&attackweapon==sword)
4072                                                     player[k].targetanimation=swordgroundstabanim;
4073                                                 //staffgroundsmash
4074                                                 if(distance<3.5*sq(player[k].scale*5)&&attackweapon==staff)
4075                                                     player[k].targetanimation=staffgroundsmashanim;
4076                                             }
4077                                             if(distance<2.5&&
4078                                                     player[k].crouchkeydown&&
4079                                                     player[k].targetanimation!=crouchstabanim&&
4080                                                     !attackweapon&&
4081                                                     player[i].dead&&
4082                                                     player[i].skeleton.free&&
4083                                                     player[i].skeleton.longdead>1000){
4084                                                 player[k].targetanimation=killanim;
4085                                                 //TODO: refactor this out, what does it do?
4086                                                 for(int j=0;j<terrain.numdecals;j++){
4087                                                     if((terrain.decaltype[j]==blooddecal||terrain.decaltype[j]==blooddecalslow)&&
4088                                                             terrain.decalalivetime[j]<2)
4089                                                         terrain.DeleteDecal(j);
4090                                                 }
4091                                                 for(int l=0;l<objects.numobjects;l++){
4092                                                     if(objects.model[l].type==decalstype)
4093                                                         for(int j=0;j<objects.model[l].numdecals;j++){
4094                                                             if((objects.model[l].decaltype[j]==blooddecal||
4095                                                                     objects.model[l].decaltype[j]==blooddecalslow)&&
4096                                                                     objects.model[l].decalalivetime[j]<2)
4097                                                                 objects.model[l].DeleteDecal(j);
4098                                                         }
4099                                                 }
4100                                             }
4101                                             if(!player[i].dead||musictype!=2)
4102                                                 if(distance<3.5&&
4103                                                         (player[k].isRun()||player[k].isIdle()&&player[k].attackkeydown)&&
4104                                                         player[k].staggerdelay<=0&&
4105                                                         (player[i].dead||
4106                                                          player[i].skeleton.longdead<300&&
4107                                                          player[k].lastattack!=spinkickanim&&
4108                                                          player[i].skeleton.free)&&
4109                                                         (!player[i].dead||musictype!=stream_fighttheme)){
4110                                                     player[k].targetanimation=dropkickanim;
4111                                                     for(int j=0;j<terrain.numdecals;j++){
4112                                                         if((terrain.decaltype[j]==blooddecal||terrain.decaltype[j]==blooddecalslow)&&
4113                                                                 terrain.decalalivetime[j]<2){
4114                                                             terrain.DeleteDecal(j);
4115                                                         }
4116                                                     }
4117                                                     for(int l=0;l<objects.numobjects;l++){
4118                                                         if(objects.model[l].type==decalstype)
4119                                                             for(int j=0;j<objects.model[l].numdecals;j++){
4120                                                                 if((objects.model[l].decaltype[j]==blooddecal||
4121                                                                         objects.model[l].decaltype[j]==blooddecalslow)&&
4122                                                                         objects.model[l].decalalivetime[j]<2){
4123                                                                     objects.model[l].DeleteDecal(j);
4124                                                                 }
4125                                                             }
4126                                                     }
4127                                                 }
4128                                         }
4129                                 if(animation[player[k].targetanimation].attack==normalattack&&
4130                                         player[k].victim==&player[i]&&
4131                                         (!player[i].skeleton.free||
4132                                          player[k].targetanimation==killanim||
4133                                          player[k].targetanimation==crouchstabanim||
4134                                          player[k].targetanimation==swordgroundstabanim||
4135                                          player[k].targetanimation==staffgroundsmashanim||
4136                                          player[k].targetanimation==dropkickanim)){
4137                                     oldattackkey=1;
4138                                     player[k].targetframe=0;
4139                                     player[k].target=0;
4140
4141                                     XYZ targetpoint=player[i].coords;
4142                                     if(player[k].targetanimation==crouchstabanim||
4143                                             player[k].targetanimation==swordgroundstabanim||
4144                                             player[k].targetanimation==staffgroundsmashanim){
4145                                         targetpoint+=(player[i].getJointFor(abdomen).position+
4146                                                  player[i].getJointFor(neck).position)/2*
4147                                                 player[i].scale;
4148                                     }
4149                                     player[k].targetrotation=roughDirectionTo(player[k].coords,targetpoint);
4150                                     player[k].targettilt2=pitchTo(player[k].coords,targetpoint);
4151
4152                                     if(player[k].targetanimation==crouchstabanim||player[k].targetanimation==swordgroundstabanim){
4153                                         player[k].targetrotation+=(float)(abs(Random()%100)-50)/4;
4154                                     }
4155
4156                                     if(player[k].targetanimation==staffgroundsmashanim)
4157                                         player[k].targettilt2+=10;
4158
4159                                     player[k].lastattack3=player[k].lastattack2;
4160                                     player[k].lastattack2=player[k].lastattack;
4161                                     player[k].lastattack=player[k].targetanimation;
4162
4163                                     if(player[k].targetanimation==swordgroundstabanim){
4164                                         player[k].targetrotation+=30;
4165                                     }
4166                                 }
4167                             }
4168                         }
4169                     if(!player[k].hasvictim){
4170                         //find victim
4171                         for(int i=0;i<numplayers;i++){
4172                             if(i==k||!(i==0||k==0))continue;
4173                             if(!player[i].skeleton.free){
4174                                 if(player[k].hasvictim){
4175                                     if(findDistancefast(&player[k].coords,&player[i].coords)<
4176                                        findDistancefast(&player[k].coords,&player[k].victim->coords))
4177                                         player[k].victim=&player[i];
4178                                 }else{
4179                                     player[k].victim=&player[i];
4180                                     player[k].hasvictim=1;
4181                                 }
4182                             }
4183                         }
4184                     }
4185                     if(player[k].aitype==playercontrolled)
4186                         //rabbit kick
4187                         if(player[k].attackkeydown&&
4188                                 player[k].isRun()&&
4189                                 player[k].wasRun()&&
4190                                 ((player[k].hasvictim&&
4191                                   findDistancefast(&player[k].coords,&player[k].victim->coords)<12*sq(player[k].scale*5)&&
4192                                   findDistancefast(&player[k].coords,&player[k].victim->coords)>7*sq(player[k].scale*5)&&
4193                                   !player[k].victim->skeleton.free&&
4194                                   player[k].victim->targetanimation!=getupfrombackanim&&
4195                                   player[k].victim->targetanimation!=getupfromfrontanim&&
4196                                   animation[player[k].victim->targetanimation].height!=lowheight&&
4197                                   player[k].aitype!=playercontrolled&& //wat???
4198                                   normaldotproduct(player[k].facing,player[k].victim->coords-player[k].coords)>0&&
4199                                   player[k].rabbitkickenabled)||
4200                                  player[k].jumpkeydown)){
4201                             oldattackkey=1;
4202                             player[k].setAnimation(rabbitkickanim);
4203                         }
4204                     //update counts
4205                     if(animation[player[k].targetanimation].attack&&k==0){
4206                         numattacks++;
4207                         switch(attackweapon){
4208                             case 0: numunarmedattack++; break;
4209                             case knife: numknifeattack++; break;
4210                             case sword: numswordattack++; break;
4211                             case staff: numstaffattack++; break;
4212                         }
4213                     }
4214                 }
4215             }
4216         }
4217     }
4218 }
4219
4220 void Game::doPlayerCollisions(){
4221         static XYZ rotatetarget;
4222     static float collisionradius;
4223     if(numplayers>1)
4224         for(int k=0;k<numplayers;k++)
4225             for(int i=k+1;i<numplayers;i++){
4226                 //neither player is part of a reversal
4227                 if((animation[player[i].targetanimation].attack!=reversed&&
4228                             animation[player[i].targetanimation].attack!=reversal&&
4229                             animation[player[k].targetanimation].attack!=reversed&&
4230                             animation[player[k].targetanimation].attack!=reversal)||(i!=0&&k!=0))
4231                 if((animation[player[i].currentanimation].attack!=reversed&&
4232                             animation[player[i].currentanimation].attack!=reversal&&
4233                             animation[player[k].currentanimation].attack!=reversed&&
4234                             animation[player[k].currentanimation].attack!=reversal)||(i!=0&&k!=0))
4235                 //neither is sleeping
4236                 if(player[i].howactive<=typesleeping&&player[k].howactive<=typesleeping)
4237                 if(player[i].howactive!=typesittingwall&&player[k].howactive!=typesittingwall)
4238                 //in same patch, neither is climbing
4239                 if(player[i].whichpatchx==player[k].whichpatchx&&
4240                         player[i].whichpatchz==player[k].whichpatchz&&
4241                         player[k].skeleton.oldfree==player[k].skeleton.free&&
4242                         player[i].skeleton.oldfree==player[i].skeleton.free&&
4243                         player[i].targetanimation!=climbanim&&
4244                         player[i].targetanimation!=hanganim&&
4245                         player[k].targetanimation!=climbanim&&
4246                         player[k].targetanimation!=hanganim)
4247                 //players are close (bounding box test)
4248                 if(player[i].coords.y>player[k].coords.y-3)
4249                 if(player[i].coords.y<player[k].coords.y+3)
4250                 if(player[i].coords.x>player[k].coords.x-3)
4251                 if(player[i].coords.x<player[k].coords.x+3)
4252                 if(player[i].coords.z>player[k].coords.z-3)
4253                 if(player[i].coords.z<player[k].coords.z+3){
4254                     //spread fire from player to player
4255                     if(findDistancefast(&player[i].coords,&player[k].coords)
4256                             <3*sq((player[i].scale+player[k].scale)*2.5)){
4257                         if(player[i].onfire||player[k].onfire){
4258                             if(!player[i].onfire)player[i].CatchFire();
4259                             if(!player[k].onfire)player[k].CatchFire();
4260                         }
4261                     }
4262
4263                     XYZ tempcoords1=player[i].coords;
4264                     XYZ tempcoords2=player[k].coords;
4265                     if(!player[i].skeleton.oldfree)
4266                         tempcoords1.y+=player[i].getJointFor(abdomen).position.y*player[i].scale;
4267                     if(!player[k].skeleton.oldfree)
4268                         tempcoords2.y+=player[k].getJointFor(abdomen).position.y*player[k].scale;
4269                     collisionradius=1.2*sq((player[i].scale+player[k].scale)*2.5);
4270                     if(player[0].hasvictim)
4271                         if(player[0].targetanimation==rabbitkickanim&&(k==0||i==0)&&!player[0].victim->skeleton.free)
4272                             collisionradius=3;
4273                     if((!player[i].skeleton.oldfree||!player[k].skeleton.oldfree)&&
4274                             (findDistancefast(&tempcoords1,&tempcoords2)<collisionradius||
4275                              findDistancefast(&player[i].coords,&player[k].coords)<collisionradius)){
4276                         //jump down on a dead body
4277                         if(k==0||i==0){
4278                             int l=i?i:k;
4279                             if(player[0].targetanimation==jumpdownanim&&
4280                                     !player[0].skeleton.oldfree&&
4281                                     !player[0].skeleton.free&&
4282                                     player[l].skeleton.oldfree&&
4283                                     player[l].skeleton.free&&
4284                                     player[l].dead&&
4285                                     player[0].lastcollide<=0&&
4286                                     fabs(player[l].coords.y-player[0].coords.y)<.2&&
4287                                     findDistancefast(&player[0].coords,&player[l].coords)<.7*sq((player[l].scale+player[0].scale)*2.5)){
4288                                 player[0].coords.y=player[l].coords.y;
4289                                 player[l].velocity=player[0].velocity;
4290                                 player[l].skeleton.free=0;
4291                                 player[l].rotation=0;
4292                                 player[l].RagDoll(0);
4293                                 player[l].DoDamage(20);
4294                                 camerashake+=.3;
4295                                 player[l].skeleton.longdead=0;
4296                                 player[0].lastcollide=1;
4297                             }
4298                         }
4299
4300                         if(     (player[i].skeleton.oldfree==1&&findLengthfast(&player[i].velocity)>1)||
4301                                 (player[k].skeleton.oldfree==1&&findLengthfast(&player[k].velocity)>1)||
4302                                 (player[i].skeleton.oldfree==0&&player[k].skeleton.oldfree==0)){
4303                             rotatetarget=player[k].velocity-player[i].velocity;
4304                             if((player[i].targetanimation!=getupfrombackanim&&player[i].targetanimation!=getupfromfrontanim||
4305                                         player[i].skeleton.free)&&
4306                                     (player[k].targetanimation!=getupfrombackanim&&player[k].targetanimation!=getupfromfrontanim||
4307                                      player[k].skeleton.free))
4308                                 if((((k!=0&&findLengthfast(&rotatetarget)>150||
4309                                                     k==0&&findLengthfast(&rotatetarget)>50&&player[0].rabbitkickragdoll)&&
4310                                                 normaldotproduct(rotatetarget,player[k].coords-player[i].coords)>0)&&
4311                                             (k==0||
4312                                              k!=0&&player[i].skeleton.oldfree==1&&animation[player[k].currentanimation].attack==neutral||
4313                                          /*i!=0&&*/player[k].skeleton.oldfree==1&&animation[player[i].currentanimation].attack==neutral))||
4314                                         (player[i].targetanimation==jumpupanim||player[i].targetanimation==jumpdownanim||player[i].isFlip())&&
4315                                         (player[k].targetanimation==jumpupanim||player[k].targetanimation==jumpdownanim||player[k].isFlip())&&
4316                                         k==0&&!player[i].skeleton.oldfree&&!player[k].skeleton.oldfree){
4317                                     //If hit by body
4318                                     if(     (i!=0||player[i].skeleton.free)&&
4319                                             (k!=0||player[k].skeleton.free)||
4320                                             (animation[player[i].targetanimation].height==highheight&&
4321                                              animation[player[k].targetanimation].height==highheight)){
4322                                         if(tutoriallevel!=1){
4323                                             emit_sound_at(heavyimpactsound, player[i].coords);
4324                                         }
4325
4326                                         player[i].RagDoll(0);
4327                                         if(player[i].damage>player[i].damagetolerance-findLengthfast(&rotatetarget)/4&&!player[i].dead){
4328                                           award_bonus(0, aimbonus);
4329                                         }
4330                                         player[i].DoDamage(findLengthfast(&rotatetarget)/4);
4331                                         player[k].RagDoll(0);
4332                                         if(player[k].damage>player[k].damagetolerance-findLengthfast(&rotatetarget)/4&&!player[k].dead){
4333                                           award_bonus(0, aimbonus); // Huh, again?
4334                                         }
4335                                         player[k].DoDamage(findLengthfast(&rotatetarget)/4);
4336
4337                                         for(int j=0;j<player[i].skeleton.num_joints;j++){
4338                                             player[i].skeleton.joints[j].velocity=player[i].skeleton.joints[j].velocity/5+player[k].velocity;
4339                                         }
4340                                         for(int j=0;j<player[k].skeleton.num_joints;j++){
4341                                             player[k].skeleton.joints[j].velocity=player[k].skeleton.joints[j].velocity/5+player[i].velocity;
4342                                         }
4343
4344                                     }
4345                                 }
4346                             if(     (animation[player[i].targetanimation].attack==neutral||
4347                                      animation[player[i].targetanimation].attack==normalattack)&&
4348                                     (animation[player[k].targetanimation].attack==neutral||
4349                                      animation[player[k].targetanimation].attack==normalattack)){
4350                                 //If bumped
4351                                 if(player[i].skeleton.oldfree==0&&player[k].skeleton.oldfree==0){
4352                                     if(findDistancefast(&player[k].coords,&player[i].coords)<.5*sq((player[i].scale+player[k].scale)*2.5)){
4353                                         rotatetarget=player[k].coords-player[i].coords;
4354                                         Normalise(&rotatetarget);
4355                                         player[k].coords=(player[k].coords+player[i].coords)/2;
4356                                         player[i].coords=player[k].coords-rotatetarget*fast_sqrt(.6)/2
4357                                             *sq((player[i].scale+player[k].scale)*2.5);
4358                                         player[k].coords+=rotatetarget*fast_sqrt(.6)/2*sq((player[i].scale+player[k].scale)*2.5);
4359                                         if(player[k].howactive==typeactive||hostile)
4360                                             if(player[k].isIdle()){
4361                                                 if(player[k].howactive<typesleeping)
4362                                                     player[k].setAnimation(player[k].getStop());
4363                                                 else if(player[k].howactive==typesleeping)
4364                                                     player[k].setAnimation(getupfromfrontanim);
4365                                                 if(!editorenabled)
4366                                                     player[k].howactive=typeactive;
4367                                             }
4368                                         if(player[i].howactive==typeactive||hostile)
4369                                             if(player[i].isIdle()){
4370                                                 if(player[i].howactive<typesleeping)
4371                                                     player[i].setAnimation(player[k].getStop());
4372                                                 else
4373                                                     player[i].setAnimation(getupfromfrontanim);
4374                                                 if(!editorenabled)
4375                                                     player[i].howactive=typeactive;
4376                                             }
4377                                     }
4378                                     //jump down on player
4379                                     if(hostile){
4380                                         if(k==0&&i!=0&&player[k].targetanimation==jumpdownanim&&
4381                                                 !player[i].isCrouch()&&
4382                                                 player[i].targetanimation!=rollanim&&
4383                                                 !player[k].skeleton.oldfree&&!
4384                                                 player[k].skeleton.free&&
4385                                                 player[k].lastcollide<=0&&
4386                                                 player[k].velocity.y<-10){
4387                                             player[i].velocity=player[k].velocity;
4388                                             player[k].velocity=player[k].velocity*-.5;
4389                                             player[k].velocity.y=player[i].velocity.y;
4390                                             player[i].DoDamage(20);
4391                                             player[i].RagDoll(0);
4392                                             player[k].lastcollide=1;
4393                                             award_bonus(k, AboveBonus);
4394                                         }
4395                                         if(i==0&&k!=0&&player[i].targetanimation==jumpdownanim&&
4396                                                 !player[k].isCrouch()&&
4397                                                 player[k].targetanimation!=rollanim&&
4398                                                 !player[i].skeleton.oldfree&&
4399                                                 !player[i].skeleton.free&&
4400                                                 player[i].lastcollide<=0&&
4401                                                 player[i].velocity.y<-10){
4402                                             player[k].velocity=player[i].velocity;
4403                                             player[i].velocity=player[i].velocity*-.3;
4404                                             player[i].velocity.y=player[k].velocity.y;
4405                                             player[k].DoDamage(20);
4406                                             player[k].RagDoll(0);
4407                                             player[i].lastcollide=1;
4408                                             award_bonus(i, AboveBonus);
4409                                         }
4410                                     }
4411                                 }
4412                             }
4413                         }
4414                         player[i].CheckKick();
4415                         player[k].CheckKick();
4416                     }
4417                 }
4418             }
4419 }
4420
4421 void Game::doAI(int i){
4422     static bool connected;
4423     if(player[i].aitype!=playercontrolled&&indialogue==-1){
4424         player[i].jumpclimb=0;
4425         //disable movement in editor
4426         if(editorenabled)
4427             player[i].stunned=1;
4428
4429         player[i].pause=0;
4430         if(findDistancefastflat(&player[0].coords,&player[i].coords)<30&&
4431                 player[0].coords.y>player[i].coords.y+2&&
4432                 !player[0].onterrain)
4433             player[i].pause=1;
4434
4435         //pathfinding
4436         if(player[i].aitype==pathfindtype){
4437             if(player[i].finalpathfindpoint==-1){
4438                 float closestdistance;
4439                 float tempdist;
4440                 int closest;
4441                 XYZ colpoint;
4442                 closest=-1;
4443                 closestdistance=-1;
4444                 for(int j=0;j<numpathpoints;j++)
4445                     if(closest==-1||findDistancefast(&player[i].finalfinaltarget,&pathpoint[j])<closestdistance){
4446                         closestdistance=findDistancefast(&player[i].finalfinaltarget,&pathpoint[j]);
4447                         closest=j;
4448                         player[i].finaltarget=pathpoint[j];
4449                     }
4450                 player[i].finalpathfindpoint=closest;
4451                 for(int j=0;j<numpathpoints;j++)
4452                     for(int k=0;k<numpathpointconnect[j];k++){
4453                         DistancePointLine(&player[i].finalfinaltarget, &pathpoint[j], &pathpoint[pathpointconnect[j][k]], &tempdist,&colpoint );
4454                         if(sq(tempdist)<closestdistance)
4455                             if(findDistance(&colpoint,&pathpoint[j])+findDistance(&colpoint,&pathpoint[pathpointconnect[j][k]])<
4456                                     findDistance(&pathpoint[j],&pathpoint[pathpointconnect[j][k]])+.1){
4457                                 closestdistance=sq(tempdist);
4458                                 closest=j;
4459                                 player[i].finaltarget=colpoint;
4460                             }
4461                     }
4462                 player[i].finalpathfindpoint=closest;
4463
4464             }
4465             if(player[i].targetpathfindpoint==-1){
4466                 float closestdistance;
4467                 float tempdist;
4468                 int closest;
4469                 XYZ colpoint;
4470                 closest=-1;
4471                 closestdistance=-1;
4472                 if(player[i].lastpathfindpoint==-1){
4473                     for(int j=0;j<numpathpoints;j++){
4474                         if(j!=player[i].lastpathfindpoint)
4475                             if(closest==-1||(findDistancefast(&player[i].coords,&pathpoint[j])<closestdistance)){
4476                                 closestdistance=findDistancefast(&player[i].coords,&pathpoint[j]);
4477                                 closest=j;
4478                             }
4479                     }
4480                     player[i].targetpathfindpoint=closest;
4481                     for(int j=0;j<numpathpoints;j++)
4482                         if(j!=player[i].lastpathfindpoint)
4483                             for(int k=0;k<numpathpointconnect[j];k++){
4484                                 DistancePointLine(&player[i].coords, &pathpoint[j], &pathpoint[pathpointconnect[j][k]], &tempdist,&colpoint );
4485                                 if(sq(tempdist)<closestdistance){
4486                                     if(findDistance(&colpoint,&pathpoint[j])+findDistance(&colpoint,&pathpoint[pathpointconnect[j][k]])<
4487                                             findDistance(&pathpoint[j],&pathpoint[pathpointconnect[j][k]])+.1){
4488                                         closestdistance=sq(tempdist);
4489                                         closest=j;
4490                                     }
4491                                 }
4492                             }
4493                     player[i].targetpathfindpoint=closest;
4494                 }
4495                 else
4496                 {
4497                     for(int j=0;j<numpathpoints;j++)
4498                         if(j!=player[i].lastpathfindpoint&&
4499                                 j!=player[i].lastpathfindpoint2&&
4500                                 j!=player[i].lastpathfindpoint3&&
4501                                 j!=player[i].lastpathfindpoint4){
4502                             connected=0;
4503                             if(numpathpointconnect[j])
4504                                 for(int k=0;k<numpathpointconnect[j];k++)
4505                                     if(pathpointconnect[j][k]==player[i].lastpathfindpoint)
4506                                         connected=1;
4507                             if(!connected)
4508                                 if(numpathpointconnect[player[i].lastpathfindpoint])
4509                                     for(int k=0;k<numpathpointconnect[player[i].lastpathfindpoint];k++)
4510                                         if(pathpointconnect[player[i].lastpathfindpoint][k]==j)
4511                                             connected=1;
4512                             if(connected){
4513                                 tempdist=findPathDist(j,player[i].finalpathfindpoint);
4514                                 if(closest==-1||tempdist<closestdistance){
4515                                     closestdistance=tempdist;
4516                                     closest=j;
4517                                 }
4518                             }
4519                         }
4520                     player[i].targetpathfindpoint=closest;
4521                 }
4522             }
4523             player[i].losupdatedelay-=multiplier;
4524
4525             player[i].targetrotation=roughDirectionTo(player[i].coords,pathpoint[player[i].targetpathfindpoint]);
4526             player[i].lookrotation=player[i].targetrotation;
4527
4528             //reached target point
4529             if(findDistancefastflat(&player[i].coords,&pathpoint[player[i].targetpathfindpoint])<.6){
4530                 player[i].lastpathfindpoint4=player[i].lastpathfindpoint3;
4531                 player[i].lastpathfindpoint3=player[i].lastpathfindpoint2;
4532                 player[i].lastpathfindpoint2=player[i].lastpathfindpoint;
4533                 player[i].lastpathfindpoint=player[i].targetpathfindpoint;
4534                 if(player[i].lastpathfindpoint2==-1)
4535                     player[i].lastpathfindpoint2=player[i].lastpathfindpoint;
4536                 if(player[i].lastpathfindpoint3==-1)
4537                     player[i].lastpathfindpoint3=player[i].lastpathfindpoint2;
4538                 if(player[i].lastpathfindpoint4==-1)
4539                     player[i].lastpathfindpoint4=player[i].lastpathfindpoint3;
4540                 player[i].targetpathfindpoint=-1;
4541             }
4542             if(     findDistancefastflat(&player[i].coords,&player[i].finalfinaltarget)<
4543                     findDistancefastflat(&player[i].coords,&player[i].finaltarget)||
4544                     findDistancefastflat(&player[i].coords,&player[i].finaltarget)<.6*sq(player[i].scale*5)||
4545                     player[i].lastpathfindpoint==player[i].finalpathfindpoint) {
4546                 player[i].aitype=passivetype;
4547             }
4548
4549             player[i].forwardkeydown=1;
4550             player[i].leftkeydown=0;
4551             player[i].backkeydown=0;
4552             player[i].rightkeydown=0;
4553             player[i].crouchkeydown=0;
4554             player[i].attackkeydown=0;
4555             player[i].throwkeydown=0;
4556
4557             if(player[i].avoidcollided>.8 && !player[i].jumpkeydown && player[i].collided<.8)
4558                 player[i].targetrotation+=90*(player[i].whichdirection*2-1);
4559
4560             if(player[i].collided<1||player[i].targetanimation!=jumpupanim)
4561                 player[i].jumpkeydown=0;
4562             if((player[i].collided>.8&&player[i].jumppower>=5))
4563                 player[i].jumpkeydown=1;
4564
4565             if((tutoriallevel!=1||cananger)&&
4566                     hostile&&
4567                     !player[0].dead&&
4568                     findDistancefast(&player[i].coords,&player[0].coords)<400&&
4569                     player[i].occluded<25){
4570                 if(findDistancefast(&player[i].coords,&player[0].coords)<12&&
4571                         animation[player[0].targetanimation].height!=lowheight&&
4572                         !editorenabled&&
4573                         (player[0].coords.y<player[i].coords.y+5||player[0].onterrain))
4574                     player[i].aitype=attacktypecutoff;
4575                 if(findDistancefast(&player[i].coords,&player[0].coords)<30&&
4576                         animation[player[0].targetanimation].height==highheight&&
4577                         !editorenabled)
4578                     player[i].aitype=attacktypecutoff;
4579
4580                 if(player[i].losupdatedelay<0&&!editorenabled&&player[i].occluded<2){
4581                     player[i].losupdatedelay=.2;
4582                     for(int j=0;j<numplayers;j++)
4583                         if(j==0||player[j].skeleton.free||player[j].aitype!=passivetype)
4584                             if(abs(Random()%2)||animation[player[j].targetanimation].height!=lowheight||j!=0)
4585                                 if(findDistancefast(&player[i].coords,&player[j].coords)<400)
4586                                     if(normaldotproduct(player[i].facing,player[j].coords-player[i].coords)>0)
4587                                         if(player[j].coords.y<player[i].coords.y+5||player[j].onterrain)
4588                                             if(!player[j].isWallJump()&&-1==checkcollide(
4589                                                             DoRotation(player[i].getJointFor(head).position,0,player[i].rotation,0)
4590                                                                 *player[i].scale+player[i].coords,
4591                                                             DoRotation(player[j].getJointFor(head).position,0,player[j].rotation,0)
4592                                                                 *player[j].scale+player[j].coords)||
4593                                                     (player[j].targetanimation==hanganim&&
4594                                                      normaldotproduct(player[j].facing,player[i].coords-player[j].coords)<0)){
4595                                                 player[i].aitype=searchtype;
4596                                                 player[i].lastchecktime=12;
4597                                                 player[i].lastseen=player[j].coords;
4598                                                 player[i].lastseentime=12;
4599                                             }
4600                 }
4601             }
4602             if(player[i].aitype==attacktypecutoff&&musictype!=2)
4603                 if(player[i].creature!=wolftype){
4604                     player[i].stunned=.6;
4605                     player[i].surprised=.6;
4606                 }
4607         }
4608
4609         if(player[i].aitype!=passivetype&&leveltime>.5)
4610             player[i].howactive=typeactive;
4611
4612         if(player[i].aitype==passivetype){
4613             player[i].aiupdatedelay-=multiplier;
4614             player[i].losupdatedelay-=multiplier;
4615             player[i].lastseentime+=multiplier;
4616             player[i].pausetime-=multiplier;
4617             if(player[i].lastseentime>1)
4618                 player[i].lastseentime=1;
4619
4620             if(player[i].aiupdatedelay<0){
4621                 if(player[i].numwaypoints>1&&player[i].howactive==typeactive&&player[i].pausetime<=0){
4622                     player[i].targetrotation=roughDirectionTo(player[i].coords,player[i].waypoints[player[i].waypoint]);
4623                     player[i].lookrotation=player[i].targetrotation;
4624                     player[i].aiupdatedelay=.05;
4625
4626                     if(findDistancefastflat(&player[i].coords,&player[i].waypoints[player[i].waypoint])<1){
4627                         if(player[i].waypointtype[player[i].waypoint]==wppause)
4628                             player[i].pausetime=4;
4629                         player[i].waypoint++;
4630                         if(player[i].waypoint>player[i].numwaypoints-1)
4631                             player[i].waypoint=0;
4632
4633                     }
4634                 }
4635
4636                 if(player[i].numwaypoints>1&&player[i].howactive==typeactive&&player[i].pausetime<=0)
4637                     player[i].forwardkeydown=1;
4638                 else
4639                     player[i].forwardkeydown=0;
4640                 player[i].leftkeydown=0;
4641                 player[i].backkeydown=0;
4642                 player[i].rightkeydown=0;
4643                 player[i].crouchkeydown=0;
4644                 player[i].attackkeydown=0;
4645                 player[i].throwkeydown=0;
4646
4647                 if(player[i].avoidcollided>.8&&!player[i].jumpkeydown&&player[i].collided<.8){
4648                     if(!player[i].avoidsomething)
4649                         player[i].targetrotation+=90*(player[i].whichdirection*2-1);
4650                     else{
4651                         XYZ leftpos,rightpos;
4652                         float leftdist,rightdist;
4653                         leftpos = player[i].coords+DoRotation(player[i].facing,0,90,0);
4654                         rightpos = player[i].coords-DoRotation(player[i].facing,0,90,0);
4655                         leftdist = findDistancefast(&leftpos, &player[i].avoidwhere);
4656                         rightdist = findDistancefast(&rightpos, &player[i].avoidwhere);
4657                         if(leftdist<rightdist)
4658                             player[i].targetrotation+=90;
4659                         else
4660                             player[i].targetrotation-=90;
4661                     }
4662                 }
4663             }
4664             if(player[i].collided<1||player[i].targetanimation!=jumpupanim)
4665                 player[i].jumpkeydown=0;
4666             if((player[i].collided>.8&&player[i].jumppower>=5))
4667                 player[i].jumpkeydown=1;
4668
4669
4670             //hearing sounds
4671             if(!editorenabled){
4672                 if(player[i].howactive<=typesleeping)
4673                     if(numenvsounds>0&&(tutoriallevel!=1||cananger)&&hostile)
4674                         for(int j=0;j<numenvsounds;j++){
4675                             float vol=player[i].howactive==typesleeping?envsoundvol[j]-14:envsoundvol[j];
4676                             if(vol>0&&findDistancefast(&player[i].coords,&envsound[j])<
4677                                     2*(vol+vol*(player[i].creature==rabbittype)*3))
4678                                 player[i].aitype=attacktypecutoff;
4679                         }
4680
4681                 if(player[i].aitype!=passivetype){
4682                     if(player[i].howactive==typesleeping)
4683                         player[i].setAnimation(getupfromfrontanim);
4684                     player[i].howactive=typeactive;
4685                 }
4686             }
4687
4688             if(player[i].howactive<typesleeping&&
4689                     ((tutoriallevel!=1||cananger)&&hostile)&&
4690                     !player[0].dead&&
4691                     findDistancefast(&player[i].coords,&player[0].coords)<400&&
4692                     player[i].occluded<25){
4693                 if(findDistancefast(&player[i].coords,&player[0].coords)<12&&
4694                         animation[player[0].targetanimation].height!=lowheight&&!editorenabled)
4695                     player[i].aitype=attacktypecutoff;
4696                 if(findDistancefast(&player[i].coords,&player[0].coords)<30&&
4697                         animation[player[0].targetanimation].height==highheight&&!editorenabled)
4698                     player[i].aitype=attacktypecutoff;
4699
4700                 //wolf smell
4701                 if(player[i].creature==wolftype){
4702                     XYZ windsmell;
4703                     for(int j=0;j<numplayers;j++){
4704                         if(j==0||(player[j].dead&&player[j].bloodloss>0)){
4705                             float smelldistance=50;
4706                             if(j==0&&player[j].num_weapons>0){
4707                                 if(weapons[player[j].weaponids[0]].bloody)
4708                                     smelldistance=100;
4709                                 if(player[j].num_weapons==2)
4710                                     if(weapons[player[j].weaponids[1]].bloody)
4711                                         smelldistance=100;
4712                             }
4713                             if(j!=0)
4714                                 smelldistance=100;
4715                             windsmell=windvector;
4716                             Normalise(&windsmell);
4717                             windsmell=windsmell*2+player[j].coords;
4718                             if(findDistancefast(&player[i].coords,&windsmell)<smelldistance&&!editorenabled)
4719                                 player[i].aitype=attacktypecutoff;
4720                         }
4721                     }
4722                 }
4723
4724                 if(player[i].howactive<typesleeping&&player[i].losupdatedelay<0&&!editorenabled&&player[i].occluded<2){
4725                     player[i].losupdatedelay=.2;
4726                     for(int j=0;j<numplayers;j++){
4727                         if(j==0||player[j].skeleton.free||player[j].aitype!=passivetype){
4728                             if(abs(Random()%2)||animation[player[j].targetanimation].height!=lowheight||j!=0)
4729                                 if(findDistancefast(&player[i].coords,&player[j].coords)<400)
4730                                     if(normaldotproduct(player[i].facing,player[j].coords-player[i].coords)>0)
4731                                         if((-1==checkcollide(
4732                                                         DoRotation(player[i].getJointFor(head).position,0,player[i].rotation,0)*
4733                                                             player[i].scale+player[i].coords,
4734                                                         DoRotation(player[j].getJointFor(head).position,0,player[j].rotation,0)*
4735                                                             player[j].scale+player[j].coords)&&
4736                                                     !player[j].isWallJump())||
4737                                                 (player[j].targetanimation==hanganim&&
4738                                                  normaldotproduct(player[j].facing,player[i].coords-player[j].coords)<0)){
4739                                             player[i].lastseentime-=.2;
4740                                             if(j==0&&animation[player[j].targetanimation].height==lowheight)
4741                                                 player[i].lastseentime-=.4;
4742                                             else
4743                                                 player[i].lastseentime-=.6;
4744                                         }
4745                             if(player[i].lastseentime<=0){
4746                                 player[i].aitype=searchtype;
4747                                 player[i].lastchecktime=12;
4748                                 player[i].lastseen=player[j].coords;
4749                                 player[i].lastseentime=12;
4750                             }
4751                         }
4752                     }
4753                 }
4754             }
4755             //alerted surprise
4756             if(player[i].aitype==attacktypecutoff&&musictype!=2){
4757                 if(player[i].creature!=wolftype){
4758                     player[i].stunned=.6;
4759                     player[i].surprised=.6;
4760                 }
4761                 if(player[i].creature==wolftype){
4762                     player[i].stunned=.47;
4763                     player[i].surprised=.47;
4764                 }
4765                 numseen++;
4766             }
4767         }
4768
4769         //search for player
4770         int j;
4771         if(player[i].aitype==searchtype){
4772             player[i].aiupdatedelay-=multiplier;
4773             player[i].losupdatedelay-=multiplier;
4774             if(!player[i].pause)
4775                 player[i].lastseentime-=multiplier;
4776             player[i].lastchecktime-=multiplier;
4777
4778             if(player[i].isRun()&&!player[i].onground){
4779                 if(player[i].coords.y>terrain.getHeight(player[i].coords.x,player[i].coords.z)+10){
4780                     XYZ test2=player[i].coords+player[i].facing;
4781                     test2.y+=5;
4782                     XYZ test=player[i].coords+player[i].facing;
4783                     test.y-=10;
4784                     j=checkcollide(test2,test,player[i].laststanding);
4785                     if(j==-1)
4786                         j=checkcollide(test2,test);
4787                     if(j==-1){
4788                         player[i].velocity=0;
4789                         player[i].setAnimation(player[i].getStop());
4790                         player[i].targetrotation+=180;
4791                         player[i].stunned=.5;
4792                         //player[i].aitype=passivetype;
4793                         player[i].aitype=pathfindtype;
4794                         player[i].finalfinaltarget=player[i].waypoints[player[i].waypoint];
4795                         player[i].finalpathfindpoint=-1;
4796                         player[i].targetpathfindpoint=-1;
4797                         player[i].lastpathfindpoint=-1;
4798                         player[i].lastpathfindpoint2=-1;
4799                         player[i].lastpathfindpoint3=-1;
4800                         player[i].lastpathfindpoint4=-1;
4801                     }
4802                     else player[i].laststanding=j;
4803                 }
4804             }
4805             //check out last seen location
4806             if(player[i].aiupdatedelay<0){
4807                 player[i].targetrotation=roughDirectionTo(player[i].coords,player[i].lastseen);
4808                 player[i].lookrotation=player[i].targetrotation;
4809                 player[i].aiupdatedelay=.05;
4810                 player[i].forwardkeydown=1;
4811
4812                 if(findDistancefastflat(&player[i].coords,&player[i].lastseen)<1*sq(player[i].scale*5)||player[i].lastchecktime<0){
4813                     player[i].forwardkeydown=0;
4814                     player[i].aiupdatedelay=1;
4815                     player[i].lastseen.x+=(float(Random()%100)-50)/25;
4816                     player[i].lastseen.z+=(float(Random()%100)-50)/25;
4817                     player[i].lastchecktime=3;
4818                 }
4819
4820                 player[i].leftkeydown=0;
4821                 player[i].backkeydown=0;
4822                 player[i].rightkeydown=0;
4823                 player[i].crouchkeydown=0;
4824                 player[i].attackkeydown=0;
4825                 player[i].throwkeydown=0;
4826
4827                 if(player[i].avoidcollided>.8&&!player[i].jumpkeydown&&player[i].collided<.8){
4828                     if(!player[i].avoidsomething)player[i].targetrotation+=90*(player[i].whichdirection*2-1);
4829                     else{
4830                         XYZ leftpos,rightpos;
4831                         float leftdist,rightdist;
4832                         leftpos = player[i].coords+DoRotation(player[i].facing,0,90,0);
4833                         rightpos = player[i].coords-DoRotation(player[i].facing,0,90,0);
4834                         leftdist = findDistancefast(&leftpos, &player[i].avoidwhere);
4835                         rightdist = findDistancefast(&rightpos, &player[i].avoidwhere);
4836                         if(leftdist<rightdist)player[i].targetrotation+=90;
4837                         else player[i].targetrotation-=90;
4838                     }
4839                 }
4840             }
4841             if(player[i].collided<1||player[i].targetanimation!=jumpupanim)
4842                 player[i].jumpkeydown=0;
4843             if((player[i].collided>.8&&player[i].jumppower>=5))
4844                 player[i].jumpkeydown=1;
4845
4846             if(numenvsounds>0&&((tutoriallevel!=1||cananger)&&hostile))
4847                 for(int k=0;k<numenvsounds;k++){
4848                     if(findDistancefast(&player[i].coords,&envsound[k])<2*(envsoundvol[k]+envsoundvol[k]*(player[i].creature==rabbittype)*3)){
4849                         player[i].aitype=attacktypecutoff;
4850                     }
4851                 }
4852
4853             if(!player[0].dead&&
4854                     player[i].losupdatedelay<0&&
4855                     !editorenabled&&
4856                     player[i].occluded<2&&
4857                     ((tutoriallevel!=1||cananger)&&hostile)){
4858                 player[i].losupdatedelay=.2;
4859                 if(findDistancefast(&player[i].coords,&player[0].coords)<4&&animation[player[i].targetanimation].height!=lowheight){
4860                     player[i].aitype=attacktypecutoff;
4861                     player[i].lastseentime=1;
4862                 }
4863                 if(abs(Random()%2)||animation[player[i].targetanimation].height!=lowheight)
4864                     //TODO: factor out canSeePlayer()
4865                     if(findDistancefast(&player[i].coords,&player[0].coords)<400)
4866                         if(normaldotproduct(player[i].facing,player[0].coords-player[i].coords)>0)
4867                             if((checkcollide(
4868                                         DoRotation(player[i].getJointFor(head).position,0,player[i].rotation,0)*
4869                                             player[i].scale+player[i].coords,
4870                                         DoRotation(player[0].getJointFor(head).position,0,player[0].rotation,0)*
4871                                             player[0].scale+player[0].coords)==-1)||
4872                                     (player[0].targetanimation==hanganim&&normaldotproduct(
4873                                         player[0].facing,player[i].coords-player[0].coords)<0)){
4874                                     /* //TODO: changed j to 0 on a whim, make sure this is correct
4875                                     (player[j].targetanimation==hanganim&&normaldotproduct(
4876                                         player[j].facing,player[i].coords-player[j].coords)<0)
4877                                     */
4878                                 player[i].aitype=attacktypecutoff;
4879                                 player[i].lastseentime=1;
4880                             }
4881             }
4882             //player escaped
4883             if(player[i].lastseentime<0){
4884                 //player[i].aitype=passivetype;
4885                 numescaped++;
4886                 player[i].aitype=pathfindtype;
4887                 player[i].finalfinaltarget=player[i].waypoints[player[i].waypoint];
4888                 player[i].finalpathfindpoint=-1;
4889                 player[i].targetpathfindpoint=-1;
4890                 player[i].lastpathfindpoint=-1;
4891                 player[i].lastpathfindpoint2=-1;
4892                 player[i].lastpathfindpoint3=-1;
4893                 player[i].lastpathfindpoint4=-1;
4894             }
4895         }
4896
4897         if(player[i].aitype!=gethelptype)
4898             player[i].runninghowlong=0;
4899
4900         //get help from buddies
4901         if(player[i].aitype==gethelptype) {
4902             player[i].runninghowlong+=multiplier;
4903             player[i].aiupdatedelay-=multiplier;
4904
4905             if(player[i].aiupdatedelay<0||player[i].ally==0) {
4906                 player[i].aiupdatedelay=.2;
4907
4908                 //find closest ally
4909                 //TODO: factor out closest search somehow
4910                 if(!player[i].ally) {
4911                     int closest=-1;
4912                     float closestdist=-1;
4913                     for(int k=0;k<numplayers;k++) {
4914                         if(k!=i&&k!=0&&!player[k].dead&&
4915                                 player[k].howactive<typedead1&&
4916                                 !player[k].skeleton.free&&
4917                                 player[k].aitype==passivetype) {
4918                             float distance=findDistancefast(&player[i].coords,&player[k].coords);
4919                             if(closestdist==-1||distance<closestdist) {
4920                                 closestdist=distance;
4921                                 closest=k;
4922                             }
4923                             closest=k;
4924                         }
4925                     }
4926                     if(closest!=-1)
4927                         player[i].ally=closest;
4928                     else
4929                         player[i].ally=0;
4930                     player[i].lastseen=player[0].coords;
4931                     player[i].lastseentime=12;
4932                 }
4933
4934
4935                 player[i].lastchecktime=12;
4936
4937                 XYZ facing=player[i].coords;
4938                 XYZ flatfacing=player[player[i].ally].coords;
4939                 facing.y+=player[i].getJointFor(head).position.y*player[i].scale;
4940                 flatfacing.y+=player[player[i].ally].getJointFor(head).position.y*player[player[i].ally].scale;
4941                 if(-1!=checkcollide(facing,flatfacing))
4942                     player[i].lastseentime-=.1;
4943
4944                 //no available ally, run back to player
4945                 if(player[i].ally<=0||
4946                         player[player[i].ally].skeleton.free||
4947                         player[player[i].ally].aitype!=passivetype||
4948                         player[i].lastseentime<=0){
4949                     player[i].aitype=searchtype;
4950                     player[i].lastseentime=12;
4951                 }
4952
4953                 //seek out ally
4954                 if(player[i].ally>0){
4955                     player[i].targetrotation=roughDirectionTo(player[i].coords,player[player[i].ally].coords);
4956                     player[i].lookrotation=player[i].targetrotation;
4957                     player[i].aiupdatedelay=.05;
4958                     player[i].forwardkeydown=1;
4959
4960                     if(findDistancefastflat(&player[i].coords,&player[player[i].ally].coords)<3){
4961                         player[i].aitype=searchtype;
4962                         player[i].lastseentime=12;
4963                         player[player[i].ally].aitype=searchtype;
4964                         if(player[player[i].ally].lastseentime<player[i].lastseentime){
4965                             player[player[i].ally].lastseen=player[i].lastseen;
4966                             player[player[i].ally].lastseentime=player[i].lastseentime;
4967                             player[player[i].ally].lastchecktime=player[i].lastchecktime;
4968                         }
4969                     }
4970
4971                     if(player[i].avoidcollided>.8&&!player[i].jumpkeydown&&player[i].collided<.8){
4972                         if(!player[i].avoidsomething)
4973                             player[i].targetrotation+=90*(player[i].whichdirection*2-1);
4974                         else{
4975                             XYZ leftpos,rightpos;
4976                             float leftdist,rightdist;
4977                             leftpos = player[i].coords+DoRotation(player[i].facing,0,90,0);
4978                             rightpos = player[i].coords-DoRotation(player[i].facing,0,90,0);
4979                             leftdist = findDistancefast(&leftpos, &player[i].avoidwhere);
4980                             rightdist = findDistancefast(&rightpos, &player[i].avoidwhere);
4981                             if(leftdist<rightdist)
4982                                 player[i].targetrotation+=90;
4983                             else
4984                                 player[i].targetrotation-=90;
4985                         }
4986                     }
4987                 }
4988
4989                 player[i].leftkeydown=0;
4990                 player[i].backkeydown=0;
4991                 player[i].rightkeydown=0;
4992                 player[i].crouchkeydown=0;
4993                 player[i].attackkeydown=0;
4994             }
4995             if(player[i].collided<1||player[i].targetanimation!=jumpupanim)
4996                 player[i].jumpkeydown=0;
4997             if(player[i].collided>.8&&player[i].jumppower>=5)
4998                 player[i].jumpkeydown=1;
4999         }
5000
5001         //retreiving a weapon on the ground
5002         if(player[i].aitype==getweapontype){
5003             player[i].aiupdatedelay-=multiplier;
5004             player[i].lastchecktime-=multiplier;
5005
5006             if(player[i].aiupdatedelay<0){
5007                 player[i].aiupdatedelay=.2;
5008
5009                 //ALLY IS WEPON
5010                 if(player[i].ally<0){
5011                     int closest=-1;
5012                     float closestdist=-1;
5013                     for(int k=0;k<weapons.size();k++)
5014                         if(weapons[k].owner==-1){
5015                             float distance=findDistancefast(&player[i].coords,&weapons[k].position);
5016                             if(closestdist==-1||distance<closestdist){
5017                                 closestdist=distance;
5018                                 closest=k;
5019                             }
5020                             closest=k;
5021                         }
5022                     if(closest!=-1)
5023                         player[i].ally=closest;
5024                     else
5025                         player[i].ally=-1;
5026                 }
5027
5028                 player[i].lastseentime=12;
5029
5030                 if(!player[0].dead&&((tutoriallevel!=1||cananger)&&hostile))
5031                     if(player[i].ally<0||player[i].weaponactive!=-1||player[i].lastchecktime<=0){
5032                         player[i].aitype=attacktypecutoff;
5033                         player[i].lastseentime=1;
5034                     }
5035                 if(!player[0].dead)
5036                     if(player[i].ally>=0){
5037                         if(weapons[player[i].ally].owner!=-1||
5038                                 findDistancefast(&player[i].coords,&weapons[player[i].ally].position)>16){
5039                             player[i].aitype=attacktypecutoff;
5040                             player[i].lastseentime=1;
5041                         }
5042                         //TODO: factor these out as moveToward()
5043                         player[i].targetrotation=roughDirectionTo(player[i].coords,weapons[player[i].ally].position);
5044                         player[i].lookrotation=player[i].targetrotation;
5045                         player[i].aiupdatedelay=.05;
5046                         player[i].forwardkeydown=1;
5047
5048
5049                         if(player[i].avoidcollided>.8&&!player[i].jumpkeydown&&player[i].collided<.8){
5050                             if(!player[i].avoidsomething)
5051                                 player[i].targetrotation+=90*(player[i].whichdirection*2-1);
5052                             else{
5053                                 XYZ leftpos,rightpos;
5054                                 float leftdist,rightdist;
5055                                 leftpos = player[i].coords+DoRotation(player[i].facing,0,90,0);
5056                                 rightpos = player[i].coords-DoRotation(player[i].facing,0,90,0);
5057                                 leftdist = findDistancefast(&leftpos, &player[i].avoidwhere);
5058                                 rightdist = findDistancefast(&rightpos, &player[i].avoidwhere);
5059                                 if(leftdist<rightdist)
5060                                     player[i].targetrotation+=90;
5061                                 else
5062                                     player[i].targetrotation-=90;
5063                             }
5064                         }
5065                     }
5066
5067                 player[i].leftkeydown=0;
5068                 player[i].backkeydown=0;
5069                 player[i].rightkeydown=0;
5070                 player[i].attackkeydown=0;
5071                 player[i].throwkeydown=1;
5072                 player[i].crouchkeydown=0;
5073                 if(player[i].targetanimation!=crouchremoveknifeanim&&
5074                         player[i].targetanimation!=removeknifeanim)
5075                     player[i].throwtogglekeydown=0;
5076                 player[i].drawkeydown=0;
5077             }
5078             if(player[i].collided<1||player[i].targetanimation!=jumpupanim)
5079                 player[i].jumpkeydown=0;
5080             if((player[i].collided>.8&&player[i].jumppower>=5))
5081                 player[i].jumpkeydown=1;
5082         }
5083
5084         if(player[i].aitype==attacktypecutoff){
5085             player[i].aiupdatedelay-=multiplier;
5086             //dodge or reverse rabbit kicks, knife throws, flips
5087             if(player[i].damage<player[i].damagetolerance*2/3)
5088                 if((player[0].targetanimation==rabbitkickanim||
5089                             player[0].targetanimation==knifethrowanim||
5090                             (player[0].isFlip()&&
5091                              normaldotproduct(player[0].facing,player[0].coords-player[i].coords)<0))&&
5092                         !player[0].skeleton.free&&
5093                         (player[i].aiupdatedelay<.1)){
5094                     player[i].attackkeydown=0;
5095                     if(player[i].isIdle())
5096                         player[i].crouchkeydown=1;
5097                     if(player[0].targetanimation!=rabbitkickanim&&player[0].weaponactive!=-1){
5098                         if(weapons[player[0].weaponids[0]].getType()==knife){
5099                             if(player[i].isIdle()||player[i].isCrouch()||player[i].isRun()||player[i].isFlip()){
5100                                 if(abs(Random()%2==0))
5101                                     player[i].setAnimation(backhandspringanim);
5102                                 else
5103                                     player[i].setAnimation(rollanim);
5104                                 player[i].targetrotation+=90*(abs(Random()%2)*2-1);
5105                                 player[i].wentforweapon=0;
5106                             }
5107                             if(player[i].targetanimation==jumpupanim||player[i].targetanimation==jumpdownanim)
5108                                 player[i].setAnimation(flipanim);
5109                         }
5110                     }
5111                     player[i].forwardkeydown=0;
5112                     player[i].aiupdatedelay=.02;
5113                 }
5114             //get confused by flips
5115             if(player[0].isFlip()&&
5116                     !player[0].skeleton.free&&
5117                     player[0].targetanimation!=walljumprightkickanim&&
5118                     player[0].targetanimation!=walljumpleftkickanim){
5119                 if(findDistancefast(&player[0].coords,&player[i].coords)<25)
5120                     if((1-player[i].damage/player[i].damagetolerance)>.5)
5121                         player[i].stunned=1;
5122             }
5123             //go for weapon on the ground
5124             if(player[i].wentforweapon<3)
5125                 for(int k=0;k<weapons.size();k++)
5126                     if(player[i].creature!=wolftype)
5127                         if(player[i].num_weapons==0&&
5128                                 weapons[k].owner==-1&&
5129                                 weapons[i].velocity.x==0&&
5130                                 weapons[i].velocity.z==0&&
5131                                 weapons[i].velocity.y==0){
5132                             if(findDistancefast(&player[i].coords,&weapons[k].position)<16) {
5133                                 player[i].wentforweapon++;
5134                                 player[i].lastchecktime=6;
5135                                 player[i].aitype=getweapontype;
5136                                 player[i].ally=-1;
5137                             }
5138                         }
5139             //dodge/reverse walljump kicks
5140             if(player[i].damage<player[i].damagetolerance/2)
5141                 if(animation[player[i].targetanimation].height!=highheight)
5142                     if(player[i].damage<player[i].damagetolerance*.5&&
5143                             ((player[0].targetanimation==walljumprightkickanim||
5144                               player[0].targetanimation==walljumpleftkickanim)&&
5145                              ((player[i].aiupdatedelay<.15&&
5146                                difficulty==2)||
5147                               (player[i].aiupdatedelay<.08&&
5148                                difficulty!=2)))){
5149                         player[i].crouchkeydown=1;
5150                     }
5151             //walked off a ledge (?)
5152             if(player[i].isRun()&&!player[i].onground)
5153                 if(player[i].coords.y>terrain.getHeight(player[i].coords.x,player[i].coords.z)+10){
5154                     XYZ test2=player[i].coords+player[i].facing;
5155                     test2.y+=5;
5156                     XYZ test=player[i].coords+player[i].facing;
5157                     test.y-=10;
5158                     j=checkcollide(test2,test,player[i].laststanding);
5159                     if(j==-1)
5160                         j=checkcollide(test2,test);
5161                     if(j==-1) {
5162                         player[i].velocity=0;
5163                         player[i].setAnimation(player[i].getStop());
5164                         player[i].targetrotation+=180;
5165                         player[i].stunned=.5;
5166                         player[i].aitype=pathfindtype;
5167                         player[i].finalfinaltarget=player[i].waypoints[player[i].waypoint];
5168                         player[i].finalpathfindpoint=-1;
5169                         player[i].targetpathfindpoint=-1;
5170                         player[i].lastpathfindpoint=-1;
5171                         player[i].lastpathfindpoint2=-1;
5172                         player[i].lastpathfindpoint3=-1;
5173                         player[i].lastpathfindpoint4=-1;
5174                     } else
5175                         player[i].laststanding=j;
5176                 }
5177             //lose sight of player in the air (?)
5178             if(player[0].coords.y>player[i].coords.y+5&&
5179                     animation[player[0].targetanimation].height!=highheight&&
5180                     !player[0].onterrain){
5181                 player[i].aitype=pathfindtype;
5182                 player[i].finalfinaltarget=player[i].waypoints[player[i].waypoint];
5183                 player[i].finalpathfindpoint=-1;
5184                 player[i].targetpathfindpoint=-1;
5185                 player[i].lastpathfindpoint=-1;
5186                 player[i].lastpathfindpoint2=-1;
5187                 player[i].lastpathfindpoint3=-1;
5188                 player[i].lastpathfindpoint4=-1;
5189             }
5190             //it's time to think (?)
5191             if(player[i].aiupdatedelay<0&&
5192                     !animation[player[i].targetanimation].attack&&
5193                     player[i].targetanimation!=staggerbackhighanim&&
5194                     player[i].targetanimation!=staggerbackhardanim&&
5195                     player[i].targetanimation!=backhandspringanim&&
5196                     player[i].targetanimation!=dodgebackanim){
5197                 //draw weapon
5198                 if(player[i].weaponactive==-1&&player[i].num_weapons>0)
5199                     player[i].drawkeydown=Random()%2;
5200                 else
5201                     player[i].drawkeydown=0;
5202                 player[i].rabbitkickenabled=Random()%2;
5203                 //chase player
5204                 XYZ rotatetarget=player[0].coords+player[0].velocity;
5205                 XYZ targetpoint=player[0].coords;
5206                 if(findDistancefast(&player[0].coords,&player[i].coords)<
5207                         findDistancefast(&rotatetarget,&player[i].coords))
5208                     targetpoint+=player[0].velocity*
5209                         findDistance(&player[0].coords,&player[i].coords)/findLength(&player[i].velocity);
5210                 player[i].targetrotation=roughDirectionTo(player[i].coords,targetpoint);
5211                 player[i].lookrotation=player[i].targetrotation;
5212                 player[i].aiupdatedelay=.2+fabs((float)(Random()%100)/1000);
5213
5214                 if(findDistancefast(&player[i].coords,&player[0].coords)>5&&(player[0].weaponactive==-1||player[i].weaponactive!=-1))
5215                     player[i].forwardkeydown=1;
5216                 else if((findDistancefast(&player[i].coords,&player[0].coords)>16||
5217                             findDistancefast(&player[i].coords,&player[0].coords)<9)&&
5218                         player[0].weaponactive!=-1)
5219                     player[i].forwardkeydown=1;
5220                 else if(Random()%6==0||(player[i].creature==wolftype&&Random()%3==0))
5221                     player[i].forwardkeydown=1;
5222                 else
5223                     player[i].forwardkeydown=0;
5224                 //chill out around the corpse
5225                 if(player[0].dead){
5226                     player[i].forwardkeydown=0;
5227                     if(Random()%10==0)
5228                         player[i].forwardkeydown=1;
5229                     if(Random()%100==0){
5230                         player[i].aitype=pathfindtype;
5231                         player[i].finalfinaltarget=player[i].waypoints[player[i].waypoint];
5232                         player[i].finalpathfindpoint=-1;
5233                         player[i].targetpathfindpoint=-1;
5234                         player[i].lastpathfindpoint=-1;
5235                         player[i].lastpathfindpoint2=-1;
5236                         player[i].lastpathfindpoint3=-1;
5237                         player[i].lastpathfindpoint4=-1;
5238                     }
5239                 }
5240                 player[i].leftkeydown=0;
5241                 player[i].backkeydown=0;
5242                 player[i].rightkeydown=0;
5243                 player[i].crouchkeydown=0;
5244                 player[i].throwkeydown=0;
5245
5246                 if(player[i].avoidcollided>.8&&!player[i].jumpkeydown&&player[i].collided<.8)
5247                     player[i].targetrotation+=90*(player[i].whichdirection*2-1);
5248                 //attack!!!
5249                 if(Random()%2==0||player[i].weaponactive!=-1||player[i].creature==wolftype)
5250                     player[i].attackkeydown=1;
5251                 else
5252                     player[i].attackkeydown=0;
5253                 if(player[i].isRun()&&Random()%6&&findDistancefast(&player[i].coords,&player[0].coords)>7)
5254                     player[i].attackkeydown=0;
5255
5256                 //TODO: wat
5257                 if(player[i].aitype!=playercontrolled&&
5258                         (player[i].isIdle()||
5259                          player[i].isCrouch()||
5260                          player[i].isRun())){
5261                     int target=-2;
5262                     for(int j=0;j<numplayers;j++)
5263                         if(j!=i&&!player[j].skeleton.free&&
5264                                 player[j].hasvictim&&
5265                                 (tutoriallevel==1&&reversaltrain||
5266                                  Random()%2==0&&difficulty==2||
5267                                  Random()%4==0&&difficulty==1||
5268                                  Random()%8==0&&difficulty==0||
5269                                  player[j].lastattack2==player[j].targetanimation&&
5270                                  player[j].lastattack3==player[j].targetanimation&&
5271                                  (Random()%2==0||difficulty==2)||
5272                                  (player[i].isIdle()||player[i].isRun())&&
5273                                  player[j].weaponactive!=-1||
5274                                  player[j].targetanimation==swordslashanim&&
5275                                  player[i].weaponactive!=-1||
5276                                  player[j].targetanimation==staffhitanim||
5277                                  player[j].targetanimation==staffspinhitanim))
5278                             if(findDistancefast(&player[j].coords,&player[j].victim->coords)<4&&
5279                                     player[j].victim==&player[i]&&
5280                                     (player[j].targetanimation==sweepanim||
5281                                      player[j].targetanimation==spinkickanim||
5282                                      player[j].targetanimation==staffhitanim||
5283                                      player[j].targetanimation==staffspinhitanim||
5284                                      player[j].targetanimation==winduppunchanim||
5285                                      player[j].targetanimation==upunchanim||
5286                                      player[j].targetanimation==wolfslapanim||
5287                                      player[j].targetanimation==knifeslashstartanim||
5288                                      player[j].targetanimation==swordslashanim&&
5289                                       (findDistancefast(&player[j].coords,&player[i].coords)<2||
5290                                        player[i].weaponactive!=-1))){
5291                                 if(target>=0)
5292                                     target=-1;
5293                                 else
5294                                     target=j;
5295                             }
5296                     if(target>=0)
5297                         player[target].Reverse();
5298                 }
5299
5300                 if(player[i].collided<1)
5301                     player[i].jumpkeydown=0;
5302                 if(player[i].collided>.8&&player[i].jumppower>=5||
5303                         findDistancefast(&player[i].coords,&player[0].coords)>400&&
5304                         player[i].onterrain&&
5305                         player[i].creature==rabbittype)
5306                     player[i].jumpkeydown=1;
5307                 //TODO: why are we controlling the human?
5308                 if(normaldotproduct(player[i].facing,player[0].coords-player[i].coords)>0)
5309                     player[0].jumpkeydown=0;
5310                 if(player[0].targetanimation==jumpdownanim&&
5311                         findDistancefast(&player[0].coords,&player[i].coords)<40)
5312                     player[i].crouchkeydown=1;
5313                 if(player[i].jumpkeydown)
5314                     player[i].attackkeydown=0;
5315
5316                 if(tutoriallevel==1)
5317                     if(!canattack)
5318                         player[i].attackkeydown=0;
5319
5320
5321                 XYZ facing=player[i].coords;
5322                 XYZ flatfacing=player[0].coords;
5323                 facing.y+=player[i].getJointFor(head).position.y*player[i].scale;
5324                 flatfacing.y+=player[0].getJointFor(head).position.y*player[0].scale;
5325                 if(player[i].occluded>=2)
5326                     if(-1!=checkcollide(facing,flatfacing)){
5327                         if(!player[i].pause)
5328                             player[i].lastseentime-=.2;
5329                         if(player[i].lastseentime<=0&&
5330                                 (player[i].creature!=wolftype||
5331                                  player[i].weaponstuck==-1)){
5332                             player[i].aitype=searchtype;
5333                             player[i].lastchecktime=12;
5334                             player[i].lastseen=player[0].coords;
5335                             player[i].lastseentime=12;
5336                         }
5337                     }else
5338                         player[i].lastseentime=1;
5339             }
5340         }
5341         if(animation[player[0].targetanimation].height==highheight&&
5342                 (player[i].aitype==attacktypecutoff||
5343                  player[i].aitype==searchtype))
5344             if(player[0].coords.y>terrain.getHeight(player[0].coords.x,player[0].coords.z)+10){
5345                 XYZ test=player[0].coords;
5346                 test.y-=40;
5347                 if(-1==checkcollide(player[0].coords,test))
5348                     player[i].stunned=1;
5349             }
5350         //stunned
5351         if(player[i].aitype==passivetype&&!(player[i].numwaypoints>1)||
5352                 player[i].stunned>0||
5353                 player[i].pause&&player[i].damage>player[i].superpermanentdamage){
5354             if(player[i].pause)
5355                 player[i].lastseentime=1;
5356             player[i].targetrotation=player[i].rotation;
5357             player[i].forwardkeydown=0;
5358             player[i].leftkeydown=0;
5359             player[i].backkeydown=0;
5360             player[i].rightkeydown=0;
5361             player[i].jumpkeydown=0;
5362             player[i].attackkeydown=0;
5363             player[i].crouchkeydown=0;
5364             player[i].throwkeydown=0;
5365         }
5366
5367
5368         XYZ facing;
5369         facing=0;
5370         facing.z=-1;
5371
5372         XYZ flatfacing=DoRotation(facing,0,player[i].rotation+180,0);
5373         facing=flatfacing;
5374
5375         if(player[i].aitype==attacktypecutoff){
5376             player[i].targetheadrotation=180-roughDirectionTo(player[i].coords,player[0].coords);
5377             player[i].targetheadrotation2=pitchTo(player[i].coords,player[0].coords);
5378         }else if(player[i].howactive>=typesleeping){
5379             player[i].targetheadrotation=player[i].targetrotation;
5380             player[i].targetheadrotation2=0;
5381         }else{
5382             if(player[i].interestdelay<=0){
5383                 player[i].interestdelay=.7+(float)(abs(Random()%100))/100;
5384                 player[i].headtarget=player[i].coords;
5385                 player[i].headtarget.x+=(float)(abs(Random()%200)-100)/100;
5386                 player[i].headtarget.z+=(float)(abs(Random()%200)-100)/100;
5387                 player[i].headtarget.y+=(float)(abs(Random()%200)-100)/300;
5388                 player[i].headtarget+=player[i].facing*1.5;
5389             }
5390             player[i].targetheadrotation=180-roughDirectionTo(player[i].coords,player[i].headtarget);
5391             player[i].targetheadrotation2=pitchTo(player[i].coords,player[i].headtarget);
5392         }
5393     }
5394 }
5395
5396 /*
5397 Values of mainmenu :
5398 1 Main menu
5399 2 Menu pause (resume/end game)
5400 3 Option menu
5401 4 Controls configuration menu
5402 5 Main game menu (choose level or challenge)
5403 6 Deleting user menu
5404 7 User managment menu (select/add)
5405 8 Choose difficulty menu
5406 9 Challenge level selection menu
5407 10 End of the campaign congratulation (is that really a menu?)
5408 11 Same that 9 ??? => unused
5409 18 stereo configuration
5410 */
5411     
5412 void Game::MenuTick(){
5413     //menu buttons
5414     
5415     // some specific case where we do something even if the left mouse button is not pressed.
5416         if((mainmenu==5) && (endgame==2)) {
5417                 accountactive->endGame();
5418                 endgame=0;
5419         }
5420         if(mainmenu==10)
5421                 endgame=2;
5422         if( (mainmenu==18) && Input::isKeyPressed(MOUSEBUTTON2) && (selected==1) )
5423                 stereoseparation-=0.001;
5424                 
5425     if(Input::MouseClicked() && (selected >= 0)) { // handling of the left mouse clic in menus
5426                 switch(mainmenu) {
5427                         case 1:
5428                         case 2:
5429                                 switch(selected) {
5430                                         case 1:
5431                                                 if(gameon) { //resume
5432                                                         mainmenu=0;
5433                                                         pause_sound(stream_menutheme);
5434                                                         resume_stream(leveltheme);
5435                                                 } else { //new game
5436                                                         fireSound(firestartsound);
5437                                                         flash();
5438                                                         mainmenu=(accountactive?5:7);
5439                                                         selected=-1;
5440                                                 }
5441                                                 break;
5442                                         case 2: //options
5443                                                 fireSound();
5444                                                 flash();
5445                                                 mainmenu=3;
5446                                                 if(newdetail>2) newdetail=detail;
5447                                                 if(newdetail<0) newdetail=detail;
5448                                                 if(newscreenwidth>3000) newscreenwidth=screenwidth;
5449                                                 if(newscreenwidth<0) newscreenwidth=screenwidth;
5450                                                 if(newscreenheight>3000) newscreenheight=screenheight;
5451                                                 if(newscreenheight<0) newscreenheight=screenheight;
5452                                                 break;
5453                                         case 3:
5454                                                 fireSound();
5455                                                 flash();
5456                                                 if(gameon){ //end game
5457                                                         gameon=0;
5458                                                         mainmenu=1;
5459                                                 } else { //quit
5460                                                         tryquit=1;
5461                                                         pause_sound(stream_menutheme);
5462                                                 }
5463                                                 break;
5464                                 }
5465                                 break;
5466                         case 3:
5467                                 fireSound();
5468                                 bool isCustomResolution,found;
5469                                 switch(selected){
5470                                         case 0:
5471                                                 extern SDL_Rect **resolutions;
5472                                                 isCustomResolution = true;
5473                                                 found = false;
5474                                                 for(int i = 0; (!found) && (resolutions[i]); i++) {
5475                                                         if((resolutions[i]->w == screenwidth) && (resolutions[i]->h == screenwidth))
5476                                                                 isCustomResolution = false;
5477
5478                                                         if((resolutions[i]->w == newscreenwidth) && (resolutions[i]->h == newscreenheight)) {
5479                                                                 i++;
5480                                                                 if(resolutions[i] != NULL) {
5481                                                                         newscreenwidth = (int) resolutions[i]->w;
5482                                                                         newscreenheight = (int) resolutions[i]->h;
5483                                                                 } else if(isCustomResolution){
5484                                                                         if((screenwidth == newscreenwidth) && (screenheight == newscreenheight)) {
5485                                                                                 newscreenwidth = (int) resolutions[0]->w;
5486                                                                                 newscreenheight = (int) resolutions[0]->h;
5487                                                                         } else {
5488                                                                                 newscreenwidth = screenwidth;
5489                                                                                 newscreenheight = screenheight;
5490                                                                         }
5491                                                                 } else {
5492                                                                         newscreenwidth = (int) resolutions[0]->w;
5493                                                                         newscreenheight = (int) resolutions[0]->h;
5494                                                                 }
5495                                                                 found = true;
5496                                                         }
5497                                                 }
5498
5499                                                 if(!found) {
5500                                                         newscreenwidth = (int) resolutions[0]->w;
5501                                                         newscreenheight = (int) resolutions[0]->h;
5502                                                 }
5503                                                 break;
5504                                         case 1:
5505                                                 newdetail++;
5506                                                 if(newdetail>2) newdetail=0;
5507                                                 break;
5508                                         case 2:
5509                                                 bloodtoggle++;
5510                                                 if(bloodtoggle>2) bloodtoggle=0;
5511                                                 break;
5512                                         case 3:
5513                                                 difficulty++;
5514                                                 if(difficulty>2) difficulty=0;
5515                                                 break;
5516                                         case 4:
5517                                                 ismotionblur = !ismotionblur;
5518                                                 break;
5519                                         case 5:
5520                                                 decals = !decals;
5521                                                 break;
5522                                         case 6:
5523                                                 musictoggle = !musictoggle;
5524
5525                                                 if(musictoggle) {
5526                                                   emit_stream_np(stream_menutheme);
5527                                                 } else {
5528                                                         pause_sound(leveltheme);
5529                                                         pause_sound(stream_fighttheme);
5530                                                         pause_sound(stream_menutheme);
5531
5532                                                         for(int i=0;i<4;i++){
5533                                                                 oldmusicvolume[i]=0;
5534                                                                 musicvolume[i]=0;
5535                                                         }
5536                                                 }
5537
5538                                                 break;
5539                                         case 7: // controls
5540                                                 flash();
5541                                                 mainmenu=4;
5542                                                 selected=-1;
5543                                                 keyselect=-1;
5544                                                 break;
5545                                         case 8:
5546                                                 flash();
5547                                                 
5548                                                 SaveSettings();
5549                                                 mainmenu=gameon?2:1;
5550                                                 break;
5551                                         case 9:
5552                                                 invertmouse = !invertmouse;
5553                                                 break;
5554                                         case 10:
5555                                                 usermousesensitivity+=.2;
5556                                                 if(usermousesensitivity>2) usermousesensitivity=.2;
5557                                                 break;
5558                                         case 11:
5559                                                 volume+=.1f;
5560                                                 if(volume>1.0001f) volume=0;
5561                                                 OPENAL_SetSFXMasterVolume((int)(volume*255));
5562                                                 break;
5563                                         case 12:
5564                                                 flash();
5565                                                 
5566                                                 newstereomode = stereomode;
5567                                                 mainmenu=18;
5568                                                 keyselect=-1;
5569                                                 break;
5570                                         case 13:
5571                                                 showdamagebar = !showdamagebar;
5572                                                 break;
5573                                 }
5574                                 break;
5575                         case 4:
5576                                 if(!waiting) {
5577                                         fireSound();
5578                                         if(selected<(debugmode?10:9) && keyselect==-1)
5579                                                 keyselect=selected;
5580                                         if(keyselect!=-1)
5581                                                 setKeySelected();
5582                                         if(selected==(debugmode?10:9)){
5583                                                 flash();
5584                                                 mainmenu=3;
5585                                         }
5586                                 }
5587                                 break;
5588                         case 5:
5589                                 fireSound();
5590                                 flash();
5591                                 if((selected-NB_CAMPAIGN_MENU_ITEM >= accountactive->getCampaignChoicesMade())) {
5592                                         startbonustotal=0;
5593
5594                                         loading=2;
5595                                         loadtime=0;
5596                                         targetlevel=7;
5597                                         if(firstload)
5598                                                 TickOnceAfter();
5599                                         else
5600                                                 LoadStuff();
5601                                         whichchoice=selected-NB_CAMPAIGN_MENU_ITEM-accountactive->getCampaignChoicesMade();
5602                                         actuallevel=(accountactive->getCampaignChoicesMade()>0?campaignlevels[accountactive->getCampaignChoicesMade()-1].nextlevel[whichchoice]:0);
5603                                         visibleloading=1;
5604                                         stillloading=1;
5605                                         Loadlevel(campaignlevels[actuallevel].mapname.c_str());
5606                                         campaign=1;
5607                                         mainmenu=0;
5608                                         gameon=1;
5609                                         pause_sound(stream_menutheme);
5610                                 }
5611                                 switch(selected){
5612                                         case 1:
5613                                                 startbonustotal=0;
5614
5615                                                 loading=2;
5616                                                 loadtime=0;
5617                                                 targetlevel=-1;
5618                                                 if(firstload) {
5619                                                         TickOnceAfter();
5620                                                 } else
5621                                                         LoadStuff();
5622                                                 Loadlevel(-1);
5623
5624                                                 mainmenu=0;
5625                                                 gameon=1;
5626                                                 pause_sound(stream_menutheme);
5627                                                 break;
5628                                         case 2:
5629                                                 mainmenu=9;
5630                                                 break;
5631                                         case 3:
5632                                                 mainmenu=6;
5633                                                 break;
5634                                         case 4:
5635                                                 mainmenu=(gameon?2:1);
5636                                                 break;
5637                                         case 5:
5638                                                 mainmenu=7;
5639                                                 break;
5640                                         case 6:
5641                                                 vector<string> campaigns = ListCampaigns();
5642                                                 vector<string>::iterator c;
5643                                                 if ((c = find(campaigns.begin(),campaigns.end(),accountactive->getCurrentCampaign()))==campaigns.end()) {
5644                                                         if(!campaigns.empty())
5645                                                                 accountactive->setCurrentCampaign(campaigns.front());
5646                                                 } else {
5647                                                         c++;
5648                                                         if(c==campaigns.end())
5649                                                                 c=campaigns.begin();
5650                                                         accountactive->setCurrentCampaign(*c);
5651                                                 }
5652                                                 LoadCampaign();
5653                                                 break;
5654                                 }
5655                                 break;
5656                         case 6:
5657                                 fireSound();
5658                                 if(selected==1) {
5659                                         flash();
5660                                         accountactive = Account::destroy(accountactive);
5661                                         mainmenu=7;
5662                                 } else if(selected==2) {
5663                                         flash();
5664                                         mainmenu=5;
5665                                 }
5666                                 break;
5667                         case 7:
5668                                 fireSound();
5669                                 if(selected==0 && Account::getNbAccounts()<8){
5670                                         entername=1;
5671                                 } else if (selected < Account::getNbAccounts()+1) {
5672                                         flash();
5673                                         mainmenu=5;
5674                                         accountactive=Account::get(selected-1);
5675                                 } else if (selected == Account::getNbAccounts()+1) {
5676                                         flash();
5677                                         mainmenu=5;
5678                                         for(int j=0;j<255;j++){
5679                                                 displaytext[0][j]=0;
5680                                         }
5681                                         displaychars[0]=0;
5682                                         displayselected=0;
5683                                         entername=0;
5684                                 }
5685                                 break;
5686                         case 8:
5687                                 fireSound();
5688                                 flash();
5689                                 if(selected<=2)
5690                                         accountactive->setDifficulty(selected);
5691                                 mainmenu=5;
5692                                 break;
5693                         case 9:
5694                                 if(selected<numchallengelevels && selected<=accountactive->getProgress()){
5695                                         fireSound();
5696                                         flash();
5697
5698                                         startbonustotal=0;
5699
5700                                         loading=2;
5701                                         loadtime=0;
5702                                         targetlevel=selected;
5703                                         if(firstload)
5704                                                 TickOnceAfter();
5705                                         else
5706                                                 LoadStuff();
5707                                         Loadlevel(selected);
5708                                         campaign=0;
5709
5710                                         mainmenu=0;
5711                                         gameon=1;
5712                                         pause_sound(stream_menutheme);
5713                                 }
5714                                 if(selected==numchallengelevels){
5715                                         fireSound();
5716                                         flash();
5717                                         mainmenu=5;
5718                                 }
5719                                 break;
5720                         case 10:
5721                                 if(selected==3){
5722                                         fireSound();
5723                                         flash();
5724                                         mainmenu=5;
5725                                 }
5726                                 break;
5727                         case 18:
5728                                 if(selected==1)
5729                                         stereoseparation+=0.001;
5730                                 else {
5731                                         fireSound();
5732                                         if(selected==0){
5733                                                 newstereomode = (StereoMode)(newstereomode + 1);
5734                                                 while(!CanInitStereo(newstereomode)){
5735                                                         printf("Failed to initialize mode %s (%i)\n", StereoModeName(newstereomode), newstereomode);
5736                                                         newstereomode = (StereoMode)(newstereomode + 1);
5737                                                         if(newstereomode >= stereoCount)
5738                                                                 newstereomode = stereoNone;
5739                                                 }
5740                                         } else if(selected==2) {
5741                                                 stereoreverse = !stereoreverse;
5742                                         } else if(selected==3) {
5743                                                 flash();
5744                                                 mainmenu=3;
5745
5746                                                 stereomode = newstereomode;
5747                                                 InitStereo(stereomode);
5748                                         }
5749                                 }
5750                                 break;
5751                 }
5752         }
5753
5754     if(Input::isKeyDown(SDLK_q) && Input::isKeyDown(SDLK_LMETA)){
5755         tryquit=1;
5756         if(mainmenu==3) {
5757             SaveSettings();
5758         }
5759     }
5760
5761     OPENAL_SetFrequency(channels[stream_menutheme], 22050);
5762
5763     if(entername) {
5764         inputText(displaytext[0],&displayselected,&displaychars[0]);
5765         if(!waiting) { // the input as finished
5766             if(displaychars[0]){ // with enter
5767                 accountactive = Account::add(string(displaytext[0]));
5768
5769                 mainmenu=8;
5770
5771                 flash();
5772
5773                 fireSound(firestartsound);
5774
5775                 for(int i=0;i<255;i++){
5776                     displaytext[0][i]=0;
5777                 }
5778                 displaychars[0]=0;
5779
5780                 displayselected=0;
5781             }
5782             entername=0;
5783         }
5784         
5785         displayblinkdelay-=multiplier;
5786         if(displayblinkdelay<=0){
5787             displayblinkdelay=.3;
5788             displayblink=1-displayblink;
5789         }
5790     }
5791 }
5792
5793 void Game::Tick(){
5794         static XYZ facing,flatfacing;
5795         static int target;
5796
5797         for(int i=0;i<15;i++){
5798                 displaytime[i]+=multiplier;
5799         }
5800
5801         keyboardfrozen=false;
5802     Input::Tick();
5803
5804         if(Input::isKeyPressed(SDLK_F6)){
5805                 if(Input::isKeyDown(SDLK_LSHIFT))
5806                         stereoreverse=true;
5807                 else
5808                         stereoreverse=false;
5809
5810                 if(stereoreverse)
5811                         printf("Stereo reversed\n");
5812                 else
5813                         printf("Stereo unreversed\n");
5814         }
5815
5816         if(Input::isKeyDown(SDLK_F7)){
5817                 if(Input::isKeyDown(SDLK_LSHIFT))
5818                         stereoseparation -= 0.001;
5819                 else
5820                         stereoseparation -= 0.010;
5821                 printf("Stereo decreased increased to %f\n", stereoseparation);
5822         }
5823
5824         if(Input::isKeyDown(SDLK_F8)){
5825                 if(Input::isKeyDown(SDLK_LSHIFT))
5826                         stereoseparation += 0.001;
5827                 else
5828                         stereoseparation += 0.010;
5829                 printf("Stereo separation increased to %f\n", stereoseparation);
5830         }
5831
5832
5833         if(Input::isKeyPressed(SDLK_TAB)&&tutoriallevel){
5834                 if(tutorialstage!=51)
5835                         tutorialstagetime=tutorialmaxtime;
5836                 emit_sound_np(consolefailsound, 128.);
5837         }
5838
5839     /*
5840     Values of mainmenu :
5841     1 Main menu
5842     2 Menu pause (resume/end game)
5843     3 Option menu
5844     4 Controls configuration menu
5845     5 Main game menu (choose level or challenge)
5846     6 Deleting user menu
5847     7 User managment menu (select/add)
5848     8 Choose difficulty menu
5849     9 Challenge level selection menu
5850     10 End of the campaign congratulation (is that really a menu?)
5851     11 Same that 9 ??? => unused
5852     18 stereo configuration
5853     */
5854         
5855         if(!console) {
5856         //campaign over?
5857                 if(mainmenu&&endgame==1)
5858             mainmenu=10;
5859         //go to level select after completing a campaign level
5860         if(campaign&&winfreeze&&mainmenu==0&&campaignlevels[actuallevel].choosenext==1) {
5861             mainmenu=5;
5862             gameon=0;
5863             winfreeze=0;
5864             fireSound();
5865             flash();
5866             if(musictoggle) {
5867                 OPENAL_SetFrequency(OPENAL_ALL, 0.001);
5868                 emit_stream_np(stream_menutheme);
5869                 pause_sound(leveltheme);
5870             }
5871                         LoadCampaign();
5872         }
5873         //escape key pressed
5874         //TODO: there must be code somewhere else that handles clicking the Back button, merge it with this
5875                 if(Input::isKeyPressed(SDLK_ESCAPE)&&
5876                 (gameon||mainmenu==0||(mainmenu>=3&&mainmenu!=8&&!(mainmenu==7&&entername)))) {
5877                         selected=-1;
5878             if(mainmenu==0&&!winfreeze)
5879                 mainmenu=2; //pause
5880             else if(mainmenu==1||mainmenu==2){
5881                 mainmenu=0; //unpause
5882             }
5883             //play menu theme
5884             if(musictoggle&&(mainmenu==1||mainmenu==2)){
5885                 OPENAL_SetFrequency(OPENAL_ALL, 0.001);
5886                 emit_stream_np(stream_menutheme);
5887                 pause_sound(leveltheme);
5888             }
5889             //on resume, play level music
5890             if(!mainmenu){
5891                 pause_sound(stream_menutheme);
5892                 resume_stream(leveltheme);
5893             }
5894             //finished with settings menu
5895                         if(mainmenu==3){
5896                                 SaveSettings();
5897                         }
5898             //effects
5899                         if(mainmenu>=3&&mainmenu!=8){
5900                                 fireSound();
5901                                 flash();
5902                         }
5903             //go back
5904             switch(mainmenu){
5905                 case 3: case 5:
5906                     mainmenu=gameon?2:1; break;
5907                 case 4: case 18:
5908                     mainmenu=3; break;
5909                 case 6: case 7: case 9: case 10:
5910                     mainmenu=5; break;
5911             }
5912                 }
5913         }
5914
5915         if(mainmenu) {
5916         MenuTick();
5917         }
5918
5919         if(!mainmenu) {
5920                 if(hostile==1)hostiletime+=multiplier;
5921                 else hostiletime=0;
5922                 if(!winfreeze)leveltime+=multiplier;
5923
5924         //keys
5925                 if(Input::isKeyPressed(SDLK_v)&&debugmode){
5926                         freeze=1-freeze;
5927                         if(freeze){
5928                                 OPENAL_SetFrequency(OPENAL_ALL, 0.001);
5929                         }
5930                 }
5931
5932                 if(Input::isKeyPressed(chatkey)&&!console&&!chatting&&debugmode)
5933                         chatting=1;
5934
5935                 if(chatting){
5936                         inputText(displaytext[0],&displayselected,&displaychars[0]);
5937                         if(!waiting) {
5938                                 if(displaychars[0]){
5939                                         for(int j=0;j<255;j++)
5940                                                 displaytext[0][j]=0;
5941                                         displaychars[0]=0;
5942                                         displayselected=0;
5943                                 }       
5944                                 chatting=0;             
5945                         }
5946
5947                         displayblinkdelay-=multiplier;
5948                         if(displayblinkdelay<=0){
5949                                 displayblinkdelay=.3;
5950                                 displayblink=1-displayblink;
5951                         }
5952                 }
5953                 if(chatting)
5954             keyboardfrozen=true;
5955
5956                 if(Input::isKeyPressed(consolekey)&&debugmode) {
5957                         console=!console;
5958                         if(console) {
5959                                 OPENAL_SetFrequency(OPENAL_ALL, 0.001);
5960                         } else {
5961                                 freeze=0;
5962                                 waiting=false;
5963                         }
5964                 }
5965
5966                 if(console)
5967             freeze=1;
5968                 if(console&&!Input::isKeyDown(SDLK_LMETA)) {
5969                         inputText(consoletext[0],&consoleselected,&consolechars[0]);
5970                         if(!waiting) {
5971                                 archiveselected=0;
5972                                 if(consolechars[0]>0) {
5973                     consoletext[0][consolechars[0]]='\0';
5974                     cmd_dispatch(consoletext[0]);
5975                                         for(int k=14;k>=1;k--) {
5976                                                 for(int j=0;j<255;j++)
5977                                                         consoletext[k][j]=consoletext[k-1][j];
5978                                                 consolechars[k]=consolechars[k-1];
5979                                         }
5980                                         for(int j=0;j<255;j++)
5981                                                 consoletext[0][j]=0;
5982                                         consolechars[0]=0;
5983                                         consoleselected=0;
5984                                 }
5985                         }
5986
5987                         consoleblinkdelay-=multiplier;
5988                         if(consoleblinkdelay<=0) {
5989                                 consoleblinkdelay=.3;
5990                                 consoleblink=1-consoleblink;
5991                         }
5992                 }
5993
5994
5995
5996                 if(Input::isKeyDown(SDLK_q)&&Input::isKeyDown(SDLK_LMETA)){
5997                         tryquit=1;
5998                         if(mainmenu==3) {
5999                                 SaveSettings();
6000                         }
6001                 }
6002
6003                 static int oldwinfreeze;
6004                 if(winfreeze&&!oldwinfreeze){
6005                         OPENAL_SetFrequency(OPENAL_ALL, 0.001);
6006                         emit_sound_np(consolesuccesssound);
6007                 }
6008                 if(winfreeze==0)
6009             oldwinfreeze=winfreeze;
6010                 else
6011             oldwinfreeze++;
6012
6013                 if((Input::isKeyPressed(jumpkey)||Input::isKeyPressed(SDLK_SPACE))&&!campaign)
6014                         if(winfreeze)
6015                 winfreeze=0;
6016                 if((Input::isKeyDown(SDLK_ESCAPE))&&!campaign&&gameon){
6017             if(console){
6018                 console=false;
6019                 freeze=0;
6020             } else if(winfreeze) {
6021                                 mainmenu=9;
6022                                 gameon=0;
6023                         }
6024         }
6025
6026
6027
6028         //TODO: what is this test?
6029                 if(!freeze&&!winfreeze&&!(mainmenu&&gameon)&&(gameon||!gamestarted)){
6030
6031             //dialogues
6032                         if(indialogue!=-1)
6033                 talkdelay=1;
6034                         talkdelay-=multiplier;
6035
6036                         if(talkdelay<=0&&indialogue==-1&&animation[player[0].targetanimation].height!=highheight)
6037                 for(int i=0;i<numdialogues;i++){
6038                     int realdialoguetype;
6039                     bool special;
6040                     if(dialoguetype[i]>49){
6041                         realdialoguetype=dialoguetype[i]-50;
6042                         special=1;
6043                     }
6044                     else if(dialoguetype[i]>39){
6045                         realdialoguetype=dialoguetype[i]-40;
6046                         special=1;
6047                     }
6048                     else if(dialoguetype[i]>29){
6049                         realdialoguetype=dialoguetype[i]-30;
6050                         special=1;
6051                     }
6052                     else if(dialoguetype[i]>19){
6053                         realdialoguetype=dialoguetype[i]-20;
6054                         special=1;
6055                     }
6056                     else if(dialoguetype[i]>9){
6057                         realdialoguetype=dialoguetype[i]-10;
6058                         special=1;
6059                     }
6060                     else {
6061                         realdialoguetype=dialoguetype[i];
6062                         special=0;
6063                     }
6064                     if((!hostile||dialoguetype[i]>40&&dialoguetype[i]<50)&&
6065                             realdialoguetype<numplayers&&
6066                             realdialoguetype>0&&
6067                             (dialoguegonethrough[i]==0||!special)&&
6068                             (special||Input::isKeyPressed(attackkey))){
6069                         if(findDistancefast(&player[0].coords,&player[realdialoguetype].coords)<6||
6070                                 player[realdialoguetype].howactive>=typedead1||
6071                                 dialoguetype[i]>40&&dialoguetype[i]<50){
6072                             whichdialogue=i;
6073                             for(int j=0;j<numdialogueboxes[whichdialogue];j++){
6074                                 player[participantfocus[whichdialogue][j]].coords=participantlocation[whichdialogue][participantfocus[whichdialogue][j]];
6075                                 player[participantfocus[whichdialogue][j]].rotation=participantrotation[whichdialogue][participantfocus[whichdialogue][j]];
6076                                 player[participantfocus[whichdialogue][j]].targetrotation=participantrotation[whichdialogue][participantfocus[whichdialogue][j]];
6077                                 player[participantfocus[whichdialogue][j]].velocity=0;
6078                                 player[participantfocus[whichdialogue][j]].targetanimation=player[participantfocus[whichdialogue][j]].getIdle();
6079                                 player[participantfocus[whichdialogue][j]].targetframe=0;
6080                             }
6081                             directing=0;
6082                             indialogue=0;
6083                             dialoguetime=0;
6084                             dialoguegonethrough[i]++;
6085                             if(dialogueboxsound[whichdialogue][indialogue]!=0){
6086                                 playdialogueboxsound();
6087                             }
6088                         }
6089                     }
6090                 }
6091
6092             windvar+=multiplier;
6093             smoketex+=multiplier;
6094             tutorialstagetime+=multiplier;
6095
6096             //hotspots
6097             static float hotspotvisual[40];
6098             if(numhotspots){
6099                 XYZ hotspotsprite;
6100                 if(editorenabled)
6101                     for(int i=0;i<numhotspots;i++)
6102                         hotspotvisual[i]-=multiplier/320;
6103
6104                 for(int i=0;i<numhotspots;i++){
6105                     //if(hotspottype[i]<=10)
6106                     while(hotspotvisual[i]<0){
6107                         hotspotsprite=0;
6108                         hotspotsprite.x=float(abs(Random()%100000))/100000*hotspotsize[i];
6109                         hotspotsprite=DoRotation(hotspotsprite,0,0,Random()%360);
6110                         hotspotsprite=DoRotation(hotspotsprite,0,Random()%360,0);
6111                         hotspotsprite+=hotspot[i];
6112                         Sprite::MakeSprite(breathsprite, hotspotsprite, hotspotsprite*0, 1,0.5,0, 7, 0.4);
6113                         hotspotvisual[i]+=0.1/hotspotsize[i]/hotspotsize[i]/hotspotsize[i];
6114                     }
6115                 }
6116
6117                 for(int i=0;i<numhotspots;i++){
6118                     if(hotspottype[i]<=10&&hotspottype[i]>0){
6119                         hotspot[i]=player[hotspottype[i]].coords;
6120                     }
6121                 }
6122             }
6123
6124             //Tutorial
6125             if(tutoriallevel){
6126                 doTutorial();
6127             }
6128
6129             //bonuses
6130             if(tutoriallevel!=1){
6131                 if(bonustime==0&&
6132                         bonus!=solidhit&&
6133                         bonus!=spinecrusher&&
6134                         bonus!=tracheotomy&&
6135                         bonus!=backstab&&
6136                         bonusvalue>10){
6137                     emit_sound_np(consolesuccesssound);
6138                 }
6139             } else if(bonustime==0){
6140                 emit_sound_np(fireendsound);
6141             }
6142             if(bonustime==0){
6143                 if(bonus!=solidhit&&
6144                         bonus!=twoxcombo&&
6145                         bonus!=threexcombo&&
6146                         bonus!=fourxcombo&&
6147                         bonus!=megacombo)
6148                     bonusnum[bonus]++;
6149                 else
6150                     bonusnum[bonus]+=0.15;
6151                 if(tutoriallevel)
6152                     bonusvalue=0;
6153                 bonusvalue/=bonusnum[bonus];
6154                 bonustotal+=bonusvalue;
6155             }
6156             bonustime+=multiplier;
6157
6158             //snow effects
6159             if(environment==snowyenvironment){
6160                 precipdelay-=multiplier;
6161                 while(precipdelay<0){
6162                     precipdelay+=.04;
6163                     if(!detail)
6164                         precipdelay+=.04;
6165                     XYZ footvel,footpoint;
6166
6167                     footvel=0;
6168                     footpoint=viewer+viewerfacing*6;
6169                     footpoint.y+=((float)abs(Random()%1200))/100-6;
6170                     footpoint.x+=((float)abs(Random()%1200))/100-6;
6171                     footpoint.z+=((float)abs(Random()%1200))/100-6;
6172                     Sprite::MakeSprite(snowsprite, footpoint,footvel, 1,1,1, .1, 1);
6173                 }
6174             }
6175
6176
6177             doAerialAcrobatics();
6178
6179
6180             static XYZ oldviewer;
6181
6182             //control keys
6183             if(indialogue==-1){
6184                 player[0].forwardkeydown=Input::isKeyDown(forwardkey);
6185                 player[0].leftkeydown=Input::isKeyDown(leftkey);
6186                 player[0].backkeydown=Input::isKeyDown(backkey);
6187                 player[0].rightkeydown=Input::isKeyDown(rightkey);
6188                 player[0].jumpkeydown=Input::isKeyDown(jumpkey);
6189                 player[0].crouchkeydown=Input::isKeyDown(crouchkey);
6190                 player[0].drawkeydown=Input::isKeyDown(drawkey);
6191                 player[0].throwkeydown=Input::isKeyDown(throwkey);
6192             }
6193             else
6194             {
6195                 player[0].forwardkeydown=0;
6196                 player[0].leftkeydown=0;
6197                 player[0].backkeydown=0;
6198                 player[0].rightkeydown=0;
6199                 player[0].jumpkeydown=0;
6200                 player[0].crouchkeydown=0;
6201                 player[0].drawkeydown=0;
6202                 player[0].throwkeydown=0;
6203             }
6204
6205             if(!player[0].jumpkeydown)
6206                 player[0].jumpclimb=0;
6207
6208
6209             if(indialogue!=-1){
6210                 cameramode=1;
6211                 if(directing){
6212                     facing=0;
6213                     facing.z=-1;
6214
6215                     facing=DoRotation(facing,-rotation2,0,0);
6216                     facing=DoRotation(facing,0,0-rotation,0);
6217
6218                     flatfacing=0;
6219                     flatfacing.z=-1;
6220
6221                     flatfacing=DoRotation(flatfacing,0,-rotation,0);
6222
6223                     if(Input::isKeyDown(forwardkey))
6224                         viewer+=facing*multiplier*4;
6225                     if(Input::isKeyDown(backkey))
6226                         viewer-=facing*multiplier*4;
6227                     if(Input::isKeyDown(leftkey))
6228                         viewer+=DoRotation(flatfacing*multiplier,0,90,0)*4;
6229                     if(Input::isKeyDown(rightkey))
6230                         viewer+=DoRotation(flatfacing*multiplier,0,-90,0)*4;
6231                     if(Input::isKeyDown(jumpkey))
6232                         viewer.y+=multiplier*4;
6233                     if(Input::isKeyDown(crouchkey))
6234                         viewer.y-=multiplier*4;
6235                     if(     Input::isKeyPressed(SDLK_1)||
6236                             Input::isKeyPressed(SDLK_2)||
6237                             Input::isKeyPressed(SDLK_3)||
6238                             Input::isKeyPressed(SDLK_4)||
6239                             Input::isKeyPressed(SDLK_5)||
6240                             Input::isKeyPressed(SDLK_6)||
6241                             Input::isKeyPressed(SDLK_7)||
6242                             Input::isKeyPressed(SDLK_8)||
6243                             Input::isKeyPressed(SDLK_9)||
6244                             Input::isKeyPressed(SDLK_0)||
6245                             Input::isKeyPressed(SDLK_MINUS)){
6246                         int whichend;
6247                         if(Input::isKeyPressed(SDLK_1))whichend=1;
6248                         if(Input::isKeyPressed(SDLK_2))whichend=2;
6249                         if(Input::isKeyPressed(SDLK_3))whichend=3;
6250                         if(Input::isKeyPressed(SDLK_4))whichend=4;
6251                         if(Input::isKeyPressed(SDLK_5))whichend=5;
6252                         if(Input::isKeyPressed(SDLK_6))whichend=6;
6253                         if(Input::isKeyPressed(SDLK_7))whichend=7;
6254                         if(Input::isKeyPressed(SDLK_8))whichend=8;
6255                         if(Input::isKeyPressed(SDLK_9))whichend=9;
6256                         if(Input::isKeyPressed(SDLK_0))whichend=0;
6257                         if(Input::isKeyPressed(SDLK_MINUS))
6258                             whichend=-1;
6259                         if(whichend!=-1){
6260                             participantfocus[whichdialogue][indialogue]=whichend;
6261                             participantlocation[whichdialogue][whichend]=player[whichend].coords;
6262                             participantrotation[whichdialogue][whichend]=player[whichend].rotation;
6263                         }
6264                         if(whichend==-1){
6265                             participantfocus[whichdialogue][indialogue]=-1;
6266                         }
6267                         if(player[participantfocus[whichdialogue][indialogue]].dead){
6268                             indialogue=-1;
6269                             directing=0;
6270                             cameramode=0;
6271                         }
6272                         dialoguecamera[whichdialogue][indialogue]=viewer;
6273                         dialoguecamerarotation[whichdialogue][indialogue]=rotation;
6274                         dialoguecamerarotation2[whichdialogue][indialogue]=rotation2;
6275                         indialogue++;
6276                         if(indialogue<numdialogueboxes[whichdialogue]){
6277                             if(dialogueboxsound[whichdialogue][indialogue]!=0){
6278                                 playdialogueboxsound();
6279                             }
6280                         }
6281
6282                         for(int j=0;j<numplayers;j++){
6283                             participantfacing[whichdialogue][indialogue][j]=participantfacing[whichdialogue][indialogue-1][j];
6284                         }
6285                     }
6286                     //TODO: should these be KeyDown or KeyPressed?
6287                     if(     Input::isKeyDown(SDLK_KP1)||
6288                             Input::isKeyDown(SDLK_KP2)||
6289                             Input::isKeyDown(SDLK_KP3)||
6290                             Input::isKeyDown(SDLK_KP4)||
6291                             Input::isKeyDown(SDLK_KP5)||
6292                             Input::isKeyDown(SDLK_KP6)||
6293                             Input::isKeyDown(SDLK_KP7)||
6294                             Input::isKeyDown(SDLK_KP8)||
6295                             Input::isKeyDown(SDLK_KP9)||
6296                             Input::isKeyDown(SDLK_KP0)){
6297                         int whichend;
6298                         if(Input::isKeyDown(SDLK_KP1))whichend=1;
6299                         if(Input::isKeyDown(SDLK_KP2))whichend=2;
6300                         if(Input::isKeyDown(SDLK_KP3))whichend=3;
6301                         if(Input::isKeyDown(SDLK_KP4))whichend=4;
6302                         if(Input::isKeyDown(SDLK_KP5))whichend=5;
6303                         if(Input::isKeyDown(SDLK_KP6))whichend=6;
6304                         if(Input::isKeyDown(SDLK_KP7))whichend=7;
6305                         if(Input::isKeyDown(SDLK_KP8))whichend=8;
6306                         if(Input::isKeyDown(SDLK_KP9))whichend=9;
6307                         if(Input::isKeyDown(SDLK_KP0))whichend=0;
6308                         participantfacing[whichdialogue][indialogue][whichend]=facing;
6309                     }
6310                     if(indialogue>=numdialogueboxes[whichdialogue]){
6311                         indialogue=-1;
6312                         directing=0;
6313                         cameramode=0;
6314                     }
6315                 }
6316                 if(!directing){
6317                     pause_sound(whooshsound);
6318                     viewer=dialoguecamera[whichdialogue][indialogue];
6319                     viewer.y=max((double)viewer.y,terrain.getHeight(viewer.x,viewer.z)+.1);
6320                     rotation=dialoguecamerarotation[whichdialogue][indialogue];
6321                     rotation2=dialoguecamerarotation2[whichdialogue][indialogue];
6322                     if(dialoguetime>0.5)
6323                         if(     Input::isKeyPressed(SDLK_1)||
6324                                 Input::isKeyPressed(SDLK_2)||
6325                                 Input::isKeyPressed(SDLK_3)||
6326                                 Input::isKeyPressed(SDLK_4)||
6327                                 Input::isKeyPressed(SDLK_5)||
6328                                 Input::isKeyPressed(SDLK_6)||
6329                                 Input::isKeyPressed(SDLK_7)||
6330                                 Input::isKeyPressed(SDLK_8)||
6331                                 Input::isKeyPressed(SDLK_9)||
6332                                 Input::isKeyPressed(SDLK_0)||
6333                                 Input::isKeyPressed(SDLK_MINUS)||
6334                                 Input::isKeyPressed(attackkey)){
6335                             indialogue++;
6336                             if(indialogue<numdialogueboxes[whichdialogue]){
6337                                 if(dialogueboxsound[whichdialogue][indialogue]!=0){
6338                                     playdialogueboxsound();
6339                                     if(dialogueboxsound[whichdialogue][indialogue]==-5){
6340                                         hotspot[numhotspots]=player[0].coords;
6341                                         hotspotsize[numhotspots]=10;
6342                                         hotspottype[numhotspots]=-1;
6343
6344                                         numhotspots++;
6345                                     }
6346                                     if(dialogueboxsound[whichdialogue][indialogue]==-6){
6347                                         hostile=1;
6348                                     }
6349
6350                                     if(player[participantfocus[whichdialogue][indialogue]].dead){
6351                                         indialogue=-1;
6352                                         directing=0;
6353                                         cameramode=0;
6354                                     }
6355                                 }
6356                             }
6357                         }
6358                     if(indialogue>=numdialogueboxes[whichdialogue]){
6359                         indialogue=-1;
6360                         directing=0;
6361                         cameramode=0;
6362                         if(dialoguetype[whichdialogue]>19&&dialoguetype[whichdialogue]<30){
6363                             hostile=1;
6364                         }
6365                         if(dialoguetype[whichdialogue]>29&&dialoguetype[whichdialogue]<40){
6366                             windialogue=true;
6367                         }
6368                         if(dialoguetype[whichdialogue]>49&&dialoguetype[whichdialogue]<60){
6369                             hostile=1;
6370                             for(int i=1;i<numplayers;i++){
6371                                 player[i].aitype = attacktypecutoff;
6372                             }
6373                         }
6374                     }
6375                 }
6376             }
6377
6378             if(!player[0].jumpkeydown){
6379                 player[0].jumptogglekeydown=0;
6380             }
6381             if(player[0].jumpkeydown&&
6382                     player[0].targetanimation!=jumpupanim&&
6383                     player[0].targetanimation!=jumpdownanim&&
6384                     !player[0].isFlip())
6385                 player[0].jumptogglekeydown=1;
6386
6387
6388             dialoguetime+=multiplier;
6389             hawkrotation+=multiplier*25;
6390             realhawkcoords=0;
6391             realhawkcoords.x=25;
6392             realhawkcoords=DoRotation(realhawkcoords,0,hawkrotation,0)+hawkcoords;
6393             hawkcalldelay-=multiplier/2;
6394
6395             if(hawkcalldelay<=0){
6396                 emit_sound_at(hawksound, realhawkcoords);
6397
6398                 hawkcalldelay=16+abs(Random()%8);
6399             }
6400
6401             doDebugKeys();
6402
6403             doAttacks();
6404
6405             doPlayerCollisions();
6406
6407             doJumpReversals();
6408
6409             for(int k=0;k<numplayers;k++)
6410                 if(k!=0&&player[k].immobile)
6411                     player[k].coords=player[k].realoldcoords;
6412
6413             for(int k=0;k<numplayers;k++){
6414                 if(!isnormal(player[k].coords.x)||!isnormal(player[k].coords.y)||!isnormal(player[k].coords.z)){
6415                     if(!isnormal(player[k].coords.x)||!isnormal(player[k].coords.y)||!isnormal(player[k].coords.z)){
6416                         player[k].DoDamage(1000);
6417                     }
6418                 }
6419             }
6420
6421             //respawn
6422             static bool respawnkeydown;
6423             if(!editorenabled&&
6424                     (whichlevel!=-2&&
6425                      (Input::isKeyDown(SDLK_z)&&
6426                       Input::isKeyDown(SDLK_LMETA)&&
6427                       debugmode)||
6428                      (Input::isKeyDown(jumpkey)&&
6429                       !respawnkeydown&&
6430                       !oldattackkey&&
6431                       player[0].dead))){
6432                 targetlevel=whichlevel;
6433                 loading=1;
6434                 leveltime=5;
6435             }
6436             if(!Input::isKeyDown(jumpkey))
6437                 respawnkeydown=0;
6438             if(Input::isKeyDown(jumpkey))
6439                 respawnkeydown=1;
6440
6441
6442
6443
6444             static bool movekey;
6445
6446             //?
6447             for(int i=0;i<numplayers;i++){
6448                 static float oldtargetrotation;
6449                 if(!player[i].skeleton.free){
6450                     oldtargetrotation=player[i].targetrotation;
6451                     if(i==0&&indialogue==-1){
6452                         //TODO: refactor repetitive code
6453                         if(!animation[player[0].targetanimation].attack&&
6454                                 player[0].targetanimation!=staggerbackhighanim&&
6455                                 player[0].targetanimation!=staggerbackhardanim&&
6456                                 player[0].targetanimation!=crouchremoveknifeanim&&
6457                                 player[0].targetanimation!=removeknifeanim&&
6458                                 player[0].targetanimation!=backhandspringanim&&
6459                                 player[0].targetanimation!=dodgebackanim&&
6460                                 player[0].targetanimation!=walljumprightkickanim&&
6461                                 player[0].targetanimation!=walljumpleftkickanim){
6462                             if(cameramode)
6463                                 player[0].targetrotation=0;
6464                             else
6465                                 player[0].targetrotation=-rotation+180;
6466                         }
6467
6468                         facing=0;
6469                         facing.z=-1;
6470
6471                         flatfacing=DoRotation(facing,0,player[i].rotation+180,0);
6472                         if(cameramode){
6473                             facing=flatfacing;
6474                         }else{
6475                             facing=DoRotation(facing,-rotation2,0,0);
6476                             facing=DoRotation(facing,0,0-rotation,0);
6477                         }
6478
6479                         player[0].lookrotation=-rotation;
6480
6481                         player[i].targetheadrotation=rotation;
6482                         player[i].targetheadrotation2=rotation2;
6483                     }
6484                     if(i!=0&&player[i].aitype==playercontrolled&&indialogue==-1){
6485                         if(!animation[player[i].targetanimation].attack&&
6486                                 player[i].targetanimation!=staggerbackhighanim&&
6487                                 player[i].targetanimation!=staggerbackhardanim&&
6488                                 player[i].targetanimation!=crouchremoveknifeanim&&
6489                                 player[i].targetanimation!=removeknifeanim&&
6490                                 player[i].targetanimation!=backhandspringanim&&
6491                                 player[i].targetanimation!=dodgebackanim&&
6492                                 player[i].targetanimation!=walljumprightkickanim&&
6493                                 player[i].targetanimation!=walljumpleftkickanim){
6494                             player[i].targetrotation=-player[i].lookrotation+180;
6495                         }
6496
6497                         facing=0;
6498                         facing.z=-1;
6499
6500                         flatfacing=DoRotation(facing,0,player[i].rotation+180,0);
6501
6502                         facing=DoRotation(facing,-player[i].lookrotation2,0,0);
6503                         facing=DoRotation(facing,0,0-player[i].lookrotation,0);
6504
6505                         player[i].targetheadrotation=player[i].lookrotation;
6506                         player[i].targetheadrotation2=player[i].lookrotation2;
6507                     }
6508                     if(indialogue!=-1){
6509                         player[i].targetheadrotation=180-roughDirection(participantfacing[whichdialogue][indialogue][i]);
6510                         player[i].targetheadrotation2=pitch(participantfacing[whichdialogue][indialogue][i]);
6511                     }
6512
6513                     if(leveltime<.5)
6514                         numenvsounds=0;
6515
6516                     player[i].avoidsomething=0;
6517
6518                     //avoid flaming things
6519                     for(int j=0;j<objects.numobjects;j++)
6520                         if(objects.onfire[j])
6521                             if(findDistancefast(&player[i].coords,&objects.position[j])<sq(objects.scale[j])*200)
6522                                 if(     findDistancefast(&player[i].coords,&objects.position[j])<
6523                                         findDistancefast(&player[i].coords,&player[0].coords)){
6524                                     player[i].collided=0;
6525                                     player[i].avoidcollided=1;
6526                                     if(player[i].avoidsomething==0||
6527                                             findDistancefast(&player[i].coords,&objects.position[j])<
6528                                             findDistancefast(&player[i].coords,&player[i].avoidwhere)){
6529                                         player[i].avoidwhere=objects.position[j];
6530                                         player[i].avoidsomething=1;
6531                                     }
6532                                 }
6533
6534                     //avoid flaming players
6535                     for(int j=0;j<numplayers;j++)
6536                         if(player[j].onfire)
6537                             if(findDistancefast(&player[j].coords,&player[i].coords)<sq(0.3)*200)
6538                                 if(     findDistancefast(&player[i].coords,&player[j].coords)<
6539                                         findDistancefast(&player[i].coords,&player[0].coords)){
6540                                     player[i].collided=0;
6541                                     player[i].avoidcollided=1;
6542                                     if(player[i].avoidsomething==0||
6543                                             findDistancefast(&player[i].coords,&player[j].coords)<
6544                                             findDistancefast(&player[i].coords,&player[i].avoidwhere)){
6545                                         player[i].avoidwhere=player[j].coords;
6546                                         player[i].avoidsomething=1;
6547                                     }
6548                                 }
6549
6550                     if(player[i].collided>.8)
6551                         player[i].avoidcollided=0;
6552
6553                     doAI(i);
6554
6555                     if(animation[player[i].targetanimation].attack==reversed){
6556                         //player[i].targetrotation=player[i].rotation;
6557                         player[i].forwardkeydown=0;
6558                         player[i].leftkeydown=0;
6559                         player[i].backkeydown=0;
6560                         player[i].rightkeydown=0;
6561                         player[i].jumpkeydown=0;
6562                         player[i].attackkeydown=0;
6563                         //player[i].crouchkeydown=0;
6564                         player[i].throwkeydown=0;
6565                     }
6566
6567                     if(indialogue!=-1){
6568                         player[i].forwardkeydown=0;
6569                         player[i].leftkeydown=0;
6570                         player[i].backkeydown=0;
6571                         player[i].rightkeydown=0;
6572                         player[i].jumpkeydown=0;
6573                         player[i].crouchkeydown=0;
6574                         player[i].drawkeydown=0;
6575                         player[i].throwkeydown=0;
6576                     }
6577
6578                     if(player[i].collided<-.3)
6579                         player[i].collided=-.3;
6580                     if(player[i].collided>1)
6581                         player[i].collided=1;
6582                     player[i].collided-=multiplier*4;
6583                     player[i].whichdirectiondelay-=multiplier;
6584                     if(player[i].avoidcollided<-.3||player[i].whichdirectiondelay<=0){
6585                         player[i].avoidcollided=-.3;
6586                         player[i].whichdirection=abs(Random()%2);
6587                         player[i].whichdirectiondelay=.4;
6588                     }
6589                     if(player[i].avoidcollided>1)
6590                         player[i].avoidcollided=1;
6591                     player[i].avoidcollided-=multiplier/4;
6592                     if(!player[i].skeleton.free){
6593                         player[i].stunned-=multiplier;
6594                         player[i].surprised-=multiplier;
6595                     }
6596                     if(i!=0&&player[i].surprised<=0&&
6597                             player[i].aitype==attacktypecutoff&&
6598                             !player[i].dead&&
6599                             !player[i].skeleton.free&&
6600                             animation[player[i].targetanimation].attack==neutral)
6601                         numresponded=1;
6602
6603                     if(!player[i].throwkeydown)
6604                         player[i].throwtogglekeydown=0;
6605
6606                     //pick up weapon
6607                     if(player[i].throwkeydown&&!player[i].throwtogglekeydown){
6608                         if(player[i].weaponactive==-1&&
6609                                 player[i].num_weapons<2&&
6610                                 (player[i].isIdle()||
6611                                  player[i].isCrouch()||
6612                                  player[i].targetanimation==sneakanim||
6613                                  player[i].targetanimation==rollanim||
6614                                  player[i].targetanimation==backhandspringanim||
6615                                  player[i].isFlip()||
6616                                  player[i].isFlip()||
6617                                  player[i].aitype!=playercontrolled)){
6618                             for(int j=0;j<weapons.size();j++){
6619                                 if((weapons[j].velocity.x==0&&weapons[j].velocity.y==0&&weapons[j].velocity.z==0||
6620                                             player[i].aitype==playercontrolled)&&
6621                                         weapons[j].owner==-1&&
6622                                         player[i].weaponactive==-1)
6623                                     if(findDistancefastflat(&player[i].coords,&weapons[j].position)<2){
6624                                         if(findDistancefast(&player[i].coords,&weapons[j].position)<2){
6625                                             if(player[i].isCrouch()||
6626                                                     player[i].targetanimation==sneakanim||
6627                                                     player[i].isRun()||
6628                                                     player[i].isIdle()||
6629                                                     player[i].aitype!=playercontrolled){
6630                                                 player[i].throwtogglekeydown=1;
6631                                                 player[i].setAnimation(crouchremoveknifeanim);
6632                                                 player[i].targetrotation=roughDirectionTo(player[i].coords,weapons[j].position);
6633                                                 player[i].hasvictim=0;
6634                                             }
6635                                             if(player[i].targetanimation==rollanim||player[i].targetanimation==backhandspringanim){
6636                                                 player[i].throwtogglekeydown=1;
6637                                                 player[i].hasvictim=0;
6638
6639                                                 if((weapons[j].velocity.x==0&&weapons[j].velocity.y==0&&weapons[j].velocity.z==0||
6640                                                                 player[i].aitype==playercontrolled)&&
6641                                                             weapons[j].owner==-1||
6642                                                         player[i].victim&&
6643                                                         weapons[j].owner==player[i].victim->id)
6644                                                     if(findDistancefastflat(&player[i].coords,&weapons[j].position)<2&&player[i].weaponactive==-1)
6645                                                         if(findDistancefast(&player[i].coords,&weapons[j].position)<1||player[i].victim){
6646                                                             if(weapons[j].getType()!=staff)
6647                                                                 emit_sound_at(knifedrawsound, player[i].coords, 128.);
6648
6649                                                             player[i].weaponactive=0;
6650                                                             weapons[j].owner=player[i].id;
6651                                                             if(player[i].num_weapons>0)
6652                                                                 player[i].weaponids[player[i].num_weapons]=player[i].weaponids[0];
6653                                                             player[i].num_weapons++;
6654                                                             player[i].weaponids[0]=j;
6655                                                         }
6656                                             }
6657                                         }else if((player[i].isIdle()||
6658                                                     player[i].isFlip()||
6659                                                     player[i].aitype!=playercontrolled)&&
6660                                                 findDistancefast(&player[i].coords,&weapons[j].position)<5&&
6661                                                 player[i].coords.y<weapons[j].position.y){
6662                                             if(!player[i].isFlip()){
6663                                                 player[i].throwtogglekeydown=1;
6664                                                 player[i].setAnimation(removeknifeanim);
6665                                                 player[i].targetrotation=roughDirectionTo(player[i].coords,weapons[j].position);
6666                                             }
6667                                             if(player[i].isFlip()){
6668                                                 player[i].throwtogglekeydown=1;
6669                                                 player[i].hasvictim=0;
6670
6671                                                 for(int k=0;k<weapons.size();k++){
6672                                                     if(player[i].weaponactive==-1)
6673                                                         if((weapons[k].velocity.x==0&&weapons[k].velocity.y==0&&weapons[k].velocity.z==0||
6674                                                                         player[i].aitype==playercontrolled)&&
6675                                                                     weapons[k].owner==-1||
6676                                                                 player[i].victim&&
6677                                                                  weapons[k].owner==player[i].victim->id)
6678                                                             if(findDistancefastflat(&player[i].coords,&weapons[k].position)<3&&
6679                                                                     player[i].weaponactive==-1){
6680                                                                 if(weapons[k].getType()!=staff)
6681                                                                     emit_sound_at(knifedrawsound, player[i].coords, 128.);
6682
6683                                                                 player[i].weaponactive=0;
6684                                                                 weapons[k].owner=player[i].id;
6685                                                                 if(player[i].num_weapons>0)
6686                                                                     player[i].weaponids[player[i].num_weapons]=player[i].weaponids[0];
6687                                                                 player[i].num_weapons++;
6688                                                                 player[i].weaponids[0]=k;
6689                                                             }
6690                                                 }
6691                                             }
6692                                         }
6693                                     }
6694                             }
6695                             if(player[i].isCrouch()||
6696                                     player[i].targetanimation==sneakanim||
6697                                     player[i].isRun()||
6698                                     player[i].isIdle()||player[i].targetanimation==rollanim||
6699                                     player[i].targetanimation==backhandspringanim){
6700                                 if(numplayers>1)
6701                                     for(int j=0;j<numplayers;j++){
6702                                         if(player[i].weaponactive==-1)
6703                                             if(j!=i)
6704                                                 if(player[j].num_weapons&&
6705                                                         player[j].skeleton.free&&
6706                                                         findDistancefast(&player[i].coords,&player[j].coords)<2/*&&player[j].dead*/&&
6707                                                         (((player[j].skeleton.forward.y<0&&
6708                                                            player[j].weaponstuckwhere==0)||
6709                                                           (player[j].skeleton.forward.y>0&&
6710                                                            player[j].weaponstuckwhere==1))||
6711                                                          player[j].weaponstuck==-1||
6712                                                          player[j].num_weapons>1)){
6713                                                     if(player[i].targetanimation!=rollanim&&player[i].targetanimation!=backhandspringanim){
6714                                                         player[i].throwtogglekeydown=1;
6715                                                         player[i].victim=&player[j];
6716                                                         player[i].hasvictim=1;
6717                                                         player[i].setAnimation(crouchremoveknifeanim);
6718                                                         player[i].targetrotation=roughDirectionTo(player[i].coords,player[j].coords);
6719                                                     }
6720                                                     if(player[i].targetanimation==rollanim||player[i].targetanimation==backhandspringanim){
6721                                                         player[i].throwtogglekeydown=1;
6722                                                         player[i].victim=&player[j];
6723                                                         player[i].hasvictim=1;
6724                                                         int k = player[j].weaponids[0];
6725                                                         if(player[i].hasvictim){
6726                                                             bool fleshstuck;
6727                                                             fleshstuck=0;
6728                                                             if(player[i].victim->weaponstuck!=-1){
6729                                                                 if(player[i].victim->weaponids[player[i].victim->weaponstuck]==k){
6730                                                                     fleshstuck=1;
6731                                                                 }
6732                                                             }
6733                                                             if(!fleshstuck){
6734                                                                 if(weapons[k].getType()!=staff)
6735                                                                   emit_sound_at(knifedrawsound, player[i].coords, 128.);
6736                                                             }
6737                                                             if(fleshstuck)
6738                                                               emit_sound_at(fleshstabremovesound, player[i].coords, 128.);
6739
6740                                                             player[i].weaponactive=0;
6741                                                             if(weapons[k].owner!=-1){
6742                                                                 if(player[i].victim->num_weapons==1)player[i].victim->num_weapons=0;
6743                                                                 else player[i].victim->num_weapons=1;
6744
6745                                                                 player[i].victim->skeleton.longdead=0;
6746                                                                 player[i].victim->skeleton.free=1;
6747                                                                 player[i].victim->skeleton.broken=0;
6748
6749                                                                 for(int l=0;l<player[i].victim->skeleton.num_joints;l++){
6750                                                                     player[i].victim->skeleton.joints[l].velchange=0;
6751                                                                     player[i].victim->skeleton.joints[l].locked=0;
6752                                                                 }
6753
6754                                                                 XYZ relative;
6755                                                                 relative=0;
6756                                                                 relative.y=10;
6757                                                                 Normalise(&relative);
6758                                                                 XYZ footvel,footpoint;
6759                                                                 footvel=0;
6760                                                                 footpoint=weapons[k].position;
6761                                                                 if(player[i].victim->weaponstuck!=-1){
6762                                                                     if(player[i].victim->weaponids[player[i].victim->weaponstuck]==k){
6763                                                                         if(bloodtoggle)Sprite::MakeSprite(cloudimpactsprite, footpoint,footvel, 1,0,0, .8, .3);
6764                                                                         weapons[k].bloody=2;
6765                                                                         weapons[k].blooddrip=5;
6766                                                                         player[i].victim->weaponstuck=-1;
6767                                                                         player[i].victim->bloodloss+=2000;
6768                                                                         player[i].victim->DoDamage(2000);
6769                                                                     }
6770                                                                 }
6771                                                                 if(player[i].victim->num_weapons>0){
6772                                                                     if(player[i].victim->weaponstuck!=0&&player[i].victim->weaponstuck!=-1)player[i].victim->weaponstuck=0;
6773                                                                     if(player[i].victim->weaponids[0]==k)
6774                                                                         player[i].victim->weaponids[0]=player[i].victim->weaponids[player[i].victim->num_weapons];
6775                                                                 }
6776
6777                                                                 player[i].victim->weaponactive=-1;
6778
6779                                                                 player[i].victim->getJointFor(abdomen).velocity+=relative*6;
6780                                                                 player[i].victim->getJointFor(neck).velocity+=relative*6;
6781                                                                 player[i].victim->getJointFor(rightshoulder).velocity+=relative*6;
6782                                                                 player[i].victim->getJointFor(leftshoulder).velocity+=relative*6;
6783                                                             }
6784                                                             weapons[k].owner=i;
6785                                                             if(player[i].num_weapons>0){
6786                                                                 player[i].weaponids[player[i].num_weapons]=player[i].weaponids[0];
6787                                                             }
6788                                                             player[i].num_weapons++;
6789                                                             player[i].weaponids[0]=k;
6790                                                         }
6791                                                     }
6792                                                 }
6793                                     }
6794                             }
6795                         }
6796                         if(player[i].weaponactive!=-1&&player[i].aitype==playercontrolled){
6797                             if(weapons[player[i].weaponids[0]].getType()==knife){
6798                                 if(player[i].isIdle()||
6799                                         player[i].isRun()||
6800                                         player[i].isCrouch()||
6801                                         player[i].targetanimation==sneakanim||
6802                                         player[i].isFlip())
6803                                     if(numplayers>1)
6804                                         for(int j=0;j<numplayers;j++){
6805                                             if(i!=j)
6806                                                 if(tutoriallevel!=1||tutorialstage==49)
6807                                                     if(hostile)
6808                                                         if(normaldotproduct(player[i].facing,player[i].coords-player[j].coords)<0&&
6809                                                                 findDistancefast(&player[i].coords,&player[j].coords)<100&&
6810                                                                 findDistancefast(&player[i].coords,&player[j].coords)>1.5&&
6811                                                                 !player[j].skeleton.free&&
6812                                                                 -1==checkcollide(DoRotation(player[j].getJointFor(head).position,0,player[j].rotation,0)*player[j].scale+player[j].coords,DoRotation(player[i].getJointFor(head).position,0,player[i].rotation,0)*player[i].scale+player[i].coords)){
6813                                                             if(!player[i].isFlip()){
6814                                                                 player[i].throwtogglekeydown=1;
6815                                                                 player[i].victim=&player[j];
6816                                                                 player[i].setAnimation(knifethrowanim);
6817                                                                 player[i].targetrotation=roughDirectionTo(player[i].coords,player[j].coords);
6818                                                                 player[i].targettilt2=pitchTo(player[i].coords,player[j].coords);
6819                                                             }
6820                                                             if(player[i].isFlip()){
6821                                                                 if(player[i].weaponactive!=-1){
6822                                                                     player[i].throwtogglekeydown=1;
6823                                                                     player[i].victim=&player[j];
6824                                                                     XYZ aim;
6825                                                                     weapons[player[i].weaponids[0]].owner=-1;
6826                                                                     aim=player[i].victim->coords+DoRotation(player[i].victim->getJointFor(abdomen).position,0,player[i].victim->rotation,0)*player[i].victim->scale+player[i].victim->velocity*findDistance(&player[i].victim->coords,&player[i].coords)/50-(player[i].coords+DoRotation(player[i].getJointFor(righthand).position,0,player[i].rotation,0)*player[i].scale);
6827                                                                     Normalise(&aim);
6828
6829                                                                     aim=DoRotation(aim,(float)abs(Random()%30)-15,(float)abs(Random()%30)-15,0);
6830
6831                                                                     weapons[player[i].weaponids[0]].velocity=aim*50;
6832                                                                     weapons[player[i].weaponids[0]].tipvelocity=aim*50;
6833                                                                     weapons[player[i].weaponids[0]].missed=0;
6834                                                                     weapons[player[i].weaponids[0]].freetime=0;
6835                                                                     weapons[player[i].weaponids[0]].firstfree=1;
6836                                                                     weapons[player[i].weaponids[0]].physics=0;
6837                                                                     player[i].num_weapons--;
6838                                                                     if(player[i].num_weapons){
6839                                                                         player[i].weaponids[0]=player[i].weaponids[player[i].num_weapons];
6840                                                                     }
6841                                                                     player[i].weaponactive=-1;
6842                                                                 }
6843                                                             }
6844                                                         }
6845                                         }
6846                             }
6847                         }
6848                         if(player[i].weaponactive!=-1&&player[i].aitype==playercontrolled){
6849                             if(player[i].isCrouch()||player[i].targetanimation==sneakanim){
6850                                 player[i].throwtogglekeydown=1;
6851                                 weapons[player[i].weaponids[0]].owner=-1;
6852                                 weapons[player[i].weaponids[0]].velocity=player[i].velocity*.2;
6853                                 if(weapons[player[i].weaponids[0]].velocity.x==0)weapons[player[i].weaponids[0]].velocity.x=.1;
6854                                 weapons[player[i].weaponids[0]].tipvelocity=weapons[player[i].weaponids[0]].velocity;
6855                                 weapons[player[i].weaponids[0]].missed=1;
6856                                 weapons[player[i].weaponids[0]].freetime=0;
6857                                 weapons[player[i].weaponids[0]].firstfree=1;
6858                                 weapons[player[i].weaponids[0]].physics=1;
6859                                 player[i].num_weapons--;
6860                                 if(player[i].num_weapons){
6861                                     player[i].weaponids[0]=player[i].weaponids[player[i].num_weapons];
6862                                     if(player[i].weaponstuck==player[i].num_weapons)player[i].weaponstuck=0;
6863                                 }
6864
6865                                 player[i].weaponactive=-1;
6866                                 for(int j=0;j<numplayers;j++){
6867                                     player[j].wentforweapon=0;
6868                                 }
6869                             }
6870                         }
6871
6872                     }
6873
6874                     //draw weapon
6875                     if(i==0||!player[0].dead||player[i].weaponactive!=-1) {
6876                         if(player[i].drawkeydown&&!player[i].drawtogglekeydown||
6877                                 player[i].num_weapons==2&&
6878                                 player[i].weaponactive==-1&&
6879                                 player[i].isIdle()||
6880                                 player[0].dead&&
6881                                 player[i].weaponactive!=-1&&
6882                                 i!=0) {
6883                             bool isgood=true;
6884                             if(player[i].weaponactive!=-1)
6885                                 if(weapons[player[i].weaponids[player[i].weaponactive]].getType()==staff)
6886                                     isgood=false;
6887                             if(isgood&&player[i].creature!=wolftype){
6888                                 if(player[i].isIdle()&&player[i].num_weapons&&weapons[player[i].weaponids[0]].getType()==knife){
6889                                     player[i].setAnimation(drawrightanim);
6890                                     player[i].drawtogglekeydown=1;
6891                                 }
6892                                 if((player[i].isIdle()||
6893                                             (player[i].aitype!=playercontrolled&&
6894                                              player[0].weaponactive!=-1&&
6895                                              player[i].isRun()))&&
6896                                         player[i].num_weapons&&
6897                                         weapons[player[i].weaponids[0]].getType()==sword){
6898                                     player[i].setAnimation(drawleftanim);
6899                                     player[i].drawtogglekeydown=1;
6900                                 }
6901                                 if(player[i].isCrouch()&&player[i].num_weapons&&weapons[player[i].weaponids[0]].getType()==knife){
6902                                     player[i].setAnimation(crouchdrawrightanim);
6903                                     player[i].drawtogglekeydown=1;
6904                                 }
6905                             }
6906                         }
6907                     }
6908                     
6909                     //clean weapon
6910                     if(player[i].weaponactive!=-1) {
6911                                                 if (player[i].isCrouch()&&
6912                                                         weapons[player[i].weaponids[player[i].weaponactive]].bloody&&
6913                                                         bloodtoggle&&
6914                                                         player[i].onterrain&&
6915                                                         player[i].num_weapons&&
6916                                                         player[i].attackkeydown&&
6917                                                         musictype!=stream_fighttheme) {
6918                                                                 if(weapons[player[i].weaponids[player[i].weaponactive]].getType()==knife)
6919                                                                         player[i].setAnimation(crouchstabanim);
6920                                                                 if(weapons[player[i].weaponids[player[i].weaponactive]].getType()==sword)
6921                                                                         player[i].setAnimation(swordgroundstabanim);
6922                                                                 player[i].hasvictim=0;
6923                                                 }
6924                                         }
6925
6926                     if(!player[i].drawkeydown)
6927                         player[i].drawtogglekeydown=0;
6928
6929                     XYZ absflatfacing;
6930                     if(i==0) {
6931                         absflatfacing=0;
6932                         absflatfacing.z=-1;
6933
6934                         absflatfacing=DoRotation(absflatfacing,0,-rotation,0);
6935                     } else
6936                                                 absflatfacing=flatfacing;
6937
6938                     if(indialogue!=-1){
6939                         player[i].forwardkeydown=0;
6940                         player[i].leftkeydown=0;
6941                         player[i].backkeydown=0;
6942                         player[i].rightkeydown=0;
6943                         player[i].jumpkeydown=0;
6944                         player[i].crouchkeydown=0;
6945                         player[i].drawkeydown=0;
6946                         player[i].throwkeydown=0;
6947                     }
6948                     movekey=0;
6949                     //Do controls
6950                     if(!animation[player[i].targetanimation].attack&&
6951                             player[i].targetanimation!=staggerbackhighanim&&
6952                             player[i].targetanimation!=staggerbackhardanim&&
6953                             player[i].targetanimation!=backhandspringanim&&
6954                             player[i].targetanimation!=dodgebackanim){
6955                         if(!player[i].forwardkeydown)
6956                             player[i].forwardstogglekeydown=0;
6957                         if(player[i].crouchkeydown){
6958                             //Crouch
6959                             target=-2;
6960                             if(i==0){
6961                                 player[i].superruntoggle=1;
6962                                 if(numplayers>1)
6963                                     for(int j=0;j<numplayers;j++)
6964                                         if(j!=i&&!player[j].skeleton.free&&player[j].aitype==passivetype)
6965                                             if(findDistancefast(&player[j].coords,&player[i].coords)<16)
6966                                                 player[i].superruntoggle=0;
6967                             }
6968
6969                             if(numplayers>1)
6970                                 for(int j=0;j<numplayers;j++){
6971                                     if(j!=i&&!player[j].skeleton.free&&player[j].victim&&player[i].lowreversaldelay<=0){
6972                                         if(findDistancefast(&player[j].coords,&player[j].victim->coords)<3&&
6973                                                 player[j].victim==&player[i]&&
6974                                                 (player[j].targetanimation==sweepanim||
6975                                                  player[j].targetanimation==upunchanim||
6976                                                  player[j].targetanimation==wolfslapanim||
6977                                                  ((player[j].targetanimation==swordslashanim||
6978                                                    player[j].targetanimation==knifeslashstartanim||
6979                                                    player[j].targetanimation==staffhitanim||
6980                                                    player[j].targetanimation==staffspinhitanim)&&
6981                                                   findDistancefast(&player[j].coords,&player[i].coords)<2))){
6982                                             if(target>=0)
6983                                                 target=-1;
6984                                             else
6985                                                 target=j;
6986                                         }
6987                                     }
6988                                 }
6989                             if(target>=0)
6990                                 player[target].Reverse();
6991                             player[i].lowreversaldelay=.5;
6992
6993                             if(player[i].isIdle()){
6994                                 player[i].setAnimation(player[i].getCrouch());
6995                                 player[i].transspeed=10;
6996                             }
6997                             if(player[i].isRun()||
6998                                     (player[i].isStop()&&
6999                                      (player[i].leftkeydown||
7000                                       player[i].rightkeydown||
7001                                       player[i].forwardkeydown||
7002                                       player[i].backkeydown))){
7003                                 player[i].setAnimation(rollanim);
7004                                 player[i].transspeed=20;
7005                             }
7006                         }
7007                         if(!player[i].crouchkeydown){
7008                             //Uncrouch
7009                             if(!player[i].isRun()&&player[i].targetanimation!=sneakanim&&i==0)player[i].superruntoggle=0;
7010                             target=-2;
7011                             if(player[i].isCrouch()){
7012                                 if(numplayers>1)
7013                                     for(int j=0;j<numplayers;j++){
7014                                         if(j!=i&&
7015                                                 !player[j].skeleton.free&&
7016                                                 player[j].victim&&
7017                                                 player[i].highreversaldelay<=0){
7018                                             if(findDistancefast(&player[j].coords,&player[j].victim->coords)<3&&
7019                                                     player[j].victim==&player[i]&&
7020                                                     (player[j].targetanimation==spinkickanim)&&
7021                                                     player[i].isCrouch()){
7022                                                 if(target>=0)
7023                                                     target=-1;
7024                                                 else
7025                                                     target=j;
7026                                             }
7027                                         }
7028                                     }
7029                                 if(target>=0)
7030                                     player[target].Reverse();
7031                                 player[i].highreversaldelay=.5;
7032
7033                                 if(player[i].isCrouch()){
7034                                     if(!player[i].wasCrouch()){
7035                                         player[i].currentanimation=player[i].getCrouch();
7036                                         player[i].currentframe=0;
7037                                     }
7038                                     player[i].setAnimation(player[i].getIdle());
7039                                     player[i].transspeed=10;
7040                                 }
7041                             }
7042                             if(player[i].targetanimation==sneakanim){
7043                                 player[i].setAnimation(player[i].getIdle());
7044                                 player[i].transspeed=10;
7045                             }
7046                         }
7047                         if(player[i].forwardkeydown){
7048                             if(player[i].isIdle()||
7049                                     (player[i].isStop()&&
7050                                      player[i].targetrotation==player[i].rotation)||
7051                                     (player[i].isLanding()&&
7052                                      player[i].targetframe>0&&
7053                                      !player[i].jumpkeydown)||
7054                                     (player[i].isLandhard()&&
7055                                      player[i].targetframe>0&&
7056                                      !player[i].jumpkeydown&&
7057                                      player[i].crouchkeydown)){
7058                                 if(player[i].aitype==passivetype)
7059                                     player[i].setAnimation(walkanim);
7060                                 else
7061                                     player[i].setAnimation(player[i].getRun());
7062                             }
7063                             if(player[i].isCrouch()){
7064                                 player[i].targetanimation=sneakanim;
7065                                 if(player[i].wasCrouch())
7066                                     player[i].target=0;
7067                                 player[i].targetframe=0;
7068                             }
7069                             if(player[i].targetanimation==hanganim/*&&(!player[i].forwardstogglekeydown||player[i].aitype!=playercontrolled)*/){
7070                                 player[i].setAnimation(climbanim);
7071                                 player[i].targetframe=1;
7072                                 player[i].jumpclimb=1;
7073                             }
7074                             if(player[i].targetanimation==jumpupanim||player[i].targetanimation==jumpdownanim||player[i].isFlip()){
7075                                 player[i].velocity+=absflatfacing*5*multiplier;
7076                             }
7077                             player[i].forwardstogglekeydown=1;
7078                             movekey=1;
7079                         }
7080                         if (player[i].rightkeydown){
7081                             if(player[i].isIdle()||
7082                                     (player[i].isStop()&&
7083                                      player[i].targetrotation==player[i].rotation)||
7084                                     (player[i].isLanding()&&
7085                                      player[i].targetframe>0&&
7086                                      !player[i].jumpkeydown)||
7087                                     (player[i].isLandhard()&&
7088                                      player[i].targetframe>0&&
7089                                      !player[i].jumpkeydown&&
7090                                      player[i].crouchkeydown)){
7091                                 player[i].setAnimation(player[i].getRun());
7092                             }
7093                             if(player[i].isCrouch()){
7094                                 player[i].targetanimation=sneakanim;
7095                                 if(player[i].wasCrouch())
7096                                     player[i].target=0;
7097                                 player[i].targetframe=0;
7098                             }
7099                             if(player[i].targetanimation==jumpupanim||player[i].targetanimation==jumpdownanim||player[i].isFlip()){
7100                                 player[i].velocity+=DoRotation(absflatfacing*5*multiplier,0,-90,0);
7101                             }
7102                             player[i].targetrotation-=90;
7103                             if(player[i].forwardkeydown)player[i].targetrotation+=45;
7104                             if(player[i].backkeydown)player[i].targetrotation-=45;
7105                             movekey=1;
7106                         }
7107                         if ( player[i].leftkeydown){
7108                             if(player[i].isIdle()||
7109                                     (player[i].isStop()&&
7110                                      player[i].targetrotation==player[i].rotation)||
7111                                     (player[i].isLanding()&&
7112                                      player[i].targetframe>0&&
7113                                      !player[i].jumpkeydown)||
7114                                     (player[i].isLandhard()&&
7115                                      player[i].targetframe>0&&
7116                                      !player[i].jumpkeydown&&
7117                                      player[i].crouchkeydown)){
7118                                 player[i].setAnimation(player[i].getRun());
7119                             }
7120                             if(player[i].isCrouch()){
7121                                 player[i].targetanimation=sneakanim;
7122                                 if(player[i].wasCrouch())
7123                                     player[i].target=0;
7124                                 player[i].targetframe=0;
7125                             }
7126                             if(player[i].targetanimation==jumpupanim||player[i].targetanimation==jumpdownanim||player[i].isFlip()){
7127                                 player[i].velocity-=DoRotation(absflatfacing*5*multiplier,0,-90,0);
7128                             }
7129                             player[i].targetrotation+=90;
7130                             if(player[i].forwardkeydown)player[i].targetrotation-=45;
7131                             if(player[i].backkeydown)player[i].targetrotation+=45;
7132                             movekey=1;
7133                         }
7134                         if(player[i].backkeydown){
7135                             if(player[i].isIdle()||
7136                                     (player[i].isStop()&&
7137                                      player[i].targetrotation==player[i].rotation)||
7138                                     (player[i].isLanding()&&
7139                                      player[i].targetframe>0&&
7140                                      !player[i].jumpkeydown)||
7141                                     (player[i].isLandhard()&&
7142                                      player[i].targetframe>0&&
7143                                      !player[i].jumpkeydown&&
7144                                      player[i].crouchkeydown)){
7145                                 player[i].setAnimation(player[i].getRun());
7146                             }
7147                             if(player[i].isCrouch()){
7148                                 player[i].targetanimation=sneakanim;
7149                                 if(player[i].wasCrouch())
7150                                     player[i].target=0;
7151                                 player[i].targetframe=0;
7152                             }
7153                             if(player[i].targetanimation==jumpupanim||player[i].targetanimation==jumpdownanim||player[i].isFlip()){
7154                                 player[i].velocity-=absflatfacing*5*multiplier;
7155                             }
7156                             if(player[i].targetanimation==hanganim){
7157                                 player[i].currentanimation=jumpdownanim;
7158                                 player[i].targetanimation=jumpdownanim;
7159                                 player[i].target=0;
7160                                 player[i].currentframe=0;
7161                                 player[i].targetframe=1;
7162                                 player[i].velocity=0;
7163                                 player[i].velocity.y+=gravity;
7164                                 player[i].coords.y-=1.4;
7165                                 player[i].grabdelay=1;
7166                             }
7167                             if ( !player[i].leftkeydown&&!player[i].rightkeydown)
7168                                 player[i].targetrotation+=180;
7169                             movekey=1;
7170                         }
7171                         if((player[i].jumpkeydown&&!player[i].jumpclimb)||player[i].jumpstart){
7172                             if((((player[i].isLanding()&&player[i].targetframe>=3)||
7173                                             player[i].isRun()||
7174                                             player[i].targetanimation==walkanim||
7175                                             player[i].isCrouch()||
7176                                             player[i].targetanimation==sneakanim)&&
7177                                         player[i].jumppower>1)&&
7178                                     ((player[i].targetanimation!=rabbitrunninganim&&
7179                                       player[i].targetanimation!=wolfrunninganim)||i!=0)){
7180                                 player[i].jumpstart=0;
7181                                 player[i].setAnimation(jumpupanim);
7182                                 player[i].rotation=player[i].targetrotation;
7183                                 player[i].transspeed=20;
7184                                 player[i].FootLand(0,1);
7185                                 player[i].FootLand(1,1);
7186
7187                                 facing=0;
7188                                 facing.z=-1;
7189                                 flatfacing=DoRotation(facing,0,player[i].targetrotation+180,0);
7190
7191                                 if(movekey)player[i].velocity=flatfacing*player[i].speed*45*player[i].scale;
7192                                 if(!movekey)player[i].velocity=0;
7193
7194                                 //Dodge sweep?
7195                                 target=-2;
7196                                 if(numplayers>1)
7197                                     for(int j=0;j<numplayers;j++){
7198                                         if(j!=i&&!player[j].skeleton.free&&player[j].victim){
7199                                             if(findDistancefast(&player[j].coords,&player[j].victim->coords)<3&&
7200                                                     player[j].victim==&player[i]&&
7201                                                     (player[j].targetanimation==sweepanim)){
7202                                                 if(target>=0)target=-1;
7203                                                 else target=j;
7204                                             }
7205                                         }
7206                                     }
7207                                     if(target>=0)player[i].velocity.y=1;
7208                                     else if(player[i].crouchkeydown||player[i].aitype!=playercontrolled){
7209                                         player[i].velocity.y=7;
7210                                         player[i].crouchtogglekeydown=1;
7211                                     }
7212                                     else player[i].velocity.y=5;
7213
7214                                     if(mousejump&&i==0&&debugmode){
7215                                         if(!player[i].isLanding())player[i].tempdeltav=deltav;
7216                                         if(player[i].tempdeltav<0)player[i].velocity.y-=(float)(player[i].tempdeltav)/multiplier/1000;
7217                                     }
7218
7219                                     player[i].coords.y+=.2;
7220                                     player[i].jumppower-=1;
7221
7222                                     if (!i)
7223                                       emit_sound_at(whooshsound, player[i].coords, 128.);
7224
7225                                     emit_sound_at(jumpsound, player[i].coords, 128.);
7226                             }
7227                             if((player[i].isIdle())&&player[i].jumppower>1){
7228                                 player[i].setAnimation(player[i].getLanding());
7229                                 player[i].targetframe=2;
7230                                 player[i].landhard=0;
7231                                 player[i].jumpstart=1;
7232                                 player[i].tempdeltav=deltav;
7233                             }
7234                             if(player[i].targetanimation==jumpupanim&&
7235                                     (((!floatjump&&
7236                                        !editorenabled)||
7237                                       !debugmode)||
7238                                      player[i].aitype!=playercontrolled)){
7239                                 if(player[i].jumppower>multiplier*6){
7240                                     player[i].velocity.y+=multiplier*6;
7241                                     player[i].jumppower-=multiplier*6;
7242                                 }
7243                                 if(player[i].jumppower<=multiplier*6){
7244                                     player[i].velocity.y+=player[i].jumppower;
7245                                     player[i].jumppower=0;
7246                                 }
7247                             }
7248                             if(((floatjump||editorenabled)&&debugmode)&&i==0)player[i].velocity.y+=multiplier*30;
7249                         }
7250
7251                         if(!movekey){
7252                             if(player[i].isRun()||player[i].targetanimation==walkanim)
7253                                 player[i].setAnimation(player[i].getStop());
7254                             if(player[i].targetanimation==sneakanim){
7255                                 player[i].targetanimation=player[i].getCrouch();
7256                                 if(player[i].currentanimation==sneakanim)
7257                                     player[i].target=0;
7258                                 player[i].targetframe=0;
7259                             }
7260                         }
7261                         if(player[i].targetanimation==walkanim&&
7262                                 (player[i].aitype==attacktypecutoff||
7263                                  player[i].aitype==searchtype||
7264                                  (player[i].aitype==passivetype&&
7265                                   player[i].numwaypoints<=1)))
7266                             player[i].setAnimation(player[i].getStop());
7267                         if(player[i].isRun()&&(player[i].aitype==passivetype))
7268                             player[i].setAnimation(player[i].getStop());
7269                     }
7270                 }
7271                 if(player[i].targetanimation==rollanim)
7272                     player[i].targetrotation=oldtargetrotation;
7273             }
7274
7275             //Rotation
7276             for(int k=0;k<numplayers;k++){
7277                 if(fabs(player[k].rotation-player[k].targetrotation)>180){
7278                     if(player[k].rotation>player[k].targetrotation)
7279                         player[k].rotation-=360;
7280                     else
7281                         player[k].rotation+=360;
7282                 }
7283
7284                 //stop to turn in right direction
7285                 if(fabs(player[k].rotation-player[k].targetrotation)>90&&(player[k].isRun()||player[k].targetanimation==walkanim))
7286                     player[k].setAnimation(player[k].getStop());
7287
7288                 if(player[k].targetanimation==backhandspringanim||player[k].targetanimation==dodgebackanim)
7289                     player[k].targettilt=0;
7290
7291                 if(player[k].targetanimation!=jumpupanim&&
7292                         player[k].targetanimation!=backhandspringanim&&
7293                         player[k].targetanimation!=jumpdownanim&&
7294                         !player[k].isFlip()){
7295                     player[k].targettilt=0;
7296                     if(player[k].jumppower<0&&!player[k].jumpkeydown)
7297                         player[k].jumppower=0;
7298                     player[k].jumppower+=multiplier*7;
7299                     if(player[k].isCrouch())
7300                         player[k].jumppower+=multiplier*7;
7301                     if(player[k].jumppower>5)
7302                         player[k].jumppower=5;
7303                 }
7304
7305                 if(player[k].isRun())
7306                     player[k].targettilt=(player[k].rotation-player[k].targetrotation)/4;
7307
7308                 player[k].tilt=stepTowardf(player[k].tilt,player[k].targettilt,multiplier*150);
7309                 player[k].grabdelay-=multiplier;
7310             }
7311
7312             //do animations
7313             for(int k=0;k<numplayers;k++){
7314                 player[k].DoAnimations();
7315                 player[k].whichpatchx=player[k].coords.x/(terrain.size/subdivision*terrain.scale);
7316                 player[k].whichpatchz=player[k].coords.z/(terrain.size/subdivision*terrain.scale);
7317             }
7318
7319             //do stuff
7320             objects.DoStuff();
7321             
7322             for(int j=numenvsounds-1;j>=0;j--){
7323                 envsoundlife[j]-=multiplier;
7324                 if(envsoundlife[j]<0){
7325                     numenvsounds--;
7326                     envsoundlife[j]=envsoundlife[numenvsounds];
7327                     envsound[j]=envsound[numenvsounds];
7328                 }
7329             }
7330             if(slomo)
7331                 OPENAL_SetFrequency(OPENAL_ALL, slomofreq);
7332             else
7333                 OPENAL_SetFrequency(OPENAL_ALL, 22050);
7334
7335             if(tutoriallevel==1){
7336                 XYZ temp;
7337                 XYZ temp2;
7338                 XYZ temp3;
7339                 XYZ oldtemp;
7340                 XYZ oldtemp2;
7341                 temp.x=1011;
7342                 temp.y=84;
7343                 temp.z=491;
7344                 temp2.x=1025;
7345                 temp2.y=75;
7346                 temp2.z=447;
7347                 temp3.x=1038;
7348                 temp3.y=76;
7349                 temp3.z=453;
7350                 oldtemp=temp;
7351                 oldtemp2=temp2;
7352                 if(tutorialstage>=51)
7353                     if(findDistancefast(&temp,&player[0].coords)>=findDistancefast(&temp,&temp2)-1||findDistancefast(&temp3,&player[0].coords)<4){
7354                         OPENAL_StopSound(OPENAL_ALL);  // hack...OpenAL renderer isn't stopping music after tutorial goes to level menu...
7355                         OPENAL_SetFrequency(OPENAL_ALL, 0.001);
7356
7357                         emit_stream_np(stream_menutheme);
7358
7359                         gameon=0;
7360                         mainmenu=5;
7361
7362                         fireSound();
7363
7364                         flash();
7365                     }
7366                 if(tutorialstage<51)
7367                     if(findDistancefast(&temp,&player[0].coords)>=findDistancefast(&temp,&temp2)-1||findDistancefast(&temp3,&player[0].coords)<4){
7368                         emit_sound_at(fireendsound, player[0].coords);
7369
7370                         player[0].coords=(oldtemp+oldtemp2)/2;
7371
7372                         flash();
7373                     }
7374                 if(tutorialstage>=14&&tutorialstage<50)
7375                     if(findDistancefast(&temp,&player[1].coords)>=findDistancefast(&temp,&temp2)-1||findDistancefast(&temp3,&player[1].coords)<4){
7376                         emit_sound_at(fireendsound, player[1].coords);
7377
7378                         for(int i=0;i<player[1].skeleton.num_joints;i++){
7379                             if(Random()%2==0){
7380                                 if(!player[1].skeleton.free)temp2=(player[1].coords-player[1].oldcoords)/multiplier/2;//velocity/2;
7381                                 if(player[1].skeleton.free)temp2=player[1].skeleton.joints[i].velocity*player[1].scale/2;
7382                                 if(!player[1].skeleton.free)temp=DoRotation(DoRotation(DoRotation(player[1].skeleton.joints[i].position,0,0,player[1].tilt),player[1].tilt2,0,0),0,player[1].rotation,0)*player[1].scale+player[1].coords;
7383                                 if(player[1].skeleton.free)temp=player[1].skeleton.joints[i].position*player[1].scale+player[1].coords;
7384                                 Sprite::MakeSprite(breathsprite, temp,temp2, 1,1,1, .6+(float)abs(Random()%100)/200-.25, 1);
7385                             }
7386                         }
7387
7388                         player[1].coords=(oldtemp+oldtemp2)/2;
7389                         for(int i=0;i<player[1].skeleton.num_joints;i++){
7390                             player[1].skeleton.joints[i].velocity=0;
7391                             if(Random()%2==0){
7392                                 if(!player[1].skeleton.free)temp2=(player[1].coords-player[1].oldcoords)/multiplier/2;//velocity/2;
7393                                 if(player[1].skeleton.free)temp2=player[1].skeleton.joints[i].velocity*player[1].scale/2;
7394                                 if(!player[1].skeleton.free)temp=DoRotation(DoRotation(DoRotation(player[1].skeleton.joints[i].position,0,0,player[1].tilt),player[1].tilt2,0,0),0,player[1].rotation,0)*player[1].scale+player[1].coords;
7395                                 if(player[1].skeleton.free)temp=player[1].skeleton.joints[i].position*player[1].scale+player[1].coords;
7396                                 Sprite::MakeSprite(breathsprite, temp,temp2, 1,1,1, .6+(float)abs(Random()%100)/200-.25, 1);
7397                             }
7398                         }
7399                     }
7400             }
7401
7402
7403             //3d sound
7404             static float gLoc[3];
7405             gLoc[0]=viewer.x;
7406             gLoc[1]=viewer.y;
7407             gLoc[2]=viewer.z;
7408             static float vel[3];
7409             vel[0]=(viewer.x-oldviewer.x)/multiplier;
7410             vel[1]=(viewer.y-oldviewer.y)/multiplier;
7411             vel[2]=(viewer.z-oldviewer.z)/multiplier;
7412
7413             //Set orientation with forward and up vectors
7414             static XYZ upvector;
7415             upvector=0;
7416             upvector.z=-1;
7417
7418             upvector=DoRotation(upvector,-rotation2+90,0,0);
7419             upvector=DoRotation(upvector,0,0-rotation,0);
7420
7421             facing=0;
7422             facing.z=-1;
7423
7424             facing=DoRotation(facing,-rotation2,0,0);
7425             facing=DoRotation(facing,0,0-rotation,0);
7426
7427
7428             static float ori[6];
7429             ori[0] = -facing.x;
7430             ori[1] = facing.y;
7431             ori[2] = -facing.z;
7432             ori[3] = -upvector.x;
7433             ori[4] = upvector.y;
7434             ori[5] = -upvector.z;
7435
7436             OPENAL_3D_Listener_SetAttributes(&gLoc[0], &vel[0], ori[0], ori[1], ori[2], ori[3], ori[4], ori[5]);
7437             OPENAL_Update();
7438
7439             oldviewer=viewer;
7440                 }
7441         }
7442
7443         if(Input::isKeyPressed(SDLK_F1))
7444                 Screenshot();
7445 }
7446
7447 void Game::TickOnce(){
7448         if(mainmenu)
7449                 rotation+=multiplier*5;
7450         else
7451                 if(directing||indialogue==-1) {
7452                         rotation+=deltah*.7;
7453                         if(!invertmouse)
7454                 rotation2+=deltav*.7;
7455                         if(invertmouse)
7456                 rotation2-=deltav*.7;
7457                         if(rotation2>90)
7458                 rotation2=90;
7459                         if(rotation2<-70)
7460                 rotation2=-70;
7461                 }
7462 }
7463
7464 void Game::TickOnceAfter(){
7465         static XYZ colviewer;
7466         static XYZ coltarget;
7467         static XYZ target;
7468         static XYZ col;
7469         static XYZ facing;
7470         static float changedelay;
7471         static bool alldead;
7472         static float unseendelay;
7473         static float cameraspeed;
7474
7475         if(!mainmenu){
7476
7477                 if(environment==snowyenvironment)
7478             leveltheme=stream_snowtheme;
7479                 if(environment==grassyenvironment)
7480             leveltheme=stream_grasstheme;
7481                 if(environment==desertenvironment)
7482             leveltheme=stream_deserttheme;
7483
7484                 realthreat=0;
7485
7486                 musictype=leveltheme;
7487                 for(int i=0;i<numplayers;i++){
7488                         if((player[i].aitype==attacktypecutoff||
7489                         player[i].aitype==getweapontype||
7490                         player[i].aitype==gethelptype||
7491                         player[i].aitype==searchtype)&&
7492                     !player[i].dead/*&&player[i].surprised<=0*/&&
7493                     (player[i].targetanimation!=sneakattackedanim&&
7494                      player[i].targetanimation!=knifesneakattackedanim&&
7495                      player[i].targetanimation!=swordsneakattackedanim)){
7496                                 musictype=stream_fighttheme;
7497                                 realthreat=1;
7498                         }
7499                 }
7500                 if(player[0].dead)
7501             musictype=stream_menutheme;
7502
7503
7504                 if(musictype==stream_fighttheme)
7505                         unseendelay=1;
7506
7507                 if(oldmusictype==stream_fighttheme&&musictype!=stream_fighttheme){
7508                         unseendelay-=multiplier;
7509                         if(unseendelay>0)
7510                                 musictype=stream_fighttheme;
7511                 }
7512
7513
7514                 if(loading==2){
7515                         musictype=stream_menutheme;
7516                         musicvolume[2]=512;
7517                         musicvolume[0]=0;
7518                         musicvolume[1]=0;
7519                         musicvolume[3]=0;
7520                 }
7521
7522                 if(musictoggle)
7523                         if(musictype!=oldmusictype&&musictype==stream_fighttheme)
7524                                 emit_sound_np(alarmsound);
7525                 musicselected=musictype;
7526
7527                 if(musicselected==leveltheme)
7528             musicvolume[0]+=multiplier*450;
7529                 else
7530             musicvolume[0]-=multiplier*450;
7531                 if(musicselected==stream_fighttheme)
7532             musicvolume[1]+=multiplier*450;
7533                 else
7534             musicvolume[1]-=multiplier*450;
7535                 if(musicselected==stream_menutheme)
7536             musicvolume[2]+=multiplier*450;
7537                 else
7538             musicvolume[2]-=multiplier*450;
7539
7540                 for(int i=0;i<3;i++){
7541                         if(musicvolume[i]<0)
7542                 musicvolume[i]=0;
7543                         if(musicvolume[i]>512)
7544                 musicvolume[i]=512;
7545                 }
7546
7547                 if(musicvolume[2]>128&&!loading&&!mainmenu)
7548             musicvolume[2]=128;
7549
7550                 if(musictoggle){
7551                         if(musicvolume[0]>0&&oldmusicvolume[0]<=0)
7552                           emit_stream_np(leveltheme, musicvolume[0]);
7553                         if(musicvolume[1]>0&&oldmusicvolume[1]<=0)
7554                           emit_stream_np(stream_fighttheme, musicvolume[1]);
7555                         if(musicvolume[2]>0&&oldmusicvolume[2]<=0)
7556                           emit_stream_np(stream_menutheme, musicvolume[2]);
7557                         if(musicvolume[0]<=0&&oldmusicvolume[0]>0)
7558                                 pause_sound(leveltheme);
7559                         if(musicvolume[1]<=0&&oldmusicvolume[1]>0)
7560                                 pause_sound(stream_fighttheme);
7561                         if(musicvolume[2]<=0&&oldmusicvolume[2]>0)
7562                                 pause_sound(stream_menutheme);
7563
7564                         if(musicvolume[0]!=oldmusicvolume[0])
7565                                 OPENAL_SetVolume(channels[leveltheme], musicvolume[0]);
7566                         if(musicvolume[1]!=oldmusicvolume[1])
7567                                 OPENAL_SetVolume(channels[stream_fighttheme], musicvolume[1]);
7568                         if(musicvolume[2]!=oldmusicvolume[2])
7569                                 OPENAL_SetVolume(channels[stream_menutheme], musicvolume[2]);
7570
7571                         for(int i=0;i<3;i++)
7572                                 oldmusicvolume[i]=musicvolume[i];
7573                 } else {
7574                         pause_sound(leveltheme);
7575                         pause_sound(stream_fighttheme);
7576                         pause_sound(stream_menutheme);
7577
7578                         for(int i=0;i<4;i++){
7579                                 oldmusicvolume[i]=0;
7580                                 musicvolume[i]=0;
7581                         }
7582                 }
7583
7584                 killhotspot=2;
7585                 for(int i=0;i<numhotspots;i++){
7586                         if(hotspottype[i]>10&&hotspottype[i]<20){
7587                                 if(player[hotspottype[i]-10].dead==0)
7588                                         killhotspot=0;
7589                                 else if(killhotspot==2)
7590                                         killhotspot=1;
7591                         }
7592                 }
7593                 if(killhotspot==2)
7594             killhotspot=0;
7595
7596
7597                 winhotspot=false;
7598                 for(int i=0;i<numhotspots;i++)
7599                         if(hotspottype[i]==-1)
7600                                 if(findDistancefast(&player[0].coords,&hotspot[i])<hotspotsize[i])
7601                                         winhotspot=true;
7602
7603                 int numalarmed=0;
7604                 for(int i=1;i<numplayers;i++)
7605                         if(!player[i].dead&&player[i].aitype==attacktypecutoff&&player[i].surprised<=0)
7606                 numalarmed++;
7607                 if(numalarmed>maxalarmed)
7608             maxalarmed=numalarmed;
7609
7610                 if(changedelay<=0&&!loading&&!editorenabled&&gameon&&!tutoriallevel&&changedelay!=-999&&!won){
7611                         if(player[0].dead&&changedelay<=0){
7612                                 changedelay=1;
7613                                 targetlevel=whichlevel;
7614                         }
7615                         alldead=true;
7616                         for(int i=1;i<numplayers;i++) {
7617                                 if(!player[i].dead&&player[i].howactive<typedead1) {
7618                     alldead=false;
7619                     break;
7620                                 }
7621                         }
7622
7623
7624                         if(alldead&&!player[0].dead&&maptype==mapkilleveryone) {
7625                                 changedelay=1;
7626                                 targetlevel=whichlevel+1;
7627                                 if(targetlevel>numchallengelevels-1)targetlevel=0;
7628                         }
7629                         if(winhotspot||windialogue) {
7630                                 changedelay=0.1;
7631                                 targetlevel=whichlevel+1;
7632                                 if(targetlevel>numchallengelevels-1)targetlevel=0;
7633                         }
7634
7635
7636                         if(killhotspot){
7637                                 changedelay=1;
7638                                 targetlevel=whichlevel+1;
7639                                 if(targetlevel>numchallengelevels-1)targetlevel=0;
7640                         }
7641
7642                         if(changedelay>0&&!player[0].dead&&!won) {
7643                                 //high scores, awards, win
7644                                 if(campaign) {
7645                                         accountactive->winCampaignLevel(whichchoice, bonustotal, leveltime);
7646                                         scoreadded=1;
7647                                 } else {
7648                                         accountactive->winLevel(whichlevel,bonustotal-startbonustotal,leveltime);
7649                                 }
7650                                 won=1;
7651                         }
7652                 }
7653
7654                 if(!winfreeze){
7655
7656                         if(leveltime<1){
7657                                 loading=0;
7658                                 changedelay=.1;
7659                                 alldead=false;
7660                                 winhotspot=false;
7661                                 killhotspot=0;
7662                         }
7663
7664                         if(!editorenabled&&gameon&&!mainmenu) {
7665                                 if(changedelay!=-999)
7666                     changedelay-=multiplier/7;
7667                                 if(player[0].dead)
7668                     targetlevel=whichlevel;
7669                                 if(loading==2&&!campaign){
7670                                         flash();
7671
7672                                         fireSound(firestartsound);
7673
7674                                         if(!player[0].dead&&targetlevel!=whichlevel)
7675                                                 startbonustotal=bonustotal;
7676                                         if(player[0].dead)
7677                         Loadlevel(whichlevel);
7678                                         else
7679                         Loadlevel(targetlevel);
7680
7681                                         fireSound();
7682
7683                                         loading=3;
7684                                 }
7685                                 if(loading==2&&targetlevel==whichlevel){
7686                                         flash();
7687                                         loadtime=0;
7688
7689                                         fireSound(firestartsound);
7690
7691                                         Loadlevel(campaignlevels[accountactive->getCampaignChoicesMade()].mapname.c_str());
7692
7693                                         fireSound();
7694
7695                                         loading=3;
7696                                 }
7697                                 if(changedelay<=-999&&
7698                         whichlevel!=-2&&
7699                         !loading&&
7700                         (player[0].dead||
7701                          (alldead&&maptype==mapkilleveryone)||
7702                          (winhotspot)||
7703                          (killhotspot)))
7704                     loading=1;
7705                                 if((player[0].dead||
7706                             (alldead&&maptype==mapkilleveryone)||
7707                             (winhotspot)||
7708                             (windialogue)||
7709                             (killhotspot))&&
7710                         changedelay<=0) {
7711                     if(whichlevel!=-2&&!loading&&!player[0].dead) {
7712                         winfreeze=true;
7713                         changedelay=-999;
7714                     }
7715                     if(player[0].dead)
7716                         loading=1;
7717                                 }
7718                         }
7719
7720                         if(campaign) {
7721                 // campaignchoosenext determines what to do when the level is complete:
7722                 // 0 = load next level
7723                 // 1 = go back to level select screen
7724                 // 2 = stealthload next level
7725                                 if(mainmenu==0&&winfreeze&&(campaignlevels[actuallevel].choosenext)==1) {
7726                                         if(campaignlevels[actuallevel].nextlevel.empty())
7727                                                 endgame=1;
7728                                 } else if(mainmenu==0&&winfreeze) {
7729                                         stealthloading = (campaignlevels[actuallevel].choosenext==2);
7730
7731                                         if(!stealthloading){
7732                                                 fireSound(firestartsound);
7733
7734                                                 flash();
7735                                         }
7736
7737                                         startbonustotal=0;
7738
7739                                         LoadCampaign();
7740
7741                                         loading=2;
7742                                         loadtime=0;
7743                                         targetlevel=7;
7744                                         if(!firstload)
7745                                                 LoadStuff();
7746                                         whichchoice=0;
7747                                         actuallevel=campaignlevels[actuallevel].nextlevel.front();
7748                                         visibleloading=1;
7749                                         stillloading=1;
7750                                         Loadlevel(campaignlevels[actuallevel].mapname.c_str());
7751                                         campaign=1;
7752                                         mainmenu=0;
7753                                         gameon=1;
7754                                         pause_sound(stream_menutheme);
7755
7756                                         stealthloading=0;
7757                                 }
7758             }
7759
7760             if(loading==3)
7761                 loading=0;
7762
7763         }
7764
7765         oldmusictype=musictype;
7766         }
7767
7768         facing=0;
7769         facing.z=-1;
7770
7771         facing=DoRotation(facing,-rotation2,0,0);
7772         facing=DoRotation(facing,0,0-rotation,0);
7773         viewerfacing=facing;
7774
7775         if(!cameramode){
7776                 if((animation[player[0].targetanimation].attack!=3&&animation[player[0].currentanimation].attack!=3)||player[0].skeleton.free)target=player[0].coords+player[0].currentoffset*(1-player[0].target)*player[0].scale+player[0].targetoffset*player[0].target*player[0].scale-player[0].facing*.05;
7777                 else target=player[0].oldcoords+player[0].currentoffset*(1-player[0].target)*player[0].scale+player[0].targetoffset*player[0].target*player[0].scale-player[0].facing*.05;
7778                 target.y+=.1;
7779                 if(player[0].skeleton.free){
7780                         for(int i=0;i<player[0].skeleton.num_joints;i++){
7781                                 if(player[0].skeleton.joints[i].position.y*player[0].scale+player[0].coords.y>target.y)
7782                                         target.y=player[0].skeleton.joints[i].position.y*player[0].scale+player[0].coords.y;
7783                         }
7784                         target.y+=.1;
7785                 }
7786                 if(player[0].skeleton.free!=2&&!autocam){
7787                         cameraspeed=20;
7788                         if(findLengthfast(&player[0].velocity)>400){
7789                                 cameraspeed=20+(findLength(&player[0].velocity)-20)*.96;
7790                         }
7791                         if(player[0].skeleton.free==0&&player[0].targetanimation!=hanganim&&player[0].targetanimation!=climbanim)target.y+=1.4;
7792                         coltarget=target-cameraloc;
7793                         if(findLengthfast(&coltarget)<multiplier*multiplier*400)cameraloc=target;
7794                         else {
7795                                 Normalise(&coltarget);
7796                                 if(player[0].targetanimation!=hanganim&&player[0].targetanimation!=climbanim&&player[0].currentanimation!=climbanim&&player[0].currentoffset.x==0)cameraloc=cameraloc+coltarget*multiplier*cameraspeed;
7797                                 else cameraloc=cameraloc+coltarget*multiplier*8;
7798                         }
7799                         if(editorenabled)cameraloc=target;
7800                         cameradist+=multiplier*5;
7801                         if(cameradist>2.3)cameradist=2.3;
7802                         viewer=cameraloc-facing*cameradist;
7803                         colviewer=viewer;
7804                         coltarget=cameraloc;
7805                         objects.SphereCheckPossible(&colviewer, findDistance(&colviewer,&coltarget));
7806                         if(terrain.patchobjectnum[player[0].whichpatchx][player[0].whichpatchz])
7807                                 for(int j=0;j<terrain.patchobjectnum[player[0].whichpatchx][player[0].whichpatchz];j++){
7808                                         int i=terrain.patchobjects[player[0].whichpatchx][player[0].whichpatchz][j];
7809                                         colviewer=viewer;
7810                                         coltarget=cameraloc;
7811                                         if(objects.model[i].LineCheckPossible(&colviewer,&coltarget,&col,&objects.position[i],&objects.rotation[i])!=-1)viewer=col;
7812                                 }
7813             if(terrain.patchobjectnum[player[0].whichpatchx][player[0].whichpatchz])
7814                 for(int j=0;j<terrain.patchobjectnum[player[0].whichpatchx][player[0].whichpatchz];j++){
7815                     int i=terrain.patchobjects[player[0].whichpatchx][player[0].whichpatchz][j];
7816                     colviewer=viewer;
7817                     if(objects.model[i].SphereCheck(&colviewer,.15,&col,&objects.position[i],&objects.rotation[i])!=-1){
7818                         viewer=colviewer;
7819                     }
7820                 }
7821             cameradist=findDistance(&viewer,&target);
7822             viewer.y=max((double)viewer.y,terrain.getHeight(viewer.x,viewer.z)+.6);
7823             if(cameraloc.y<terrain.getHeight(cameraloc.x,cameraloc.z)){
7824                 cameraloc.y=terrain.getHeight(cameraloc.x,cameraloc.z);
7825             }
7826                 }
7827                 if(player[0].skeleton.free!=2&&autocam){
7828                         cameraspeed=20;
7829                         if(findLengthfast(&player[0].velocity)>400){
7830                                 cameraspeed=20+(findLength(&player[0].velocity)-20)*.96;
7831                         }
7832                         if(player[0].skeleton.free==0&&player[0].targetanimation!=hanganim&&player[0].targetanimation!=climbanim)target.y+=1.4;
7833                         cameradist+=multiplier*5;
7834                         if(cameradist>3.3)cameradist=3.3;
7835                         coltarget=target-cameraloc;
7836                         if(findLengthfast(&coltarget)<multiplier*multiplier*400)cameraloc=target;
7837                         else if(findLengthfast(&coltarget)>1)
7838                         {
7839                                 Normalise(&coltarget);
7840                                 if(player[0].targetanimation!=hanganim&&player[0].targetanimation!=climbanim&&player[0].currentanimation!=climbanim&&player[0].currentoffset.x==0)cameraloc=cameraloc+coltarget*multiplier*cameraspeed;
7841                                 else cameraloc=cameraloc+coltarget*multiplier*8;
7842                         }
7843                         if(editorenabled)cameraloc=target;
7844                         viewer=cameraloc;
7845                         colviewer=viewer;
7846                         coltarget=cameraloc;
7847                         objects.SphereCheckPossible(&colviewer, findDistance(&colviewer,&coltarget));
7848                         if(terrain.patchobjectnum[player[0].whichpatchx][player[0].whichpatchz])
7849                                 for(int j=0;j<terrain.patchobjectnum[player[0].whichpatchx][player[0].whichpatchz];j++){
7850                                         int i=terrain.patchobjects[player[0].whichpatchx][player[0].whichpatchz][j];
7851                                         colviewer=viewer;
7852                                         coltarget=cameraloc;
7853                                         if(objects.model[i].LineCheckPossible(&colviewer,&coltarget,&col,&objects.position[i],&objects.rotation[i])!=-1)viewer=col;
7854                                 }
7855             if(terrain.patchobjectnum[player[0].whichpatchx][player[0].whichpatchz])
7856                 for(int j=0;j<terrain.patchobjectnum[player[0].whichpatchx][player[0].whichpatchz];j++){
7857                     int i=terrain.patchobjects[player[0].whichpatchx][player[0].whichpatchz][j];
7858                     colviewer=viewer;
7859                     if(objects.model[i].SphereCheck(&colviewer,.15,&col,&objects.position[i],&objects.rotation[i])!=-1){
7860                         viewer=colviewer;
7861                     }
7862                 }
7863             cameradist=findDistance(&viewer,&target);
7864             viewer.y=max((double)viewer.y,terrain.getHeight(viewer.x,viewer.z)+.6);
7865             if(cameraloc.y<terrain.getHeight(cameraloc.x,cameraloc.z)){
7866                 cameraloc.y=terrain.getHeight(cameraloc.x,cameraloc.z);
7867             }
7868                 }
7869                 if(camerashake>.8)camerashake=.8;
7870                 //if(woozy>10)woozy=10;
7871                 //woozy+=multiplier;
7872                 woozy+=multiplier;
7873                 if(player[0].dead)camerashake=0;
7874                 if(player[0].dead)woozy=0;
7875                 camerashake-=multiplier*2;
7876                 blackout-=multiplier*2;
7877                 //if(player[0].isCrouch())woozy-=multiplier*8;
7878                 if(camerashake<0)camerashake=0;
7879                 if(blackout<0)blackout=0;
7880                 //if(woozy<0)woozy=0;
7881                 if(camerashake){
7882                         viewer.x+=(float)(Random()%100)*.0005*camerashake;
7883                         viewer.y+=(float)(Random()%100)*.0005*camerashake;
7884                         viewer.z+=(float)(Random()%100)*.0005*camerashake;
7885                 }
7886         }
7887 }
7888