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