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