Guest User

Untitled

a guest
Feb 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.35 KB | None | 0 0
  1. #NOTE: every cell is just a list.  When empty, that cell-list is [None] (not []).  This way we can pass a reference to a cell around and change the cell-list without having to remember co-ordinates, or have a dedicated cell datastructure.
  2.  
  3. def _fix_overfull_cell(self, cell_list):
  4.     #-TODO: Docstrings?  How are they supposed to be written?
  5.     # takes a reference to a cell's contents list -- so we can change it in this function and grid's referenced version will be changed too
  6.  
  7.     if length(cell_list) > 1:
  8.         #make a list that contains all bots in cell that moved there this turn
  9.         bots_to_move_back = filter(lambda bot:(bot.has_moved == 1), cell) #-TODO:will throw a AttributeError if there's food/wall in this cell.  Might want to raise our own error, and catch the AttributeError if only to have informative crash messages.
  10.  
  11.         for bot in bots_to_move_back:
  12.             self.grid.move(bot, bot.last_position)
  13.             bot.has_moved = -1
  14.             #-TODO replace with setter, raise actionfailed error with bot when set to -1
  15.  
  16.         for bot in bots_to_move_back:
  17.             self._fix_overfull_cell(bot.position) #check the cell into which we just moved them
  18.  
  19. def _is_stationary(self, cell_element):
  20.     if cell_element == 'WALL':
  21.         return True
  22.     elif cell_element == None:
  23.         return False
  24.     elif cell_element.__class__ == #-TODO fill in ref to Food class
  25.         return True
  26.     elif cell_element.__class__ == #-TODO fill in ref to Bot class
  27.         return (not cell_element.get_action_type() == 'move') or (cell_element.has_moved == -1)
  28.         #other team bots will count as stationary; their action is None
  29.         #truth table -- bots that have been unmoved are stationary (has_moved == -1)
  30.         #has_moved == -1 | action == 'move' | not action == 'move' | desired return value
  31.         #    1           |        1         |           0          |     1
  32.         #    0           |        1         |           0          |     0
  33.         #    0           |        0         |           1          |     1
  34.  
  35.     else:
  36.         assert False, "World._is_stationary doesn't recognize cell_element: ", cell_element
  37.  
  38. def _is_valid_attack_target(self, bot, target):
  39.     """Checks if action 'attack' parameters from brain are acceptable
  40.  
  41.    Arguments:
  42.    bot -- the bot trying to take the action
  43.    target -- the cell_entity bot is trying to take the action on
  44.  
  45.    Returns True if target is within range, and is bot
  46.    Returns False otherwise
  47.    """
  48.     try:
  49.         if #-TODO:How to find distance between target and bot?
  50.         #if target is on enemy team and cell_element is within range, return true
  51.     except AttributeError:
  52.         return False
  53.  
  54. def _is_valid_move_target(self, #-TODO: check if input gives illegal distance to move
  55.  
  56. def _is_valid_harvest_target(self, bot, target):
  57.     #-TODO: decide how mining will work
  58.     pass
  59.  
  60. def _is_valid_reproduce_target(self):
  61.     #-TODO: decide how reproduce will work
  62.     pass
  63.  
  64. def _is_valid_message(self, action_params):
  65.     #-TODO: write/design message
  66.     pass
  67.  
  68. #-TODO make this a public method of grid
  69. def grid.move(self, bot, destination_cell_list):
  70.     bot.last_position = bot.position
  71.     bot.position = destination_cell_list
  72.  
  73.     self.add_to_cell(destination_cell_list, bot) #-TODO implement this, remove None when filling empty cell.
  74.     self.remove_from_cell(bot.last_position, bot) #-TODO implement this, add None to empty cells
  75.  
  76. # world logic
  77. # Apply and check action loop
  78. for bot in self._current_team:
  79.     #-TODO we should do this in start-turn or end-turn cleanup.  Just putting it here so we don't forget.
  80.     bot.has_moved = None
  81.     action_type = bot.get_action_type() #-TODO:Change to just get_action, with a tuple
  82.     action_params = bot.get_action_params()
  83.  
  84.     if action_type == 'move':
  85.         dest_cell_list = action_params
  86.         if filter(self._is_valid_move_target(bot.position, ###dest_cell_list element###), dest_cell_list).length > 0: #-TODO:  How to pass functions as arguments, but with one parameter already filled?
  87.         ###The above replaces: if filter(self._is_stationary(), dest_cell_list).length > 0: pass
  88.         #NOTE: Moving into same cell as a bot that has moved or is going to move MUST be allowed, because that bot might have to move forward or backwards.  And we don't want the first bot we iterate over to get precedence.  We want to disallow either one from moving.
  89.             bot.has_moved = -1
  90.         else:
  91.             self.grid.move(bot, dest_cell_list)
  92.             bot.has_moved = 1
  93.     elif action_type == 'attack':
  94.         pass
  95.     elif action_type == 'harvest':
  96.         pass
  97.     elif action_type == 'reproduce':
  98.         pass
  99.     elif action_type == 'send':
  100.         pass
  101.     else:
  102.         #-TODO: send error to bot brain
  103.         pass
  104.  
  105. # Undo illegal moves
  106. for cell in self.grid:
  107.     self._fix_overfull_cell(cell)
  108.  
  109. #-TODO: Cleanup loop.  Set lots of bot properties to None.  action, has_moved, last_position, any others?
  110.  
  111. #-TODO: new public methods/fields:
  112. #BOT
  113.     #bot.position -- is a reference to bot's current cell-list
  114.     #                rename to current_cell
  115.     #bot.last_position -- basically, should just be set to None.  We only use it in the apply/check action loop and the undo illegal move loop
  116.     #                     rename to previous_cell
  117.     #bot.has_moved --   None if action isn't move
  118.     #                   -1 if bot has been moved back or had move fail
  119.     #                   0 if bot hasn't tried to move yet, but intends to
  120.     #                   1 if bot has moved, and hasn't been moved back yet
  121.     #                   should change name to move_state since not boolean; 4 possible values
  122.     #bot.getTeam()
  123.     #mechanism for sending failed action notifications
  124.     # Should maybe replace with getter and setter methods?
  125. #WORLD
  126.     #world.kill_bot(bot), removes bot from player botlist and grid (using grid.remove_from_cell(cellentity))
  127. #GRID
  128.     #grid.add_to_cell(cellentity)
  129.     #grid.remove_from_cell(cellentity)
  130.     #make grid an iterable object, returning each cell in no particular order.
  131.     #make each cell a list of its contents.  If empty, it should be [None] not []
  132. #GLOBAL CONSTANTS
  133.     #has_moved: -1 = DEMOVED, 1 = MOVED, 0 = WILLMOVE
  134.     #bot.actions: MOVE, etc...
  135.     #bot move distance (BOT_MOVE_DISTANCE)
  136.     #bot transmit distance (BOT_SEND_DISTANCE)
Add Comment
Please, Sign In to add comment