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