mobs.txt 3.41 KB

def run_mobs(players, mud):

    from utils import load_object_from_file, save_object_to_file, calc_att, get_att
    for pid, player in players.items():
        if not player['name']:
            continue
        room_monsters = load_object_from_file('rooms/{}_monsters.json'.format(player['room']))
        if not room_monsters:
            continue
        for mon_name, monster in room_monsters.items():
            monster_template = load_object_from_file('mobs/{}.json'.format(mon_name))
            if not monster_template:
                continue
            for active_monster_idx, active_monster in enumerate(monster['active']):
                mp = active_monster['mp']
                hp = active_monster['hp']
                sta = active_monster['sta']
                if mp < active_monster['maxmp']:
                    mp += monster_template['mpr']
                    active_monster['mp'] = mp
                if sta < active_monster['maxsta']:
                    sta += monster_template['star']
                    active_monster['sta'] = sta
                if active_monster['action'] == "attack" and active_monster['target'] == player['name']:
                    if player.get("weapon"):
                        weapon = load_object_from_file('inventory/{}.json'.format(player['weapon']))
                        att = get_att(weapon['damage'])
                        mud.send_message(pid, "Your %s strikes the %s for %d" % (weapon['title'], mon_name, att,))
                    else:
                        att = get_att(player['aa'])
                        mud.send_message(pid, "You hit the %s for %d" % (mon_name, att,))
                    hp -= att
                    if hp == 0:
                        DEAD = 1
                    if active_monster.get("weapon"):
                        weapon = load_object_from_file('inventory/{}.json'.format(active_monster['weapon']))
                        att = get_att(weapon['damage'])
                        mud.send_message(pid, "The %s strikes you with a %s for %d" % (mon_name, weapon['title'], att,))
                    else:
                        att = get_att(monster_template['aa'])
                        mud.send_message(pid, "You were hit by a %s for %d" % (mon_name, att,))
                    players[pid]['hp'] -= att
                    # wait until at least 50% of stamina or mp are regenerated or hp is less than 25%

                    if (hp/active_monster['maxhp'] < 0.25) or (mp > 0 and mp/active_monster['maxmp'] > 0.5) or (sta > 0 and sta/active_monster['maxsta'] > 0.5):
                        magic_cast = False
                        if mp > 0:
                            att, mp = calc_att(mud, pid, monster_template['sp'], mp)
                            active_monster['mp'] = mp
                            players[pid]['hp'] -= att
                            if att > 0:
                                magic_cast = True                            
                        if not magic_cast:
                            if sta > 0:
                                att, sta = calc_att(mud, pid, monster_template['at'], sta)
                                active_monster['sta'] = sta
                                players[pid]['hp'] -= att

                    room_monsters[mon_name]['active'][active_monster_idx] = active_monster
            
        save_object_to_file(room_monsters, 'rooms/{}_monsters.json'.format(player['room']))

run_mobs(players, mud)
del run_mobs