def __init__(self, parent, currentmap): self.currentmap = currentmap self.mode = UNSELECTED self.overlays = np.zeros_like(self.currentmap.terrain) self.red_counters = np.ones_like(self.currentmap.terrain) * -1 self.selectedTile = None self.sourceTile = None self.original_board = None self.arrows = [] self.circles = [] width_px, height_px = self.currentmap.width * 32 + 16, self.currentmap.height * 26 + 8 MapPanel.__init__(self, parent, background_tile=wx.Bitmap("logo_background_repeating.png"), size=(width_px, height_px)) ## create buffer with mask self.Buffer = wx.BitmapFromBufferRGBA(width_px, height_px, np.ones((width_px, height_px), np.int32) * int("0xff00ff", 0)) self.battle_result_panel = BattleResultPanel(self) self.battle_result_panel.SetBackgroundColour("WHITE") self.battle_result_panel.Hide() self.RedrawMap() self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) self.Bind(wx.EVT_MOTION, self.OnMove) self.Bind(wx.EVT_SIZE, self.OnResize)
def __init__(self, parent, currentmap): self.currentmap = currentmap self.mode = UNSELECTED self.overlays = np.zeros_like(self.currentmap.terrain) self.red_counters = np.ones_like(self.currentmap.terrain) * -1 self.selectedTile = None self.sourceTile = None self.original_board = None self.arrows = [] self.circles = [] width_px, height_px = self.currentmap.width * 32 + 16, self.currentmap.height * 26 + 8 MapPanel.__init__( self, parent, background_tile=wx.Bitmap("logo_background_repeating.png"), size=(width_px, height_px) ) ## create buffer with mask self.Buffer = wx.BitmapFromBufferRGBA( width_px, height_px, np.ones((width_px, height_px), np.int32) * int("0xff00ff", 0) ) self.battle_result_panel = BattleResultPanel(self) self.battle_result_panel.SetBackgroundColour("WHITE") self.battle_result_panel.Hide() self.RedrawMap() self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) self.Bind(wx.EVT_MOTION, self.OnMove) self.Bind(wx.EVT_SIZE, self.OnResize)
class MapPanelWrapper(MapPanel): def __init__(self, parent, currentmap): self.currentmap = currentmap self.mode = UNSELECTED self.overlays = np.zeros_like(self.currentmap.terrain) self.red_counters = np.ones_like(self.currentmap.terrain) * -1 self.selectedTile = None self.sourceTile = None self.original_board = None self.arrows = [] self.circles = [] width_px, height_px = self.currentmap.width * 32 + 16, self.currentmap.height * 26 + 8 MapPanel.__init__(self, parent, background_tile=wx.Bitmap("logo_background_repeating.png"), size=(width_px, height_px)) ## create buffer with mask self.Buffer = wx.BitmapFromBufferRGBA(width_px, height_px, np.ones((width_px, height_px), np.int32) * int("0xff00ff", 0)) self.battle_result_panel = BattleResultPanel(self) self.battle_result_panel.SetBackgroundColour("WHITE") self.battle_result_panel.Hide() self.RedrawMap() self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) self.Bind(wx.EVT_MOTION, self.OnMove) self.Bind(wx.EVT_SIZE, self.OnResize) def OnResize(self, e): self.battle_result_panel.CentreOnParent() super(MapPanelWrapper, self).OnResize(e) def RedrawMap(self): dc = wx.MemoryDC() dc.SelectObject(self.Buffer) for (rownum, colnum), value in np.ndenumerate(self.currentmap.tiles): if self.currentmap.terrain[rownum, colnum] > 0: self.putImage(dc, "%s.png" % terrain.type[self.currentmap.terrain[rownum, colnum]].picture, rownum, colnum) if self.overlays[rownum, colnum] & BLUE_RING: self.putImage(dc, "selected_border_blue.png", rownum, colnum) if self.overlays[rownum, colnum] & RED_RING: self.putImage(dc, "selected_border_red.png", rownum, colnum) for (rownum, colnum), value in np.ndenumerate(self.currentmap.tiles): unit_id, unit_color, unit_health = self.currentmap.board[:, rownum, colnum] if unit_id > 0: self.putImage(dc, "%s_%s.png" % (colors.type[unit_color].name, units.type[unit_id].picture), rownum, colnum) self.putImage(dc, "counter_%s.png" % unit_health, rownum, colnum) if self.red_counters[rownum, colnum] > -1: self.putImage(dc, "counter_%d_red.png" % self.red_counters[rownum, colnum], rownum, colnum) if self.overlays[rownum, colnum] & SHADED: self.putImage(dc, "selected_overlay.png", rownum, colnum) gc = wx.GraphicsContext.Create(dc) gc.SetAntialiasMode(True) for x in self.arrows: coords1, coords2, width, color = x self.drawArrow(gc, coords1, coords2, width, color) for coords, text in self.circles: x, y = coords gc.SetBrush(wx.Brush("#E60000", wx.SOLID)) #gc.SetPen(wx.TRANSPARENT_PEN) gc.SetPen(wx.Pen(wx.BLACK, 1, wx.SOLID)) gc.DrawRoundedRectangle(x, y, 12, 12, 3) font = wx.Font(6, wx.DEFAULT, wx.NORMAL, wx.BOLD) gc.SetFont(font, wx.WHITE) txtWidth, txtWeight, txtDescent, txtExternalLeading = gc.GetFullTextExtent(text) gc.DrawText(text, int(x + 6 - (txtWidth / 2)), int(y + 6 - (txtWeight / 2))) #gc.DrawText(text, x, y) b = dc.GetAsBitmap() del dc b.SetMaskColour("#FF00FF") self.setInnerBitmap(b) #self.Refresh(eraseBackground=False) self.Update() def drawArrow(self, gc, coords1, coords2, width, color): c1, c2 = np.array(coords1), np.array(coords2) c3 = c1 - c2 l = math.sqrt(c3[0]**2 + c3[1]**2) ## stop 15 pixels short of target c2 = c2 + (c3 / l) * 12 norm = c3 / l * width / 2 ortho = np.array((c3[1], -c3[0])) / l * width / 2 gc.SetBrush(wx.Brush(color, wx.SOLID)) gc.SetPen(wx.Pen("#000000", 1, wx.SOLID)) #gc.SetPen(wx.TRANSPARENT_PEN) gc.DrawLines([c1 - ortho, c1 + ortho, c2 + ortho + (5 * norm), c2 + 3 * ortho + (5 * norm), c2, c2 - 3 * ortho + (5 * norm), c2 - ortho + (5 * norm), c1 - ortho]) # def onMouseMove(self, event): # self.panel.SetFocus() # print event 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 putImage(self, dc, img, row, col): png = wx.Bitmap(img) target_row, target_col = hexlib.cube_to_oddr(col, 0, row) target_col -= math.ceil(self.currentmap.height / 2) - 1 pixel_x, pixel_y = target_col * 32 + (target_row % 2) * 16, target_row * 26 b = dc.DrawBitmap(png, pixel_x, pixel_y, True) #dc.DrawText("%d,%d" % (row, col), pixel_x, pixel_y) return b def onExit(self, e): self.Close(True) def moveUnit(self, (source_row, source_col), (target_row, target_col)): for color in self.currentmap.units: for u in self.currentmap.units[color]: if (u.row, u.col) == (source_row, source_col): u.row, u.col = target_row, target_col
class MapPanelWrapper(MapPanel): def __init__(self, parent, currentmap): self.currentmap = currentmap self.mode = UNSELECTED self.overlays = np.zeros_like(self.currentmap.terrain) self.red_counters = np.ones_like(self.currentmap.terrain) * -1 self.selectedTile = None self.sourceTile = None self.original_board = None self.arrows = [] self.circles = [] width_px, height_px = self.currentmap.width * 32 + 16, self.currentmap.height * 26 + 8 MapPanel.__init__( self, parent, background_tile=wx.Bitmap("logo_background_repeating.png"), size=(width_px, height_px) ) ## create buffer with mask self.Buffer = wx.BitmapFromBufferRGBA( width_px, height_px, np.ones((width_px, height_px), np.int32) * int("0xff00ff", 0) ) self.battle_result_panel = BattleResultPanel(self) self.battle_result_panel.SetBackgroundColour("WHITE") self.battle_result_panel.Hide() self.RedrawMap() self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) self.Bind(wx.EVT_MOTION, self.OnMove) self.Bind(wx.EVT_SIZE, self.OnResize) def OnResize(self, e): self.battle_result_panel.CentreOnParent() super(MapPanelWrapper, self).OnResize(e) def RedrawMap(self): dc = wx.MemoryDC() dc.SelectObject(self.Buffer) for (rownum, colnum), value in np.ndenumerate(self.currentmap.tiles): if self.currentmap.terrain[rownum, colnum] > 0: self.putImage( dc, "%s.png" % terrain.type[self.currentmap.terrain[rownum, colnum]].picture, rownum, colnum ) if self.overlays[rownum, colnum] & BLUE_RING: self.putImage(dc, "selected_border_blue.png", rownum, colnum) if self.overlays[rownum, colnum] & RED_RING: self.putImage(dc, "selected_border_red.png", rownum, colnum) for (rownum, colnum), value in np.ndenumerate(self.currentmap.tiles): unit_id, unit_color, unit_health = self.currentmap.board[:, rownum, colnum] if unit_id > 0: self.putImage( dc, "%s_%s.png" % (colors.type[unit_color].name, units.type[unit_id].picture), rownum, colnum ) self.putImage(dc, "counter_%s.png" % unit_health, rownum, colnum) if self.red_counters[rownum, colnum] > -1: self.putImage(dc, "counter_%d_red.png" % self.red_counters[rownum, colnum], rownum, colnum) if self.overlays[rownum, colnum] & SHADED: self.putImage(dc, "selected_overlay.png", rownum, colnum) gc = wx.GraphicsContext.Create(dc) gc.SetAntialiasMode(True) for x in self.arrows: coords1, coords2, width, color = x self.drawArrow(gc, coords1, coords2, width, color) for coords, text in self.circles: x, y = coords gc.SetBrush(wx.Brush("#E60000", wx.SOLID)) # gc.SetPen(wx.TRANSPARENT_PEN) gc.SetPen(wx.Pen(wx.BLACK, 1, wx.SOLID)) gc.DrawRoundedRectangle(x, y, 12, 12, 3) font = wx.Font(6, wx.DEFAULT, wx.NORMAL, wx.BOLD) gc.SetFont(font, wx.WHITE) txtWidth, txtWeight, txtDescent, txtExternalLeading = gc.GetFullTextExtent(text) gc.DrawText(text, int(x + 6 - (txtWidth / 2)), int(y + 6 - (txtWeight / 2))) # gc.DrawText(text, x, y) b = dc.GetAsBitmap() del dc b.SetMaskColour("#FF00FF") self.setInnerBitmap(b) # self.Refresh(eraseBackground=False) self.Update() def drawArrow(self, gc, coords1, coords2, width, color): c1, c2 = np.array(coords1), np.array(coords2) c3 = c1 - c2 l = math.sqrt(c3[0] ** 2 + c3[1] ** 2) ## stop 15 pixels short of target c2 = c2 + (c3 / l) * 12 norm = c3 / l * width / 2 ortho = np.array((c3[1], -c3[0])) / l * width / 2 gc.SetBrush(wx.Brush(color, wx.SOLID)) gc.SetPen(wx.Pen("#000000", 1, wx.SOLID)) # gc.SetPen(wx.TRANSPARENT_PEN) gc.DrawLines( [ c1 - ortho, c1 + ortho, c2 + ortho + (5 * norm), c2 + 3 * ortho + (5 * norm), c2, c2 - 3 * ortho + (5 * norm), c2 - ortho + (5 * norm), c1 - ortho, ] ) # def onMouseMove(self, event): # self.panel.SetFocus() # print event 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 putImage(self, dc, img, row, col): png = wx.Bitmap(img) target_row, target_col = hexlib.cube_to_oddr(col, 0, row) target_col -= math.ceil(self.currentmap.height / 2) - 1 pixel_x, pixel_y = target_col * 32 + (target_row % 2) * 16, target_row * 26 b = dc.DrawBitmap(png, pixel_x, pixel_y, True) # dc.DrawText("%d,%d" % (row, col), pixel_x, pixel_y) return b def onExit(self, e): self.Close(True) def moveUnit(self, (source_row, source_col), (target_row, target_col)): for color in self.currentmap.units: for u in self.currentmap.units[color]: if (u.row, u.col) == (source_row, source_col): u.row, u.col = target_row, target_col