UPDATED Oct 18, partial changelog
- Very old bug with flagship surrenders causing fleets to go neutral should be (hopefully) fixed
- Levis's idea of a progression of strength for effects on surrender chances implemented in a progression of powers raising to 0.625, then 0.5, then 0.4 as a given catagory of surrender reason reaches the more extreme stages.
- Demasted enemies should now stop running and try to fight you, please let me know if they do not
- tons new retreat checks, and canceling retreat, under approrpiate cirucmstances, including ammo, shifts in power, etc
- Minor tweaks, cleaning up some tracelog entries, linking them to a debug code, altering some threshold conditions, etc
- unified AIShip ship withdrawal code
- Changed the distance cutoff for counting a ship's power to medium range rather than long.
- Increased forts to 3000HP for powerratio estimation to scare off larger coastal raiders
UPDATED Oct 16
Old first post in spoiler, here is the completely rewritten chanelog (since this is now much expanded from original conception). I've also extensivelly commented the file, please see code and comments for full details.
Bugfixes and improvements
A) Completely rewritten FindPowerRatio function:
Bug fixes:
1) Powerratio function now actually works to count ships (before it had many issues that kept it returning 0 values in most circumstances, including problems with obtaining the distance for the formula, problems with skipping the ship counting itself when alone, etc). Extensive restructuring, and inserting some additional checks like if(rel != RELATION_FRIEND) to avoid skipping self calculation if alone.
2) Powerratio function now counts forts (before it tried to count them with a gethp function that always returned 0)
Improvements:
1) Powerratio function now considers cannon size/quality in addition to just number of cannons (before, it counted 4lbs cannons same as 32 lbs) Cannon price provides the proxy, new function written to return it easily by character for use here. Firepower rating determined by cannon qty times price.
2) Powerratio function no longer treats distance as such a powerful influence, and instead fully counts visible ships (though before the distance calculation was not working anyway, part of was retruning 0 values)
3) Powerratio function discounts ships outside long range, before it tried to count them
4) Powerratio function uses a simple, understandable, and generally valid method--remaining firepower multiplied by remaining hp measured over each ship (with a square root because if you have the same Firepower and hp distributed on two ships, then say 5*5+5*5=50, while 10*10=100, you want them actually equal, and to keep things from shifting too dramatically).
B) Expanded surrender options
Bug Fixes:
1) Powerratio function properly inverted in the function before use in surrender chances (before, the AI was told to think it was stronger when it was actually weaker for the purpose of surrender chance influence, and since the function was broken it more or less amounted to the AI always thinking itself weaker)
Improvements:
1) Before, if morale was above a threshold determined by AI captain leadership, no surrender was possible. Conceptually, surrender was something the crew always forced on an unwilling captain, no captain was free to decide it on their own.
Now, if morale is above that threshold, surrender is still possible if you do huge damage to the enemy hull, outmatch it in firepower, and cut off its retreat by tearing up its sails (but higher morale makes it less likely even then). Now, conceptually, surrender is something the Captain can decide on if his situation is hopeless, in addition to the crew forcing it.
So now there are two conceptual routes to forcing surender, rather than just one, and player tactical options increase in a manner that feels more immersive for roleplaying.
NOTE: Significantly expanded from first version, by carving off the surrender calculations into two seperate paths, the first remains the same as before, morale the primary influence, reflecting the potential for the crew to force a surrender on the captain, the second new one looks primarilly at the tactical situation, and reflects the Captain deciding on his own to surrender.
C) Intelligent Retreats
Bug Fixes:
1) Before, the code for a ship with good morale resuming group task if running away happened before the mechanical check for retreating if damaged, meaning it wouldn't work to cause the AI to resume the group task, they would just be set to runaway again. Now, if the group is not fleeing, a high morale enemy may continue to fight even if it passes the HP theshold (but should still flee if the group is fleeing)
2) The below tactical retreat addition fixes the coastal raiders suiciding against forts bug: Drastically reduce chance of Pirate encounter in friendly ports | Page 3 | PiratesAhoy! A fort has a huge powerratio with its arsenal, and pretty much everything will run from it. However, if an enemy is marked "nosurrender" and could otherwise not retreat because of quest scripting, they should still not retreat (as the new retreat works alongside preexisiting retreats from damage, so they will only now retreat if they could retreat before)
Improvements:
1) Tactical retreats that will cause an AI fleet to run if their fleet is significantly outmatched by the enemies as to make fighting hopeless. Uses the rewritten powerratio determination, and happens after the morale check, so even at high morale they may decide to run. Responds to the changing cirucmstances of battle, as ships lose HP or cannons, or new opponents arrive.
2) Before retreats were either mechanical based on lost HP, or set by somewhat less nuanced cutoffs in screwface functions that looked at things like ship tier and hp in isolation. Those are all still currently in place, though the hard limit on HP running was lowered a bit since it is a very blunt instrument and can be arbitrary, with many of the same concerns handled better in the tactical withdrawls, which does respond to the state of the enemy as well (ie, if you damage an enemy to 39% HP when you are alreadfy at 5%, he would be forced to run. So best to lower that a bit since the tactical withdrawals are more fluid in responding to the current situation)
Also added a check on morale here, so that if the captain is still effectivelly in full control, then he won't run if he is winning heavily by strength ratio, or has no sails.
If the captain is not in full control, ie mroale has passed the surrender threshold, then his ship will run exactly as before based on mechanical considerations (even if quite unwise)
3) Made it a bit more gradiated, with the captain starting to lose a bit of control over his ship when temporary morale becomes within 5 of surrender morale (ie, the run check starts not getting cleared back to group task)
FUTURE STEPS
What I may do later, is to get the screwface functions for retreat to also make use of the new powrratio function, as some of them are aiming for the same thing but with less acurrate measurements.But for now the two systems work alongside one another.
File attached. This one is for playtesting, and as such has various traces enabled so that your compile log with record what is happening with the new stuff.
FEEDBACK note: I have set the tactical withdrawl to happen when the enemy is 2x as powerful or more, and still has ok sails. This might be right (remember, they will still have the normal screwface retreats, like merchants who feel outmatched), or it might be too low, and we should instead set it to 2x. Please provide as detailed feedback as you can about when retreats happen, as well as posting a compile log if you have them, if you feel an interesting retreat happened (ie, a smart one, or a bad one).
Also, note that while generally you must heavily damage a ship before there is any chance of surrender at all, if you are much more than 5x as powerful as the enemy fleet, they might surrender with less damage, and if you are 10x as powerful, they might surrender right away (that would take having 10x their HP and 10x their cannon firepower, because power involves square roots). In general, surrender chance needs a number of "reasons to surrender" disctated by the power difference, with squareroots used to enforce the proper number needed. The number of factors needed at various strength discrepencies will be tweaked based on feedback.
(and that because we are dealing with squareroots, 10x the power might be more like 100x the power really. So maybe only happens if you 100 times powerful really. We'll see how it works out)
So please, report any experiences, and especially would appreciate you attaching the compile logs after you've had a ship battle in a play session, as the traces will tell how everything was assessed.
Attached version was made from Levis's latest Oct 7th file. Will be overwitten by any future install of AIShip
File goes into PROGRAM/SEA_AI
Any feedback, especially as detailed in the feedback requests above, is greatly appreciated, so it can be further finetuned if needed.
Further details and examples below of how all this works in a post below!
- Very old bug with flagship surrenders causing fleets to go neutral should be (hopefully) fixed
- Levis's idea of a progression of strength for effects on surrender chances implemented in a progression of powers raising to 0.625, then 0.5, then 0.4 as a given catagory of surrender reason reaches the more extreme stages.
- Demasted enemies should now stop running and try to fight you, please let me know if they do not
- tons new retreat checks, and canceling retreat, under approrpiate cirucmstances, including ammo, shifts in power, etc
- Minor tweaks, cleaning up some tracelog entries, linking them to a debug code, altering some threshold conditions, etc
- unified AIShip ship withdrawal code
- Changed the distance cutoff for counting a ship's power to medium range rather than long.
- Increased forts to 3000HP for powerratio estimation to scare off larger coastal raiders
UPDATED Oct 16
Old first post in spoiler, here is the completely rewritten chanelog (since this is now much expanded from original conception). I've also extensivelly commented the file, please see code and comments for full details.
Bugfixes and improvements
A) Completely rewritten FindPowerRatio function:
Bug fixes:
1) Powerratio function now actually works to count ships (before it had many issues that kept it returning 0 values in most circumstances, including problems with obtaining the distance for the formula, problems with skipping the ship counting itself when alone, etc). Extensive restructuring, and inserting some additional checks like if(rel != RELATION_FRIEND) to avoid skipping self calculation if alone.
2) Powerratio function now counts forts (before it tried to count them with a gethp function that always returned 0)
Improvements:
1) Powerratio function now considers cannon size/quality in addition to just number of cannons (before, it counted 4lbs cannons same as 32 lbs) Cannon price provides the proxy, new function written to return it easily by character for use here. Firepower rating determined by cannon qty times price.
2) Powerratio function no longer treats distance as such a powerful influence, and instead fully counts visible ships (though before the distance calculation was not working anyway, part of was retruning 0 values)
3) Powerratio function discounts ships outside long range, before it tried to count them
4) Powerratio function uses a simple, understandable, and generally valid method--remaining firepower multiplied by remaining hp measured over each ship (with a square root because if you have the same Firepower and hp distributed on two ships, then say 5*5+5*5=50, while 10*10=100, you want them actually equal, and to keep things from shifting too dramatically).
B) Expanded surrender options
Bug Fixes:
1) Powerratio function properly inverted in the function before use in surrender chances (before, the AI was told to think it was stronger when it was actually weaker for the purpose of surrender chance influence, and since the function was broken it more or less amounted to the AI always thinking itself weaker)
Improvements:
1) Before, if morale was above a threshold determined by AI captain leadership, no surrender was possible. Conceptually, surrender was something the crew always forced on an unwilling captain, no captain was free to decide it on their own.
Now, if morale is above that threshold, surrender is still possible if you do huge damage to the enemy hull, outmatch it in firepower, and cut off its retreat by tearing up its sails (but higher morale makes it less likely even then). Now, conceptually, surrender is something the Captain can decide on if his situation is hopeless, in addition to the crew forcing it.
So now there are two conceptual routes to forcing surender, rather than just one, and player tactical options increase in a manner that feels more immersive for roleplaying.
NOTE: Significantly expanded from first version, by carving off the surrender calculations into two seperate paths, the first remains the same as before, morale the primary influence, reflecting the potential for the crew to force a surrender on the captain, the second new one looks primarilly at the tactical situation, and reflects the Captain deciding on his own to surrender.
C) Intelligent Retreats
Bug Fixes:
1) Before, the code for a ship with good morale resuming group task if running away happened before the mechanical check for retreating if damaged, meaning it wouldn't work to cause the AI to resume the group task, they would just be set to runaway again. Now, if the group is not fleeing, a high morale enemy may continue to fight even if it passes the HP theshold (but should still flee if the group is fleeing)
2) The below tactical retreat addition fixes the coastal raiders suiciding against forts bug: Drastically reduce chance of Pirate encounter in friendly ports | Page 3 | PiratesAhoy! A fort has a huge powerratio with its arsenal, and pretty much everything will run from it. However, if an enemy is marked "nosurrender" and could otherwise not retreat because of quest scripting, they should still not retreat (as the new retreat works alongside preexisiting retreats from damage, so they will only now retreat if they could retreat before)
Improvements:
1) Tactical retreats that will cause an AI fleet to run if their fleet is significantly outmatched by the enemies as to make fighting hopeless. Uses the rewritten powerratio determination, and happens after the morale check, so even at high morale they may decide to run. Responds to the changing cirucmstances of battle, as ships lose HP or cannons, or new opponents arrive.
2) Before retreats were either mechanical based on lost HP, or set by somewhat less nuanced cutoffs in screwface functions that looked at things like ship tier and hp in isolation. Those are all still currently in place, though the hard limit on HP running was lowered a bit since it is a very blunt instrument and can be arbitrary, with many of the same concerns handled better in the tactical withdrawls, which does respond to the state of the enemy as well (ie, if you damage an enemy to 39% HP when you are alreadfy at 5%, he would be forced to run. So best to lower that a bit since the tactical withdrawals are more fluid in responding to the current situation)
Also added a check on morale here, so that if the captain is still effectivelly in full control, then he won't run if he is winning heavily by strength ratio, or has no sails.
If the captain is not in full control, ie mroale has passed the surrender threshold, then his ship will run exactly as before based on mechanical considerations (even if quite unwise)
3) Made it a bit more gradiated, with the captain starting to lose a bit of control over his ship when temporary morale becomes within 5 of surrender morale (ie, the run check starts not getting cleared back to group task)
FUTURE STEPS
What I may do later, is to get the screwface functions for retreat to also make use of the new powrratio function, as some of them are aiming for the same thing but with less acurrate measurements.But for now the two systems work alongside one another.
File attached. This one is for playtesting, and as such has various traces enabled so that your compile log with record what is happening with the new stuff.
FEEDBACK note: I have set the tactical withdrawl to happen when the enemy is 2x as powerful or more, and still has ok sails. This might be right (remember, they will still have the normal screwface retreats, like merchants who feel outmatched), or it might be too low, and we should instead set it to 2x. Please provide as detailed feedback as you can about when retreats happen, as well as posting a compile log if you have them, if you feel an interesting retreat happened (ie, a smart one, or a bad one).
Also, note that while generally you must heavily damage a ship before there is any chance of surrender at all, if you are much more than 5x as powerful as the enemy fleet, they might surrender with less damage, and if you are 10x as powerful, they might surrender right away (that would take having 10x their HP and 10x their cannon firepower, because power involves square roots). In general, surrender chance needs a number of "reasons to surrender" disctated by the power difference, with squareroots used to enforce the proper number needed. The number of factors needed at various strength discrepencies will be tweaked based on feedback.
(and that because we are dealing with squareroots, 10x the power might be more like 100x the power really. So maybe only happens if you 100 times powerful really. We'll see how it works out)
So please, report any experiences, and especially would appreciate you attaching the compile logs after you've had a ship battle in a play session, as the traces will tell how everything was assessed.
Had another unexpected break today, so made the attached (early-stage prototype) improvement to the Sea AI.
I extensively commented the changes, so the best way to understand would be to take a look at them in the code I inserted below.
But here is a brief summary:
1) Tactical retreats: AI will be likely to run quickly if heavily outmatched. They do a strength calculation of all friendlies and enemies occasionally during combat, and if they decide their enemies are much more powerful than them, and they still have good sails, they run.
Tests show it seems to solve the suicidal coastal raider AI attacking forts--now, if a coastal raider appears in port, he will quickly turn and run unless he has a chance against the fort (ie, is very powerful ship) (though he might still get killed before he can get away if he is very weak).
Drastically reduce chance of Pirate encounter in friendly ports | Page 2 | PiratesAhoy!
Previously the retreat decisions were mostly based on ship role and hard limits on damage, meaning a little pirate ship would often insist on getting blasted quite to bits by a fort before realizing it was a bad idea to stick around. Now, they figure that out after the first volley.
FEEDBACK REQUEST: I have the AI start to retreat when they estimate the enemy group as twice as powerful as their group (but the power calculations are somewhat blunted, so it might be more like three times their power in realistic terms sometimes). That might need to be changed in response to feedback, but seems to work well so far. Let me know if you think they are running too easily or too late against overwhelming force.
Bear in mind: A) they will only run if they also have halfway decent sails under this check, B) they will still run as before if heavily damaged or if their AI for ship type otherwise tells them to run (this is just an additional possibility, all the past retreat stuff is still in), C) they need to be in combat for a few moments before the check runs, so they might take a moment to assess before fleeing
2) Fixed the usage of the function for the AI checking relative strength
Before, someone had accidentally programmed it backwards in this file--the AI thought they were more powerful when they were weaker, and the reverse as well. So I inverted the number to get the correct usage.
Very weird, probably only escaped notice because the FindPowerRatio function was almost never used (which is crazy, considering the potential to allow the AI to make intelligent withdrawal decisions--instead, it was only used to modify the surrender chance, which of course in the prior incorrect reversed state made the AI less likely to surrender when weaker. Fixed entirely now, and put to full use.
3) High morale no longer an absolute bar to surrender--if masts broken and hull heavily damaged, a captain may himself tactically decide to surrender even if the crew is still in ok discipline. However, some significant damage to the ship is needed before this can happen.
Also, the old morale threshold is now a modifier, with a gradiated level of modifiers, so decent morale ships are still unlikely to surrender unless you've really pounded them to bits.
4) New influences of sails damage and relative strength on surrender chances. Demasting the enemy ship and heavily outmatching it in firepower will cause it to be likely to surrender earlier, while an enemy that has much greater firepower than you will be less likely to surrender.
(the firepower difference part was intended originally, but because the usage of the powerratio was reversed, the old code accidentially had the opposite of the intended effect, as detailed above. Fixed that and made it have more levels to finetune the influence. )
FEEDBACK REQUEST:
If you have the AI surrender when they shouldn't, let me know, together with as much detail on the circumstances as you can provide (their hull, sails, morale, other ships around, and relative strengths, as best as you remember.)
If the AI doesn't surrender when you think it should, let me know as well.
(this should generally make the AI more likely to surrender than before, because before morale was an absolute bar to surrender, and the FindPowerRatio was reversed in the error so the AI became less likely to surrender the weaker it got.).
So will certainly be finetuning in response to feedback and playtesting.
Most of the changes are below (just a couple of other ones elsewhere in the file, all are commented)
I extensively commented the changes, so the best way to understand would be to take a look at them in the code I inserted below.
But here is a brief summary:
1) Tactical retreats: AI will be likely to run quickly if heavily outmatched. They do a strength calculation of all friendlies and enemies occasionally during combat, and if they decide their enemies are much more powerful than them, and they still have good sails, they run.
Tests show it seems to solve the suicidal coastal raider AI attacking forts--now, if a coastal raider appears in port, he will quickly turn and run unless he has a chance against the fort (ie, is very powerful ship) (though he might still get killed before he can get away if he is very weak).
Drastically reduce chance of Pirate encounter in friendly ports | Page 2 | PiratesAhoy!
Previously the retreat decisions were mostly based on ship role and hard limits on damage, meaning a little pirate ship would often insist on getting blasted quite to bits by a fort before realizing it was a bad idea to stick around. Now, they figure that out after the first volley.
FEEDBACK REQUEST: I have the AI start to retreat when they estimate the enemy group as twice as powerful as their group (but the power calculations are somewhat blunted, so it might be more like three times their power in realistic terms sometimes). That might need to be changed in response to feedback, but seems to work well so far. Let me know if you think they are running too easily or too late against overwhelming force.
Bear in mind: A) they will only run if they also have halfway decent sails under this check, B) they will still run as before if heavily damaged or if their AI for ship type otherwise tells them to run (this is just an additional possibility, all the past retreat stuff is still in), C) they need to be in combat for a few moments before the check runs, so they might take a moment to assess before fleeing
2) Fixed the usage of the function for the AI checking relative strength
Before, someone had accidentally programmed it backwards in this file--the AI thought they were more powerful when they were weaker, and the reverse as well. So I inverted the number to get the correct usage.
Very weird, probably only escaped notice because the FindPowerRatio function was almost never used (which is crazy, considering the potential to allow the AI to make intelligent withdrawal decisions--instead, it was only used to modify the surrender chance, which of course in the prior incorrect reversed state made the AI less likely to surrender when weaker. Fixed entirely now, and put to full use.
3) High morale no longer an absolute bar to surrender--if masts broken and hull heavily damaged, a captain may himself tactically decide to surrender even if the crew is still in ok discipline. However, some significant damage to the ship is needed before this can happen.
Also, the old morale threshold is now a modifier, with a gradiated level of modifiers, so decent morale ships are still unlikely to surrender unless you've really pounded them to bits.
4) New influences of sails damage and relative strength on surrender chances. Demasting the enemy ship and heavily outmatching it in firepower will cause it to be likely to surrender earlier, while an enemy that has much greater firepower than you will be less likely to surrender.
(the firepower difference part was intended originally, but because the usage of the powerratio was reversed, the old code accidentially had the opposite of the intended effect, as detailed above. Fixed that and made it have more levels to finetune the influence. )
FEEDBACK REQUEST:
If you have the AI surrender when they shouldn't, let me know, together with as much detail on the circumstances as you can provide (their hull, sails, morale, other ships around, and relative strengths, as best as you remember.)
If the AI doesn't surrender when you think it should, let me know as well.
(this should generally make the AI more likely to surrender than before, because before morale was an absolute bar to surrender, and the FindPowerRatio was reversed in the error so the AI became less likely to surrender the weaker it got.).
So will certainly be finetuning in response to feedback and playtesting.
Most of the changes are below (just a couple of other ones elsewhere in the file, all are commented)
Attached version was made from Levis's latest Oct 7th file. Will be overwitten by any future install of AIShip
File goes into PROGRAM/SEA_AI
Any feedback, especially as detailed in the feedback requests above, is greatly appreciated, so it can be further finetuned if needed.
Further details and examples below of how all this works in a post below!
Attachments
Last edited: