mobs.txt 3.78 KB

def run_mobs(players, mud):
    def get_att(d):
        att = 0
        if 'd' in d:
            dice = d.split('d')
            for d in range(int(dice[0])):
                att += randrange(int(dice[1])) + 1
        else:
            att = int(d)
        return att
    def calc_att(pid, attacks, bank):
        v_att = []
        att = 0
        for attack in attacks:
            if attack['cost'] < bank:
                v_att.append(attack)
        # Select a random attack
        if len(v_att) > 0:
            attack = v_att[randrange(len(v_att))]
            att = get_att(attack['dmg'])
            mud.send_message(pid, "%s for %d" % (attack['desc'], att,))
            bank -= attack['cost']
        return att, bank 

    def randrange(start, stop=None):
        if 'esp' in sys.platform:
            if start == 1:
                return 0
            if stop is None:
                stop = start
                start = 0
            upper = stop - start
            bits = 0
            pwr2 = 1
            while upper > pwr2:
                pwr2 <<= 1
                bits += 1
            while True:
                from urandom import getrandbits
                r = getrandbits(bits)
                if r < upper:
                    break
            return r + start
        else:
            import random
            return random.randrange(start)

    for pid, player in players.items():
        if not player['name']:
            continue
        room_monsters = utils.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 = utils.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 hp == 0:
                        DEAD = 1
                    att = get_att(monster_template['aa'])
                    players[pid]['hp'] -= att
                    mud.send_message(pid, "You were hit by a %s for %d" % (mon_name, 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(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(pid, monster_template['at'], sta)
                                active_monster['sta'] = sta
                                players[pid]['hp'] -= att

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

run_mobs(players, mud)