Simplify state machines

This commit is contained in:
Tanner Collin 2020-12-03 03:30:54 +00:00
parent 6ba9288fd7
commit 5a7a2f0113
4 changed files with 109 additions and 155 deletions

View File

@ -16,6 +16,8 @@ for name, data in JSON_BLOCKS.items():
for state in data['states']:
BLOCKS[state['id']] = name.replace('minecraft:', '')
BREAK_DISTANCE = 5
AIR = 0
SAND = 66
SINGLE_SNOW = 3921

36
game.py
View File

@ -306,7 +306,7 @@ class Game:
if solution:
solution = list(solution)
self.g.path = solution
self.g.job.state = self.g.job.stop
self.g.job.stop()
print(len(solution))
print(solution)
print(round(time.time() - start, 3), 'seconds')
@ -350,6 +350,11 @@ class Game:
self.g.command_lock = False
return
if text == 'You are now AFK.':
self.g.afk = True
elif text == 'You are no longer AFK.':
self.g.afk = False
match1 = re.match(r'<(\w+)> (.*)', text)
match2 = re.match(r'\[(\w+) -> me] (.*)', text)
if match1:
@ -360,6 +365,9 @@ class Game:
else:
return
if text == 'zzz':
text = '!zzz'
if text.startswith('! '):
text = text[2:]
elif text.startswith('!'):
@ -430,8 +438,9 @@ class Game:
reply += ', I need a bed'
if command == 'stop':
self.g.job.state = self.g.job.stop
self.g.job.stop()
self.g.path = []
self.g.look_at = None
reply = 'ok'
if command == 'inv':
@ -522,6 +531,18 @@ class Game:
print(traceback.format_exc())
reply = 'error'
if command == 'zzz':
if not self.g.afk:
reply = '/afk'
if command == 'print':
try:
reply = str(eval(data))
except BaseException as e:
import traceback
print(traceback.format_exc())
reply = 'Error: {} - {}\n'.format(e.__class__.__name__, e)
if reply:
print(reply)
if private and not reply.startswith('/'):
@ -545,8 +566,14 @@ class Game:
g.item_lock = False
def break_block(self, location):
p = utils.pint(self.g.pos)
if utils.phyp(p, location) > blocks.BREAK_DISTANCE:
return False
bid = self.g.chunks.get_block_at(*location)
if bid != 0:
if bid == 0:
return False
packet = PlayerDiggingPacket()
packet.status = 0
packet.location = location
@ -555,6 +582,7 @@ class Game:
self.g.breaking = location
self.g.break_time = time.time() + utils.break_time(bid, self.g.holding)
return True
def break_finish(self):
packet = PlayerDiggingPacket()
@ -733,8 +761,8 @@ class Game:
obj.item_count = entry.value.item_count
def handle_spawn_living(self, packet):
return
print(packet)
return
def handle_entity_position(self, packet):
obj = self.g.objects.get(packet.entity_id, None)

208
jobs.py
View File

