spawner.txt 1.44 KB
def spawn_mobs(players):
    from os import listdir
    from utils import get_att
    rooms = listdir('rooms')
    for room in rooms:
        if '_monsters.json' not in room:
            continue
        room_monsters = load_object_from_file('rooms/{}'.format(room))
        for mon_name, monster in room_monsters.items():
            monster_template = load_object_from_file('mobs/{}.json'.format(mon_name))
            if not monster_template:
                continue
            while len(room_monsters[mon_name]['active']) < monster['max']:
                print('Spawning {} in {}'.format(mon_name, room))
                mp = get_att(monster_template['spawn']["mp"])
                hp = get_att(monster_template['spawn']["hp"])
                sta = get_att(monster_template['spawn']["sta"])
                new_active = {
                    "mp": mp, 
                    "maxhp": hp,
                    "hp": hp,
                    "sta": sta,
                    "maxmp": mp,
                    "target": "",
                    "action": "attack",
                    "maxsta": sta}
                room_monsters[mon_name]['active'].append(new_active)
                for pid, pl in players.items():
                    if players[pid]['room'].lower() == room.split('_')[0]:
                        mud.send_message(pid, "a {} arrived".format(mon_name))
        save_object_to_file(room_monsters, 'rooms/{}'.format(room))

spawn_mobs(players)
del spawn_mobs