Page 1 of 1

Questions about enemy api

Posted: Fri Mar 04, 2022 8:03 pm
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.

Re: Questions about enemy api

Posted: Fri Mar 04, 2022 10:46 pm
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.

Re: Questions about enemy api

Posted: Sun Mar 06, 2022 10:35 am
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.