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