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