[Mod/AIScript] Order "Protect Sector"

The place to discuss scripting and game modifications for X4: Foundations.

Moderators: Scripting / Modding Moderators, Moderators for English X Forum

User avatar
ChemODun
Posts: 405
Joined: Mon, 12. Feb 07, 21:58
x4

[Mod/AIScript] Order "Protect Sector"

Post by ChemODun »

Protect Sector

Image

This extension allows you to protect a sector from being harassed by hostile ships.
This is useful when you want to prevent hostile ships to attack your ships and stations. Or when you want to prevent from building in a sector.

Compatibility

Compatible with X4: Foundations 7.10 and upper. At least it written and tested with versions 7.10 and 7.50.

Features
  • Always the closest possible target will be selected to attack.
  • Several ships/fleets with one order can work in one sector or crossed sectors.
  • Not recommended to use single ship - the fleet is always better.
  • Mimic order in a fleet is fully supported. But, again, not set single ship to mimic mode - use the fleet instead.
  • The Lost Ship Replacement feature of version 7.50 is fully supported.
  • The own (experimental) order for station attacks is available.
  • The workaround for player ships repairing/restocking not only in current sector is implemented.
  • From version 1.08 have own icons for the Protect Sector and Station attack orders.
Download

You can download the latest version via Steam client - Protect Sector
Or you can do it via the Nexus Mods

Executing the order

You can select the order as any other, like "Protect Station" or "Protect Ship", from the "Combat" section of orders.
Please be aware - this order requires the ship captain to have at least two stars in the "Pilot" skill.

Configuration

There are a several configuration options available. You can see all of them on a screenshot.
Spoiler
Show
Image
Home Sector

This is a sector which will be protected. You can select it from the list of discovered sectors.
If you select not current sector, the ship will fly to the selected sector to some "safe" point before to start protecting it.

It can be any known sector, even if it is not yours.

Attack Stations

Disabled by default.

If enabled, hostile stations in the protected sector will be attacked by ship and fleet.

Use "Coordinate attack"

Enabled by default.

If enabled, the ship will use the "Coordinate Attack" order to attack the stations. It will be used for the stations only, not for the ships.
If disabled, the ship will use the usual "Attack" order to attack the stations.

Use experimental "Attack Station"

Disabled by default.
If enabled, the ship will use the experimental "Attack Station" order to attack the stations. It will be used for the stations only, not for the ships.

Will work only if the ships in a fleet contains only L or XL classes only. Otherwise it will be de-selected together with "Attack stations" checkbox.

For L only - four and more ships are recommended.

Important notice

If Use experimental "Attack Station" selected, the ship will use the experimental order independently of the order type selected in the previous parameter.

Common warning for station attacks

In non-OOS mode, i.e. when the Player is in the same sector, the stations drones will attack the ships. The appropriate logic to react on this event is implemented in experimental order, and working not bad :-).
But still not recommended to be in the same sector with the ship, independently of the order type selected.

Attack Ships

You can select exact ship types:
  • XL
  • L
  • M
  • S
By default are set depending on the player ship type.

If it will find a hostile squad - it will not be attacked, if it contains at least one ship of the type higher than highest selected one.

Attack farther from stations

There is a slider to define a minimal distance from the possible target to the hostile station. If the distance is less than the defined value, the ship will be selected as a target.
If value is 0 - the distance will not be checked.

Default value is 0.

Attack only visible targets

Enabled by default.

Used to prevent the ship from attacking the target, which is not revealed yet or not visible for the player.

It has slightly different behavior for stations and ships:
  • For stations - the station should be visible on a map. So - it has to be revealed, but it is not required to be in a live view, i.e. in radar range of your ships, stations, satellites, etc.
  • For ships - the ship should be visible online. So - it has to be in a radar range of your ships, stations, satellites, etc.
Attack only hostile targets

Enabled by default.

If enabled, the ship will attack only hostile targets. It will use the appropriate command to filter the targets.

By negative relation

This is a slider to define the relation to the Player. Take in account - relation is shown as positive value, due to limitation of the game engine.
So, if you want to attack the ships with relation -10 and lower - you have to set the value to 10.
Default value is 25, i.e. attack the ships with relation -25 and lower.

