Question: Meandering behavior for enemies

Discuss modding questions and implementation details.
imsobadatnicknames
Posts: 371
Joined: Sun Jun 02, 2019 4:28 pm
Location: Colombia

Question: Meandering behavior for enemies

Post by imsobadatnicknames »

Something I noticed a while ago is that when enemies don't have anything to fight (like when they're assigned to the player team and there's no other enemies around to make them go aggro) they just stand completely still.

I'm a total noob to Unity and C# in general, so idek if this is a dumb question but... would it be possible to make a mod that takes the AI that makes commoners in towns walk around aimlessly and apply it to enemy entities when there's nothing triggering their aggression?

Of course... I'm not asking anyone to make this or even explain to me how to make this, and I wouldn't blame anyone for thinking it's a waste of time, as the current system works fine as is and this behavior is something the average player will probably never notice or care about.

Tbqh the only reason I care about it is kinda niche and dumb, I just think adding meandering behavior to enemies would improve my Populated Buildings mod a bit, as the NPCs created with it currently just kinda awkwardly stand around, and walking around would make it seem like they're going about their day. Also it would probably improve my Ghaksha companion mod (and by extension the two other companion mods Knightwalker41 has made based on its code), as currently when companions from these mods are with you in a dungeon and you clear out all nearby enemies they just awkwardly stand in one place until the mod tells them to teleport closer to you.
Released mods: https://www.nexusmods.com/users/5141135 ... files&BH=0
Daggerfall isn't the only ridiculously intrincate fantasy world simulator with the initials DF that I make mods for: http://www.bay12forums.com/smf/index.php?topic=177071.0

l3lessed
Posts: 1403
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Question: Meandering behavior for enemies

Post by l3lessed »

So you know, this is my next big planned project. I have worked through enemy ai and motor scripts, I can tell you, this will take some time.

I want to build a full follower mod with companions that pathfind/follow behind the player.

We can possibly steal/replicate the combat ai path finding, repurpose it, and set it up for general pathfinding in an area, but I'm thinking this will take some heavy lifting. Not 100% sure how far we can push the base scripts.
My Daggerfall Mod Github: l3lessed DFU Mod Github

My Beth Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

Enjoy the free work I'm doing? Consider lending your support.

User avatar
Hazelnut
Posts: 3016
Joined: Sat Aug 26, 2017 2:46 pm
Contact:

Re: Question: Meandering behavior for enemies

Post by Hazelnut »

Any ideas how to do it without needing to fork DFU again?
See my mod code for examples of how to change various aspects of DFU: https://github.com/ajrb/dfunity-mods

l3lessed
Posts: 1403
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Question: Meandering behavior for enemies

Post by l3lessed »

I need to look into enemy motor and AI scripts again. I do think there may be some accessible objects to make it stand alone, but not 100% sure. If I was able to get the fpsweapon rebuilt for combat overhaul as a stand alone mod, than hopeful can do this without branching.

I can't open the code right now to look. The key will be if there is a way to easily access the enemies vector position in the world and update it within the base code and then use the difference between them to run the actual movement/path finding code. I don't believe there is an easy way right now to just feed vector 3 positions into the motor scripts and have the code do all the pathfinding for you.

Grabbing positions is easy. I need to explore the updating their vector 3 position. There is a way to move enemies, kind of pushing them. It's what I use for the knock back effects of my mod. However, it's really basic, and it has no animations, so the animations would have to be tied to that move method; this approach may work in mimicking follower walking and moving, but I'm skeptical because it hijacks the combat knock back code to do it. And, the knock back method is a pretty basic vector 3 movement method.

I believe also need to setup built in debugging and teleporting, so when npcs do get stuck somewhere, they will still teleport to the next vector 3 location, like previous Beth games with followers.

Also, I think town ai works by not having tons of objects for civilians to get stuck on. Notice how empty the city is of everyday objects? So, we would need to develop something similar to the combat path finding but maybe even more nuanced.
My Daggerfall Mod Github: l3lessed DFU Mod Github

My Beth Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

Enjoy the free work I'm doing? Consider lending your support.

l3lessed
Posts: 1403
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Question: Meandering behavior for enemies

Post by l3lessed »

Took a quick peek into enemy motor script. The pathfinding is basic. It pretty much waits until a player/enemy is sensed using the senses script (which detects audio from player and shoots out raycasts to mimic enemy vision). Once sense script detects you/enemy, the entity's motor pathfinding kicks in.

It is really basic. It grabs the enemies last known location from the entities sense hitray cast or detected audio, then it tries to head that way by shooting out raycasts and moving in that direction. As it does this, it also is shooting out raycasts to detect objects or possible cliffs/falls, and if it does find them, it tries to find an alternate route by shooting out more raycasts in 45 degree angles. It does this until it finds an alternate path or gives up and keeps moving towards the players last known location.

That's pretty much it.

Seems we would need to write a new pathfinding script for when enemies aren't in combat, including pacified. When enemies are not in combat, the mod takes over their movement by setting up a sandbox area, engaging the wonder pathfinding ai/script(which would use some form of sphere/arc/raycasting to calculate paths and detect objects to move around), and then when the enemy senses detect a player and kick on the base engine enemy motor script, the mod shuts off the custom pathfinding.

