def run(progress_q, results_q, threadid, adhoc_filename, episodes,
        results_folder, world_size):
    random_instance = random.Random(100 + threadid)
    random.seed(100 + threadid)
    np.random.seed(100 + threadid)

    num_agents = 4
    # adhoc = AdhocAgent(3, mcts_c=mcts_c, mcts_k=mcts_k, mcts_n=mcts_n, behavior_model_size=bsize,
    #                    environment_model_size=esize)
    # load_run(dataset_folder / dataset_name, adhoc, episodes, fit=False, compute_metrics=False)
    adhoc = AdhocAgent.load(adhoc_filename)
    agents = [agent_type(i) for i in range(num_agents - 1)] + [adhoc]
    transition_f = get_transition_function(num_agents, world_size,
                                           random.Random(100))
    reward_f = get_reward_function(num_agents, world_size)

    world = World(
        PursuitState.random_state(num_agents, world_size, random_instance),
        agents, transition_f, reward_f)
    timesteps, reward = world.run(0, 500)
    progress_q.put(1)

    results_q.put(
        (str(results_folder / 'results_eps{}'.format(episodes)), timesteps))
    results_q.put((str(results_folder / 'eaccuracy_eps{}'.format(episodes)),
                   np.average(adhoc.e_model.metric)))
    results_q.put((str(results_folder / 'baccuracy_eps{}'.format(episodes)),
                   np.average(adhoc.b_model.metric)))
    results_q.put(
        (str(results_folder / 'eaccuracyprey_eps{}'.format(episodes)),
         np.average(adhoc.e_model.metric_prey)))
Ejemplo n.º 2
0
 def __call__(self, state_node):
     result = super().__call__(state_node)
     world = World(state_node.state, [GreedyAgent(i) for i in range(4)],
                   get_transition_function(4, world_size),
                   get_reward_function(4, world_size))
     ts, reward = world.run(0, 1000)
     rollouts[self.k].append(result)
     rewards[self.k].append(reward)
     return result
Ejemplo n.º 3
0
    def __init__(self):
        logging.info('Initialising vision')
        #required on DICE:
        #self.capture = MPlayerCapture(self.rawSize)
        self.capture = Capture(self.rawSize)

        world = World('blue') # arbitrary colour
        world.pointer=None

        self.threshold = vision.threshold.AltRaw()
        self.pre = Preprocessor(self.rawSize, self.threshold, None)
        self.gui = GUI(world, self.rawSize, self.threshold)
        logging.debug('Vision initialised')
Ejemplo n.º 4
0
def run(q, threadid):
    random_instance = random.Random(100 + threadid)
    random.seed(100 + threadid)
    np.random.seed(100 + threadid)

    num_agents = 4
    # adhoc = AdhocAgent(3, mcts_c=mcts_c, mcts_k=mcts_k, mcts_n=mcts_n, behavior_model_size=bsize,
    #                    environment_model_size=esize)
    adhoc = AdhocAfterNAgent(agent_type(3),
                             episodes - 1,
                             3,
                             mcts_c=mcts_c,
                             mcts_k=mcts_k,
                             mcts_n=mcts_n,
                             behavior_model_size=bsize,
                             environment_model_size=esize)
    agents = [agent_type(i) for i in range(num_agents - 1)] + [adhoc]
    transition_f = get_transition_function(num_agents, world_size,
                                           random.Random(100))
    reward_f = get_reward_function(num_agents, world_size)

    world = World(
        PursuitState.random_state(num_agents, world_size, random_instance),
        agents, transition_f, reward_f)
    results = []
    bmodelmetric = []
    emodelmetric = []
    emodelmetric_prey = []
    try:
        for i in range(episodes):
            world.initial_state = PursuitState.random_state(
                num_agents, world_size, random_instance)
            timesteps, reward = world.run(0, 200)
            results.append(timesteps)
            timesteps = max(1, timesteps)
            bmodelmetric.append(
                sum(adhoc.b_model.metric[-timesteps:]) / timesteps)
            emodelmetric.append(
                sum(adhoc.e_model.metric[-timesteps:]) / timesteps)
            emodelmetric_prey.append(
                sum(adhoc.e_model.metric_prey[-timesteps:]) / timesteps)
            q.put(1)
    finally:
        np.save(str(results_folder / 'results_{}'.format(threadid)),
                np.array(results))
        np.save(str(results_folder / 'eaccuracy_{}'.format(threadid)),
                np.array(emodelmetric))
        np.save(str(results_folder / 'baccuracy_{}'.format(threadid)),
                np.array(bmodelmetric))
        np.save(str(results_folder / 'eaccuracyprey_{}'.format(threadid)),
                np.array(emodelmetric_prey))
