def recreate(self, cache): """Update locations of objects Assumes stars and planet locations do not change. """ print "Updating starmap" self.objects = cache.objects self.starmap.clearTempLines() designs = self.getDesigns(cache) for object in cache.objects.values(): pos = self.getScaledPosition(tp_helpers.getAbsPosition(object)) #print "updating", object.id, object.name, object._subtype, "at", pos if object._subtype is FLEET: # Get parent system and the number of other fleets # FIXME: What if a fleet is destroyed parent = self.updateObjectIndex(object, "fleets", FLEET) if object.parent != 1 and self.starmap.hasObject(object.parent): pos = self.starmap.nodes[object.parent].position if self.starmap.hasObject(object.id): self.starmap.setFleet(object, pos, parent) else: fleet_type = (object.Ships[0][0][0][0] - 1) % designs[tp_helpers.getOwner(object)] self.starmap.addFleet(object, pos, parent, fleet_type) if hasattr(self.objects[0], "turn"): helpers.setWidgetText("TopBar/Turn", "Turn %i" % self.objects[0].turn) self.starmap.updateMapExtents() self.starmap.updateBoundingBox()
def moveTo(self, source, destination): """Orders a fleet to move to a destination source - ID of the fleet to move destination - ID of the destination object """ object = self.objects[source] if object.subtype is FLEET: target = self.objects[destination] descs = OrderDescs() order_types = objectutils.getOrderTypes(self.getCache(), source) for order_type in order_types.popitem()[1]: if not descs.has_key(order_type): continue descclass = descs[order_type] if descclass._name in ['Move', 'Move To', 'Intercept']: orderargs = [0, source, -1, descclass.subtype, 0, []] for prop in descclass.properties: if isinstance(prop, parameters.OrderParamAbsSpaceCoords): pos = [x for x in tp_helpers.getAbsPosition(target)] orderargs.append([pos]) if isinstance(prop, parameters.OrderParamObject): orderargs.append(destination) order = objects.Order(*orderargs) order._dirty = True self.sendOrder(source, order) self.starmap.connectObjects(source, destination) break
def calculateScale(self, objects): """Calculate a reasonable scale from a list of objects""" closest = None lower_left = [0, 0] upper_right = [0, 0] for obj in objects: if obj._subtype is not STAR: continue x, y, z = tp_helpers.getAbsPosition(obj) if lower_left[0] > x: lower_left[0] = x if lower_left[1] > y: lower_left[1] = y if upper_right[0] < x: upper_right[0] = x if upper_right[1] < y: upper_right[1] = y print upper_right, lower_left, closest map_width = abs(abs(upper_right[0]) - lower_left[0]) map_height = abs(abs(upper_right[1]) - lower_left[1]) print map_width, map_height scale = math.hypot(map_width, map_height) return scale
def moveTo(self, source, destination): """Orders a fleet to move to a destination source - ID of the fleet to move destination - ID of the destination object """ object = self.objects[source] if object.subtype is FLEET: target = self.objects[destination] descs = OrderDescs() order_types = objectutils.getOrderTypes(self.getCache(), source) for order_type in order_types.popitem()[1]: if not descs.has_key(order_type): continue descclass = descs[order_type] if descclass._name in ['Move', 'Move To', 'Intercept']: orderargs = [0, source, -1, descclass.subtype, 0, []] for prop in descclass.properties: if isinstance(prop, parameters.OrderParamAbsSpaceCoords): pos = [ x for x in tp_helpers.getAbsPosition(target) ] orderargs.append([pos]) if isinstance(prop, parameters.OrderParamObject): orderargs.append(destination) order = objects.Order(*orderargs) order._dirty = True self.sendOrder(source, order) self.starmap.connectObjects(source, destination) break
def create(self, cache): """Creates list of objects from cache""" print "creating the starmap" self.objects = cache.objects #self.starmap.createBackground() designs = self.getDesigns(cache) self.map_scale = self.calculateScale(self.objects.values()) pan_speed = 100000000 / self.map_scale * settings.distance_units if pan_speed < self.pan_speed: pan_speed = self.pan_speed else: self.pan_speed = pan_speed self.scroll_speed = pan_speed / 5 self.distance_units = settings.distance_units for object in self.objects.values(): pos = self.getScaledPosition(tp_helpers.getAbsPosition(object)) #print "creating", object.id, object.name, "\ttype:", object._subtype, "at", pos #if hasattr(object, "parent"): #print "parent of %s is %i" % (object.name, object.parent) if object._subtype is STAR: node = self.starmap.addStar(object, pos) if object._subtype is PLANET: # Get parent system and the number of other planets parent = self.updateObjectIndex(object, "planets", PLANET) if object.parent != 1 and self.starmap.hasObject(object.parent): pos = self.starmap.nodes[object.parent].position node = self.starmap.addPlanet(object, pos, parent) if object._subtype is FLEET: # Get parent system and the number of other fleets parent = self.updateObjectIndex(object, "fleets", FLEET) # Assign fleet type according to how many designs player has fleet_type = (object.Ships[0][0][0][1] - 1) % designs[tp_helpers.getOwner(object)] #print "ship_design: %i designs: %i fleet type: %i" % (object.Ships[0][0][0][0], designs[tp_helpers.getOwner(object), fleet_type) if object.parent != 1 and self.starmap.hasObject(object.parent): pos = self.starmap.nodes[object.parent].position node = self.starmap.addFleet(object, pos, parent, fleet_type, self.SELECTABLE) if object._subtype is WORMHOLE: end_pos = self.getScaledPosition(object.end) self.starmap.addWormHole(object, pos, end_pos) self.system_window.create(cache) self.message_window.create(cache) self.created = True if hasattr(self.objects[0], "turn"): helpers.setWidgetText("TopBar/Turn", "Turn %i" % self.objects[0].turn) self.starmap.updateMapExtents() self.starmap.autofit() self.starmap.updateBoundingBox() self.design_window.populateDesignsWindow(cache.designs)
def recreate(self, cache): """Update locations of objects Assumes stars and planet locations do not change. """ print "Updating starmap" self.objects = cache.objects self.starmap.clearTempLines() designs = self.getDesigns(cache) for object in cache.objects.values(): pos = self.getScaledPosition(tp_helpers.getAbsPosition(object)) #print "updating", object.id, object.name, object._subtype, "at", pos if object._subtype is FLEET: # Get parent system and the number of other fleets # FIXME: What if a fleet is destroyed parent = self.updateObjectIndex(object, "fleets", FLEET) if object.parent != 1 and self.starmap.hasObject( object.parent): pos = self.starmap.nodes[object.parent].position if self.starmap.hasObject(object.id): self.starmap.setFleet(object, pos, parent) else: fleet_type = (object.Ships[0][0][0][0] - 1) % designs[tp_helpers.getOwner(object)] self.starmap.addFleet(object, pos, parent, fleet_type) if hasattr(self.objects[0], "turn"): helpers.setWidgetText("TopBar/Turn", "Turn %i" % self.objects[0].turn) self.starmap.updateMapExtents() self.starmap.updateBoundingBox()
def setText(self, object): """Sets text inside information window""" cache = self.parent.getCache() text = "" text += "Name: %s (%s)\n" % (str(object.name), str(object.id)) text += "Position: %s\n" % str(tp_helpers.getAbsPosition(object)) text += "Velocity: %s\n" % str(tp_helpers.getVelocity(object)) text += "Size: %s\n" % str(object.size) if hasattr(object, "Ownership"): owner = tp_helpers.getOwner(object) if owner != -1: owner_name = cache.players[owner].name race_name = cache.players[owner].race_name text += "Owner: %s(%s)\n" % (owner_name, race_name) else: text += "Neutral\n" if hasattr(object, "Ships"): text += "-- Ships --\n" ships = [] total = 0 for group in object.Ships[0]: group = group[0] design_name = cache.designs[group[1]].name text += " %s: %i\n" % (design_name, group[2]) if hasattr(object, "Resources"): text += "-- Resources --\n" resources = [] print object.Resources for resource in object.Resources: if len(resource[0]) > 0: resource = resource[0][0] info = cache.resources[resource[0]] resource_string = " %s: %s available, %s minable, %s inaccessable" % \ (info.name_plural, resource[1], resource[2], resource[3]) text += resource_string helpers.setWidgetText("Information/Text", text)
def create(self, cache): """Creates list of objects from cache""" print "creating the starmap" self.objects = cache.objects #self.starmap.createBackground() designs = self.getDesigns(cache) self.map_scale = self.calculateScale(self.objects.values()) pan_speed = 100000000 / self.map_scale * settings.distance_units if pan_speed < self.pan_speed: pan_speed = self.pan_speed else: self.pan_speed = pan_speed self.scroll_speed = pan_speed / 5 self.distance_units = settings.distance_units for object in self.objects.values(): pos = self.getScaledPosition(tp_helpers.getAbsPosition(object)) #print "creating", object.id, object.name, "\ttype:", object._subtype, "at", pos #if hasattr(object, "parent"): #print "parent of %s is %i" % (object.name, object.parent) if object._subtype is STAR: node = self.starmap.addStar(object, pos) if object._subtype is PLANET: # Get parent system and the number of other planets parent = self.updateObjectIndex(object, "planets", PLANET) if object.parent != 1 and self.starmap.hasObject( object.parent): pos = self.starmap.nodes[object.parent].position node = self.starmap.addPlanet(object, pos, parent) if object._subtype is FLEET: # Get parent system and the number of other fleets parent = self.updateObjectIndex(object, "fleets", FLEET) # Assign fleet type according to how many designs player has fleet_type = (object.Ships[0][0][0][1] - 1) % designs[tp_helpers.getOwner(object)] #print "ship_design: %i designs: %i fleet type: %i" % (object.Ships[0][0][0][0], designs[tp_helpers.getOwner(object), fleet_type) if object.parent != 1 and self.starmap.hasObject( object.parent): pos = self.starmap.nodes[object.parent].position node = self.starmap.addFleet(object, pos, parent, fleet_type, self.SELECTABLE) if object._subtype is WORMHOLE: end_pos = self.getScaledPosition(object.end) self.starmap.addWormHole(object, pos, end_pos) self.system_window.create(cache) self.message_window.create(cache) self.created = True if hasattr(self.objects[0], "turn"): helpers.setWidgetText("TopBar/Turn", "Turn %i" % self.objects[0].turn) self.starmap.updateMapExtents() self.starmap.autofit() self.starmap.updateBoundingBox() self.design_window.populateDesignsWindow(cache.designs)