Modding:Modding Efficiently: Difference between revisions

From Vintage Story Wiki
Line 27: Line 27:
Use [https://github.com/anegostudios/vsessentialsmod/blob/master/Systems/EntityPartitioning.cs EntityPartitioning] - <code>api.ModLoader.GetModSystem<EntityPartitioning>().WalkEntityPartitions()</code>. This system partitions entities into 8x8x8 block buckets through the entire loaded game world, then only searches inside those buckets, within your supplied search radius, greatly reducing the search space.</li>
Use [https://github.com/anegostudios/vsessentialsmod/blob/master/Systems/EntityPartitioning.cs EntityPartitioning] - <code>api.ModLoader.GetModSystem<EntityPartitioning>().WalkEntityPartitions()</code>. This system partitions entities into 8x8x8 block buckets through the entire loaded game world, then only searches inside those buckets, within your supplied search radius, greatly reducing the search space.</li>
<li><strong>Block Search</strong><br>
<li><strong>Block Search</strong><br>
a) If you want an entity to find a certain, not extremely common block, use the [https://github.com/anegostudios/vsessentialsmod/blob/master/Systems/POIRegistry.cs POI Registry]. This registry lets block entities register a point of interest that entities can search through very efficiently. They are also partitioned into chunk column buckets to additionally reduce the search space.<br>
a) If you want an entity to find a certain, not extremely common block, use the [https://github.com/anegostudios/vsessentialsmod/blob/master/Systems/POIRegistry.cs POI Registry]. This registry lets block entities register a point of interest that entities can search through very efficiently. They are also partitioned into chunk column buckets to additionally reduce the search space. For a code sample, check out the [https://github.com/anegostudios/vssurvivalmod/blob/master/BlockEntity/BEBerryBush.cs berry bush block entity]<br>
b) For regular full chunk scans, I recommend a separate background thread that loads the chunk and access the raw block data via <code>chunk.Blocks</code>. It's an array indexed with <code>(y * chunksize + z) * chunksize + x</code>, where the xyz are local coordinate from 0 to chunkize-1<br>
b) For regular full chunk scans, I recommend a separate background thread that loads the chunk and access the raw block data via <code>chunk.Blocks</code>. It's an array indexed with <code>(y * chunksize + z) * chunksize + x</code>, where the xyz are local coordinate from 0 to chunkize-1<br>
c) For rare area scans, there is `api.World.GetBlockAccessorPrefetch()`. This blockaccessor lets you pre-load an area and then rather efficiently iterate through each block in given area
c) For rare area scans, there is `api.World.GetBlockAccessorPrefetch()`. This blockaccessor lets you pre-load an area and then rather efficiently iterate through each block in given area
</li>
</li>
</ul>
</ul>