Fri 21 June 2019
fromthedepths Adventures in Modding Part 2
Well, the Lua method injection framework I mentioned was pretty easy to create. I only ran into one or two complications. I originally thought I could be clever and cache my own reference to the
LuaBinding's constructor, but maybe I was using Harmony wrong because I never seemed to have that reference (it was always
null). So I eventually resorted to using reflection.
Anyway, over the course of a few days, I added methods for:
Getting the terrain or wave height for a point
Getting the max terrain or wave height along a ray
Getting the max terrain height along a ray
Calculating dodge direction like I normally do in my scripts
Calculating friendly avoidance direction
Determining the number of "hits" on the terrain along a given direction
Aside from the wave height thing (which isn't available to Lua, hence I why I modded it in), these were all things I was already doing in Lua. I just modded C# versions that would hopefully be faster than doing it solely from Lua. (As I understand it, most of the slowdown in Lua comes from jumping the Lua/C# barrier... and that comes from many hidden places, such as calling methods or accessing properties of the usual Unity classes, e.g. Vector3. Not to mention the explicit API calls into FtD which sometimes return non-trivial data structures.)
Not much to explain. Simply returns the maximum of terrain or wave height at a given point. Underneath, it uses the same method available to PIDs/breadboards.
I actually ended up not using this once I injected other methods.
GetWaveOrTerrainAltitudeLookingAhead & GetTerrainAltitudeLookingAhead
I needed these for my hover & seabed-following logic. When I dug into it, FtD's "look ahead" was actually pretty simplistic: it only looked at the start point and the end point. Here I thought it was doing raycasting or whatever.
My methods were simply straight translations of what I do in Lua: Given the look-ahead ray, divide it into multiple points using a given "resolution." Then sample the terrain/wave height at those points.
Not very efficient if your resolution is small, but offloading the whole thing to C# has got to be faster.
I calculate a dodge vector very simply: figure out which missile will hit the vehicle (abstracted as a sphere) the soonest, determine where it will hit, then pass that information on the my (Lua) AI. Since there's a decent amount of math (ray-sphere intersection, which is quadratic) and a lot of passing of
MissileWarningInfo classes between C# & Lua, it seemed like a natural fit for C# offloading.
Of course, it meant I had to bring all my math helper stuff in as well, but that was a pretty interesting exercise in itself.
CalculateFriendlyAvoidanceVector & GetTerrainHits
Both of these relate to both friendly avoidance and terrain avoidance. I avoided (hah) digging into how FtD did it and just did straight translations from my original Lua versions.
I have no real plans to release my mod or convert it into a standard FtD mod (if even possible... seems like Harmony would not be something that was allowed). So originally, I branched my scripts into vanilla & modded versions. But I had no real desire to maintain two branches, so I brought them together again and did runtime determination to see if my modded extensions were present.
So if there's ever another public release of my scripts, they will probably be these hybridized versions that (hopefully) still work in vanilla...