An Example: Scratch Spell
OK, let's take a look at some sample code. This is a fairly
sophisticated example. You should make sure you've gone through
the Wiz Tutorials before
continuing.
This example is a little spell that makes the target do various
mildly embarrassing things every 20 seconds. It works by setting
a repeating timer, and whenever the timer goes off, it makes
the target do something random from a list.
<span class='string'>"""</span><br><span class='keyword'></span>scratch<span class='keyword'></span>.<span class='keyword'></span>py<span class='keyword'></span><br><br><span class='keyword'></span>An<span class='keyword'></span> <span class='keyword'></span>example<span class='keyword'></span> <span class='keyword'></span>spell<span class='keyword'></span>, <span class='keyword'></span>for<span class='keyword'></span> <span class='keyword'></span>demonstrating<span class='keyword'></span> <span class='keyword'></span>logging<span class='keyword'></span>. <span class='keyword'></span>There<span class='keyword'></span> <span class='keyword'></span>is<span class='keyword'></span><br><span class='keyword'></span>no<span class='keyword'></span> <span class='keyword'></span>logging<span class='keyword'></span> <span class='keyword'></span>code<span class='keyword'></span> <span class='keyword'></span>in<span class='keyword'></span> <span class='keyword'></span>this<span class='keyword'></span> <span class='keyword'></span>version<span class='keyword'></span> <span class='keyword'></span>of<span class='keyword'></span> <span class='keyword'></span>the<span class='keyword'></span> <span class='keyword'></span>spell<span class='keyword'></span>.<br><br><span class='keyword'></span>Copyright<span class='keyword'></span> <span class='keyword'></span>2003<span class='keyword'></span> <span class='keyword'></span>Cabochon<span class='keyword'></span> <span class='keyword'></span>Technologies<span class='keyword'></span>, <span class='keyword'></span>Inc<span class='keyword'></span>.<br><span class='keyword'></span>Author<span class='keyword'></span>: <span class='keyword'></span>Steve<span class='keyword'></span> <span class='keyword'></span>Yegge<span class='keyword'></span><br><span class='string'>"""</span><br><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>java<span class='keyword'></span>.<span class='keyword'></span>lang<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>String<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>Timed<span class='keyword'></span>, <span class='keyword'></span>Range<span class='keyword'></span>, <span class='keyword'></span>Kernel<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>classes<span class='keyword'></span>.<span class='keyword'></span>magic<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>Spell<span class='keyword'></span><br><br><span class='keyword'>class</span> <span class='function'>scratch</span>(Spell, Timed):<br><br> <span class='keyword'>def</span> <span class='function'>initialize</span>(self):<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>super__initialize<span class='keyword'></span>()<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setIntProperty<span class='keyword'></span>('<span class='keyword'></span>lore<span class='keyword'></span>', <span class='keyword'></span>1<span class='keyword'></span>)<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setProperty<span class='keyword'></span>('<span class='keyword'></span>short<span class='keyword'></span>', '<span class='keyword'></span>scratch<span class='keyword'></span>')<br><br> <span class='keyword'>def</span> <span class='function'>getArt</span>(self):<br> <span class='keyword'></span>return<span class='keyword'></span> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>INCANTATION<span class='keyword'></span><br><br> <span class='keyword'>def</span> <span class='function'>getElement</span>(self):<br> <span class='keyword'></span>return<span class='keyword'></span> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>SPIRIT<span class='keyword'></span><br><br> <span class='keyword'>def</span> <span class='function'>start</span>(self):<br><span class='comment'> # look for target, or use caster if there isn't one</span><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>target<span class='keyword'></span> = <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>findTargetObject<span class='keyword'></span>(<span class='keyword'></span>1<span class='keyword'></span>)<br> <span class='keyword'></span>if<span class='keyword'></span> (<span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>target<span class='keyword'></span> == <span class='keyword'></span>None<span class='keyword'></span>):<br> <span class='keyword'></span>return<span class='keyword'></span><br><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>timer<span class='keyword'></span> = <span class='keyword'></span>Kernel<span class='keyword'></span>.<span class='keyword'></span>setRepeatingTimer<span class='keyword'></span> ( <span class='keyword'></span>20000<span class='keyword'></span>, <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span> )<br><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>makeDispellable<span class='keyword'></span>()<br><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>getAgent<span class='keyword'></span>().<span class='keyword'></span>message<span class='keyword'></span>('<span class='keyword'></span>You<span class='keyword'></span> <span class='keyword'></span>cast<span class='keyword'></span> <span class='keyword'></span>scratch<span class='keyword'></span> <span class='keyword'></span>on<span class='keyword'></span> ' + <span class='keyword'></span>str<span class='keyword'></span>(<span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>target<span class='keyword'></span>))<br><br> <span class='keyword'>def</span> <span class='function'>timerExpired</span>(self):<br> <span class='keyword'></span>target<span class='keyword'></span> = <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>target<span class='keyword'></span><br> <span class='keyword'></span>roll<span class='keyword'></span> = <span class='keyword'></span>Range<span class='keyword'></span>.<span class='keyword'></span>percent<span class='keyword'></span>()<br> <span class='keyword'></span>pronoun<span class='keyword'></span> = <span class='keyword'></span>target<span class='keyword'></span>.<span class='keyword'></span>getGenderString<span class='keyword'></span>()<br> <span class='keyword'></span>name<span class='keyword'></span> = <span class='keyword'></span>target<span class='keyword'></span>.<span class='keyword'></span>getName<span class='keyword'></span>()<br><br> <span class='keyword'></span>if<span class='keyword'></span> <span class='keyword'></span>roll<span class='keyword'></span> < <span class='keyword'></span>20<span class='keyword'></span>:<br> <span class='keyword'></span>target<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span> ( <span class='string'>"<span class='keyword'></span>You<span class='keyword'></span> <span class='keyword'></span>scratch<span class='keyword'></span> <span class='keyword'></span>yourself<span class='keyword'></span>."</span> )<br> <span class='keyword'></span>target<span class='keyword'></span>.<span class='keyword'></span>broadcast<span class='keyword'></span> ( <span class='keyword'></span>name<span class='keyword'></span> + <span class='string'>" <span class='keyword'></span>scratches<span class='keyword'></span> "</span> +<br> <span class='keyword'></span>pronoun<span class='keyword'></span> + <span class='string'>"<span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span> <span class='keyword'></span>vigorously<span class='keyword'></span>."</span> )<br> <span class='keyword'></span>elif<span class='keyword'></span> <span class='keyword'></span>roll<span class='keyword'></span> < <span class='keyword'></span>40<span class='keyword'></span>:<br> <span class='keyword'></span>target<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span> ( <span class='string'>"<span class='keyword'></span>You<span class='keyword'></span> <span class='keyword'></span>rub<span class='keyword'></span> <span class='keyword'></span>your<span class='keyword'></span> <span class='keyword'></span>nose<span class='keyword'></span>."</span> )<br> <span class='keyword'></span>target<span class='keyword'></span>.<span class='keyword'></span>broadcast<span class='keyword'></span> ( <span class='keyword'></span>name<span class='keyword'></span> + <span class='string'>" <span class='keyword'></span>rubs<span class='keyword'></span> "</span> +<br> <span class='keyword'></span>pronoun<span class='keyword'></span> + <span class='string'>" <span class='keyword'></span>nose<span class='keyword'></span> <span class='keyword'></span>for<span class='keyword'></span> <span class='keyword'></span>a<span class='keyword'></span> <span class='keyword'></span>long<span class='keyword'></span> <span class='keyword'></span>while<span class='keyword'></span>."</span> )<br> <span class='keyword'></span>elif<span class='keyword'></span> <span class='keyword'></span>roll<span class='keyword'></span> < <span class='keyword'></span>60<span class='keyword'></span>:<br> <span class='keyword'></span>target<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span> ( <span class='string'>"<span class='keyword'></span>You<span class='keyword'></span> <span class='keyword'></span>scratch<span class='keyword'></span> <span class='keyword'></span>your<span class='keyword'></span> <span class='keyword'></span>ear<span class='keyword'></span>."</span> )<br> <span class='keyword'></span>target<span class='keyword'></span>.<span class='keyword'></span>broadcast<span class='keyword'></span> ( <span class='keyword'></span>name<span class='keyword'></span> + <span class='string'>" <span class='keyword'></span>sticks<span class='keyword'></span> "</span> +<br> <span class='keyword'></span>pronoun<span class='keyword'></span> + <span class='string'>" <span class='keyword'></span>finger<span class='keyword'></span> <span class='keyword'></span>in<span class='keyword'></span> "</span> + <span class='keyword'></span>pronoun<span class='keyword'></span> +<br> <span class='string'>" <span class='keyword'></span>ear<span class='keyword'></span> <span class='keyword'></span>and<span class='keyword'></span> <span class='keyword'></span>scratches<span class='keyword'></span> <span class='keyword'></span>an<span class='keyword'></span> <span class='keyword'></span>itch<span class='keyword'></span>."</span> )<br> <span class='keyword'></span>elif<span class='keyword'></span> <span class='keyword'></span>roll<span class='keyword'></span> < <span class='keyword'></span>80<span class='keyword'></span>:<br> <span class='keyword'></span>target<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span> ( <span class='string'>"<span class='keyword'></span>You<span class='keyword'></span> <span class='keyword'></span>suddenly<span class='keyword'></span> <span class='keyword'></span>belch<span class='keyword'></span>."</span> )<br> <span class='keyword'></span>target<span class='keyword'></span>.<span class='keyword'></span>broadcast<span class='keyword'></span> ( <span class='keyword'></span>name<span class='keyword'></span> + <span class='string'>" <span class='keyword'></span>belches<span class='keyword'></span> <span class='keyword'></span>loudly<span class='keyword'></span>, "</span> +<br> <span class='string'>"<span class='keyword'></span>and<span class='keyword'></span> <span class='keyword'></span>looks<span class='keyword'></span> <span class='keyword'></span>very<span class='keyword'></span> <span class='keyword'></span>embarrassed<span class='keyword'></span> <span class='keyword'></span>about<span class='keyword'></span> <span class='keyword'></span>it<span class='keyword'></span>."</span> )<br> <span class='keyword'></span>else<span class='keyword'></span>:<br> <span class='keyword'></span>target<span class='keyword'></span>.<span class='keyword'></span>broadcast<span class='keyword'></span> ( <span class='keyword'></span>name<span class='keyword'></span> + <span class='string'>" <span class='keyword'></span>smells<span class='keyword'></span> <span class='keyword'></span>terrible<span class='keyword'></span> <span class='keyword'></span>today<span class='keyword'></span>."</span> )<br><br> <span class='keyword'>def</span> <span class='function'>dispel</span>(self):<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>super__dispel<span class='keyword'></span>()<br> <span class='keyword'></span>Kernel<span class='keyword'></span>.<span class='keyword'></span>killTimer<span class='keyword'></span>(<span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>timer<span class='keyword'></span>)<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>target<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span> ( <span class='string'>"<span class='keyword'></span>You<span class='keyword'></span> <span class='keyword'></span>suddenly<span class='keyword'></span> <span class='keyword'></span>feel<span class='keyword'></span> <span class='keyword'></span>more<span class='keyword'></span> <span class='keyword'></span>sophisticated<span class='keyword'></span>."</span> )<br><br> <span class='keyword'>def</span> <span class='function'>__repr__</span>(self):<br> <span class='keyword'></span>return<span class='keyword'></span> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>getProperty<span class='keyword'></span>('<span class='keyword'></span>short<span class='keyword'></span>')<br><br> <span class='keyword'>def</span> <span class='function'>toString</span>(self):<br> <span class='keyword'></span>return<span class='keyword'></span> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>__repr__<span class='keyword'></span>()<br><br> <span class='keyword'>def</span> <span class='function'>getSpellDescription</span>(self):<br> <span class='keyword'></span>return<span class='keyword'></span> (<span class='string'>"<span class='keyword'></span>This<span class='keyword'></span> "</span> + <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>getDescString<span class='keyword'></span>() +<br> <span class='string'>" <span class='keyword'></span>lowers<span class='keyword'></span> <span class='keyword'></span>the<span class='keyword'></span> <span class='keyword'></span>target<span class='keyword'></span> <span class='keyword'></span>several<span class='keyword'></span> <span class='keyword'></span>rungs<span class='keyword'></span> <span class='keyword'></span>on<span class='keyword'></span> <span class='keyword'></span>the<span class='keyword'></span> <span class='keyword'></span>Social<span class='keyword'></span> <span class='keyword'></span>Ladder<span class='keyword'></span>."</span>)
|
First this spell finds a target object. If you type "cast scratch on
muffin", and the player Muffin is standing next to you, then Muffin
will be afflicted with the scratch. If Muffin is not there, YOU will
be afflicted. That's what happens when you pass 1 (i.e, true) to
Spell.getTargetObject().
Then the spell sets a repeating timer to go off every 20 seconds
(20000 milliseconds), and when it goes off, it issues different
messages to the target and to everyone around the target. Not much to
it.
This version of the spell should work correctly, but if you were
writing it from, er, scratch, then you might want to put in some
logging statements to see what's going on inside the spell. Let's see
how to do that, in the next lesson.
|