throwItem() and damage?

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
DaggorathMaster
Posts: 37
Joined: Thu Sep 08, 2022 7:29 pm

throwItem() and damage?

Post by DaggorathMaster »

How do I get a script-thrown item to do real damage?

If I do the following, aimed at a monster with 0 protection, it does about 3 damage.

Code: Select all

function test()
	local blade = spawn ("throwing_axe", self.go.level, 3, 29, 3, 0)
	blade.item:throwItem (3, 25)
	blade.projectile:setAttackPower (100)
	blade:setWorldPositionY (1.5)
end
shootProjectile() didn't recognize "throwing_axe", even though those exist in LoG1 where shootProjectile() belongs.
minmay
Posts: 2768
Joined: Mon Sep 23, 2013 2:24 am

Re: throwItem() and damage?

Post by minmay »

There's no easy way to do this, unfortunately. As you've discovered, ProjectileComponent:setAttackPower() only sets the attack power used for hitEffects, which can be useful for fireballs but not for throwing axes.

The easiest method I can think of is as follows.

In your init files, assuming you don't want your projectile to be able to miss or crit:
Define a silent sound effect (use any filename and a volume of 0).
Assuming you don't want your projectile to be able to miss or crit, make a trait with an onComputeCritChance hook that returns -1000 if its level isn't 0 (and returns nil otherwise), and an onComputeAccuracy hook that returns 10000 if its level isn't 0 (and returns nil otherwise). I'll call this trait "throw_hack_trait".

Each time you want to "throw" an object like this:
1. Pick a champion (doesn't really matter which one). Record their current energy, food level, and invisibility duration.
2. Give the item a ThrowAttackComponent if it doesn't already have one, and make it the item's primary action. Make sure the item has a stack size of 1 or isn't stackable.
3. Give the ThrowAttackComponent the attack power you want, multiplied by 1/(1+champion's Throwing skill/5), set its base damage stat to "none", set its cooldown to 0, and make it the item's primary action. Set its attack sound to that silent sound.
4. Put the item in a champion's hand, remove the "double_throw" trait from them if they have it, give them the "throw_hack_trait" trait, and use Champion:attack() to make them throw it.
5. Remove the "throw_hack_trait" trait, put back the original item that was in that champion's hand (if any), return their "double_throw" trait if they had it, return their previous energy and food values, and if they had invisibility, restore that invisibility duration to all four champions.
6. Set the item's world position and world rotation, its newly created ProjectileComponent's speed, etc. to whatever you want.
7. If the item isn't fragile, remember to return the item's original primary action, remove the ThrowAttackComponent if you added it, or if it originally had one, return its attack power, base damage, cooldown, attack sound, etc. to their original values.

Even this isn't great; it will set the ammo autopickup for the champion you use, which you presumably don't want, and of course swapping around items in the champion's hands might break weird onEquipItem/onUnequipItem hooks so if you have weird equip hooks you'll have to change them to account for this.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
DaggorathMaster
Posts: 37
Joined: Thu Sep 08, 2022 7:29 pm

Re: throwItem() and damage?

Post by DaggorathMaster »

Hmm. That sounds uber-hacky.

There is an easy way, though.

I added an onProjectileHit hook. Currently it does damageTile, which is OK for the purpose, but it's the shortcoming of doing it the easy way.
If I needed per-target damage I suppose I'd have to use setHealth(), not below 1, and let the normal hit happen for XP which means it would still show 1-5 damage.

It's for a spell, so it didn't need setup, cleanup, and pickup of inventory projectiles, or strength, or critical.

And the spell is a bit spammy with the projectiles, so counting which ones hit which target is not crucial, and would make it unbalanced compared to the elemental equivalent of the spell.
minmay
Posts: 2768
Joined: Mon Sep 23, 2013 2:24 am

Re: throwItem() and damage?

Post by minmay »

Oh, if you're ok with using damageTile, then you can just use a hitEffect with a TileDamagerComponent, like the fireballs I mentioned in my first post. That will allow you to use ProjectileComponent:setAttackPower() as you originally wanted.
Since your original post used an item projectile I assumed you wanted an item projectile :P
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
DaggorathMaster
Posts: 37
Joined: Thu Sep 08, 2022 7:29 pm

Re: throwItem() and damage?

Post by DaggorathMaster »

I would have liked 1-to-1 hits if it was more straightforward, but it isn't, and this is good enough.
Post Reply