[AI Hunter] Mechanics Behind: "Simulation"

Purpose

    Simulation is a mechanic made to prevent issues occur when hunters are out of player’s render/ simulation distance. Issues may include hunters being unable to move (if they are in "mob-unable-to-move" zone) and hunter duplication (if hunters are completely unloaded).

 

How it works

    In short, simulation prevents said issues by uploading a hunter when there is no player within its present zone. The actions of uploaded hunters will be simulated, and they will return if their distance to any player is less than return range.


Present Zone & Present Range

    Present zone is a cuboid shaped detection zone with hunter as its centre, with present range x 2 as both its width and length, and its height span all the way from the void to the build limit. Every Minecraft tick the system check if any player is inside the present zone. If no player is within the zone, the corresponding hunter will get uploaded. Each hunter have its own present zone.


    Present range equal to is a half of the width/ length of present zone. It is normally 120 blocks, and in potato device mode it is 24 blocks. 120 and 24 values was determined based on how mob behave near the edge of player's render/ simulation distance when it were set to 2 and 8 chunks.


Upload

    Upload basically spawn a fresh area effect cloud entity, copies the data of a hunter into that area effect cloud entity using scoreboard, and the original hunter entity will be deleted (killed) without triggering the respawn system. Transferred data include position (x, z values), hp, idle timer, upgrade timer, upgrade level and intention. There will be one area effect cloud entity for each uploaded hunter.


Brain & Simulation

    Said area effect cloud entity with a hunter's data essentially becomes the brain of that hunter. Every 6 Minecraft ticks, the brain will simulate hunter's action including travel, regenerate hp, reduce idle timer, doing upgrade and change intention. Simulation results will update the brain's data in the scoreboard.


Return & Return Range

    Once the brain determined that the distance between its simulated hunter and a player is less than return range, a fresh hunter will be spawned in and be loaded with the updated data of the brain. The brain area effect cloud entity will be deleted immediately after. This process is called return.


Possible Questions

Q: Why not use marker entity over area effect cloud entity as brain?

A: Yes marker entity would be little more efficient than area effect cloud, but marker entity was introduced in Minecraft 1.17, and AI Hunter datapack needs to be compatible from Minecraft 1.16 (at least for now).