Questions about enemy api

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
turoni
Burner Inserter
Burner Inserter
Posts: 10
Joined: Thu Aug 04, 2016 9:42 pm
Contact:

Questions about enemy api

Post by turoni »

Hi
I have a mod idea to add a new dimension to enemies and I might want to learn the modding api for it.
Before diving in to some guides I first wanted to do a general check whether what I wanted to do is possible.
- I want to add a new enemy
- trigger certain events based on how much health the enemy has
- spawn and delete enemies
- completely override the way enemies move
more specifically, add certain paths that enemies will follow, completely ignoring the player
The last one I'm least sure about whether it is possible.
Thanks for any advice.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Questions about enemy api

Post by Pi-C »

turoni wrote:
Fri Mar 04, 2022 8:03 pm
- I want to add a new enemy
Many mods do that. Just define a new prototype in data.raw.unit.
- trigger certain events based on how much health the enemy has
To the best of my knowledge, that would work, kind of. You could listen to on_entity_damaged and check event.final_health. That will definitely work for positive damage amounts; I'm not sure if this will also work for negative damage amounts (i.e. healing). Also, the event will not be triggered if other mods decide to change the unit's health.
- spawn and delete enemies
Should work.
- completely override the way enemies move
more specifically, add certain paths that enemies will follow, completely ignoring the player
I really didn't deal much with pathfinding yet. There's LuaSurface.request_path, but according to the description this will request a path using the vanilla algorithm for units. About "completely ignoring the player" -- LuaEntity.set_command and LuaEntity.set_distraction_command look useful (using no distraction). But implementing a completely new pathing algorithm where you'd need to check position and motion of every unit you've created would probably be quite expensive in terms of UPS.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

turoni
Burner Inserter
Burner Inserter
Posts: 10
Joined: Thu Aug 04, 2016 9:42 pm
Contact:

Re: Questions about enemy api

Post by turoni »

Thanks for the help. These look like excellent starting point.
It's not so much that I want to do a new pathfinding every frame but rather pass along a predetermined path at the moment of creation.
Probably mostly simple paths like lines, circles, ...
So I think it shouldn't be too much of an extra strain on the UPS with the proposals you have given.

Post Reply

Return to “Modding discussion”