Ejemplo n.º 5
0
 def __init__(self, agents, world_size=(5, 5), max_steps=1000):
     self.world_size = world_size
     self.agent = DummyAgent(3)
     initial_state = self._get_new_state()
     transition_f = get_transition_function(4, world_size)
     reward_f = get_reward_function(4, world_size)
     self.world = World(initial_state, agents + [self.agent], transition_f,
                        reward_f)
     self.reward_range = (-1, 0)
     self.action_space = spaces.Discrete(4)
     self.observation_space = spaces.Box(low=0,
                                         high=max(world_size),
                                         shape=(8, ))
     self.max_steps = max_steps
     self.i = 0
Ejemplo n.º 6
0
def save_run(filename, number_episodes, agents, world_size=(5, 5), seed=100):
    random_instance = random.Random(seed)
    num_agents = len(agents)
    transition_f = get_transition_function(num_agents, world_size, random.Random(seed))
    reward_f = get_reward_function(num_agents, world_size)
    transition_recorder = TransitionRecorder()
    world = World(PursuitState.random_state(num_agents, world_size, random_instance), agents, transition_f, reward_f,
                  visualizers=(transition_recorder, ))

    for i in range(number_episodes):
        world.initial_state = PursuitState.random_state(num_agents, world_size, random_instance)
        _, _ = world.run(0, 1000)

    output_file = open(filename, 'wb')
    pickle.dump(transition_recorder.transitions, output_file)
    output_file.close()
Ejemplo n.º 7
0
class WorldEnv(gym.Env):
    def __init__(self, agents, world_size=(5, 5), max_steps=1000):
        self.world_size = world_size
        self.agent = DummyAgent(3)
        initial_state = self._get_new_state()
        transition_f = get_transition_function(4, world_size)
        reward_f = get_reward_function(4, world_size)
        self.world = World(initial_state, agents + [self.agent], transition_f,
                           reward_f)
        self.reward_range = (-1, 0)
        self.action_space = spaces.Discrete(4)
        self.observation_space = spaces.Box(low=0,
                                            high=max(world_size),
                                            shape=(8, ))
        self.max_steps = max_steps
        self.i = 0

    def _get_new_state(self):
        return PursuitState.random_state(4, self.world_size)

    def step(self, action):
        self.agent.action = action
        self.world.next()
        observation = self.world.current_state.features_relative_prey()
        reward = self.agent.reward
        done = self.world.current_state.terminal or self.i >= self.max_steps

        self.i += 1

        return observation, reward, done, {}

    def reset(self):
        self.i = 0
        self.world.reset()
        self.world.current_state = self._get_new_state()
        return self.world.current_state.features_relative_prey()

    def render(self, mode='human'):
        pass
Ejemplo n.º 8
0
def init(episodes, world_q):
    random_instance = random.Random(100)
    random.seed(100)
    np.random.seed(100)

    num_agents = 4
    adhoc = AdhocAfterNAgent(agent_type(3), episodes, 3,
                             mcts_c=mcts_c, mcts_k=mcts_k, mcts_n=mcts_n, behavior_model_size=bsize,
                             environment_model_size=esize)
    agents = [agent_type(i) for i in range(num_agents - 1)] + [adhoc]
    transition_f = get_transition_function(num_agents, world_size, random.Random(100))
    reward_f = get_reward_function(num_agents, world_size)

    world = World(PursuitState.random_state(num_agents, world_size, random_instance), agents, transition_f, reward_f)

    for _ in tqdm.tqdm(range(episodes)):
        world.initial_state = PursuitState.random_state(num_agents, world_size, random_instance)
        world.run(0, 200)

    for _ in range(n_threads):
        world_q.put(world)

    return world, adhoc
