Ejemplo n.º 1
0
def robot_loop_2(g,s):
    
    tw,th = g.iso_w,g.iso_h
    sx,sy = s.rect.centerx/tw,s.rect.centery/th
        
    if s.frame%FPS in (0,10,20,30): #HACK, uh oh!
        #rint g.slayer#
        
        #sx,sy = s.rect.centerx/tw,s.rect.centery/th
        p = g.castle
        px,py = p.rect.centerx/tw,p.rect.centery/th
        ox,oy = s.origin[0],s.origin[1]
        dx,dy = px-ox,py-oy #px-sx,py-sy
        
        if (dx*dx+dy*dy)**0.5 < 12: #follow if near by origin
            sx,sy = s.rect.centerx/tw,s.rect.centery/th
            p = g.castle
            px,py = p.rect.centerx/tw,p.rect.centery/th
            s.path = algo.astar((sx,sy),(px,py),g.robot_layer,dist)
        else: #else return to origin
            s.path = algo.astar((sx,sy),(ox,oy),g.robot_layer,dist)
            
        
    path = s.path
    if 1: # or len(path) < 12:
        while len(path) > 1:
            bx,by = path[1]
            if (bx,by) == (sx,sy): 
                path.pop(0)
                continue
            dx,dy = bx-sx,by-sy
            v = 4
            s.rect.x += sign(dx)*v
            s.rect.y += sign(dy)*v
            break
        

        
            
    
    s.frame += 1
Ejemplo n.º 2
0
def robot_loop_2(g, s):

    tw, th = g.iso_w, g.iso_h
    sx, sy = s.rect.centerx / tw, s.rect.centery / th

    if s.frame % FPS in (0, 10, 20, 30):  #HACK, uh oh!
        #rint g.slayer#

        #sx,sy = s.rect.centerx/tw,s.rect.centery/th
        p = g.castle
        px, py = p.rect.centerx / tw, p.rect.centery / th
        ox, oy = s.origin[0], s.origin[1]
        dx, dy = px - ox, py - oy  #px-sx,py-sy

        if (dx * dx + dy * dy)**0.5 < 12:  #follow if near by origin
            sx, sy = s.rect.centerx / tw, s.rect.centery / th
            p = g.castle
            px, py = p.rect.centerx / tw, p.rect.centery / th
            s.path = algo.astar((sx, sy), (px, py), g.robot_layer, dist)
        else:  #else return to origin
            s.path = algo.astar((sx, sy), (ox, oy), g.robot_layer, dist)

    path = s.path
    if 1:  # or len(path) < 12:
        while len(path) > 1:
            bx, by = path[1]
            if (bx, by) == (sx, sy):
                path.pop(0)
                continue
            dx, dy = bx - sx, by - sy
            v = 4
            s.rect.x += sign(dx) * v
            s.rect.y += sign(dy) * v
            break

    s.frame += 1
Ejemplo n.º 3
0
def drawfunc(karta):
    screen.clear()
    screen.tracer(0, 0)
    rect(drawMap(karta))
    screen.update()

    g = makeGraph(karta)
    g = connectGraph(g)
    g = algo.convertGraphToNodes(g)

    path = []

    while True:
        outOfTime = time() + 5
        path = algo.astar(g, outOfTime)

        for ss in range(len(path)):
            path[ss] = (path[ss][0] * s, path[ss][1] * -s, 'P')
        rect(path)
        print(path)
        break
Ejemplo n.º 4
0
    def event(self, g, e):
        tw, th = g.iso_w, g.iso_h
        s = self
        sx, sy = s.rect.centerx / tw, s.rect.centery / th

        if hasattr(e, 'pos'):
            epos = e.pos[0] - S_VIEW.x, e.pos[1] - S_VIEW.y
            #print epos
            #print "sx:%s  sy:%s" % (sx, sy)

        if e.type is MOUSEBUTTONDOWN and e.button == 3:
            tx, ty = g.screen_to_tile(epos)
            #print 'left',tx,ty
            w, h = g.size
            if (tx, ty) != (sx, sy) and (tx >= 0 and ty >= 0 and tx < w
                                         and ty < h):
                self.target = tx, ty
                self.path = algo.astar((sx, sy), (tx, ty), g.castle_layer,
                                       dist)
                #print self.path
            #elif self.doing:
            #    self.yes_pickup(e)

        #print e


