9b22a762 by Barry

Moved several files to a frozen folder that contains files that had to be

moved to the modules folder in the baked in bin so they could be read from
flash instead of ram.
1 parent d809b92a
......@@ -49,7 +49,7 @@ def look(id, mud, players, tokens):
mud.send_message(id, "Exits are: {}".format(", ".join(room_data.get('exits'))))
# send player a message containing the list of exits from this room
if len(room_data.get('inventory').keys()) > 0:
if len(room_data.get('inventory')) > 0:
mud.send_message(id, "Items here: {}".format(", ".join(room_data.get('inventory').keys())))
# send player a message containing the list of players in the room
......
......@@ -3,6 +3,7 @@ def save(id, players, mud):
mud.send_message(id, "Saving...")
utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"]))
mud.send_message(id, "Save complete")
return True
save(id, players, mud)
del save
\ No newline at end of file
......
import json
import sys
if 'esp' in sys.platform:
from urandom import getrandbits
else:
import random
from urandom import getrandbits
def get_color(name):
......@@ -32,25 +28,22 @@ def load_object_from_file(filename):
return None
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:
r = getrandbits(bits)
if r < upper:
break
return r + start
else:
return random.randrange(start)
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:
r = getrandbits(bits)
if r < upper:
break
return r + start
def get_att(d):
att = 0
......
......@@ -6,17 +6,28 @@ MudServer author: Mark Frimston - mfrimston@gmail.com
Micropython port and expansion author: Barry Ruffner - barryruffner@gmail.com
"""
from time import sleep
from sys import platform
if 'esp' in platform:
from gc import collect, mem_free
from machine import Pin
if 'esp' in platform:
collect()
from time import sleep
if 'esp' in platform:
collect()
# import the MUD server class
from mudserver import MudServer
if 'esp' in platform:
collect()
from utils import load_object_from_file, save_object_to_file
if 'esp' in platform:
collect()
from math import floor
if 'esp' in platform:
collect()
import micropython
micropython.mem_info(1)
print('STARTING MUD\r\n\r\n\r\n')
if 'esp' in platform:
......@@ -64,6 +75,8 @@ while True:
except Exception as e:
print('spawner error:')
print(e)
if 'esp' in platform:
collect()
if tick >= 1:
if 'esp' in platform:
print(mem_free())
......@@ -71,8 +84,13 @@ while True:
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)
......@@ -161,6 +179,8 @@ while True:
else:
from commandhandler import CommandHandler
if 'esp' in platform:
collect()
cmd_handler = CommandHandler()
handler_results = cmd_handler.parse(id, command, params, mud, players)
......
......@@ -10,19 +10,12 @@ def run_mobs(players, mud):
if player['sta'] < player['maxsta']:
players[pid]['sta'] += player['star']
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 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
......@@ -34,11 +27,10 @@ def run_mobs(players, mud):
else:
att = get_att(player['aa'])
mud.send_message(pid, "You hit the %s for %d" % (mon_name, att,), color='yellow')
hp -= att
if hp <= 0:
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'])
# TODO: Create a corpse
break
if active_monster.get("weapon"):
weapon = load_object_from_file('inventory/{}.json'.format(active_monster['weapon']))
......@@ -48,13 +40,10 @@ def run_mobs(players, mud):
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
# 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):
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 mp > 0:
att, mp = calc_att(mud, pid, monster_template['sp'], mp)
active_monster['mp'] = mp
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
......
......@@ -14,12 +14,9 @@ BAUDRATE = 115200
folders = ['help', 'rooms', 'inventory', 'commands', 'mobs']
files = [
"commandhandler.py",
"main.py",
"mobs.txt",
"spawner.txt",
"mudserver.py",
"utils.py",
"welcome.txt",
"wifiweb.py",
"defaultplayer.json"
......@@ -44,7 +41,9 @@ with serial.Serial(PORT, BAUDRATE, timeout=1) as ser:
print('Creating folders.')
# we are missing folders so they need to be created.
for folder in folders:
tmp_folders = folders
tmp_folders.append('players')
for folder in tmp_folders:
if folder in root:
continue
print('Creating folder: {}'.format(folder))
......
......@@ -30,4 +30,5 @@ def spawn_mobs(players):
mud.send_message(pid, "a {} arrived".format(mon_name))
save_object_to_file(room_monsters, 'rooms/{}'.format(room))
spawn_mobs(players)
\ No newline at end of file
spawn_mobs(players)
del spawn_mobs
\ No newline at end of file
......