]> git.jsancho.org Git - lugaru.git/blob - Source/Weapons.cpp
More PlaySoundEx eradication
[lugaru.git] / Source / Weapons.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 /**> HEADER FILES <**/
23 #include "Weapons.h"
24 #include "openal_wrapper.h"
25 #include "Animation.h"
26 #include "Sounds.h"
27 #include "Game.h"
28 #include "Awards.h"
29
30 extern float multiplier;
31 extern int channels[100];
32 extern Terrain terrain;
33 extern float gravity;
34 extern int environment;
35 extern int detail;
36 extern FRUSTUM frustum;
37 extern XYZ viewer;
38 extern float realmultiplier;
39 extern int slomo;
40 extern float slomodelay;
41 extern bool cellophane;
42 extern float texdetail;
43 extern GLubyte bloodText[512*512*3];
44 extern int bloodtoggle;
45 extern Objects objects;
46 extern bool osx;
47 extern bool autoslomo;
48 extern float camerashake;
49 extern float woozy;
50 extern float terraindetail;
51 extern float viewdistance;
52 extern float blackout;
53 extern int difficulty;
54 extern bool freeze;
55 extern int tutoriallevel;
56 extern int numthrowkill;
57
58 void    Weapons::DoStuff(){
59         static int i,whichpatchx,whichpatchz,j,k,whichhit,m;
60         static XYZ start,end,colpoint,normalrot,footvel,footpoint;
61         static XYZ terrainnormal;
62         static XYZ vel;
63         static XYZ midp;
64         static XYZ newpoint1,newpoint2;
65         static float friction=3.5;
66         static float elasticity=.4;
67         static XYZ bounceness;
68         static float frictionness;
69         static float moveamount;
70         int closestline;
71         static float closestdistance;
72         static float distance;
73         static XYZ point[3];
74         static XYZ closestpoint;
75         static XYZ closestswordpoint;
76         static XYZ extramove;
77         static float proportion;
78         static float tempmult;
79
80         //Move
81
82         for(i=0;i<numweapons;i++){
83                 if(owner[i]!=-1){
84                         oldowner[i]=owner[i];
85                 }
86                 if(damage[i]>=2&&type[i]==staff&&owner[i]!=-1){
87                         emit_sound_at(staffbreaksound, tippoint[i]);
88                         XYZ tempvel;
89                         XYZ speed;
90                         //speed=(tippoint[i]-oldtippoint[i])/multiplier/6;
91                         speed=0;
92                         for(j=0;j<40;j++){
93                                 tempvel.x=float(abs(Random()%100)-50)/20;
94                                 tempvel.y=float(abs(Random()%100)-50)/20;
95                                 tempvel.z=float(abs(Random()%100)-50)/20;
96                                 tempvel+=speed;
97                                 Sprite::MakeSprite(splintersprite, position[i]+(tippoint[i]-position[i])*((float)j-8)/32,tempvel*.5, 115/255,73/255,12/255, .1, 1);
98                         }
99                         int tempowner;
100                         tempowner=owner[i];
101                         owner[i]=-1;
102                         hitsomething[i]=0;
103                         missed[i]=1;
104                         freetime[i]=0;
105                         firstfree[i]=1;
106                         position[i]=0;
107                         physics[i]=0;
108                         if(tempowner!=-1){
109                                 player[tempowner].num_weapons--;
110                                 if(player[tempowner].num_weapons){
111                                         player[tempowner].weaponids[0]=player[tempowner].weaponids[player[tempowner].num_weapons];
112                                         if(player[tempowner].weaponstuck==player[tempowner].num_weapons)player[tempowner].weaponstuck=0;
113                                 }
114                                 player[tempowner].weaponactive=-1;
115                         }
116                 }
117                 oldposition[i]=position[i];
118                 oldtippoint[i]=tippoint[i];
119                 if(owner[i]==-1&&(velocity[i].x||velocity[i].y||velocity[i].z)&&!physics[i]){
120                         position[i]+=velocity[i]*multiplier;
121                         tippoint[i]+=velocity[i]*multiplier;
122                         whichpatchx=position[i].x/(terrain.size/subdivision*terrain.scale*terraindetail);
123                         whichpatchz=position[i].z/(terrain.size/subdivision*terrain.scale*terraindetail);
124                         if(whichpatchx>0&&whichpatchz>0&&whichpatchx<subdivision&&whichpatchz<subdivision)
125                                 if(terrain.patchobjectnum[whichpatchx][whichpatchz]){
126                                         for(j=0;j<terrain.patchobjectnum[whichpatchx][whichpatchz];j++){
127                                                 k=terrain.patchobjects[whichpatchx][whichpatchz][j];
128                                                 start=oldtippoint[i];
129                                                 end=tippoint[i];
130                                                 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
131                                                 if(whichhit!=-1){
132                                                         if(objects.type[k]==treetrunktype){
133                                                                 objects.model[k].MakeDecal(breakdecal,DoRotation(colpoint-objects.position[k],0,-objects.rotation[k],0),.1,1,Random()%360);
134                                                                 normalrot=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0);
135                                                                 velocity[i]=0;
136                                                                 if(type[i]==knife)position[i]=colpoint-normalrot*.1;
137                                                                 if(type[i]==sword)position[i]=colpoint-normalrot*.2;
138                                                                 if(type[i]==staff)position[i]=colpoint-normalrot*.2;
139                                                                 XYZ temppoint1,temppoint2,tempforward;
140                                                                 float distance;
141
142                                                                 temppoint1=0;
143                                                                 temppoint2=normalrot;
144                                                                 distance=findDistance(&temppoint1,&temppoint2);
145                                                                 rotation2[i]=asin((temppoint1.y-temppoint2.y)/distance);
146                                                                 rotation2[i]*=360/6.28;
147                                                                 temppoint1.y=0;
148                                                                 temppoint2.y=0;
149                                                                 rotation1[i]=acos((temppoint1.z-temppoint2.z)/findDistance(&temppoint1,&temppoint2));
150                                                                 rotation1[i]*=360/6.28;
151                                                                 if(temppoint1.x>temppoint2.x)rotation1[i]=360-rotation1[i];
152
153                                                                 rotation3[i]=0;
154                                                                 smallrotation[i]=90;
155                                                                 smallrotation2[i]=0;
156                                                                 bigtilt[i]=0;
157                                                                 bigtilt2[i]=0;
158                                                                 bigrotation[i]=0;
159
160                                                                 emit_sound_at(knifesheathesound, position[i], 128.);
161
162                                                                 bloody[i]=0;
163
164                                                                 Sprite::MakeSprite(cloudimpactsprite, position[i],velocity[i], 1,1,1, .8, .3);
165                                                         }
166                                                         else {
167                                                                 physics[i]=1;
168                                                                 firstfree[i]=1;
169                                                                 position[i]-=velocity[i]*multiplier;
170                                                                 tippoint[i]-=velocity[i]*multiplier;
171                                                                 tipvelocity[i]=velocity[i];
172                                                         }
173                                                 }
174                                         }
175                                 }
176                                 if(velocity[i].x||velocity[i].y||velocity[i].z)
177                                         for(j=0;j<numplayers;j++){
178                                                 footvel=0;
179                                                 footpoint=DoRotation((player[j].skeleton.joints[player[j].skeleton.jointlabels[abdomen]].position+player[j].skeleton.joints[player[j].skeleton.jointlabels[neck]].position)/2,0,player[j].rotation,0)*player[j].scale+player[j].coords;
180                                                 if(owner[i]==-1&&findDistancefastflat(&position[i],&player[j].coords)<1.5&&findDistancefast(&position[i],&player[j].coords)<4&&player[j].weaponstuck==-1&&!player[j].skeleton.free&&j!=oldowner[i]){
181                                                         if((player[j].aitype!=attacktypecutoff||abs(Random()%6)==0||(player[j].targetanimation!=backhandspringanim&&player[j].targetanimation!=rollanim&&player[j].targetanimation!=flipanim&&Random()%2==0))&&!missed[i]){
182                                                                 bool caught=0;
183                                                                 if((player[j].creature==wolftype&&Random()%3!=0&&player[j].weaponactive==-1&&(player[j].isIdle()||player[j].isRun()||player[j].targetanimation==walkanim))||(player[j].creature==rabbittype&&Random()%2==0&&player[j].aitype==attacktypecutoff&&player[j].weaponactive==-1)){
184                                                                         emit_sound_at(knifedrawsound, player[j].coords, 128.);
185
186                                                                         player[j].weaponactive=0;
187                                                                         player[j].targetanimation=removeknifeanim;
188                                                                         player[j].targetframe=1;
189                                                                         player[j].target=1;
190                                                                         owner[i]=player[j].id;
191                                                                         if(player[j].num_weapons>0){
192                                                                                 player[j].weaponids[player[j].num_weapons]=player[j].weaponids[0];
193                                                                         }
194                                                                         player[j].num_weapons++;
195                                                                         player[j].weaponids[0]=i;
196
197                                                                         player[j].aitype=attacktypecutoff;
198                                                                 }
199                                                                 else {
200                                                                         if(j!=0)numthrowkill++;
201                                                                         player[j].num_weapons++;
202                                                                         player[j].weaponstuck=player[j].num_weapons-1;
203                                                                         if(normaldotproduct(player[j].facing,velocity[i])>0)player[j].weaponstuckwhere=1;
204                                                                         else player[j].weaponstuckwhere=0;
205
206                                                                         player[j].weaponids[player[j].num_weapons-1]=i;
207
208                                                                         player[j].RagDoll(0);
209                                                                         player[j].skeleton.joints[player[j].skeleton.jointlabels[abdomen]].velocity+=velocity[i]*2;
210                                                                         player[j].skeleton.joints[player[j].skeleton.jointlabels[neck]].velocity+=velocity[i]*2;
211                                                                         player[j].skeleton.joints[player[j].skeleton.jointlabels[rightshoulder]].velocity+=velocity[i]*2;
212                                                                         player[j].skeleton.joints[player[j].skeleton.jointlabels[leftshoulder]].velocity+=velocity[i]*2;
213                                                                         //player[j].Puff(abdomen);
214                                                                         if(bloodtoggle&&tutoriallevel!=1)Sprite::MakeSprite(cloudimpactsprite, footpoint,footvel, 1,0,0, .8, .3);
215                                                                         if(tutoriallevel==1)Sprite::MakeSprite(cloudimpactsprite, footpoint,footvel, 1,1,1, .8, .3);
216                                                                         footvel=tippoint[i]-position[i];
217                                                                         Normalise(&footvel);
218                                                                         if(bloodtoggle&&tutoriallevel!=1)Sprite::MakeSprite(bloodflamesprite, footpoint,footvel*-1, 1,0,0, .6, 1);
219
220                                                                         if(tutoriallevel!=1){
221                                                                                 if(player[j].weaponstuckwhere==0)player[j].DoBloodBig(2,205);
222                                                                                 if(player[j].weaponstuckwhere==1)player[j].DoBloodBig(2,200);
223                                                                                 player[j].damage+=200/player[j].armorhigh;
224                                                                                 player[j].deathbleeding=1;
225                                                                                 player[j].bloodloss+=(200+abs((float)(Random()%40))-20)/player[j].armorhigh;
226                                                                                 owner[i]=j;
227                                                                                 bloody[i]=2;
228                                                                                 blooddrip[i]=5;
229                                                                         }
230
231                                                                         emit_sound_at(fleshstabsound, position[i], 128.);
232
233                                                                         if(animation[player[0].targetanimation].height==highheight)
234                                                                           award_bonus(0, ninja);
235                                                                         else
236                                                                           award_bonus(0, Bullseyebonus);
237                                                                 }
238                                                         }
239                                                         else missed[i]=1;
240                                                 }
241                                         }
242                                         if(position[i].y<terrain.getHeight(position[i].x,position[i].z)){
243                                                 if(terrain.getOpacity(position[i].x,position[i].z)<.2){
244                                                         velocity[i]=0;
245                                                         if(terrain.lineTerrain(oldposition[i],position[i],&colpoint)!=-1){
246                                                                 position[i]=colpoint*terrain.scale;
247                                                         }
248                                                         else position[i].y=terrain.getHeight(position[i].x,position[i].z);
249
250                                                         terrain.MakeDecal(shadowdecalpermanent,position[i],.06,.5,0);
251                                                         normalrot=terrain.getNormal(position[i].x,position[i].z)*-1;
252                                                         velocity[i]=0;
253                                                         //position[i]-=normalrot*.1;
254                                                         glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
255                                                         glPushMatrix();
256                                                                 GLfloat M[16];
257                                                                 glLoadIdentity();
258                                                                 glRotatef(bigrotation[i],0,1,0);
259                                                                 glRotatef(bigtilt2[i],1,0,0);
260                                                                 glRotatef(bigtilt[i],0,0,1);
261                                                                 glRotatef(-rotation1[i]+90,0,1,0);
262                                                                 glRotatef(-rotation2[i]+90,0,0,1);
263                                                                 glRotatef(-rotation3[i],0,1,0);
264                                                                 glRotatef(smallrotation[i],1,0,0);
265                                                                 glRotatef(smallrotation2[i],0,1,0);
266                                                                 glTranslatef(0,0,1);
267                                                                 glGetFloatv(GL_MODELVIEW_MATRIX,M);
268                                                                 tippoint[i].x=M[12];
269                                                                 tippoint[i].y=M[13];
270                                                                 tippoint[i].z=M[14];
271                                                         glPopMatrix();
272                                                         position[i]-=tippoint[i]*.15;
273                                                         XYZ temppoint1,temppoint2,tempforward;
274                                                         float distance;
275
276                                                         rotation3[i]=0;
277                                                         smallrotation[i]=90;
278                                                         smallrotation2[i]=0;
279                                                         bigtilt[i]=0;
280                                                         bigtilt2[i]=0;
281                                                         bigrotation[i]=0;
282
283                                                         emit_sound_at(knifesheathesound, position[i], 128.);
284
285                                                         XYZ terrainlight;
286                                                         terrainlight=terrain.getLighting(position[i].x,position[i].z);
287                                                         if(environment==snowyenvironment){
288                                                                 if(findDistancefast(&position[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, position[i],velocity[i], terrainlight.x,terrainlight.y,terrainlight.z, .5, .7);
289                                                         }
290                                                         else if(environment==grassyenvironment){
291                                                                 if(findDistancefast(&position[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, position[i],velocity[i], terrainlight.x*90/255,terrainlight.y*70/255,terrainlight.z*8/255, .5, .5);
292                                                         }
293                                                         else if(environment==desertenvironment){
294                                                                 if(findDistancefast(&position[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, position[i],velocity[i], terrainlight.x*190/255,terrainlight.y*170/255,terrainlight.z*108/255, .5, .7);
295                                                         }
296
297                                                         bloody[i]=0;
298                                                 }
299                                                 else {
300                                                         physics[i]=1;
301                                                         firstfree[i]=1;
302                                                         position[i]-=velocity[i]*multiplier;
303                                                         tippoint[i]-=velocity[i]*multiplier;
304                                                         tipvelocity[i]=velocity[i];
305                                                 }
306                                         }
307                                         if(velocity[i].x!=0||velocity[i].z!=0||velocity[i].y!=0){
308                                                 velocity[i].y+=gravity*multiplier;
309
310                                                 XYZ temppoint1,temppoint2,tempforward;
311                                                 float distance;
312
313                                                 temppoint1=0;
314                                                 temppoint2=velocity[i];
315                                                 distance=findDistance(&temppoint1,&temppoint2);
316                                                 rotation2[i]=asin((temppoint1.y-temppoint2.y)/distance);
317                                                 rotation2[i]*=360/6.28;
318                                                 temppoint1.y=0;
319                                                 temppoint2.y=0;
320                                                 rotation1[i]=acos((temppoint1.z-temppoint2.z)/findDistance(&temppoint1,&temppoint2));
321                                                 rotation1[i]*=360/6.28;
322                                                 rotation3[i]=0;
323                                                 smallrotation[i]=90;
324                                                 smallrotation2[i]=0;
325                                                 bigtilt[i]=0;
326                                                 bigtilt2[i]=0;
327                                                 bigrotation[i]=0;
328                                                 if(temppoint1.x>temppoint2.x)rotation1[i]=360-rotation1[i];
329                                         }
330                 }
331                 //Sword physics
332                 XYZ mid;
333                 XYZ oldmid;
334                 XYZ oldmid2;
335
336                 tempmult=multiplier;
337                 multiplier/=10;
338                 for(int l=0;l<10;l++){
339                         if(owner[i]==-1&&(velocity[i].x||velocity[i].y||velocity[i].z)&&physics[i]){
340                                 //move
341                                 position[i]+=velocity[i]*multiplier;
342                                 tippoint[i]+=tipvelocity[i]*multiplier;
343
344                                 //Length constrain
345                                 midp=(position[i]*mass[i]+tippoint[i]*tipmass[i])/(mass[i]+tipmass[i]);
346                                 vel=tippoint[i]-midp;
347                                 Normalise(&vel);
348                                 newpoint1=midp-vel*length[i]*(tipmass[i]/(mass[i]+tipmass[i]));
349                                 newpoint2=midp+vel*length[i]*(mass[i]/(mass[i]+tipmass[i]));
350                                 if(!freeze){
351                                         if(freetime[i]>.04)velocity[i]=velocity[i]+(newpoint1-position[i])/multiplier;
352                                         if(freetime[i]>.04)tipvelocity[i]=tipvelocity[i]+(newpoint2-tippoint[i])/multiplier;
353                                 }
354                                 position[i]=newpoint1;
355                                 tippoint[i]=newpoint2;
356
357
358                                 //Object collisions
359                                 whichpatchx=(position[i].x)/(terrain.size/subdivision*terrain.scale*terraindetail);
360                                 whichpatchz=(position[i].z)/(terrain.size/subdivision*terrain.scale*terraindetail);
361                                 if(whichpatchx>0&&whichpatchz>0&&whichpatchx<subdivision&&whichpatchz<subdivision)
362                                         if(terrain.patchobjectnum[whichpatchx][whichpatchz]){
363                                                 for(j=0;j<terrain.patchobjectnum[whichpatchx][whichpatchz];j++){
364                                                         k=terrain.patchobjects[whichpatchx][whichpatchz][j];
365
366                                                         if(firstfree[i]){
367                                                                 if(type[i]!=staff){
368                                                                         start=position[i]-(tippoint[i]-position[i])/5;
369                                                                         end=tippoint[i]+(tippoint[i]-position[i])/30;
370                                                                         whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
371                                                                         if(whichhit!=-1){
372                                                                                 XYZ diff;
373                                                                                 diff=(colpoint-tippoint[i]);
374                                                                                 Normalise(&diff);
375                                                                                 hitsomething[i]=1;
376
377                                                                                 position[i]+=(colpoint-tippoint[i])+diff*.05;
378                                                                                 tippoint[i]=colpoint+diff*.05;
379                                                                                 oldposition[i]=position[i];
380                                                                                 oldtippoint[i]=tippoint[i];
381                                                                         }
382                                                                 }
383                                                                 if(type[i]==staff){
384                                                                         start=tippoint[i]-(position[i]-tippoint[i])/5;
385                                                                         end=position[i]+(position[i]-tippoint[i])/30;
386                                                                         whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
387                                                                         if(whichhit!=-1){
388                                                                                 XYZ diff;
389                                                                                 diff=(colpoint-position[i]);
390                                                                                 Normalise(&diff);
391                                                                                 hitsomething[i]=1;
392
393                                                                                 tippoint[i]+=(colpoint-position[i])+diff*.05;
394                                                                                 position[i]=colpoint+diff*.05;
395                                                                                 oldtippoint[i]=tippoint[i];
396                                                                                 oldposition[i]=tippoint[i];
397                                                                         }
398                                                                 }
399                                                         }
400
401                                                         start=oldposition[i];
402                                                         end=position[i];
403                                                         whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
404                                                         if(whichhit!=-1){
405                                                                 hitsomething[i]=1;
406                                                                 position[i]=colpoint;
407                                                                 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
408                                                                 ReflectVector(&velocity[i],&terrainnormal);
409                                                                 position[i]+=terrainnormal*.002;
410
411                                                                 bounceness=terrainnormal*findLength(&velocity[i])*(abs(normaldotproduct(velocity[i],terrainnormal)));
412                                                                 if(findLengthfast(&velocity[i])<findLengthfast(&bounceness))bounceness=0;
413                                                                 frictionness=abs(normaldotproduct(velocity[i],terrainnormal));
414                                                                 velocity[i]-=bounceness;
415                                                                 if(1-friction*frictionness>0)velocity[i]*=1-friction*frictionness;
416                                                                 else velocity[i]=0;
417                                                                 velocity[i]+=bounceness*elasticity;
418
419                                                                 if(findLengthfast(&bounceness)>1){
420                                                                         int whichsound;
421                                                                         if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
422                                                                         if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
423                                                                         emit_sound_at(whichsound, position[i], 128*findLengthfast(&bounceness));
424                                                                 }
425                                                         }
426                                                         start=oldtippoint[i];
427                                                         end=tippoint[i];
428                                                         whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
429                                                         if(whichhit!=-1){
430                                                                 hitsomething[i]=1;
431                                                                 tippoint[i]=colpoint;
432                                                                 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
433                                                                 ReflectVector(&tipvelocity[i],&terrainnormal);
434                                                                 tippoint[i]+=terrainnormal*.002;
435
436                                                                 bounceness=terrainnormal*findLength(&tipvelocity[i])*(abs(normaldotproduct(tipvelocity[i],terrainnormal)));
437                                                                 if(findLengthfast(&tipvelocity[i])<findLengthfast(&bounceness))bounceness=0;
438                                                                 frictionness=abs(normaldotproduct(tipvelocity[i],terrainnormal));
439                                                                 tipvelocity[i]-=bounceness;
440                                                                 if(1-friction*frictionness>0)tipvelocity[i]*=1-friction*frictionness;
441                                                                 else tipvelocity[i]=0;
442                                                                 tipvelocity[i]+=bounceness*elasticity;
443
444                                                                 if(findLengthfast(&bounceness)>1){
445                                                                         int whichsound;
446                                                                         if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
447                                                                         if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
448                                                                         emit_sound_at(whichsound, position[i], 128*findLengthfast(&bounceness));
449                                                                 }
450                                                         }
451
452                                                         if((objects.type[k]!=boxtype&&objects.type[k]!=platformtype&&objects.type[k]!=walltype&&objects.type[k]!=weirdtype)||objects.rotation2[k]!=0)
453                                                                 for(m=0;m<2;m++){
454                                                                         mid=(position[i]*(21+(float)m*10)+tippoint[i]*(19-(float)m*10))/40;
455                                                                         oldmid2=mid;
456                                                                         oldmid=(oldposition[i]*(21+(float)m*10)+oldtippoint[i]*(19-(float)m*10))/40;
457
458                                                                         start=oldmid;
459                                                                         end=mid;
460                                                                         whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
461                                                                         if(whichhit!=-1){
462                                                                                 hitsomething[i]=1;
463                                                                                 mid=colpoint;
464                                                                                 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
465                                                                                 ReflectVector(&velocity[i],&terrainnormal);
466
467                                                                                 bounceness=terrainnormal*findLength(&velocity[i])*(abs(normaldotproduct(velocity[i],terrainnormal)));
468                                                                                 if(findLengthfast(&velocity[i])<findLengthfast(&bounceness))bounceness=0;
469                                                                                 frictionness=abs(normaldotproduct(velocity[i],terrainnormal));
470                                                                                 velocity[i]-=bounceness;
471                                                                                 if(1-friction*frictionness>0)velocity[i]*=1-friction*frictionness;
472                                                                                 else velocity[i]=0;
473                                                                                 velocity[i]+=bounceness*elasticity;
474
475                                                                                 if(findLengthfast(&bounceness)>1){
476                                                                                         int whichsound;
477                                                                                         if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
478                                                                                         if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
479                                                                                         emit_sound_at(whichsound, mid, 128*findLengthfast(&bounceness));
480                                                                                 }
481                                                                                 position[i]+=(mid-oldmid2)*(20/(1+(float)m*10));
482                                                                         }
483
484                                                                         mid=(position[i]*(19-(float)m*10)+tippoint[i]*(21+(float)m*10))/40;
485                                                                         oldmid2=mid;
486                                                                         oldmid=(oldposition[i]*(19-(float)m*10)+oldtippoint[i]*(21+(float)m*10))/40;
487
488                                                                         start=oldmid;
489                                                                         end=mid;
490                                                                         whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
491                                                                         if(whichhit!=-1){
492                                                                                 hitsomething[i]=1;
493                                                                                 mid=colpoint;
494                                                                                 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
495                                                                                 ReflectVector(&tipvelocity[i],&terrainnormal);
496
497                                                                                 bounceness=terrainnormal*findLength(&tipvelocity[i])*(abs(normaldotproduct(tipvelocity[i],terrainnormal)));
498                                                                                 if(findLengthfast(&tipvelocity[i])<findLengthfast(&bounceness))bounceness=0;
499                                                                                 frictionness=abs(normaldotproduct(tipvelocity[i],terrainnormal));
500                                                                                 tipvelocity[i]-=bounceness;
501                                                                                 if(1-friction*frictionness>0)tipvelocity[i]*=1-friction*frictionness;
502                                                                                 else tipvelocity[i]=0;
503                                                                                 tipvelocity[i]+=bounceness*elasticity;
504
505                                                                                 if(findLengthfast(&bounceness)>1){
506                                                                                         int whichsound;
507                                                                                         if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
508                                                                                         if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
509                                                                                         emit_sound_at(whichsound, mid, 128*findLengthfast(&bounceness));
510                                                                                 }
511                                                                                 tippoint[i]+=(mid-oldmid2)*(20/(1+(float)m*10));
512                                                                         }
513                                                                 }
514                                                         else
515                                                         {
516                                                                 start=position[i];
517                                                                 end=tippoint[i];
518                                                                 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
519                                                                 if(whichhit!=-1){
520                                                                         hitsomething[i]=1;
521                                                                         closestdistance=-1;
522                                                                         closestswordpoint=colpoint;//(position[i]+tippoint[i])/2;
523                                                                         point[0]=DoRotation(objects.model[k].vertex[objects.model[k].Triangles[whichhit].vertex[0]],0,objects.rotation[k],0)+objects.position[k];
524                                                                         point[1]=DoRotation(objects.model[k].vertex[objects.model[k].Triangles[whichhit].vertex[1]],0,objects.rotation[k],0)+objects.position[k];
525                                                                         point[2]=DoRotation(objects.model[k].vertex[objects.model[k].Triangles[whichhit].vertex[2]],0,objects.rotation[k],0)+objects.position[k];
526                                                                         if(DistancePointLine(&closestswordpoint, &point[0], &point[1], &distance,&colpoint ))
527                                                                                 if(distance<closestdistance||closestdistance==-1){
528                                                                                         closestpoint=colpoint;
529                                                                                         closestdistance=distance;
530                                                                                         closestline=0;
531                                                                                 }
532                                                                                 if(DistancePointLine(&closestswordpoint, &point[1], &point[2], &distance,&colpoint ))
533                                                                                         if(distance<closestdistance||closestdistance==-1){
534                                                                                                 closestpoint=colpoint;
535                                                                                                 closestdistance=distance;
536                                                                                                 closestline=1;
537                                                                                         }
538                                                                                         if(DistancePointLine(&closestswordpoint, &point[2], &point[0], &distance,&colpoint ))
539                                                                                                 if(distance<closestdistance||closestdistance==-1){
540                                                                                                         closestpoint=colpoint;
541                                                                                                         closestdistance=distance;
542                                                                                                         closestline=2;
543                                                                                                 }
544                                                                                                 if(closestdistance!=-1&&isnormal(closestdistance)){
545                                                                                                         if(DistancePointLine(&closestpoint, &position[i], &tippoint[i], &distance,&colpoint )){
546                                                                                                                 closestswordpoint=colpoint;
547                                                                                                                 velocity[i]+=(closestpoint-closestswordpoint);
548                                                                                                                 tipvelocity[i]+=(closestpoint-closestswordpoint);
549                                                                                                                 position[i]+=(closestpoint-closestswordpoint);
550                                                                                                                 tippoint[i]+=(closestpoint-closestswordpoint);
551                                                                                                         }
552                                                                                                 }
553                                                                 }
554                                                         }
555
556                                                 }
557                                         }
558                                         //Terrain collisions
559                                         whichhit=terrain.lineTerrain(oldposition[i],position[i],&colpoint);
560                                         if(whichhit!=-1||position[i].y<terrain.getHeight(position[i].x,position[i].z)){
561                                                 hitsomething[i]=1;
562                                                 if(whichhit!=-1)position[i]=colpoint*terrain.scale;
563                                                 else position[i].y=terrain.getHeight(position[i].x,position[i].z);
564
565                                                 terrainnormal=terrain.getNormal(position[i].x,position[i].z);
566                                                 ReflectVector(&velocity[i],&terrainnormal);
567                                                 position[i]+=terrainnormal*.002;
568                                                 bounceness=terrainnormal*findLength(&velocity[i])*(abs(normaldotproduct(velocity[i],terrainnormal)));
569                                                 if(findLengthfast(&velocity[i])<findLengthfast(&bounceness))bounceness=0;
570                                                 frictionness=abs(normaldotproduct(velocity[i],terrainnormal));
571                                                 velocity[i]-=bounceness;
572                                                 if(1-friction*frictionness>0)velocity[i]*=1-friction*frictionness;
573                                                 else velocity[i]=0;
574                                                 if(terrain.getOpacity(position[i].x,position[i].z)<.2)velocity[i]+=bounceness*elasticity*.3;
575                                                 else velocity[i]+=bounceness*elasticity;
576 //if (type[i]==knife) printf("velocity of knife %d now %f,%f,%f.\n", i, velocity[i].x, velocity[i].y, velocity[i].z);
577                                                 if(findLengthfast(&bounceness)>1){
578                                                         int whichsound;
579                                                         if(terrain.getOpacity(position[i].x,position[i].z)>.2){
580                                                                 if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
581                                                                 if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
582                                                         }
583                                                         else whichsound=footstepsound+abs(Random()%2);
584                                                         emit_sound_at(whichsound, position[i],
585                                                                       findLengthfast(&bounceness)
586                                                                       * (terrain.getOpacity(position[i].x,position[i].z) > .2
587                                                                          ? 128.
588                                                                          : 32.));
589
590                                                         if(terrain.getOpacity(position[i].x,position[i].z)<.2){
591                                                                 XYZ terrainlight;
592                                                                 terrainlight=terrain.getLighting(position[i].x,position[i].z);
593                                                                 if(environment==snowyenvironment){
594                                                                         if(findDistancefast(&position[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, position[i],velocity[i], terrainlight.x,terrainlight.y,terrainlight.z, .5, .7);
595                                                                 }
596                                                                 else if(environment==grassyenvironment){
597                                                                         if(findDistancefast(&position[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, position[i],velocity[i], terrainlight.x*90/255,terrainlight.y*70/255,terrainlight.z*8/255, .5, .5);
598                                                                 }
599                                                                 else if(environment==desertenvironment){
600                                                                         if(findDistancefast(&position[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, position[i],velocity[i], terrainlight.x*190/255,terrainlight.y*170/255,terrainlight.z*108/255, .5, .7);
601                                                                 }
602                                                         }
603                                                 }
604                                         }
605                                         whichhit=terrain.lineTerrain(oldtippoint[i],tippoint[i],&colpoint);
606                                         if(whichhit!=-1||tippoint[i].y<terrain.getHeight(tippoint[i].x,tippoint[i].z)){
607                                                 if(whichhit!=-1)tippoint[i]=colpoint*terrain.scale;
608                                                 else tippoint[i].y=terrain.getHeight(tippoint[i].x,tippoint[i].z);
609
610                                                 terrainnormal=terrain.getNormal(tippoint[i].x,tippoint[i].z);
611                                                 ReflectVector(&tipvelocity[i],&terrainnormal);
612                                                 tippoint[i]+=terrainnormal*.002;
613                                                 bounceness=terrainnormal*findLength(&tipvelocity[i])*(abs(normaldotproduct(tipvelocity[i],terrainnormal)));
614                                                 if(findLengthfast(&tipvelocity[i])<findLengthfast(&bounceness))bounceness=0;
615                                                 frictionness=abs(normaldotproduct(tipvelocity[i],terrainnormal));
616                                                 tipvelocity[i]-=bounceness;
617                                                 if(1-friction*frictionness>0)tipvelocity[i]*=1-friction*frictionness;
618                                                 else tipvelocity[i]=0;
619                                                 if(terrain.getOpacity(tippoint[i].x,tippoint[i].z)<.2)tipvelocity[i]+=bounceness*elasticity*.3;
620                                                 else tipvelocity[i]+=bounceness*elasticity;
621 //if (type[i]==knife) printf("tipvelocity of knife %d now %f,%f,%f.\n", i, tipvelocity[i].x, tipvelocity[i].y, tipvelocity[i].z);
622
623                                                 if(findLengthfast(&bounceness)>1){
624                                                         int whichsound;
625                                                         if(terrain.getOpacity(tippoint[i].x,tippoint[i].z)>.2){
626                                                                 if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
627                                                                 if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
628                                                         }
629                                                         else whichsound=footstepsound+abs(Random()%2);
630                                                         emit_sound_at(whichsound, tippoint[i],
631                                                                       findLengthfast(&bounceness)
632                                                                       * (terrain.getOpacity(tippoint[i].x,tippoint[i].z) > .2
633                                                                          ? 128.
634                                                                          : 32.));
635
636                                                         if(terrain.getOpacity(tippoint[i].x,tippoint[i].z)<.2){
637                                                                 XYZ terrainlight;
638                                                                 terrainlight=terrain.getLighting(tippoint[i].x,tippoint[i].z);
639                                                                 if(environment==snowyenvironment){
640                                                                         if(findDistancefast(&tippoint[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, tippoint[i],tipvelocity[i], terrainlight.x,terrainlight.y,terrainlight.z, .5, .7);
641                                                                 }
642                                                                 else if(environment==grassyenvironment){
643                                                                         if(findDistancefast(&tippoint[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, tippoint[i],tipvelocity[i], terrainlight.x*90/255,terrainlight.y*70/255,terrainlight.z*8/255, .5, .5);
644                                                                 }
645                                                                 else if(environment==desertenvironment){
646                                                                         if(findDistancefast(&tippoint[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, tippoint[i],tipvelocity[i], terrainlight.x*190/255,terrainlight.y*170/255,terrainlight.z*108/255, .5, .7);
647                                                                 }
648                                                         }
649                                                 }
650                                         }
651
652                                         //Edges
653                                         mid=position[i]+tippoint[i];
654                                         mid/=2;
655                                         mid+=(position[i]-mid)/20;
656                                         oldmid=mid;
657                                         if(mid.y<terrain.getHeight(mid.x,mid.z)){
658                                                 hitsomething[i]=1;
659                                                 mid.y=terrain.getHeight(mid.x,mid.z);
660
661                                                 terrainnormal=terrain.getNormal(mid.x,mid.z);
662                                                 ReflectVector(&velocity[i],&terrainnormal);
663                                                 //mid+=terrainnormal*.002;
664                                                 bounceness=terrainnormal*findLength(&velocity[i])*(abs(normaldotproduct(velocity[i],terrainnormal)));
665                                                 if(findLengthfast(&velocity[i])<findLengthfast(&bounceness))bounceness=0;
666                                                 frictionness=abs(normaldotproduct(velocity[i],terrainnormal));
667                                                 velocity[i]-=bounceness;
668                                                 if(1-friction*frictionness>0)velocity[i]*=1-friction*frictionness;
669                                                 else velocity[i]=0;
670                                                 if(terrain.getOpacity(mid.x,mid.z)<.2)velocity[i]+=bounceness*elasticity*.3;
671                                                 else velocity[i]+=bounceness*elasticity;
672
673                                                 if(findLengthfast(&bounceness)>1){
674                                                         int whichsound;
675                                                         if(terrain.getOpacity(mid.x,mid.z)>.2){
676                                                                 if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
677                                                                 if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
678                                                         }
679                                                         else whichsound=footstepsound+abs(Random()%2);
680                                                         emit_sound_at(whichsound, mid,
681                                                                       findLengthfast(&bounceness)
682                                                                       * (terrain.getOpacity(position[i].x,position[i].z) > .2
683                                                                          ? 128.
684                                                                          : 32.));
685                                                 }
686                                                 position[i]+=(mid-oldmid)*20;
687                                         }
688
689                                         mid=position[i]+tippoint[i];
690                                         mid/=2;
691                                         mid+=(tippoint[i]-mid)/20;
692                                         oldmid=mid;
693                                         if(mid.y<terrain.getHeight(mid.x,mid.z)){
694                                                 hitsomething[i]=1;
695                                                 mid.y=terrain.getHeight(mid.x,mid.z);
696
697                                                 terrainnormal=terrain.getNormal(mid.x,mid.z);
698                                                 ReflectVector(&tipvelocity[i],&terrainnormal);
699                                                 //mid+=terrainnormal*.002;
700                                                 bounceness=terrainnormal*findLength(&tipvelocity[i])*(abs(normaldotproduct(tipvelocity[i],terrainnormal)));
701                                                 if(findLengthfast(&tipvelocity[i])<findLengthfast(&bounceness))bounceness=0;
702                                                 frictionness=abs(normaldotproduct(tipvelocity[i],terrainnormal));
703                                                 tipvelocity[i]-=bounceness;
704                                                 if(1-friction*frictionness>0)tipvelocity[i]*=1-friction*frictionness;
705                                                 else tipvelocity[i]=0;
706                                                 if(terrain.getOpacity(mid.x,mid.z)<.2)tipvelocity[i]+=bounceness*elasticity*.3;
707                                                 else tipvelocity[i]+=bounceness*elasticity;
708
709                                                 if(findLengthfast(&bounceness)>1){
710                                                         int whichsound;
711                                                         if(terrain.getOpacity(mid.x,mid.z)>.2){
712                                                                 if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
713                                                                 if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
714                                                         }
715                                                         else whichsound=footstepsound+abs(Random()%2);
716                                                         emit_sound_at(whichsound, mid,
717                                                                       findLengthfast(&bounceness)
718                                                                       * (terrain.getOpacity(position[i].x,position[i].z) > .2
719                                                                          ? 128.
720                                                                          : 32.));
721                                                 }
722                                                 tippoint[i]+=(mid-oldmid)*20;
723                                         }
724                                         /*XYZ mid;
725                                         mid=position[i]+tippoint[i];
726                                         mid/=2;
727                                         if(position[i].y<terrain.getHeightExtrude(mid.x,mid.z,position[i].x,position[i].z)){
728                                         hitsomething[i]=1;
729                                         position[i].y=terrain.getHeightExtrude(mid.x,mid.z,position[i].x,position[i].z);
730
731                                         terrainnormal=terrain.getNormal(mid.x,mid.z);
732                                         ReflectVector(&velocity[i],&terrainnormal);
733                                         position[i]+=terrainnormal*.002;
734                                         bounceness=terrainnormal*findLength(&velocity[i])*(abs(normaldotproduct(velocity[i],terrainnormal)));
735                                         if(findLengthfast(&velocity[i])<findLengthfast(&bounceness))bounceness=0;
736                                         frictionness=abs(normaldotproduct(velocity[i],terrainnormal));
737                                         velocity[i]-=bounceness;
738                                         if(1-friction*frictionness>0)velocity[i]*=1-friction*frictionness;
739                                         else velocity[i]=0;
740                                         if(terrain.getOpacity(mid.x,mid.z)<.2)velocity[i]+=bounceness*elasticity*.3;
741                                         else velocity[i]+=bounceness*elasticity;
742
743                                         if(findLengthfast(&bounceness)>1){
744                                         float gLoc[3];
745                                         float vel[3];
746                                         int whichsound;
747                                         if(terrain.getOpacity(mid.x,mid.z)>.2){
748                                         if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
749                                         if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
750                                         }
751                                         else whichsound=footstepsound+abs(Random()%2);
752                                         gLoc[0]=position[i].x;
753                                         gLoc[1]=position[i].y;
754                                         gLoc[2]=position[i].z;
755                                         vel[0]=0;
756                                         vel[1]=0;
757                                         vel[2]=0;
758                                         PlaySoundEx( whichsound, samp[whichsound], NULL, true);
759                                         OPENAL_3D_SetAttributes(channels[whichsound], gLoc, vel);
760                                         if(terrain.getOpacity(position[i].x,position[i].z)>.2)OPENAL_SetVolume(channels[whichsound], 128*findLengthfast(&bounceness));
761                                         else OPENAL_SetVolume(channels[whichsound], 32*findLengthfast(&bounceness));
762                                         OPENAL_SetPaused(channels[whichsound], false);
763                                         }
764                                         }
765
766                                         if(tippoint[i].y<terrain.getHeightExtrude(mid.x,mid.z,tippoint[i].x,tippoint[i].z)){
767                                         hitsomething[i]=1;
768                                         tippoint[i].y=terrain.getHeightExtrude(mid.x,mid.z,tippoint[i].x,tippoint[i].z);
769
770                                         terrainnormal=terrain.getNormal(mid.x,mid.z);
771                                         ReflectVector(&tipvelocity[i],&terrainnormal);
772                                         tippoint[i]+=terrainnormal*.002;
773                                         bounceness=terrainnormal*findLength(&tipvelocity[i])*(abs(normaldotproduct(tipvelocity[i],terrainnormal)));
774                                         if(findLengthfast(&tipvelocity[i])<findLengthfast(&bounceness))bounceness=0;
775                                         frictionness=abs(normaldotproduct(tipvelocity[i],terrainnormal));
776                                         tipvelocity[i]-=bounceness;
777                                         if(1-friction*frictionness>0)tipvelocity[i]*=1-friction*frictionness;
778                                         else tipvelocity[i]=0;
779                                         if(terrain.getOpacity(mid.x,mid.z)<.2)tipvelocity[i]+=bounceness*elasticity*.3;
780                                         else tipvelocity[i]+=bounceness*elasticity;
781
782                                         if(findLengthfast(&bounceness)>1){
783                                         float gLoc[3];
784                                         float vel[3];
785                                         int whichsound;
786                                         if(terrain.getOpacity(mid.x,mid.z)>.2){
787                                         if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
788                                         if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
789                                         }
790                                         else whichsound=footstepsound+abs(Random()%2);
791                                         gLoc[0]=tippoint[i].x;
792                                         gLoc[1]=tippoint[i].y;
793                                         gLoc[2]=tippoint[i].z;
794                                         vel[0]=0;
795                                         vel[1]=0;
796                                         vel[2]=0;
797                                         PlaySoundEx( whichsound, samp[whichsound], NULL, true);
798                                         OPENAL_3D_SetAttributes(channels[whichsound], gLoc, vel);
799                                         if(terrain.getOpacity(tippoint[i].x,tippoint[i].z)>.2)OPENAL_SetVolume(channels[whichsound], 128*findLengthfast(&bounceness));
800                                         else OPENAL_SetVolume(channels[whichsound], 32*findLengthfast(&bounceness));
801                                         OPENAL_SetPaused(channels[whichsound], false);
802                                         }
803                                         }*/
804
805                                         //Fix terrain edge collision
806                                         /*start=position[i];
807                                         end=tippoint[i];
808                                         whichhit=terrain.lineTerrain(start,end,&colpoint);
809                                         if(whichhit!=-1){
810                                         XYZ tippoi,posit;
811                                         tippoi=tippoint[i];
812                                         posit=position[i];
813
814
815                                         while(whichhit!=-1){
816                                         position[i].y+=.1;
817                                         tippoint[i].y+=.1;
818                                         velocity[i].y+=.1;
819                                         tipvelocity[i].y+=.1;
820                                         start=position[i];
821                                         end=tippoint[i];
822                                         whichhit=terrain.lineTerrain(start,end,&colpoint);
823                                         if(whichhit!=-1)
824                                         closestpoint=colpoint*terrain.scale;
825                                         }
826                                         position[i].y-=.1;
827                                         tippoint[i].y-=.1;
828                                         velocity[i].y-=.1;
829                                         tipvelocity[i].y-=.1;
830                                         start=position[i];
831                                         end=tippoint[i];
832                                         whichhit=terrain.lineTerrain(start,end,&colpoint);
833                                         while(whichhit!=-1){
834                                         position[i].y+=.01;
835                                         tippoint[i].y+=.01;
836                                         velocity[i].y+=.01;
837                                         tipvelocity[i].y+=.01;
838                                         start=position[i];
839                                         end=tippoint[i];
840                                         whichhit=terrain.lineTerrain(start,end,&colpoint);
841                                         if(whichhit!=-1)
842                                         closestpoint=colpoint*terrain.scale;
843                                         }
844                                         }*/
845                                         /*if(whichhit!=-1){
846                                         whichhit=terrain.lineTerrain(end,start,&closestswordpoint);
847                                         if(whichhit!=-1){
848                                         colpoint=(closestswordpoint*terrain.scale+colpoint*terrain.scale)/2;
849                                         proportion=findDistance(&tippoint[i],&colpoint)/findDistance(&position[i],&tippoint[i]);
850                                         if(proportion<=1){
851                                         while(whichhit!=-1){
852                                         position[i].y+=.1*proportion;
853                                         tippoint[i].y+=.1*(1-proportion);
854                                         velocity[i].y+=.1*proportion;
855                                         tipvelocity[i].y+=.1*(1-proportion);
856                                         start=position[i];
857                                         end=tippoint[i];
858                                         whichhit=terrain.lineTerrain(start,end,&colpoint);
859                                         }
860                                         position[i].y-=.1*proportion;
861                                         tippoint[i].y-=.1*(1-proportion);
862                                         velocity[i].y-=.1*proportion;
863                                         tipvelocity[i].y-=.1*(1-proportion);
864                                         start=position[i];
865                                         end=tippoint[i];
866                                         whichhit=terrain.lineTerrain(start,end,&colpoint);
867                                         while(whichhit!=-1){
868                                         position[i].y+=.01*proportion;
869                                         tippoint[i].y+=.01*(1-proportion);
870                                         velocity[i].y+=.01*proportion;
871                                         tipvelocity[i].y+=.01*(1-proportion);
872                                         start=position[i];
873                                         end=tippoint[i];
874                                         whichhit=terrain.lineTerrain(start,end,&colpoint);
875                                         }
876                                         }
877                                         }
878                                         }
879                                         */
880                                         //Gravity
881                                         velocity[i].y+=gravity*multiplier;
882                                         tipvelocity[i].y+=gravity*multiplier;
883                                         //position[i].y+=gravity*multiplier*multiplier;
884                                         //tippoint[i].y+=gravity*multiplier*multiplier;
885
886                                         //Rotation
887                                         XYZ temppoint1,temppoint2,tempforward;
888                                         float distance;
889
890                                         temppoint1=position[i];
891                                         temppoint2=tippoint[i];
892                                         distance=findDistance(&temppoint1,&temppoint2);
893                                         rotation2[i]=asin((temppoint1.y-temppoint2.y)/distance);
894                                         rotation2[i]*=360/6.28;
895                                         temppoint1.y=0;
896                                         temppoint2.y=0;
897                                         rotation1[i]=acos((temppoint1.z-temppoint2.z)/findDistance(&temppoint1,&temppoint2));
898                                         rotation1[i]*=360/6.28;
899                                         rotation3[i]=0;
900                                         smallrotation[i]=90;
901                                         smallrotation2[i]=0;
902                                         bigtilt[i]=0;
903                                         bigtilt2[i]=0;
904                                         bigrotation[i]=0;
905                                         if(temppoint1.x>temppoint2.x)rotation1[i]=360-rotation1[i];
906
907                                         //Stop moving
908                                         if(findLengthfast(&velocity[i])<.3&&findLengthfast(&tipvelocity[i])<.3&&hitsomething[i]){
909                                                 freetime[i]+=multiplier;
910                                         }
911
912                                         //velocity[i]=(position[i]-oldposition[i])/multiplier;
913                                         //tipvelocity[i]==(tippoint[i-+oldtippoint[i])/multiplier;
914                                         if(freetime[i]>.4){
915                                                 velocity[i]=0;
916                                                 tipvelocity[i]=0;
917                                         }
918                                         firstfree[i]=0;
919                         }
920                 }
921                 multiplier=tempmult;
922                 if(blooddrip[i]&&bloody[i]){
923                         blooddripdelay[i]-=blooddrip[i]*multiplier/2;
924                         blooddrip[i]-=multiplier;
925                         if(blooddrip[i]<0)blooddrip[i]=0;
926                         if(blooddrip[i]>5)blooddrip[i]=5;
927                         if(blooddripdelay[i]<0&&bloodtoggle){
928                                 blooddripdelay[i]=1;
929                                 XYZ bloodvel;
930                                 XYZ bloodloc;
931                                 bloodloc=position[i]+(tippoint[i]-position[i])*.7;
932                                 bloodloc.y-=.05;
933                                 if(bloodtoggle){
934                                         bloodvel=0;
935                                         Sprite::MakeSprite(bloodsprite, bloodloc,bloodvel, 1,1,1, .03, 1);
936                                 }
937                         }
938                 }
939                 if(onfire[i]){
940                         flamedelay[i]-=multiplier;
941                         if(onfire[i]&&flamedelay[i]<=0){
942                                 flamedelay[i]=.020;
943                                 flamedelay[i]-=multiplier;
944                                 normalrot=0;
945                                 if(owner[i]!=-1){
946                                         normalrot=player[owner[i]].velocity;
947                                 }
948                                 normalrot.y+=1;
949                                 if(owner[i]!=-1){
950                                         if(player[owner[i]].onterrain){
951                                                 normalrot.y=1;
952                                         }
953                                 }
954                                 Sprite::MakeSprite(weaponflamesprite, position[i]+tippoint[i]*(((float)abs(Random()%100))/600+.05),normalrot, 1,1,1, (.6+(float)abs(Random()%100)/200-.25)*1/3, 1);
955                                 Sprite::setLastSpriteSpeed(4);
956                                 Sprite::setLastSpriteAlivetime(.3);
957                         }
958                 }
959
960                 if(!onfire[i]&&owner[i]==-1&&type[i]!=staff){
961                         flamedelay[i]-=multiplier;
962                         if(flamedelay[i]<=0){
963                                 flamedelay[i]=.020;
964                                 flamedelay[i]-=multiplier;
965                                 normalrot=0;
966                                 if(Random()%50==0&&findDistancefast(&position[i],&viewer)>80){
967                                         XYZ shinepoint;
968                                         shinepoint=position[i]+(tippoint[i]-position[i])*(((float)abs(Random()%100))/100);
969                                         Sprite::MakeSprite(weaponshinesprite, shinepoint,normalrot, 1,1,1, (.1+(float)abs(Random()%100)/200-.25)*1/3*fast_sqrt(findDistance(&shinepoint,&viewer)), 1);
970                                         Sprite::setLastSpriteSpeed(4);
971                                         Sprite::setLastSpriteAlivetime(.3);
972                                 }
973                         }
974                 }
975         }
976 }
977
978 int Weapons::Draw()
979 {
980         static int i,j;
981         static XYZ terrainlight;
982         static GLfloat M[16];
983         static bool draw;
984         glAlphaFunc(GL_GREATER, 0.9);
985         glEnable(GL_TEXTURE_2D);
986         glEnable(GL_BLEND);
987         glEnable(GL_CULL_FACE);
988         glCullFace(GL_FRONT);
989         glDepthMask(1);
990         for(i=0;i<numweapons;i++)
991         {
992                 if((frustum.SphereInFrustum(position[i].x,position[i].y,position[i].z,1)&&findDistancefast(&viewer,&position[i])<viewdistance*viewdistance))
993                 {
994                         draw=0;
995                         if(owner[i]==-1)
996                         {
997                                 draw=1;
998                                 if(velocity[i].x&&!physics[i])drawhowmany[i]=10;
999                                 else drawhowmany[i]=1;
1000                         }
1001                         if(owner[i]!=-1)
1002                         {
1003                                 if(player[owner[i]].occluded<25)
1004                                         if((frustum.SphereInFrustum(player[owner[i]].coords.x,player[owner[i]].coords.y+player[owner[i]].scale*3,player[owner[i]].coords.z,player[owner[i]].scale*8)&&findDistancefast(&viewer,&player[owner[i]].coords)<viewdistance*viewdistance)||player[owner[i]].skeleton.free==3)
1005                                                 draw=1;
1006                                 if((player[owner[i]].targetanimation==knifeslashstartanim||player[owner[i]].targetanimation==swordsneakattackanim||(player[owner[i]].currentanimation==staffhitanim&&player[owner[i]].currentframe>1)||(player[owner[i]].currentanimation==staffhitreversedanim&&player[owner[i]].currentframe>1)||(player[owner[i]].currentanimation==staffspinhitanim&&player[owner[i]].currentframe>1)||(player[owner[i]].currentanimation==staffspinhitreversedanim&&player[owner[i]].currentframe>1)||(player[owner[i]].currentanimation==staffgroundsmashanim&&player[owner[i]].currentframe>1)||(player[owner[i]].targetanimation==swordslashanim&&player[owner[i]].targetframe<7)||player[owner[i]].targetanimation==crouchstabanim||player[owner[i]].targetanimation==swordslashreversalanim||player[owner[i]].targetanimation==swordslashreversedanim||player[owner[i]].targetanimation==knifefollowanim||player[owner[i]].targetanimation==swordgroundstabanim||player[owner[i]].targetanimation==knifethrowanim)&&player[owner[i]].targetanimation==lastdrawnanim[i]&&!player[owner[i]].skeleton.free)
1007                                 {
1008                                         drawhowmany[i]=10;
1009                                 }
1010                                 else drawhowmany[i]=1;
1011                                 if(player[owner[i]].targetanimation==swordgroundstabanim)
1012                                 {
1013                                         lastdrawnrotation1[i]=rotation1[i];
1014                                         lastdrawnrotation2[i]=rotation2[i];
1015                                         lastdrawnrotation3[i]=rotation3[i];
1016                                         lastdrawnbigrotation[i]=bigrotation[i];
1017                                         lastdrawnbigtilt[i]=bigtilt[i];
1018                                         lastdrawnbigtilt2[i]=bigtilt2[i];
1019                                         lastdrawnsmallrotation[i]=smallrotation[i];
1020                                         lastdrawnsmallrotation2[i]=smallrotation2[i];
1021                                 }
1022                         }
1023                         if(draw)
1024                         {
1025                                 terrainlight=terrain.getLighting(position[i].x,position[i].z);
1026                                 if(drawhowmany[i]>0)
1027                                 {
1028                                         glAlphaFunc(GL_GREATER, 0.01);
1029                                 }
1030                                 for(j=drawhowmany[i];j>0;j--)
1031                                 {
1032                                         glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
1033                                         glPushMatrix();
1034                                                 glColor4f(terrainlight.x,terrainlight.y,terrainlight.z,j/drawhowmany[i]);
1035                                                 if(owner[i]!=-1)glTranslatef(position[i].x*(((float)(j))/drawhowmany[i])+lastdrawnposition[i].x*(1-((float)(j))/drawhowmany[i]),position[i].y*(((float)(j))/drawhowmany[i])-.02+lastdrawnposition[i].y*(1-((float)(j))/drawhowmany[i]),position[i].z*(((float)(j))/drawhowmany[i])+lastdrawnposition[i].z*(1-((float)(j))/drawhowmany[i]));
1036                                                 if(owner[i]==-1)glTranslatef(position[i].x*(((float)(j))/drawhowmany[i])+lastdrawnposition[i].x*(1-((float)(j))/drawhowmany[i]),position[i].y*(((float)(j))/drawhowmany[i])+lastdrawnposition[i].y*(1-((float)(j))/drawhowmany[i]),position[i].z*(((float)(j))/drawhowmany[i])+lastdrawnposition[i].z*(1-((float)(j))/drawhowmany[i]));
1037                                                 //glTranslatef(position[i].x,position[i].y-.02,position[i].z);
1038                                                 glRotatef(bigrotation[i]*(((float)(j))/drawhowmany[i])+lastdrawnbigrotation[i]*(1-((float)(j))/drawhowmany[i]),0,1,0);
1039                                                 glRotatef(bigtilt2[i]*(((float)(j))/drawhowmany[i])+lastdrawnbigtilt2[i]*(1-((float)(j))/drawhowmany[i]),1,0,0);
1040                                                 glRotatef(bigtilt[i]*(((float)(j))/drawhowmany[i])+lastdrawnbigtilt[i]*(1-((float)(j))/drawhowmany[i]),0,0,1);
1041                                                 glRotatef(-rotation1[i]*(((float)(j))/drawhowmany[i])-lastdrawnrotation1[i]*(1-((float)(j))/drawhowmany[i])+90,0,1,0);
1042                                                 glRotatef(-rotation2[i]*(((float)(j))/drawhowmany[i])-lastdrawnrotation2[i]*(1-((float)(j))/drawhowmany[i])+90,0,0,1);
1043                                                 glRotatef(-rotation3[i]*(((float)(j))/drawhowmany[i])-lastdrawnrotation3[i]*(1-((float)(j))/drawhowmany[i]),0,1,0);
1044                                                 glRotatef(smallrotation[i]*(((float)(j))/drawhowmany[i])+lastdrawnsmallrotation[i]*(1-((float)(j))/drawhowmany[i]),1,0,0);
1045                                                 glRotatef(smallrotation2[i]*(((float)(j))/drawhowmany[i])+lastdrawnsmallrotation2[i]*(1-((float)(j))/drawhowmany[i]),0,1,0);
1046
1047                                                 if(owner[i]!=-1)
1048                                                 {
1049                                                         if(player[owner[i]].targetanimation==staffhitanim||player[owner[i]].currentanimation==staffhitanim||player[owner[i]].targetanimation==staffhitreversedanim||player[owner[i]].currentanimation==staffhitreversedanim)
1050                                                         {
1051                                                                 glTranslatef(0,0,-.3);
1052                                                         }
1053                                                         if(player[owner[i]].targetanimation==staffgroundsmashanim||player[owner[i]].currentanimation==staffgroundsmashanim||player[owner[i]].targetanimation==staffspinhitreversedanim||player[owner[i]].currentanimation==staffspinhitreversedanim||player[owner[i]].targetanimation==staffspinhitanim||player[owner[i]].currentanimation==staffspinhitanim)
1054                                                         {
1055                                                                 glTranslatef(0,0,-.1);
1056                                                         }
1057                                                 }
1058                                                 /*if(type[i]==knife){
1059                                                 if(owner[i]==-1){
1060                                                 if(!physics[i]&&findDistance(&position[i],&oldposition[i])*5>1)glScalef(1,1,findDistance(&position[i],&oldposition[i])*5);
1061                                                 }
1062                                                 }*/
1063
1064                                                 if(type[i]==knife)
1065                                                 {
1066                                                         glEnable(GL_LIGHTING);
1067                                                         if(!bloody[i]||!bloodtoggle)throwingknifemodel.drawdifftex(knifetextureptr);
1068                                                         if(bloodtoggle)
1069                                                         {
1070                                                                 if(bloody[i]==1)throwingknifemodel.drawdifftex(lightbloodknifetextureptr);
1071                                                                 if(bloody[i]==2)throwingknifemodel.drawdifftex(bloodknifetextureptr);
1072                                                         }
1073                                                 }
1074                                                 if(type[i]==sword)
1075                                                 {
1076                                                         glEnable(GL_LIGHTING);
1077                                                         if(!bloody[i]||!bloodtoggle)swordmodel.drawdifftex(swordtextureptr);
1078                                                         if(bloodtoggle)
1079                                                         {
1080                                                                 if(bloody[i]==1)swordmodel.drawdifftex(lightbloodswordtextureptr);
1081                                                                 if(bloody[i]==2)swordmodel.drawdifftex(bloodswordtextureptr);
1082                                                         }
1083                                                 }
1084                                                 if(type[i]==staff)
1085                                                 {
1086                                                         glEnable(GL_LIGHTING);
1087                                                         staffmodel.drawdifftex(stafftextureptr);
1088                                                 }
1089
1090                                         glPopMatrix();
1091                                 }
1092
1093                                 lastdrawnposition[i]=position[i];
1094                                 lastdrawntippoint[i]=tippoint[i];
1095                                 lastdrawnrotation1[i]=rotation1[i];
1096                                 lastdrawnrotation2[i]=rotation2[i];
1097                                 lastdrawnrotation3[i]=rotation3[i];
1098                                 lastdrawnbigrotation[i]=bigrotation[i];
1099                                 lastdrawnbigtilt[i]=bigtilt[i];
1100                                 lastdrawnbigtilt2[i]=bigtilt2[i];
1101                                 lastdrawnsmallrotation[i]=smallrotation[i];
1102                                 lastdrawnsmallrotation2[i]=smallrotation2[i];
1103                                 if(owner[i]!=-1)lastdrawnanim[i]=player[owner[i]].currentanimation;
1104                         }
1105                         if(owner[i]!=-1)
1106                         {
1107                                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
1108                                 glPushMatrix();
1109                                         glLoadIdentity();
1110                                         glTranslatef(position[i].x,position[i].y-.02,position[i].z);
1111                                         glRotatef(bigrotation[i],0,1,0);
1112                                         glRotatef(bigtilt2[i],1,0,0);
1113                                         glRotatef(bigtilt[i],0,0,1);
1114                                         glRotatef(-rotation1[i]+90,0,1,0);
1115                                         glRotatef(-rotation2[i]+90,0,0,1);
1116                                         glRotatef(-rotation3[i],0,1,0);
1117                                         glRotatef(smallrotation[i],1,0,0);
1118                                         glRotatef(smallrotation2[i],0,1,0);
1119                                         glTranslatef(0,0,length[i]);
1120                                         glGetFloatv(GL_MODELVIEW_MATRIX,M);
1121                                         tippoint[i].x=M[12];
1122                                         tippoint[i].y=M[13];
1123                                         tippoint[i].z=M[14];
1124                                 glPopMatrix();
1125                         }
1126                         /*XYZ shinepoint;
1127                         XYZ nothingpoint;
1128                         nothingpoint=0;
1129                         shinepoint=position[i];
1130                         Sprite::MakeSprite(weaponshinesprite, shinepoint,nothingpoint, 1,1,1,multiplier*2, 1);
1131                         Sprite::speed[Sprite::numsprites-1]=4;
1132                         Sprite::alivetime[Sprite::numsprites-1]=.3;
1133                         shinepoint=tippoint[i];
1134                         Sprite::MakeSprite(weaponshinesprite, shinepoint,nothingpoint, 1,1,1,multiplier*2, 1);
1135                         Sprite::speed[Sprite::numsprites-1]=4;
1136                         Sprite::alivetime[Sprite::numsprites-1]=.3;*/
1137                 }
1138         }
1139         return 0;
1140 }
1141
1142 Weapons::Weapons()
1143 {
1144         numweapons = 0;
1145
1146         //              Model throwingknifemodel;
1147         knifetextureptr = 0;
1148         lightbloodknifetextureptr = 0;
1149         bloodknifetextureptr = 0;
1150
1151         //              Model swordmodel;
1152         swordtextureptr = 0;
1153         lightbloodswordtextureptr = 0;
1154         bloodswordtextureptr = 0;
1155
1156         //              Model staffmodel;
1157         stafftextureptr = 0;
1158 }
1159
1160 Weapons::~Weapons()
1161 {
1162         if (stafftextureptr) glDeleteTextures( 1, &stafftextureptr );
1163         if (knifetextureptr) glDeleteTextures( 1, &knifetextureptr );
1164         if (lightbloodknifetextureptr) glDeleteTextures( 1, &lightbloodknifetextureptr );
1165         if (bloodknifetextureptr) glDeleteTextures( 1, &bloodknifetextureptr );
1166         if (swordtextureptr) glDeleteTextures( 1, &swordtextureptr );
1167         if (lightbloodswordtextureptr) glDeleteTextures( 1, &lightbloodswordtextureptr );
1168         if (bloodswordtextureptr) glDeleteTextures( 1, &bloodswordtextureptr );
1169 }
1170