Nexus Modding > Scripting
The AI and You
The_Small_Time_Modder:
The fourth chunk (hope im not overwhelming you)
--- Code: ---RULE event Tick
Name "latertick"
:action
// basic energy management
Debug("realBattle tick");
AIFleetL3:=GetFreeSel();
SelectShips(AIFleetL3, s.race=#race_raptor);
ExecList(AIFleetL3,
if(s.this:reserveLevel>25, //full power to weapons, hovever we leave reserve for shield augmenting
s.this:tEnergyMode(1, 2);
,
s.this:tEnergyMode(1, 0);
);
if(s.this:shieldLevel<50, //boost shield when low
s.this:tEnergyMode(2, 2);
,
s.this:tEnergyMode(2, 0);
);
);
if(isValid(m.AITeam_leader)&isValid(m.mainTarget)¬(m.mainTarget:wreck)¬(m.AITeam_leader:wreck),
if(range(m.AITeam_leader,m.mainTarget)>3, //main target running away
LocalEvent(GetMainTarget);
);
);
if(isValid(m.secondaryTarget)&isValid(m.mainTarget)¬(m.mainTarget:wreck)¬(m.secondaryTarget:wreck),
if(range(m.secondaryTarget,m.mainTarget)>3, //secondary target running away
LocalEvent(GetSecondaryTarget);
);
);
if(isValid(m.missileTarget)¬(m.missileTarget:wreck),
InBlastRange:=0;
AIFleetL4:=GetFreeSel();
SelectShips(AIFleetL4, s.race=#race_raptor);
ExecList(AIFleetL4,
if(range(m.missileTarget,s.this)=2,
InBlastRange:=1;
);
);
DeleteSelect(AIFleetL4);
if(InBlastRange,LocalEvent(GetSecondaryTarget)); //some of our ships are in blast range - retargeting missiles
);
DeleteSelect(AIFleetL3);
:end
END
Tick 6
RULE event CO_ShipInRange
:action
Debug("In range");
AIFleetL5:=GetFreeSel();
SelectShips(AIFleetL5, s.race=#race_raptor);
ExecList(AIFleetL5,
EnableDodge(s.this, 1);
s.this:tEngineMode(2);
);
DeleteSelect(AIFleetL5);
:end
END
RULE event CO_DeviceDestroyed
condition e.device:owner:race=#race_player&DeviceFocused(e.device,#race_raptor)
:action
LocalEvent(FocusOnDevices);
LocalEvent(GetSecondaryTarget);
:end
END
RULE event ShieldDown
condition e.ship:race=#race_player
:action
LocalEvent(FireMainShield);
:end
END
RULE event ShieldUp
condition E.ship:race=#race_player
:action
LocalEvent(FireMainShield);
:end
END
RULE event FocusOnDevices
:action
PlayerFleetL6:=GetFreeSel();
SelectShips(PlayerFleetL6, s.race=#race_player);
ExecList(PlayerFleetL6,
WGDevL:=GetFreeSel();
SelectDevices(WGDevL, s.this, s.this:damage<100&InSet(S.this, 70)); //focusing on weapon generators
if(isValid(PickFirst(WGDevL)), FocusDevice(PickFirst(WGDevL), #race_raptor, 1));
DeleteSelect(WGDevL);
);
DeleteSelect(PlayerFleetL6);
:end
END
RULE event GetMainTarget
:action
Debug("GetMainTarget");
if(isValid(m.AITeam_leader)¬(m.AITeam_leader:wreck),
PlayerFleetL7:=GetFreeSel();
SelectShips(PlayerFleetL7, s.race=#race_player&range(s.this,m.AITeam_leader)=2&s.this:damage>35); //if there is heavy damaged ship nearby, first finish it off
if(AllNumOf(PlayerFleetL7)>0,
m.mainTarget:=PickMax(PlayerFleetL7,s.this:damage);
,
SelectShips(PlayerFleetL7, s.race=#race_player&range(s.this,m.AITeam_leader)=2); //else we pick most dangerous nearby ship
if(AllNumOf(PlayerFleetL7)>0,
m.mainTarget:=PickMax(PlayerFleetL7, (s.this:tDamage(m.AITeam_leader,2,1)+s.this:tDamage(m.AITeam_leader,2,2)+s.this:tDamage(m.AITeam_leader,2,3)+s.this:tDamage(m.AITeam_leader,3,1)+s.this:tDamage(m.AITeam_leader,3,2)+s.this:tDamage(m.AITeam_leader,3,3)));
,
SelectShips(PlayerFleetL7, s.race=#race_player); //else we pick closest target
if(AllNumOf(PlayerFleetL7)>0, m.mainTarget:=PickMax(PlayerFleetL7, 1 / distance(s.this, m.AITeam_leader)), ChangeState(noTeam0,0));
);
);
ExecList(AIShipSHBusterL,
s.this:tMoveTo(m.mainTarget, 2, 0);
);
LocalEvent(FireMainHull);
LocalEvent(FireMainShield);
DeleteSelect(PlayerFleetL7);
,
LocalEvent(FatalDamaged, E.ship=m.AITeam_leader);
);
:end
END
RULE event GetSecondaryTarget
:action
Debug("GetSecondaryTarget");
if(NumOf(AIShipDeviceBusterL)>0,
if(isValid(m.mainTarget)¬(m.mainTarget:wreck),
PlayerFleetL8:=GetFreeSel();
SelectShips(PlayerFleetL8, s.race=#race_player&(range(s.this,m.mainTarget)=2|range(s.this,m.mainTarget)=3)); // we want to have main target in device buster ships artilery range
if(AllNumOf(PlayerFleetL8)>0, m.secondaryTarget:=PickMax(PlayerFleetL8, (s.this:tDamage(m.AITeam_leader,2,1)+s.this:tDamage(m.AITeam_leader,2,2)+s.this:tDamage(m.AITeam_leader,2,3)+s.this:tDamage(m.AITeam_leader,3,1)+s.this:tDamage(m.AITeam_leader,3,2)+s.this:tDamage(m.AITeam_leader,3,3))), m.secondaryTarget:=m.mainTarget); //most dangerous opponent
DeleteSelect(PlayerFleetL8);
,
LocalEvent(GetMainTarget);
);
ExecList(AIShipDeviceBusterL,
s.this:tMoveTo(m.secondaryTarget, 2, 0);
);
,
m.secondaryTarget:=m.mainTarget;
);
LocalEvent(FireToSecondary);
:end
END
RULE event GetMissileTarget
:action
Debug("GetMissileTarget");
if(isValid(m.AITeam_leader)¬(m.AITeam_leader:wreck),
PlayerFleetL9:=GetFreeSel();
SelectShips(PlayerFleetL9, s.race=#race_player&range(s.this,m.AITeam_leader)>2);
if(AllNumOf(PlayerFleetL9)>0,
AIFleetL9:=GetFreeSel();
SelectShips(AIFleetL9, s.race=#race_raptor&s.this!=m.AITeam_leader); // we don't want to have any AI ships in blast range
if(AllNumOf(AIFleetL9)>0,
ExecList(PlayerFleetL9,
PotentialTarget:=s.this;
ExecList(AIFleetL9,
if(range(PotentialTarget,s.this)=2,
RemoveItem(PlayerFleetL9, PotentialTarget);
);
);
);
);
DeleteSelect(AIFleetL9);
if(NumOf(PlayerFleetL9)>0,
m.missileTarget:=PickMax(PlayerFleetL9, 1 / distance(s.this, m.AITeam_leader));
LocalEvent(FireMissile);
);
);
DeleteSelect(PlayerFleetL9);
,
LocalEvent(FatalDamaged, E.ship=m.AITeam_leader);
);
:end
END
RULE event FireMainHull
:action
Debug("FireMainHull");
if(isValid(m.AITeam_leader)&isValid(m.mainTarget)¬(m.mainTarget:wreck)¬(m.AITeam_leader:wreck),
AIFleetL10:=GetFreeSel();
SelectShips(AIFleetL10, s.race=#race_raptor);
ExecList(AIFleetL10,
s.this:tSetForbid(#FORBID_FIRE);
s.this:tSetForbid(#FORBID_FIRE_BOMBER);
AIFleetWeaponL10:=GetFreeSel();
SelectDevices(AIFleetWeaponL10, s.this, S.working&S.weapon&s.count!=0&s.hullValue=1);
ExecList(AIFleetWeaponL10,
s.this:contFire:=1;
ActivateDevice(S.this, 1);
TargetWeapon(s.this, m.mainTarget);
);
DeleteSelect(AIFleetWeaponL10);
AIFleetBomberL10:=GetFreeSel();
SelectDevices(AIFleetBomberL10, s.this, s.hullValue=1&s.working&(Inset(s.this,20)|Inset(s.this,21)|Inset(s.this,22)));
ExecList(AIFleetBomberL10,WeaponMode(S.this,5,m.mainTarget));
DeleteSelect(AIFleetBomberL10);
);
DeleteSelect(AIFleetL10);
);
:end
END
RULE event FireMainShield
:action
Debug("FireMainShield");
if(isValid(m.AITeam_leader)¬(m.AITeam_leader:wreck),
if(isValid(m.mainTarget)¬(m.mainTarget:wreck),
AIFleetL11:=GetFreeSel();
SelectShips(AIFleetL11, s.race=#race_raptor);
ExecList(AIFleetL11,
AIFleetWeaponL11:=GetFreeSel();
SelectDevices(AIFleetWeaponL11, s.this, S.working&S.weapon&s.count!=0&s.hullValue=0&s.shieldValue=1);
ExecList(AIFleetWeaponL11,
s.this:contFire:=1;
ActivateDevice(S.this, 1);
if(m.mainTarget:isShielded,
TargetWeapon(s.this, m.mainTarget);
,
if(isValid(m.secondaryTarget)¬(m.secondaryTarget:wreck)
if(m.secondaryTarget:isShielded,
TargetWeapon(s.this, m.secondaryTarget);
,
PlayerFleetL11:=GetFreeSel();
SelectShips(PlayerFleetL11, s.race=#race_player&s.this:isShielded);
if(AllNumOf(PlayerFleetL11)>0,
TargetWeapon(s.this,PickMax(PlayerFleetL11, 1 / distance(s.this, m.AITeam_leader)));
);
);
,
LocalEvent(GetSecondaryTarget);
);
);
);
);
DeleteSelect(AIFleetL11);
,
LocalEvent(GetMainTarget);
);
);
:end
END
--- End code ---
The_Small_Time_Modder:
AND FINALLY the fifth chunk... (DANG thats a crap load of coding)
--- Code: ---RULE event FireToSecondary
:action
Debug("FireToSecondary");
if(isValid(m.secondaryTarget)¬(m.secondaryTarget:wreck),
AIFleetL12:=GetFreeSel();
SelectShips(AIFleetL12, s.race=#race_raptor);
ExecList(AIFleetL12,
AIFleetWeaponL12:=GetFreeSel();
SelectDevices(AIFleetWeaponL12, s.this, S.working&S.weapon&s.count!=0&s.hullValue=0&s.shieldValue=0&s.DeviceValue=1);
ExecList(AIFleetWeaponL12,
s.this:contFire:=1;
ActivateDevice(S.this, 1);
TargetWeapon(s.this, m.secondaryTarget);
);
DeleteSelect(AIFleetWeaponL12);
AIFleetBomberL12:=GetFreeSel();
SelectDevices(AIFleetBomberL12, s.this, s.hullValue=0&s.shieldValue=0&s.DeviceValue=1&s.working&(Inset(s.this,20)|Inset(s.this,21)|Inset(s.this,22)));
ExecList(AIFleetBomberL12,WeaponMode(S.this,5,m.secondaryTarget));
DeleteSelect(AIFleetBomberL12);
);
DeleteSelect(AIFleetL12);
);
:end
END
RULE event FireMissile
:action
Debug("FireMissile");
if(IsValid(m.missileTarget)&Not(m.missileTarget:wreck),
AIFleetL13:=GetFreeSel();
SelectShips(AIFleetL13, s.race=#race_raptor);
ExecList(AIFleetL13,
s.this:tSetForbid(#FORBID_FIRE_MISSILE);
AIMissileL13:=GetFreeSel();
SelectDevices(AIMissileL13, s.this, s.weapon & s.working & InSet(s.this, 5) & s.count!=0); //missiles & e-bombs
ExecList(AIMissileL13,
s.this:contFire:=1;
ActivateDevice(S.this, 1);
TargetWeapon(S.this, m.missileTarget);
);
DeleteSelect(AIMissileL13);
);
DeleteSelect(AIFleetL13);
,
LocalEvent(FatalDamaged,E.ship=m.missileTarget);
);
:end
END
RULE event FatalDamaged
:action
Debug("realBattleFatalDamaged");
if(e.ship:Race=#race_player,
PlayerFleetL14:=GetFreeSel();
SelectShips(PlayerFleetL14, s.race=#race_player);
if(NumOf(PlayerFleetL14)>0,
if(e.ship=m.mainTarget,
LocalEvent(GetMainTarget);
);
if(e.ship=m.secondaryTarget,
LocalEvent(GetSecondaryTarget);
);
if(e.ship=m.misileTarget,
LocalEvent(GetMissileTarget);
);
,
ChangeState(noTeam0,0);
);
DeleteSelect(PlayerFleetL14);
,
RemoveItem(AIShipDeviceBusterL,e.ship);
RemoveItem(AIShipSHBusterL,e.ship);
if(e.ship=m.AITeam_leader,
AIFleetL14:=GetFreeSel();
SelectShips(AIFleetL14, s.race=#race_raptor);
if(AllNumOf(AIFleetL14)>0, m.AITeam_leader:=PickMax(AIFleetL14, 1 / s.this:tGetMaxVelocity()), ChangeState(noTeam1,0));
DeleteSelect(AIFleetL14);
Delay(0,1,LocalEvent(GetMainTarget),0);
Delay(0,3,LocalEvent(GetSecondaryTarget),0);
Delay(0,5,LocalEvent(GetMissileTarget),0);
);
);
:end
END
END //STATE realBattle
STATE noTeam1
RULE event In
:action
:end
END
END //STATE noTeam1
STATE noTeam0
RULE event In
:action
:end
END
END //STATE noTeam1
END //MACHINE "AI"
--- End code ---
Mularac:
Yeah... I found what the error was. The file you put here:
MACHINE "INNER_AI"
#include "11_default_ai.mach"
END
is not the one I meant by when I gave you the first example, but this file: http://arparso.de/nexus/forum/index.php/topic,222.msg1569.html#msg1569
The_Small_Time_Modder:
Aaaah... ok, glad you were able to figure it out!
I should applaud you for that, in fact i will! :D
i might post a few more questions / problems i run into in the future here, im sure everyone will benefit from it.
Edit: THANK GOD! it works now! :D
Btw, going clear back to the first page of this topic...
About setting up an area that the AI will kick in if you have a ship go into it... can you make it visible in the editor? i want to make sure i have it in the right place... :-\
OH yeah, and another thing, can you code for a ship appear to be damaged when the mission starts? and where would you put that piece of coding?
Mularac:
area that appears on the editor: nope. You can, however, put a ship in those coordinates and if the radius of the ship is of a similar length to the ARTILLERY and COMBAT ones, then you can use the display options in the mission editor.
Ship appearing damaged: easy as pie. just meddle with this atribute: m.ship:damage := 0 to 100; where 0 means full health and 100 = death. I recommend applying this change just before making that ship appear, to keep the code clear.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version