Esempio n. 1
0
    def __init__(self,
                 game=None):
        """
        """
        self._game = game

        self._max_move = 12
        self._inertia = 2

        self._name = "Sabertooth Mk I"
        self._pos = ShipPos(x=1,y=1,facing="s",
                            curr_move=self._max_move,momentum=0)

        self._imname = "BaseFighter.png"
        self._texture = None
        self._animations = []

        self._move_cost = {
            "FORWARD" : 1,
            "TURN_LEFT" : 2,
            "TURN_RIGHT" : 2,
            "JINK_LEFT" : 2,
            "JINK_RIGHT" : 2,
            "HARD_LEFT" : 3,
            "HARD_RIGHT" : 3,
            "REVERSE" : 3,
            }

        self._evasion_value = {
            "FORWARD" : 0,
            "TURN_LEFT" : 1,
            "TURN_RIGHT" : 1,
            "JINK_LEFT" : 2,
            "JINK_RIGHT" : 2,
            "HARD_LEFT" : 2,
            "HARD_RIGHT" : 2,
            "REVERSE" : 2,
            }
        
        self._moves_this_turn = []    
        self._hexes_this_turn = set((self._pos.x,self._pos.y))

        self._base_init = 10
        self._move_wagered = 0
        self._current_init = 0

        self._special_this_turn = False
        self._special_max_uses = 3
        self._special_uses = 0    

        self.map_possible_moves()