Please take in account - when previous parameter is enabled, you can set this one in between 25 and 30 (-25 and -30 relation).
If the Attack only hostile targets is disabled - the value can be set in between 0 and 30 (0 and -30 relation).
Use it carefully.

Protect our ships and stations in sector

Enabled by default.

If enabled, the ship will react on the event when the player's ships or stations are attacked in the sector. It will try to protect them by attacking the hostile ships.

- except military ships

Enabled by default.

If enabled, the ship will not protect the military ships. It will not attack the hostile ships, which are attacking the military ships.

Pursue fleeing targets

Disabled by default.

If enabled, the ship will pursue the target, which is trying to flee after is being attacked.

Share target with other

Enabled by default.

If you have more than one ship or squad with the same order, you can disable this option to prevent them from attacking the same target.

Attack distance: percentage of radar range

This setting allows you to define the ship behavior when the target is identified and selected.
The attack de facto contains two stages:
  • The ship is trying to reach the target till some acceptable distance. This distance is defined as a percentage of the radar range.
  • The ship is trying to attack the target when it is in the acceptable distance.
Avoid boosters usage

Disabled by default.

If enabled, the ship will not use the boosters to increase the speed. In game version prior 7.50 it will help to preserve the shield energy. For version 7.50 and higher, boosters are used always, despite the Avoid boosters usage option.

Disable on destruction threat

It is a percentage of hull of the target to make this order disabled. Useful when you want to achieve more abandoned ships.

By default, it is 0 percent - i.e. disabled this check.

If set to non-zero value, the ship will stop attacking when hull is less than desired percent.

Park at sector core

Disabled by default.

If enabled, the ship will try to park at the sector core, when it is not targets to fight against.

Park exactly there

It's optional parameter, it means - it can be skipped to set.
Moreover, if Park at sector core is enabled - this parameter will not only be ignored, its value will be cleared. So, if you want to park exactly there - disable the Park at sector core option.
To set the value of this parameter - you have to select the exact position on the galaxy map inside the appropriate sector.

Important Notice

If the sector, selected in this parameter, is not the same as the Home Sector - the Home Sector will be changed to the selected one.

Delay between scans, seconds

Default value is 4 seconds.
If you want - you can set it in between 1 and 90 seconds, with step 4 seconds. Bigger value will make less load on the CPU...

Ignore the threats of Hazardous zones

Disabled by default.

Please take in account, some sectors are not safe, even if the ship is not attacked. For example, the sectors with the hazardous zones, like The Void. If ship is in the hazardous zone it's shield and then hull will be continuously damaged.
From version 1.08 Protect Sector order will automatically finished in such sectors. Appropriate message will be shown in the logbook.

If you still want to use this order in the hazardous zone - you can enable this option. But please be aware - the ships can be destroyed in the hazardous zone, as there is no good solutions to avoid such zones.

Record to logbook

Enabled by default.

If enabled, the ship will record the events to the logbook. I.e. starts, travel to desired sector, flying to the target, attacking the target, destroying the target, etc.

Situation when nothing to attack

If no Attack target is selected (i.e. no station and no any ship types are selected) - the ship's captain will periodically call to the player to inform about absence of the order.

Special Thanks

Special thanks to the pilot Assailer for his awesome script "Sector Patrol" which was used as a base for this one.

Changelog

[1.09] - 2025-05-18

Fixed
  • Mimic mode not started correctly.
Changed
  • If experimental order of Attack Station is selected but fleet has ships smaller than L- the both experimental and Attack Station options are disabled.
[1.08] - 2025-05-11

Added
  • Internal (experimental) station attack order. Written from scratch positioning ships against stations, including reaction on stations drones attacks in non-OOS mode. Works only for fleets with L and XL ships. For only L ships - four and more ships are recommended.
  • Possibility to select station attack order - by default it is Coordinate attack. Other possibilities are usual one and experimental one.
  • Additional confirmation for the working in hazardous regions, like The Void. Please be aware - the order will stop working in the sectors with the hazardous regions without the confirmation.
  • Own icons for the Protect Sector and Station attack orders.