Ejemplo n.º 9
0
 def __init__(self, evManager):
     """Create the world and wait for the greet msg from the server. """
     
     global log
     log = logging.getLogger('client')
     
     self._em = evManager
     self._em.reg_cb(NwRcvGreetEvt, self.on_greet)
     # all other callbacks are registered AFTER having been greeted
     
     self.avs = dict() # currently connected avatars from players
     self.creeps = dict() #need to remove creeps manually when they die
     
     self.acceptinput = False # start accepting player input after greeted by server
     
     self.world = World(evManager, log)
     self.chatlog = ChatLog(evManager)
Ejemplo n.º 10
0
    def __init__(self, evManager, nw):
        self._em = evManager
        # link with network controller
        self._nw = nw
        self._nw.model = self
        
        # Players + creeps = charactors 
        self.players = dict()
        self.creeps = dict()
        
        # build world
        self.world = World(evManager, self.log)
        mapname = config_get_mapname()
        self.world.build_world(mapname, callbacks=[self._nw.on_worldbuilt])
        self.world.buildpath()

        # when server starts, the game is not ON yet
        self.gameon = False
        
        # scheduling of game actions
        self._sched = Scheduler(self._em, self.world)
Ejemplo n.º 11
0
agent_colors = [(random.randint(0, 255), random.randint(0, 50),
                 random.randint(0, 255)) for _ in range(num_agents)]
visualizer = PygameVisualizer(400,
                              400,
                              agent_colors=agent_colors,
                              agents=agents)
visualizers = (visualizer, )

for i in range(iters):
    transition_f = get_transition_function(num_agents, world_size,
                                           random.Random(100 + i))
    reward_f = get_reward_function(num_agents, world_size)
    world = World(
        PursuitState.random_state(num_agents, world_size,
                                  random.Random(100 + i)),
        agents,
        transition_f,
        reward_f,
    )
    timesteps, reward = world.run(0., 5000)
    results.append(timesteps)
    print(timesteps)

plt.plot(results)
plt.plot([np.average(results[:i]) for i in range(1, len(results))],
         label='average')
