Few features I'd like to know how to make

Started by TomaszA2, October 12, 2019, 04:02:48 PM

Previous topic - Next topic

TomaszA2

Okay, now as it works(thanks for this.parent.def.tools...) I've tried to change = 200 to +=20.
Works fine but is there anything that would make this bonus unique to one item instance?
For now every one spawned sword of this type is getting +20 power (to it's three attack types) everytime another is spawned. (including hauling, equiping and dropping, etc.)
For example first one is in one place with 20 damage, second, third and fourth one are spawned somewhere and all of these items are having 80 damage.

LWM

3 approaches I can think of:

1.  find a way to increase damage based on the ThingComp, not on the def.  Then you could have the One True ThingComp that can increase stats.  I have nooooo idea if that's possible, no idea how damage is handled by weapons, etc.  But it would mean the ThingComp could register itself with a GameComp (game component) and then if there's no other one registered, it could decide it's special and make the damage increase happen.

2.  Make it so there can only be one Omnipotent Sword of Slain People
Either by affecting how it's created (quest? store? dunno?) or by making it so anything that's not the Omnipotent Sword of Super Power Fed on Death turns into an ordinary weapon.

This code works, for what it's worth: (I was playing with code to make randomly colored cloth)

namespace LWM.Dyeable {
    public class RandomColoredCloth : ThingWithComps {
        static RandomColoredCloth() { // just setting up my random colored cloth - not relevant to you:
            coloredCloths=new List<ThingDef>();
            coloredCloths.Add(DefDatabase<ThingDef>.GetNamed("RedCloth"));
            coloredCloths.Add(DefDatabase<ThingDef>.GetNamed("BlueCloth"));
        }
        public override void PostMake() {
            this.def=coloredCloths[rng.Next(2)];
            base.PostMake();
        }

        static Random rng=new Random();
        public static List<ThingDef> coloredCloths;
    }
}

Then in the item creation, there's no way to produce blue cloth, but there is a way to produce "randomly colored cloth" that has a (XML) thingClass of RandomColoredCloth.  I have not tried to do this with a comp, but it's possible a ThingComp could also change the parents' definition and things would still work.

3.  Aaaaah, I forget approach 3?  Probably involves knowing how weapons do damage too?

Anyway, hope that helps :)

--LWM

TomaszA2

#17
What if I'd use/create small ID system for this type of weapon? Comp is created only once and ThingDef everytime an object is created?
I was thinking about some simple array in Comp that would check which one is spawned. ThingDef would have just it's own ID. If something is spawned then ThingDef with this one ID would get boost only for itself.
Problems that I noticed about this idea are I'd need to know which one is spawned and I'm not sure if I can make this type of interaction between Comp and ThingDef.
Do you think it's possible without bigger problems?

About your first approach, I'm looking in ThingComp and there is nothing even connected to ,,damage" stats.(power stat of tool inside a weapon) I was thinking before about using PostPreApplyDamage(DamageInfo dinfo, out bool absorbed) and changing DamageInfo before the damage was dealt(probably misunderstood what is this method for) but DamageInfo objects and read-only.
Thing Comp doesn't also contain any class that is connected to something that has Tool class list(not even single Tool class) inside.
ThingComp's parent is ThingWithComps, which has this method:

I am not sure yet and it might be reverse to what I think, I mean Comp created every time object is created and ThingDef one for all. If so I maybe could use above method somehow to call every ThingComp of all this type objects separately. (or no because it would anyway change power of one ,,global" ThingDef which may result in having +x*amount of copies damage bonus)

About the second option. It's nice but it feels for me like escaping from the problem and making solution around of it. Then I may have problem if I'd want to allow few of them possible but also extremaly rare.

Your code gives me an idea I didn't think about before, why not just add item to the game without using xml at all? Am I right it would be then much easier to manage them?
I am yet a little confused and trying to understand your note written under the code fragment.
Anyway, why doesn't ThingWithComps have any ThingDef inside by default? And does creating one really work without any issues? By default are ThingWithComps' not having any representation as an item lying on the ground? (is it that what ThingDef does?)

Approach 3, as I see tools have their ID's. Maybe that's the easiest way if it would work. (but this ID variable is under [unsaved] tag in ILSpy, I am not sure what does it mean and if I could use it as I want)

LWM

Quote from: TomaszA2 on October 20, 2019, 06:58:59 AM
What if I'd use/create small ID system for this type of weapon?
In C# you can create static methods and static variables that are class-wide, so that's a good way to keep track of any of this information.  For example, each SuperWeaponThingComp could check the function

private bool AmITheOne() {
  if (theOne==null) theOne=this;
  return (this==theOne);
}

static SuperWeaponThingComp theOne=null; // would also work as SpecialWeaponThing


Quote
Comp is created only once and ThingDef everytime an object is created?

There's one thingdef object that sits in the defdatabase; each object in the game world has a corresponding Thing.

QuoteI was thinking about some simple array in Comp that would check which one is spawned.

Also easy to do - just have a public static List<YourThingClass> that you register everything to.

QuoteProblems that I noticed about this idea are I'd need to know which one is spawned and I'm not sure if I can make this type of interaction between Comp and ThingDef.
Comp->Thing:  this.parent (you'd probably want this)
Comp->ThingDef: this.parent.def
Thing->Comp:  var comp=this.TryGetComp<MyMagicalThingComp>(); if (comp!=null)...
ThingDef->Comp: difficult.

QuoteAbout your first approach, I'm looking in ThingComp and there is nothing even connected to ,,damage" stats.(power stat of tool inside a weapon) I was thinking before about using PostPreApplyDamage(DamageInfo dinfo, out bool absorbed) and changing DamageInfo before the damage was dealt(probably misunderstood what is this method for) but DamageInfo objects and read-only.

I mean, you can always edit the details of an object - they are pass by reference.  But the ApplyDamage() functions are applying damage to the object not the object applying damage to others.  Check out what CompExplosive is doing, for example.

Quote(or no because it would anyway change power of one ,,global" ThingDef which may result in having +x*amount of copies damage bonus)
There is only one ThingDef.......that's why option #2 might work, because you can manipulate the thingDef directly to change the values, whereas for a ThingComp, you have to have some way to adjust the damage dealt from ThingComps...


QuoteAbout the second option. It's nice but it feels for me like escaping from the problem and making solution around of it. Then I may have problem if I'd want to allow few of them possible but also extremaly rare.
That is exactly a problem with this approach.

QuoteYour code gives me an idea I didn't think about before, why not just add item to the game without using xml at all? Am I right it would be then much easier to manage them?

Aaaaaah.  It's ...difficult.  In theory, yes, but it would need its own thingDef and creating that is ...difficult.

QuoteI am yet a little confused and trying to understand your note written under the code fragment.
I was thinking that it might be possible to use the ThingComp to change the Thing's associated ThingDef (this.parent.def=NewThingDef), but that has its own set of problems ;)  As noted, making your own def on the fly is tricky, and if you want more than one magic sword, probably not an approach that's worth doing...

It sounds like finding a way to have the ThingComp affect damage is ...difficult.  :/

Approaches I might take:
1.  Continue to look for a way to have a ThingComp affect how much damage is dealt.
2.  Find where the damage for a weapon is calculated for the first time and then use Harmony (magic!) to adjust it for your comps (damage isn't calculated ALL the time, so probably minimal impact) (I do enough Harmony magic that this is probably what I'd do)
3.  See if it's possible to change the Tools of your weapon to use your own defs that could check the comps directly.
Oh, and 4.  Post a new thread in the Forum: Anyone know how to change damage dealt from a ThingComp?