Improved
  • Fully rewritten the logic of idling and scanning for enemy ships. Now it is more precise.
  • Implemented workaround for the repairing/restocking ships. By default Player owned ships can be repaired only in current sector. Please be aware - after installing version 1.08 the ships controlled by this order can go to be repaired/restocked after some time. It is not a bug, but a feature :-).
Fixed
  • Fixed a reaction on attack on player's ships/stations by enemy drones.
  • Accidentally attacks from the non-enemies are not considered as an attack on the player ships/stations.
[1.07] - 2025-03-16

Added
  • By negative relation to set the relation to possible targets more precisely.
  • Park exactly there to set the exact position where the ship should park when it is not targets to fight against. Useful for big sectors.
Improved
  • Idle flying when any parking is not set.
  • Attack of the stations via Coordinate attack in now visible, as it is run not as a script, but as an order.
[1.06] - 2025-03-01

Fixed
  • The Lost Ship Replacement not always works correctly.
[1.05] - 2025-03-01

Added
  • Support for the Lost Ship Replacement feature of version 7.50
  • Added additional parameter to set a threshold from moving to target to attack it. In percentage of the radar range.
Fixed
  • For version 7.50 and higher, boosters are used always, despite the Avoid boosters usage option.
  • The default value for Disable on destruction threat is 0 percent - i.e. disabled this check.
[1.04] - 2024-12-31

Fixed
  • Slow floating to sector center when the "Park at sector core" is not enabled.
[1.03] - 2024-12-26

Added
  • Added possibility to use the Protect Sector order in the fleet mode with the Mimic order.
Improved
  • Improved the target separation in case of "sharing" is disabled - variable wait is solve the problem with the same target selection.
[1.02] - 2024-12-20

Added
  • Added parameter to set exact delay between scans.
Fixed
  • Fixed a bug with approaching to the target - if it has equal speeds the catching is never happened.
  • Added additional small delay in the approaching routine to prevent freezing
Improved
  • Improved the experience gaining
[1.01] - 2024-12-13

Added
  • Added option to protect ships and stations in the desired sector via reaction on attacks on them by hostile ships.
  • Added option to record events to the logbook, including starts, travel to desired sector, flying to the target, attacking the target, destroying the target, etc.
Changed
  • Between scans it uses move.idle script.
  • Changed the type of option to disable on destruction threat: from boolean to the percentage of hull to make this order disabled.
Improved
  • Approaching to the target is rewritten.
  • Attack of the ship is now under control of the order.fight.attack.object script.
[1.00] - 2024-12-06

Added
  • Initial release of the Protect Sector extension.
  • Allows protection of a sector from hostile ships.
  • Configuration options for home sector, attack stations, attack ships, attack farther from stations, attack only visible targets, attack only hostile targets, pursue fleeing targets, share target with other, preserve shield energy, and disable on destruction threat.
  • Compatibility with X4: Foundations 7.1.
Last edited by ChemODun on Sun, 18. May 25, 12:58, edited 11 times in total.
Multiply entropy by absolute zero

Freedom in space
User avatar
Baconnaise
Posts: 764
Joined: Sat, 23. Nov 13, 15:50
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by Baconnaise »

Nice mod mate. I used to use sector patrol a bunch or reaction force. Good stuff.
hasler74
Posts: 4
Joined: Sun, 24. Nov 24, 18:16
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by hasler74 »

You sir are my hero.

Though one question. On attack farther from stations, I assume in you mean more than the value to target and not less. Since less would mean attacking closer to the enemy station. That is unless I am misunderstanding that command.
hasler74
Posts: 4
Joined: Sun, 24. Nov 24, 18:16
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by hasler74 »

I have been testing it out. Mimic does not work, maybe subordinate skill issue?

The other thing is attack only hostile won’t engage khaak. When I uncheck that the squadron responds to their attacks.
User avatar
ChemODun
Posts: 405
Joined: Mon, 12. Feb 07, 21:58
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by ChemODun »

