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