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