Thanks for everyone for the response and supportive reaction.
hasler74 wrote: Tue, 10. Dec 24, 16:13 I have been testing it out. Mimic does not work, maybe subordinate skill issue?
Never used, will try to check.
hasler74 wrote: Tue, 10. Dec 24, 16:13 The other thing is attack only hostile won’t engage khaak. When I uncheck that the squadron responds to their attacks.
Very strange for me - my "protectors' killed a lot of khaaks ... May be you have some rules which modified relations?

P.S. Excuse me for silence, notification on mail wasn't set before
Multiply entropy by absolute zero

Freedom in space
User avatar
ChemODun
Posts: 405
Joined: Mon, 12. Feb 07, 21:58
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by ChemODun »

By the way, I'm new for X4 and its scripting.

Can any skilled person explain to me what "@" does with variables?
I saw in vanilla scripts usage "@$variable", but not fully understand...
Multiply entropy by absolute zero

Freedom in space
Rothank
XWiki Moderator
XWiki Moderator
Posts: 85
Joined: Mon, 11. Aug 14, 22:48
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by Rothank »

Most modders hang around Egosoft's Discord these days. You may have better luck getting answers there!
Image Image
Image
User avatar
Dj_FRedy
Posts: 244
Joined: Mon, 27. Jun 11, 05:58
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by Dj_FRedy »

ChemODun wrote: Thu, 12. Dec 24, 01:51 By the way, I'm new for X4 and its scripting.

Can any skilled person explain to me what "@" does with variables?
I saw in vanilla scripts usage "@$variable", but not fully understand...
Maybe it will clarify your doubts a little more. Recommended reading.
Lookup tests and suppressing errors: https://wiki.egosoft.com:1337/X%20Rebir ... you%20look
"All my contributions to the Technical Support and Public Beta Feedback sections will be concise and to the point, no diatribes, that's what the other sections are for".
Thank you for your efforts.
User avatar
ChemODun
Posts: 405
Joined: Mon, 12. Feb 07, 21:58
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by ChemODun »

Rothank wrote: Thu, 12. Dec 24, 07:00 Most modders hang around Egosoft's Discord these days. You may have better luck getting answers there!
Thanks, will think about ;-)
Dj_FRedy wrote: Thu, 12. Dec 24, 07:06 Maybe it will clarify your doubts a little more. Recommended reading.
Lookup tests and suppressing errors: https://wiki.egosoft.com:1337/X%20Rebir ... you%20look
Thanks! Somehow missed it.
Multiply entropy by absolute zero

Freedom in space
User avatar
ChemODun
Posts: 405
Joined: Mon, 12. Feb 07, 21:58
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by ChemODun »

[1.01] - 2024-12-13
Added
- Added option to protect ships and stations in the desired sector via reaction on attacks on them by hostile ships.
- Added option to record events to the logbook, including starts, travel to desired sector, flying to the target, attacking the target, destroying the target, etc.

Changed
- Between scans it uses move.idle script.
- Changed the type of option to disable on destruction threat: from boolean to the percentage of hull to make this order disabled.

Improved
- Approaching to the target is rewritten.
- Attack of the ship is now under control o
Multiply entropy by absolute zero

Freedom in space
User avatar
ChemODun
Posts: 405
Joined: Mon, 12. Feb 07, 21:58
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by ChemODun »

[1.02] - 2024-12-20
Added
- Added parameter to set exact delay between scans.
Fixed
- Fixed a bug with approaching to the target - if it has equal speeds the catching is never happened.
- Added additional small delay in the approaching routine to prevent freezing
Improved
- Improved the experience gaining
Multiply entropy by absolute zero

Freedom in space
User avatar
ChemODun
Posts: 405
Joined: Mon, 12. Feb 07, 21:58
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by ChemODun »

[1.03] - 2024-12-26
Added
- Added possibility to use the Protect Sector order in the fleet mode with the Mimic order.
Improved
- Improved the target separation in case of "sharing" is disabled - variable wait is solve the problem with the same target selection.
Multiply entropy by absolute zero

Freedom in space
User avatar
ChemODun
Posts: 405
Joined: Mon, 12. Feb 07, 21:58
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by ChemODun »

[1.04] - 2024-12-31
Fixed
- Slow floating to sector center when the "Park at sector core" is not enabled.
Multiply entropy by absolute zero