#        if (e.type is MOUSEBUTTONDOWN and e.button == 3 or
#            e.type == JOYBUTTONDOWN and e.button ==JOY_FIRE_BUTTON):
        if (e.type is MOUSEBUTTONDOWN and e.button == 1):

            # use up some steam in order to fire.
            self.unit.prep_fire()

        if (e.type is MOUSEBUTTONUP and e.button == 1):
            tx, ty = g.screen_to_tile(epos)
            #print 'right',tx,ty

            x, y = g.screen_to_tile(epos)
            cannon_pressure = self.unit.try_do_fire()

            tw, th = g.iso_w, g.iso_h
            cball_new(g, self.rect, (x * tw + tw / 2, y * th + th / 2),
                      self.unit.stats['Damage'], cannon_pressure * 1.5)

        if e.type == JOYBUTTONDOWN:
            pass

        #if self.direction_dy != 0:
        #    self.last_direction_dy = self.direction_dy
        #if self.direction_dx != 0:
        #    self.last_direction_dx = self.direction_dx

        if e.type == JOYAXISMOTION:
            if e.axis == 0:
                if round(e.value) < 0:
                    self.direction_dx = -1
                    self.last_direction_dx = self.direction_dx
                    self.last_direction_dy = self.direction_dy
                    #print "less than"
                elif round(e.value) > 0:
                    self.direction_dx = 1
                    self.last_direction_dx = self.direction_dx
                    self.last_direction_dy = self.direction_dy
                    #print "greater than"
                else:
                    #print "nothing!"
                    self.last_direction_dx = self.direction_dx
                    self.direction_dx = 0

            if e.axis == 1:
                if round(e.value) < 0:
                    self.direction_dy = -1
                    self.last_direction_dy = self.direction_dy
                    self.last_direction_dx = self.direction_dx
                elif round(e.value) > 0:
                    self.direction_dy = 1
                    self.last_direction_dy = self.direction_dy
                    self.last_direction_dx = self.direction_dx
                else:
                    self.last_direction_dy = self.direction_dy
                    self.direction_dy = 0

        if (e.type == JOYBUTTONUP and e.button == JOY_FIRE_BUTTON):
            pass

            if 0:

                self.direction_dx = 0
                self.direction_dy = 0
                tx, ty = g.screen_to_tile(epos)
                x, y = g.screen_to_tile(epos)

                if self.unit.try_fire():
                    #print (x*tw+tw/2,y*th+th/2)
                    cball_new(g, s.rect, (x * tw + tw / 2, y * th + th / 2),
                              self.unit.stats['Damage'])

        if (e.type == JOYBUTTONDOWN and e.button == JOY_FIRE_BUTTON):

            # TODO: find out which way we are facing.
            # TODO: fire in facing direction.
            self.fire_state = "firing"

        if ((e.type == JOYBUTTONDOWN and e.button == JOY_FIRE_BUTTON) or
            (e.type == KEYDOWN and e.key in [K_f, K_LCTRL, K_RCTRL, K_SPACE])):

            sx, sy = self.rect.centerx / tw, self.rect.centery / th

            #print "last dx:%s:   last dy:%s:" % (self.last_direction_dx, self.last_direction_dy)
            #print "sx:%s:   sy:%s:" % (sx, sy)

            self.unit.prep_fire()

        if ((e.type == JOYBUTTONUP and e.button == JOY_FIRE_BUTTON) or
            (e.type == KEYUP and e.key in [K_f, K_LCTRL, K_RCTRL, K_SPACE])):
            facing_x, facing_y = (self.last_direction_dx,
                                  self.last_direction_dy)
            facing_x, facing_y = iso_facing_to_screen_facing(
                self.last_direction_dx, self.last_direction_dy)

            x = (5 * facing_x) + sx
            y = (5 * facing_y) + sy

            #x,y = g.screen_to_tile(epos)
            cannon_pressure = self.unit.try_do_fire()

            tw, th = g.iso_w, g.iso_h
            cball_new(g, self.rect, (x * tw + tw / 2, y * th + th / 2),
                      self.unit.stats['Damage'], cannon_pressure)

            # use up some steam in order to fire.
            #if self.unit.try_fire():
            #    cball_new(g,s.rect,(x*tw+tw/2,y*th+th/2), self.unit.stats['Damage'])

        if self.doing:
            if ((e.type == KEYDOWN and self.doing and e.key == K_RETURN)
                    or (e.type == KEYDOWN and self.doing and e.key == K_SPACE)
                    or (e.type == MOUSEBUTTONDOWN and e.button == 3) or
                (e.type == JOYBUTTONDOWN and e.button == JOY_PICKUP_BUTTON)):
                self.yes_pickup(e)

        if e.type == KEYUP:

            if e.key in [K_UP, K_w]:
                self.last_direction_dy = self.direction_dy
                self.direction_dy = 0
            elif e.key in [K_DOWN, K_s]:
                self.last_direction_dy = self.direction_dy
                self.direction_dy = 0
            elif e.key in [K_LEFT, K_a]:
                self.last_direction_dx = self.direction_dx
                self.direction_dx = 0
            elif e.key in [K_RIGHT, K_d]:
                self.last_direction_dx = self.direction_dx
                self.direction_dx = 0

        # some testing keys.
        if e.type == KEYDOWN:

            if e.key in [K_UP, K_w]:
                self.direction_dy = -1
                self.last_direction_dy = self.direction_dy
                self.last_direction_dx = self.direction_dx
            elif e.key in [K_DOWN, K_s]:
                self.direction_dy = 1
                self.last_direction_dy = self.direction_dy
                self.last_direction_dx = self.direction_dx
            elif e.key in [K_LEFT, K_a]:
                self.direction_dx = -1
                self.last_direction_dx = self.direction_dx
                self.last_direction_dy = self.direction_dy
            elif e.key in [K_RIGHT, K_d]:
                self.direction_dx = 1
                self.last_direction_dx = self.direction_dx
                self.last_direction_dy = self.direction_dy

            if e.key == K_KP_PLUS:
                self.unit.stats['Speed'] += 1

            elif e.key == K_KP_MINUS:
                self.unit.stats['Speed'] -= 1

            elif e.key == K_h:
                self.unit.hit(2)

            elif e.key == K_r:
                #tx, ty = self.item_tile.tx, self.item_tile.ty
                # drop rubble test.
                tx, ty = self.rect.centerx / tw, self.rect.centery / th

                self.g.set((tx, ty), 7)

            if UPGRADE_FUN:

                if e.key == K_1:
                    self.upgrade_something("UpEngine Efficiency")
                elif e.key == K_2:
                    self.upgrade_something("UpEngine Speed")
                elif e.key == K_3:
                    self.upgrade_something("UpCannon Balls")
                elif e.key == K_4:
                    self.upgrade_something("UpCannon Power")
                elif e.key == K_5:
                    self.upgrade_something("UpArmour")
                elif e.key == K_6:
                    self.upgrade_something("UpSteam Tank")
                elif e.key == K_7:
                    self.upgrade_something("UpWater Tank")
                elif e.key == K_8:
                    self.upgrade_something("UpCoal Tank")

                elif e.key in [K_9, K_0]:
                    pprint.pprint(self.unit.stats)

            if e.key == K_y:
                #get_savable_upgrade_amounts()
                print self.backup_castle()
            if e.key == K_u:
                #get_savable_upgrade_amounts()
                print self.reset_castle()
            if e.key == K_z:
                #get_savable_upgrade_amounts()
                print self.unit.stats
