"""Magic trap:  random magical effects.

Copyright 2000 Cabochon Technologies, Inc.
Author:  Steve Yegge
"""

from wyvern.lib.properties import WalkNotify
from wyvern.lib.classes.traps import AbstractTrap
from wyvern.lib import Range, Kernel
from wyvern.common.config import Wyvern

class magic_trap(AbstractTrap):

    def initialize(self):
        self.super__initialize()
        self.setDefaultCategory("traps")
        self.setDefaultBitmap("magic_trap")
        self.setProperty('short', 'magic trap')
        self.setAnimated(1)
        self.addProperty("nopickup")
        self.setWeight("10 lb")
        self.root = Wyvern.getRootDir()

    def steppedOn(self, monster):
        if not self.isRevealed():
            monster.message("You step into a magic trap!")

        # call superclass
        WalkNotify.steppedOn(self, monster)

        roll = Range.percent()
        if roll < 30:
            self.doMessage(monster)
        elif roll < 60:
            self.castSpell(monster, 'lib/spells/identify.py')
        elif roll < 70:
            monster.message('You feel like someone is helping you.')
            self.castSpell(monster, 'lib/spells/remove_curse.py')
        else:
            self.summonMonsters(monster)

        if Range.randomValue ( 10 ) == 1:
            self.remove()

    def doMessage(self, monster):
        roll = Range.percent()
        if roll < 20:
            monster.message('You shudder for a moment.')
        elif roll < 40:
            monster.message('You suddenly yearn for your distant homeland.')
        elif roll < 60:
            monster.message('A tingle runs up and down your spine.')
        elif roll < 80:
            monster.message('You smell charred flesh.')
        else:
            monster.message('You hear a distant howling.')

    # casts an arbitrary spell
    def castSpell(self, agent, name):
        spell = self.cloneSpell(name)
        spell.setMagicItem(self)
        spell.setTarget(agent)
        spell.start()

    def cloneSpell(self, spell):
        return Kernel.instantiatePyClass(self.root + spell)

    # summons a bunch of monsters and blinds the agent
    def summonMonsters(self, agent):
        if agent.hasProperty('blind'):
            agent.message('You feel hemmed in.')
        else:
            agent.message('You are blinded by a flash of light!')
            self.castSpell(agent, 'lib/spells/blindness.py')

        # figure out number and type of monsters to generate
        num = Range.randomValue(2, 5)
        lvl = agent.getLevel()
        if lvl < 1:  lvl = 1
        if lvl > 18: lvl = 18
        arch = 'random/monster/random_monster' + str(lvl)

        # generate them
        map = self.getMap()
        loc = self.getReferenceLoc()
        for i in range(1, num):
            mon = Kernel.instantiate(arch)
            spot = map.findFreeSpot(loc, mon)
            if spot:
                # don't need to start it; happens automatically
                mon.setMap(map, spot.x, spot.y)

    def toString(self, *args):
        if len(args) > 0:
            return self.super__toString(args[0])
        else:
            return 'magic trap'