| Home » Renegade Discussions » Mod Forum » Turret and GT not shooting Goto Forum:
	| 
		
			| Turret and GT not shooting [message #469012] | Sun, 10 June 2012 19:58  |  
			| 
				
				
					|  robbyke Messages: 348
 Registered: September 2010
 Location: Belgium
 
	Karma: 
 | Recruit |  |  |  
	| the turret and GT models wont fire 
 theyll only fire when someone is nearly in it
 
 i thought it was my scripts first but then every defence should have the problem or am i wrong?
 
 header file
class KB_Base_Defence : public ScriptImpClass {
	unsigned int id1;
	unsigned int id2;
	unsigned int id3;
	unsigned int objtype;
	int CurrentTarget;
	bool Firing;
	void Created(GameObject *obj);
	void Enemy_Seen(GameObject *obj,GameObject *enemy);
	bool IsValidEnemy(GameObject* WeaponObj, GameObject* EnemyObj);
	void Action_Complete(GameObject *obj,int action_id,ActionCompleteReason complete_reason);
	void Timer_Expired(GameObject *obj,int number);
	public: void Register_Auto_Save_Variables();
};
 
 
 cpp file
void KB_Base_Defence::Created(GameObject *obj)
{
	Vector3 pos,pos1,pos2,pos3;
	GameObject *object;
	objtype = Commands->Get_Player_Type(obj);
	Commands->Enable_Hibernation(obj,false);
	Commands->Innate_Enable(obj);
	Commands->Enable_Enemy_Seen(obj,true);
	pos = Commands->Get_Position(obj);
	CurrentTarget = NULL;
	pos1.X = pos.X - 10;
	pos1.Y = pos.Y - 10;
	pos1.Z = pos.Z + 2;
	pos2.X = pos.X + 10;
	pos2.Y = pos.Y;
	pos2.Z = pos.Z + 2;
	pos3.X = pos.X + 10;
	pos3.Y = pos.Y - 10;
	pos3.Z = pos.Z + 2;
	Firing = false;
	object = Commands->Create_Object("Invisible_Object",pos1);
	if (object)
	{
		id1 = Commands->Get_ID(object);
	}
	object = Commands->Create_Object("Invisible_Object",pos2);
	if (object)
	{
		id2 = Commands->Get_ID(object);
	}
	object = Commands->Create_Object("Invisible_Object",pos3);
	if (object)
	{
		id3 = Commands->Get_ID(object);
	}
	Commands->Start_Timer(obj,this,10,1);
}
void KB_Base_Defence::Enemy_Seen(GameObject *obj,GameObject *enemy)
{
	GameObject *o = Get_Vehicle(enemy);
	if (o)
	{
		enemy = o;
	}
	ActionParamsStruct params;
	Vector3 pos,pos2;
	float maxattack;
	float attacktimer;
	pos = Commands->Get_Position(obj);
	pos2 = Commands->Get_Position(enemy);
	
	if (IsValidEnemy(obj,enemy) && !Firing)
	{
		maxattack = Get_Float_Parameter("MaxAttackDistance");
		params.Set_Basic(this,100,2);
		if(Get_Int_Parameter("AdjustAim") != 0 && enemy->As_SoldierGameObj())
		{
			params.Set_Attack(Commands->Get_Position(enemy),maxattack,0.0,true);		
		}
		else
		{
			params.Set_Attack(enemy,maxattack,0.0,true);
		}
		params.AttackCheckBlocked = false;
		params.AttackForceFire = true;
		Commands->Action_Attack(obj,params);
		CurrentTarget = Commands->Get_ID(enemy);
		attacktimer = Get_Float_Parameter("AttackTimer");
		Commands->Start_Timer(obj,this,attacktimer,2);
	}
}
bool KB_Base_Defence::IsValidEnemy(GameObject* WeaponObj, GameObject* EnemyObj) {
	if (!EnemyObj) return false;
	if (Commands->Get_Player_Type(EnemyObj) == Commands->Get_Player_Type(WeaponObj)) return false;
	if (Commands->Get_Health(EnemyObj) <= 0) return false;
	if (!Commands->Is_Object_Visible(WeaponObj, EnemyObj)) return false;
	if (Is_Script_Attached(EnemyObj,"KB_Friendly_Zone_Generated")) return false;
	if (EnemyObj->As_VehicleGameObj() && Get_Vehicle_Driver(EnemyObj))
	{
		if (Is_Script_Attached(Get_Vehicle_Driver(EnemyObj),"KB_Friendly_Zone_Generated")) return false;	
	}
	float minattack = Get_Float_Parameter("MinAttackDistance");
	float maxattack = Get_Float_Parameter("MaxAttackDistance");
	Vector3 WeaponObjPos = Commands->Get_Position(WeaponObj);
	Vector3 WeaponObjPosXY = WeaponObjPos;
	WeaponObjPosXY.Z = 0;
	Vector3 EnemyObjPos = Commands->Get_Position(EnemyObj);
	Vector3 EnemyObjPosXY = EnemyObjPos;
	EnemyObjPosXY.Z = 0;
	float DistanceXY = Commands->Get_Distance(WeaponObjPosXY, EnemyObjPosXY);
	
	return DistanceXY > minattack && DistanceXY < maxattack;
}
void KB_Base_Defence::Action_Complete(GameObject *obj,int action_id,ActionCompleteReason complete_reason)
{
	if (action_id == 2)
	{
		Commands->Action_Reset(obj,100);
	}
}
void KB_Base_Defence::Timer_Expired(GameObject *obj,int number)
{
	ActionParamsStruct var;
	GameObject *object;
	float f;
	if(number == 1 && !Firing)
	{
		f = Commands->Get_Random(0.0f,2.9999f);
		switch (__min(int(f),2))
		{
		case 0:
			object = Commands->Find_Object(id1);
			if (object)
			{
				var.Set_Basic(this,70,1);
				var.Set_Attack(object,0.0,0.0,true);
				Commands->Action_Attack(obj,var);
			}
			break;
		case 1:
			object = Commands->Find_Object(id2);
			if (object)
			{
				var.Set_Basic(this,70,1);
				var.Set_Attack(object,0.0,0.0,true);
				Commands->Action_Attack(obj,var);
			}
			break;
		default:
			object = Commands->Find_Object(id3);
			if (object)
			{
				var.Set_Basic(this,70,1);
				var.Set_Attack(object,0.0,0.0,true);
				Commands->Action_Attack(obj,var);
			}
		}
		Commands->Start_Timer(obj,this,10,1);
	}
	if(number == 2)
	{
		if(!IsValidEnemy(obj,Commands->Find_Object(CurrentTarget)))
		{
			Commands->Action_Reset(obj,100);
			Firing = false;
		}
	}
}
void KB_Base_Defence::Register_Auto_Save_Variables()
{
	Auto_Save_Variable(&id1,4,1);
	Auto_Save_Variable(&id2,4,2);
	Auto_Save_Variable(&id3,4,3);
	Auto_Save_Variable(&objtype,4,4);
}
ScriptRegistrant<KB_Base_Defence> KB_Base_Defence_Registrant("KB_Base_Defence","MinAttackDistance=0.0:float,MaxAttackDistance=300.0:float,AttackTimer=10.0:float,AdjustAim=0:int");
 
 
 this is my defence AI
 it targets both vtol and ground
 and is power independant
 
 ive made it using several ai scripts from TT
 
 edit : fixed the spoiler things
   
 Owner of kambot TT server
 
 kambot.freeforums.org
 [Updated on: Wed, 13 June 2012 15:04] Report message to a moderator |  
	|  |  | 
 
 Current Time: Thu Oct 30 20:59:58 MST 2025 
 Total time taken to generate the page: 0.00751 seconds |