Page 1 of 1

OK,Here's a Hard One!

Posted: Tue Jul 22, 2008 6:44 pm
by metal_head
I'm asking a lot of questions,because I'm newbie,but I'm trying to make the perfectai.s script better,so that bigger newbies than me can enjoy it :D.
Now,here's the question.
Is it possible to make the pawns using the perfectai.s script attack the player when they are running towards him.
If the shoot range of an enemy is 500,the player has to be into that distance in order the pawn to attack him.If the player is in let's say 550 texels from the pawn,the pawn stops attacking the player and starts running in order to get in 500 texels from the player so it can attack again.That doesn't really look very good.My idea is to make 2 shooting ranges(let's say the first is 500 and the secon 800)and when the player is in 500 or less from the pawn,the pawn attacks the player standing still,but if the player gets further than 500 the pawn starts to chase the player,but shooting at him,and if the player gets in more that 800 texels from the pawn the pawn continues to chas the player,but stops shooting.
Is that possible?What should be changed?and could this order be the answer(I mean changing something here)

Code: Select all

ai_run[ (dist)
	{
		if (attack_state = AS_MELEE) 			// do melee attack
		{
			ai_run_melee();
		}
		if (attack_state = AS_MISSILE) 			// do missile attack
		{
			ai_run_missile();
		}

		attack_state = AS_NONE;

		if (CheckAnyAttack()) 				// check if you can start the actual attack
		{
			return 0;
		}
		if(RUNSPEED > 0)
		{
			walk_movetogoal(dist); 			// else move toward the enemy
		}
	} ]

Re: OK,Here's a Hard One!

Posted: Sat Jul 26, 2008 4:51 pm
by Juutis
I've never really used perfectai. To me it's just so messy and there's a lot of unnecessary stuff in there. But most likely you would have to redo the whole running order(s). I'd start by creating different orders for each of the distances (>500, 500-800, <800). I don't have access to perfectai right now so I can't tell exactly what to change, but maybe once I get back home in a few days...

Re: OK,Here's a Hard One!

Posted: Sat Jul 26, 2008 6:22 pm
by metal_head
Here's perfectai:

Code: Select all

{
	
	SCALE				[1]			// scale of actor
	GROUP				[enemy]	// name of monster group
	BOXWIDTH			[25]		// width and depth of bounding box
	HOSTILEPLAYER		[true]		// hostile to player
	HOSTILEDIFFERENT	[false]		// hostile to different Pawn group
	HOSTILESAME			[false]		// hostile to same Pawn group
	HEALTHATTRIBUTE		[enemy_health]	// name of health attribute
	HEALTH				[2000]		// initial amount of health
	SIGHTDIST			[2000]		// max distance monster can see at idle
	ALERTSIGHTDIST		[3500]		// max distance monster can see when alert
	FOV					[360]		// field of view in degrees
	ATTACKFOV			[360]		// attacking field of view
	YAWSPEED			[360]		// speed of rotation in deg/sec
	DAMAGEATTRIBUTE		[health]	// attribute damaged by attack
	ALERTTRIGGER		[AlertG]	// name of alert trigger

	// define the type of monster this will be

	ATTACKTYPE			[melee]	// type of attack - melee or missile
	PATROL				[false]		// false - ambush or true - patrol
	PC					[0]			// make any visible point the next point

	// idling and turning

	STAND				[idle]		// idle animation
	TURNL				[fliplef]		// turn left animation
	TURNR				[flipright]		// turn right animation

	// when in pain

	PAIN				[pain_head]		// pain animations
	PAIN1				[pain_stomach]
	PAIN2				[pain_head]
	PAIN3				[pain_stomach]
	PAINPERCENT			[10]				// percentage of time pain is shown
	PAINSOUND			[pl_pain6.wav]		// sounds played when in pain
	PAINSOUND1			[injury.wav]
	PAINSOUND2			[injury.wav]
	PAINSOUND3			[injury.wav]

	// when dying

	DIE					[die_backwards]		// dying animations
	DIE1				[die_backwards]
	DIE2				[die_backwards]
	DIE3				[die_backwards]
	DIEHOLD				[2]				// time corpse still appears
	DIEFADE				[2]				// fadeout time of corpse
	DIESOUND			[deathyell.wav]		// sounds played when dying
	DIESOUND1			[die.wav]
	DIESOUND2			[death1.wav]
	DIESOUND3			[deathyell.wav]

		// when running to attack

	RUN					[run]			// running animation
	RUNSPEED			[500]			// average run speed
	RUNSOUNDDELAY		[2]				// delay between making sounds when running to attack
	RUNSOUND			[breath.wav]	// sound played while running to attack

	// when walking while patroling

	WALK				[walk]			// walking animation
	WALKSPEED			[100]			// average walking speed
	WALKSOUND			[breath.wav]	// walking sound

	// the projectile attack mode

	MISSILEATTACK		[punches]	// missile attacking animations
	MISSILEATTACK1		[punches]
	MISSILEATTACK2		[punches]
	SHOOTUP				[punches]
	SHOOTDOWN			[punches]
	FIREDELAY			[0.3]						// delay after animation starts before projectile launch
	FIREDELAY1			[0.3]
	FIREDELAY2			[0.3]
	MISSILESOUND		[]				// sounds played when shooting
	MISSILESOUND1		[]
	MISSILESOUND2		[]
	MISSILERANGE		[50]						// max distance to start missile attack
	PROJECTILE			[nosound]				// projectile name
	FIREBONE			[Bip01 R Finger1]			// projectile launch bone
	OFFSETX			[0]				// launch offsets
	OFFSETY			[0]
	OFFSETZ			[0]
	ATTACKDELAY			[0.3]				// time between shots
	SKILL				[5]				// skill level 1 to 10

	// the melee attack mode

	MELEEATTACK			[ref_shoot_kick_blend1]	// melee attacking animations
	MELEEATTACK1		[ref_shoot_kick_blend1]
	MELEEATTACK2		[ref_shoot_kick_blend1]
	MELEESOUND			[uuh.wav]		// sounds played when melee attacking
	MELEESOUND1			[uuh.wav]
	MELEESOUND2			[uuh.wav]
	MELEERANGE			[55]			// max distance to start melee attack
	//MAXMELEERANGE		[400]			// max distance for melee mode
	MINMELEEDAMAGE		[10]			// minimum amount of damage per melee attack
	MAXMELEEDAMAGE		[10]			// maximum amount of damage per melee attack
	MELEEDELAY			[2]			// number of seconds between melee damages
	MELEEDAMAGESOUND		[melee2.wav]		// sound played when damage is done

	// search for enemy

	LOSTTIME			[10]	// time to search for enemy before giving up
	POINTRADIUS			[20]	// radius from point when considered there

	// obstacle avoidance forces for jumping

	FORCEUP			[15]	// obstacle avoidance jump speed
	FORCEFORWARD		[20]	// obstacle avoidance forward speed
	FORCESIDE			[20]	// obstacle avoidance sideways speed

	// local variables - do not change

	
	RUNFUNC			[monster_run_start]		// monster run to attack function
	MISSILEFUNC			[monster_missile_start]		// monster missile function
	MELEEFUNC			[monster_melee_start]		// monster melee function
	LOSTFUNC			[monster_lost_target_start]	// monster lost target function

	AS_NONE			[0]
	AS_MELEE			[1]
	AS_MISSILE			[2]
	AS_STRAIGHT			[3]
	attack_delay			[0]
	melee_time			[0]
	lost_time			[0]
	back_up				[false]
	back_time			[0]
	left_time				[0]
	back_flag			[false]
	fire_delay			[0]
	skill_time			[0]
	attack_state			[0]
	run_sound_time			[0]
	missile_sound			[NULL]

	// spawn pawn and do setup work

	Spawn[ ()
	{
		Console(true);
		Scale(SCALE);	// scale the actor
		if(BOXWIDTH > 0)
		{
			BoxWidth(BOXWIDTH*SCALE);	// set bounding box width/depth
		}
		AttributeOrder(HEALTHATTRIBUTE, HEALTH, "Death");	// give monster health
		HostilePlayer(HOSTILEPLAYER);						// set who monster is hostile to
		HostileSame(HOSTILESAME);
		HostileDifferent(HOSTILEDIFFERENT);
		SetFOV(360,"Bip01 Head");							// set field of view
		SetGroup(GROUP);									// assign a group to belong to
		FindTargetOrder(SIGHTDIST, "FoundTarget", DAMAGEATTRIBUTE);	// seen a target to chase
		AddPainOrder("IdlePain", 100);							// show pain and trigger alert
		AvoidOrder("Avoidance"); 								// avoid obstacles
		AddTriggerOrder("IdleToAlert", ALERTTRIGGER, 0);	// go to alert when triggered
		SetWeapon("Mp5k2"); 								// give the monster a big gun
		RotateToPoint(STAND, YAWSPEED, false, ""); 			// go to point if any specified
		MoveToPoint(WALK, WALKSPEED*SCALE, WALKSOUND);
		if(PATROL = true)
		{
			NewOrder("Patrol");
		}
		else
		{
			NewOrder("Idle");
		}
	} ]

	// avoid objects when doing a MoveToPoint

	Avoidance[ ()
	{
		if(random(1,10)<3)	// backup and move sideways sometimes
		{
			MoveBackward(WALK, WALKSPEED*SCALE, (WALKSPEED/2)*SCALE, "");
			MoveRight(WALK, WALKSPEED*SCALE, (WALKSPEED/2)*SCALE, "");
		}
		else
		{
			Jump(RUN, FORCEUP*SCALE, true, "");
			if(random(1,10)<6)
			{
				Move("", FORCEFORWARD*SCALE, (FORCEFORWARD/2)*SCALE, 0, 90, 0, "");
			}
			else
			{
				Move("", FORCEFORWARD*SCALE, (FORCEFORWARD/2)*SCALE, 0, -90, 0, "");
			}
		}
		Return();
	} ]

	// idle in place waiting for something to happen

	Idle[ ()
	{
		PlayAnimation(STAND, true, "");
		RestartOrder();
	} ]	

	// walk the beat from point to point

	Patrol[ ()
	{
		NextPoint();
		RotateMoveToPoint(WALK, YAWSPEED, WALKSPEED*SCALE, false, WALKSOUND);
		MoveToPoint(WALK, WALKSPEED*SCALE, WALKSOUND);
		RestartOrder();
	} ]

	// show pain at idle then trigger to alert

	IdlePain[ ()
	{
		
		NewOrder("FoundTarget");
	} ]	

	// start shifting from idle to alert

	IdleToAlert[ ()
	{
		NewOrder("FoundTarget");
	} ]

	// look around at alert looking for enemy

	Alert[ ()
	{
		NewOrder("FoundTarget");
	} ]	

	// show pain at alert

	AlertPain[ ()
	{
		NewOrder("FoundTarget");
	} ]	

	// timed out at alert

	AlertToIdle[ ()
	{
		NewOrder("FoundTarget");
	} ]

	// found a target to attack

	FoundTarget[ ()
	{
		SetFOV(360,"Bip01 Head");	// set field of view
		LowLevel(RUNFUNC);		// attack functions are low level
	} ]

	// lost target while attacking so go back to idle again

	LostTarget[ ()
	{
		FindTargetOrder(SIGHTDIST, "FoundTarget", DAMAGEATTRIBUTE);	// seen a target to chase
		FindPointOrder("Patrol");							// do point stuff
		if(PATROL = true)
		{
			NewOrder("Patrol");
		}
		else
		{
			NewOrder("Idle");
		}
	} ]

	// you died

	Death[ ()
	{
		//SetNoCollision();		// remove bounding box so there are no collisions with corpse
		Remove = false;			// remove actor
	    	RemoveWeapon = false; 		// remove the current weapon actor.
	   	return 0;
   	 }]

	// Low level attack routines

	// Start of run to attack

	monster_run_start[ ()
	{
		TargetPlayer();
		UpdateEnemyVis(true); 			//always aware of player position
		UpdateTarget();
		Animate(RUN);				// play run animation
		self.ThinkTime = 0;			// start thinking on next frame
		self.think = "monster_run";		// go to run attack routine
		self.ideal_yaw = enemy_yaw;		// set direction to run
		self.yaw_speed = YAWSPEED;		// set rotation speed
		back_up = false;				// initialize obstacle avoidance
		attack_state = AS_NONE;			// not attacking yet
		melee_time = time;
		run_sound_time = time;
	} ]

	// run to enemy to attack

	monster_run[ ()
	{
		self.ThinkTime = 0;
		if(self.health<=0)
		{
			self.think = "death_biz"; 			// dead
			return 0;
		}
		if((self.in_pain = true) and (random(1,100)<PAINPERCENT))
		{
			self.think = "monster_run_pain_start"; 	// in pain
			return 0;
		}
		if(EnemyExist(DAMAGEATTRIBUTE) < 3)
		{
			HighLevel("LostTarget"); 			// enemy is gone or dead
			return 0;
		}
		if(enemy_vis = false)
		{
			self.think = LOSTFUNC; 			// lost sight of enemy
			lost_time = time + LOSTTIME;
			return 0;
		}
		if(run_sound_time < time)
		{
			if(random(1,10)>7)
			{
				run_sound_time = time + RUNSOUNDDELAY;
				PlaySound(RUNSOUND);
			}
		}
		ai_run(random((RUNSPEED-2)*SCALE,(RUNSPEED+2)*SCALE)); // run toward enemy
	} ]

	// start of pain while running

	monster_run_pain_start[ ()
	{
		switch(random(1,4)) 					// play one of 4 pain animations
		{
			case 1
			{
				Animate(PAIN);
				PlaySound("pl_pain2.wav", 500);
			}
			case 2
			{
				Animate(PAIN1);
				PlaySound("pl_pain2.wav", 500);
			}
			case 3
			{
				Animate(PAIN2);
				PlaySound("pl_pain2.wav", 500);
			}
			case 4
			{
				Animate(PAIN3);
				PlaySound("pl_pain2.wav", 500);
			}
		}
		SetHoldAtEnd(true);					// set to stop at animation end
		self.ThinkTime = 0;
		self.think = "monster_run_pain";
	} ]

	// wait for animation to stop

	monster_run_pain[ ()
	{
		self.ThinkTime = 0;
		if(self.animate_at_end = true) 		// wait for end of animation
		{
			self.think = "monster_run_start"; 	// start running again
			SetHoldAtEnd(false); 		// remove animation stop
			self.ThinkTime = 0;
		}
	} ]

	// start of lost sight of enemy routine

	monster_lost_target_start[ ()
	{
		Animate(RUN); 				// play run animation
		self.ThinkTime = 0;			// start thinking on next frame
		self.think = "monster_lost_target";
		run_sound_time = time + RUNSOUNDDELAY;
	} ]

	// go to last known location of enemy

	monster_lost_target[ ()
	{
		self.ThinkTime = 0;
		if(lost_time<time)
		{
			HighLevel("LostTarget"); 			// timed out while looking
			return 0;
		}
		self.ThinkTime = 0;
		if(self.health<=0)
		{
			self.think = "death_biz"; 			// dead
			return 0;
			}
		if((self.in_pain = true) and (random(1,100)<PAINPERCENT))
		{
			self.think = "monster_lost_pain_start"; 	// in pain
			return 0;
		}
		if(EnemyExist(DAMAGEATTRIBUTE) < 3)
		{
			HighLevel("LostTarget"); 			// enemy died or was removed
			return 0;
		}
		if(enemy_vis = true)
		{
			PlaySound("overhere_a.wav", 500);
			self.think = "monster_run_start"; 		// seen again so run to attack
			self.ThinkTime = 0;
			return 0;
		} 
		if(run_sound_time < time)
		{
			if(random(1,10)>7)
			{
				run_sound_time = time + RUNSOUNDDELAY;
				PlaySound(RUNSOUND);
			}
		}
		if((enemy_range>POINTRADIUS) and (RUNSPEED > 0)) // get close to last known location
		{
			walk_movetogoal(random((RUNSPEED-2)*SCALE,(RUNSPEED+2)*SCALE));
					} 
		else
		{
			HighLevel("LostTarget"); // can't find at last known location
			return 0;
		}
	} ]

	// start of showing pain while searching

	monster_lost_pain_start[ ()
	{
		switch(random(1,4)) // play one of 4 pain animations
		{
			case 1
			{
				Animate(PAIN);
				PlaySound("pl_pain2.wav", 500);
			}
			case 2
			{
				Animate(PAIN1);
				PlaySound("pl_pain2.wav", 500);
			}
			case 3
			{
				Animate(PAIN2);
				PlaySound("pl_pain2.wav", 500);
			}
			case 4
			{
				Animate(PAIN3);
				PlaySound("pl_pain2.wav", 500);
			}
		}
		SetHoldAtEnd(true);	// set to stop at end
		self.ThinkTime = 0;
		self.think = "monster_lost_pain";
	} ]

	// wait till animation is done

	monster_lost_pain[ ()
	{
		self.ThinkTime = 0;
		if(self.animate_at_end = true)			 // animation done
		{
			self.think = "monster_lost_target_start"; 	// go back to finding target
			SetHoldAtEnd(false); 			// remove stop at end
			self.ThinkTime = 0;
		}
	} ]
	
	CheckForPoints[()
	{
		self.ThinkTime=0; // Process every frame
		if(self.point_vis)	
		{
			// point is visible - go to high level for nav and targetting
			HighLevel("LostTarget");
			return 0;
		}
		else
		{
			// Point not visible - cycle points
			PC=PC+1; // Increment point counter variable
			if(PC > 110)
			{
				// Point count has exceeded max - set to 0. (Max 110)
				PC=1;
			}
			NextPoint();
		}
	}]
	
	// start of missile attack

	monster_missile_start[ ()
	{
		switch(random(1,3)) // play one of 3 missile animations
		{
			case 1
			{
				Animate(MISSILEATTACK);
				fire_delay = time + FIREDELAY; // set firing delay
				PlaySound("shotfire.wav", 500);
			}
			case 2
			{
				Animate(MISSILEATTACK1);
				fire_delay = time + FIREDELAY1; // set firing delay
				PlaySound("shotfire.wav", 500);
			}
			case 3
			{
				Animate(MISSILEATTACK2);
				fire_delay = time + FIREDELAY2; // set firing delay
				PlaySound("shotfire.wav", 500);
			}
		}
		TargetPlayer();
		UpdateEnemyVis(true); 			//always aware of player position
		UpdateTarget();
		self.ideal_yaw = enemy_yaw;		// set direction to run
		SetHoldAtEnd(true);
		self.ThinkTime = 0.3;
		self.think = "monster_missile";
		skill_time = time + (SKILL*0.1);		// calculate next update time
		attack_delay = time + ATTACKDELAY;	// delay until next shot
	} ]

	// attack target with projectile

	monster_missile[ ()
	{
		self.ThinkTime = 0.3;
		if(self.health<=0)
		{
			self.think = "death_biz";
			return 0;
			}
		if((self.in_pain = true) and (random(1,100)<PAINPERCENT))
		{
			self.think = "monster_missile_pain_start"; 	// in pain
			SetHoldAtEnd(false);
			return 0;
		}
		exist = EnemyExist(DAMAGEATTRIBUTE);
		if(exist < 2)
		{
			SetHoldAtEnd(false);
			HighLevel("LostTarget"); 			// enemy is dead and gone
			return 0;
		}
		if(exist = 2)
		{
			SetHoldAtEnd(false);
			HighLevel("LostTarget"); 			// enemy is dead but body remains
			return 0;
		}
		if(enemy_vis = false)
		{
			self.think = LOSTFUNC; 			// lost sight of enemy
			lost_time = time + LOSTTIME;
			SetHoldAtEnd(false);
			return 0;
		}
		if(self.player_Y>self.current_Y)
		{
			Animate(SHOOTUP);
			FireProjectile(PROJECTILE, FIREBONE, OFFSETX, OFFSETY, OFFSETZ, DAMAGEATTRIBUTE);
			PlaySound("shotfire.wav", 500);
			SetHoldAtEnd(false);
			self.ThinkTime = 0.3;
		}
		if(self.player_Y<self.current_Y)
		{
			Animate(SHOOTDOWN);
			FireProjectile(PROJECTILE, FIREBONE, OFFSETX, OFFSETY, OFFSETZ, DAMAGEATTRIBUTE);
			PlaySound("shotfire.wav", 500);
			SetHoldAtEnd(false);
			self.ThinkTime = 0.3;
		}
		if(enemy_range>MISSILERANGE)
		{
			Animate(MISSILEATTACK);
			FireProjectile(PROJECTILE, FIREBONE, OFFSETX, OFFSETY, OFFSETZ, DAMAGEATTRIBUTE);
			fire_delay = time + FIREDELAY; 		// set firing delay
			PlaySound("shotfire.wav", 500);
			self.think = RUNFUNC; 			// too far away so run toward
			SetHoldAtEnd(false);
			return 0;
		}
		if(skill_time<time) 				// update according to skill level
		{
			UpdateTarget();				// update target location
			skill_time = time + (SKILL*0);
			ai_face();				// face enemy while attacking
		}

		if(fire_delay<time) 				// delay after animation starts before firing
		{
			FireProjectile(PROJECTILE, FIREBONE, OFFSETX, OFFSETY, OFFSETZ, DAMAGEATTRIBUTE);
			fire_delay = time + 1000; 			// set delay well ahead so it is ignored
			PlaySound("shotfire.wav", 500);
		}
		if(self.animate_at_end = true)
		{
			if(attack_delay < time)
			{
				//self.think = RUNFUNC; 		// no
				self.think = "monster_missile_start"; 
				SetHoldAtEnd(false);
				self.ThinkTime = 0;
			}
		}
	} ]
		
	death_biz[ ()
	{
		Animate(DIE);
		PlaySound("deathyell.wav", 500);
		SetHoldAtEnd(true);				// set to stop at end
		self.ThinkTime = 0.1;
		self.think = "Death";
		return 0;
	} ]

	// start of showing pain

	monster_missile_pain_start[ ()
	{
		switch(random(1,4)) // play one of 4 pain animations
		{
			case 1
			{
				Animate(PAIN);
				PlaySound("pl_pain2.wav", 500);
			}
			case 2
			{
				Animate(PAIN1);
				PlaySound("pl_pain2.wav", 500);
			}
			case 3
			{
				Animate(PAIN2);
				PlaySound("pl_pain2.wav", 500);
			}
			case 4
			{
				Animate(PAIN3);
				PlaySound("pl_pain2.wav", 500);
			}
		}
		SetHoldAtEnd(true);	// set to stop at end
		self.ThinkTime = 0;
		self.think = "monster_missile_pain";
	} ]

	// wait until animation is done

	monster_missile_pain[ ()
	{
		self.ThinkTime = 0;
		if(self.animate_at_end = true) 			// animation is done
		{
			self.think = "monster_missile_start"; 	// go back to missile attack
			SetHoldAtEnd(false);
			self.ThinkTime = 0;
		}
	} ]

	// start of melee attack

	monster_melee_start[ ()
	{
		switch(random(1,3)) 	// play one of 3 melee animations
		{
			case 1
			{
				Animate(MELEEATTACK);
			}
			case 2
			{
				Animate(MELEEATTACK1);
			}
			case 3
			{
				Animate(MELEEATTACK2);
			}
		}
		SetHoldAtEnd(true);			// set to stop at end
		self.ThinkTime = 0.1;
		self.think = "monster_melee";
	} ]

	// melee attack

	monster_melee[ ()
	{
		self.ThinkTime = 0;
		if(self.health<=0)
		{
			PlaySound("deathyell.wav", 500);
			self.think = "death_biz"; 	// in pain
			return 0;
		}
		if((self.in_pain = true) and (random(1,100)<PAINPERCENT))
		{
			SetHoldAtEnd(false);
			self.think = "monster_melee_pain_start"; 		// in pain
			return 0.1;
		}
		exist = EnemyExist(DAMAGEATTRIBUTE); 			// see if target is around
		if(exist < 2)
		{
			SetHoldAtEnd(false);
			HighLevel("LostTarget"); 				// enemy is dead and gone
			return 0;
		}
		if(exist = 2)
		{
			SetHoldAtEnd(false);
			HighLevel("DeadTarget"); 			// enemy is dead but body remains
			return 0;
		}
		if(enemy_vis = false)
		{
			SetHoldAtEnd(false);
			self.think = LOSTFUNC; 				// lost sight of enemy
			lost_time = time + LOSTTIME;
			return 0;
		}
		if(enemy_range>(MELEERANGE*SCALE))
		{
			SetHoldAtEnd(false);
			self.think = RUNFUNC; 				// too far away so run toward
			return 0;
		}
		ai_face();						// face enemy while attacking
		if(self.animate_at_end = true) 				// animation is done
		{
			SetHoldAtEnd(false);
			switch(random(1,3)) 				// play one of 3 melee animations
			{
				case 1
				{
					Animate(MELEEATTACK);
				}
				case 2
				{
					Animate(MELEEATTACK1);
				}	
				case 3
				{
					Animate(MELEEATTACK2);
				}
			}
			SetHoldAtEnd(true);									// set to stop at end
		}
		if(time>melee_time) 							// if time then damage
		{
			damage = random(MINMELEEDAMAGE, MAXMELEEDAMAGE); 	// get damage amount
			Damage(damage, DAMAGEATTRIBUTE); 				// damage target
			melee_time = time + MELEEDELAY; 				// reset time until next damage
		}
	} ]

	// start of showing pain

	monster_melee_pain_start[ ()
	{
		switch(random(1,4)) // play one of 4 pain animations
		{
			case 1
			{
				Animate(PAIN);
				PlaySound("pl_pain2.wav", 500);
			}
			case 2
			{
				Animate(PAIN1);
				PlaySound("pl_pain2.wav", 500);
			}
			case 3
			{
				Animate(PAIN2);
				PlaySound("pl_pain2.wav", 500);
			}
			case 4
			{
				Animate(PAIN3);
				PlaySound("pl_pain2.wav", 500);
			}
		}
		SetHoldAtEnd(true);	// set to stop at end
		self.ThinkTime = 0.1;
		self.think = "monster_melee_pain";
	} ]

	// wait until animation is done

	monster_melee_pain[ ()
	{
		self.ThinkTime = 0.1;
		if(self.animate_at_end = true) 		// animation is done
		{
			self.think = "monster_melee_start"; // go back to melee attack
			SetHoldAtEnd(false);
			self.ThinkTime = 0.1;
			melee_time = time + MELEEDELAY; // reset attack deley
		}
	} ]

	// basic AI routines

	// run toward enemy and see if you are ready to attack

	ai_run[ (dist)
	{
		if (attack_state = AS_MELEE) 			// do melee attack
		{
			ai_run_melee();
		}
		if (attack_state = AS_MISSILE) 			// do missile attack
		{
			ai_run_missile();
		}

		attack_state = AS_NONE;

		if (CheckAnyAttack()) 				// check if you can start the actual attack
		{
			return 0;
		}
		if(RUNSPEED > 0)
		{
			walk_movetogoal(dist); 			// else move toward the enemy
		}
	} ]

	// check if ready to do actual attacking

	CheckAnyAttack[ ()
	{
		if(enemy_range<MELEERANGE)
		{
			if(enemy_range<(MELEERANGE*SCALE)) 		// inside melee range
			{
				attack_state = AS_MELEE; 		// do a melee attack
				return true;
			}
			return false;
		}

		if(enemy_range<MISSILERANGE)
		{
			if(attack_delay>time)
			{
				return false;
			}
			if(enemy_range<MISSILERANGE) 
			{
				attack_state = AS_MISSILE; 		// do a missile attack
				return true;
			}
		}
		return false;
	} ]

	// missile attack setup

	ai_run_missile[ ()
	{
		ai_face();
		if(FacingIdeal()) 				// got close enough
		{
			self.think = MISSILEFUNC;	// start missile attack
			attack_state = AS_STRAIGHT;
			UpdateTarget();
			skill_time = time + (SKILL*0.1);
		}
	} ]

	// melee attack setup

	ai_run_melee[ ()
	{
		ai_face(); 			// turn to face target
		if(FacingIdeal()) 			// got close enough
		{
			self.think = MELEEFUNC;	// start melee attack
			self.attack_state = AS_STRAIGHT;
		}
	} ]

	// face enemy

	ai_face[ ()
	{
		self.ideal_yaw = enemy_yaw;
		ChangeYaw(); // rotate to face enemy
	} ]

	// use walkmove to naviagte to enemy

	walk_movetogoal[ (dist)
	{
		if(IsFalling = true)
		{
			return 0;	// don't move while falling
		} 
		if(dist < 0)
		{
			return 0;
		}
		if(back_up = false)
		{
			ai_face();				// turn to face enemy
			if(FacingIdeal())
			{
				if(walkmove(self.current_yaw, dist) = true)
				{
					return 0;			// can move in current direction
				}
				else
				{
					if(random(1,10)<3)	// backup and move sideways
					{
						back_up = false;
						back_time = time + 0.5;
						back_flag = false;
						return 0;
					}
					else 
					{ 
						ForceUp(FORCEUP*SCALE);	// jump up, forward and to side
						ForceForward(FORCEFORWARD*SCALE);
						if(random(1,10)<6)
						{
							ForceRight(FORCESIDE*SCALE);
						}
						else
						{
							ForceLeft(FORCESIDE*SCALE);
						} 
					} 
				} 
			}
		}
		else
		{
			if(back_flag = false) 		// go backward 1/2 sec
			{
				if(back_time > time)
				{
					walkmove((self.current_yaw-(180*0.0174532925199433)), dist);
					return 0;
				}
				else
				{
					back_time = time + 0.5;
					back_flag = true;
				}
			}
			if(back_time > time) 		// go sideways 1/2 sec
			{
				walkmove((self.current_yaw-(90*0.0174532925199433)), dist);
				return 0;
			}
			back_up = false;
		}
	} ]

	// check if nearly facing enemy

	FacingIdeal[ ()
	{
		selfangle = self.current_yaw/0.0174532925199433; 	// your direction in degrees
		idealangle = self.ideal_yaw/0.0174532925199433;	// his direction in degrees
		delta = selfangle - idealangle; 			// difference in directions
		if (delta > -20 and delta < 20) 			// within 20 degrees is close enough
		{
			return true;
		}
		return false;
	} ]

}
Now I know I'll have to add a new variable,which will define the distance in which the pawn will attack when running,but I don't have idea where to start.It's very hard for me.I'm learning different commands and I'm beginning to understand when reading a script,but I can't make a full order or something myself yet.I don't know If you have been through that period (I mean you are maybe a better learner),but but the ai_run [() command is the right place to start working right?

Re: OK,Here's a Hard One!

Posted: Sun Jul 27, 2008 7:21 pm
by metal_head
Is it possible to do it anyway,because the pawn will have to do 2 actions in the same time-running and attacking and I don't have an idea how to do that...

Re: OK,Here's a Hard One!

Posted: Sun Jul 27, 2008 9:43 pm
by Juutis
Yeah, it is possible. Shouldn't be too hard either. I'm gonna dig into the script when I have more time. It's really not a 10-minute thing. :)

Re: OK,Here's a Hard One!

Posted: Sun Jul 27, 2008 10:52 pm
by metal_head
Well,I'm sure it's not gonna work,but I was thinking of copying a part if the attack order of the script and pasting it into the run order and thgat just set it to detect if the player is in 800 texels of the pawn and start attacking...never mind I don't understand it yet :D,I appreciate a lot your help Juutis!

Re: OK,Here's a Hard One!

Posted: Tue Jul 29, 2008 1:21 pm
by Juutis
Ok, so basically the only-run-to-player is already there as well as only-shoot-the-player. So we need to merge them to create the run-and-shoot order. First of all let's give the pawn some new variables! Namely the distances, the animation for attack-move and some other variables:

Code: Select all

MISSILERANGE      [500]
ATTACKMOVERANGE  [800]

ATTACK_MOVE        [run]

AS_ATTACK_MOVE   [4]
ATTACK_MOVE_FUNC    [monster_missile_run_start]
(MISSILERANGE is already there in the script)

Then add some new orders for the attack-move stuff:

Code: Select all

   monster_missile_run_start[ ()
   {
      TargetPlayer();
      UpdateEnemyVis(true);          //always aware of player position
      UpdateTarget();
      Animate(RUN);            // play run animation
      self.ThinkTime = 0;         // start thinking on next frame
      self.think = "monster_missile_run";      // go to run attack routine
      self.ideal_yaw = enemy_yaw;      // set direction to run
      self.yaw_speed = YAWSPEED;      // set rotation speed
      back_up = false;            // initialize obstacle avoidance
      attack_state = AS_NONE;         // not attacking yet
      melee_time = time;
      run_sound_time = time;
      skill_time = time + (SKILL*0.1);      // calculate next update time
      attack_delay = time + ATTACKDELAY;   // delay until next shot
   } ]

   // run and attack enemy

   monster_missile_run[ ()
   {
      self.ThinkTime = 0;
      if(self.health<=0)
      {
         self.think = "death_biz";          // dead
         return 0;
      }
      if((self.in_pain = true) and (random(1,100)<PAINPERCENT))
      {
         self.think = "monster_missile_run_pain_start";    // in pain
         return 0;
      }
      if(EnemyExist(DAMAGEATTRIBUTE) < 3)
      {
         HighLevel("LostTarget");          // enemy is gone or dead
         return 0;
      }
      if(enemy_vis = false)
      {
         self.think = LOSTFUNC;          // lost sight of enemy
         lost_time = time + LOSTTIME;
         return 0;
      }
      if(run_sound_time < time)
      {
         if(random(1,10)>7)
         {
            run_sound_time = time + RUNSOUNDDELAY;
            PlaySound(RUNSOUND);
         }
      }

      if(enemy_range<MISSILERANGE)  //close enough to stop running
      {
         ai_run_missile();
      }
      if(enemy_range>ATTACKMOVERANGE)  //too far
      {
         self.think = RUNFUNC;          //run toward
         SetHoldAtEnd(false);
         return 0;
      }

      if(skill_time<time)             // update according to skill level
      {
         UpdateTarget();            // update target location
         skill_time = time + (SKILL*0);
         ai_face();            // face enemy while attacking
      }
      if(attack_delay<time)             // delay until next shot
      {
         FireProjectile(PROJECTILE, FIREBONE, OFFSETX, OFFSETY, OFFSETZ, DAMAGEATTRIBUTE);
         attack_delay = time + ATTACKDELAY;   // delay until next shot
         PlaySound("shotfire.wav", 500);
      }

      walk_movetogoal(random((RUNSPEED-2)*SCALE,(RUNSPEED+2)*SCALE)); // run toward enemy
   } ]

   // start of pain while running

   monster_missile_run_pain_start[ ()
   {
      switch(random(1,4))                // play one of 4 pain animations
      {
         case 1
         {
            Animate(PAIN);
            PlaySound("pl_pain2.wav", 500);
         }
         case 2
         {
            Animate(PAIN1);
            PlaySound("pl_pain2.wav", 500);
         }
         case 3
         {
            Animate(PAIN2);
            PlaySound("pl_pain2.wav", 500);
         }
         case 4
         {
            Animate(PAIN3);
            PlaySound("pl_pain2.wav", 500);
         }
      }
      SetHoldAtEnd(true);               // set to stop at animation end
      self.ThinkTime = 0;
      self.think = "monster_missile_run_pain";
   } ]

   // wait for animation to stop

   monster_missile_run_pain[ ()
   {
      self.ThinkTime = 0;
      if(self.animate_at_end = true)       // wait for end of animation
      {
         self.think = "monster_missile_run_start";    // start running again
         SetHoldAtEnd(false);       // remove animation stop
         self.ThinkTime = 0;
      }
   } ]
And then some modifications to ai_run() and CheckAnyAttack() routines:

Code: Select all

   ai_run[ (dist)
   {
      if (attack_state = AS_MELEE)          // do melee attack
      {
         ai_run_melee();
      }
      if (attack_state = AS_MISSILE)          // do missile attack
      {
         ai_run_missile();
      }
      if (attack_state = AS_ATTACK_MOVE)          // attack-move
      {
         ai_run_attack_move();
      }

      attack_state = AS_NONE;

      if (CheckAnyAttack())             // check if you can start the actual attack
      {
         return 0;
      }
      if(RUNSPEED > 0)
      {
         walk_movetogoal(dist);          // else move toward the enemy
      }
   } ]

   // check if ready to do actual attacking

   CheckAnyAttack[ ()
   {
      if(enemy_range<MELEERANGE)
      {
         if(enemy_range<(MELEERANGE*SCALE))       // inside melee range
         {
            attack_state = AS_MELEE;       // do a melee attack
            return true;
         }
         return false;
      }

      if(enemy_range<MISSILERANGE)
      {
         if(attack_delay>time)
         {
            return false;
         }
         if(enemy_range<MISSILERANGE)
         {
            attack_state = AS_MISSILE;       // do a missile attack
            return true;
         }
      }
      if((enemy_range>MISSILERANGE) and (enemy_range<ATTACKMOVERANGE))
      {
         if(attack_delay>time)
         {
            return false;
         }
          attack_state = AS_ATTACK_MOVE;       // attack-move
          return true;
      }
      return false;
   } ]
And finally add one more order:

Code: Select all

   ai_run_attack_move[ ()
   {
      ai_face();
      if(FacingIdeal())             // got close enough
      {
         self.think = ATTACK_MOVE_FUNC;   // start missile attack
         attack_state = AS_STRAIGHT;
         UpdateTarget();
         skill_time = time + (SKILL*0.1);
      }
   } ]
That should do it. I haven't tested the script so it may need some debugging. Just turn the console on (Console(true);) if it doesn't work and see what it has to say.

***EDIT***
Oh ya, I really don't blame you for not understanding it. :shock: I had some troubles too. And that's why I don't like perfectai. There are a lot of easier ways to doing this stuff. At least easier for me. I understand that for some people this may be the best way.

Re: OK,Here's a Hard One!

Posted: Tue Jul 29, 2008 10:38 pm
by metal_head
WoW,I tough that it will require editing only the ai_run routine :shock: that scripting needs logical thinking! I'll test the script and I'll try to fix it if there's a bug! REally big thanks!!!

Re: OK,Here's a Hard One!

Posted: Tue Jul 29, 2008 11:15 pm
by metal_head
OK,really sorry for the double posting,but the console says:

Code: Select all

scripts/agent.s:monster missile run: 33 method ai run missile not found
But I don't understand why...everything in the script seems OK the ai_run_missile is there and I see nothing wrong in it (I know,sounds awesome,but I understood actually almost everything of what you did in perfectai :D)
I tough that I've made a mistake when putting the commands in the scirpt,because I rewrot them,not copied them so that I can hopefuly learn something more and get used to simkin,but I checked twice and don't seem to have difference with your routines and theese I rewrote.

Re: OK,Here's a Hard One!

Posted: Sat Aug 02, 2008 4:32 pm
by Juutis
I tested the script. It worked fine for me. My guess is that you have forgotten to put a } or ] in the end of an order.

Re: OK,Here's a Hard One!

Posted: Sun Aug 03, 2008 2:14 pm
by metal_head
Thanks,Juutis,it was my bad.Really,I don't know how to thank you,man!That's one of the best additions to a existing script I've ever seen!!! I'll make a script with all the things you showed me and upload it!