def warp(self, newpos, specialargslist=[]): """Warp!""" if 'override' in specialargslist: pass elif self.states.get('warp') != None and self.states['warp'][1] >= 10: pass elif len(newpos) != 2: raise Exception('Bad input: received newpos = ', str(newpos)) else: return None # this ship cannot warp if self.states[ 'position'] != None: # if for some reason this was the first warp... oripos = self.states['position'] mapmethods.updatePoints(oripos) else: pass self.states[ 'position'] = newpos # sets the position to the new position mapmethods.updatePoints(newpos) if 'silent' in specialargslist: return None else: print(self.states['name'] + ' warped to ' + str(newpos))
def updateView(self,bol=False): """Tells the faction to see all of his own objects, and to see all they can see.""" for i in self.allMyObjects(): i.scan('lazy',['free']) self.see(i) mapmethods.updatePoints(i.pos(),self) # updates the view in a smart way for a in self.persistent_view: self.see(a) mapmethods.updatePoints(a.pos(),self) # updates the view in a smart way if bol == True: self.dumpView()
def updateView(self, bol=False): """Tells the faction to see all of his own objects, and to see all they can see.""" for i in self.allMyObjects(): i.scan('lazy', ['free']) self.see(i) mapmethods.updatePoints(i.pos(), self) # updates the view in a smart way for a in self.persistent_view: self.see(a) mapmethods.updatePoints(a.pos(), self) # updates the view in a smart way if bol == True: self.dumpView()
def see(self, obj): """Adds the object to the faction's view.""" if isinstance(obj, objectmethods.Sobject) != True: raise Exception('Wrong. Received a non-sobject: ' + str(obj)) if obj in self.tracker: # if it's already there. #if obj.pos() not in self.view[obj.pos()] # if the object is not where you see it ## should not happen! #self.updateView(obj.pos()) # you now see it exactly there pass else: self.tracker.append(obj) mapmethods.updatePoints(obj.pos(), self) return None
def see(self,obj): """Adds the object to the faction's view.""" if isinstance(obj,objectmethods.Sobject) != True: raise Exception('Wrong. Received a non-sobject: ' +str(obj)) if obj in self.tracker: # if it's already there. #if obj.pos() not in self.view[obj.pos()] # if the object is not where you see it ## should not happen! #self.updateView(obj.pos()) # you now see it exactly there pass else: self.tracker.append(obj) mapmethods.updatePoints(obj.pos(),self) return None
def spawn(self, sobject): """Makes self spawn one or more sobjects.""" if isinstance(sobject, list): for elem in sobject: if isinstance(elem, Sobject) == False or elem.objectclass != 'ship': return 'Not a spawnable object: received a ' + str(elem) else: return self.spawn(elem) elif isinstance(sobject, Sobject): pass # passes only if the sobject is a Sobject object else: raise Exception('Bad input for spawn function; received a ' + str(sobject) + ' instead of a spawnable.') if self.states['spawn'] < sobject.size(): return 'Cannot spawn this ship from this object.' else: pass def dodge(tpl): """Returns all eight squares adjacent to a x,y position tuple.""" x, y = tpl # unpack return random.choice([((x + 1), (y + 1)), ((x - 1), (y - 1)), ((x + 1), (y - 1)), ((x - 1), (y + 1)), ((x - 1), (y)), ((x + 1), (y)), ((x - 1), (y)), ((x + 1), (y))]) selfpos = self.states['position'] if len(selfpos) != 2: raise Exception('Something wrong here, my position is ' + str(selfpos)) pos = dodge(selfpos) sobject.states['position'] = mapmethods.torusize(pos) # torusize! mapmethods.updatePoints(pos) # UPDATES the map! print( str(sobject) + ' spawned from ' + str(self) + ' at position ' + str(pos))
def move(self, arg): """Takes as input a position tuple, a direction, a sobject or a list of instructions. position tuple: goes there if it is not out of range, otherwise it moves as close as possible. direction: goes in that direction as far as it can list of instructions: it follows it as far as it can sobject: transforms it into a tuple (its position).""" if self.canMove(arg) == True: pass else: return None if arg != None: if isinstance(arg, Sobject): arg = arg.states['position'] else: raise Exception('No direction to move to. Arg is None.') if self.states.get('position') == None: # only for utility self.warp(arg, ['silent', 'override']) if self.states['position'] == arg: return None oripos = deepcopy(self.states['position']) instructionsDict = { 'u': (0, -1), 'd': (0, 1), 'l': (-1, 0), 'r': (1, 0), 'ul': (-1, -1), 'ur': (1, -1), 'dl': (-1, 1), 'dr': (1, 1) } if isinstance(arg, str): if not [letter in 'udlr' for letter in arg ] == [True for i in range(len(arg))]: # checks the syntax raise Exception('Input error for move function: received' + arg) else: counter = 0 while counter < len( arg ): # until the end of the string or the end of its speed; the shortest if counter >= self.states['speed']: print('The object cannot move that much: breaking...') break syllable = 'guruguru' if len(arg) >= counter + 2: syllable = arg[counter] + arg[ counter + 1] # picks the next two letters. if syllable in ['ul', 'ur', 'dl', 'dr']: # recognize X, Y = self.states['position'] X = X + instructionsDict[letter][0] Y = Y + instructionsDict[letter][1] if self.ap_pay('move2') == True: # AP checkpoint pass else: return 'The ship has no more action_points' self.states['position'] = mapmethods.torusize((X, Y)) counter + 2 # counter goes 2 up else: letter = arg[ counter] # parses the string from 0 to end X, Y = self.states['position'] X = X + instructionsDict[letter][0] Y = Y + instructionsDict[letter][1] if self.ap_pay('move1') == True: # AP checkpoint pass else: return 'The ship has no more action_points' self.states['position'] = mapmethods.torusize((X, Y)) counter += 1 elif isinstance(arg, tuple): arg = self.closestTowards(arg) dist = mapmethods.distance(oripos, arg) if self.ap_pay(dist) == True: # AP checkpoint pass else: raise Exception( 'Something wrong here. My APs are {}; oripos = {}, arg = {}' .format(self.actionpoints(), oripos, arg)) self.states['position'] = arg pass else: raise Exception('Unrecognized argument for move routine: ' + str(arg)) if self.objectclass != 'fleet': pass else: for ship in self.states['shiplist']: ship.states['position'] = self.states[ 'position'] # moves all ships in the shiplist at its new position newpos = deepcopy(self.states['position']) mapmethods.updatePoints(oripos, self) mapmethods.updatePoints(newpos, self) # important! updates the mapcode_tracker
def initBuilding(self, specialattrs): """Building Sobject initializer.""" buildingclass = specialattrs.get('buildingclass') self.states['buildingclass'] = buildingclass if buildingclass == None: raise Exception( 'Building constructor wants a buildingclass; no default building.' ) else: pass buildingdict = { 'base': { 'max_attack': 150, 'max_hull_integrity': 500, 'max_range': 4, 'max_shields': 20, 'max_spawn': 1, 'code': 'H' }, 'battery': { 'max_attack': 100, 'max_hull_integrity': 200, 'max_range': 5, 'max_shields': 15, 'code': 'B' }, 'buoy': { 'max_attack': 0, 'max_hull_integrity': 5, 'max_range': 0, 'max_shields': 0, 'code': 'b' } } possiblestates = [ 'max_attack', 'max_speed', 'max_range', 'max_warp', 'max_shields', 'max_spawn', 'cargo', 'code', 'max_hull_integrity', 'max_range' ] for a in possiblestates: # list of all possible states a freshly initialized building may have fetchedvalue = buildingdict[buildingclass].get(a, None) if fetchedvalue != None: self.states[a] = fetchedvalue else: pass for key in specialattrs: # here we should retrieve the position! self.states[key] = specialattrs[key] selfpos = self.states.get('position') if selfpos == None: raise Exception('Bad bad bad.') myasteroid = mapcode_tracker[selfpos] # finds his asteroid self.states['asteroid'] = myasteroid sobject_tracker.append(self) #### updates sobject_tracker if myasteroid.states['building'] != None: raise Exception( 'Trying to place a building on an already built asteroid.') myasteroid.states[ 'building'] = self # tells the asteroid that now there is a building on it self.states['hull_integrity'] = self.states[ 'max_hull_integrity'] # sets life to maximum self.states['special_conditions'] = [ ] # no special condition to begin with self.states[ 'action_points'] = 5.0 # sets the action points to 10, which is half the maximum self.setupSensorArrays() # Sets up default sensor arrays self.checkStates() mapmethods.updatePoints(self.states['position'])