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