Jump to content

6 posts in this topic Last Reply

Highlighted Posts

Posted:
Last Online:  
 

A small example how to replace the AI of a custom building asset on startup:

  • In this example the AI extends from PlayerBuildingAI, that means it has basic stats like a construction cost, it can catch fire etc. But of course you can also extend from any other AI (BuildingAI or CommonBuildingAI if you want to start from scratch).
  • The AI in this example displays a counter value in the building info window. The counter is increased after SimulationStep is called 16 times (SimulationStep is called about once every second on normal speed).
  • The mod expects a local building asset with the file name "thing" and asset name "Thing".
  • The AI is replaced before the prefab is initialized. That means there should be no bad side effects.
  • The mod requires Harmony. Just place 0Harmony.dll in the same folder as the mod dll.
  • It is not compatible with Loading Screen Mod. If you want to support LSM and LSM Test, take a look at the source code of Custom Animation Loader.

 

using ColossalFramework.Packaging;
using Harmony;
using ICities;
using UnityEngine;

namespace AiReplace
{
    public class AiReplaceMod : IUserMod
    {
        private const string HarmonyId = "boformer.AiReplace";
        private HarmonyInstance _harmony;

        public string Name => "AI Replace";
        public string Description => "Replaces AI";

        public void OnEnabled()
        {
            if (_harmony != null) return;

            Debug.Log("AiReplace Patching...");
            _harmony = HarmonyInstance.Create(HarmonyId);
            _harmony.PatchAll(GetType().Assembly);
        }

        public void OnDisabled()
        {
            _harmony.UnpatchAll(HarmonyId);
            _harmony = null;
            Debug.Log("AiReplace Reverted...");
        }
    }

    public class MyBuildingAI : PlayerBuildingAI
    {
        public override string GetLocalizedStats(ushort buildingID, ref Building data)
        {
            var counter = data.m_customBuffer1;
            return $"Counter: {counter}";
        }

        public override void SimulationStep(ushort buildingID, ref Building data)
        {
            Debug.Log("SimulationStep!");

            if ((SimulationManager.instance.m_currentFrameIndex & 0xFFF) >= 0xF00)
            {
                Debug.Log("SimulationStep was called 16 times!");
                data.m_customBuffer1++;
            }

            base.SimulationStep(buildingID, ref data);
        }
    }

    // Vanilla Asset Loader
    [HarmonyPatch(typeof(PackageDeserializer), "DeserializeGameObject")]
    public static class PackageAssetPatch
    {
        public static void Postfix(Package package, GameObject __result)
        {
            var prefab = __result.GetComponent<BuildingInfo>();
            if (prefab == null) return;

            Debug.Log($"Instantiate {package.packageName} {prefab.name}");

            // packageName is file name of the .crp file (for local .crp files)
            // prefab name is the "Asset Name" you set in Asset Editor + "_Data"
            if (package.packageName == "thing" && prefab.name == "Thing_Data")
            {
                var oldAi = prefab.gameObject.GetComponent<BuildingAI>();
                UnityEngine.Object.DestroyImmediate(oldAi);

                var newAi = prefab.gameObject.AddComponent<MyBuildingAI>();

                Debug.Log($"AI replaced!");
            }
        }
    }
}

 

 

  • Like 1

Share this post


Link to post
Share on other sites
Posted:
Last Online:  
 

Nice! Maybe it would be even possible to create a mod that would save and load the custom AI libraries directly to/from the assets.

With that you could do for example buildings with working clocks without need to install additional mods.

Share this post


Link to post
Share on other sites
  • Original Poster
  • Posted:
    Last Online:  
     

    Do you think you can find out how to slow down ferries/cargo/passenger ships while the bridge is opening?

    Maybe by adding a custom flag to the ship line?

     

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    I can try it. To be frank, at this moment I have no idea how to do it. I know a bit about car behavior, but with ships you pretty much have to start from the scratch, as there probably never was demand to alter their behavior. I check what's in TMPE.

    Currently I decided to finish the "Smart Intersection Plopper". It is nasty and buggy as hell, it won't probably be a big hit, but I would like to have it off myself. Then this is definitely what I would like to work on.

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    I am sorry for breaking my promise to look at how the ships work! Apparently you managed to find everything out in the meantime. Now, as always, it will be me who will be learning... Good job!

    Share this post


    Link to post
    Share on other sites
  • Original Poster
  • Posted:
    Last Online:  
     
    7 hours ago, Strdate said:

    I am sorry for breaking my promise to look at how the ships work! Apparently you managed to find everything out in the meantime. Now, as always, it will be me who will be learning... Good job!

    Oh there's still a lot of work to do, and there are a lot more possibilities to modify ship behavior (canal locks?) now that I found out how to slow them down!

    If you are interested in the current source (Mod.cs is especially interesting), here it is: https://gist.github.com/boformer/58183421d3d9b7b2428ebf40384bd8ad

    • Thanks 1

    Share this post


    Link to post
    Share on other sites

    Sign In or register to comment...

    To comment in reply, you must be a community member

    Sign In  

    Already have an account? Sign in here.

    Sign In Now

    Create an Account  

    Sign up to join our friendly community. It's easy!  

    Register a New Account


    ×

    Thank You for the Continued Support!

    Simtropolis depends on donations to fund site maintenance costs.
    Without your support, we just would not be in our 24th year online!  You really help make this a great community. *:thumb:

    But we still need your support to stay online. If you're able to, please consider a donation to help us stay up and running. This helps sustain a platform where we can share our community creations for years to come.

    Make a Donation, Get a Gift!

    Expand your city with the best from the Simtropolis Exchange.
    Make a Donation and get one or all three discs today!

    STEX Collections

    By way of a "Thank You" gift, we'd like to send you our STEX Collector's DVD. It's some of the best buildings, lots, maps and mods collected for you over the years. Check out the STEX Collections for more info.

    Each donation helps keep Simtropolis online, open and free!

    Thank you for reading and enjoy the site!

    More About STEX Collections