def initRandomVehicles(self): for index in range(0, int(len(self.structures)*UNIVERSAL_SHIP_DENSITY)): pilot = Actor('miner') ship = Miner(pilot, self) pilot.setVehicle(ship) pilot.setProfession('miner') self.actors.append(pilot)
def __init__(self, state_size, action_size, random_seed): """Initialize an Agent object. Params ====== state_size (int): dimension of each state action_size (int): dimension of each action random_seed (int): random seed """ self.state_size = state_size self.action_size = action_size self.seed = random.seed(random_seed) self.time_step = 0 # Actor Network (w/ Target Network) self.actor_local = Actor(state_size, action_size, random_seed).to(device) self.actor_target = Actor(state_size, action_size, random_seed).to(device) self.actor_optimizer = optim.Adam(self.actor_local.parameters(), lr=LR_ACTOR) # Critic Network (w/ Target Network) self.critic_local = Critic(state_size, action_size, random_seed).to(device) self.critic_target = Critic(state_size, action_size, random_seed).to(device) self.critic_optimizer = optim.Adam(self.critic_local.parameters(), lr=LR_CRITIC, weight_decay=WEIGHT_DECAY) # Noise process self.noise = OUNoise(action_size, random_seed) # Replay memory self.memory = ReplayBuffer(action_size, BUFFER_SIZE, BATCH_SIZE, random_seed)
def __init__(self): self.zombies = [Actor.Zombie()] self.plants = [Actor.Plant(), Actor.Plant()] self.Board = Board() #self.plants[0].Position.x = 200 if self.Board.putPlant(0, 1): self.plants[0].setPosition(self.Board.getPosition(0, 1)) if self.Board.putPlant(0, 0): self.plants[1].setPosition(self.Board.getPosition(0, 0))
def generate(self): if self.butterfly == None: self.butterfly = Actor.Actor() self.butterfly.loadModel('phase_4/models/props/SZ_butterfly-mod.bam') self.butterfly.loadAnims({'flutter': 'phase_4/models/props/SZ_butterfly-flutter.bam', 'glide': 'phase_4/models/props/SZ_butterfly-glide.bam', 'land': 'phase_4/models/props/SZ_butterfly-land.bam'}) index = self.doId % len(self.wingTypes) chosenType = self.wingTypes[index] node = self.butterfly.getGeomNode() for type in self.wingTypes: wing = node.find('**/' + type) if type != chosenType: wing.hide() else: if index == 0 or index == 1: color = self.yellowColors[self.doId % len(self.yellowColors)] else: if index == 2 or index == 3: color = self.whiteColors[self.doId % len(self.whiteColors)] else: if index == 4: color = self.paleYellowColors[self.doId % len(self.paleYellowColors)] else: color = Vec4(1, 1, 1, 1) wing.setColor(color) self.butterfly2 = Actor.Actor(other=self.butterfly) self.butterfly.enableBlend(blendType=PartBundle.BTLinear) self.butterfly.loop('flutter') self.butterfly.loop('land') self.butterfly.loop('glide') rng = RandomNumGen.RandomNumGen(self.doId) playRate = 0.6 + 0.8 * rng.random() self.butterfly.setPlayRate(playRate, 'flutter') self.butterfly.setPlayRate(playRate, 'land') self.butterfly.setPlayRate(playRate, 'glide') self.butterfly2.setPlayRate(playRate, 'flutter') self.butterfly2.setPlayRate(playRate, 'land') self.butterfly2.setPlayRate(playRate, 'glide') self.glideWeight = rng.random() * 2 lodNode = LODNode('butterfly-node') lodNode.addSwitch(100, 40) lodNode.addSwitch(40, 0) self.butterflyNode = NodePath.NodePath(lodNode) self.butterfly2.setH(180.0) self.butterfly2.reparentTo(self.butterflyNode) self.butterfly.setH(180.0) self.butterfly.reparentTo(self.butterflyNode) self.__initCollisions() self.dropShadow = loader.loadModelCopy('phase_3/models/props/drop_shadow') self.dropShadow.setColor(0, 0, 0, 0.3) self.dropShadow.setPos(0, 0.1, -0.05) self.dropShadow.setScale(self.shadowScaleBig) self.dropShadow.reparentTo(self.butterfly) return
def createAllGameObjects(self): """ Create instances (each item) of GameObject class (which is a child class of Actor) """ #GameObjects that are gettable self.guardianScroll = Actor.GameObject() self.guardianScroll.name = "T'Shal Scroll" self.guardianScroll.isGettable = True self.exitKey = Actor.GameObject() self.exitKey.name = "Large Gate Key" self.exitKey.isGettable = True #GameObjects that are threats self.suspiciousDoor = Actor.GameObject() self.suspiciousDoor.name = "Suspicious Door" self.suspiciousTile = Actor.GameObject() self.suspiciousTile.name = "Suspicious Tile" #GameObjects that are secrets self.deadEndRoomSouthWall = Actor.GameObject() self.deadEndRoomSouthWall.name = "South Wall" self.corridor2WestWall = Actor.GameObject() self.corridor2WestWall.name = "West Wall" #GameObjects that can be interacted with (open, etc?) self.treasureChest = Actor.GameObject() self.treasureChest.name = "Treasure Chest" #unlockable doors/gates go here too self.exitGate = Actor.GameObject() self.exitGate.name = "Large Gate"
def loadBam(self, filePath): """ Loads a Toontown Beta Bam file. Args: filePath (string): Path to Bam file to load. """ isAnimation = 0 # First, we need to check if this Bam file is an animation. It's definitely not animation if filePath[-8:-4] not in ('1500', '1000', '-500', '-250', '-800', '-400', '-mod'): for actorBase, actorPhase in ACTOR_REFERENCE.items(): if filePath.find(actorBase[:-4]) > -1: # If the beginning of the file name is found in the Actor Reference, treat # this file as an animation isAnimation = 1 geom = Actor.Actor(actorPhase + '/' + actorBase, {'anim': filePath}) geom.loop('anim') break # If it's not an animation, just load a regular ol' bam file if not isAnimation: geom = base.loader.loadModel(filePath) geom.reparentTo(render) return geom
def __init__(self, node): AnimatedProp.AnimatedProp.__init__(self, node) self.fish = Actor.Actor() self.fish.reparentTo(self.node.getParent()) self.fish.setTransform(self.node.getTransform()) self.node.clearMat() self.fish.prepareBundle(self.node) self.fish.loadAnims({ 'jump': 'phase_4/models/props/SZ_fish-jump', 'swim': 'phase_4/models/props/SZ_fish-swim' }) self.splashSfxList = ( loader.loadSfx('phase_4/audio/sfx/TT_splash1.mp3'), loader.loadSfx('phase_4/audio/sfx/TT_splash2.mp3')) self.node = self.fish self.geom = self.fish.getGeomNode() self.exitRipples = Ripples(self.geom) self.exitRipples.setBin('fixed', 25, 1) self.exitRipples.setPosHprScale(-0.3, 0.0, 1.24, 0.0, 0.0, 0.0, 0.7, 0.7, 0.7) self.splash = Splash(self.geom) self.splash.setPosHprScale(-1, 0.0, 1.23, 0.0, 0.0, 0.0, 0.7, 0.7, 0.7) randomSplash = random.choice(self.splashSfxList) self.track = Sequence(FunctionInterval(self.randomizePosition), Parallel( self.fish.actorInterval('jump'), Sequence(Wait(0.25), Func(self.exitRipples.play, 0.75)), Sequence( Wait(1.14), Func(self.splash.play), SoundInterval(randomSplash, volume=0.8, node=self.node))), Wait(5 + 10 * random.random()), name=self.uniqueName('Fish'))
def init(): pygame.init() cfg.SURFACE_MAIN = pygame.display.set_mode( (cfg.SCREEN_WIDTH, cfg.SCREEN_HEIGHT)) cfg.PLAYER = Actor.Player() cfg.PLAYER.x = (int)(cfg.SCREEN_WIDTH / 2 - (cfg.PLAYER.width / 2)) cfg.PLAYER.y = (int)(cfg.SCREEN_HEIGHT / 2 - (cfg.PLAYER.height / 2)) cfg.PLAYER.add(cfg.ACTORS, cfg.INFECTED) for i in range(0, cfg.INIT_ACTORS): new_actor = Actor.Circle() new_actor.x = random.randint(0, cfg.SCREEN_WIDTH - new_actor.width) new_actor.y = random.randint(0, cfg.SCREEN_HEIGHT - new_actor.height) new_actor.add(cfg.ACTORS, cfg.AI_ACTORS, cfg.HEALTHY)
def __getPropCopy(self, name): if self.propTypes[name] == 'actor': if not self.props.has_key(name): prop = Actor.Actor() prop.loadModel(self.propStrings[name][0]) animDict = {} animDict[name] = self.propStrings[name][1] prop.loadAnims(animDict) prop.name = name self.storeProp(name, prop) if name in Variants: self.makeVariant(name) return Actor.Actor(other=self.props[name]) else: if not self.props.has_key(name): prop = loader.loadModel(self.propStrings[name][0]) prop.name = name self.storeProp(name, prop) if name in Variants: self.makeVariant(name) return self.props[name].copyTo(hidden)
def createAllCharacters(self): """create instances (PC, NPC's) of Character class, which is a subclass of Actor Right now, any character with HP > 1 cant be killed in one hit. Keep in mind when adding characters later. """ #PC self.player = Actor.GameCharacter() self.player.name = "player" #NPC self.skeletons = Actor.GameCharacter() self.skeletons.name = "Guardian Skeletons" self.skeletons.HP = 3 # purely arbitrary. Why not, there are 3 skeletons. self.skeletons.isPlural = 1 # 3 in one cluster self.zombie = Actor.GameCharacter() self.zombie.name = "Zombie" self.guardianBoss = Actor.GameCharacter() self.guardianBoss.name = "Guardian" self.guardianBoss.HP = 1000 #purely arbitrary
def initRandomVehicles(self): for index in range(0, int(len(self.structures) * UNIVERSAL_SHIP_DENSITY)): pilot = Actor('miner') ship = Miner(pilot, self) pilot.setVehicle(ship) pilot.setProfession('miner') self.actors.append(pilot)
def __init__(self, name, remotes=None): self.name = name self.remotes = remotes self.state = 0 self.status = "running" log("Initializing robot", self.name) self.observer = Observer(self.name) self.thinker = Thinker(self.name) self.actor = Actor(self.name) self.session_done = False self.vague = False self.actions = [] self.rewards = [] self.cont_reward = [] if self.remotes: # Observer try: self.remotes['observer'] except: self.observer = Pyro4.Proxy(self.remotes['observer']) # Thinker try: self.remotes['thinker'] except: self.thinker = Pyro4.Proxy(self.remotes['thinker']) # Actor try: self.remotes['actor'] except: self.actor = Pyro4.Proxy(self.remotes['actor']) self.observer.initialize() self.observer.register_device('microphone', Microphone(self.name)) self.observer.register_device('camera', Camera(self.name)) self.actor.register_device('speaker', Speaker(self.name)) self.actor.register_device('eyes', Eyes(self.name))
def __init__(self, env, state_size, action_size, max_action): """ TD3 PyTorch Td3 (agent) policy network model. Args: - env: Openai Gym environment - state_size: Dimensionality of states. - action_size: Dimensionality of actions. - max_action: Maximum action value. """ self.env = env self.state_size = state_size self.action_size = action_size self.max_action = max_action self.actor = Actor(self.state_size, self.action_size, self.max_action) self.actor_target = Actor(self.state_size, self.action_size, self.max_action) self.actor_target.load_state_dict(self.actor.state_dict()) self.actor_optimizer = torch.optim.Adam(self.actor.parameters()) self.critic = Critic(self.state_size, self.action_size) self.critic_target = Critic(self.state_size, self.action_size) self.critic_target.load_state_dict(self.critic.state_dict()) self.critic_optimizer = torch.optim.Adam(self.critic.parameters())
def __loadStuff(self): if self.pole == None: self.pole = Actor.Actor() self.pole.loadModel('phase_4/models/props/fishing-pole-mod') self.pole.loadAnims({'cast': 'phase_4/models/props/fishing-pole-chan'}) self.pole.pose('cast', 0) self.ptop = self.pole.find('**/joint_attachBill') if self.bob == None: import Ripples self.bob = loader.loadModelCopy('phase_4/models/props/fishing_bob') self.ripples = Ripples.Ripples(self.nodePath) self.ripples.hide() if self.splashSound == None: self.splashSound = base.loadSfx('phase_4/audio/sfx/TT_splash1.mp3') return
def generate_new_actors(): """ 50% chance of generation every 200ms if less than MAX_ACTORS Circles shoot in from the sides """ if len(cfg.AI_ACTORS) < cfg.MAX_ACTORS and checkTimeInterval( cfg.GENERATION_INTERVAL) and checkProbability(cfg.GENERATION_PROB): new_circle = Actor.Circle() new_circle.x = 0 - new_circle.width if checkProbability( 0.5) else cfg.SCREEN_WIDTH new_circle.y = random.randint(0, cfg.SCREEN_HEIGHT - new_circle.height) initial_v = random.randint(new_circle.MOVE_SPEED, new_circle.MAX_SPEED) direction = 1 if new_circle.x < 0 else -1 new_circle.x_speed = initial_v * direction new_circle.add(cfg.ACTORS, cfg.AI_ACTORS, cfg.HEALTHY)
def get_actor(self, actor_id): """ Gets an Actor instance using an actor_id :param actor_id: string id of an actor :return: an Actor """ if actor_id == self.director: retrieve_id = "director-{0}".format(actor_id) director = True else: retrieve_id = actor_id director = False default = Actor.Actor('NonsenseActorId', director) if Actor.Actor.all_actors.get(retrieve_id, None): return Actor.Actor.all_actors[retrieve_id] else: return default
def __init__(self, node): AnimatedProp.AnimatedProp.__init__(self, node) self.periscope = Actor.Actor() self.periscope.reparentTo(self.node.getParent()) self.periscope.prepareBundle(self.node) self.periscope.loadAnims( {'anim': 'phase_3.5/models/props/HQ_periscope-chan'}) self.periscope.pose('anim', 0) self.node = self.periscope self.track = Sequence( Wait(2.0), self.periscope.actorInterval('anim', startTime=0.0, endTime=40 / 24.0), Wait(0.69999999999999996), self.periscope.actorInterval('anim', startTime=40 / 24.0, endTime=90 / 24.0), Wait(0.69999999999999996), self.periscope.actorInterval('anim', startTime=91 / 24.0, endTime=121 / 24.0), Wait(0.69999999999999996), self.periscope.actorInterval('anim', startTime=121 / 24.0, endTime=91 / 24.0), Wait(0.69999999999999996), self.periscope.actorInterval('anim', startTime=90 / 24.0, endTime=40 / 24.0), Wait(0.69999999999999996), self.periscope.actorInterval('anim', startTime=40 / 24.0, endTime=90 / 24.0), Wait(0.69999999999999996), self.periscope.actorInterval('anim', startTime=91 / 24.0, endTime=121 / 24.0), Wait(0.5), self.periscope.actorInterval('anim', startTime=121 / 24.0, endTime=148 / 24.0), Wait(3.0), name=self.uniqueName('HQPeriscope'))
def __init__(self, node): AnimatedProp.AnimatedProp.__init__(self, node) self.telescope = Actor.Actor() self.telescope.reparentTo(self.node.getParent()) self.telescope.prepareBundle(self.node) self.telescope.loadAnims( {'anim': 'phase_3.5/models/props/HQ_telescope-chan'}) self.telescope.pose('anim', 0) self.node = self.telescope self.track = Sequence( Wait(5.0), self.telescope.actorInterval('anim', startTime=0.0, endTime=32 / 24.0), Wait(0.5), self.telescope.actorInterval('anim', startTime=32 / 24.0, endTime=78 / 24.0), Wait(0.5), self.telescope.actorInterval('anim', startTime=79 / 24.0, endTime=112 / 24.0), Wait(0.5), self.telescope.actorInterval('anim', startTime=112 / 24.0, endTime=79 / 24.0), Wait(0.5), self.telescope.actorInterval('anim', startTime=78 / 24.0, endTime=32 / 24.0), Wait(0.5), self.telescope.actorInterval('anim', startTime=32 / 24.0, endTime=78 / 24.0), Wait(0.5), self.telescope.actorInterval('anim', startTime=79 / 24.0, endTime=112 / 24.0), Wait(0.5), self.telescope.actorInterval('anim', startTime=112 / 24.0, endTime=148 / 24.0), Wait(4.0), name=self.uniqueName('HQTelescope'))
def getLoseActor(self): if self.loseActor == None: (filePrefix, phase) = ModelDict[self.style.body] loseModel = 'phase_' + str(phase) + filePrefix + 'lose-mod' loseAnim = 'phase_' + str(phase) + filePrefix + 'lose' self.loseActor = Actor.Actor(loseModel, {'lose': loseAnim}) loseNeck = self.loseActor.find('**/joint-head') for part in self.headParts: part.instanceTo(loseNeck) self.setSuitClothes(self.loseActor) self.loseActor.setScale(self.scale) self.loseActor.setPos(self.getPos()) self.loseActor.setHpr(self.getHpr()) shadowJoint = self.loseActor.find('**/joint-shadow') dropShadow = loader.loadModelCopy('phase_3/models/props/drop_shadow') dropShadow.setScale(0.45000000000000001) dropShadow.setColor(0.0, 0.0, 0.0, 0.5) dropShadow.reparentTo(shadowJoint) return self.loseActor
def update(self): #handle keyboard control stuff here, make turns pass when appropriate Actor.update(self)
def __init__(self): Actor.__init__(self, 0, 0, '@', libtcod.Color(255, 255, 255))
class Agent(): """Interacts with and learns from the environment.""" def __init__(self, state_size, action_size, random_seed): """Initialize an Agent object. Params ====== state_size (int): dimension of each state action_size (int): dimension of each action random_seed (int): random seed """ self.state_size = state_size self.action_size = action_size self.seed = random.seed(random_seed) self.time_step = 0 # Actor Network (w/ Target Network) self.actor_local = Actor(state_size, action_size, random_seed).to(device) self.actor_target = Actor(state_size, action_size, random_seed).to(device) self.actor_optimizer = optim.Adam(self.actor_local.parameters(), lr=LR_ACTOR) # Critic Network (w/ Target Network) self.critic_local = Critic(state_size, action_size, random_seed).to(device) self.critic_target = Critic(state_size, action_size, random_seed).to(device) self.critic_optimizer = optim.Adam(self.critic_local.parameters(), lr=LR_CRITIC, weight_decay=WEIGHT_DECAY) # Noise process self.noise = OUNoise(action_size, random_seed) # Replay memory self.memory = ReplayBuffer(action_size, BUFFER_SIZE, BATCH_SIZE, random_seed) def step(self, state, action, reward, next_state, done): # print("inside step.") """Save experience in replay memory, and use random sample from buffer to learn.""" # Save experience / reward # self.memory.add(state, action, reward, next_state, done) for times in range(20): self.memory.add(state[times], action[times], reward[times], next_state[times], done[times]) # Learn, if enough samples are available in memory if self.time_step % 20 == 0 and len(self.memory) > BATCH_SIZE: for i in range(10): experiences = self.memory.sample() self.learn(experiences, GAMMA) def act(self, state, add_noise=True): """Returns actions for given state as per current policy.""" state = torch.from_numpy(state).float().to(device) self.actor_local.eval() with torch.no_grad(): action = self.actor_local(state).cpu().data.numpy() self.actor_local.train() if add_noise: action += self.noise.sample() return action def reset(self): self.noise.reset() def learn(self, experiences, gamma): """Update policy and value parameters using given batch of experience tuples. Q_targets = r + γ * critic_target(next_state, actor_target(next_state)) where: actor_target(state) -> action critic_target(state, action) -> Q-value Params ====== experiences (Tuple[torch.Tensor]): tuple of (s, a, r, s', done) tuples gamma (float): discount factor """ states, actions, rewards, next_states, dones = experiences # ---------------------------- update critic ---------------------------- # # Get predicted next-state actions and Q values from target models actions_next = self.actor_target(next_states) Q_targets_next = self.critic_target(next_states, actions_next) # Compute Q targets for current states (y_i) Q_targets = rewards + (gamma * Q_targets_next * (1 - dones)) # Compute critic loss Q_expected = self.critic_local(states, actions) critic_loss = F.mse_loss(Q_expected, Q_targets) # Minimize the loss self.critic_optimizer.zero_grad() critic_loss.backward() self.critic_optimizer.step() # ---------------------------- update actor ---------------------------- # # Compute actor loss actions_pred = self.actor_local(states) actor_loss = -self.critic_local(states, actions_pred).mean() # Minimize the loss self.actor_optimizer.zero_grad() actor_loss.backward() self.actor_optimizer.step() # ----------------------- update target networks ----------------------- # self.soft_update(self.critic_local, self.critic_target, TAU) self.soft_update(self.actor_local, self.actor_target, TAU) def soft_update(self, local_model, target_model, tau): """Soft update model parameters. θ_target = τ*θ_local + (1 - τ)*θ_target Params ====== local_model: PyTorch model (weights will be copied from) target_model: PyTorch model (weights will be copied to) tau (float): interpolation parameter """ for target_param, local_param in zip(target_model.parameters(), local_model.parameters()): target_param.data.copy_(tau*local_param.data + (1.0-tau)*target_param.data)
def OpenLevelUpWindow(): """Sets up the level up window.""" import GUIREC global LevelUpWindow, TextAreaControl, NewProfPoints, actor global DoneButton global NewSkillPoints, KitName, LevelDiff global Level, Classes, NumClasses, DualSwap, IsMulti global OldHPMax, OldSaves, OldLore, OldThaco, DeltaDSpells, DeltaWSpells global NewDSpells, NewWSpells, OldDSpells, OldWSpells, pc, HLACount, ClassName, IsDual LevelUpWindow = GemRB.LoadWindow (3) if GameCheck.IsBG2(): InfoButton = LevelUpWindow.GetControl (125) InfoButton.SetText (13707) InfoButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, LevelUpInfoPress) # hide "Character Generation" LevelUpWindow.DeleteControl(0x1000007e) DoneButton = LevelUpWindow.GetControl (0) DoneButton.SetText (11962) DoneButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, LevelUpDonePress) DoneButton.SetState (IE_GUI_BUTTON_DISABLED) DoneButton.MakeDefault() # also disable closing by ESC, so we don't stack upgrades DoneButton.MakeEscape () # name pc = GemRB.GameGetSelectedPCSingle () actor = Actor.Actor(pc) Label = LevelUpWindow.GetControl (0x10000000+90) Label.SetText (GemRB.GetPlayerName (pc)) # some current values OldHPMax = GemRB.GetPlayerStat (pc, IE_MAXHITPOINTS, 1) OldThaco = GemRB.GetPlayerStat (pc, IE_TOHIT, 1) OldLore = GemRB.GetPlayerStat (pc, IE_LORE, 1) for i in range (5): OldSaves[i] = GemRB.GetPlayerStat (pc, IE_SAVEVSDEATH+i, 1) # class Label = LevelUpWindow.GetControl (0x10000000+106) Label.SetText (GUICommon.GetActorClassTitle (pc)) print("Title: " + GUICommon.GetActorClassTitle (pc) + "\tActor Title: " + actor.ClassTitle()) Class = GemRB.GetPlayerStat (pc, IE_CLASS) print("Class: " + str(Class) + "\tActor Class: " + str(actor.classid)) # kit ClassName = GUICommon.GetClassRowName (Class, "class") Kit = GUICommon.GetKitIndex (pc) print("Kit: " + str(Kit) + "\tActor Kit: " + str(actor.KitIndex())) print("ClassName: " + ClassName + "\tActor ClassNames: " + str(actor.ClassNames())) # need this for checking gnomes RaceName = GemRB.GetPlayerStat (pc, IE_RACE, 1) RaceName = CommonTables.Races.FindValue (3, RaceName) RaceName = CommonTables.Races.GetRowName (RaceName) # figure our our proficiency table and index if Kit == 0: KitName = ClassName else: #rowname is just a number, the kitname is the first data column KitName = CommonTables.KitList.GetValue(Kit, 0) # our multiclass variables IsMulti = GUICommon.IsMultiClassed (pc, 1) Classes = [IsMulti[1], IsMulti[2], IsMulti[3]] NumClasses = IsMulti[0] # 2 or 3 if IsMulti; 0 otherwise IsMulti = NumClasses > 1 IsDual = 0 DualSwap = 0 # not multi, check dual if not IsMulti: # check if we're dual classed IsDual = GUICommon.IsDualClassed (pc, 1) Classes = [] # either dual or single only care about 1 class NumClasses = 1 # not dual, must be single if IsDual[0] == 0: Classes = [Class] else: # resolve kits to classes (new class goes first) if IsDual[0] == 3: # 1st kit Classes.append (CommonTables.KitList.GetValue (IsDual[2], 7)) else: # 1st class Classes.append (CommonTables.Classes.GetValue (IsDual[2], 5)) if IsDual[0] == 1: # 2nd kit Classes.append (CommonTables.KitList.GetValue (IsDual[1], 7)) else: # 2nd class Classes.append (CommonTables.Classes.GetValue (IsDual[1], 5)) # store a boolean for IsDual IsDual = IsDual[0] > 0 print("NumClasses: " + str(NumClasses) + "\tActor NumClasses: " + str(actor.NumClasses())) if IsDual: # convert the classes from indicies to class id's DualSwap = GUICommon.IsDualSwap (pc) ClassName = GUICommon.GetClassRowName (Classes[0], "index") KitName = ClassName # for simplicity throughout the code print("Classes: " + str(Classes) + "\tActor Classes: " + str(actor.Classes())) print("IsDual: " + str(IsDual > 0) + "\tActor IsDual: " + str(actor.isdual)) # get the next target levels and difference between levels Level = LUCommon.GetNextLevels(pc, Classes) LevelDiff = LUCommon.GetLevelDiff(pc, Level) # clear some globals, since we may get called multiple times with different classes DeltaWSpells = 0 DeltaDSpells = 0 OldDSpells = [0]*7 OldWSpells = [0]*9 NewDSpells = [0]*7 NewWSpells = [0]*9 # calculate the new spells (results are stored in global variables) GetNewSpells(pc, Classes, Level, LevelDiff, Kit) # this is handled by core # # setup class bonuses for this class # if IsMulti or IsDual or Kit == 0: # ABTable = CommonTables.ClassSkills.GetValue (TmpClassName, "ABILITIES") # else: # single-classed with a kit # ABTable = CommonTables.KitList.GetValue (str(Kit), "ABILITIES") # # # add the abilites if we have a table to ref # if ABTable != "*" and GemRB.HasResource (ABTable, RES_2DA, 1): # GUICommon.AddClassAbilities (pc, ABTable, Level[i], LevelDiff[i]) print("Actor CurrentLevels:" + str(actor.Levels())) print("Levels: " + str(Level) + "\tActor NextLevels: " + str(actor.NextLevels())) print("LevelDiffs: " + str(LevelDiff) + "\tActor LevelDiffs: " + str(actor.LevelDiffs())) #update our saves, thaco, hp and lore LUCommon.SetupSavingThrows (pc, Level) LUCommon.SetupThaco (pc, Level) LUCommon.SetupLore (pc, LevelDiff) LUCommon.SetupHP (pc, Level, LevelDiff) # we set up these labels so late, so they can show the new HP if GameCheck.IsBG1() or GameCheck.IsIWD1(): # armorclass Label = LevelUpWindow.GetControl (0x10000057) Label.SetText (str (GemRB.GetPlayerStat (pc, IE_ARMORCLASS))) Label.SetTooltip (17183) # hp now Label = LevelUpWindow.GetControl (0x10000058) Label.SetText (str (GemRB.GetPlayerStat (pc, IE_HITPOINTS))) Label.SetTooltip (17184) # hp max Label = LevelUpWindow.GetControl (0x10000059) Label.SetText (str (GemRB.GetPlayerStat (pc, IE_MAXHITPOINTS))) Label.SetTooltip (17378) # use total levels for HLAs HLACount = 0 if GameCheck.HasTOB(): # make sure SoA doesn't try to get it HLATable = GemRB.LoadTable("lunumab") # we need to check each level against a multi value (this is kinda screwy) if actor.multiclass: print("Actor HLA Names: " + str(["MULTI" + str(actor.NumClasses()) + name \ for name in actor.ClassNames()])) else: print("Actor HLA Names: " + str(actor.ClassNames())) for i in range (NumClasses): if IsMulti: # get the row name for lookup ex. MULTI2FIGHTER, MULTI3THIEF MultiName = GUICommon.GetClassRowName (Classes[i], "class") MultiName = "MULTI" + str(NumClasses) + MultiName else: MultiName = ClassName # if we can't learn for this class, we can't learn at all FirstLevel = HLATable.GetValue (MultiName, "FIRST_LEVEL", GTV_INT) if Level[i] < FirstLevel: HLACount = 0 break if (Level[i] - LevelDiff[i]) < FirstLevel: # count only from FirstLevel up HLACount += (Level[i] - FirstLevel + 1) else: HLACount += LevelDiff[i] # set values required by the hla level up code HLACount = HLACount // HLATable.GetValue (ClassName, "RATE", GTV_INT) GemRB.SetVar ("HLACount", HLACount) if GameCheck.IsBG2(): HLAButton = LevelUpWindow.GetControl (126) if HLACount: HLAButton.SetText (4954) HLAButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, LevelUpHLAPress) else: HLAButton.SetFlags (IE_GUI_BUTTON_DISABLED, OP_OR) # setup our profs Level1 = [] for i in range (len (Level)): Level1.append (Level[i]-LevelDiff[i]) if GameCheck.IsBG2(): LUProfsSelection.SetupProfsWindow (pc, LUProfsSelection.LUPROFS_TYPE_LEVELUP, LevelUpWindow, RedrawSkills, Level1, Level) else: LUProfsSelection.SetupProfsWindow (pc, LUProfsSelection.LUPROFS_TYPE_LEVELUP, LevelUpWindow, RedrawSkills, Level1, Level, 0, False, 0) NewProfPoints = GemRB.GetVar ("ProfsPointsLeft") #we autohide the skills and let SetupSkillsWindow show them if needbe for i in range (4): HideSkills (i) if GameCheck.IsBG2(): LUSkillsSelection.SetupSkillsWindow (pc, LUSkillsSelection.LUSKILLS_TYPE_LEVELUP, LevelUpWindow, RedrawSkills, Level1, Level) else: LUSkillsSelection.SetupSkillsWindow (pc, LUSkillsSelection.LUSKILLS_TYPE_LEVELUP, LevelUpWindow, RedrawSkills, Level1, Level, 0, False) NewSkillPoints = GemRB.GetVar ("SkillPointsLeft") if GameCheck.IsBG2(): TextAreaControl = LevelUpWindow.GetControl(110) TextAreaControl.SetText(GetLevelUpNews()) else: TextAreaControl = LevelUpWindow.GetControl(42) TextAreaControl.SetText(GUIREC.GetStatOverview(pc, LevelDiff)) RedrawSkills() LevelUpWindow.ShowModal (MODAL_SHADOW_GRAY) # if we have a sorcerer who can learn spells, we need to do spell selection for c in range(NumClasses): if Spellbook.HasSorcererBook (pc, Classes[c]) and DeltaWSpells > 0: LUSpellSelection.OpenSpellsWindow (pc, "SPLSRCKN", Level[c], LevelDiff[c])
Level([ " ", " ", "PPPP ", "PPPP ", " PPPPPPP ", " ", " ", "PPPPPPPP PPP", "PPPPPPPPPPPPPPPPPPPPPP", "PPPPPPPPPPPPPPPPPPPPPP", "PPPPPPPPPPPPPPPPPPPPPP", ])) game.enemyList.append([ Actor(10 * 64, 3 * 64, "Enemy"), Actor(14 * 64, 3 * 63, "Enemy"), Actor(12 * 64, 7 * 64, "Enemy") ]) game.nameList.append("Watch out!") game.levelList.append( Level([ " ", " ", " PPPPPP", " PPPPPPPPP", " PPPPPPPPPP ", " PPPPPPP ", " ", "PPPPP PPPP",
""" import Battlefield as field import Actor as actors import Advisor as advisors import Role as roles import matplotlib.pyplot as plt import math ateam = [] bteam = [] numA = 5 numB = 3 for i in range(numA): ateam.append(actors.Object(name="Reds " + str(i))) for i in range(numB): bteam.append(actors.Object(name="Blues " + str(i))) for human in ateam: human.addRole(roles.Fighter(hp=5, atk=1, defs=0)) human.addAI(advisors.Basic()) for human in bteam: human.addRole(roles.Fighter(hp=5, atk=1, defs=0)) human.addAI(advisors.Basic()) bf = field.Battlefield(ateam, bteam) for i in range(100):
def copyProp(prop): import Actor if isinstance(prop, Actor.Actor): return Actor.Actor(other=prop) else: return prop.copyTo(hidden)
env.reset() totalLife = env.getLives() if totalLife > 0: life0 = life1 = totalLife gameOver = False for frameCount in xrange(frameStart,frameMax): actionIndex = Actor.forward(sess, memory.History) reward = env.act(actionIndex) cumulativeReward += reward # reward = 0 observe = env.observe() gameOver = terminal = env.terminal() if totalLife > 0: life1 = env.getLives() if life1 < life0: reward += lifeReward terminal = True # env.observe()
Looking closer at the floor you can see blood stains. You get the feeling you should leave as soon as possible.""" ) # Build the links nodeA.undirectedConnect(nodeB, ["use"], "Grey hallway") nodeA.directedConnect(nodeC, ["use", "open"], "Green doorway") nodeC.directedConnect(nodeA, ["use", "open"], "White doorway") nodeD.undirectedConnect(nodeB, ["use", "climb"], "Ladder") # Build the player player_commands = { "use": UseCommand(), "open": OpenCommand(), "climb": ClimbCommand() } player = Actor("Some person", commandList=player_commands, location=nodeA) # Create the interface interface = Interface(default="Enter a command: ", options=player_commands) # Main loop running = True while running: if (player.moved): interface.output(player.location.describe()) player.moved = False userInput = interface.getInput() commandName, arguments = interface.getCommand(userInput) command = player.getCommand(commandName) commandError = None
def OpenLevelUpWindow(): """Sets up the level up window.""" import GUIREC global LevelUpWindow, TextAreaControl, NewProfPoints, actor global DoneButton global NewSkillPoints, KitName, LevelDiff global Level, Classes, NumClasses, DualSwap, IsMulti global OldHPMax, OldSaves, OldLore, OldThaco, DeltaDSpells, DeltaWSpells global NewDSpells, NewWSpells, OldDSpells, OldWSpells, pc, HLACount, ClassName, IsDual LevelUpWindow = GemRB.LoadWindow(3) if GameCheck.IsBG2(): InfoButton = LevelUpWindow.GetControl(125) InfoButton.SetText(13707) InfoButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, LevelUpInfoPress) DoneButton = LevelUpWindow.GetControl(0) DoneButton.SetText(11962) DoneButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, LevelUpDonePress) DoneButton.SetState(IE_GUI_BUTTON_DISABLED) DoneButton.SetFlags(IE_GUI_BUTTON_DEFAULT, OP_OR) # hide "Character Generation" Label = LevelUpWindow.CreateLabel( 0x1000007e, 0, 0, 0, 0, "NUMBER", "", IE_FONT_ALIGN_CENTER | IE_FONT_SINGLE_LINE) # name pc = GemRB.GameGetSelectedPCSingle() actor = Actor.Actor(pc) Label = LevelUpWindow.GetControl(0x10000000 + 90) Label.SetText(GemRB.GetPlayerName(pc)) if GameCheck.IsBG1() or GameCheck.IsIWD1(): # armorclass Label = LevelUpWindow.GetControl(0x10000057) Label.SetText(str(GemRB.GetPlayerStat(pc, IE_ARMORCLASS))) Label.SetTooltip(17183) # hp now Label = LevelUpWindow.GetControl(0x10000058) Label.SetText(str(GemRB.GetPlayerStat(pc, IE_HITPOINTS))) Label.SetTooltip(17184) # hp max Label = LevelUpWindow.GetControl(0x10000059) Label.SetText(str(GemRB.GetPlayerStat(pc, IE_MAXHITPOINTS))) Label.SetTooltip(17378) # some current values OldHPMax = GemRB.GetPlayerStat(pc, IE_MAXHITPOINTS, 1) OldThaco = GemRB.GetPlayerStat(pc, IE_TOHIT, 1) OldLore = GemRB.GetPlayerStat(pc, IE_LORE, 1) for i in range(5): OldSaves[i] = GemRB.GetPlayerStat(pc, IE_SAVEVSDEATH + i, 1) # class Label = LevelUpWindow.GetControl(0x10000000 + 106) Label.SetText(GUICommon.GetActorClassTitle(pc)) print "Title:", GUICommon.GetActorClassTitle( pc), "\tActor Title:", actor.ClassTitle() Class = GemRB.GetPlayerStat(pc, IE_CLASS) print "Class:", Class, "\tActor Class:", actor.classid SkillTable = GemRB.LoadTable("skills") # kit ClassName = GUICommon.GetClassRowName(Class, "class") Kit = GUICommon.GetKitIndex(pc) print "Kit:", Kit, "\tActor Kit:", actor.KitIndex() print "ClassName:", ClassName, "\tActor ClassNames:", actor.ClassNames() # need this for checking gnomes RaceName = GemRB.GetPlayerStat(pc, IE_RACE, 1) RaceName = CommonTables.Races.FindValue(3, RaceName) RaceName = CommonTables.Races.GetRowName(RaceName) # figure our our proficiency table and index if Kit == 0: KitName = ClassName else: #rowname is just a number, the kitname is the first data column KitName = CommonTables.KitList.GetValue(Kit, 0) # our multiclass variables IsMulti = GUICommon.IsMultiClassed(pc, 1) Classes = [IsMulti[1], IsMulti[2], IsMulti[3]] NumClasses = IsMulti[0] # 2 or 3 if IsMulti; 0 otherwise IsMulti = NumClasses > 1 IsDual = 0 DualSwap = 0 # not multi, check dual if not IsMulti: # check if we're dual classed IsDual = GUICommon.IsDualClassed(pc, 1) Classes = [IsDual[2], IsDual[1]] # make sure the new class is first # either dual or single only care about 1 class NumClasses = 1 # not dual, must be single if IsDual[0] == 0: Classes = [Class] else: # make sure Classes[1] is a class, not a kit if IsDual[0] == 1: # kit Classes[1] = CommonTables.KitList.GetValue(IsDual[1], 7) else: # class TmpClassName = GUICommon.GetClassRowName(Classes[1], "index") Classes[1] = CommonTables.Classes.GetValue(TmpClassName, "ID") # store a boolean for IsDual IsDual = IsDual[0] > 0 print "NumClasses:", NumClasses, "\tActor NumClasses:", actor.NumClasses() Level = [0] * 3 LevelDiff = [0] * 3 # reorganize the leves if we're dc so the one we want to use is in Level[0] # and the old one is in Level[1] (used to regain old class abilities) if IsDual: # convert the classes from indicies to class id's DualSwap = GUICommon.IsDualSwap(pc) ClassName = GUICommon.GetClassRowName(Classes[0], "index") KitName = ClassName # for simplicity throughout the code Classes[0] = CommonTables.Classes.GetValue(ClassName, "ID") # Class[1] is taken care of above # we need the old level as well if DualSwap: Level[1] = GemRB.GetPlayerStat(pc, IE_LEVEL) else: Level[1] = GemRB.GetPlayerStat(pc, IE_LEVEL2) print "Classes:", Classes, "\tActor Classes:", actor.Classes() print "IsDual:", IsDual > 0, "\tActor IsDual", actor.isdual hp = 0 HaveCleric = 0 # clear some globals, since we may get called multiple times with different classes DeltaWSpells = 0 DeltaDSpells = 0 OldDSpells = [0] * 7 OldWSpells = [0] * 9 NewDSpells = [0] * 7 NewWSpells = [0] * 9 # get a bunch of different things each level for i in range(NumClasses): # print "Class:",Classes[i] # we don't care about the current level, but about the to-be-achieved one # get the next level Level[i] = LUCommon.GetNextLevelFromExp( GemRB.GetPlayerStat(pc, IE_XP) / NumClasses, Classes[i]) TmpClassName = GUICommon.GetClassRowName(Classes[i], "class") # find the level diff for each classes (3 max, obviously) if i == 0: if DualSwap: LevelDiff[i] = Level[i] - GemRB.GetPlayerStat(pc, IE_LEVEL2) else: LevelDiff[i] = Level[i] - GemRB.GetPlayerStat(pc, IE_LEVEL) elif i == 1: LevelDiff[i] = Level[i] - GemRB.GetPlayerStat(pc, IE_LEVEL2) elif i == 2: LevelDiff[i] = Level[i] - GemRB.GetPlayerStat(pc, IE_LEVEL3) # print "Level (",i,"):",Level[i] # print "Level Diff (",i,"):",LevelDiff[i] # save our current and next spell amounts StartLevel = Level[i] - LevelDiff[i] DruidTable = CommonTables.ClassSkills.GetValue(TmpClassName, "DRUIDSPELL", GTV_STR) ClericTable = CommonTables.ClassSkills.GetValue( TmpClassName, "CLERICSPELL", GTV_STR) MageTable = CommonTables.ClassSkills.GetValue(TmpClassName, "MAGESPELL", GTV_STR) # see if we have mage spells if MageTable != "*": # we get 1 extra spell per level if we're a specialist Specialist = 0 if CommonTables.KitList.GetValue( Kit, 7) == 1: # see if we're a kitted mage Specialist = 1 if Spellbook.HasSorcererBook(pc, Classes[i]): MageTable = "SPLSRCKN" MageTable = GemRB.LoadTable(MageTable) # loop through each spell level and save the amount possible to cast (current) for j in range(MageTable.GetColumnCount()): NewWSpells[j] = MageTable.GetValue(str(Level[i]), str(j + 1), GTV_INT) OldWSpells[j] = MageTable.GetValue(str(StartLevel), str(j + 1), GTV_INT) if NewWSpells[ j] > 0: # don't want specialist to get 1 in levels they should have 0 NewWSpells[j] += Specialist if OldWSpells[j] > 0: OldWSpells[j] += Specialist DeltaWSpells = sum(NewWSpells) - sum(OldWSpells) elif ClericTable != "*": # check for cleric spells if not GemRB.HasResource(ClericTable, RES_2DA, 1): ClericTable = "MXSPLPRS" # iwd1 doesn't have a DRUIDSPELL column in the table ClericTable = GemRB.LoadTable(ClericTable) HaveCleric = 1 # same as above for j in range(ClericTable.GetColumnCount()): NewDSpells[j] = ClericTable.GetValue(str(Level[i]), str(j + 1), GTV_INT) OldDSpells[j] = ClericTable.GetValue(str(StartLevel), str(j + 1), GTV_INT) DeltaDSpells = sum(NewDSpells) - sum(OldDSpells) elif DruidTable != "*": # clerics have precedence in multis (ranger/cleric) if HaveCleric == 0: #use MXSPLPRS if we can't find the resource (SoA fix) if not GemRB.HasResource(DruidTable, RES_2DA): DruidTable = "MXSPLPRS" # check druid spells DruidTable = GemRB.LoadTable(DruidTable) # same as above for j in range(DruidTable.GetColumnCount()): NewDSpells[j] = DruidTable.GetValue( str(Level[i]), str(j + 1), GTV_INT) OldDSpells[j] = DruidTable.GetValue( str(StartLevel), str(j + 1), GTV_INT) DeltaDSpells = sum(NewDSpells) - sum(OldDSpells) # this is handled by core # # setup class bonuses for this class # if IsMulti or IsDual or Kit == 0: # ABTable = CommonTables.ClassSkills.GetValue (TmpClassName, "ABILITIES") # else: # single-classed with a kit # ABTable = CommonTables.KitList.GetValue (str(Kit), "ABILITIES") # # # add the abilites if we have a table to ref # if ABTable != "*" and GemRB.HasResource (ABTable, RES_2DA, 1): # GUICommon.AddClassAbilities (pc, ABTable, Level[i], LevelDiff[i]) print "Actor CurrentLevels:", actor.Levels() print "Levels:", Level, "Actor NextLevels:", actor.NextLevels() print "LevelDiffs:", LevelDiff, "Actor LevelDiffs:", actor.LevelDiffs() #update our saves, thaco, hp and lore LUCommon.SetupSavingThrows(pc, Level) LUCommon.SetupThaco(pc, Level) LUCommon.SetupLore(pc, LevelDiff) LUCommon.SetupHP(pc, Level, LevelDiff) # use total levels for HLAs HLACount = 0 if GameCheck.HasTOB(): # make sure SoA doesn't try to get it HLATable = GemRB.LoadTable("lunumab") # we need to check each level against a multi value (this is kinda screwy) if actor.multiclass: print "Actor HLA Names:",["MULTI"+str(actor.NumClasses())+name \ for name in actor.ClassNames()] else: print "Actor HLA Names:", actor.ClassNames() for i in range(NumClasses): if IsMulti: # get the row name for lookup ex. MULTI2FIGHTER, MULTI3THIEF MultiName = GUICommon.GetClassRowName(Classes[i], "class") MultiName = "MULTI" + str(NumClasses) + MultiName else: MultiName = ClassName # if we can't learn for this class, we can't learn at all FirstLevel = HLATable.GetValue(MultiName, "FIRST_LEVEL", GTV_INT) if Level[i] < FirstLevel: HLACount = 0 break if (Level[i] - LevelDiff[i]) < FirstLevel: # count only from FirstLevel up HLACount += (Level[i] - FirstLevel + 1) else: HLACount += LevelDiff[i] # set values required by the hla level up code HLACount = HLACount / HLATable.GetValue(ClassName, "RATE", GTV_INT) GemRB.SetVar("HLACount", HLACount) if GameCheck.IsBG2(): HLAButton = LevelUpWindow.GetControl(126) if HLACount: HLAButton.SetText(4954) HLAButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, LevelUpHLAPress) else: HLAButton.SetFlags(IE_GUI_BUTTON_DISABLED, OP_OR) # setup our profs Level1 = [] for i in range(len(Level)): Level1.append(Level[i] - LevelDiff[i]) if GameCheck.IsBG2(): LUProfsSelection.SetupProfsWindow( pc, LUProfsSelection.LUPROFS_TYPE_LEVELUP, LevelUpWindow, RedrawSkills, Level1, Level) else: LUProfsSelection.SetupProfsWindow( pc, LUProfsSelection.LUPROFS_TYPE_LEVELUP, LevelUpWindow, RedrawSkills, Level1, Level, 0, False, 0) NewProfPoints = GemRB.GetVar("ProfsPointsLeft") #we autohide the skills and let SetupSkillsWindow show them if needbe for i in range(4): HideSkills(i) if GameCheck.IsBG2(): LUSkillsSelection.SetupSkillsWindow( pc, LUSkillsSelection.LUSKILLS_TYPE_LEVELUP, LevelUpWindow, RedrawSkills, Level1, Level) else: LUSkillsSelection.SetupSkillsWindow( pc, LUSkillsSelection.LUSKILLS_TYPE_LEVELUP, LevelUpWindow, RedrawSkills, Level1, Level, 0, False) NewSkillPoints = GemRB.GetVar("SkillPointsLeft") if GameCheck.IsBG2(): TextAreaControl = LevelUpWindow.GetControl(110) TextAreaControl.SetText(GetLevelUpNews()) else: TextAreaControl = LevelUpWindow.GetControl(42) TextAreaControl.SetText(GUIREC.GetStatOverview(pc, LevelDiff)) RedrawSkills() GemRB.SetRepeatClickFlags(GEM_RK_DISABLE, OP_NAND) LevelUpWindow.ShowModal(MODAL_SHADOW_GRAY) # if we have a sorcerer who can learn spells, we need to do spell selection for c in range(len(Classes)): if Spellbook.HasSorcererBook(pc, Classes[c]) and DeltaWSpells > 0: LUSpellSelection.OpenSpellsWindow(pc, "SPLSRCKN", Level[c], LevelDiff[c])
[actor_belief_values[option] for option in action if action != []]) receiver_rewards.append( [receiver_reward_values[option] for option in action if action != []]) # Convert to numpy arrays. actions = np.array(actions) actor_rewards = np.array(actor_rewards) actor_beliefs = np.array(actor_beliefs) receiver_rewards = np.array(receiver_rewards) # Instantiate a state, actor, and observer. state = State(actions=actions, actor_rewards=actor_rewards, actor_beliefs=actor_beliefs, receiver_rewards=receiver_rewards) actor = Actor() observer = Observer(state, actor) print("Actions:") print(actions) index = 0 action = actions[index] print("Action:") print(action) # Run the sacrifice model. print(observer.sacrifice(action)) # Run the utilitarian model. print(observer.utilitarian(action, receiver_reward_values))
with tf.device(device): # if networkType == "CNN": # import NN # if RLType == 'Value': # with tf.variable_scope("train") as train_scope: # Q_train = NN.NN(opt,trainable=True) # with tf.variable_scope("target") as target_scope: # Q_target= NN.NN(opt,trainable=False) import Actor import Critic with tf.variable_scope("Critic") as Critic_Scope: Critic = Critic.NN(opt, trainable=True) with tf.variable_scope("Actor") as Actor_Scope: Actor = Actor.NN(opt, trainable=True) # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.1) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True sess = tf.InteractiveSession(config=config) sess.run(tf.initialize_all_variables()) frameStart = 0 saver = tf.train.Saver(max_to_keep=None) if loadModel is True: print 'Loading model from %s ...' % pathModel, saver.restore(sess, pathModel) print 'Finished\n'
def client_thread(t_conn): pre_client = "[" + str(t_conn.getpeername()[0]) + ":" + str( t_conn.getpeername()[1]) + "] " while True: # noinspection PyBroadException try: data = t_conn.recv(Constants.REQUEST_LENGTH) if not data: break now = datetime.datetime.now() c_msg = str(data, "utf8") c_t3 = "[" + str(now.day) + "." + str(now.month) + "." + str(now.year) + ", " \ + str(now.hour) + ":" + str(now.minute) + ":" + str(now.second) + "] " pre_c = c_t3 + pre_client while (len(pre_c)) < 47: pre_c += " " # UI_CLIENT_DATA_REQUEST if c_msg == Constants.UI_CLIENT_DATA_REQUEST: if log_data_requests: print(pre_c, "Received UI_CLIENT_DATA_REQUEST") msg = get_info() while len(bytes(msg, "utf8")) < Constants.SERVER_ANSWER_LENGTH: msg += "#" t_conn.sendall(bytes(msg, "utf8")) # UI_CLIENT_SENSOR_DATA_REQUEST if c_msg == Constants.UI_CLIENT_SENSOR_DATA_REQUEST: # TODO Send Sensor-Data requested by external rpi-Server pass # UI_CLIENT_COMMAND_IDENTIFIER if Constants.UI_CLIENT_COMMAND_IDENTIFIER in c_msg: c_msg = c_msg.replace("#", "") print(pre_c, "Received UI_CLIENT_COMMAND:", c_msg) c_parts = c_msg.split("_") c_cmd_name = c_parts[1] c_cmd_n_state = c_parts[2] actors[c_cmd_name].switch_to(int(c_cmd_n_state)) msg = get_info() while len(bytes(msg, "utf8")) < Constants.SERVER_ANSWER_LENGTH: msg += "#" t_conn.sendall(bytes(msg, "utf8")) # UI_CLIENT_ADD_DEVICE_IDENTIFIER if Constants.UI_CLIENT_ADD_DEVICE_IDENTIFIER in c_msg: c_msg = c_msg.replace("#", "") print(pre_c, "Received UI_CLIENT_ADD_DEVICE:", c_msg) c_parts = c_msg.split("_") n_name = c_parts[1] n_ip = c_parts[2] n_port = int(c_parts[3]) n_states = int(c_parts[4]) if n_name not in actors: if len(c_parts) == 5 + n_states: n_state_names = c_parts[5:len(c_parts)] actors[n_name] = Actor.Actor(n_ip, n_port, n_states, n_name, n_state_names) else: n_state_names = None actors[n_name] = Actor.Actor(n_ip, n_port, n_states, n_name) try: actors[n_name].connect() file = open(actors_file_name, 'r') all_text = "" line = file.readline() while line: all_text += line line = file.readline() file.close() all_text = all_text[0:len(all_text) - 2] all_text += ",\n" all_text += " \"" + n_name + "\": {\n" all_text += " \"ip\": \"" + n_ip + "\",\n" all_text += " \"port\": " + str(n_port) + ",\n" all_text += " \"state_count\": " + str( n_states) + ",\n" if n_state_names is not None: all_text += " \"state_names\": [" for name in n_state_names: all_text += "\"" + name + "\", " all_text = all_text[0:len(all_text) - 2] if n_state_names: all_text += "]\n" all_text += " }\n" all_text += "}" file = open(actors_file_name, "w") file.write(all_text) file.close() except: print("Actor not found.") del actors[n_name] msg = get_info() while len(bytes(msg, "utf8")) < Constants.SERVER_ANSWER_LENGTH: msg += "#" t_conn.sendall(bytes(msg, "utf8")) except Exception as e: print("[ERROR] " + str(e)) t_conn.close()
# File: I (Python 2.2) global startTime from PandaModules import * from ShowBaseGlobal import * from IntervalGlobal import * from Actor import * import Mopath boat = loader.loadModel('models/misc/smiley') boat.reparentTo(render) donald = Actor() donald.loadModel('phase_6/models/char/donald-wheel-1000') donald.loadAnims({ 'steer': 'phase_6/models/char/donald-wheel-wheel' }) donald.reparentTo(boat) dock = loader.loadModel('models/misc/smiley') dock.reparentTo(render) sound = loader.loadSfx('phase_6/audio/sfx/SZ_DD_waterlap.mp3') foghorn = loader.loadSfx('phase_6/audio/sfx/SZ_DD_foghorn.mp3') mp = Mopath.Mopath() mp.loadFile(Filename('phase_6/paths/dd-e-w')) boatMopath = MopathInterval(mp, boat, 'boatpath') boatTrack = Track([ boatMopath], 'boattrack') BOAT_START = boatTrack.getIntervalStartTime('boatpath') BOAT_END = boatTrack.getIntervalEndTime('boatpath') donaldSteerInterval = ActorInterval(donald, 'steer') donaldLoopInterval = ActorInterval(donald, 'steer', loop = 1, duration = 10.0) donaldSteerTrack = Track([ donaldSteerInterval, donaldLoopInterval], name = 'steerTrack')
tf.reset_default_graph() options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) meta = tf.RunMetadata() config = tf.ConfigProto(log_device_placement=True, device_count={ "CPU": 10, "GPU": 1 }, inter_op_parallelism_threads=10) with Sess(options, meta, config=config) as sess: workers = [] for i in range(settings.NB_ACTORS): with tf.device("/device:CPU:" + str(i)): workers.append(Actor.Actor(sess, i + 1)) print("Initializing learner...") with tf.device("/device:GPU:0"): learner = Learner(sess, *workers[0].get_env_features()) print("Learner initialized !\n") if settings.LOAD: learner.load() threads = [] for i in range(settings.NB_ACTORS): thread = threading.Thread(target=workers[i].run) threads.append(thread) threads.append(threading.Thread(target=learner.run))