Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- #from threading import Thread
- #import subprocess
- #import Queue
- import pexpect
- import ships
- import re
- import math
- class CLIGame:
- def __init__(self, command):
- self.game = pexpect.spawn(command)
- def readall(self):
- data = ''
- while True:
- try:
- char = self.game.read_nonblocking(timeout=0.05)
- except pexpect.TIMEOUT:
- return data
- else:
- data += char
- def send_cmd(self, cmd):
- data = ''
- self.game.sendline(cmd)
- return self.readall()
- class Trek(CLIGame):
- def __init__(self, length=0, difficulty=0, password='hunter2'):
- lengths = ['short', 'medium', 'long']
- difficulties = ['novice', 'fair', 'good', 'expert', 'commodore', 'impossible']
- if length >= len(lengths) or difficulty >= len(difficulties):
- raise ValueError
- CLIGame.__init__(self, 'trek')
- print self.send_cmd('\n'.join([
- '', # There's a "press return to continue" bit
- lengths[length],
- difficulties[difficulty],
- password
- ]))
- self.enterprise = ships.Enterprise([0, 0])
- self.update_pos()
- def coords_to_pos(self, coords):
- # y1,x1/y2,x2
- print coords
- coords = coords.split('/')
- print coords
- coords = [[int(j) for j in i.split(',')] for i in coords]
- print coords
- pos = (coords[0][0]+coords[1][1]*0.1, coords[0][1]+coords[1][0]*0.1)
- print pos
- return pos
- def update_pos(self):
- print 'Updating position data...'
- srscan = self.send_cmd('srscan')
- pos = re.search(
- r'position\s+?(\d+,\d+/\d+,\d+)',
- srscan
- )
- pos = pos.group(1)
- print 'Found pos string',pos
- pos = self.coords_to_pos(pos)
- print 'Converted to', pos
- self.enterprise.pos = pos
- def move_angle(self, angle, distance):
- self.send_cmd('m {} {}'.format(angle, distance))
- self.update_pos()
- class UserCommands:
- commands = []
- def string_to_code(self, string):
- for i, command in enumerate(self.commands):
- if string in string_list[0]:
- return command_code[i]
- return False
- class AutoTrek(Trek):
- user_commands = UserCommands()
- user_commands.commands = [
- [['attack', 'a'], 'self.send_cmd(self.generate_attack_command())']
- ]
- klingons = []
- def get_offset(self, target_pos):
- return (target_pos[0]-self.pos[0], target_pos[1]-self.enterprise.pos[1])
- def get_distance(self, target_pos):
- return math.hypot( *self.get_offset(target_pos) )
- def get_heading(self, target_pos):
- target_offset = self.get_offset(target_pos)
- heading = math.atan2(*target_offset)
- print 'heading (unconverted):',heading
- heading = math.degrees(heading)
- print 'heading (converted): ',heading
- # converted to int so that str(heading) doesn't have a .0 and mess up commands
- heading = int(round(heading))
- print 'heading (rounded): ',heading
- # this doesn't seem to work. It once output 360.
- while heading < 0:
- print 'heading is < 0'
- heading += 360
- print 'new heading: ',heading
- return heading
- def get_phaser_efficiency(self, distance):
- return ( 0.9**float(distance*10) * 98 + 0.5 ) / 100
- def generate_attack_command(self, *klingons):
- command = 'phaser manual'
- for klingon in klingons:
- command += '\n'
- efficiency = self.get_phaser_efficiency(
- self.get_distance(
- klingon.pos
- )
- )
- command += str(int(round(klingon.hp/efficiency)))+' '
- command += str(int(round(self.get_heading(klingon.pos))))+' '
- command += '0'
- command += '\n0'
- return command
- def user_input(self, cmd):
- if cmd[0] == '/':
- exec cmd[1:]
- else:
- print self.send_cmd(cmd)
- while True:
- ai = AutoTrek()
- while True:
- ai.user_input(raw_input('$'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement