From The Depths - Forum

Full Version: Naval AI with terrain & friendly avoidance, evasive maneuvers [2017-11-05]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
This is a naval AI script originally inspired by KuramaFox's simple boat AI. It's strictly 2D meaning it only controls yaw and propulsion. However, it works fine on submarines & airships too as long as depth/altitude is controlled by some other means (G.P. PID, ACB, another script...)

It has a number of features:
  • Basic closing, broadside/attack, escape behaviors based on range from current target. Each behavior can have its own angle (relative to target bearing) and throttle setting defined.
  • Can optionally attempt to maintain a fixed distance while broadsiding by either changing its attack angle or moving in reverse.
  • Alternative "attack run" behavior can also be configured where it bounces between the minimum and maximum distances.
  • Terrain and friendly avoidance. Determines the size of its ship, multiplies it by some fudge factor (to be safe) and uses this to judge whether it can safely pass over terrain or over/under friendlies. If it can't, it will steer away as best as it can.
  • Built-in PID for yaw for smoother control.
  • Optional dodging of incoming missiles (default enabled). On surface vessels, the utility of this is... questionable. However, it will result in a hard-to-port or hard-to-starboard maneuver depending on which quadrant the missiles are predicted to impact. No, Lua cannot detect CRAMs.
  • Optional pseudo-random evasive maneuver settings for each behavior. You can specify the number of degrees to evade as well as (somewhat) control the rate. Based on the built-in Perlin noise generator in the Mathf library, so it is not quite as predictable as basic sinusoidal movement.
  • Optional air raid evasion setting. You can specify a 4th set of evasion settings for use when engaging targets above a certain altitude.
  • Ship can optionally return to where it started/was brought into play and/or return to fleet formation.
  • Works with or without naval AI card. By default, only works when AI is set to "combat" mode. Can be configured to control ship when AI is set to "on" as well.
  • Forward/reverse-facing dediblades can be used for propulsion as well.
  • Sideways-facing dediblades can be used for yaw.
Thanks to KuramaFox, Madwand, and Draba and their scripts. I was a Lua noob, but I think I got the hang of it now. Smile

Script link

Just load it into a Lua Box and switch the AI to "combat." If you don't have a naval AI card, uncomment (remove the "--") from the on = true line from the ActivateWhen section near the top.

And if you use it on an airship, remember to change Mode = WATER to Mode = AIR.

More comprehensive docs can be found here. It's a novel, but I recommend at least skimming so you get a gist of what the important options do and what the script is capable of.

Feel free to modify, create derivative works, or use in blueprints you publish. Just give me credit.

Other scripts:

Other scripts of mine that incorporate this naval-ai, but meant for different control schemes. Most will have my advanced & unpublished missile script, so be warned. (But you can disable it easily near the top.)
  • airplane.lua for anything that flies like an airplane (needs yaw/pitch/roll/forward propulsion). WIP.
  • airship.lua for airships (needs yaw + propulsion and an independent means to stay aloft, such as jets and/or dediblades)
  • submarine.lua for hydrofoil-based naval vessels (surface and submarine). Requires yaw + propulsion and hydrofoils in front of and behind the CoM.
  • tank-ai.lua for tanks using dual drive maintainers for independent track control. Be sure to disable terrain avoidance (set TerrainAvoidanceWeight to 0).
  • tank.lua is a beefier version of tank-ai.lua. Includes my cannon, missile, and shield manager modules. Same caveat about terrain avoidance.
  • warship.lua a beefier version of my naval-ai.lua. Includes missile and shield manager modules.
Updates:
Code:
2017-11-05
Tweaks to dodging (frontal dodge, dodge drive, resets when impact quadrant changes). Fixed yaw control (using plain vehicle controls) so it properly reverses direction when going in reverse.

2017-10-25
When dodging, max out the throttle (forward or reverse) if AttackReverse is true and vehicle was currently attacking.

2017-10-22
Added AttackReverse option (default false). When true and AttackDistance is set, it will reverse instead of flipping AttackDistance to maintain a set distance.

2017-05-21
PID added (configured by AttackPIDConfig) to scale the effective AttackAngle when near AttackDistance. Far less oscillation when straddling the edge of AttackDistance. Only used when AttackDistance is non-nil, of course.

2017-05-10
Add AttackDistance option. When set, it behaves as the optimum attack distance. While target range < AttackDistance, the AttackAngle is taken to be 180 - AttackAngle.

2017-04-18
Removed spinner support. Dediblade support remains. Now also supports using side-facing dediblades for yaw. See YawThrottleSpinnerFractions for enabling dediblade support for each axis.

2017-01-18
Added PreferredBroadside option. Default is no preference.

2017-01-14
Improved attack run logic. Added MinAttackTime setting (for use when AttackRuns = true)

2016-12-27
Added dodging of detected missiles (will turn hard port/starboard depending on predicted impact point). Too bad CRAM detections aren't passed to Lua.

2016-07-14
Fix major screw up with my periodic module. It was always running every Update.

2016-06-26
Switched PID implementation over to standard form to match PIDs in-game. Ti = Kp/Ki and Td = Kd/Kp to convert. Use nil Ti to disable integral.
Added support for using forward- (or reverse) facing spinners (including dedi-spinners) for propulsion.

2016-06-21
Been writing and re-writing the terrain and friendly avoidance code over the past week. Updated to the current version, which I'm pretty happy with.
interisting is the evasing fixed movment patterns or depents on shell/missel detection?

and my secound question can it handle vector thrust(yaw) (would it a big deal too impliment?)?
(2016-06-14 06:13 PM)khkpck Wrote: [ -> ]interisting is the evasing fixed movment patterns or depents on shell/missel detection?

It's based on Perlin noise so it's just pseudo-random and not based on any outside influence. Maybe if/once we can detect CRAM shells in flight (I hear that's on the horizon), it will be worth attempting to dodge them.

(2016-06-14 06:13 PM)khkpck Wrote: [ -> ]and my secound question can it handle vector thrust(yaw) (would it a big deal too impliment?)?

It only controls the ship via yaw (propellers or rudders) at this point. I'm still an FtD noob, so I'm not sure how different it would be to support jets.
not jets i have an "Gondola" propulsion system for my ships

i fact heliblades on spinblocks in first place to achive what u have achived with the PID

but i like the look of it very much

added a small example craft

not working that well on tis small thing
yiggels a lot at the beginning


a maybe better example is my corvette

http://www.fromthedepthsgame.com/forum/s...?tid=20043

the script isent my work
its from madwand
So I've been using this on all my ships. Lately (your last few versions...but it's possible I just never noticed it happening) I am having trouble with it locking me out of steering. Particularly if I have bow thrusters (or aft thrusters, since it seems the ones towards the rear in reverse mode are the culprits) or with a spinblock rudder. It's possible that it is because I don't know how to tweak PID settings properly, although your defaults for ship seem to work fine for anything of mine to date. It is fine if the AI is driving, it's just if I want to make a manual course correction (like steer away from land which the AI seems to love to hug).

Also being as whenever I see you update, I update (so I am always poking params), I been meaning to ask what is the usefulness of the return to origin? Is that good for adventure mode, cause I've found it a major annoyance in campaign and always shut it off.

Also got a question about missiles, but I'll put it in the missiles thread.

Finally (and it doesn't have a thread I know of so will ask here) was trying to play with the gunship AI today (regular gunship not quad, quad is working just fine) and I could not get it to behave at all. Granted the SS Shadowcat was obviously designed to fly around in circles (big left facing thruster, no counterpart on right) but even after modifying it heavily for flight and ripping out all the ACB, I just cannot get it to do anything but hover.
(2017-01-10 06:11 AM)Krougal Wrote: [ -> ]...

I've never really noticed... but then again, I don't try to manually steer. I've tested it with the current version and a version from December, and they both seem equally difficult to control when in "combat" mode. You're basically just fighting with the script for control. (The ship I tested it with has bow thrusters and a rudder.)

Does steering/propulsion work normally when the AI is "off" or "on"?

Also the fact that you have to steer it away from land is a bit troubling. Does that happen often? I mean, that's like the primary reason I wrote this script because otherwise I'd still be using KuramaFox's boat AI. Wink

ReturnToOrigin is pretty useful (for non-flagship ships) in adventure mode. It's basically "return to my fleet waypoint." If you fleet up your ships, they will attempt to keep their relative positions even as the flagship moves.

As for the flagship itself, if ReturnToOrigin is enabled, it will attempt to head to the fleet waypoint on the strategic map ("M" map), which is only available in campaign mode.

One last note about ReturnToOrigin... if it's enabled, then the script never gives up control in "combat" mode, not even when out of combat.

Though even with it off, it still fights you a bit because the avoidance code is still running. It's always attempting to go straight (bearing 0). I might have to fix that (by removing it) since it seems more trouble than it's worth.

As for the gunship script... Lua control of jets is really wonky because the game tries to keep thrust balanced. So basically the jets have to line up with the CoM and if not, there should be a counter jet at the opposite side of the CoM. It's the main reason I prefer quadcopters since all I have to worry about forward/yaw (or for gunships: forward/reverse/lateral) as far as jets are concerned.

But I took a look at the SS Shadowcat, slapped on jets here and there (so it has full 6 degrees of motion), removed that big side-facing jet, and then moved the large jet at the rear down 1 block (to line it up with the CoM).

It seems to work with my gunship script now... but you might have better luck with Draba's. Wink

Edit: Actually, "work" is subjective. It's so unbalanced (because the heliblades are high up while the jets are down low) that it does some hilarious things when trying to dodge missiles.
(2017-01-10 08:10 AM)ZerothAngel Wrote: [ -> ]
(2017-01-10 06:11 AM)Krougal Wrote: [ -> ]...
...

Yeah, I typically let the AI drive too. Mostly testing (course I think I spend 90% designer, 10% campaign) For the most part my corvette is fine with the script AI turned off, although the script does keep it more stable (I have a hydrofoil in the rear to pitch the nose up for speed, and if I am not careful it'll flip, even with ACB limiter) my 1st gen frigate and destroyer the spinblock rudders work fine with AI off or AI driving, but the wierdness I see now makes perfect sense since you said you've got them tying to head north (my sub, which works perfectly otherwise with your sub AI does that too, with just normal rudder; here I thought I had done something wrong lol).

As far as coasts, the thing seems to be that Eriwick is a big cliff, the water is fairly deep even right next to the shoreline. So the ships will get themselves into trouble easily. For the most part it does well, just sooner or later gets into a place it can't turn. I have been tinkering with adjusting the look ahead settings out further, I also don't know how to figure out the turning radius really. The sub also scrapes itself on the bottom in undersea canyons fairly often it seems. I've got it set false 20 in both modes, which should be plenty, considering it is only 9 meters H/W. Generally on the edge though. I don't know if it's possible to add some padding on the sides, but that would probably resolve any issues. I've been conservative about upping look ahead time and angles, haven't tried anything extreme since I'm guessing it would impact script performance badly.

I'll have to try the origin again, would be nice to keep fleet formations. I thought it was sending them all back to where they were spawned, cause they weren't going to waypoints, but then I've learned since about the quirks of the E/M/N views and also realized that at the end of a battle the ships will go back to where they were when it started before going back to following normal waypoint orders so some things are just unavoidable vanilla behavior, and I may have been misinterpreting what I was observing.

The Shadowcat with Draba's script will fly, but winds up upside down in the water quickly and constantly. I wonder too if the 2 rotors being opposite directions messes the scripts up? I really have had little success with aircraft, other than your quadcopters, which really other than the lack of a fast interceptor/something to draw fire away from them have served me well. The SS Falcon and X-30 I've been playing around with and they seem decent (for small/cheap) and the odd design from the workshop. Thanks for taking a look at it, hopefully I can learn something from your edits.

Edit - Is bearing relative or absolute in regard to scripts? So like 80 deg attack angle, is that +/- 80 (80 or 280) or strictly 80? I'm guessing it is the later, being as that seems to be part of the running aground issue, where I will try to turn the ship out to open water and make it turn the other side, but soon as I relinquish control, there we are hugging the coast again.
(2017-01-10 07:32 PM)Krougal Wrote: [ -> ]Yeah, I typically let the AI drive too. Mostly testing (course I think I spend 90% designer, 10% campaign) For the most part my corvette is fine with the script AI turned off, although the script does keep it more stable (I have a hydrofoil in the rear to pitch the nose up for speed, and if I am not careful it'll flip, even with ACB limiter) my 1st gen frigate and destroyer the spinblock rudders work fine with AI off or AI driving, but the wierdness I see now makes perfect sense since you said you've got them tying to head north (my sub, which works perfectly otherwise with your sub AI does that too, with just normal rudder; here I thought I had done something wrong lol).

Just FYI, switching the AI to anything but "combat" just disables the combat behavior of my scripts. Typically, like for the naval-ai, it will simply relinquish yaw & propulsion control.

Things like depth/altitude control and pitch/roll stabilization are always active, regardless of AI mode.

And I should have qualified as relative bearing 0, meaning it will try to maintain its current course (not head due north). It's still a bit weird to me, since if the AI has no course corrections, it shouldn't be sending yaw commands... so maybe there's a bug there in my script or there's some odd interaction with the API method to suppress the default AI.

(2017-01-10 07:32 PM)Krougal Wrote: [ -> ]As far as coasts, the thing seems to be that Eriwick is a big cliff, the water is fairly deep even right next to the shoreline. So the ships will get themselves into trouble easily. For the most part it does well, just sooner or later gets into a place it can't turn. I have been tinkering with adjusting the look ahead settings out further, I also don't know how to figure out the turning radius really. The sub also scrapes itself on the bottom in undersea canyons fairly often it seems. I've got it set false 20 in both modes, which should be plenty, considering it is only 9 meters H/W. Generally on the edge though. I don't know if it's possible to add some padding on the sides, but that would probably resolve any issues. I've been conservative about upping look ahead time and angles, haven't tried anything extreme since I'm guessing it would impact script performance badly.

Give it a try anyway and give it more padding, by either increasing the look-ahead times (there are 2 settings -- one for depth/altitude, the other for 2D movement). Or by increasing the ClearanceFactor (which affects H/W/L... but only for 2D avoidance).

But yeah, I did all my development & testing in those narrow channels 2-3 grids east of the starting position. It was never perfect, but always good enough.

And a lot of it will be a function of turning radius (or for subs, vertical speed).

(2017-01-10 07:32 PM)Krougal Wrote: [ -> ]The Shadowcat with Draba's script will fly, but winds up upside down in the water quickly and constantly. I wonder too if the 2 rotors being opposite directions messes the scripts up? I really have had little success with aircraft, other than your quadcopters, which really other than the lack of a fast interceptor/something to draw fire away from them have served me well. The SS Falcon and X-30 I've been playing around with and they seem decent (for small/cheap) and the odd design from the workshop. Thanks for taking a look at it, hopefully I can learn something from your edits.

On my gunship.lua script at least, it assumes "always up" on dediblades is 1. So any positive rotation generates lift (DediBladesAlwaysUp option). It works on the Shadowcat because that's the way the blades are set up by default (even the counter-rotating blade).

But I think the main problem with the Shadowcat is that the dediblades are too high above the CoM. They act as a sort of pivot, so when forward/reverse/lateral thrust is applied against the CoM, it causes it to pitch/roll wildly out of control. Even adding downward-facing jets (for pitch/roll control) hardly helps.

Anyway, another cheap aircraft too look at is the SS Kitakami's drone. I only played with it before the detection patch and I haven't looked at it recently, but that's the ship my gunship script was originally written for... and the basis of sorts for all my quadcopters.
Bumping the padding up to 1.25, the look ahead angle to 40 and the look ahead time to 30 for both sections on the submarine AI seems to do the trick. At least for my little sub. I probably have way too many hydrofoils in it, but I'm not really sure if that's good/bad/indifferent. Now to work on my ships. And try the helicopter. And finish updating missile configs. Also learning to love the return to origin. And the shield manager (still need to try that thing for lasershields too, or hope Nick fixes it before I go up against LH). Not sure why either gave me trouble before.
Is AttackAngle the relative bearing of the target to attempt to stay at? I'm just giving this a go - I've set it up so it should make attack runs between 2500m & 2000m & AttackAngle of 80, but it's just turning nose on to target & then turning almost fully away at about 1500m. All PID controllers on the boat including in the script are set up as PD, so it's not an integer windup issue unless your script is sneaking something in ( I haven't deconstructed it much ). I think there is definitely something amiss in the steering given every sec or so it has a large judder like it's flapping the rudders. If it was constant I'd say it was the yaw pid, but it's irregularity & the lack of integral controller is a bit of a head-scratcher. I much prefer PID forms where you set Kp/Ki/Kd individually, especially for discrete systems like this ( dt here is fixed & guaranteed ).
Pages: 1 2 3 4 5
Reference URL's