plt.show()
# print(results)
# print(world_size)
# print(k)
print(np.average(results))
Ejemplo n.º 12
0
class Game:
    """ The top of the model. Contains players and world. """

    
    def __init__(self, evManager):
        """Create the world and wait for the greet msg from the server. """
        
        global log
        log = logging.getLogger('client')
        
        self._em = evManager
        self._em.reg_cb(NwRcvGreetEvt, self.on_greet)
        # all other callbacks are registered AFTER having been greeted
        
        self.avs = dict() # currently connected avatars from players
        self.creeps = dict() #need to remove creeps manually when they die
        
        self.acceptinput = False # start accepting player input after greeted by server
        
        self.world = World(evManager, log)
        self.chatlog = ChatLog(evManager)



    
    #############  greet  #############
     
    def on_greet(self, event):
        """ When the server greets me, set my name,
        start world building process, and add other connected players and creeps.
        Start also listening to player inputs, and other network messages.
        """
        
        mapname, newname = event.mapname, event.newname
        myinfo = event.myinfo
        onlineppl, creeps = event.onlineppl, event.creeps
        
        # build map and notify the view when it's done
        self.mapname = mapname
        self.world.build_world(mapname, MBuiltMapEvt)
        
        # myself, local player
        # The map has to be built before positioning the player's avatar.
        self.myname = newname
        log.info('My name is %s' % newname)
        self.add_player(newname, myinfo)
        # notify the view 
        ev = MGreetNameEvt(newname)
        self._em.post(ev)
        
        # creeps
        for cname, creepinfo in creeps.items():
            self.add_creep(cname, creepinfo)
            
        # remote players
        for pname, pinfo in onlineppl.items():
            self.add_player(pname, pinfo)
           

        # -- start listening to player's input and network messages

        self._em.reg_cb(InputAtkRequest, self.on_localatk)
        self._em.reg_cb(NwRcvAtkEvt, self.on_remoteatk)
        
        self._em.reg_cb(InputSkillRequest, self.on_localskill)
        self._em.reg_cb(NwRcvSkillEvt, self.on_remoteskill)
        
        self._em.reg_cb(InputMoveRequest, self.on_localavmove)
        self._em.reg_cb(NwRcvCharMoveEvt, self.on_remotemove)
        
        self._em.reg_cb(NwRcvPlayerJoinEvt, self.on_playerjoin)
        self._em.reg_cb(NwRcvCreepJoinEvt, self.on_creepjoin)
        self._em.reg_cb(NwRcvPlayerLeftEvt, self.on_playerleft)
        
        self._em.reg_cb(NwRcvNameChangeEvt, self.on_namechange)
        self._em.reg_cb(NwRcvNameChangeFailEvt, self.on_namechangefail)
        
        self._em.reg_cb(NwRcvDeathEvt, self.on_death)
        self._em.reg_cb(NwRcvRezEvt, self.on_resurrect)
        self._em.reg_cb(NwRcvHpsEvt, self.on_hps)
        
        self._em.reg_cb(NwRcvGameAdminEvt, self.on_gameadmin)
        
        self.acceptinput = True
        


    def get_charactor(self, name):
        """ Return a SCharactor (SCreep or SAvatar) from its name """
        if name in self.avs:
            return self.avs[name]
        else: 
            try: 
                return self.creeps[name]
            except KeyError: # creep not found
                log.error("Creep %s not found" % name)
                return None




    ############  attack  ##########
    
    
    def on_localatk(self, event):
        """ Unless dead, when user pressed atk button, make him atk. """
        
        if self.acceptinput:
            mychar = self.avs[self.myname]
            mychar.atk_local()


    def on_remoteatk(self, event):
        """ When the server tells about an attack, update the local model.
        My attacks also update the model when they come from the server,
        not when the player inputs them and the dmg are displayed on the screen. 
        """
        
        atker = self.get_charactor(event.atker)
        defer = self.get_charactor(event.defer)
        
        dmg = event.dmg
        fromremotechar = atker.name != self.myname # when attacks come from remote charactors
        if fromremotechar: 
            ev = RemoteCharactorAtkEvt(atker) # notify the view
            self._em.post(ev)
        defer.rcv_dmg(dmg, fromremotechar)
        
    

    ##########  death  #########
    
        
    def on_death(self, event):
        """ A creep or avatar died. """
        
        name = event.name
        if name in self.creeps: # a creep died
            self.remove_creep(name)
        
        elif name in self.avs: # another avatar died
            av = self.get_charactor(name)
            av.die()
        
            if name == self.myname: # if my local avatar died
                self.acceptinput = False # stop accepting player inputs
                
        else: # name not found
            log.warn('Received death of ' + name
                          + ', but model does not know that name.')
        
        
    def remove_creep(self, name):
        """ Remove a creep. """
        
        try:
            self.creeps[name].rmv()
            del self.creeps[name]
        except KeyError:
            log.warning('Creep %s may have already been removed' % name) 
           

    ###########  gameadmin  ##############
    
    
    def on_gameadmin(self, event):
        """ If game stops, notify the view/widgets.
        Creeps are killed automatically on the server side.
        """
        
        pname, cmd = event.pname, event.cmd
        ev = MGameAdminEvt(pname, cmd)
        self._em.post(ev)
        
        log.info(event.pname + ' ' + event.cmd + ' the game')
    
    
        
    ###########  hps  #######################
    
    def on_hps(self, event):
        """ An avatar changed hps. """
        
        name, info = event.name, event.info
        hp, mhp = info['hp'], info['maxhp']
        # update the model
        char = self.get_charactor(name)
        char.update_hps(hp, mhp)
        # update the view
        ev = MdHpsChangeEvt(char)
        self._em.post(ev)
        
            
    ##################  join, left  #####################################
    
    def on_playerjoin(self, event):
        """ When a new player arrives, add him to the connected players. """
        
        self.add_player(event.pname, event.pinfo)
        
        
    def add_player(self, pname, pinfo):
        """ add a new player to the list of connected players.
        pinfo contains a dic made by the server-side SAvatar object. 
        """
        
        coords, facing = pinfo['coords'], pinfo['facing']
        atk = pinfo['atk']
        hp, maxhp = pinfo['hp'], pinfo['maxhp']
        atk_cd = pinfo['atk_cd']
        cell = self.world.get_cell(coords)

        # whether that Player is the local client or a remote client
        islocal = hasattr(self, 'myname') and pname == self.myname 
        
        newplayer = Avatar(pname, cell, facing, atk, hp, maxhp, atk_cd, islocal, self._em)       
        self.avs[pname] = newplayer
        
        # notify the view 
        ev = MdAddPlayerEvt(pname)
        self._em.post(ev)
        
    
    def on_creepjoin(self, event):
        """ Create a creep. """
        
        cname, cinfo = event.cname, event.cinfo
        self.add_creep(cname, cinfo)
        
            
    def add_creep(self, cname, creepinfo):
        """ Add a new creep to the existing creeps.
        Info is a dic made on the server side by SCreep. """
        
        cell, facing = self.world.get_cell(creepinfo['coords']), creepinfo['facing']
        atk = creepinfo['atk']
        hp, maxhp = creepinfo['hp'], creepinfo['maxhp']
        
        creep = Creep(self._em, cname, cell, facing, atk, hp, maxhp)       
        self.creeps[cname] = creep

        
    
    ######################  left  ##################################
            
    def on_playerleft(self, event):
        """ remove a player """
        
        pname = event.pname
        try:
            self.avs[pname].rmv()
            #don't forget to clean the player data from the dict
            del self.avs[pname]
            # notify the view
            ev = MPlayerLeftEvt(pname)
            self._em.post(ev)
        except KeyError:
            log.warning('Player ' + pname + ' had already been removed') 
        

 
    ##################  move  ######################## 

    def on_localavmove(self, event):
        """ Unless dead, when the user pressed up, down, right, or left,
        move his avatar.
        """
        if self.acceptinput:
            mychar = self.avs[self.myname]
            mychar.move_relative(event.direction, event.strafing, event.rotating)

  
    def on_remotemove(self, event):
        """ Move the avatar of the player which cname is pname. """
        
        name = event.name
        coords, facing = event.coords, event.facing
        
        if name != self.myname: # client is in charge of making its local av move
            char = self.get_charactor(name)
            destcell = self.world.get_cell(coords)
            char.move_absolute(destcell, facing)

        
        
            
    ################## namechange ###################
    
    def on_namechange(self, event):
        """ update players' names when they change it """
        
        oldname, newname = event.oldname, event.newname
         
        if newname in self.avs:
            log.warning('%s changed name to %s, which was already in use'
                             % (oldname, newname))
        
        if self.myname == oldname: # I changed name
            self.myname = newname
            # notify the widget in charge of holding my cname
            log.info('Changed name to %s' % newname)
            ev = MMyNameChangedEvent(oldname, newname)
            self._em.post(ev)
        else: # someone else changed name
            ev = MNameChangedEvt(oldname, newname)
            self._em.post(ev)
            
        av = self.avs[oldname]
        av.changename(newname)
        self.avs[newname] = av
        del self.avs[oldname] #only del the mapping, not the player itself


    def on_namechangefail(self, event):
        """ When the player asked to change name but it was denied,
        notify the view. """
        
        ev = MNameChangeFailEvt(event.failname, event.reason)
        self._em.post(ev)
        
       
            
    ##############  resurrect  #############   
    
    def on_resurrect(self, event):
        """ An avatar (maybe myself) was resurrected. """
        
        name, info = event.name, event.info
        coords, facing = info['coords'], info['facing']
        hp, atk = info['hp'], info['atk']
        
        cell = self.world.get_cell(coords)
        
        # rez the char (update the view if local avatar)
        char = self.get_charactor(name)
        char.resurrect(cell, facing, hp, atk)
        
        # restart accepting input
        if name == self.myname:
            self.acceptinput = True


        
    ####################  skill  ################
    
    def on_localskill(self, event):
        """ Local player wants to cast a skill. """
        
        skname = event.skname
        
        if self.acceptinput:
            mychar = self.avs[self.myname]
            try:
                getattr(mychar, 'localcast_' + skname)() 
            except AttributeError as e:
                log.error(e)
            

    def on_remoteskill(self, event):
        """ The server notified that someone used a skill """
        
        pname, skname = event.pname, event.skname     
        av = self.avs[pname]
        try:
            getattr(av, 'remotecast_' + skname)()
        except AttributeError as e:
            log.error(e)
