Simplify state machines

This commit is contained in:
2020-12-03 03:30:54 +00:00
parent 6ba9288fd7
commit 5a7a2f0113
4 changed files with 109 additions and 155 deletions
+40 -12
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,16 +566,23 @@ class Game:
g.item_lock = False
def break_block(self, location):
bid = self.g.chunks.get_block_at(*location)
if bid != 0:
packet = PlayerDiggingPacket()
packet.status = 0
packet.location = location
packet.face = 1
self.g.connection.write_packet(packet)
p = utils.pint(self.g.pos)
if utils.phyp(p, location) > blocks.BREAK_DISTANCE:
return False
self.g.breaking = location
self.g.break_time = time.time() + utils.break_time(bid, self.g.holding)
bid = self.g.chunks.get_block_at(*location)
if bid == 0:
return False
packet = PlayerDiggingPacket()
packet.status = 0
packet.location = location
packet.face = 1
self.g.connection.write_packet(packet)
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)