Home » MODDING HQ 1.13 » v1.13 Coding Talk » AI light avoiding behavior
AI light avoiding behavior[message #302647] Sun, 01 April 2012 17:46 Go to next message
Registered:April 2012
Let me first say that I play JA2 since day one and only now had a look at your mod. It is really excellent in most aspects and I'm really enjoying it.

There is one big gripe for me however, the behavior of AI in nighttime combat in lighted areas. This can be mainly seen with militia but is probably true for enemies as well. On their way to their destination when stepping into a lighted tile they will turn around halfway and leave the lighted area, going back where they came from. Or, if they are already standing in light, they will leave the light, then turn around and continue to their destination, stepping back into the light. This will get them stuck in a continous loop where they will never reach a destination on the one hand and step into light every single turn on the other hand. It is horrible at Drassen airport where most are caught at the gates and only a tiny fraction of the militia will fight while the rest will waste your time and turn the combat into a frustrating experience.

It shouldn't be too hard to change this behaviour and I would be willing to put my time into it. I have not looked at the source yet, so I will have to guess right now. Since JA2 is from the nineties I expect it to use a variation of A* or some related pathfinding. If this is the case the problem could be solved easily by increasing the pathing cost for lighted tiles. This would solve the problem as they would stll continue running trough light if it was the only path available and otherwise avoid lighted tiles. The portion of the code that makes them leave light when they do not have a target destination could be left unchanged.

I hope I selected the right part of the forum for this post. Should I start looking at the code and implement a solution or would that be unwanted behaviour around here? I'm new to this forum and don't mean to offend anyone. Is someone in charge of AI behavior?
Re: AI light avoiding behavior[message #302649] Sun, 01 April 2012 18:00 Go to previous messageGo to next message

Registered:July 2009
Hello hermano, Welcome to the Pit :wave:
AI is a topic that can be summarized like this:
Everyone knows it's broken, many will voice that opinion loudly, few are willing to do something about it and none so far have had the skill, time and patience to actually do something about it without losing their sanity. You have been warned :devilaugh:

Other than that, we are open to contributions. Depending on the scope of your changes, either release them as a separate project for testing and evaluation/discussion (that includes posting the source) or send minor fixes and improvement directly to RoWa21

Re: AI light avoiding behavior[message #302650] Sun, 01 April 2012 18:18 Go to previous messageGo to next message
Registered:April 2012
Thank you, DepressivesBrot. I'm downloading the sourcwes right now and will have at look at it. If I'm guessing right the changes should not be too big. I'll try not to lose my sanity, the little that is left at least Smile .

I will probably have something to show at the weekend, my free time this week is a bit limited. I first had to install Windows as VisualStudio would not work on wine. Installing and updating all that stuff was a waste of time and I currently really hate Microsoft and their products.
I had a look at the code and it shouldn't be much of a problem to change the pathing to avoid lighted areas. I'll have to put in a bit more effort as merc pathing will be affected as well, both in turn based and real time situations. This is a two-edged sword. It will be quite nice if mercs avoid stepping into light when sneaking around. On the other hand it would be annoying if you want them to run somewhere and the pathing would not give you a direct path when light is around. I think making the behaviour dependent on merc stance should be a good solution. Prone or crouched mercs will avoid lighted areas at night and will only path through it if the alternatives are to long or if you send them right into the light, while walking and running mercs will completely ignore lights for their paths.
Another byproduct would be that AI and mercs would avoid burning areas as these are lighted as well, if I remember correctly. This is also two-edged. On the one hand more intelligent behaviour will be nice, on the other hand those joke situations where enemy soldiers run repeatedly into burning areas and die horribly by the dozen will be a thing of the past.

I will report back when I have something to show.

[Updated on: Mon, 02 April 2012 17:39] by Moderator

Re: AI light avoiding behavior[message #302882] Fri, 06 April 2012 20:27 Go to previous messageGo to next message
Registered:April 2012
I had some time to code today and implemented a first version of light avoidance for pathing.

A light avoiding path will be computed under following conditions:
The map is aboveground, lightlevel is nighttime (so dusk/dawn are excluded),
for milita and enemies: regulars will avoid light under red and black status, elites will always avoid light, green milita and administrators will not avoid light
for mercs: prone and crouching mercs will avoid light, others will ignore light.

Pathing costs (not AP costs) for lighted tiles are doubled, so characters will usually chose an unlit path unless its AP costs are more than twice as big as those of a lit path. There should be no slowdown during daytime, in lighted areas during night the slowdown should be pretty much imperceptible.

I don't have time to test how these changes are working out today, I'll probably be able to run a few tests on monday. If everything works fine I'll post the changed files (PATHAI.h and PATHAI.cpp) and a compiled .exe here.
Re: AI light avoiding behavior[message #302886] Fri, 06 April 2012 23:07 Go to previous messageGo to next message

Registered:April 2009
Location: Germany
Perhaps this might be worth considering:

I reember an occasion where I had a squad of six mercs on a mission south of tixa. The road-sector they were was attacked by a patrol. This was then reinforced from two other patrols (thereby the enemy came from two sides). All in all about 60-70 soldiers.

There was no good defensible position, so I used a small trick: I put my mercs into the northwest corner of the map. All of them were positioned towards an attack from the direct middle of the map. They didn't defend their south or east position. Instead, I fired two lightning mortar rounds about 30 tiles away to the south and west. As a result, these areas were practically unpassable by the AI (they couldn't pass because of the behaviour you mentioned). This left only a small corridor to my position in between that wasn't alight. The AI only used that, and was consequently very tightly packed and easily destroyed. I basically fortified my position with pure light Smile

What I'm trying to tell by this is that it might be good to have the enemy try to run through the light once he's decided to go through and not decide to turn bakck. Don't know how hard it might be to implement that... just thought to give you an idea.

Re: AI light avoiding behavior[message #303096] Mon, 09 April 2012 14:15 Go to previous messageGo to next message
Registered:April 2012
I have terrible news. The old notebook I've been working on died yesterday, it's CPU took its last breath. I do have a copy of the sourcecode and will post it here. My work on this project however will be halted for the foreseeable future. It would be great if someone would test these changes, or if someone knew someone else who would be willing to.

Let me explain what I changed about the code, it's just a few lines that should be easy to read, even for a c beginner, and are well documented. All changed parts can be found by searching for 'hermano'.

In PATHAI.cpp a copy of the function 'FindBestPath' named 'FindBestPathAvoidLight' has been added. This way slowdown of pathfinding in all circumstances where light avoidance will not be needed is prevented.
In 'FindBestPathAvoidLight' there is this line:
if( LightTrueLevel( newLoc, ubLevel ) < NORMAL_LIGHTLEVEL_NIGHT ) nextCost*=2;

doubling the path cost for all tiles with a light level higher than nighttime light level. Wether the factor two is a good solution will have to be tested, a factor of 1.5 might work well too.
The function 'PlotPath' in PATHAI.cpp was changed, as well. Here it is being decided if usual pathing or light avoiding pathing should be used. This should effect all instances of pathfinding.
In PATHAI.h a reference to 'FindBestPathAvoidLight' has been added.

In 'DecideAction.cpp' I tried to change AI light avoiding behaviour as mentioned by FlugEnte in the previous post. AI should not abort all actions and flee from light if it still has a path computed. I don't know the code well enough to state that the added clause
&& pSoldier->pathing.bPathStored == FALSE

will actually work as intended. I wanted to test this today.

That's it, as you see the changes are pretty minor and simple. The code compiled without problems, but I never got to test it, I'm sorry. As I added only a few lines I hope there will be no bugs, but as you know typos do happen. I'll check back to answer any questions. I hope somebody will have a look at this.

Files: Sources
The original files were taken from last weeks source SVN.

Edit: Found a bug, changed the line and reuploaded the files.

[Updated on: Mon, 09 April 2012 14:25] by Moderator

Re: AI light avoiding behavior[message #324000] Thu, 15 August 2013 12:26 Go to previous messageGo to next message

Registered:February 2008
Location: Zagreb - Croatia
Hi there,

Recently start testing and debuging AI from latest build, and after few minutes of playing game run into AI weird endless loop "seek enemy then go run for cover" decisions. Funny part is finding cover in LOS of my mercs from which AI try to hide, and turn after turn he repeats the same bad decision Razz

Anyway although this problem is not directly related to running from light or running into fire, but could be related in similar bad decisions, so I would like see what hermano did.
Unfortunately this topic is pretty old and also link to hermano files is invalid, does maybe anybody had those files?

Re: AI light avoiding behavior[message #324019] Fri, 16 August 2013 00:15 Go to previous messageGo to next message

Registered:August 2004
Location: So. Cal. or texas
I don't have those files, I'd wonder if they are on the SVN though.

also if your looking into this, perhaps you could check into how many steps ahead the pathfinding looks, I think I read someplace on one of the subforums the system batches things in like 4 tiles, which is why often the enemy and militia run 3-4 tiles, then turn and run back, then try again, rinse repeat etc etc.

I was wondering since the visual range is adjusted and all if this couldn't be changed to a % based on the visual range constant. 50%+1 in daylight 25%+1 in nighttime or something like that
Re: AI light avoiding behavior[message #324022] Fri, 16 August 2013 02:12 Go to previous messageGo to next message

Registered:February 2008
Location: Zagreb - Croatia
Already check SVN, there are no changes named hermano in source files. Anyway running from light was deliberately put in AI decisions, so rather will put this on hold because community need to decide how this should be redesigned.
For now my plans are to try fix most obvious AI bad decisions and eventually adding small AI improvements (like AI friendly fire control), which will not lead to savegame inconsistency.

Not sure if we are talking of same thing this 3-4 tiles looping problem maybe is not related to pathing, I notice that AI do strange things when reach your latest known location and find nothing (e.g. you sneak out) most of soldiers will just roam to this location and do similar thing you described.

Sorry don't know about this visual ranges, and if this is something new then answer should be given by author Smile
Re: AI light avoiding behavior[message #327065] Thu, 24 October 2013 06:44 Go to previous messageGo to next message

Registered:October 2013
I know this is a bit late and perhaps you found the files already, but have you tried speaking with depressivebrot or flugente if they have a copy of said files?

There's also this: http://www.bears-pit.com/board/ubbthreads.php/topics/303650/What_should_the_AI_consider.html#Post303650

[Updated on: Thu, 24 October 2013 06:56] by Moderator

Re: AI light avoiding behavior[message #327109] Thu, 24 October 2013 21:43 Go to previous message

Registered:February 2008
Location: Zagreb - Croatia
Thanks Bananarepublic,
Scheinworld already provide me with necessary files, forget to update topic.

Currently not working on this problem so who ever like could try to improve this.
Previous Topic: Create Cyborg IMP(half robot,half human)
Next Topic: Movement while aiming? (edit: AP cost weighting by weapon type?)
Goto Forum:

Current Time: Mon Mar 25 08:05:26 EET 2019

Total time taken to generate the page: 0.01999 seconds