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