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