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