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