Ejemplo n.º 5
0
    def event(self, g , e):
        tw,th = g.iso_w,g.iso_h
        s = self
        sx,sy = s.rect.centerx/tw,s.rect.centery/th



        if hasattr(e,'pos'):
            epos = e.pos[0]-S_VIEW.x,e.pos[1]-S_VIEW.y
            #print epos
            #print "sx:%s  sy:%s" % (sx, sy)
        
        if e.type is MOUSEBUTTONDOWN and e.button == 3:
            tx,ty = g.screen_to_tile(epos)
            #print 'left',tx,ty
            w,h = g.size
            if (tx,ty) != (sx,sy) and (tx >= 0 and ty >=0 and tx < w and ty <h):
                self.target = tx,ty
                self.path = algo.astar((sx,sy),(tx,ty),g.castle_layer,dist)
                #print self.path
            #elif self.doing:
            #    self.yes_pickup(e)
                
        #print e   
#        if (e.type is MOUSEBUTTONDOWN and e.button == 3 or 
#            e.type == JOYBUTTONDOWN and e.button ==JOY_FIRE_BUTTON):
        if (e.type is MOUSEBUTTONDOWN and e.button == 1):

            # use up some steam in order to fire.
            self.unit.prep_fire()
                
        if (e.type is MOUSEBUTTONUP and e.button == 1):
            tx,ty = g.screen_to_tile(epos)
            #print 'right',tx,ty
            
            x,y = g.screen_to_tile(epos)
            cannon_pressure = self.unit.try_do_fire()

            tw,th = g.iso_w,g.iso_h
            cball_new(g,self.rect,(x*tw+tw/2,y*th+th/2), self.unit.stats['Damage'],cannon_pressure*1.5)







        if e.type == JOYBUTTONDOWN:
            pass

        #if self.direction_dy != 0:
        #    self.last_direction_dy = self.direction_dy
        #if self.direction_dx != 0:
        #    self.last_direction_dx = self.direction_dx

        if e.type == JOYAXISMOTION:
            if e.axis == 0:
                if round(e.value) < 0:
                    self.direction_dx = -1
                    self.last_direction_dx = self.direction_dx
                    self.last_direction_dy = self.direction_dy
                    #print "less than"
                elif round(e.value) > 0:
                    self.direction_dx = 1
                    self.last_direction_dx = self.direction_dx
                    self.last_direction_dy = self.direction_dy
                    #print "greater than"
                else:
                    #print "nothing!"
                    self.last_direction_dx = self.direction_dx
                    self.direction_dx = 0



            if e.axis == 1:
                if round(e.value) < 0:
                    self.direction_dy = -1
                    self.last_direction_dy = self.direction_dy
                    self.last_direction_dx = self.direction_dx
                elif round(e.value) > 0:
                    self.direction_dy = 1
                    self.last_direction_dy = self.direction_dy
                    self.last_direction_dx = self.direction_dx
                else:
                    self.last_direction_dy = self.direction_dy
                    self.direction_dy = 0
                

        if (e.type == JOYBUTTONUP and e.button ==JOY_FIRE_BUTTON):
            pass

            if 0:

                self.direction_dx = 0
                self.direction_dy = 0
                tx,ty = g.screen_to_tile(epos)
                x,y = g.screen_to_tile(epos)


                if self.unit.try_fire():
                    #print (x*tw+tw/2,y*th+th/2)
                    cball_new(g,s.rect,(x*tw+tw/2,y*th+th/2), self.unit.stats['Damage'])



        if (e.type == JOYBUTTONDOWN and e.button ==JOY_FIRE_BUTTON):

            # TODO: find out which way we are facing.
            # TODO: fire in facing direction.
            self.fire_state = "firing"


        if ((e.type == JOYBUTTONDOWN and e.button ==JOY_FIRE_BUTTON) or
            (e.type == KEYDOWN and e.key in [K_f, K_LCTRL, K_RCTRL, K_SPACE])):

            sx,sy = self.rect.centerx/tw, self.rect.centery/th

            #print "last dx:%s:   last dy:%s:" % (self.last_direction_dx, self.last_direction_dy)
            #print "sx:%s:   sy:%s:" % (sx, sy)

            
            
            self.unit.prep_fire()
            
        if ((e.type == JOYBUTTONUP and e.button ==JOY_FIRE_BUTTON) or
            (e.type == KEYUP and e.key in [K_f, K_LCTRL, K_RCTRL, K_SPACE])):
            facing_x, facing_y = (self.last_direction_dx, self.last_direction_dy)
            facing_x, facing_y = iso_facing_to_screen_facing(self.last_direction_dx, self.last_direction_dy)

            x = (5 * facing_x) + sx
            y = (5 * facing_y) + sy

            #x,y = g.screen_to_tile(epos)
            cannon_pressure = self.unit.try_do_fire()

            tw,th = g.iso_w,g.iso_h
            cball_new(g,self.rect,(x*tw+tw/2,y*th+th/2), self.unit.stats['Damage'],cannon_pressure)



            # use up some steam in order to fire.
            #if self.unit.try_fire():
            #    cball_new(g,s.rect,(x*tw+tw/2,y*th+th/2), self.unit.stats['Damage'])




        
        if self.doing:
            if (
                (e.type == KEYDOWN and self.doing and e.key == K_RETURN) or
                (e.type == KEYDOWN and self.doing and e.key == K_SPACE) or
                (e.type == MOUSEBUTTONDOWN and e.button == 3) or 
                (e.type == JOYBUTTONDOWN and e.button == JOY_PICKUP_BUTTON)):
                self.yes_pickup(e)


        if e.type == KEYUP:

            if e.key in [K_UP, K_w]:
                self.last_direction_dy = self.direction_dy
                self.direction_dy = 0
            elif e.key in [K_DOWN, K_s]:
                self.last_direction_dy = self.direction_dy
                self.direction_dy = 0
            elif e.key in [K_LEFT, K_a]:
                self.last_direction_dx = self.direction_dx
                self.direction_dx = 0
            elif e.key in [K_RIGHT, K_d]:
                self.last_direction_dx = self.direction_dx
                self.direction_dx = 0


        # some testing keys.
        if e.type == KEYDOWN:

            if e.key in [K_UP, K_w]:
                self.direction_dy = -1
                self.last_direction_dy = self.direction_dy
                self.last_direction_dx = self.direction_dx
            elif e.key in [K_DOWN, K_s]:
                self.direction_dy = 1
                self.last_direction_dy = self.direction_dy
                self.last_direction_dx = self.direction_dx
            elif e.key in [K_LEFT, K_a]:
                self.direction_dx = -1
                self.last_direction_dx = self.direction_dx
                self.last_direction_dy = self.direction_dy
            elif e.key in [K_RIGHT, K_d]:
                self.direction_dx = 1
                self.last_direction_dx = self.direction_dx
                self.last_direction_dy = self.direction_dy



            if e.key == K_KP_PLUS:
                self.unit.stats['Speed'] += 1

            elif e.key == K_KP_MINUS:
                self.unit.stats['Speed'] -= 1

            elif e.key == K_h:
                self.unit.hit(2)

            elif e.key == K_r:
                #tx, ty = self.item_tile.tx, self.item_tile.ty
                # drop rubble test.
                tx,ty = self.rect.centerx/tw, self.rect.centery/th

                self.g.set((tx,ty),7)

            if UPGRADE_FUN:

                if e.key == K_1:
                    self.upgrade_something("UpEngine Efficiency")
                elif e.key == K_2:
                    self.upgrade_something("UpEngine Speed")
                elif e.key == K_3:
                    self.upgrade_something("UpCannon Balls")
                elif e.key == K_4:
                    self.upgrade_something("UpCannon Power")
                elif e.key == K_5:
                    self.upgrade_something("UpArmour")
                elif e.key == K_6:
                    self.upgrade_something("UpSteam Tank")
                elif e.key == K_7:
                    self.upgrade_something("UpWater Tank")
                elif e.key == K_8:
                    self.upgrade_something("UpCoal Tank")

                elif e.key in [K_9, K_0]:
                    pprint.pprint( self.unit.stats )

                    
            if e.key == K_y:
                #get_savable_upgrade_amounts()
                print self.backup_castle()
            if e.key == K_u:
                #get_savable_upgrade_amounts()
                print self.reset_castle()
            if e.key == K_z:
                #get_savable_upgrade_amounts()
                print self.unit.stats
