Unique items possible?

Started by Meatbomb, June 20, 2017, 07:03:37 AM

Previous topic - Next topic

Meatbomb

Hello guys.  I am just getting started so this is a very general question as I plan out an idea I would like to mod.

Is it possible to create unique items? Such that there is one available to you at game start, but it is not on the build menus, or in trader stocks, etc?

Is it possible to create items with a specific allowed number?  So you can build only one of them and then that's it? Or build a pair of them and then no more?

jamaicancastle

To your first question: yes. When you create an item you can (through inheritance, most easily) specify whether or not it is craftable, and when you create a building you can specify whether or not it shows up in any of the Architect submenus. You can also use the <tradeability> tag to determine whether traders will sell them, buy them but not sell them, or not deal in them at all. (Some other tags will likewise control what NPCs, if any, can spawn with it.) Once you've closed off other avenues of acquisition, you can either use the in-game scenario editor to start with one, or edit the scenario defs to automatically include it.

To your second question: not easily. You would need to write custom C# code in order to facilitate dynamic construction based on the number that already exist. You could conceivably use the first method to give the player, say, one or two of a special crafting resource, which would serve as a practical limit on their construction, but that supply wouldn't regenerate if they were lost.

Meatbomb

That is very helpful, thank you.

Xnope

#3
C# is probably what you'd need to satisfy both your questions. Several ways to do it, some less elegant than others, and I don't pretend to know the most elegant solution.

However, in addition to limiting generation options in the ThingDef(s) that jamaicancastle describes, I can imagine a Harmony patch could do the trick. Specifically, the patch could be a Postfix() for Verse.PawnGenerator.GeneratePawn(PawnGenerationRequest request) that dynamically changes the generated pawn's inventory/gear to reflect whether or not you want your special item to generate, i.e., if there already exists a certain number of them in that particular savegame.

Slow morning, so I wrote some actual example code to get you started:

using Harmony;
using Verse;
using System.Collections.Generic;

namespace MeatbombMod
{
    [HarmonyPatch(typeof(PawnGenerator), "GeneratePawn", typeof(PawnGenerationRequest))]
    public static class LimitUniqueItemsPatch
    {
private static Dictionary<ThingDef, int> maxNumUniqueThings = new Dictionary<ThingDef, int>();

        static LimitUniqueItemsPatch()
        {
// add all your unique ThingDefs and how many there should be here
maxNumUniqueThings.Add(ThingDefOf.Apparel_Parka, 2);
        }

        public static void Postfix(ref Pawn __result)
        {
// Loop through all of the pawn's gear and inventory,
// check if maxNumUniqueThings contains each item's def,
// if it does check how many of those Things are currently in the savegame
// (number of ways to do this, you might consider putting a cache List of created unique Things somewhere),
// and finally if that number is greater than the number in the Dictionary, remove it from the pawn,
// destroy it, and update the cache List accordingly if you used that approach.
// You might also want to look into regenerating something new to replace the removed thing.
        }

    }
}


I know PawnGenerationRequests can have a validator function attached to them, you could possibly put similar code in said validator function wherever these requests are constructed, but that might be more intensive. Could change the example above to a prefix and alter the request accordingly. I dunno. Best of luck, friend, and welcome to modding.

Edit:
Maybe ignore the part about validator functions in the request. The validator is checked before the pawn's gear/inventory is generated, so I doubt it's a good approach.
My W.I.P. mods:
Carnivale: GitHub | Forum Post
XnopeCore: GitHub

Meatbomb

xnope at the moment you are way way out ahead of me... but I do appreciate your time and attention.

As I am still at super n00b level of awareness and competence I will just file your answer away in "yes possible, after you have learned a shit tonne about modding"  :)

But anyways good for others interested in similar issue, thanks again.