Esempio n. 2
0
class Ship():
    """
    """

    ### ---------------------------------------------------
    ### CONSTRUCTOR
    ### ---------------------------------------------------

    def __init__(self,
                 game=None):
        """
        """
        self._game = game

        self._max_move = 12
        self._inertia = 2

        self._name = "Sabertooth Mk I"
        self._pos = ShipPos(x=1,y=1,facing="s",
                            curr_move=self._max_move,momentum=0)

        self._imname = "BaseFighter.png"
        self._texture = None
        self._animations = []

        self._move_cost = {
            "FORWARD" : 1,
            "TURN_LEFT" : 2,
            "TURN_RIGHT" : 2,
            "JINK_LEFT" : 2,
            "JINK_RIGHT" : 2,
            "HARD_LEFT" : 3,
            "HARD_RIGHT" : 3,
            "REVERSE" : 3,
            }

        self._evasion_value = {
            "FORWARD" : 0,
            "TURN_LEFT" : 1,
            "TURN_RIGHT" : 1,
            "JINK_LEFT" : 2,
            "JINK_RIGHT" : 2,
            "HARD_LEFT" : 2,
            "HARD_RIGHT" : 2,
            "REVERSE" : 2,
            }
        
        self._moves_this_turn = []    
        self._hexes_this_turn = set((self._pos.x,self._pos.y))

        self._base_init = 10
        self._move_wagered = 0
        self._current_init = 0

        self._special_this_turn = False
        self._special_max_uses = 3
        self._special_uses = 0    

        self.map_possible_moves()

    ### ---------------------------------------------------
    ### INITIATIVE
    ### ---------------------------------------------------

    def start_init_phase(self):
        self._move_wagered = 0
        

    ### ---------------------------------------------------
    ### MOVEMENT
    ### ---------------------------------------------------

    def start_move_phase(self):
        self._moves_this_turn = []    
        self._hexes_this_turn = set((self._pos.x,self._pos.y))
        self._pos.curr_move = self._max_move
        self._special_this_turn = False
        print "Mapping possible moves..."
        map_possible_moves()
    
    def move_valid(self,move_type):
        return self._pos.move_valid(self,move_type)

    def can_use_special(self):
        if self._special_this_turn or \
                self._special_uses >= self._special_max_uses:
            return False
        return True        

    def move(self,move_type):

        # CHECK VALIDITY
        if self.move_valid(move_type) == False:
            return

        # NOTE STARTING POSITION
        start_x = self._pos.x
        start_y = self._pos.y
        start_hex = (start_x, start_y)
        start_facing = self._pos.facing

        # RECORD FOR HISTORY
        self._moves_this_turn.append(move_type)
        self._hexes_this_turn.add((self._pos.x, self._pos.y))

        # GET THE NEW POSITION
        new_pos = self._pos.move(self, move_type)
        self._pos = new_pos

        # CHECK IF THE SHIP MOVED OR TURNED
        if (self._pos.x == start_x) and (self._pos.y == start_y):
            moved = False
        else:
            moved = True

        if (self._pos.facing == start_facing):
            turned = False
        else:
            turned = True

        # START ANIMATION
        end_hex = (self._pos.x, self._pos.y)
        if moved:
            new_anim = ShipAnimation(
                ship=self,
                start_pos=start_hex,
                stop_pos=end_hex,
                csys_pos="HEX",
                start_ang=start_facing,
                stop_ang=start_facing,
                csys_ang="HEX"
                )
            self._animations.append(new_anim)

        if turned:
            new_anim = ShipAnimation(
                ship=self,
                start_pos=end_hex,
                stop_pos=end_hex,
                csys_pos="HEX",
                start_ang=start_facing,
                stop_ang=self._pos.facing,
                csys_ang="HEX",
                )
            self._animations.append(new_anim)

        self._hexes_this_turn.add((self._pos.x, self._pos.y))

    def activate_special(self):
        if self.can_use_special() == False:
            return

        start_hex = (self._pos.x, self._pos.y)
        target_hex = start_hex
        self._hexes_this_turn.add((self._pos.x, self._pos.y))
        for i in range(5):
            target_hex = \
                hex_math.neighbor((target_hex[0], target_hex[1]), self._pos.facing)
            self._hexes_this_turn.add(target_hex)
        self._pos._x = target_hex[0]
        self._pos._y = target_hex[1]
        new_anim = ShipAnimation(
            ship=self,
            start_pos=start_hex,
            stop_pos=target_hex,
            csys_pos="HEX",
            start_ang=self._pos.facing,
            stop_ang=self._pos.facing,
            csys_ang="HEX",
            frac_rate=1./20.
            )
        self._animations.append(new_anim)
        self._special_this_turn = True
        self._special_uses += 1

    ### ---------------------------------------------------
    ### AI AND POSSIBLE PATHS
    ### ---------------------------------------------------

    def map_possible_moves(self):
        """
        Work out all possible moves for the ship from its current
        position and save them (in some kind of graph structure?).
        """
        
        nodes = []
        new_nodes = [MoveNode(pos=self._pos,
                              moves=self._pos.possible_moves(self))]
        while new_nodes != []:
            for node in new_nodes:
                nodes.append(node)
            new_nodes = []
            for node in nodes:                
                for edge in node.edges:
                    if edge.stop_pos == None:
                        edge.stop_pos = \
                            edge.start_pos.move(self,edge.move_type)
                        duplicate = False
                        #for test in nodes:
                        #    if edge.stop_pos.equal_except_cost(test.pos):
                        #        duplicate = True
                        if duplicate == False:
                            possible_moves = \
                                edge.stop_pos.possible_moves(self)
                            new_nodes.append(MoveNode(pos=edge.stop_pos,
                                                      moves=possible_moves))
        self._move_graph = nodes
        print len(self._move_graph)
        
    ### ---------------------------------------------------
    ### DISPLAY
    ### ---------------------------------------------------
            
    def update_display(self):
        if self._animations != []:
            active_anim = self._animations[0]
            active_anim.update()
            if active_anim.done == True:
                self._animations.remove(active_anim)

    def world_pos(self):
        if self._animations != []:
            active_anim = self._animations[0]
            return active_anim.world_pos()
        return hex_math.hex_to_world((self._pos.x, self._pos.y), \
                                         self._game.hex_rad)

    def hex_pos(self):
        return (self._pos.x, self._pos.y)

    def angle_degrees(self):
        if self._animations != []:
            active_anim = self._animations[0]
            return active_anim.angle_degrees()
        return -1.0*Hex_Direction(self._pos.facing).degrees()

    ### ---------------------------------------------------
    ### SAVE/LOAD
    ### ---------------------------------------------------

    ### ---------------------------------------------------
    ### PROPERTY ACCESS
    ### ---------------------------------------------------
    
    @property
    def name(self):
        return self._name

    @property
    def game(self):
        return self._game

    @property
    def x(self):
        return self._pos.x

    @property
    def y(self):
        return self._pos.y

    @property
    def curr_move(self):
        return self._pos.curr_move

    @property
    def max_move(self):
        return self._max_move

    @property
    def move_cost(self):
        return self._move_cost

    @property
    def inertia(self):
        return self._inertia

    @property
    def evasion(self):
        return self._pos.evasion

    @property
    def facing(self):
        return self._pos.facing

    @property
    def imname(self):
        return self._imname

    @property
    def texture(self):
        return self._texture