2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
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.
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.
15 See the GNU General Public License for more details.
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.
22 /**> HEADER FILES <**/
24 #include "openal_wrapper.h"
25 #include "Animation.h"
30 extern float multiplier;
31 extern int channels[100];
32 extern Terrain terrain;
34 extern int environment;
36 extern FRUSTUM frustum;
38 extern float realmultiplier;
40 extern float slomodelay;
41 extern bool cellophane;
42 extern float texdetail;
43 extern GLubyte bloodText[512*512*3];
44 extern int bloodtoggle;
45 extern Objects objects;
47 extern bool autoslomo;
48 extern float camerashake;
50 extern float terraindetail;
51 extern float viewdistance;
52 extern float blackout;
53 extern int difficulty;
55 extern int tutoriallevel;
56 extern int numthrowkill;
58 void Weapons::DoStuff(){
59 static int i,whichpatchx,whichpatchz,j,k,whichhit,m;
60 static XYZ start,end,colpoint,normalrot,footvel,footpoint;
61 static XYZ terrainnormal;
64 static XYZ newpoint1,newpoint2;
65 static float friction=3.5;
66 static float elasticity=.4;
67 static XYZ bounceness;
68 static float frictionness;
69 static float moveamount;
71 static float closestdistance;
72 static float distance;
74 static XYZ closestpoint;
75 static XYZ closestswordpoint;
77 static float proportion;
78 static float tempmult;
82 for(i=0;i<numweapons;i++){
86 if(damage[i]>=2&&type[i]==staff&&owner[i]!=-1){
87 emit_sound_at(staffbreaksound, tippoint[i]);
90 //speed=(tippoint[i]-oldtippoint[i])/multiplier/6;
93 tempvel.x=float(abs(Random()%100)-50)/20;
94 tempvel.y=float(abs(Random()%100)-50)/20;
95 tempvel.z=float(abs(Random()%100)-50)/20;
97 Sprite::MakeSprite(splintersprite, position[i]+(tippoint[i]-position[i])*((float)j-8)/32,tempvel*.5, 115/255,73/255,12/255, .1, 1);
109 player[tempowner].num_weapons--;
110 if(player[tempowner].num_weapons){
111 player[tempowner].weaponids[0]=player[tempowner].weaponids[player[tempowner].num_weapons];
112 if(player[tempowner].weaponstuck==player[tempowner].num_weapons)player[tempowner].weaponstuck=0;
114 player[tempowner].weaponactive=-1;
117 oldposition[i]=position[i];
118 oldtippoint[i]=tippoint[i];
119 if(owner[i]==-1&&(velocity[i].x||velocity[i].y||velocity[i].z)&&!physics[i]){
120 position[i]+=velocity[i]*multiplier;
121 tippoint[i]+=velocity[i]*multiplier;
122 whichpatchx=position[i].x/(terrain.size/subdivision*terrain.scale*terraindetail);
123 whichpatchz=position[i].z/(terrain.size/subdivision*terrain.scale*terraindetail);
124 if(whichpatchx>0&&whichpatchz>0&&whichpatchx<subdivision&&whichpatchz<subdivision)
125 if(terrain.patchobjectnum[whichpatchx][whichpatchz]){
126 for(j=0;j<terrain.patchobjectnum[whichpatchx][whichpatchz];j++){
127 k=terrain.patchobjects[whichpatchx][whichpatchz][j];
128 start=oldtippoint[i];
130 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
132 if(objects.type[k]==treetrunktype){
133 objects.model[k].MakeDecal(breakdecal,DoRotation(colpoint-objects.position[k],0,-objects.rotation[k],0),.1,1,Random()%360);
134 normalrot=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0);
136 if(type[i]==knife)position[i]=colpoint-normalrot*.1;
137 if(type[i]==sword)position[i]=colpoint-normalrot*.2;
138 if(type[i]==staff)position[i]=colpoint-normalrot*.2;
139 XYZ temppoint1,temppoint2,tempforward;
143 temppoint2=normalrot;
144 distance=findDistance(&temppoint1,&temppoint2);
145 rotation2[i]=asin((temppoint1.y-temppoint2.y)/distance);
146 rotation2[i]*=360/6.28;
149 rotation1[i]=acos((temppoint1.z-temppoint2.z)/findDistance(&temppoint1,&temppoint2));
150 rotation1[i]*=360/6.28;
151 if(temppoint1.x>temppoint2.x)rotation1[i]=360-rotation1[i];
160 emit_sound_at(knifesheathesound, position[i], 128.);
164 Sprite::MakeSprite(cloudimpactsprite, position[i],velocity[i], 1,1,1, .8, .3);
169 position[i]-=velocity[i]*multiplier;
170 tippoint[i]-=velocity[i]*multiplier;
171 tipvelocity[i]=velocity[i];
176 if(velocity[i].x||velocity[i].y||velocity[i].z)
177 for(j=0;j<numplayers;j++){
179 footpoint=DoRotation((player[j].skeleton.joints[player[j].skeleton.jointlabels[abdomen]].position+player[j].skeleton.joints[player[j].skeleton.jointlabels[neck]].position)/2,0,player[j].rotation,0)*player[j].scale+player[j].coords;
180 if(owner[i]==-1&&findDistancefastflat(&position[i],&player[j].coords)<1.5&&findDistancefast(&position[i],&player[j].coords)<4&&player[j].weaponstuck==-1&&!player[j].skeleton.free&&j!=oldowner[i]){
181 if((player[j].aitype!=attacktypecutoff||abs(Random()%6)==0||(player[j].targetanimation!=backhandspringanim&&player[j].targetanimation!=rollanim&&player[j].targetanimation!=flipanim&&Random()%2==0))&&!missed[i]){
183 if((player[j].creature==wolftype&&Random()%3!=0&&player[j].weaponactive==-1&&(player[j].isIdle()||player[j].isRun()||player[j].targetanimation==walkanim))||(player[j].creature==rabbittype&&Random()%2==0&&player[j].aitype==attacktypecutoff&&player[j].weaponactive==-1)){
184 emit_sound_at(knifedrawsound, player[j].coords, 128.);
186 player[j].weaponactive=0;
187 player[j].targetanimation=removeknifeanim;
188 player[j].targetframe=1;
190 owner[i]=player[j].id;
191 if(player[j].num_weapons>0){
192 player[j].weaponids[player[j].num_weapons]=player[j].weaponids[0];
194 player[j].num_weapons++;
195 player[j].weaponids[0]=i;
197 player[j].aitype=attacktypecutoff;
200 if(j!=0)numthrowkill++;
201 player[j].num_weapons++;
202 player[j].weaponstuck=player[j].num_weapons-1;
203 if(normaldotproduct(player[j].facing,velocity[i])>0)player[j].weaponstuckwhere=1;
204 else player[j].weaponstuckwhere=0;
206 player[j].weaponids[player[j].num_weapons-1]=i;
208 player[j].RagDoll(0);
209 player[j].skeleton.joints[player[j].skeleton.jointlabels[abdomen]].velocity+=velocity[i]*2;
210 player[j].skeleton.joints[player[j].skeleton.jointlabels[neck]].velocity+=velocity[i]*2;
211 player[j].skeleton.joints[player[j].skeleton.jointlabels[rightshoulder]].velocity+=velocity[i]*2;
212 player[j].skeleton.joints[player[j].skeleton.jointlabels[leftshoulder]].velocity+=velocity[i]*2;
213 //player[j].Puff(abdomen);
214 if(bloodtoggle&&tutoriallevel!=1)Sprite::MakeSprite(cloudimpactsprite, footpoint,footvel, 1,0,0, .8, .3);
215 if(tutoriallevel==1)Sprite::MakeSprite(cloudimpactsprite, footpoint,footvel, 1,1,1, .8, .3);
216 footvel=tippoint[i]-position[i];
218 if(bloodtoggle&&tutoriallevel!=1)Sprite::MakeSprite(bloodflamesprite, footpoint,footvel*-1, 1,0,0, .6, 1);
220 if(tutoriallevel!=1){
221 if(player[j].weaponstuckwhere==0)player[j].DoBloodBig(2,205);
222 if(player[j].weaponstuckwhere==1)player[j].DoBloodBig(2,200);
223 player[j].damage+=200/player[j].armorhigh;
224 player[j].deathbleeding=1;
225 player[j].bloodloss+=(200+abs((float)(Random()%40))-20)/player[j].armorhigh;
231 emit_sound_at(fleshstabsound, position[i], 128.);
233 if(animation[player[0].targetanimation].height==highheight)
234 award_bonus(0, ninja);
236 award_bonus(0, Bullseyebonus);
242 if(position[i].y<terrain.getHeight(position[i].x,position[i].z)){
243 if(terrain.getOpacity(position[i].x,position[i].z)<.2){
245 if(terrain.lineTerrain(oldposition[i],position[i],&colpoint)!=-1){
246 position[i]=colpoint*terrain.scale;
248 else position[i].y=terrain.getHeight(position[i].x,position[i].z);
250 terrain.MakeDecal(shadowdecalpermanent,position[i],.06,.5,0);
251 normalrot=terrain.getNormal(position[i].x,position[i].z)*-1;
253 //position[i]-=normalrot*.1;
254 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
258 glRotatef(bigrotation[i],0,1,0);
259 glRotatef(bigtilt2[i],1,0,0);
260 glRotatef(bigtilt[i],0,0,1);
261 glRotatef(-rotation1[i]+90,0,1,0);
262 glRotatef(-rotation2[i]+90,0,0,1);
263 glRotatef(-rotation3[i],0,1,0);
264 glRotatef(smallrotation[i],1,0,0);
265 glRotatef(smallrotation2[i],0,1,0);
267 glGetFloatv(GL_MODELVIEW_MATRIX,M);
272 position[i]-=tippoint[i]*.15;
273 XYZ temppoint1,temppoint2,tempforward;
283 emit_sound_at(knifesheathesound, position[i], 128.);
286 terrainlight=terrain.getLighting(position[i].x,position[i].z);
287 if(environment==snowyenvironment){
288 if(findDistancefast(&position[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, position[i],velocity[i], terrainlight.x,terrainlight.y,terrainlight.z, .5, .7);
290 else if(environment==grassyenvironment){
291 if(findDistancefast(&position[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, position[i],velocity[i], terrainlight.x*90/255,terrainlight.y*70/255,terrainlight.z*8/255, .5, .5);
293 else if(environment==desertenvironment){
294 if(findDistancefast(&position[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, position[i],velocity[i], terrainlight.x*190/255,terrainlight.y*170/255,terrainlight.z*108/255, .5, .7);
302 position[i]-=velocity[i]*multiplier;
303 tippoint[i]-=velocity[i]*multiplier;
304 tipvelocity[i]=velocity[i];
307 if(velocity[i].x!=0||velocity[i].z!=0||velocity[i].y!=0){
308 velocity[i].y+=gravity*multiplier;
310 XYZ temppoint1,temppoint2,tempforward;
314 temppoint2=velocity[i];
315 distance=findDistance(&temppoint1,&temppoint2);
316 rotation2[i]=asin((temppoint1.y-temppoint2.y)/distance);
317 rotation2[i]*=360/6.28;
320 rotation1[i]=acos((temppoint1.z-temppoint2.z)/findDistance(&temppoint1,&temppoint2));
321 rotation1[i]*=360/6.28;
328 if(temppoint1.x>temppoint2.x)rotation1[i]=360-rotation1[i];
338 for(int l=0;l<10;l++){
339 if(owner[i]==-1&&(velocity[i].x||velocity[i].y||velocity[i].z)&&physics[i]){
341 position[i]+=velocity[i]*multiplier;
342 tippoint[i]+=tipvelocity[i]*multiplier;
345 midp=(position[i]*mass[i]+tippoint[i]*tipmass[i])/(mass[i]+tipmass[i]);
346 vel=tippoint[i]-midp;
348 newpoint1=midp-vel*length[i]*(tipmass[i]/(mass[i]+tipmass[i]));
349 newpoint2=midp+vel*length[i]*(mass[i]/(mass[i]+tipmass[i]));
351 if(freetime[i]>.04)velocity[i]=velocity[i]+(newpoint1-position[i])/multiplier;
352 if(freetime[i]>.04)tipvelocity[i]=tipvelocity[i]+(newpoint2-tippoint[i])/multiplier;
354 position[i]=newpoint1;
355 tippoint[i]=newpoint2;
359 whichpatchx=(position[i].x)/(terrain.size/subdivision*terrain.scale*terraindetail);
360 whichpatchz=(position[i].z)/(terrain.size/subdivision*terrain.scale*terraindetail);
361 if(whichpatchx>0&&whichpatchz>0&&whichpatchx<subdivision&&whichpatchz<subdivision)
362 if(terrain.patchobjectnum[whichpatchx][whichpatchz]){
363 for(j=0;j<terrain.patchobjectnum[whichpatchx][whichpatchz];j++){
364 k=terrain.patchobjects[whichpatchx][whichpatchz][j];
368 start=position[i]-(tippoint[i]-position[i])/5;
369 end=tippoint[i]+(tippoint[i]-position[i])/30;
370 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
373 diff=(colpoint-tippoint[i]);
377 position[i]+=(colpoint-tippoint[i])+diff*.05;
378 tippoint[i]=colpoint+diff*.05;
379 oldposition[i]=position[i];
380 oldtippoint[i]=tippoint[i];
384 start=tippoint[i]-(position[i]-tippoint[i])/5;
385 end=position[i]+(position[i]-tippoint[i])/30;
386 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
389 diff=(colpoint-position[i]);
393 tippoint[i]+=(colpoint-position[i])+diff*.05;
394 position[i]=colpoint+diff*.05;
395 oldtippoint[i]=tippoint[i];
396 oldposition[i]=tippoint[i];
401 start=oldposition[i];
403 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
406 position[i]=colpoint;
407 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
408 ReflectVector(&velocity[i],&terrainnormal);
409 position[i]+=terrainnormal*.002;
411 bounceness=terrainnormal*findLength(&velocity[i])*(abs(normaldotproduct(velocity[i],terrainnormal)));
412 if(findLengthfast(&velocity[i])<findLengthfast(&bounceness))bounceness=0;
413 frictionness=abs(normaldotproduct(velocity[i],terrainnormal));
414 velocity[i]-=bounceness;
415 if(1-friction*frictionness>0)velocity[i]*=1-friction*frictionness;
417 velocity[i]+=bounceness*elasticity;
419 if(findLengthfast(&bounceness)>1){
421 if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
422 if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
423 emit_sound_at(whichsound, position[i], 128*findLengthfast(&bounceness));
426 start=oldtippoint[i];
428 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
431 tippoint[i]=colpoint;
432 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
433 ReflectVector(&tipvelocity[i],&terrainnormal);
434 tippoint[i]+=terrainnormal*.002;
436 bounceness=terrainnormal*findLength(&tipvelocity[i])*(abs(normaldotproduct(tipvelocity[i],terrainnormal)));
437 if(findLengthfast(&tipvelocity[i])<findLengthfast(&bounceness))bounceness=0;
438 frictionness=abs(normaldotproduct(tipvelocity[i],terrainnormal));
439 tipvelocity[i]-=bounceness;
440 if(1-friction*frictionness>0)tipvelocity[i]*=1-friction*frictionness;
441 else tipvelocity[i]=0;
442 tipvelocity[i]+=bounceness*elasticity;
444 if(findLengthfast(&bounceness)>1){
446 if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
447 if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
448 emit_sound_at(whichsound, position[i], 128*findLengthfast(&bounceness));
452 if((objects.type[k]!=boxtype&&objects.type[k]!=platformtype&&objects.type[k]!=walltype&&objects.type[k]!=weirdtype)||objects.rotation2[k]!=0)
454 mid=(position[i]*(21+(float)m*10)+tippoint[i]*(19-(float)m*10))/40;
456 oldmid=(oldposition[i]*(21+(float)m*10)+oldtippoint[i]*(19-(float)m*10))/40;
460 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
464 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
465 ReflectVector(&velocity[i],&terrainnormal);
467 bounceness=terrainnormal*findLength(&velocity[i])*(abs(normaldotproduct(velocity[i],terrainnormal)));
468 if(findLengthfast(&velocity[i])<findLengthfast(&bounceness))bounceness=0;
469 frictionness=abs(normaldotproduct(velocity[i],terrainnormal));
470 velocity[i]-=bounceness;
471 if(1-friction*frictionness>0)velocity[i]*=1-friction*frictionness;
473 velocity[i]+=bounceness*elasticity;
475 if(findLengthfast(&bounceness)>1){
477 if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
478 if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
479 emit_sound_at(whichsound, mid, 128*findLengthfast(&bounceness));
481 position[i]+=(mid-oldmid2)*(20/(1+(float)m*10));
484 mid=(position[i]*(19-(float)m*10)+tippoint[i]*(21+(float)m*10))/40;
486 oldmid=(oldposition[i]*(19-(float)m*10)+oldtippoint[i]*(21+(float)m*10))/40;
490 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
494 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
495 ReflectVector(&tipvelocity[i],&terrainnormal);
497 bounceness=terrainnormal*findLength(&tipvelocity[i])*(abs(normaldotproduct(tipvelocity[i],terrainnormal)));
498 if(findLengthfast(&tipvelocity[i])<findLengthfast(&bounceness))bounceness=0;
499 frictionness=abs(normaldotproduct(tipvelocity[i],terrainnormal));
500 tipvelocity[i]-=bounceness;
501 if(1-friction*frictionness>0)tipvelocity[i]*=1-friction*frictionness;
502 else tipvelocity[i]=0;
503 tipvelocity[i]+=bounceness*elasticity;
505 if(findLengthfast(&bounceness)>1){
507 if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
508 if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
509 emit_sound_at(whichsound, mid, 128*findLengthfast(&bounceness));
511 tippoint[i]+=(mid-oldmid2)*(20/(1+(float)m*10));
518 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
522 closestswordpoint=colpoint;//(position[i]+tippoint[i])/2;
523 point[0]=DoRotation(objects.model[k].vertex[objects.model[k].Triangles[whichhit].vertex[0]],0,objects.rotation[k],0)+objects.position[k];
524 point[1]=DoRotation(objects.model[k].vertex[objects.model[k].Triangles[whichhit].vertex[1]],0,objects.rotation[k],0)+objects.position[k];
525 point[2]=DoRotation(objects.model[k].vertex[objects.model[k].Triangles[whichhit].vertex[2]],0,objects.rotation[k],0)+objects.position[k];
526 if(DistancePointLine(&closestswordpoint, &point[0], &point[1], &distance,&colpoint ))
527 if(distance<closestdistance||closestdistance==-1){
528 closestpoint=colpoint;
529 closestdistance=distance;
532 if(DistancePointLine(&closestswordpoint, &point[1], &point[2], &distance,&colpoint ))
533 if(distance<closestdistance||closestdistance==-1){
534 closestpoint=colpoint;
535 closestdistance=distance;
538 if(DistancePointLine(&closestswordpoint, &point[2], &point[0], &distance,&colpoint ))
539 if(distance<closestdistance||closestdistance==-1){
540 closestpoint=colpoint;
541 closestdistance=distance;
544 if(closestdistance!=-1&&isnormal(closestdistance)){
545 if(DistancePointLine(&closestpoint, &position[i], &tippoint[i], &distance,&colpoint )){
546 closestswordpoint=colpoint;
547 velocity[i]+=(closestpoint-closestswordpoint);
548 tipvelocity[i]+=(closestpoint-closestswordpoint);
549 position[i]+=(closestpoint-closestswordpoint);
550 tippoint[i]+=(closestpoint-closestswordpoint);
559 whichhit=terrain.lineTerrain(oldposition[i],position[i],&colpoint);
560 if(whichhit!=-1||position[i].y<terrain.getHeight(position[i].x,position[i].z)){
562 if(whichhit!=-1)position[i]=colpoint*terrain.scale;
563 else position[i].y=terrain.getHeight(position[i].x,position[i].z);
565 terrainnormal=terrain.getNormal(position[i].x,position[i].z);
566 ReflectVector(&velocity[i],&terrainnormal);
567 position[i]+=terrainnormal*.002;
568 bounceness=terrainnormal*findLength(&velocity[i])*(abs(normaldotproduct(velocity[i],terrainnormal)));
569 if(findLengthfast(&velocity[i])<findLengthfast(&bounceness))bounceness=0;
570 frictionness=abs(normaldotproduct(velocity[i],terrainnormal));
571 velocity[i]-=bounceness;
572 if(1-friction*frictionness>0)velocity[i]*=1-friction*frictionness;
574 if(terrain.getOpacity(position[i].x,position[i].z)<.2)velocity[i]+=bounceness*elasticity*.3;
575 else velocity[i]+=bounceness*elasticity;
576 //if (type[i]==knife) printf("velocity of knife %d now %f,%f,%f.\n", i, velocity[i].x, velocity[i].y, velocity[i].z);
577 if(findLengthfast(&bounceness)>1){
579 if(terrain.getOpacity(position[i].x,position[i].z)>.2){
580 if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
581 if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
583 else whichsound=footstepsound+abs(Random()%2);
584 emit_sound_at(whichsound, position[i],
585 findLengthfast(&bounceness)
586 * (terrain.getOpacity(position[i].x,position[i].z) > .2
590 if(terrain.getOpacity(position[i].x,position[i].z)<.2){
592 terrainlight=terrain.getLighting(position[i].x,position[i].z);
593 if(environment==snowyenvironment){
594 if(findDistancefast(&position[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, position[i],velocity[i], terrainlight.x,terrainlight.y,terrainlight.z, .5, .7);
596 else if(environment==grassyenvironment){
597 if(findDistancefast(&position[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, position[i],velocity[i], terrainlight.x*90/255,terrainlight.y*70/255,terrainlight.z*8/255, .5, .5);
599 else if(environment==desertenvironment){
600 if(findDistancefast(&position[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, position[i],velocity[i], terrainlight.x*190/255,terrainlight.y*170/255,terrainlight.z*108/255, .5, .7);
605 whichhit=terrain.lineTerrain(oldtippoint[i],tippoint[i],&colpoint);
606 if(whichhit!=-1||tippoint[i].y<terrain.getHeight(tippoint[i].x,tippoint[i].z)){
607 if(whichhit!=-1)tippoint[i]=colpoint*terrain.scale;
608 else tippoint[i].y=terrain.getHeight(tippoint[i].x,tippoint[i].z);
610 terrainnormal=terrain.getNormal(tippoint[i].x,tippoint[i].z);
611 ReflectVector(&tipvelocity[i],&terrainnormal);
612 tippoint[i]+=terrainnormal*.002;
613 bounceness=terrainnormal*findLength(&tipvelocity[i])*(abs(normaldotproduct(tipvelocity[i],terrainnormal)));
614 if(findLengthfast(&tipvelocity[i])<findLengthfast(&bounceness))bounceness=0;
615 frictionness=abs(normaldotproduct(tipvelocity[i],terrainnormal));
616 tipvelocity[i]-=bounceness;
617 if(1-friction*frictionness>0)tipvelocity[i]*=1-friction*frictionness;
618 else tipvelocity[i]=0;
619 if(terrain.getOpacity(tippoint[i].x,tippoint[i].z)<.2)tipvelocity[i]+=bounceness*elasticity*.3;
620 else tipvelocity[i]+=bounceness*elasticity;
621 //if (type[i]==knife) printf("tipvelocity of knife %d now %f,%f,%f.\n", i, tipvelocity[i].x, tipvelocity[i].y, tipvelocity[i].z);
623 if(findLengthfast(&bounceness)>1){
625 if(terrain.getOpacity(tippoint[i].x,tippoint[i].z)>.2){
626 if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
627 if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
629 else whichsound=footstepsound+abs(Random()%2);
630 emit_sound_at(whichsound, tippoint[i],
631 findLengthfast(&bounceness)
632 * (terrain.getOpacity(tippoint[i].x,tippoint[i].z) > .2
636 if(terrain.getOpacity(tippoint[i].x,tippoint[i].z)<.2){
638 terrainlight=terrain.getLighting(tippoint[i].x,tippoint[i].z);
639 if(environment==snowyenvironment){
640 if(findDistancefast(&tippoint[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, tippoint[i],tipvelocity[i], terrainlight.x,terrainlight.y,terrainlight.z, .5, .7);
642 else if(environment==grassyenvironment){
643 if(findDistancefast(&tippoint[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, tippoint[i],tipvelocity[i], terrainlight.x*90/255,terrainlight.y*70/255,terrainlight.z*8/255, .5, .5);
645 else if(environment==desertenvironment){
646 if(findDistancefast(&tippoint[i],&viewer)<viewdistance*viewdistance/4)Sprite::MakeSprite(cloudsprite, tippoint[i],tipvelocity[i], terrainlight.x*190/255,terrainlight.y*170/255,terrainlight.z*108/255, .5, .7);
653 mid=position[i]+tippoint[i];
655 mid+=(position[i]-mid)/20;
657 if(mid.y<terrain.getHeight(mid.x,mid.z)){
659 mid.y=terrain.getHeight(mid.x,mid.z);
661 terrainnormal=terrain.getNormal(mid.x,mid.z);
662 ReflectVector(&velocity[i],&terrainnormal);
663 //mid+=terrainnormal*.002;
664 bounceness=terrainnormal*findLength(&velocity[i])*(abs(normaldotproduct(velocity[i],terrainnormal)));
665 if(findLengthfast(&velocity[i])<findLengthfast(&bounceness))bounceness=0;
666 frictionness=abs(normaldotproduct(velocity[i],terrainnormal));
667 velocity[i]-=bounceness;
668 if(1-friction*frictionness>0)velocity[i]*=1-friction*frictionness;
670 if(terrain.getOpacity(mid.x,mid.z)<.2)velocity[i]+=bounceness*elasticity*.3;
671 else velocity[i]+=bounceness*elasticity;
673 if(findLengthfast(&bounceness)>1){
675 if(terrain.getOpacity(mid.x,mid.z)>.2){
676 if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
677 if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
679 else whichsound=footstepsound+abs(Random()%2);
680 emit_sound_at(whichsound, mid,
681 findLengthfast(&bounceness)
682 * (terrain.getOpacity(position[i].x,position[i].z) > .2
686 position[i]+=(mid-oldmid)*20;
689 mid=position[i]+tippoint[i];
691 mid+=(tippoint[i]-mid)/20;
693 if(mid.y<terrain.getHeight(mid.x,mid.z)){
695 mid.y=terrain.getHeight(mid.x,mid.z);
697 terrainnormal=terrain.getNormal(mid.x,mid.z);
698 ReflectVector(&tipvelocity[i],&terrainnormal);
699 //mid+=terrainnormal*.002;
700 bounceness=terrainnormal*findLength(&tipvelocity[i])*(abs(normaldotproduct(tipvelocity[i],terrainnormal)));
701 if(findLengthfast(&tipvelocity[i])<findLengthfast(&bounceness))bounceness=0;
702 frictionness=abs(normaldotproduct(tipvelocity[i],terrainnormal));
703 tipvelocity[i]-=bounceness;
704 if(1-friction*frictionness>0)tipvelocity[i]*=1-friction*frictionness;
705 else tipvelocity[i]=0;
706 if(terrain.getOpacity(mid.x,mid.z)<.2)tipvelocity[i]+=bounceness*elasticity*.3;
707 else tipvelocity[i]+=bounceness*elasticity;
709 if(findLengthfast(&bounceness)>1){
711 if(terrain.getOpacity(mid.x,mid.z)>.2){
712 if(type[i]==staff)whichsound=footstepsound3+abs(Random()%2);
713 if(type[i]!=staff)whichsound=clank1sound+abs(Random()%4);
715 else whichsound=footstepsound+abs(Random()%2);
716 emit_sound_at(whichsound, mid,
717 findLengthfast(&bounceness)
718 * (terrain.getOpacity(position[i].x,position[i].z) > .2
722 tippoint[i]+=(mid-oldmid)*20;
725 velocity[i].y+=gravity*multiplier;
726 tipvelocity[i].y+=gravity*multiplier;
727 //position[i].y+=gravity*multiplier*multiplier;
728 //tippoint[i].y+=gravity*multiplier*multiplier;
731 XYZ temppoint1,temppoint2,tempforward;
734 temppoint1=position[i];
735 temppoint2=tippoint[i];
736 distance=findDistance(&temppoint1,&temppoint2);
737 rotation2[i]=asin((temppoint1.y-temppoint2.y)/distance);
738 rotation2[i]*=360/6.28;
741 rotation1[i]=acos((temppoint1.z-temppoint2.z)/findDistance(&temppoint1,&temppoint2));
742 rotation1[i]*=360/6.28;
749 if(temppoint1.x>temppoint2.x)rotation1[i]=360-rotation1[i];
752 if(findLengthfast(&velocity[i])<.3&&findLengthfast(&tipvelocity[i])<.3&&hitsomething[i]){
753 freetime[i]+=multiplier;
756 //velocity[i]=(position[i]-oldposition[i])/multiplier;
757 //tipvelocity[i]==(tippoint[i-+oldtippoint[i])/multiplier;
766 if(blooddrip[i]&&bloody[i]){
767 blooddripdelay[i]-=blooddrip[i]*multiplier/2;
768 blooddrip[i]-=multiplier;
769 if(blooddrip[i]<0)blooddrip[i]=0;
770 if(blooddrip[i]>5)blooddrip[i]=5;
771 if(blooddripdelay[i]<0&&bloodtoggle){
775 bloodloc=position[i]+(tippoint[i]-position[i])*.7;
779 Sprite::MakeSprite(bloodsprite, bloodloc,bloodvel, 1,1,1, .03, 1);
784 flamedelay[i]-=multiplier;
785 if(onfire[i]&&flamedelay[i]<=0){
787 flamedelay[i]-=multiplier;
790 normalrot=player[owner[i]].velocity;
794 if(player[owner[i]].onterrain){
798 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);
799 Sprite::setLastSpriteSpeed(4);
800 Sprite::setLastSpriteAlivetime(.3);
804 if(!onfire[i]&&owner[i]==-1&&type[i]!=staff){
805 flamedelay[i]-=multiplier;
806 if(flamedelay[i]<=0){
808 flamedelay[i]-=multiplier;
810 if(Random()%50==0&&findDistancefast(&position[i],&viewer)>80){
812 shinepoint=position[i]+(tippoint[i]-position[i])*(((float)abs(Random()%100))/100);
813 Sprite::MakeSprite(weaponshinesprite, shinepoint,normalrot, 1,1,1, (.1+(float)abs(Random()%100)/200-.25)*1/3*fast_sqrt(findDistance(&shinepoint,&viewer)), 1);
814 Sprite::setLastSpriteSpeed(4);
815 Sprite::setLastSpriteAlivetime(.3);
825 static XYZ terrainlight;
826 static GLfloat M[16];
828 glAlphaFunc(GL_GREATER, 0.9);
829 glEnable(GL_TEXTURE_2D);
831 glEnable(GL_CULL_FACE);
832 glCullFace(GL_FRONT);
834 for(i=0;i<numweapons;i++)
836 if((frustum.SphereInFrustum(position[i].x,position[i].y,position[i].z,1)&&findDistancefast(&viewer,&position[i])<viewdistance*viewdistance))
842 if(velocity[i].x&&!physics[i])drawhowmany[i]=10;
843 else drawhowmany[i]=1;
847 if(player[owner[i]].occluded<25)
848 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)
850 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)
854 else drawhowmany[i]=1;
855 if(player[owner[i]].targetanimation==swordgroundstabanim)
857 lastdrawnrotation1[i]=rotation1[i];
858 lastdrawnrotation2[i]=rotation2[i];
859 lastdrawnrotation3[i]=rotation3[i];
860 lastdrawnbigrotation[i]=bigrotation[i];
861 lastdrawnbigtilt[i]=bigtilt[i];
862 lastdrawnbigtilt2[i]=bigtilt2[i];
863 lastdrawnsmallrotation[i]=smallrotation[i];
864 lastdrawnsmallrotation2[i]=smallrotation2[i];
869 terrainlight=terrain.getLighting(position[i].x,position[i].z);
872 glAlphaFunc(GL_GREATER, 0.01);
874 for(j=drawhowmany[i];j>0;j--)
876 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
878 glColor4f(terrainlight.x,terrainlight.y,terrainlight.z,j/drawhowmany[i]);
879 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]));
880 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]));
881 //glTranslatef(position[i].x,position[i].y-.02,position[i].z);
882 glRotatef(bigrotation[i]*(((float)(j))/drawhowmany[i])+lastdrawnbigrotation[i]*(1-((float)(j))/drawhowmany[i]),0,1,0);
883 glRotatef(bigtilt2[i]*(((float)(j))/drawhowmany[i])+lastdrawnbigtilt2[i]*(1-((float)(j))/drawhowmany[i]),1,0,0);
884 glRotatef(bigtilt[i]*(((float)(j))/drawhowmany[i])+lastdrawnbigtilt[i]*(1-((float)(j))/drawhowmany[i]),0,0,1);
885 glRotatef(-rotation1[i]*(((float)(j))/drawhowmany[i])-lastdrawnrotation1[i]*(1-((float)(j))/drawhowmany[i])+90,0,1,0);
886 glRotatef(-rotation2[i]*(((float)(j))/drawhowmany[i])-lastdrawnrotation2[i]*(1-((float)(j))/drawhowmany[i])+90,0,0,1);
887 glRotatef(-rotation3[i]*(((float)(j))/drawhowmany[i])-lastdrawnrotation3[i]*(1-((float)(j))/drawhowmany[i]),0,1,0);
888 glRotatef(smallrotation[i]*(((float)(j))/drawhowmany[i])+lastdrawnsmallrotation[i]*(1-((float)(j))/drawhowmany[i]),1,0,0);
889 glRotatef(smallrotation2[i]*(((float)(j))/drawhowmany[i])+lastdrawnsmallrotation2[i]*(1-((float)(j))/drawhowmany[i]),0,1,0);
893 if(player[owner[i]].targetanimation==staffhitanim||player[owner[i]].currentanimation==staffhitanim||player[owner[i]].targetanimation==staffhitreversedanim||player[owner[i]].currentanimation==staffhitreversedanim)
895 glTranslatef(0,0,-.3);
897 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)
899 glTranslatef(0,0,-.1);
902 /*if(type[i]==knife){
904 if(!physics[i]&&findDistance(&position[i],&oldposition[i])*5>1)glScalef(1,1,findDistance(&position[i],&oldposition[i])*5);
910 glEnable(GL_LIGHTING);
911 if(!bloody[i]||!bloodtoggle)throwingknifemodel.drawdifftex(knifetextureptr);
914 if(bloody[i]==1)throwingknifemodel.drawdifftex(lightbloodknifetextureptr);
915 if(bloody[i]==2)throwingknifemodel.drawdifftex(bloodknifetextureptr);
920 glEnable(GL_LIGHTING);
921 if(!bloody[i]||!bloodtoggle)swordmodel.drawdifftex(swordtextureptr);
924 if(bloody[i]==1)swordmodel.drawdifftex(lightbloodswordtextureptr);
925 if(bloody[i]==2)swordmodel.drawdifftex(bloodswordtextureptr);
930 glEnable(GL_LIGHTING);
931 staffmodel.drawdifftex(stafftextureptr);
937 lastdrawnposition[i]=position[i];
938 lastdrawntippoint[i]=tippoint[i];
939 lastdrawnrotation1[i]=rotation1[i];
940 lastdrawnrotation2[i]=rotation2[i];
941 lastdrawnrotation3[i]=rotation3[i];
942 lastdrawnbigrotation[i]=bigrotation[i];
943 lastdrawnbigtilt[i]=bigtilt[i];
944 lastdrawnbigtilt2[i]=bigtilt2[i];
945 lastdrawnsmallrotation[i]=smallrotation[i];
946 lastdrawnsmallrotation2[i]=smallrotation2[i];
947 if(owner[i]!=-1)lastdrawnanim[i]=player[owner[i]].currentanimation;
951 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
954 glTranslatef(position[i].x,position[i].y-.02,position[i].z);
955 glRotatef(bigrotation[i],0,1,0);
956 glRotatef(bigtilt2[i],1,0,0);
957 glRotatef(bigtilt[i],0,0,1);
958 glRotatef(-rotation1[i]+90,0,1,0);
959 glRotatef(-rotation2[i]+90,0,0,1);
960 glRotatef(-rotation3[i],0,1,0);
961 glRotatef(smallrotation[i],1,0,0);
962 glRotatef(smallrotation2[i],0,1,0);
963 glTranslatef(0,0,length[i]);
964 glGetFloatv(GL_MODELVIEW_MATRIX,M);
973 shinepoint=position[i];
974 Sprite::MakeSprite(weaponshinesprite, shinepoint,nothingpoint, 1,1,1,multiplier*2, 1);
975 Sprite::speed[Sprite::numsprites-1]=4;
976 Sprite::alivetime[Sprite::numsprites-1]=.3;
977 shinepoint=tippoint[i];
978 Sprite::MakeSprite(weaponshinesprite, shinepoint,nothingpoint, 1,1,1,multiplier*2, 1);
979 Sprite::speed[Sprite::numsprites-1]=4;
980 Sprite::alivetime[Sprite::numsprites-1]=.3;*/
990 // Model throwingknifemodel;
992 lightbloodknifetextureptr = 0;
993 bloodknifetextureptr = 0;
997 lightbloodswordtextureptr = 0;
998 bloodswordtextureptr = 0;
1000 // Model staffmodel;
1001 stafftextureptr = 0;
1006 if (stafftextureptr) glDeleteTextures( 1, &stafftextureptr );
1007 if (knifetextureptr) glDeleteTextures( 1, &knifetextureptr );
1008 if (lightbloodknifetextureptr) glDeleteTextures( 1, &lightbloodknifetextureptr );
1009 if (bloodknifetextureptr) glDeleteTextures( 1, &bloodknifetextureptr );
1010 if (swordtextureptr) glDeleteTextures( 1, &swordtextureptr );
1011 if (lightbloodswordtextureptr) glDeleteTextures( 1, &lightbloodswordtextureptr );
1012 if (bloodswordtextureptr) glDeleteTextures( 1, &bloodswordtextureptr );