+
+ @property
+ def player(self):
+ return self.get_thing(self.player_id)
+
+ def new_thing_id(self):
+ if len(self.things) == 0:
+ return 0
+ return self.things[-1].id_ + 1
+
+ def get_map(self, map_pos, create_unfound=True):
+ if not (map_pos in self.maps and
+ self.maps[map_pos].size == self.map_size):
+ if create_unfound:
+ self.maps[map_pos] = Map(self.map_size)
+ for pos in self.maps[map_pos]:
+ self.maps[map_pos][pos] = '.'
+ else:
+ return None
+ return self.maps[map_pos]
+
+ def proceed_to_next_player_turn(self):
+ """Run game world turns until player can decide their next step.
+
+ Iterates through all non-player things, on each step
+ furthering them in their tasks (and letting them decide new
+ ones if they finish). The iteration order is: first all things
+ that come after the player in the world things list, then
+ (after incrementing the world turn) all that come before the
+ player; then the player's .proceed() is run, and if it does
+ not finish his task, the loop starts at the beginning. Once
+ the player's task is finished, or the player is dead, the loop
+ breaks.
+
+ """
+ while True:
+ player_i = self.things.index(self.player)
+ for thing in self.things[player_i+1:]:
+ thing.proceed()
+ self.turn += 1
+ for pos in self.maps[YX(0,0)]:
+ if self.maps[YX(0,0)][pos] == '.' and \
+ len(self.things_at_pos((YX(0,0), pos))) == 0 and \
+ self.rand.random() > 0.999:
+ self.add_thing_at('food', (YX(0,0), pos))
+ for thing in self.things[:player_i]:
+ thing.proceed()
+ self.player.proceed(is_AI=False)
+ if self.player.task is None or not self.player_is_alive:
+ break
+
+ def add_thing_at(self, type_, pos):
+ t = self.thing_types[type_](self)
+ t.position = pos
+ self.things += [t]
+ return t
+
+ def make_new_world(self, yx, seed):
+
+ def add_thing_at_random(type_):
+ while True:
+ new_pos = (YX(0,0),
+ YX(self.rand.randint(0, yx.y - 1),
+ self.rand.randint(0, yx.x - 1)))
+ if self.maps[new_pos[0]][new_pos[1]] != '.':
+ continue
+ if len(self.things_at_pos(new_pos)) > 0:
+ continue
+ return self.add_thing_at(type_, new_pos)
+
+ self.things = []
+ self.rand.seed(seed)
+ self.turn = 0
+ self.maps = {}
+ self.map_size = yx
+ map_ = self.get_map(YX(0,0))
+ for pos in map_:
+ map_[pos] = self.rand.choice(('.', '.', '.', '~', 'x'))
+ player = add_thing_at_random('human')
+ self.player_id = player.id_
+ add_thing_at_random('monster')
+ add_thing_at_random('monster')
+ add_thing_at_random('food')
+ add_thing_at_random('food')
+ add_thing_at_random('food')
+ add_thing_at_random('food')
+ return 'success'
+