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