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