Ejemplo n.º 6
0
def truck_loop(g, s):
    s.unit.loop()

    s.frame += 1

    tw, th = g.iso_w, g.iso_h
    ww, hh = g.size
    sx, sy = s.rect.centerx / tw, s.rect.centery / th
    if s.state == 'init':
        best_s, best_d = None, 65535
        for ss in g.sprites:
            if hasattr(ss, 'type') and ss.type == 'factory':
                fx, fy = ss.rect.centerx / tw, ss.rect.centery / th
                dst = dist((sx, sy), (fx, fy))
                if dst < best_d:
                    best_s, best_d = ss, dst
        s.factory = best_s
        s.state = 'search'
        return

    if s.state == 'search':
        coals = []
        for y in xrange(0, ww):
            for x in xrange(0, hh):
                if g.tlayer[y][x] == 1:  #HACK: if is coal
                    coals.append((x, y))
        if len(coals) == 0:
            s.state = 'wait'
            return

        best, best_d = None, 65535
        fx, fy = s.factory.rect.centerx / tw, s.factory.rect.centery / th
        for cx, cy in coals:
            dst = dist((cx, cy), (fx, fy))
            if dst < best_d:
                best, best_d = (cx, cy), dst

        cx, cy = best
        s.target = cx, cy
        s.path = algo.astar((sx, sy), (cx, cy), g.truck_layer, dist)
        s.state = 'coal'

    #once a second check if some coal has appeared...
    if s.state == 'wait':
        if s.frame % FPS == 0:
            s.state = 'search'
        return

    if s.state == 'coal':
        fx, fy = s.target
        n = g.tlayer[fy][fx]
        if n != 1:  #HACK: coal?
            s.state = 'search'
            return
        if (sx, sy) == s.target:
            s.coal = 1
            g.set((sx, sy), 0)

            fx, fy = s.factory.rect.centerx / tw, s.factory.rect.centery / th
            s.target = fx, fy
            s.path = algo.astar((sx, sy), (fx, fy), g.truck_layer, dist)
            s.state = 'factory'

    if s.state == 'factory':
        if (sx, sy) == s.target:
            s.coal = 0
            #NOTE: give the coal to the factory here....
            s.state = 'search'
            return

    path = s.path
    while len(path) > 1:
        bx, by = path[1]
        if (bx, by) == (sx, sy):
            path.pop(0)
            continue
        dx, dy = bx - sx, by - sy
        v = 4
        s.rect.x += sign(dx) * v
        s.rect.y += sign(dy) * v
        break
