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