Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Barry
/
esp8266-Mud
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
4c1a58d5
authored
2018-05-05 00:42:00 -0700
by
Barry
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Added regneration for magic, stamina. Added handlers for spells and
special attacks for monsters.
1 parent
97404a56
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
100 additions
and
46 deletions
commandhandler.py
commands/go.txt
main.py
mobs.txt
mobs/cricket.json
players/test.json
rooms/tavern_monsters.json
commandhandler.py
View file @
4c1a58d
...
...
@@ -32,25 +32,26 @@ class CommandHandler(object):
locals
()[
'tokens'
]
=
tokens
locals
()[
'next_command'
]
=
None
try
:
if
cmd
in
utils
.
load_object_from_file
(
'rooms/'
+
players
[
id
][
"room"
]
+
'.txt'
)
.
get
(
'exits'
):
params
=
cmd
+
" "
+
params
.
lower
()
.
strip
()
cmd
=
"go"
if
cmd
==
''
:
return
True
ldict
=
locals
()
with
open
(
'commands/{}.txt'
.
format
(
cmd
),
'r'
,
encoding
=
'utf-8'
)
as
f
:
exec
(
f
.
read
(),
globals
(),
ldict
)
if
ldict
[
'next_command'
]
!=
None
:
locals
()[
'tokens'
]
=
[]
tokens
=
[]
with
open
(
'commands/{}.txt'
.
format
(
ldict
[
'next_command'
]),
'r'
,
encoding
=
'utf-8'
)
as
f
:
exec
(
f
.
read
())
# try:
if
cmd
in
utils
.
load_object_from_file
(
'rooms/'
+
players
[
id
][
"room"
]
+
'.txt'
)
.
get
(
'exits'
):
params
=
cmd
+
" "
+
params
.
lower
()
.
strip
()
cmd
=
"go"
if
cmd
==
''
:
return
True
except
Exception
as
e
:
print
(
'Something happened...'
)
print
(
e
)
mud
.
send_message
(
id
,
"Unknown command '{}'"
.
format
(
cmd
))
return
False
command
=
cmd
ldict
=
locals
()
with
open
(
'commands/{}.txt'
.
format
(
cmd
),
'r'
,
encoding
=
'utf-8'
)
as
f
:
exec
(
f
.
read
(),
globals
(),
ldict
)
if
ldict
[
'next_command'
]
!=
None
:
locals
()[
'tokens'
]
=
[]
tokens
=
[]
with
open
(
'commands/{}.txt'
.
format
(
ldict
[
'next_command'
]),
'r'
,
encoding
=
'utf-8'
)
as
f
:
exec
(
f
.
read
())
return
True
# except Exception as e:
# print('Something happened...')
# print(e)
# mud.send_message(id, "Unknown command '{}'".format(cmd))
# return False
...
...
commands/go.txt
View file @
4c1a58d
...
...
@@ -49,4 +49,6 @@ def go(id, params, players, mud, tokens, command):
else:
# send back an 'unknown exit' message
mud.send_message(id, "Unknown exit '{}'".format(params))
if command is None and cmd is not None:
command = cmd
next_command = go(id, params, players, mud, tokens, command)
\ No newline at end of file
...
...
main.py
View file @
4c1a58d
...
...
@@ -102,7 +102,7 @@ while True:
"name"
:
None
,
"room"
:
None
,
"inventory"
:
{},
"prompt"
:
"> "
,
"prompt"
:
"
hp
%
hp mp
%
mp
> "
,
"aliases"
:
{},
"hp"
:
100
,
"mp"
:
100
,
...
...
mobs.txt
View file @
4c1a58d
...
...
@@ -38,30 +38,90 @@ def run_mobs(players, mud):
for pid, player in players.items():
if not player['name']:
continue
print(player)
print('checking actions in room: ' + player['room'])
#
print(player)
#
print('checking actions in room: ' + player['room'])
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():
print(mon_name)
monster_template = utils.load_object_from_file('mobs/{}.json'.format(mon_name))
if not monster_template:
continue
print(monster)
for active_monster in monster['active']:
for active_monster_idx, active_monster in enumerate(monster['active']):
# Recover MP and Stamina
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
active_monster['hp']
== 0:
if
hp
== 0:
DEAD = 1
if "d" in monster_template['aa']:
dice = monster_template['aa'].split('d')
att = 0
for d in range(int(dice[0])):
att += randrange(int(dice[1])
+ 1)
att += randrange(int(dice[1])
) + 1
players[pid]['hp'] -= att
mud.send_message(pid, "You were hit by a %s for %d" % (mon_name, att,))
else:
print(randrange(120))
# 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
# Try magic first
if mp > 0:
valid_spells = []
for spell in monster_template['sp']:
if spell['cost'] < mp:
valid_spells.append(spell)
# Select a random spell
if len(valid_spells) > 0:
spell = valid_spells[randrange(len(valid_spells))]
del valid_spells
att = 0
if 'd' in spell['dmg']:
dice = spell['dmg'].split('d')
for d in range(int(dice[0])):
att += randrange(int(dice[1])) + 1
else:
att = int(spell['dmg'])
active_monster['mp'] -= spell['cost']
players[pid]['hp'] -= att
mud.send_message(pid, "%s for %d" % (spell['desc'], att,))
magic_cast = True
if not magic_cast:
# if mp is exhaused or unavailable switch to stamina actions
if sta > 0:
print(sta)
valid_attacks = []
for attack in monster_template['at']:
if attack['cost'] < sta:
valid_attacks.append(attack)
# Select a random attack
if len(valid_attacks) > 0:
rand_val = randrange(len(valid_attacks))
print("Random attack result: {} Len: {}".format(rand_val, len(valid_attacks)))
attack = valid_attacks[rand_val]
del valid_attacks
att = 0
if 'd' in attack['dmg']:
dice = attack['dmg'].split('d')
for d in range(int(dice[0])):
att += randrange(int(dice[1])) + 1
else:
att = int(attack['dmg'])
active_monster['sta'] -= attack['cost']
players[pid]['hp'] -= att
mud.send_message(pid, "%s for %d" % (attack['desc'], att,))
magic_cast = True
room_monsters[mon_name]['active'][active_monster_idx] = active_monster
utils.save_object_to_file(room_monsters, 'rooms/{}_monsters.json'.format(player['room']))
# {
# "cricket": {
# "max": 1,
...
...
mobs/cricket.json
View file @
4c1a58d
...
...
@@ -2,16 +2,20 @@
"name"
:
"cricket"
,
"desc"
:
"A small green chirping insect with large legs suited for jumping."
,
"aa"
:
"1d2"
,
"mpr"
:
0.25
,
"star"
:
0.4
,
"spawn"
:
{
"hp"
:
"2d2"
,
"mp"
:
"0"
,
"mp"
:
"
1
0"
,
"sta"
:
"10"
},
"say"
:
[
"A cricket quietly chirps"
,
"A cricket moves quietly"
],
"sp"
:
[],
"sp"
:
[
{
"name"
:
"zap"
,
"cost"
:
5
,
"dmg"
:
"2d4"
,
"desc"
:
"The cricket magically sparks lightning by rubbing it's legs together"
}
],
"at"
:
[
{
"name"
:
"kick"
,
"cost"
:
5
,
"dmg"
:
"2d4"
,
"desc"
:
"The cricket kicks with powerful legs"
},
{
"name"
:
"antenna tickle"
,
"cost"
:
2
,
"dmg"
:
"1d3"
,
"desc"
:
"The cricket brushes you with antenna"
}
...
...
players/test.json
View file @
4c1a58d
{
"name"
:
"test"
,
"room"
:
"Outside"
,
"inventory"
:
{
"candle"
:
1
},
"prompt"
:
"%hp> "
,
"aliases"
:
{},
"hp"
:
96
,
"mp"
:
100
,
"sta"
:
10
}
\ No newline at end of file
{
"name"
:
"test"
,
"room"
:
"Tavern"
,
"inventory"
:
{
"candle"
:
1
},
"prompt"
:
"%hp> "
,
"aliases"
:
{},
"hp"
:
953
,
"mp"
:
100
,
"sta"
:
10
}
\ No newline at end of file
...
...
rooms/tavern_monsters.json
View file @
4c1a58d
{
"cricket"
:
{
"max"
:
1
,
"active"
:
[
{
"hp"
:
100
,
"mp"
:
0
,
"sta"
:
10
,
"action"
:
"attack"
,
"target"
:
"test"
}
]
}
}
\ No newline at end of file
{
"cricket"
:
{
"max"
:
1
,
"active"
:
[{
"hp"
:
100
,
"mp"
:
4.0
,
"sta"
:
2.550000000000001
,
"maxhp"
:
100
,
"maxmp"
:
10
,
"maxsta"
:
10
,
"action"
:
"attack"
,
"target"
:
"test"
}]}}
\ No newline at end of file
...
...
Please
register
or
sign in
to post a comment