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