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