021a4bc9 by Barry

Moved spawner and mobs to main to ease memory use and installation /

builds.
1 parent c6f62199
import utils
import sys
global_aliases = {
'n': 'north',
......@@ -65,8 +66,7 @@ class CommandHandler(object):
del ldict
return True
except Exception as e:
print('Something happened...')
import sys
print('Command handler exception:')
if 'esp' not in sys.platform:
import traceback
traceback.print_exc()
......
......@@ -9,10 +9,11 @@ Micropython port and expansion author: Barry Ruffner - barryruffner@gmail.com
from time import sleep
from math import floor
from sys import platform
from os import listdir
from mudserver import MudServer
from commandhandler import CommandHandler
from utils import load_object_from_file, save_object_to_file, password_hash
from utils import load_object_from_file, save_object_to_file, password_hash, calc_att, get_att
if 'esp' in platform:
from gc import collect, mem_free
......@@ -50,6 +51,92 @@ tick = 0.0
spawn = 0.0
cmd_handler = CommandHandler()
def spawn_mobs(players):
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))
def run_mobs(players, mud):
for pid, player in players.items():
if not player or not player.get("name") or not player.get('password'):
continue
if player['mp'] < player['maxmp']:
players[pid]['mp'] += player['mpr']
if player['sta'] < player['maxsta']:
players[pid]['sta'] += player['star']
room_monsters = load_object_from_file('rooms/{}_monsters.json'.format(player['room']))
for mon_name, monster in room_monsters.items():
monster_template = load_object_from_file('mobs/{}.json'.format(mon_name))
for active_monster_idx, active_monster in enumerate(monster['active']):
sta = active_monster['sta']
if active_monster['mp'] < active_monster['maxmp']:
active_monster['mp'] += monster_template['mpr']
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,), color='yellow')
else:
att = get_att(player['aa'])
mud.send_message(pid, "You hit the %s for %d" % (mon_name, att,), color='yellow')
active_monster['hp'] -= att
if active_monster['hp'] <= 0:
del room_monsters[mon_name]['active'][active_monster_idx]
mud.send_message(pid, "The %s dies." % (mon_name,), color=['bold', 'blue'])
break
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,), color='magenta')
else:
att = get_att(monster_template['aa'])
mud.send_message(pid, "You were hit by a %s for %d" % (mon_name, att,), color='magenta')
players[pid]['hp'] -= att
if (active_monster['hp']/active_monster['maxhp'] < 0.25) or (active_monster['mp'] > 0 and active_monster['mp']/active_monster['maxmp'] > 0.5) or (sta > 0 and sta/active_monster['maxsta'] > 0.5):
magic_cast = False
if active_monster['mp'] > 0:
att, active_monster['mp'] = calc_att(mud, pid, monster_template['sp'], active_monster['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']))
# main game loop. We loop forever (i.e. until the program is terminated)
while True:
if 'esp' in platform:
......@@ -65,9 +152,7 @@ while True:
if spawn >= 10:
spawn = 0
try:
ldict = {}
with open('spawner.txt', 'r', encoding='utf-8') as f:
exec(f.read(), globals(), ldict)
spawn_mobs(players)
except Exception as e:
print('spawner error:')
print(e)
......@@ -78,18 +163,11 @@ while True:
print(mem_free())
spawn += tick
tick = 0
# try:
ldict = {}
if 'esp' in platform:
print(mem_free())
collect()
with open('mobs.txt', 'r', encoding='utf-8') as f:
exec(f.read(), globals(), ldict)
if 'esp' in platform:
collect()
# except Exception as e:
# print('mob error:')
# print(e)
try:
run_mobs(players, mud)
except Exception as e:
print('mob error:')
print(e)
# 'update' must be called in the loop to keep the game running and give
# us up-to-date information
mud.update()
......
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 or not player.get("name") or not player.get('password'):
continue
if player['mp'] < player['maxmp']:
players[pid]['mp'] += player['mpr']
if player['sta'] < player['maxsta']:
players[pid]['sta'] += player['star']
room_monsters = load_object_from_file('rooms/{}_monsters.json'.format(player['room']))
for mon_name, monster in room_monsters.items():
monster_template = load_object_from_file('mobs/{}.json'.format(mon_name))
for active_monster_idx, active_monster in enumerate(monster['active']):
sta = active_monster['sta']
if active_monster['mp'] < active_monster['maxmp']:
active_monster['mp'] += monster_template['mpr']
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,), color='yellow')
else:
att = get_att(player['aa'])
mud.send_message(pid, "You hit the %s for %d" % (mon_name, att,), color='yellow')
active_monster['hp'] -= att
if active_monster['hp'] <= 0:
del room_monsters[mon_name]['active'][active_monster_idx]
mud.send_message(pid, "The %s dies." % (mon_name,), color=['bold', 'blue'])
break
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,), color='magenta')
else:
att = get_att(monster_template['aa'])
mud.send_message(pid, "You were hit by a %s for %d" % (mon_name, att,), color='magenta')
players[pid]['hp'] -= att
if (active_monster['hp']/active_monster['maxhp'] < 0.25) or (active_monster['mp'] > 0 and active_monster['mp']/active_monster['maxmp'] > 0.5) or (sta > 0 and sta/active_monster['maxsta'] > 0.5):
magic_cast = False
if active_monster['mp'] > 0:
att, active_monster['mp'] = calc_att(mud, pid, monster_template['sp'], active_monster['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
\ No newline at end of file
{"cricket": {"max": 1, "active": [{"hp": 15, "mp": 10, "maxmp": 10, "maxhp": 15, "sta": 10, "maxsta": 10, "target": "", "action": "attack"}]}}
\ No newline at end of file
{"cricket": {"max": 1, "active": [{"mp": 10, "maxhp": 14, "hp": 14, "sta": 10, "maxmp": 10, "target": "", "action": "attack", "maxsta": 10}]}}
\ No newline at end of file
......
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
\ No newline at end of file