Ejemplo n.º 7
0
def truck_loop(g,s):
    s.unit.loop()

    s.frame += 1
    
    tw,th = g.iso_w,g.iso_h
    ww,hh = g.size
    sx,sy = s.rect.centerx/tw,s.rect.centery/th
    if s.state == 'init':
        best_s,best_d = None,65535
        for ss in g.sprites:
            if hasattr(ss,'type') and ss.type == 'factory':
                fx,fy = ss.rect.centerx/tw,ss.rect.centery/th
                dst = dist((sx,sy),(fx,fy))
                if dst < best_d:
                    best_s,best_d = ss,dst
        s.factory = best_s
        s.state = 'search'
        return
                
    
                
    if s.state == 'search':
        coals = []
        for y in xrange(0,ww):
            for x in xrange(0,hh):
                if g.tlayer[y][x] == 1: #HACK: if is coal
                    coals.append((x,y))
        if len(coals) == 0:
            s.state = 'wait'
            return
        
        best,best_d = None,65535
        fx,fy = s.factory.rect.centerx/tw,s.factory.rect.centery/th
        for cx,cy in coals:
            dst = dist((cx,cy),(fx,fy))
            if dst < best_d:
                best,best_d = (cx,cy),dst
            
        cx,cy = best
        s.target = cx,cy
        s.path = algo.astar((sx,sy),(cx,cy),g.truck_layer,dist)
        s.state = 'coal'
                    
    #once a second check if some coal has appeared...
    if s.state == 'wait':
        if s.frame%FPS == 0:
            s.state = 'search'
        return
        
        
    if s.state == 'coal':
        fx,fy = s.target
        n = g.tlayer[fy][fx] 
        if n != 1: #HACK: coal?
            s.state = 'search'
            return
        if (sx,sy) == s.target:
            s.coal = 1
            g.set((sx,sy),0)
            
            fx,fy = s.factory.rect.centerx/tw,s.factory.rect.centery/th
            s.target = fx,fy
            s.path = algo.astar((sx,sy),(fx,fy),g.truck_layer,dist)
            s.state = 'factory'
        
    if s.state == 'factory':
        if (sx,sy) == s.target:
            s.coal = 0
            #NOTE: give the coal to the factory here....
            s.state = 'search'
            return
    
    path = s.path
    while len(path) > 1:
        bx,by = path[1]
        if (bx,by) == (sx,sy): 
            path.pop(0)
            continue
        dx,dy = bx-sx,by-sy
        v = 4
        s.rect.x += sign(dx)*v
        s.rect.y += sign(dy)*v
        break