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