Any other way would probably require working on the enemy motor base script for future builds more. I don't see how could setup the mod to not cause movement bugs/conflicts without disabling the enemy motor and reenabling it when needing it.
My Daggerfall Mod Github: l3lessed DFU Mod Github

My Beth Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

Enjoy the free work I'm doing? Consider lending your support.

imsobadatnicknames
Posts: 371
Joined: Sun Jun 02, 2019 4:28 pm
Location: Colombia

Re: Question: Meandering behavior for enemies

Post by imsobadatnicknames »

l3lessed wrote: Mon Sep 28, 2020 6:50 pm Took a quick peek into enemy motor script. The pathfinding is basic. It pretty much waits until a player/enemy is sensed using the senses script (which detects audio from player and shoots out raycasts to mimic enemy vision). Once sense script detects you/enemy, the entity's motor pathfinding kicks in.

It is really basic. It grabs the enemies last known location from the entities sense hitray cast or detected audio, then it tries to head that way by shooting out raycasts and moving in that direction. As it does this, it also is shooting out raycasts to detect objects or possible cliffs/falls, and if it does find them, it tries to find an alternate route by shooting out more raycasts in 45 degree angles. It does this until it finds an alternate path or gives up and keeps moving towards the players last known location.

That's pretty much it.

Seems we would need to write a new pathfinding script for when enemies aren't in combat, including pacified. When enemies are not in combat, the mod takes over their movement by setting up a sandbox area, engaging the wonder pathfinding ai/script(which would use some form of sphere/arc/raycasting to calculate paths and detect objects to move around), and then when the enemy senses detect a player and kick on the base engine enemy motor script, the mod shuts off the custom pathfinding.

Any other way would probably require working on the enemy motor base script for future builds more. I don't see how could setup the mod to not cause movement bugs/conflicts without disabling the enemy motor and reenabling it when needing it.
Wow! When I made this thread I didn't expect anyone to think about this so deeply, it was pretty much a combination of curiosity and wishful thinking. But that's super cool!

So, theoretically it'd be possible to make a script that kicks in when the senses script isn't sensing anything and casts rays with a relatively small length every 45 degrees, detects which of those rays aren't hitting anything, and randomly picks the direction of one of those rays to start moving in, and every few frames checks again and picks a new direction, right? (Sorry if I'm not phrasing that very eloquently, english isn't my first language and there are some days where i just can't english if you know what I mean lol). I think that behaviour would make for a relatively convincing meandering movement while preventing them from bumping into obstacles too much, right?
Released mods: https://www.nexusmods.com/users/5141135 ... files&BH=0
Daggerfall isn't the only ridiculously intrincate fantasy world simulator with the initials DF that I make mods for: http://www.bay12forums.com/smf/index.php?topic=177071.0

l3lessed
Posts: 1403
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Question: Meandering behavior for enemies

Post by l3lessed »

In a simplified way, yes, that is the thought.

This is similar to how I am working my fps combat mods. I shut off the fpsweapon script in as compartmentalized way as possible (by forcing the engine to not render the sprite); This keeps the fpsweapon and weaponmanager scripts operating so the base engine still functions, but stops any actual rendering/animating code. Then, I pretty much rebuild and inject my mod to recreate the weapon and shield renders.

There are tons of things to consider, and a number of issues you could possibly run into. The biggest off the top of my head is animating the sprites. I also would consider digging into and understanding all the connected scripts and objects. I only did a real quick scan of one script file, so there can be dependencies and objects I am missing that could add complications to this.

Also, you have to consider cases and switches for things like flying npcs.

Also, I'm unsure how the city npcs wander. I don't know if they are also hijacking the enemymotor.cs script or if they have their own.
My Daggerfall Mod Github: l3lessed DFU Mod Github

My Beth Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

Enjoy the free work I'm doing? Consider lending your support.

User avatar
Jay_H
Posts: 4070
Joined: Tue Aug 25, 2015 1:54 am
Contact:

Re: Question: Meandering behavior for enemies

Post by Jay_H »

l3lessed wrote: Mon Sep 28, 2020 8:16 pm Also, I'm unsure how the city npcs wander. I don't know if they are also hijacking the enemymotor.cs script or if they have their own.
They have a navgrid inside cities that they follow; check out this article.

l3lessed
Posts: 1403
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Question: Meandering behavior for enemies

Post by l3lessed »

interesting, Cool trick for cities.
My Daggerfall Mod Github: l3lessed DFU Mod Github

My Beth Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

Enjoy the free work I'm doing? Consider lending your support.

imsobadatnicknames
Posts: 371
Joined: Sun Jun 02, 2019 4:28 pm
Location: Colombia

Re: Question: Meandering behavior for enemies

Post by imsobadatnicknames »

That's really interesting. So it means their wandering behavior would be impossible to implement indoors.
Released mods: https://www.nexusmods.com/users/5141135 ... files&BH=0
Daggerfall isn't the only ridiculously intrincate fantasy world simulator with the initials DF that I make mods for: http://www.bay12forums.com/smf/index.php?topic=177071.0

Post Reply