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