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