@ -21,8 +21,6 @@ importlib.reload(items)
import data
importlib.reload(data)
BREAK_DISTANCE = 5
class FindGappleStates:
def idle(self):
@ -339,7 +337,7 @@ class GatherSandStates:
self.sand = check
break
if utils.phyp(p, self.sand) > BREAK_DISTANCE:
if utils.phyp(p, self.sand) > blocks.BREAK_DISTANCE:
self.state = self.nav_to_sand
else:
self.state = self.dig_sand
@ -895,7 +893,7 @@ class ClearLeavesStates:
w = self.g.world
p = utils.pint(self.g.pos)
for l in w.find_leaves(p, BREAK_DISTANCE):
for l in w.find_leaves(p, blocks.BREAK_DISTANCE):
self.leaves.append(l)
self.state = self.break_leaves
@ -1008,135 +1006,9 @@ class GrabSaplingStates:
class JobStates:
def idle(self):
return None
return []
def cache_items(self):
s1 = self.cache_items_states
if s1.state == s1.idle:
s1.state = s1.init
elif s1.state == s1.done:
self.state = self.idle
s1.run()
def find_gapple(self):
s1 = self.find_gapple_states
if s1.state == s1.idle:
s1.state = s1.init
elif s1.state == s1.done:
s1.state = s1.tp_to_coord
s1.run()
def gather_sand(self):
s1 = self.gather_sand_states
s2 = self.grab_sand_states
s3 = self.sleep_with_bed_states
s4 = self.cache_items_states
if s1.state == s1.idle:
s1.state = s1.init
s2.state = s2.init
s3.state = s3.init
s4.state = s4.init
elif s1.state == s1.done:
if s2.state != s2.done:
s2.run()
return
if s3.state != s3.done:
s3.run()
return
if s4.state != s4.done:
s4.run()
return
s1.state = s1.init
s2.state = s2.init
s3.state = s3.init
s4.state = s4.init
return
s1.run()
def gather_wood(self):
s1 = self.gather_wood_states
s2 = self.sleep_with_bed_states
s3 = self.cache_items_states
if s1.state == s1.idle:
s1.state = s1.init
s2.state = s2.init
s3.state = s3.init
elif s1.state == s1.done:
if s2.state != s2.done:
s2.run()
return
if s3.state != s3.done:
s3.run()
return
s1.state = s1.init
s2.state = s2.init
s3.state = s3.init
return
s1.run()
def farm_wood(self):
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
s1 = self.gather_wood_states
s2 = self.plant_tree_states
s3 = self.clear_leaves_states
s4 = self.grab_sapling_states
s5 = self.sleep_with_bed_states
s6 = self.cache_items_states
if s1.state == s1.idle:
s1.state = s1.init
s2.state = s2.init
s3.state = s3.init
s4.state = s4.init
s5.state = s5.init
s6.state = s6.init
elif s1.state == s1.done:
if s2.state != s2.done:
s2.run()
return
if s3.state != s3.done:
s3.run()
return
if s4.state != s4.done:
s4.run()
return
if s5.state != s5.done:
s5.run()
return
if s6.state != s6.done:
s6.run()
return
s1.state = s1.init
s2.state = s2.init
s3.state = s3.init
s4.state = s4.init
s5.state = s5.init
s6.state = s6.init
return
s1.run()
def stop(self):
def init_machines(self):
self.gather_wood_states = GatherWoodStates(self.g)
self.gather_sand_states = GatherSandStates(self.g)
self.sleep_with_bed_states = SleepWithBedStates(self.g)
@ -1146,22 +1018,72 @@ class JobStates:
self.clear_leaves_states = ClearLeavesStates(self.g)
self.grab_sapling_states = GrabSaplingStates(self.g)
self.grab_sand_states = GrabSandStates(self.g)
def run_machines(self, machines):
for m in machines:
if m.state == m.idle:
continue
if m.state != m.done:
m.run()
return
# if we went through them all
for m in machines:
m.state = m.init
def gather_sand(self):
machines = [
self.gather_sand_states,
self.grab_sand_states,
self.cache_items_states,
self.sleep_with_bed_states,
]
return machines
def cache_items(self):
machines = [
self.cache_items_states,
]
return machines
def find_gapple(self):
machines = [
self.find_gapple_states,
]
return machines
def gather_wood(self):
machines = [
self.gather_wood_states,
self.sleep_with_bed_states,
self.cache_items_states,
]
return machines
def farm_wood(self):
machines = [
self.gather_wood_states,
self.plant_tree_states,
self.clear_leaves_states,
self.grab_sapling_states,
self.sleep_with_bed_states,
self.cache_items_states,
]
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
return machines
def stop(self):
self.init_machines()
self.state = self.idle
def __init__(self, global_state):
self.g = global_state
self.init_machines()
self.state = self.idle
self.prev_state = None
self.gather_wood_states = GatherWoodStates(self.g)
self.gather_sand_states = GatherSandStates(self.g)
self.sleep_with_bed_states = SleepWithBedStates(self.g)
self.cache_items_states = CacheItemsStates(self.g)
self.find_gapple_states = FindGappleStates(self.g)
self.plant_tree_states = PlantTreeStates(self.g)
self.clear_leaves_states = ClearLeavesStates(self.g)
self.grab_sapling_states = GrabSaplingStates(self.g)
self.grab_sand_states = GrabSandStates(self.g)
def tick(self):
self.state()
self.run_machines(self.state())

View File

@ -20,10 +20,12 @@ g.mcdata = False
g.pos = False
g.inv = {}
g.objects = {}
g.mobs = {}
g.window = None
g.job = None
g.correction_count = 0
g.holding = 0
g.afk = False
@app.route('/')
def hello_world():