def fsm_setup(self): self.fsm = FSM('INIT', []) self.fsm.set_default_transition(lgFSM.Error, 'INIT') self.fsm.add_transition_any('INIT', None, 'INIT') self.fsm.add_transition('RW', 'INIT', lgFSM.Root) self.fsm.add_transition('Xp', 'INIT', lgFSM.Period) self.fsm.add_transition('Wd', 'INIT', lgFSM.Declarative, 'DECL') self.fsm.add_transition('Wd', 'DECL', lgFSM.Declarative, 'DECL') self.fsm.add_transition('Ss', 'DECL', lgFSM.Subject, 'INIT') self.fsm.add_transition('AF', 'DECL', lgFSM.Object, 'INIT')
def __init__(self, initial_state, memory=[]): # Map (input_symbol, current_state) --> (action, next_state). self.state_transitions = {} # Map (current_state) --> (action, next_state). self.input_symbol = None self.initial_state = initial_state self.current_state = self.initial_state self.next_state = None self.action = None self.memory = memory self.registers = {} self.register_history = [] self.L_registers = {} self.R_registers = {} self.fsm = FSM('INIT') self.fsm.add_transition('front_left', 'INIT', None, 'FRONT_L') self.fsm.add_transition('front_right', 'INIT', None, 'FRONT_R')
class grammarFSM: def fsm_setup(self): self.fsm = FSM('INIT', []) self.fsm.set_default_transition(lgFSM.Error, 'INIT') self.fsm.add_transition_any('INIT', None, 'INIT') self.fsm.add_transition('RW', 'INIT', lgFSM.Root) self.fsm.add_transition('Xp', 'INIT', lgFSM.Period) self.fsm.add_transition('Wd', 'INIT', lgFSM.Declarative, 'DECL') self.fsm.add_transition('Wd', 'DECL', lgFSM.Declarative, 'DECL') self.fsm.add_transition('Ss', 'DECL', lgFSM.Subject, 'INIT') self.fsm.add_transition('AF', 'DECL', lgFSM.Object, 'INIT') def fsm_run(self, input): debug(input) return self.fsm.process_list(input)
def PaintWorld(): global players, limits, obs, enemies, time2, time1, time, debug, ts, enemy_bullets, player_bullets try: # Keys Pressed and saved in buffer keyOperations() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glLoadIdentity() glTranslatef(0.0, -20.0, -140.0) ####################### # Draw the Objects drawLife(players[0], enemies) # Plane drawPlane() # Limits of the world drawLimits(limits) # Obstacles drawObstacle(obstacle1) drawObstacle(obstacle2) drawObstacle(obstacle3) # Draw Food drawFood(food) # Player for player in players: if player.life == 0: print "" print " GAME OVER... " print "" print " Thanks for playing Battle Cubes :D " print "" sys.exit(0) drawAgent(player) #Agent's Bullets for bullet in player_bullets: drawBullet(bullet, 1) #Agent' Bullets for bullet in enemy_bullets: drawBullet(bullet, 2) # Enemies for enemy in enemies: enemy_bullets = enemy_bullets + enemy.bullets enemy.bullets = [] if enemy.life <= 0: enemies.remove(enemy) characters.remove(enemy) break drawEnemy(enemy) drawNavMesh(ts) ####################### ############# # Behaviour # ############# fsm = FSM() steerings = [ SteeringOutput(), SteeringOutput(), SteeringOutput(), SteeringOutput() ] # Iterating through all the enemies for i in range(0, len(enemies)): enemy = enemies[i] if enemy.life > 0: # Updating the state on the FSM enemy.state = fsm.update(enemy, characters, food) # Retrieving the new steering steerings[i] = getSteering(characters, player, enemy, obs, ts, food) physics = check_physics(characters, obs, obstacle_ob) # Get end just before calculating new positions, # velocities and accelerations time2 = datetime.now() time = ((time2 - time1).microseconds) / 1000000.0 # Updating player stats updatePlayer(player, time, obstacle_ob) #Updating agent's bullets from agent for b in player_bullets: if b.position[1] < 0: player_bullets.remove(b) else: #print b.velocity steering = SteeringOutput() # Acceleration in y-axes (gravity) b.update(steering, time) #Check bullet position check_shot(b, enemies) for ob in obstacle_ob: if inside_ob(b, ob): player_bullets.remove(b) break #for wall in obs: # agent_wall(agent,wall) #print "posicion " + str(b.position) #Updating enemy's bullets for b in enemy_bullets: if b.position[1] < 0: enemy_bullets.remove(b) else: #print b.velocity steering = SteeringOutput() # Acceleration in y-axes (gravity) b.update(steering, time) #Check bullet position check_shot(b, [player]) for ob in obstacle_ob: if inside_ob(b, ob): enemy_bullets.remove(b) break #print player.velocity # Updating enemies stats for i in range(0, len(enemies)): if enemies[i].life > 0: enemies[i].update(steerings[i], time, obstacle_ob, "auto") for col in physics: col[0].update(col[1], time, obstacle_ob, "auto") # Get initial time just after calculating everything time1 = datetime.now() #################### # End of Behaviour # #################### glutSwapBuffers() except Exception, e: traceback.print_exc() sys.exit(-1)
def PaintWorld(): global players, limits, obs, enemies, time2, time1, time, debug, ts, enemy_bullets, player_bullets, itime, ftime, rtime, mm, is_firing try: # Keys Pressed and saved in buffer keyOperations() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glLoadIdentity() glTranslatef(0.0, -20.0, -140.0) ####################### # Draw the Objects drawLife(players[0],enemies) # Plane drawPlane() # Limits of the world drawLimits(limits) # Obstacles drawObstacle(obstacle1) drawObstacle(obstacle2) drawObstacle(obstacle3) # Draw Food drawFood(food) # Player for player in players: if player.life == 0: print "" print " GAME OVER... " print "" print " Thanks for playing Battle Cubes :D " print "" sys.exit(0) drawAgent(player) #Agent's Bullets for bullet in player_bullets: drawBullet(bullet,1) #Agent' Bullets for bullet in enemy_bullets: drawBullet(bullet,2) # Enemies for enemy in enemies: enemy_bullets = enemy_bullets + enemy.bullets enemy.bullets = [] if enemy.life <= 0: enemies.remove(enemy) characters.remove(enemy) break drawEnemy(enemy) # Drawing the Nav Mesh & the sound drawNavMesh(ts,mm) ####################### # Refreshing the map connections ftime = datetime.now() rtime = (ftime - itime).microseconds if (rtime > 900000): itime = datetime.now() refreshMap(mm) go = True # Refreshing the is_firing status for i in range(len(mm.m)): for j in range(len(mm.m)): if mm.m[i][j] != -1 and mm.m[i][j]!=20: go = False if go: is_firing = False ############# # Behaviour # ############# fsm = FSM() steerings = [SteeringOutput(),SteeringOutput(),SteeringOutput(),SteeringOutput()] tg = TacticGraph() player.life = 20 # Iterating through all the enemies for i in range(0,len(enemies)): enemy = enemies[i] if enemy.life > 0: # Updating the state on the FSM enemy.state = fsm.update(enemy,characters,food,is_firing) # Retrieving the new steering steerings[i] = getSteering(characters,player, enemy,obs,ts,food,tg,mm) physics = check_physics(characters,obs,obstacle_ob) # Get end just before calculating new positions, # velocities and accelerations time2 = datetime.now() time = ( (time2 - time1).microseconds ) / 1000000.0 # Updating player stats updatePlayer(player,time,obstacle_ob) # Updating agent's bullets from agent for b in player_bullets: if b.position[1] < 0: player_bullets.remove(b) else: #print b.velocity steering = SteeringOutput() # Acceleration in y-axes (gravity) b.update(steering, time) #Check bullet position check_shot(b,enemies) for ob in obstacle_ob: if inside_ob(b,ob): player_bullets.remove(b) break #for wall in obs: # agent_wall(agent,wall) #print "posicion " + str(b.position) #Updating enemy's bullets for b in enemy_bullets: if b.position[1] < 0: enemy_bullets.remove(b) else: #print b.velocity steering = SteeringOutput() # Acceleration in y-axes (gravity) b.update(steering, time) #Check bullet position check_shot(b,[player]) for ob in obstacle_ob: if inside_ob(b,ob): enemy_bullets.remove(b) break #print player.velocity # Updating enemies stats for i in range(0,len(enemies)): if enemies[i].life > 0: enemies[i].update(steerings[i],time,obstacle_ob,"auto") for col in physics: col[0].update(col[1],time,obstacle_ob,"auto") # Get initial time just after calculating everything time1 = datetime.now() #################### # End of Behaviour # #################### glutSwapBuffers() except Exception, e: traceback.print_exc() sys.exit(-1)
class NDPDA_FSM: """ So this is a non-deterministic FSM can be used as a push-down Automata (PDA) since a PDA is a FSM + memory.""" registers = None def __init__(self, initial_state, memory=[]): # Map (input_symbol, current_state) --> (action, next_state). self.state_transitions = {} # Map (current_state) --> (action, next_state). self.input_symbol = None self.initial_state = initial_state self.current_state = self.initial_state self.next_state = None self.action = None self.memory = memory self.registers = {} self.register_history = [] self.L_registers = {} self.R_registers = {} self.fsm = FSM('INIT') self.fsm.add_transition('front_left', 'INIT', None, 'FRONT_L') self.fsm.add_transition('front_right', 'INIT', None, 'FRONT_R') def reset (self): self.current_state = self.initial_state self.input_symbol = None def set_register_state(self, in_state): #debug(in_state) pass def match_register_state(self, in_state): #debug(in_state) pass def add_transition(self, input_symbol, state, action=None, next_state=None): #debug(state) if next_state is None: next_state = state #debug(state) #debug(input_symbol) self.state_transitions[input_symbol] = (state, action, next_state) def get_transition(self, input_symbol): for regex_transitions in self.state_transitions: re_to_match = re.compile(regex_transitions) re_search = re_to_match.search(input_symbol) if re_search: yield self.state_transitions[regex_transitions] def process(self, input_symbol): output = None self.input_symbol = input_symbol for transitions in self.get_transition(self.input_symbol): self.state, self.action, self.next_state = transitions #debug(self.registers) if self.match_register_state(self.state): self.set_register_state(self.next_state) if self.state: output = {self.action:{'state':self.state,'set_state':self.next_state}} else: output = {self.action:{'set_state':self.next_state}} break self.current_state = self.next_state self.next_state = None self.memory.pop(0) if output: return output def process_list (self, input_symbols): debug(input_symbols) output = [] current_item = 0 for s in input_symbols: #debug(s) runner = self.process(s) output.append((current_item, runner)) current_item += 1 return output