Ejemplo n.º 13
0
        tree = MCTS(tree_policy=UCB1(c=self.mcts_c),
                    default_policy=RandomKStepRollOut2(self.mcts_k),
                    backup=monte_carlo)
        self.prev_action = tree(self.root, n=n)
        # print([[y.n for y in x.children.values()] for x in self.root.children.values()])
        return self.prev_action


for k in (10, 100, 1000):
    for n in (1000, ):
        for c in (100, ):
            agents = [GreedyAgent(i) for i in range(4)]
            random.seed(100)
            agents[-1] = MCTSAgent(3, n, k, c * k)
            results = []
            for i in range(1):
                world = World(
                    PursuitState.random_state(len(agents), world_size), agents,
                    get_transition_function(len(agents), world_size),
                    get_reward_function(len(agents), world_size))
                timesteps, reward = world.run(0, 1000)
                results.append(timesteps)

            print("k: " + str(k))
            print("n: " + str(n))
            print("c: " + str(c))
            print("avg: " + str(sum(results) / len(results)))

print(rollouts)
print(rewards)
adhoc = AdhocAgent(3,
                   mcts_c=mcts_c,
                   mcts_k=mcts_k,
                   mcts_n=mcts_n,
                   behavior_model_size=bsize,
                   environment_model_size=esize,
                   eps=1.0,
                   fit=None)
