When working with attack/defense issues regarding creatures (player, npc or monsters) and without requiring source editing always refer to creaturescripts.
Lua:
onStatsChange(cid, attacker, type, combat, value)
Specifically
onStatsChange is the creaturescript you will want to construct your code in and
statschange is the type.
You can see this information simply by looking up
CREATURE_EVENT_STATSCHANGE in creatureevent.cpp.
Define the basic script 1st
Lua:
function onStatsChange(cid, attacker, type, combat, value)
return true
end
Get an understanding of onStatsChange's parameters.
Both
cid and
attacker are creature id's these id's can not be negative numbers.
Both
type and
combat are non negative numbers and correspond to their respective const.
For the
types there are:
C++:
STATSCHANGE_HEALTHGAIN,
STATSCHANGE_HEALTHLOSS,
STATSCHANGE_MANAGAIN,
STATSCHANGE_MANALOSS
And for
combat there is:
C++:
COMBAT_NONE
COMBAT_PHYSICALDAMAGE
COMBAT_ENERGYDAMAGE
COMBAT_EARTHDAMAGE
COMBAT_FIREDAMAGE
COMBAT_UNDEFINEDDAMAGE
COMBAT_LIFEDRAIN
COMBAT_MANADRAIN
COMBAT_HEALING
COMBAT_DROWNDAMAGE
COMBAT_ICEDAMAGE
COMBAT_HOLYDAMAGE
COMBAT_DEATHDAMAGE
Value can be a positive or negative number.
Once you have the script written then you will need to add it to creaturescripts.xml
HTML:
<event type="statschange" name="NameOfTheEvent" event="script" value="scriptOfTheEvent.lua"/>
And then register it with the player in login.lua.
Lua:
registerCreatureEvent(cid, "NameOfTheEvent")
And register the event with the monster by placing this inside its xml file.
HTML:
<script>
<event name="NameOfTheEvent"/>
</script>
Example:
HTML:
<?xml version="1.0" encoding="utf-8"?>
<monster name="Chicken" namedescription="a chicken" race="blood" experience="0" speed="200" manacost="220">
<health now="15" max="15"/>
<look type="111" corpse="6042"/>
<targetchange interval="5000" chance="8"/>
<strategy attack="100" defense="0"/>
<script>
<event name="NameOfTheEvent"/>
</script>
<flags>
<flag summonable="1"/>
<flag attackable="1"/>
<flag hostile="0"/>
<flag illusionable="1"/>
<flag convinceable="1"/>
<flag pushable="1"/>
<flag canpushitems="0"/>
<flag canpushcreatures="0"/>
<flag targetdistance="1"/>
<flag staticattack="90"/>
<flag runonhealth="15"/>
</flags>
<defenses armor="1" defense="2"/>
<voices interval="5000" chance="10">
<voice sentence="gokgoooook"/>
<voice sentence="cluck cluck"/>
</voices>
<loot>
<item id="3976" countmax="3" chance="22500"/><!-- worm -->
<item id="5890" countmax="1" chance="25000"/><!-- chicken feather -->
<item id="2695" chance="1700"/><!-- egg -->
<item id="2666" countmax="2" chance="3250"/><!-- meat -->
</loot>
</monster>
I think this explanation is worth more than 1 script