Nexus Modding > Scripting
The AI and You
The_Small_Time_Modder:
--- Quote ---wait.... in the file named 11_default_ai.mach there is a line including the file 11_default_ai.mach???
--- End quote ---
Do you mean the MAIN_AI for the second file? or the INNER_AI? that sounds so weird that you have to script to include the 11_default_ai.mach in the 11_default_ai.mach... ???
Mularac:
No, you don't. That will certainly crash the game.
Please post here the contents of the file 11_default_ai.mach, the one you included in the Machine "INNER_AI"
The_Small_Time_Modder:
Ok, so to sum it up, you basically include the AI that the skirmisher generated in the INNER_AI. Right?
anyway, i can't cram it in all at once, so ill give it to you in chunks, heres the first chunk.
--- Code: ---MACHINE "AI"
STATE battle
RULE event In
:action
AIFleetL1:=GetFreeSel();
SelectShips(AIFleetL1, s.race=#race_raptor);
if(AllNumOf(AIFleetL1)>0, m.AITeam_leader:=PickMax(AIFleetL1, 1 / s.this:tGetMaxVelocity()), ChangeState(noTeam1,0)); //slowest ship is AI leader
//for cloaking ships, we will have to change code below
ExecList(AIFleetL1,
MakeFullyKnown(s.this);
FreezeReconState(s.this, #race_player, 1);
);
PlayerFleetL1:=GetFreeSel();
SelectShips(PlayerFleetL1, s.race=#race_player);
ExecList(PlayerFleetL1,
MakeFullyKnown(s.this);
FreezeReconState(s.this, #race_raptor, 1);
);
m.PShipsCount:= AllNumOf(PlayerFleetL1);
//time to turn on defence systems in order of importance
ExecList(AIFleetL1,
EnergyLeft:=s.this:availSuppForDevices;
TempEcmL := GetFreeSel();
TempEccmL := GetFreeSel();
TempCloakL := GetFreeSel();
TempSensorL := GetFreeSel();
If(SelectDevices(TempEcmL, s.this, s.working & InSet(s.this, 16)), //ecm
Ecm:=PickFirst(TempEcmL);
If(EnergyLeft>=Ecm:energyIn,
ActivateDevice(Ecm, 1);
EnergyLeft:=EnergyLeft-Ecm:energyIn;
);
);
If(SelectDevices(TempEccmL, s.this, s.working & InSet(s.this, 17)), //eccm
Eccm:=PickFirst(TempEccmL);
If(EnergyLeft>=Eccm:energyIn,
ActivateDevice(Eccm, 1);
EnergyLeft:=EnergyLeft-Eccm:energyIn;
);
);
If(SelectDevices(TempCloakL, s.this, s.working & InSet(s.this, 9)), //cloaking also give some protection
Cloak:=PickFirst(TempCloakL);
If(EnergyLeft>=Cloak:energyIn,
ActivateDevice(Cloak, 1);
EnergyLeft:=EnergyLeft-Cloak:energyIn;
);
);
//If(SelectDevices(TempSensorL, s.this, s.working & InSet(s.this, 18)), //sensor, need for cloakers
// Sensor:=PickFirst(TempSensorL);
// If(EnergyLeft>=Sensor:energyIn,
// ActivateDevice(Sensor, 1);
// EnergyLeft:=EnergyLeft-Sensor:energyIn;
// );
//);
DeleteSelect(TempEcmL);
DeleteSelect(TempEccmL);
DeleteSelect(TempCloakL);
DeleteSelect(TempSensorL);
);
m.AITeam_leader:tRequestShipInRangeEvent(3,3); //to know when we are close enough to call realBattle state
DeleteSelect(AIFleetL1);
DeleteSelect(PlayerFleetL1);
Delay(0,2,LocalEvent(GetClosestTarget),0);
:end
END
--- End code ---
The_Small_Time_Modder:
And heres the second chunk....
--- Code: ---RULE event GetClosestTarget
condition isValid(m.AITeam_leader)¬(m.AITeam_leader:wreck)
:action
Debug("GetClosestTarget");
PlayerFleetL2:=GetFreeSel();
SelectShips(PlayerFleetL2, s.race=#race_player);
if(AllNumOf(PlayerFleetL2)>0, TempMainTarget:=PickMax(PlayerFleetL2, 1 / distance(s.this, m.AITeam_leader)), ChangeState(noTeam0,0));
if(m.mainTarget!=TempMainTarget, // to avoid cpu overhead becouse of tick
Debug("New target");
m.mainTarget:=TempMainTarget;
LocalEvent(EvaluateAIFleet);
LocalEvent(FireMissile);
LocalEvent(MoveInFormation);
);
DeleteSelect(PlayerFleetL2);
:end
END
RULE event EvaluateAIFleet //evaluate AI fleet missile capabilities
name "evaluateAI"
:action
Debug("EvaluateAIFleet");
Disable("evaluateAI"); //We call it only once
AIFleetL3:=GetFreeSel();
SelectShips(AIFleetL3, s.race=#race_raptor);
MissCap:=0;
ExecList(AIFleetL3,
AIMissileDL:=GetFreeSel();
SelectDevices(AIMissileDL, s.this, s.weapon & s.working & InSet(s.this, 5) & s.count>1);
MissCap := MissCap + AllNumOf(AIMissileDL);
DeleteSelect(AIMissileDL);
);
if((MissCap/(m.PShipsCount+1))>0.4,
LocalEvent(SendAIBombers,e.cover:=1); //with enough missiles, we send in fighters early to saturate enemy flak, leaving only 1 squad for anti-missile defence
,
LocalEvent(SendAIBombers,e.cover:=10); //else we leave all fighters on defence
);
DeleteSelect(AIFleetL3);
:end
END
RULE event MoveInFormation
:action
if(IsValid(m.AITeam_leader)&Not(m.AITeam_leader:wreck)&IsValid(m.mainTarget)&Not(m.mainTarget:wreck),
AIFleetL4 := GetFreeSel();
SelectShips(AIFleetL4, s.race=#race_raptor);
//RemoveItem(AIFleetL4, m.AITeam_leader); //formation works worse then simple move
//if(NumOf(AIFleetL4)>0,
// m.AITeam_leader:tSetFormation(2, 1);
// ExecList(AIFleetL4,
// S.this:tSetForbid(#FORBID_MOTION_MOTIVATION_AWAY);
// S.this:tFollow(m.AITeam_leader, 2000, 10);
// );
//);
ExecList(AIFleetL4,
s.this:tSetForbid(#FORBID_MOTION_MOTIVATION_AWAY);
SetAbsSpeed(s.this, m.AITeam_leader:tGetMaxVelocity());
s.this:tMoveTo(m.mainTarget, 2, 0);
);
DeleteSelect(AIFleetL4);
,
LocalEvent(FatalDamaged);
);
:end
END
RULE event FireMissile
:action
Debug("FireMissile");
if(isValid(m.AITeam_leader)&Not(m.AITeam_leader:wreck)&IsValid(m.mainTarget)&Not(m.mainTarget:wreck),
AIFleetL5:=GetFreeSel();
SelectShips(AIFleetL5, s.race=#race_raptor);
ExecList(AIFleetL5,
s.this:tClearFire;
s.this:tSetForbid(#FORBID_FIRE_MISSILE); //we don't want AI to mess with it
AIMissileL:=GetFreeSel();
SelectDevices(AIMissileL, s.this, s.weapon & s.working & InSet(s.this, 5) & s.count!=0); //missiles & e-bombs
ExecList(AIMissileL,
s.this:contFire:=1; //burst missile fire
ActivateDevice(S.this, 1);
TargetWeapon(S.this, m.mainTarget);
);
DeleteSelect(AIMissileL);
);
DeleteSelect(AIFleetL5);
,
LocalEvent(FatalDamaged);
);
:end
END
--- End code ---
The_Small_Time_Modder:
The third chunk....
--- Code: ---RULE event SendAIBombers
:action
Debug("Send Bombers");
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);
AIFleetL6:=GetFreeSel();
SelectShips(AIFleetL6, s.race=#race_raptor);
ExecList(AIFleetL6,
s.this:tSetForbid(#FORBID_FIRE_BOMBER); //we don't want AI to mess with it as well
AIBombersL:=GetFreeSel();
HostShip:=s.this;
NumofCover:=0;
SelectDevices(AIBombersL, s.this, s.weapon&s.working&s.count!=0&InSet(s.this,22)); //fighters by SET
ExecList(AIBombersL,
ActivateDevice(s.this, 1);
if(NumofCover<e.cover,
NumofCover:=NumofCover+1;
WeaponMode(s.this,3,HostShip);
,
WeaponMode(S.this,5,m.mainTarget);
);
);
SelectDevices(AIBombersL, s.this, s.weapon&s.working&s.count!=0&InSet(s.this,20)); //then gunboats by SET
ExecList(AIBombersL,
ActivateDevice(s.this, 1);
if(NumofCover<e.cover,
NumofCover:=NumofCover+1;
WeaponMode(s.this,3,HostShip);
,
WeaponMode(S.this,5,m.mainTarget);
);
);
SelectDevices(AIBombersL, s.this, s.weapon&s.working&s.count!=0&InSet(s.this,21)); //then bombers by SET
ExecList(AIBombersL,
ActivateDevice(s.this, 1);
if(NumofCover<e.cover,
NumofCover:=NumofCover+1;
WeaponMode(s.this,3,HostShip);
,
WeaponMode(S.this,5,m.mainTarget);
);
);
);
DeleteSelect(AIFleetL6);
:end
END
RULE event Tick
:action
// basic energy management
Debug("tick battle");
AIFleetL7:=GetFreeSel();
SelectShips(AIFleetL7, s.race=#race_raptor);
ExecList(AIFleetL7,
if(s.this:reserveLevel>25, //full power to weapons, however we leave reserve for shield augmenting
s.this:tEnergyMode(1, 2);
,
s.this:tEnergyMode(1, 0);
);
);
DeleteSelect(AIFleetL7);
LocalEvent(GetClosestTarget); //during battle we constantly update closest target
:end
END
Tick 6
RULE event CO_ShipInRange
:action
Debug("In range");
ChangeState(realBattle,0) //time to close in action
:end
END
RULE event FatalDamaged
:action
Debug("battleFatalDamaged");
if(e.ship:Race=#race_player,
m.PShipsCount:=m.PShipsCount-1;
if(e.ship=m.mainTarget,
LocalEvent(GetClosestTarget);
);
,
if(e.ship=m.AITeam_leader,
AIFleetL8:=GetFreeSel();
SelectShips(AIFleetL8, s.race=#race_raptor);
if(AllNumOf(AIFleetL8)>0, m.AITeam_leader:=PickMax(AIFleetL8, 1 / s.this:tGetMaxVelocity()), ChangeState(noTeam1,0));
DeleteSelect(AIFleetL8);
//LocalEvent(MoveInFormation);
LocalEvent(GetClosestTarget);
);
);
:end
END
RULE event out //disband formation
:action
if(isValid(m.AITeam_leader)&Not(m.AITeam_leader:wreck)&IsValid(m.mainTarget)&Not(m.mainTarget:wreck),
AIFleetL9 := GetFreeSel();
SelectShips(AIFleetL9, s.race=#race_raptor);
ExecList(AIFleetL9,
SetAbsSpeed(s.this, s.this:tGetMaxVelocity());
s.this:tMoveTo(m.mainTarget, 2, 0);
);
DeleteSelect(AIFleetL9);
);
:end
END
END //STATE battle
STATE realBattle
RULE event In
:action
Disable("latertick");
//just precaution if something changed during switching states
if(Not(isValid(m.AITeam_leader))|m.AITeam_leader:wreck,
AIFleetL1:=GetFreeSel();
SelectShips(AIFleetL1, s.race=#race_raptor);
if(AllNumOf(AIFleetL1)>0, m.AITeam_leader:=PickMax(AIFleetL1, 1 / s.this:tGetMaxVelocity()), ChangeState(noTeam1,0)); //slowest ship is AI leader
DeleteSelect(AIFleetL1);
);
if(not(IsValid(m.mainTarget))|m.mainTarget:wreck,
PlayerFleetL1:=GetFreeSel();
SelectShips(PlayerFleetL1, s.race=#race_player);
if(AllNumOf(PlayerFleetL1)>0, m.mainTarget:=PickMax(PlayerFleetL1, 1 / distance(s.this, m.AITeam_leader)), ChangeState(noTeam0,0));
DeleteSelect(PlayerFleetL1);
);
m.AITeam_leader:tRequestShipInRangeEvent(2,3); //to know when we are close enough to enable deflection and combat engines
Delay(0,1,LocalEvent(EvaluateAIFleet),0);
Delay(0,2,LocalEvent(GetMainTarget),0);
Delay(0,4,LocalEvent(GetSecondaryTarget),0);
Delay(0,6,LocalEvent(GetMissileTarget),0);
Delay(0,8,Enable("latertick"),0);
:end
END
RULE event EvaluateAIFleet
:action
AIFleetL2:=GetFreeSel();
SelectShips(AIFleetL2, s.race=#race_raptor);
AIShipSHBusterL:=GetFreeSel(); //shield and hull buster ships
AIShipDeviceBusterL:=GetFreeSel(); //device buster ships
ExecList(AIFleetL2,
AIDevBustDL:=GetFreeSel();
AISHBustDL:=GetFreeSel();
SelectDevices(AIDevBustDL, s.this, s.weapon & s.working & s.deviceValue=1 & s.count!=0);
SelectDevices(AISHBustDL, s.this, s.weapon & s.working & (s.hullValue=1 | s.shieldValue=1) & s.count!=0);
SubList(AIDevBustDL, AISHBustDL); // remove multipurpose weapons
if(NumOf(AIDevBustDL)>NumOf(AISHBustDL),
AddItem(AIShipDeviceBusterL, s.this); // ships with primary device busting capablities
,
AddItem(AIShipSHBusterL, s.this); // ships with primary hull and shield busting capablities
);
//DeleteSelect(AIMissileDL);
DeleteSelect(AIDevBustDL);
DeleteSelect(AISHBustDL);
);
DeleteSelect(AIFleetL2);
LocalEvent(FocusOnDevices);
:end
END
--- End code ---
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version