# adhoc = AdhocAgent.load('adhoc_dataset/10x10greedy_random_200')
agents = [agent_type(i) for i in range(num_agents - 1)] + [adhoc]
transition_f = get_transition_function(num_agents, world_size,
                                       random.Random(100))
reward_f = get_reward_function(num_agents, world_size)

world = World(
    PursuitState.random_state(num_agents, world_size, random_instance), agents,
    transition_f, reward_f)
save_episodes = (1, 5, 10, 20, 50, 100, 150, 200)
current_episode = 0
for episodes in save_episodes:
    for current_episode in range(current_episode, episodes):

        world.initial_state = PursuitState.random_state(
            num_agents, world_size, random_instance)
        timesteps, reward = world.run(0, 100)
        print(timesteps)

        print("acc average " + str(np.average(adhoc.e_model.metric)))
        print("acc prey average " + str(np.average(adhoc.e_model.metric_prey)))
        print("behavior average " + str(np.average(adhoc.b_model.metric)))
Ejemplo n.º 15
0
class SGame():
    
    log = logging.getLogger('server')

    def __init__(self, evManager, nw):
        self._em = evManager
        # link with network controller
        self._nw = nw
        self._nw.model = self
        
        # Players + creeps = charactors 
        self.players = dict()
        self.creeps = dict()
        
        # build world
        self.world = World(evManager, self.log)
        mapname = config_get_mapname()
        self.world.build_world(mapname, callbacks=[self._nw.on_worldbuilt])
        self.world.buildpath()

        # when server starts, the game is not ON yet
        self.gameon = False
        
        # scheduling of game actions
        self._sched = Scheduler(self._em, self.world)

        #self.startgame('nobody')
      

    def __str__(self):
        args = (self.world.mapname, len(self.players), len(self.creeps))
        return '%s, %d players, %d creeps' % args 
    


    

    ######################  attack  #####################################
    
    def on_attack(self, atkername, defername, atk):
        """ This function is called by the network controller only.
        Trigger the attacking charactor's function attack(defer)
        """
        
        # get charactors from names
        atker = self.get_charactor(atkername)
        defer = self.get_charactor(defername)
        # defer may be None if atker = client with outdated model and defer = creep already dead 
        
        if atker and defer: # both are still present
            dmg = atker.attack(defer)
            if dmg == None: # atker attacked cell where the defer was not
                self.log.debug('%s missed the cell of %s' % (atkername, defername))
                # TODO: FT broadcast a miss msg
            elif dmg != atk:
                self.log.warn('%s says it attacked %s for %d, but server computed'
                              + ' %d instead' % (atkername, defername, atk, dmg))
        else:
            self.log.debug('%s attacked %s who was dead already' % (atkername, defername))
            # Dont do anything else: the client rendered his attack locally already
            # so, no need for the other clients to know about the failed attack.
            
             
                
    def get_charactor(self, name):
        """ Return an SCharactor (SCreep or SAvatar) from its name """
        
        if name in self.players:
            return self.players[name]
        else: 
            try:
                return self.creeps[name]
            except KeyError: # char not found = player left or creep died
                return None


    ##########################  chat  #################################
    
            
    def on_chat(self, pname, txt):
        """ When a chat message is received, 
        parse eventual commands,
        or broadcast the text to all connected users.
        """
                
        self.log.debug(pname + ' says ' + txt)
        
        gmprefix = config_get_gmcmdprefix()
        if txt and txt[0:len(gmprefix)] == gmprefix: # command
            args = txt.split()
            cmd = args[0][1:] # remove the leading '/'
            gm_func_name = 'gm_' + cmd
            if hasattr(self, gm_func_name):
                getattr(self, gm_func_name)(pname, args[1:])
            
        else: # normal chat msg
            self._nw.bc_chat(pname, txt)


    ##################### death ################
    

    def rmv_creep(self, name):
        """ Remove a creep given its name. Called by Creep. """
        try:
            del self.creeps[name]
        except KeyError:
            self.log.warning('Tried to remove creep %s but failed.' % (name))


    ##############  join  ######################

            
    def on_playerjoin(self, pname):
        """ Create player's avatar, and send him the list of connected ppl.
        Send also the list of creeps in range.
        Do not include him in the list of connected people. 
        """
        
        if pname in self.players:
            # player was already connected, 
            # or his pname had not been removed when he disconnected
            self.log.warn('Player %s already was connected.' % pname)
            return
                
        # Build list of connected players with their coords and facing direction.
        onlineppl = dict()
        for (otherpname, otherplayer) in self.players.items():
            onlineppl[otherpname] = otherplayer.serialize()
        
        # Build list of creeps with their coords.
        creeps = dict()
        for cname, creep in self.creeps.items():
            creeps[cname] = creep.serialize()
        
        # build the new player on the entrance cell and facing upwards
        cell = self.world.get_entrance()
        facing = DIRECTION_UP
        player = SAvatar(self, self._nw, self._sched, pname, cell, facing)
        self.players[pname] = player
        
        # greet the new player
        myinfo = player.serialize() 
        self._nw.greet(self.world.mapname, pname, myinfo,
                         onlineppl, creeps)
        
        # notify the connected players of this arrival
        self._nw.bc_playerjoined(pname, myinfo)
            

    
    ##############  left  ######################

    def on_playerleft(self, pname):
        """ remove player's avatar from game state and notify everyone """
        
        try:
            av = self.players[pname]
            av.on_logout() # remove avatar from the world
            del self.players[pname]
        except KeyError:
            self.log.warn('Tried to remove player ' + pname + 
                          ', but it was not found in player list')
                
        
    ###########################  move  ##################################
    
    def on_move(self, pname, coords, facing):
        """ Make a player move. 
        The player object will check for cheats itself. 
        """

        player = self.players[pname]
        newcell = self.world.get_cell(coords) #None means non-walkable or out
        player.move(newcell, facing)
            
                   
    ##############  namechange  ######################

    def on_playernamechange(self, oldname, newname):
        """ Change player's name only if the new name is not taken already. """
                
        if newname in self.players: # newname is already taken
            reason = 'Name already taken.'
            self._nw.send_namechangefail(oldname, newname, reason)
            
        else: # name not taken already
            player = self.players[oldname]
            canchange, reason = player.change_name(newname)
            if canchange: # new name is not too long
                self.log.debug(oldname + ' changed name into ' + newname)
                self.players[newname] = player
                del self.players[oldname]
                self._nw.bc_namechange(oldname, newname)
            else: # could not change name (e.g. too long or too short)
                self._nw.send_namechangefail(oldname, newname, reason)
        


    #####################  skill  ###########################
    
    def on_playerskill(self, pname, skname):
        """ A player used a skill """

        # TODO: refactor/cleanup
        if skname in ['burst']:
            av = self.players[pname]
            av.skill_burst()
        else:
            self.log.warn('Skill %s not found for player %s' % (skname, pname))
        


    ######################### GM commands ##############################
    # TODO: FT this should be in a Commander

    def gm_nick(self, pname, args):
        """ change player name """
        if args: # check that the user provided a new name
            newname = args[0]
            self.on_playernamechange(pname, newname)
    
    def gm_rez(self, pname, args):
        """ rez the player's avatar """
        av = self.players[pname]
        av.resurrect()
        
    def gm_start(self, pname, args):
        """ start the game """
        if self.gameon:
            self.stopgame(pname)
        self.startgame(pname)

    def gm_stop(self, pname, args):
        """ stop the game """
        if self.gameon:
            self.stopgame(pname)
        
    def gm_hp(self, pname, args):
        """ change the player's hp (and mhp if specified).
        '/hp 1' changes hp to 1, 
        '/hp 1/2' changes both hp to 1 and mhp to 2,
        '/hp /2' changes mhp, and keeps sure that hp <= mhp,
        '/hp 1/2/...' is the same as '/hp 1/2'
        """ 
        if args:
            hp_strs = args[0].split('/') # len(hps) is 0 if no '/' found
            try:
                hp = int(hp_strs[0])
            except ValueError: # casting str to int failed
                hp = None # cases like '/hp not_a_number'
            try:
                if len(hp_strs) >= 2:
                    mhp = int(hp_strs[1])
                else:
                    mhp = None
            except ValueError: # casting str to int failed
                mhp = None # cases like '/hp not_a_number'
        else: # no args == full heal
            mhp = hp = None
        
        av = self.players[pname]
        av.update_hps(hp, mhp)
            
        

    ########################  game commands  ###################
    
    def startgame(self, pname):
        """ start the game: creeps arrive """
        
        self.gameon = True
        self.log.info('Game started by ' + pname)
        self._nw.bc_gameadmin(pname, 'start')

        numcreeps = config_get_creepsperwave()
        cell = self.world.get_lair()
        for x in range(numcreeps):
            cname = 'creep-%d' % x
            delay = x * 1000
            creep = SCreep(self, self._sched, delay, 
                           self._nw, cname, cell, DIRECTION_LEFT) #face left
            self.creeps[cname] = creep
            
            
    def stopgame(self, pname):
        """ stop the game: kill all creeps """
        
        self.gameon = False
        self.log.info('Game stopped by ' + pname)
        self._nw.bc_gameadmin(pname, 'stop')
        
        # kill all the creeps
        creepscopy = self.creeps.copy()
        for creep in creepscopy.values():
            creep.die() # this removes the creep from self.creeps
