AI in Lua
#1
I'm starting a basic AI plugin. I thought I'd share my approach and see what you think.

First off, I'm using HOOK_WORLD_TICK to trigger activity.

I only fire off my routine if the last time I triggered was more than 250ms ago, and was for another world.

I then iterate through each entity in that world and check that it is a mob. If it is a mob and within 25 blocks of a player character in that world I store it and some parameters in a table.

I then house clean the table for entities that are out of range or no longer in the game.

I then loop through the table and make them do things.

a) get to safety (out of water, lava, sunlight, etc...)
b) move towards target player
c) move towards villager
d) attack target player or villager

This is just my starting list.

Here's the start of the project. It only has the part that stores the mobs right now.

https://github.com/planetx/AI
Reply
Thanks given by:
#2
Unfortunately your code as it is won't work and will most likely crash the server quite soon. You're doing something that should never ever be done - you're storing cEntity objects for later usage. If the server destroys the entity in between the calls to AI_OnWorldTick(), your AI_Mob table contains reference to an object that is no longer valid and accessing it in any way is Undefined Behavior, which may result in the code appearing to work or crashing the server or causing a nuclear meltdown or devouring the entire Earth in a black hole. You can only store the UniqueID and look the entity object up again when needed.
Reply
Thanks given by:
#3
(01-29-2015, 04:35 PM)xoft Wrote: Unfortunately your code as it is won't work and will most likely crash the server quite soon. You're doing something that should never ever be done - you're storing cEntity objects for later usage. If the server destroys the entity in between the calls to AI_OnWorldTick(), your AI_Mob table contains reference to an object that is no longer valid and accessing it in any way is Undefined Behavior, which may result in the code appearing to work or crashing the server or causing a nuclear meltdown or devouring the entire Earth in a black hole. You can only store the UniqueID and look the entity object up again when needed.

The plan is to do the lookup. That is why I store the UUID. I couldn't find the code to look up the entity by UUID. So I did this to test. Smile
Reply
Thanks given by:
#4
cWorld:DoWithEntityByID()
Reply
Thanks given by:
#5
"devouring the entire Earth in a black hole"

The things software do these days.
Reply
Thanks given by:
#6
(01-30-2015, 08:27 AM)tigerw Wrote: "devouring the entire Earth in a black hole"

The things software do these days.

Well, it did create a small singularity above my server. I fed it some Cheetos and it disappeared in an orange cloud with a small poof!
Reply
Thanks given by:
#7
Thanks for cheering me up guys, I desperately needed it today.
Reply
Thanks given by:
#8
How programming works:

[Image: programming.gif]
Reply
Thanks given by:
#9
Maybe every few ticks, poll all entities and if they're a mob, add their ID to a table, check if the entity of that ID is nil or not, if its not, do routines, and on death, remove their ID from the table?

If there are alot of mobs, it may cause some slowdowns tho, but I doubt it with your i7.
Reply
Thanks given by:
#10
(02-02-2015, 05:10 AM)DiamondToaster Wrote: Maybe every few ticks, poll all entities and if they're a mob, add their ID to a table, check if the entity of that ID is nil or not, if its not, do routines, and on death, remove their ID from the table?

If there are alot of mobs, it may cause some slowdowns tho, but I doubt it with your i7.

That's what I'm doing. But, I'm also only interfering with a mob if it is within 50 blocks of a player. That setting will be configurable of course.
Reply
Thanks given by:




Users browsing this thread: 10 Guest(s)