Freedom in space
User avatar
ChemODun
Posts: 405
Joined: Mon, 12. Feb 07, 21:58
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by ChemODun »

[1.06] - 2025-03-01

Fixed
  • The Lost Ship Replacement not always works correctly.
[1.05] - 2025-03-01

Added
  • Support for the Lost Ship Replacement feature of version 7.50
  • Added additional parameter to set a threshold from moving to target to attack it. In percentage of the radar range.
Fixed
  • For version 7.50 and higher, boosters are used always, despite the Avoid boosters usage option.
  • The default value for Disable on destruction threat is 0 percent - i.e. disabled this check.
Multiply entropy by absolute zero

Freedom in space
User avatar
ChemODun
Posts: 405
Joined: Mon, 12. Feb 07, 21:58
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by ChemODun »

[1.08] - 2025-05-11

Added
  • Internal (experimental) station attack order. Written from scratch positioning ships against stations, including reaction on stations drones attacks in non-OOS mode. Works only for fleets with L and XL ships. For only L ships - four and more ships are recommended.
  • Possibility to select station attack order - by default it is Coordinate attack. Other possibilities are usual one and experimental one.
  • Additional confirmation for the working in hazardous regions, like The Void. Please be aware - the order will stop working in the sectors with the hazardous regions without the confirmation.
  • Own icons for the Protect Sector and Station attack orders.
Improved
  • Fully rewritten the logic of idling and scanning for enemy ships. Now it is more precise.
  • Implemented workaround for the repairing/restocking ships. By default Player owned ships can be repaired only in current sector. Please be aware - after installing version 1.08 the ships controlled by this order can go to be repaired/restocked after some time. It is not a bug, but a feature :-).
Fixed
  • Fixed a reaction on attack on player's ships/stations by enemy drones.
  • Accidentally attacks from the non-enemies are not considered as an attack on the player ships/stations.
[1.07] - 2025-03-16

Added
  • By negative relation to set the relation to possible targets more precisely.
  • Park exactly there to set the exact position where the ship should park when it is not targets to fight against. Useful for big sectors.
Improved
  • Idle flying when any parking is not set.
  • Attack of the stations via Coordinate attack in now visible, as it is run not as a script, but as an order.
Multiply entropy by absolute zero

Freedom in space
User avatar
ChemODun
Posts: 405
Joined: Mon, 12. Feb 07, 21:58
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by ChemODun »

There is a comparison for the three available orders to attack station: two from vanilla and one from the Protect Sector.
Target: Xenon Wharf in Matrix #79B.
Fleet: 5 Odysseus E and 6 Syn
Mode: In Sector. I.e. player is the same sector, moreover, on one of the involved ships, but not manage the attack.
  • "Standard" Attack:
    • Time to full destroy: 23 min
    • Combat losses: 4 / 11 + some ship has hull and / or component's damaged
    • Link on YouTube
  • "Coordinate Attack"
    • Time to full destroy: 23 min
    • Combat losses: 2 / 11 + some ship has hull and / or component's damaged
    • Link on YouTube
  • Protect Sector "Experimental" Station Attack
    • Time to full destroy: 33 min
    • Combat losses: 0 / 11, no ships with hull or components damaged
    • Link on YouTube
Video is on 3x speed.

Goal is achieved.
Safe stations attack order is done.

Specifics

The radius of "rotating" around during attack is depended on the skill of ship's captain. The more skilled use less distance, faster destroy of target.

Script uses some "heavy" calculation, so, currently will not be acceptable for NPC fleets.
Multiply entropy by absolute zero

Freedom in space
User avatar
ChemODun
Posts: 405
Joined: Mon, 12. Feb 07, 21:58
x4

Re: [Mod/AIScript] Order "Protect Sector"

Post by ChemODun »

[1.09] - 2025-05-18

Fixed
  • Mimic mode not started correctly.
Changed
  • If experimental order of Attack Station is selected but fleet has ships smaller than L- both experimental and Attack Station options are disabled.
Multiply entropy by absolute zero

Freedom in space

Return to “X4: Foundations - Scripts and Modding”