Ejemplo n.º 16
0
agents = [TeammateAwareAgent(i) for i in range(num_agents)]
prey_moves = [(-1, 0), (1, 0), (0, 0)]
transition_f = get_transition_function(num_agents,
                                       world_size,
                                       prey_moves=prey_moves)
reward_f = get_reward_function(num_agents, world_size)
agent_colors = [(255, 0, 0), (175, 0, 75), (75, 0, 175), (0, 0, 255)]
visualizer = PygameVisualizer(200,
                              200,
                              agent_colors=agent_colors,
                              agents=agents)
visualizers = (visualizer, )

initial_state = PursuitState(((0, 1), (1, 0), (0, 3), (1, 2)), ((0, 0), ),
                             world_size)

world = World(initial_state,
              agents,
              transition_f,
              reward_f,
              visualizers=visualizers)
print(world.run(1, 100))

# expected actions
# RIGHT LEFT UP DOWN NOOP
# 4, 2, 2, 4 DOWN LEFT LEFT DOWN
# 4, 2, 2, 1 DOWN LEFT LEFT RIGH
# 4, 3, 2, 1 DOWN UUUP LEFT RIGH
# 1, 3, 2, 3 RIGH UUUP LEFT UUUP
# 1, 3, 2, 1 RIGH UUUP LEFT RIGH
# 1, 3, 2, 1 RIGH UUUP LEFT RIGH
Ejemplo n.º 17
0
#! /usr/bin/env python
# -*- coding: utf-8 -*-

from vision2.vision import Vision
from common.world import World
import sys

import logging
#logging.basicConfig(level=logging.DEBUG)

args = len(sys.argv)
if args < 1:
    print "Usage: vision.py [filename]"
    sys.exit(2)

world = World()
if args == 1:
    v = Vision(world)
elif args > 1:
    files = sys.argv[1:]
    v = Vision(world, files)

v.run()