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 Terrain terrain;
33 extern int environment;
35 extern FRUSTUM frustum;
37 extern float realmultiplier;
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;
46 extern bool autoslomo;
47 extern float camerashake;
49 extern float viewdistance;
50 extern float blackout;
51 extern int difficulty;
53 extern int tutoriallevel;
54 extern int numthrowkill;
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;
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;
69 static float closestdistance;
70 static float distance;
72 static XYZ closestpoint;
73 static XYZ closestswordpoint;
75 static float proportion;
76 static float tempmult;
80 for(i=0;i<numweapons;i++){
84 if(damage[i]>=2&&type[i]==staff&&owner[i]!=-1){
85 emit_sound_at(staffbreaksound, tippoint[i]);
88 //speed=(tippoint[i]-oldtippoint[i])/multiplier/6;
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;
95 Sprite::MakeSprite(splintersprite, position[i]+(tippoint[i]-position[i])*((float)j-8)/32,tempvel*.5, 115/255,73/255,12/255, .1, 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;
112 player[tempowner].weaponactive=-1;
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];
128 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
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);
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;
141 temppoint2=normalrot;
142 distance=findDistance(&temppoint1,&temppoint2);
143 rotation2[i]=asin((temppoint1.y-temppoint2.y)/distance);
144 rotation2[i]*=360/6.28;
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];
158 emit_sound_at(knifesheathesound, position[i], 128.);
162 Sprite::MakeSprite(cloudimpactsprite, position[i],velocity[i], 1,1,1, .8, .3);
167 position[i]-=velocity[i]*multiplier;
168 tippoint[i]-=velocity[i]*multiplier;
169 tipvelocity[i]=velocity[i];
174 if(velocity[i].x||velocity[i].y||velocity[i].z)
175 for(j=0;j<numplayers;j++){
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]){
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.);
184 player[j].weaponactive=0;
185 player[j].targetanimation=removeknifeanim;
186 player[j].targetframe=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];
192 player[j].num_weapons++;
193 player[j].weaponids[0]=i;
195 player[j].aitype=attacktypecutoff;
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;
204 player[j].weaponids[player[j].num_weapons-1]=i;
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];
216 if(bloodtoggle&&tutoriallevel!=1)Sprite::MakeSprite(bloodflamesprite, footpoint,footvel*-1, 1,0,0, .6, 1);
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;
229 emit_sound_at(fleshstabsound, position[i], 128.);
231 if(animation[player[0].targetanimation].height==highheight)
232 award_bonus(0, ninja);
234 award_bonus(0, Bullseyebonus);
240 if(position[i].y<terrain.getHeight(position[i].x,position[i].z)){
241 if(terrain.getOpacity(position[i].x,position[i].z)<.2){
243 if(terrain.lineTerrain(oldposition[i],position[i],&colpoint)!=-1){
244 position[i]=colpoint*terrain.scale;
246 else position[i].y=terrain.getHeight(position[i].x,position[i].z);
248 terrain.MakeDecal(shadowdecalpermanent,position[i],.06,.5,0);
249 normalrot=terrain.getNormal(position[i].x,position[i].z)*-1;
251 //position[i]-=normalrot*.1;
252 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
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);
265 glGetFloatv(GL_MODELVIEW_MATRIX,M);
270 position[i]-=tippoint[i]*.15;
271 XYZ temppoint1,temppoint2,tempforward;
281 emit_sound_at(knifesheathesound, position[i], 128.);
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);
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);
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);
300 position[i]-=velocity[i]*multiplier;
301 tippoint[i]-=velocity[i]*multiplier;
302 tipvelocity[i]=velocity[i];
305 if(velocity[i].x!=0||velocity[i].z!=0||velocity[i].y!=0){
306 velocity[i].y+=gravity*multiplier;
308 XYZ temppoint1,temppoint2,tempforward;
312 temppoint2=velocity[i];
313 distance=findDistance(&temppoint1,&temppoint2);
314 rotation2[i]=asin((temppoint1.y-temppoint2.y)/distance);
315 rotation2[i]*=360/6.28;
318 rotation1[i]=acos((temppoint1.z-temppoint2.z)/findDistance(&temppoint1,&temppoint2));
319 rotation1[i]*=360/6.28;
326 if(temppoint1.x>temppoint2.x)rotation1[i]=360-rotation1[i];
336 for(int l=0;l<10;l++){
337 if(owner[i]==-1&&(velocity[i].x||velocity[i].y||velocity[i].z)&&physics[i]){
339 position[i]+=velocity[i]*multiplier;
340 tippoint[i]+=tipvelocity[i]*multiplier;
343 midp=(position[i]*mass[i]+tippoint[i]*tipmass[i])/(mass[i]+tipmass[i]);
344 vel=tippoint[i]-midp;
346 newpoint1=midp-vel*length[i]*(tipmass[i]/(mass[i]+tipmass[i]));
347 newpoint2=midp+vel*length[i]*(mass[i]/(mass[i]+tipmass[i]));
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;
352 position[i]=newpoint1;
353 tippoint[i]=newpoint2;
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];
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]);
371 diff=(colpoint-tippoint[i]);
375 position[i]+=(colpoint-tippoint[i])+diff*.05;
376 tippoint[i]=colpoint+diff*.05;
377 oldposition[i]=position[i];
378 oldtippoint[i]=tippoint[i];
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]);
387 diff=(colpoint-position[i]);
391 tippoint[i]+=(colpoint-position[i])+diff*.05;
392 position[i]=colpoint+diff*.05;
393 oldtippoint[i]=tippoint[i];
394 oldposition[i]=tippoint[i];
399 start=oldposition[i];
401 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
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;
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;
415 velocity[i]+=bounceness*elasticity;
417 if(findLengthfast(&bounceness)>1){
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));
424 start=oldtippoint[i];
426 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
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;
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;
442 if(findLengthfast(&bounceness)>1){
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));
450 if((objects.type[k]!=boxtype&&objects.type[k]!=platformtype&&objects.type[k]!=walltype&&objects.type[k]!=weirdtype)||objects.rotation2[k]!=0)
452 mid=(position[i]*(21+(float)m*10)+tippoint[i]*(19-(float)m*10))/40;
454 oldmid=(oldposition[i]*(21+(float)m*10)+oldtippoint[i]*(19-(float)m*10))/40;
458 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
462 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
463 ReflectVector(&velocity[i],&terrainnormal);
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;
471 velocity[i]+=bounceness*elasticity;
473 if(findLengthfast(&bounceness)>1){
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));
479 position[i]+=(mid-oldmid2)*(20/(1+(float)m*10));
482 mid=(position[i]*(19-(float)m*10)+tippoint[i]*(21+(float)m*10))/40;
484 oldmid=(oldposition[i]*(19-(float)m*10)+oldtippoint[i]*(21+(float)m*10))/40;
488 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
492 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
493 ReflectVector(&tipvelocity[i],&terrainnormal);
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;
503 if(findLengthfast(&bounceness)>1){
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));
509 tippoint[i]+=(mid-oldmid2)*(20/(1+(float)m*10));
516 whichhit=objects.model[k].LineCheck(&start,&end,&colpoint,&objects.position[k],&objects.rotation[k]);
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;
530 if(DistancePointLine(&closestswordpoint, &point[1], &point[2], &distance,&colpoint ))
531 if(distance<closestdistance||closestdistance==-1){
532 closestpoint=colpoint;
533 closestdistance=distance;
536 if(DistancePointLine(&closestswordpoint, &point[2], &point[0], &distance,&colpoint ))
537 if(distance<closestdistance||closestdistance==-1){
538 closestpoint=colpoint;
539 closestdistance=distance;
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);
557 whichhit=terrain.lineTerrain(oldposition[i],position[i],&colpoint);
558 if(whichhit!=-1||position[i].y<terrain.getHeight(position[i].x,position[i].z)){
560 if(whichhit!=-1)position[i]=colpoint*terrain.scale;
561 else position[i].y=terrain.getHeight(position[i].x,position[i].z);
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;
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){
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);
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
588 if(terrain.getOpacity(position[i].x,position[i].z)<.2){
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);
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);
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);
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);
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);
621 if(findLengthfast(&bounceness)>1){
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);
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
634 if(terrain.getOpacity(tippoint[i].x,tippoint[i].z)<.2){
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);
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);
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);
651 mid=position[i]+tippoint[i];
653 mid+=(position[i]-mid)/20;
655 if(mid.y<terrain.getHeight(mid.x,mid.z)){
657 mid.y=terrain.getHeight(mid.x,mid.z);
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;
668 if(terrain.getOpacity(mid.x,mid.z)<.2)velocity[i]+=bounceness*elasticity*.3;
669 else velocity[i]+=bounceness*elasticity;
671 if(findLengthfast(&bounceness)>1){
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);
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
684 position[i]+=(mid-oldmid)*20;
687 mid=position[i]+tippoint[i];
689 mid+=(tippoint[i]-mid)/20;
691 if(mid.y<terrain.getHeight(mid.x,mid.z)){
693 mid.y=terrain.getHeight(mid.x,mid.z);
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;
707 if(findLengthfast(&bounceness)>1){
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);
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
720 tippoint[i]+=(mid-oldmid)*20;
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;
729 XYZ temppoint1,temppoint2,tempforward;
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;
739 rotation1[i]=acos((temppoint1.z-temppoint2.z)/findDistance(&temppoint1,&temppoint2));
740 rotation1[i]*=360/6.28;
747 if(temppoint1.x>temppoint2.x)rotation1[i]=360-rotation1[i];
750 if(findLengthfast(&velocity[i])<.3&&findLengthfast(&tipvelocity[i])<.3&&hitsomething[i]){
751 freetime[i]+=multiplier;
754 //velocity[i]=(position[i]-oldposition[i])/multiplier;
755 //tipvelocity[i]==(tippoint[i-+oldtippoint[i])/multiplier;
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){
773 bloodloc=position[i]+(tippoint[i]-position[i])*.7;
777 Sprite::MakeSprite(bloodsprite, bloodloc,bloodvel, 1,1,1, .03, 1);
782 flamedelay[i]-=multiplier;
783 if(onfire[i]&&flamedelay[i]<=0){
785 flamedelay[i]-=multiplier;
788 normalrot=player[owner[i]].velocity;
792 if(player[owner[i]].onterrain){
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);
802 if(!onfire[i]&&owner[i]==-1&&type[i]!=staff){
803 flamedelay[i]-=multiplier;
804 if(flamedelay[i]<=0){
806 flamedelay[i]-=multiplier;
808 if(Random()%50==0&&findDistancefast(&position[i],&viewer)>80){
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);
823 static XYZ terrainlight;
824 static GLfloat M[16];
826 glAlphaFunc(GL_GREATER, 0.9);
827 glEnable(GL_TEXTURE_2D);
829 glEnable(GL_CULL_FACE);
830 glCullFace(GL_FRONT);
832 for(i=0;i<numweapons;i++)
834 if((frustum.SphereInFrustum(position[i].x,position[i].y,position[i].z,1)&&findDistancefast(&viewer,&position[i])<viewdistance*viewdistance))
840 if(velocity[i].x&&!physics[i])drawhowmany[i]=10;
841 else drawhowmany[i]=1;
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)
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)
852 else drawhowmany[i]=1;
853 if(player[owner[i]].targetanimation==swordgroundstabanim)
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];
867 terrainlight=terrain.getLighting(position[i].x,position[i].z);
870 glAlphaFunc(GL_GREATER, 0.01);
872 for(j=drawhowmany[i];j>0;j--)
874 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
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);
891 if(player[owner[i]].targetanimation==staffhitanim||player[owner[i]].currentanimation==staffhitanim||player[owner[i]].targetanimation==staffhitreversedanim||player[owner[i]].currentanimation==staffhitreversedanim)
893 glTranslatef(0,0,-.3);
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)
897 glTranslatef(0,0,-.1);
900 /*if(type[i]==knife){
902 if(!physics[i]&&findDistance(&position[i],&oldposition[i])*5>1)glScalef(1,1,findDistance(&position[i],&oldposition[i])*5);
908 glEnable(GL_LIGHTING);
909 if(!bloody[i]||!bloodtoggle)throwingknifemodel.drawdifftex(knifetextureptr);
912 if(bloody[i]==1)throwingknifemodel.drawdifftex(lightbloodknifetextureptr);
913 if(bloody[i]==2)throwingknifemodel.drawdifftex(bloodknifetextureptr);
918 glEnable(GL_LIGHTING);
919 if(!bloody[i]||!bloodtoggle)swordmodel.drawdifftex(swordtextureptr);
922 if(bloody[i]==1)swordmodel.drawdifftex(lightbloodswordtextureptr);
923 if(bloody[i]==2)swordmodel.drawdifftex(bloodswordtextureptr);
928 glEnable(GL_LIGHTING);
929 staffmodel.drawdifftex(stafftextureptr);
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;
949 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
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);
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;*/
988 // Model throwingknifemodel;
990 lightbloodknifetextureptr = 0;
991 bloodknifetextureptr = 0;
995 lightbloodswordtextureptr = 0;
996 bloodswordtextureptr = 0;
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 );