Basically I want to create some magical weapons that do special damage to specific monster types. For example, let's create a fabled Magic Sword that does some flaming damage to only undead creatures.
Add a RACE=# tag to the weapons, and they'll do double damage against creatures belonging to the specific race (and yes, undeads already belong to the Undead race in races.dfn, which would be RACE=11).
I think that's a somewhat problematic solution, because, to me, the script lies in the wrong place.
Fundamentally, you're wanting to script the behaviour of the weapon when it is used to attack. So you really want a script that you can attach to an item, not a character.
I've not looked at the code in a bit, so I'm not entirely sure. But I think we need to figure out the events that are really required, and what they trigger for.
Off the top of my head (because I have to go out shortly)
onAttack needs to trigger for both the character and the weapon
onDefend needs to trigger for the target, at least, with maybe support for items as well? (eg the onDefend script decides that something special happens, and potentially triggers the onDefend script for a magic piece of armour?)
onBeginCombat/onEndCombat are fairly self explanatory. onEndCombat may want to pass a flag, though, something like:
onEndCombat( attacker, defender, flag );
where flag
1 == defender died
2 == attacker died
3 == defender fled
4 == attacker fled
Getting all these in together and playing well could be quite an issue, to say the least. The two onAttacks and onDefends would need to be able to communicate (perhaps the onAttack for the weapon does the pretty stuff, and passes back a damage modifier/canceller? ditto for onDefend), I think, and I would suggest that there are three scenarios for that
1) Player has onAttack, item has no script
2) Player has no script, item has onAttack
3) Player and item have onAttack
Only in case 3 can you rely on the player script calling the item one. In the other two, you'd have to handle it in code. Similar thing would happen for onDefend.
onSwing would be an event that determines if an attack lands (ie leads to an onAttack event). This would potentially be like onAttack, attached to both PC and/or item (wrestling vs weapon damage). And a quick after thought, as I'm about to run out the door: you could probably do a triple implementation for onSpellCast as well. For the attacker, for the defender, and for the defender's armour (if any).
Maarc wrote:
onSwing would be an event that determines if an attack lands (ie leads to an onAttack event). This would potentially be like onAttack, attached to both PC and/or item (wrestling vs weapon damage).
Isn't the onSwing event meant as a way to completely override the combat calculations?
Maarc wrote:And a quick after thought, as I'm about to run out the door: you could probably do a triple implementation for onSpellCast as well. For the attacker, for the defender, and for the defender's armour (if any).
With magic moving to JS is this event really needed?
If onSwing is meant to be a complete override, it isn't. Nothing that returns from that call will stop the code from executing, it just gets called every time a character takes a swing. Note that also, it occurs before range checks, so an onSwing event will occur even if you're half the map away.
As for onSpellCast, yes, there's still a need. Sure, magic's get moved out to JS, but that's the behaviour of the spells themselves, by and large, only. You'll still want to know when a character is casting: for NPCs, so you can switch targets and so on, for defender's, so you know when you've been hit by a spell (this would be triggered by the JS code), and on the defender's armour (if relevant and any), so that a magic platemail of grounding can discharge lightning, for instance.
And they were just thoughts off the top of my head. This isn't a comment on how the code is NOW, just some ideas as to how I think it should behave, in a broad sense. It would give enough hooks, hopefully, that all aspects of the combat code can be hooked relevantly with little fuss (for eg, right now, giwo's script would need to be applied to NPCs, not the weapon itself, where it logically should sit in the behaviour of the weapon).
While the JS code I pasted above isn't being fired by the item (and thus you don't have the script associated with the item but the character) it would work well enough.
Basically just put that script as 0 (global) and set the type of the fire weapons to 40 (or 41, I don't recall off the top of my head what random type number I picked).
There's something to be said for using the system as it is.
If you've no objection, giwo, I'll see if I can't dig into the combat event routines and try and rationalise some of it, make life a bit easier, see how feasible the idea I presented is.