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