- }
- }
- player[i].targetpathfindpoint=closest;
- }
- else
- {
- for(int j=0;j<numpathpoints;j++)
- if(j!=player[i].lastpathfindpoint&&
- j!=player[i].lastpathfindpoint2&&
- j!=player[i].lastpathfindpoint3&&
- j!=player[i].lastpathfindpoint4){
- connected=0;
- if(numpathpointconnect[j])
- for(int k=0;k<numpathpointconnect[j];k++)
- if(pathpointconnect[j][k]==player[i].lastpathfindpoint)
- connected=1;
- if(!connected)
- if(numpathpointconnect[player[i].lastpathfindpoint])
- for(int k=0;k<numpathpointconnect[player[i].lastpathfindpoint];k++)
- if(pathpointconnect[player[i].lastpathfindpoint][k]==j)
- connected=1;
- if(connected){
- tempdist=findPathDist(j,player[i].finalpathfindpoint);
- if(closest==-1||tempdist<closestdistance){
- closestdistance=tempdist;
- closest=j;
- }
- }
- }
- player[i].targetpathfindpoint=closest;
- }
- }
- player[i].losupdatedelay-=multiplier;
-
- player[i].targetrotation=roughDirectionTo(player[i].coords,pathpoint[player[i].targetpathfindpoint]);
- player[i].lookrotation=player[i].targetrotation;
-
- //reached target point
- if(findDistancefastflat(&player[i].coords,&pathpoint[player[i].targetpathfindpoint])<.6){
- player[i].lastpathfindpoint4=player[i].lastpathfindpoint3;
- player[i].lastpathfindpoint3=player[i].lastpathfindpoint2;
- player[i].lastpathfindpoint2=player[i].lastpathfindpoint;
- player[i].lastpathfindpoint=player[i].targetpathfindpoint;
- if(player[i].lastpathfindpoint2==-1)
- player[i].lastpathfindpoint2=player[i].lastpathfindpoint;
- if(player[i].lastpathfindpoint3==-1)
- player[i].lastpathfindpoint3=player[i].lastpathfindpoint2;
- if(player[i].lastpathfindpoint4==-1)
- player[i].lastpathfindpoint4=player[i].lastpathfindpoint3;
- player[i].targetpathfindpoint=-1;
- }
- if( findDistancefastflat(&player[i].coords,&player[i].finalfinaltarget)<
- findDistancefastflat(&player[i].coords,&player[i].finaltarget)||
- findDistancefastflat(&player[i].coords,&player[i].finaltarget)<.6*sq(player[i].scale*5)||
- player[i].lastpathfindpoint==player[i].finalpathfindpoint) {
- player[i].aitype=passivetype;
- }
-
- player[i].forwardkeydown=1;
- player[i].leftkeydown=0;
- player[i].backkeydown=0;
- player[i].rightkeydown=0;
- player[i].crouchkeydown=0;
- player[i].attackkeydown=0;
- player[i].throwkeydown=0;
-
- if(player[i].avoidcollided>.8 && !player[i].jumpkeydown && player[i].collided<.8)
- player[i].targetrotation+=90*(player[i].whichdirection*2-1);
-
- if(player[i].collided<1||player[i].targetanimation!=jumpupanim)
- player[i].jumpkeydown=0;
- if((player[i].collided>.8&&player[i].jumppower>=5))
- player[i].jumpkeydown=1;
-
- if((tutoriallevel!=1||cananger)&&
- hostile&&
- !player[0].dead&&
- findDistancefast(&player[i].coords,&player[0].coords)<400&&
- player[i].occluded<25){
- if(findDistancefast(&player[i].coords,&player[0].coords)<12&&
- animation[player[0].targetanimation].height!=lowheight&&
- !editorenabled&&
- (player[0].coords.y<player[i].coords.y+5||player[0].onterrain))
- player[i].aitype=attacktypecutoff;
- if(findDistancefast(&player[i].coords,&player[0].coords)<30&&
- animation[player[0].targetanimation].height==highheight&&
- !editorenabled)
- player[i].aitype=attacktypecutoff;
-
- if(player[i].losupdatedelay<0&&!editorenabled&&player[i].occluded<2){
- player[i].losupdatedelay=.2;
- for(int j=0;j<numplayers;j++)
- if(j==0||player[j].skeleton.free||player[j].aitype!=passivetype)
- if(abs(Random()%2)||animation[player[j].targetanimation].height!=lowheight||j!=0)
- if(findDistancefast(&player[i].coords,&player[j].coords)<400)
- if(normaldotproduct(player[i].facing,player[j].coords-player[i].coords)>0)
- if(player[j].coords.y<player[i].coords.y+5||player[j].onterrain)
- if(!player[j].isWallJump()&&-1==checkcollide(
- DoRotation(playerJoint(i,head).position,0,player[i].rotation,0)
- *player[i].scale+player[i].coords,
- DoRotation(playerJoint(j,head).position,0,player[j].rotation,0)
- *player[j].scale+player[j].coords)||
- (player[j].targetanimation==hanganim&&
- normaldotproduct(player[j].facing,player[i].coords-player[j].coords)<0)){
- player[i].aitype=searchtype;
- player[i].lastchecktime=12;
- player[i].lastseen=player[j].coords;
- player[i].lastseentime=12;
- }
- }
- }
- if(player[i].aitype==attacktypecutoff&&musictype!=2)
- if(player[i].creature!=wolftype){
- player[i].stunned=.6;
- player[i].surprised=.6;
- }
- }
-
- if(player[i].aitype!=passivetype&&leveltime>.5)
- player[i].howactive=typeactive;
-
- if(player[i].aitype==passivetype){
- player[i].aiupdatedelay-=multiplier;
- player[i].losupdatedelay-=multiplier;
- player[i].lastseentime+=multiplier;
- player[i].pausetime-=multiplier;
- if(player[i].lastseentime>1)
- player[i].lastseentime=1;
-
- if(player[i].aiupdatedelay<0){
- if(player[i].numwaypoints>1&&player[i].howactive==typeactive&&player[i].pausetime<=0){
- player[i].targetrotation=roughDirectionTo(player[i].coords,player[i].waypoints[player[i].waypoint]);
- player[i].lookrotation=player[i].targetrotation;
- player[i].aiupdatedelay=.05;
-
- if(findDistancefastflat(&player[i].coords,&player[i].waypoints[player[i].waypoint])<1){
- if(player[i].waypointtype[player[i].waypoint]==wppause)
- player[i].pausetime=4;
- player[i].waypoint++;
- if(player[i].waypoint>player[i].numwaypoints-1)
- player[i].waypoint=0;
-
- }
- }
-
- if(player[i].numwaypoints>1&&player[i].howactive==typeactive&&player[i].pausetime<=0)
- player[i].forwardkeydown=1;
- else
- player[i].forwardkeydown=0;
- player[i].leftkeydown=0;
- player[i].backkeydown=0;
- player[i].rightkeydown=0;
- player[i].crouchkeydown=0;
- player[i].attackkeydown=0;
- player[i].throwkeydown=0;
-
- if(player[i].avoidcollided>.8&&!player[i].jumpkeydown&&player[i].collided<.8){
- if(!player[i].avoidsomething)
- player[i].targetrotation+=90*(player[i].whichdirection*2-1);
- else{
- XYZ leftpos,rightpos;
- float leftdist,rightdist;
- leftpos = player[i].coords+DoRotation(player[i].facing,0,90,0);
- rightpos = player[i].coords-DoRotation(player[i].facing,0,90,0);
- leftdist = findDistancefast(&leftpos, &player[i].avoidwhere);
- rightdist = findDistancefast(&rightpos, &player[i].avoidwhere);
- if(leftdist<rightdist)
- player[i].targetrotation+=90;
- else
- player[i].targetrotation-=90;
- }
- }
- }
- if(player[i].collided<1||player[i].targetanimation!=jumpupanim)
- player[i].jumpkeydown=0;
- if((player[i].collided>.8&&player[i].jumppower>=5))
- player[i].jumpkeydown=1;
-
-
- //hearing sounds
- if(!editorenabled){
- if(player[i].howactive<=typesleeping)
- if(numenvsounds>0&&(tutoriallevel!=1||cananger)&&hostile)
- for(int j=0;j<numenvsounds;j++){
- float vol=player[i].howactive==typesleeping?envsoundvol[j]-14:envsoundvol[j];
- if(vol>0&&findDistancefast(&player[i].coords,&envsound[j])<
- 2*(vol+vol*(player[i].creature==rabbittype)*3))
- player[i].aitype=attacktypecutoff;
- }
-
- if(player[i].aitype!=passivetype){
- if(player[i].howactive==typesleeping)
- setAnimation(i,getupfromfrontanim);
- player[i].howactive=typeactive;
- }
- }
-
- if(player[i].howactive<typesleeping&&
- ((tutoriallevel!=1||cananger)&&hostile)&&
- !player[0].dead&&
- findDistancefast(&player[i].coords,&player[0].coords)<400&&
- player[i].occluded<25){
- if(findDistancefast(&player[i].coords,&player[0].coords)<12&&
- animation[player[0].targetanimation].height!=lowheight&&!editorenabled)
- player[i].aitype=attacktypecutoff;
- if(findDistancefast(&player[i].coords,&player[0].coords)<30&&
- animation[player[0].targetanimation].height==highheight&&!editorenabled)
- player[i].aitype=attacktypecutoff;
-
- //wolf smell
- if(player[i].creature==wolftype){
- XYZ windsmell;
- for(int j=0;j<numplayers;j++){
- if(j==0||(player[j].dead&&player[j].bloodloss>0)){
- float smelldistance=50;
- if(j==0&&player[j].num_weapons>0){
- if(weapons[player[j].weaponids[0]].bloody)
- smelldistance=100;
- if(player[j].num_weapons==2)
- if(weapons[player[j].weaponids[1]].bloody)
- smelldistance=100;
- }
- if(j!=0)
- smelldistance=100;
- windsmell=windvector;
- Normalise(&windsmell);
- windsmell=windsmell*2+player[j].coords;
- if(findDistancefast(&player[i].coords,&windsmell)<smelldistance&&!editorenabled)
- player[i].aitype=attacktypecutoff;
- }
- }
- }
-
- if(player[i].howactive<typesleeping&&player[i].losupdatedelay<0&&!editorenabled&&player[i].occluded<2){
- player[i].losupdatedelay=.2;
- for(int j=0;j<numplayers;j++){
- if(j==0||player[j].skeleton.free||player[j].aitype!=passivetype){
- if(abs(Random()%2)||animation[player[j].targetanimation].height!=lowheight||j!=0)
- if(findDistancefast(&player[i].coords,&player[j].coords)<400)
- if(normaldotproduct(player[i].facing,player[j].coords-player[i].coords)>0)
- if((-1==checkcollide(
- DoRotation(playerJoint(i,head).position,0,player[i].rotation,0)*
- player[i].scale+player[i].coords,
- DoRotation(playerJoint(j,head).position,0,player[j].rotation,0)*
- player[j].scale+player[j].coords)&&
- !player[j].isWallJump())||
- (player[j].targetanimation==hanganim&&
- normaldotproduct(player[j].facing,player[i].coords-player[j].coords)<0)){
- player[i].lastseentime-=.2;
- if(j==0&&animation[player[j].targetanimation].height==lowheight)
- player[i].lastseentime-=.4;
- else
- player[i].lastseentime-=.6;
- }
- if(player[i].lastseentime<=0){
- player[i].aitype=searchtype;
- player[i].lastchecktime=12;
- player[i].lastseen=player[j].coords;
- player[i].lastseentime=12;
- }
- }
- }
- }
- }
- //alerted surprise
- if(player[i].aitype==attacktypecutoff&&musictype!=2){
- if(player[i].creature!=wolftype){
- player[i].stunned=.6;
- player[i].surprised=.6;
- }
- if(player[i].creature==wolftype){
- player[i].stunned=.47;
- player[i].surprised=.47;
- }
- numseen++;
- }
- }
-
- //search for player
- int j;
- if(player[i].aitype==searchtype){
- player[i].aiupdatedelay-=multiplier;
- player[i].losupdatedelay-=multiplier;
- if(!player[i].pause)
- player[i].lastseentime-=multiplier;
- player[i].lastchecktime-=multiplier;
-
- if(player[i].isRun()&&!player[i].onground){
- if(player[i].coords.y>terrain.getHeight(player[i].coords.x,player[i].coords.z)+10){
- XYZ test2=player[i].coords+player[i].facing;
- test2.y+=5;
- XYZ test=player[i].coords+player[i].facing;
- test.y-=10;
- j=checkcollide(test2,test,player[i].laststanding);
- if(j==-1)
- j=checkcollide(test2,test);
- if(j==-1){
- player[i].velocity=0;
- setAnimation(i,player[i].getStop());
- player[i].targetrotation+=180;
- player[i].stunned=.5;
- //player[i].aitype=passivetype;
- player[i].aitype=pathfindtype;
- player[i].finalfinaltarget=player[i].waypoints[player[i].waypoint];
- player[i].finalpathfindpoint=-1;
- player[i].targetpathfindpoint=-1;
- player[i].lastpathfindpoint=-1;
- player[i].lastpathfindpoint2=-1;
- player[i].lastpathfindpoint3=-1;
- player[i].lastpathfindpoint4=-1;
- }
- else player[i].laststanding=j;
- }
- }
- //check out last seen location
- if(player[i].aiupdatedelay<0){
- player[i].targetrotation=roughDirectionTo(player[i].coords,player[i].lastseen);
- player[i].lookrotation=player[i].targetrotation;
- player[i].aiupdatedelay=.05;
- player[i].forwardkeydown=1;
-
- if(findDistancefastflat(&player[i].coords,&player[i].lastseen)<1*sq(player[i].scale*5)||player[i].lastchecktime<0){
- player[i].forwardkeydown=0;
- player[i].aiupdatedelay=1;
- player[i].lastseen.x+=(float(Random()%100)-50)/25;
- player[i].lastseen.z+=(float(Random()%100)-50)/25;
- player[i].lastchecktime=3;
- }
-
- player[i].leftkeydown=0;
- player[i].backkeydown=0;
- player[i].rightkeydown=0;
- player[i].crouchkeydown=0;
- player[i].attackkeydown=0;
- player[i].throwkeydown=0;
-
- if(player[i].avoidcollided>.8&&!player[i].jumpkeydown&&player[i].collided<.8){
- if(!player[i].avoidsomething)player[i].targetrotation+=90*(player[i].whichdirection*2-1);
- else{
- XYZ leftpos,rightpos;
- float leftdist,rightdist;
- leftpos = player[i].coords+DoRotation(player[i].facing,0,90,0);
- rightpos = player[i].coords-DoRotation(player[i].facing,0,90,0);
- leftdist = findDistancefast(&leftpos, &player[i].avoidwhere);
- rightdist = findDistancefast(&rightpos, &player[i].avoidwhere);
- if(leftdist<rightdist)player[i].targetrotation+=90;
- else player[i].targetrotation-=90;
- }
- }
- }
- if(player[i].collided<1||player[i].targetanimation!=jumpupanim)
- player[i].jumpkeydown=0;
- if((player[i].collided>.8&&player[i].jumppower>=5))
- player[i].jumpkeydown=1;
-
- if(numenvsounds>0&&((tutoriallevel!=1||cananger)&&hostile))
- for(int k=0;k<numenvsounds;k++){
- if(findDistancefast(&player[i].coords,&envsound[k])<2*(envsoundvol[k]+envsoundvol[k]*(player[i].creature==rabbittype)*3)){
- player[i].aitype=attacktypecutoff;
- }
- }
-
- if(!player[0].dead&&
- player[i].losupdatedelay<0&&
- !editorenabled&&
- player[i].occluded<2&&
- ((tutoriallevel!=1||cananger)&&hostile)){
- player[i].losupdatedelay=.2;
- if(findDistancefast(&player[i].coords,&player[0].coords)<4&&animation[player[i].targetanimation].height!=lowheight){
- player[i].aitype=attacktypecutoff;
- player[i].lastseentime=1;
- }
- if(abs(Random()%2)||animation[player[i].targetanimation].height!=lowheight)
- //TODO: factor out canSeePlayer()
- if(findDistancefast(&player[i].coords,&player[0].coords)<400)
- if(normaldotproduct(player[i].facing,player[0].coords-player[i].coords)>0)
- if((checkcollide(
- DoRotation(playerJoint(i,head).position,0,player[i].rotation,0)*
- player[i].scale+player[i].coords,
- DoRotation(playerJoint(0,head).position,0,player[0].rotation,0)*
- player[0].scale+player[0].coords)==-1)||
- (player[0].targetanimation==hanganim&&normaldotproduct(
- player[0].facing,player[i].coords-player[0].coords)<0)){
- /* //TODO: changed j to 0 on a whim, make sure this is correct
- (player[j].targetanimation==hanganim&&normaldotproduct(
- player[j].facing,player[i].coords-player[j].coords)<0)
- */
- player[i].aitype=attacktypecutoff;
- player[i].lastseentime=1;
- }
- }
- //player escaped
- if(player[i].lastseentime<0){
- //player[i].aitype=passivetype;
- numescaped++;
- player[i].aitype=pathfindtype;
- player[i].finalfinaltarget=player[i].waypoints[player[i].waypoint];
- player[i].finalpathfindpoint=-1;
- player[i].targetpathfindpoint=-1;
- player[i].lastpathfindpoint=-1;
- player[i].lastpathfindpoint2=-1;
- player[i].lastpathfindpoint3=-1;
- player[i].lastpathfindpoint4=-1;
- }
- }
-
- if(player[i].aitype!=gethelptype)
- player[i].runninghowlong=0;
-
- //get help from buddies
- if(player[i].aitype==gethelptype){
- player[i].runninghowlong+=multiplier;
- player[i].aiupdatedelay-=multiplier;
-
- if(player[i].aiupdatedelay<0||player[i].ally==0){
- player[i].aiupdatedelay=.2;
-
- //find closest ally
- //TODO: factor out closest search somehow
- if(!player[i].ally){
- int closest=-1;
- float closestdist=-1;
- for(int k=0;k<numplayers;k++){
- if(k!=i&&k!=0&&!player[k].dead&&
- player[k].howactive<typedead1&&
- !player[k].skeleton.free&&
- player[k].aitype==passivetype){
- float distance=findDistancefast(&player[i].coords,&player[k].coords);
- if(closestdist==-1||distance<closestdist){
- closestdist=distance;
- closest=k;
- }
- closest=k;
- }
- }
- if(closest!=-1)
- player[i].ally=closest;
- else
- player[i].ally=0;
- player[i].lastseen=player[0].coords;
- player[i].lastseentime=12;
- }
-
-
- player[i].lastchecktime=12;
-
- XYZ facing=player[i].coords;
- XYZ flatfacing=player[player[i].ally].coords;
- facing.y+=playerJoint(i,head).position.y*player[i].scale;
- flatfacing.y+=playerJoint(player[i].ally,head).position.y*player[player[i].ally].scale;
- if(-1!=checkcollide(facing,flatfacing))
- player[i].lastseentime-=.1;
-
- //no available ally, run back to player
- if(player[i].ally<=0||
- player[player[i].ally].skeleton.free||
- player[player[i].ally].aitype!=passivetype||
- player[i].lastseentime<=0){
- player[i].aitype=searchtype;
- player[i].lastseentime=12;
- }
-
- //seek out ally
- if(player[i].ally>0){
- player[i].targetrotation=roughDirectionTo(player[i].coords,player[player[i].ally].coords);
- player[i].lookrotation=player[i].targetrotation;
- player[i].aiupdatedelay=.05;
- player[i].forwardkeydown=1;
-
- if(findDistancefastflat(&player[i].coords,&player[player[i].ally].coords)<3){
- player[i].aitype=searchtype;
- player[i].lastseentime=12;
- player[player[i].ally].aitype=searchtype;
- if(player[player[i].ally].lastseentime<player[i].lastseentime){
- player[player[i].ally].lastseen=player[i].lastseen;
- player[player[i].ally].lastseentime=player[i].lastseentime;
- player[player[i].ally].lastchecktime=player[i].lastchecktime;
- }
- }
-
- if(player[i].avoidcollided>.8&&!player[i].jumpkeydown&&player[i].collided<.8){
- if(!player[i].avoidsomething)
- player[i].targetrotation+=90*(player[i].whichdirection*2-1);
- else{
- XYZ leftpos,rightpos;
- float leftdist,rightdist;
- leftpos = player[i].coords+DoRotation(player[i].facing,0,90,0);
- rightpos = player[i].coords-DoRotation(player[i].facing,0,90,0);
- leftdist = findDistancefast(&leftpos, &player[i].avoidwhere);
- rightdist = findDistancefast(&rightpos, &player[i].avoidwhere);
- if(leftdist<rightdist)
- player[i].targetrotation+=90;
- else
- player[i].targetrotation-=90;