cast.txt 2.32 KB
import utils

# for now spells can only be cast on the thing you are fighting since I don't have a good lexer to pull
# out targets. This also means that spells are offensive only since you cant cast on 'me'
# TODO: Add proper parsing so you can do: cast fireball on turkey or even better, cast fireball on the first turkey
def cast(id, params, players, mud, command):
    if params == '':
        params = command.strip()
    else:
        params = params.strip()
    if len(params) == 0:
        mud.send_message(id, 'What spell do you want to cast?')
        return True
    spell = players[id]['sp'].get(params)
    if not spell:
        mud.send_message(id, 'You do not appear to know how to cast %s.' % (params,))
        return True
    mp = players[id]['mp']
    if mp > 0 and mp > spell['cost']:
        room_monsters = utils.load_object_from_file('rooms/{}_monsters.json'.format(players[id]['room']))
        if not room_monsters:
            mud.send_message(id, 'There is nothing here to cast that spell on.')
        for mon_name, monster in room_monsters.items():
            monster_template = utils.load_object_from_file('mobs/{}.json'.format(mon_name))
            if not monster_template:
                continue
            fighting = False
            for active_monster_idx, active_monster in enumerate(monster['active']):
                if active_monster['action'] == "attack" and active_monster['target'] == players[id]['name']:
                    fighting = True
                    att, mp = utils.calc_att(mud, id, [], mp, spell)

                    players[id]['mp'] = mp
                    active_monster['hp'] -= att
                    # don't let them off without saving cheating sons of bees
                    utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"]))
                    room_monsters[mon_name]['active'][active_monster_idx] = active_monster
            if not fighting:
                mud.send_message(id, 'You are not currently in combat')
                return True
            
        utils.save_object_to_file(room_monsters, 'rooms/{}_monsters.json'.format(players[id]['room']))
    else:
        mud.send_message(id, 'You do not have enough mana to cast that spell.')

if command is None and cmd is not None:
    command = cmd
cast(id, params.strip(), players, mud, command)
del cast