def OnMove(self, e): row, col = hexlib.pixel_to_hexcoords(self.GetVirtualPosition(e.GetPosition()), self.currentmap.width, self.currentmap.height) #row, col = max(0, row), max(0, col) height, width = self.currentmap.terrain.shape if 0 <= row < height and 0 <= col < width: if self.currentmap.terrain[row, col] > 0: print "BOARD:", self.currentmap.terrain[row, col]
def OnMove(self, e): row, col = hexlib.pixel_to_hexcoords( self.GetVirtualPosition(e.GetPosition()), self.currentmap.width, self.currentmap.height ) # row, col = max(0, row), max(0, col) height, width = self.currentmap.terrain.shape if 0 <= row < height and 0 <= col < width: if self.currentmap.terrain[row, col] > 0: print "BOARD:", self.currentmap.terrain[row, col]
def OnLeftUp(self, e): if self.battle_result_panel.IsShown(): return row, col = hexlib.pixel_to_hexcoords(self.GetVirtualPosition(e.GetPosition()), self.currentmap.width, self.currentmap.height) if self.mode == UNSELECTED: unit_type, unit_color, unit_health = self.currentmap.board[:, row, col] if not unit_type: return unit_class = units.type[unit_type].unitclass movement_cost = units.unitclass2movementcost[unit_class] movecost = movement_cost[self.currentmap.terrain] ## do not move to blocked tiles movecost[(self.currentmap.board[1] != 0) & (self.currentmap.board[1] != unit_color)] = 888 zoc = self.currentmap.zoc(unit_class, unit_color) #movecost *= - ((zoc * 2) - 1) print movecost visited = np.ones(self.currentmap.terrain.shape, dtype=np.int64) * -1 can_move = units.type[unit_type].movementpoints[0] t = time.time() reachable = aux_functions.find_paths(visited, zoc, movecost, row, col, can_move) print (time.time() - t) * 1000.0 self.overlays[((reachable == -1) & (self.currentmap.terrain > 0)) | (self.currentmap.board[0] > 0)] |= SHADED self.overlays[row, col] = BLUE_RING self.original_board = self.currentmap.board.copy() self.mode = MOVING self.selectedTile = (row, col) self.sourceTile = (row, col) ## flip value #overlays[row, col] ^= 1 elif self.mode == MOVING: row, col = hexlib.pixel_to_hexcoords(self.GetVirtualPosition(e.GetPosition()), self.currentmap.width, self.currentmap.height) source_row, source_col = self.sourceTile print "DISTANCE:", getDistance(row, col, source_row, source_col) ## deselect if (row, col) == self.selectedTile or self.overlays[row, col] != 0: print "self.overlays[row, col] = ", self.overlays[row, col] self.selectedTile = None self.sourceTile = None self.mode = UNSELECTED self.arrows = [] self.overlays[:] = 0 else: # move unit source_row, source_col = self.selectedTile self.currentmap.board[:, row, col] = self.currentmap.board[:, source_row, source_col] self.currentmap.board[:, source_row, source_col] = 0 self.selectedTile = row, col self.mode = MOVING_CONFIRM ## shade everything but target tile self.overlays[self.currentmap.terrain > 0] |= SHADED self.overlays[row, col] = 0 source_coords = hexlib.hexcoords_to_pixel(self.sourceTile, self.currentmap.width, self.currentmap.height) target_coords = hexlib.hexcoords_to_pixel((row, col), self.currentmap.width, self.currentmap.height) self.arrows = [(source_coords, target_coords, 5, "#80b3ff")] elif self.mode == MOVING_CONFIRM: row, col = hexlib.pixel_to_hexcoords(self.GetVirtualPosition(e.GetPosition()), self.currentmap.width, self.currentmap.height) ## unselect if (row, col) != self.selectedTile: ## todo: moved units must be shaded self.overlays[:] = 0 ## move unit back source_row, source_col = self.sourceTile target_row, target_col = self.selectedTile self.currentmap.board[:, source_row, source_col] = self.currentmap.board[:, target_row, target_col] self.currentmap.board[:, target_row, target_col] = 0 self.selectedTile = None self.mode = UNSELECTED self.arrows = [] else: # move unit source_row, source_col = self.sourceTile self.moveUnit((source_row, source_col), (row, col)) self.mode = ATTACKING ## attack overlay self.overlays[self.currentmap.terrain > 0] |= SHADED unit_type, unit_color, unit_health = self.currentmap.board[:, row, col] min_range, max_range = units.type[unit_type].attackrange attackable = sum([hexlib.rings(self.currentmap.terrain.shape, row, col, x) for x in range(min_range, max_range + 1)]) print "attackable classes:", np.arange(8)[np.array(units.type[unit_type].power) > 0] self.overlays[(attackable > 0) & (self.currentmap.board[1, :, :] > 0) & (self.currentmap.board[1, :, :] != unit_color)] = RED_RING elif self.mode == ATTACKING: if self.selectedTile == (row, col): dlg = YesNoDialog(self) dlg.Show() return defending_row, defending_col = hexlib.pixel_to_hexcoords(self.GetVirtualPosition(e.GetPosition()), self.currentmap.width, self.currentmap.height) attacking_row, attacking_col = self.selectedTile defending_type, defending_color, defending_health = self.currentmap.board[:, defending_row, defending_col] attacking_type, attacking_color, attacking_health = self.currentmap.board[:, attacking_row, attacking_col] attacking_terrain = self.currentmap.terrain[attacking_row, attacking_col] defending_terrain = self.currentmap.terrain[defending_row, defending_col] #print attacking_type, attacking_color, attacking_health #print defending_type, defending_color, defending_health attacking_left, defending_left = battle.evaluate(attacking_type, attacking_health, attacking_terrain, defending_type, defending_health, defending_terrain) self.red_counters[attacking_row, attacking_col] = attacking_health - attacking_left self.red_counters[defending_row, defending_col] = defending_health - defending_left #self.circles.append([hexlib.hexcoords_to_pixel((defending_row, defending_col), self.currentmap.width, self.currentmap.height), "-5"]) #self.circles.append([hexlib.hexcoords_to_pixel((attacking_row, attacking_col), self.currentmap.width, self.currentmap.height), "X"]) self.battle_result_panel.showBattleResult(attacking_color, attacking_type, attacking_health - attacking_left, attacking_left, defending_color, defending_type, defending_health - defending_left, defending_left) self.RedrawMap() self.UpdateDrawing()
def OnLeftUp(self, e): if self.battle_result_panel.IsShown(): return row, col = hexlib.pixel_to_hexcoords( self.GetVirtualPosition(e.GetPosition()), self.currentmap.width, self.currentmap.height ) if self.mode == UNSELECTED: unit_type, unit_color, unit_health = self.currentmap.board[:, row, col] if not unit_type: return unit_class = units.type[unit_type].unitclass movement_cost = units.unitclass2movementcost[unit_class] movecost = movement_cost[self.currentmap.terrain] ## do not move to blocked tiles movecost[(self.currentmap.board[1] != 0) & (self.currentmap.board[1] != unit_color)] = 888 zoc = self.currentmap.zoc(unit_class, unit_color) # movecost *= - ((zoc * 2) - 1) print movecost visited = np.ones(self.currentmap.terrain.shape, dtype=np.int64) * -1 can_move = units.type[unit_type].movementpoints[0] t = time.time() reachable = aux_functions.find_paths(visited, zoc, movecost, row, col, can_move) print (time.time() - t) * 1000.0 self.overlays[ ((reachable == -1) & (self.currentmap.terrain > 0)) | (self.currentmap.board[0] > 0) ] |= SHADED self.overlays[row, col] = BLUE_RING self.original_board = self.currentmap.board.copy() self.mode = MOVING self.selectedTile = (row, col) self.sourceTile = (row, col) ## flip value # overlays[row, col] ^= 1 elif self.mode == MOVING: row, col = hexlib.pixel_to_hexcoords( self.GetVirtualPosition(e.GetPosition()), self.currentmap.width, self.currentmap.height ) source_row, source_col = self.sourceTile print "DISTANCE:", getDistance(row, col, source_row, source_col) ## deselect if (row, col) == self.selectedTile or self.overlays[row, col] != 0: print "self.overlays[row, col] = ", self.overlays[row, col] self.selectedTile = None self.sourceTile = None self.mode = UNSELECTED self.arrows = [] self.overlays[:] = 0 else: # move unit source_row, source_col = self.selectedTile self.currentmap.board[:, row, col] = self.currentmap.board[:, source_row, source_col] self.currentmap.board[:, source_row, source_col] = 0 self.selectedTile = row, col self.mode = MOVING_CONFIRM ## shade everything but target tile self.overlays[self.currentmap.terrain > 0] |= SHADED self.overlays[row, col] = 0 source_coords = hexlib.hexcoords_to_pixel( self.sourceTile, self.currentmap.width, self.currentmap.height ) target_coords = hexlib.hexcoords_to_pixel((row, col), self.currentmap.width, self.currentmap.height) self.arrows = [(source_coords, target_coords, 5, "#80b3ff")] elif self.mode == MOVING_CONFIRM: row, col = hexlib.pixel_to_hexcoords( self.GetVirtualPosition(e.GetPosition()), self.currentmap.width, self.currentmap.height ) ## unselect if (row, col) != self.selectedTile: ## todo: moved units must be shaded self.overlays[:] = 0 ## move unit back source_row, source_col = self.sourceTile target_row, target_col = self.selectedTile self.currentmap.board[:, source_row, source_col] = self.currentmap.board[:, target_row, target_col] self.currentmap.board[:, target_row, target_col] = 0 self.selectedTile = None self.mode = UNSELECTED self.arrows = [] else: # move unit source_row, source_col = self.sourceTile self.moveUnit((source_row, source_col), (row, col)) self.mode = ATTACKING ## attack overlay self.overlays[self.currentmap.terrain > 0] |= SHADED unit_type, unit_color, unit_health = self.currentmap.board[:, row, col] min_range, max_range = units.type[unit_type].attackrange attackable = sum( [hexlib.rings(self.currentmap.terrain.shape, row, col, x) for x in range(min_range, max_range + 1)] ) print "attackable classes:", np.arange(8)[np.array(units.type[unit_type].power) > 0] self.overlays[ (attackable > 0) & (self.currentmap.board[1, :, :] > 0) & (self.currentmap.board[1, :, :] != unit_color) ] = RED_RING elif self.mode == ATTACKING: if self.selectedTile == (row, col): dlg = YesNoDialog(self) dlg.Show() return defending_row, defending_col = hexlib.pixel_to_hexcoords( self.GetVirtualPosition(e.GetPosition()), self.currentmap.width, self.currentmap.height ) attacking_row, attacking_col = self.selectedTile defending_type, defending_color, defending_health = self.currentmap.board[:, defending_row, defending_col] attacking_type, attacking_color, attacking_health = self.currentmap.board[:, attacking_row, attacking_col] attacking_terrain = self.currentmap.terrain[attacking_row, attacking_col] defending_terrain = self.currentmap.terrain[defending_row, defending_col] # print attacking_type, attacking_color, attacking_health # print defending_type, defending_color, defending_health attacking_left, defending_left = battle.evaluate( attacking_type, attacking_health, attacking_terrain, defending_type, defending_health, defending_terrain ) self.red_counters[attacking_row, attacking_col] = attacking_health - attacking_left self.red_counters[defending_row, defending_col] = defending_health - defending_left # self.circles.append([hexlib.hexcoords_to_pixel((defending_row, defending_col), self.currentmap.width, self.currentmap.height), "-5"]) # self.circles.append([hexlib.hexcoords_to_pixel((attacking_row, attacking_col), self.currentmap.width, self.currentmap.height), "X"]) self.battle_result_panel.showBattleResult( attacking_color, attacking_type, attacking_health - attacking_left, attacking_left, defending_color, defending_type, defending_health - defending_left, defending_left, ) self.RedrawMap() self.UpdateDrawing()