Example #1
0
		def _set_entry(button, icon, building_id):
			"""Configure a single build menu button"""
			if self.unlocking_strategy == self.__class__.unlocking_strategies.single_per_increment and \
			   self.get_building_increments()[building_id] > self.session.world.player.settler_level:
				return

			building = Entities.buildings[building_id]
			button.helptext = self.session.db.get_building_tooltip(building_id)

			enough_res = False # don't show building by default
			if settlement is not None: # settlement is None when the mouse has left the settlement
				res_overview = self.session.ingame_gui.resource_overview
				button.mapEvents({
				  button.name+"/mouseEntered/buildtab" : Callback(res_overview.set_construction_mode, settlement, building.costs),
				  button.name+"/mouseExited/buildtab" : res_overview.close_construction_mode
				  })

				(enough_res, missing_res) = Build.check_resources({}, building.costs, settlement.owner, [settlement])
			#check whether to disable build menu icon (not enough res available)
			if enough_res:
				icon.image = "content/gui/images/buttons/buildmenu_button_bg.png"
				path = "content/gui/icons/buildmenu/{id:03d}{{mode}}.png".format(id=building_id)
				button.down_image = path.format(mode='_h')
				button.hover_image = path.format(mode='_h')
			else:
				icon.image = "content/gui/images/buttons/buildmenu_button_bg_bw.png"
				path = "content/gui/icons/buildmenu/greyscale/{id:03d}{{mode}}.png".format(id=building_id)
				button.down_image = path.format(mode='')
				button.hover_image = path.format(mode='')
			button.up_image = path.format(mode='')

			button.capture(Callback(self.build_callback, building_id))
Example #2
0
		def _set_entry(button, icon, building_id):
			"""Configure a single build menu button"""
			if self.unlocking_strategy == self.__class__.unlocking_strategies.single_per_increment and \
			   self.get_building_increments()[building_id] > self.session.world.player.settler_level:
				return

			building = Entities.buildings[building_id]
			button.helptext = self.session.db.get_building_tooltip(building_id)

			enough_res = False # don't show building by default
			if settlement is not None: # settlement is None when the mouse has left the settlement
				res_overview = self.session.ingame_gui.resource_overview
				button.mapEvents({
				  button.name+"/mouseEntered/buildtab" : Callback(res_overview.set_construction_mode, settlement, building.costs),
				  button.name+"/mouseExited/buildtab" : res_overview.close_construction_mode
				  })

				(enough_res, missing_res) = Build.check_resources({}, building.costs, settlement.owner, [settlement])
			#check whether to disable build menu icon (not enough res available)
			if enough_res:
				icon.image = "content/gui/images/buttons/buildmenu_button_bg.png"
				path = "content/gui/icons/buildmenu/{id:03d}{{mode}}.png".format(id=building_id)
				button.down_image = path.format(mode='_h')
				button.hover_image = path.format(mode='_h')
			else:
				icon.image = "content/gui/images/buttons/buildmenu_button_bg_bw.png"
				path = "content/gui/icons/buildmenu/greyscale/{id:03d}{{mode}}.png".format(id=building_id)
				button.down_image = path.format(mode='')
				button.hover_image = path.format(mode='')
			button.up_image = path.format(mode='')

			button.capture(Callback(self.build_callback, building_id))
Example #3
0
	def have_resources(self, extra_resources = None):
		"""Return a boolean showing whether we have the resources to build the building right now."""
		# the copy has to be made because Build.check_resources modifies it
		extra_resources = copy.copy(extra_resources) if extra_resources is not None else {}
		inventories = [self.land_manager.settlement, self.ship]
		(enough_res, _) = Build.check_resources(extra_resources, \
			Entities.buildings[self.building_id].costs, self.land_manager.owner, inventories)
		return enough_res
Example #4
0
 def have_resources(self, land_manager, ship=None, extra_resources=None):
     """Return a boolean showing whether we have the resources to build the building right now."""
     # the copy has to be made because Build.check_resources modifies it
     extra_resources = copy.copy(
         extra_resources) if extra_resources is not None else {}
     inventories = [land_manager.settlement, ship]
     return Build.check_resources(
         extra_resources, Entities.buildings[self.building_id].costs,
         land_manager.owner, inventories)[0]
Example #5
0
    def update_images(self):
        """Shows background images and building icons where defined
		(columns as follows, left to right):
		# 1,3,5,7.. | 2,4,6,8.. | 21,23,25,27.. | 22,24,26,28..
		"""
        settlement = LastActivePlayerSettlementManager().get()
        for position, building_id in self.__class__.image_data[self.tabindex].iteritems():
            button = self.widget.child_finder("button_{position}".format(position=position))
            building = Entities.buildings[building_id]

            icon = self.widget.child_finder("icon_{position}".format(position=position))

            # xgettext:python-format
            button.helptext = self.session.db.get_building_tooltip(building_id)

            enough_res = False  # don't show building by default
            if settlement is not None:  # settlement is None when the mouse has left the settlement
                res_overview = self.session.ingame_gui.resource_overview
                cbs = (
                    Callback(res_overview.set_construction_mode, settlement, building.costs),
                    Callback(res_overview.close_construction_mode),
                )

                # can't use mapEvents since the events are taken by the tooltips.
                # they do however provide an auxiliary way around this:
                button.clear_entered_callbacks()
                button.clear_exited_callbacks()
                button.add_entered_callback(cbs[0])
                button.add_exited_callback(cbs[1])

                (enough_res, missing_res) = Build.check_resources({}, building.costs, settlement.owner, [settlement])
                # check whether to disable build menu icon (not enough res available)
                # TODO this does not refresh right now, the icons should get active
                # as soon as enough res are available!
            if enough_res:
                icon.image = "content/gui/images/buttons/buildmenu_button_bg.png"
                path = "content/gui/icons/buildmenu/{id:03d}{{mode}}.png".format(id=building_id)
                button.down_image = path.format(mode="_h")
                button.hover_image = path.format(mode="_h")
            else:
                icon.image = "content/gui/images/buttons/buildmenu_button_bg_bw.png"
                path = "content/gui/icons/buildmenu/greyscale/{id:03d}{{mode}}.png".format(id=building_id)
                button.down_image = path.format(mode="")
                button.hover_image = path.format(mode="")
            button.up_image = path.format(mode="")

            button.capture(self.callback_mapping[building_id])
Example #6
0
        def _set_entry(button, icon, building_id):
            """Configure a single build menu button"""
            if self.unlocking_strategy == self.__class__.unlocking_strategies.single_per_tier and \
               self.get_building_tiers()[building_id] > self.session.world.player.settler_level:
                return

            building = Entities.buildings[building_id]
            button.helptext = self.session.db.get_building_tooltip(building_id)

            # Add necessary resources to tooltip
            # [br] means newline
            button.helptext += u'[br]' + _('Resources needed:') + u'[br]'
            for resource_id, amount_needed in sorted(building.costs.items()):
                resource_name = self.session.db.get_res_name(resource_id)
                button.helptext += u'[br]'
                #xgettext:python-format
                # You usually do not need to change anything here when translating
                button.helptext += _('{resource}: {amount}').format(
                    resource=resource_name, amount=amount_needed)

            enough_res = False  # don't show building by default
            if settlement is not None:  # settlement is None when the mouse has left the settlement
                res_overview = self.session.ingame_gui.resource_overview
                show_costs = Callback(res_overview.set_construction_mode,
                                      settlement, building.costs)
                button.mapEvents({
                    button.name + "/mouseEntered/buildtab":
                    show_costs,
                    button.name + "/mouseExited/buildtab":
                    res_overview.close_construction_mode
                })

                (enough_res,
                 missing_res) = Build.check_resources({}, building.costs,
                                                      settlement.owner,
                                                      [settlement])
            #check whether to disable build menu icon (not enough res available)
            if enough_res:
                icon.image = "content/gui/images/buttons/buildmenu_button_bg.png"
                button.path = "icons/buildmenu/{id:03d}".format(id=building_id)
            else:
                icon.image = "content/gui/images/buttons/buildmenu_button_bg_bw.png"
                button.path = "icons/buildmenu/greyscale/{id:03d}".format(
                    id=building_id)

            button.capture(Callback(self.build_callback, building_id))
Example #7
0
        def _set_entry(button, icon, building_id):
            """Configure a single build menu button"""
            if self.unlocking_strategy == self.__class__.unlocking_strategies.single_per_tier and \
               self.get_building_tiers()[building_id] > self.session.world.player.settler_level:
                return

            building = Entities.buildings[building_id]
            button.helptext = building.get_tooltip()

            # Add necessary resources to tooltip text.
            # tooltip.py will then place icons from this information.
            required_resources = ''
            for resource_id, amount_needed in sorted(building.costs.items()):
                required_resources += ' {}:{}'.format(resource_id,
                                                      amount_needed)
            required_text = '[[Buildmenu{}]]'.format(required_resources)
            button.helptext = required_text + button.helptext

            enough_res = False  # don't show building by default
            if settlement is not None:  # settlement is None when the mouse has left the settlement
                res_overview = self.session.ingame_gui.resource_overview
                show_costs = Callback(res_overview.set_construction_mode,
                                      settlement, building.costs)
                button.mapEvents({
                    button.name + "/mouseEntered/buildtab":
                    show_costs,
                    button.name + "/mouseExited/buildtab":
                    res_overview.close_construction_mode
                })

                (enough_res,
                 missing_res) = Build.check_resources({}, building.costs,
                                                      settlement.owner,
                                                      [settlement])
            # Check whether to disable build menu icon (not enough res available).
            if enough_res:
                icon.image = "content/gui/images/buttons/buildmenu_button_bg.png"
                button.path = "icons/buildmenu/{id:03d}".format(id=building_id)
            else:
                icon.image = "content/gui/images/buttons/buildmenu_button_bg_bw.png"
                button.path = "icons/buildmenu/greyscale/{id:03d}".format(
                    id=building_id)

            button.capture(Callback(self.build_callback, building_id))
        def _set_entry(button, icon, building_id):
            """Configure a single build menu button"""
            if (
                self.unlocking_strategy == self.__class__.unlocking_strategies.single_per_tier
                and self.get_building_tiers()[building_id] > self.session.world.player.settler_level
            ):
                return

            building = Entities.buildings[building_id]
            button.helptext = building.get_tooltip()

            # Add necessary resources to tooltip text.
            # tooltip.py will then place icons from this information.
            required_resources = ""
            for resource_id, amount_needed in sorted(building.costs.items()):
                required_resources += " %s:%s" % (resource_id, amount_needed)
            required_text = "[[Buildmenu%s]]" % (required_resources)
            button.helptext = required_text + button.helptext

            enough_res = False  # don't show building by default
            if settlement is not None:  # settlement is None when the mouse has left the settlement
                res_overview = self.session.ingame_gui.resource_overview
                show_costs = Callback(res_overview.set_construction_mode, settlement, building.costs)
                button.mapEvents(
                    {
                        button.name + "/mouseEntered/buildtab": show_costs,
                        button.name + "/mouseExited/buildtab": res_overview.close_construction_mode,
                    }
                )

                (enough_res, missing_res) = Build.check_resources({}, building.costs, settlement.owner, [settlement])
                # Check whether to disable build menu icon (not enough res available).
            if enough_res:
                icon.image = "content/gui/images/buttons/buildmenu_button_bg.png"
                button.path = "icons/buildmenu/{id:03d}".format(id=building_id)
            else:
                icon.image = "content/gui/images/buttons/buildmenu_button_bg_bw.png"
                button.path = "icons/buildmenu/greyscale/{id:03d}".format(id=building_id)

            button.capture(Callback(self.build_callback, building_id))
		def _set_entry(button, icon, building_id):
			"""Configure a single build menu button"""
			if self.unlocking_strategy == self.__class__.unlocking_strategies.single_per_tier and \
			   self.get_building_tiers()[building_id] > self.session.world.player.settler_level:
				return

			building = Entities.buildings[building_id]
			button.helptext = building.get_tooltip()

			# Add necessary resources to tooltip
			# [br] means newline
			button.helptext += u'[br]' + _('Resources needed:') + u'[br]'
			for resource_id, amount_needed in sorted(building.costs.items()):
				resource_name = self.session.db.get_res_name(resource_id)
				button.helptext += u'[br]'
				#xgettext:python-format
				# You usually do not need to change anything here when translating
				button.helptext += _('{resource}: {amount}').format(resource=resource_name, amount=amount_needed)

			enough_res = False # don't show building by default
			if settlement is not None: # settlement is None when the mouse has left the settlement
				res_overview = self.session.ingame_gui.resource_overview
				show_costs = Callback(res_overview.set_construction_mode, settlement, building.costs)
				button.mapEvents({
				  button.name+"/mouseEntered/buildtab" : show_costs,
				  button.name+"/mouseExited/buildtab" : res_overview.close_construction_mode
				  })

				(enough_res, missing_res) = Build.check_resources({}, building.costs, settlement.owner, [settlement])
			#check whether to disable build menu icon (not enough res available)
			if enough_res:
				icon.image = "content/gui/images/buttons/buildmenu_button_bg.png"
				button.path = "icons/buildmenu/{id:03d}".format(id=building_id)
			else:
				icon.image = "content/gui/images/buttons/buildmenu_button_bg_bw.png"
				button.path = "icons/buildmenu/greyscale/{id:03d}".format(id=building_id)

			button.capture(Callback(self.build_callback, building_id))
Example #10
0
	def have_resources(cls, inventory_holders, owner):
		return Build.check_resources({}, cls.costs, owner, inventory_holders)[0]
Example #11
0
    def preview_build(self, point1, point2, force=False):
        """Display buildings as preview if build requirements are met"""
        #self.session.view.renderer['InstanceRenderer'].removeAllColored()
        self.log.debug("BuildingTool: preview build at %s, %s", point1, point2)
        new_buildings = self._class.check_build_line(self.session,
                                                     point1,
                                                     point2,
                                                     rotation=self.rotation,
                                                     ship=self.ship)
        # optimization: If only one building is in the preview and the position hasn't changed
        # => don't preview. Otherwise the preview is redrawn on every mouse move
        if not force and len(new_buildings) == len(self.buildings) == 1 and \
           new_buildings[0] == self.buildings[0]:
            return  # we don't want to redo the preview

        # remove old fife instances and coloring
        self._remove_building_instances()

        # get new ones
        self.buildings = new_buildings
        # resize list of action set ids to new buildings
        self.buildings_action_set_ids = self.buildings_action_set_ids + (
            [None] *
            (len(self.buildings) - len(self.buildings_action_set_ids)))
        self.buildings_action_set_ids = self.buildings_action_set_ids[:len(
            self.buildings)]
        # delete old infos
        self.buildings_fife_instances.clear()
        self.buildings_missing_resources.clear()

        settlement = None  # init here so we can access it below loop
        needed_resources = {}
        # check if the buildings are buildable and color them appropriately
        for i, building in enumerate(self.buildings):
            # get gfx for the building
            # workaround for buildings like settler, that don't use the current level of
            # the player, but always start at a certain lvl
            level = self._class.get_initial_level(self.session.world.player)

            if self._class.id == BUILDINGS.TREE and not building.buildable:
                continue  # Tree/ironmine that is not buildable, don't preview
            else:
                fife_instance, action_set_id = \
                 self._class.getInstance(self.session, building.position.origin.x,
                                building.position.origin.y, rotation=building.rotation,
                                action=building.action, level=level,
                                action_set_id=self.buildings_action_set_ids[i])
                self.buildings_fife_instances[building] = fife_instance
                # remember action sets per order of occurrence
                # (this is far from good when building lines, but suffices for our purposes, which is mostly single build)
                self.buildings_action_set_ids[i] = action_set_id

            settlement = self.session.world.get_settlement(
                building.position.origin)
            if settlement is not None and settlement.owner != self.session.world.player:
                settlement = None  # no fraternizing with the enemy, else there would be peace

            if self._class.id != BUILDINGS.WAREHOUSE:
                # Player shouldn't be allowed to build in this case, else it can trigger
                # a new_settlement notification
                if settlement is None:
                    building.buildable = False

            # check required resources
            (enough_res, missing_res) = Build.check_resources(
                needed_resources, self._class.costs, self.session.world.player,
                [settlement, self.ship])
            if building.buildable and not enough_res:
                # make building red
                self.renderer.addColored(
                    self.buildings_fife_instances[building],
                    *self.not_buildable_color)
                building.buildable = False
                # set missing info for gui
                self.buildings_missing_resources[building] = missing_res

            # color this instance with fancy stuff according to buildability

            # this order determines highlight priority
            # draw ordinary ranges first, then later color related buildings (they are more important)
            self._make_surrounding_transparent(building)
            self._color_preview_building(building)
            if building.buildable:
                self._draw_preview_building_range(building, settlement)
            self._highlight_related_buildings_in_range(building, settlement)
            self._highlight_inversely_related_buildings(building, settlement)

        self.session.ingame_gui.resource_overview.set_construction_mode(
            self.ship if self.ship is not None else settlement,
            needed_resources)
        self._add_listeners(self.ship if self.ship is not None else settlement)
Example #12
0
	def preview_build(self, point1, point2, force=False):
		"""Display buildings as preview if build requirements are met"""
		#self.session.view.renderer['InstanceRenderer'].removeAllColored()
		self.log.debug("BuildingTool: preview build at %s, %s", point1, point2)
		new_buildings = self._class.check_build_line(self.session, point1, point2,
		                                             rotation=self.rotation, ship=self.ship)
		# optimization: If only one building is in the preview and the position hasn't changed
		# => don't preview. Otherwise the preview is redrawn on every mouse move
		if not force and len(new_buildings) == len(self.buildings) == 1 and \
		   new_buildings[0] == self.buildings[0]:
			return # we don't want to redo the preview

		# remove old fife instances and coloring
		self._remove_building_instances()

		# get new ones
		self.buildings = new_buildings
		# resize list of action set ids to new buildings
		self.buildings_action_set_ids = self.buildings_action_set_ids + ([None] * (len(self.buildings) - len(self.buildings_action_set_ids)))
		self.buildings_action_set_ids = self.buildings_action_set_ids[ : len(self.buildings) ]
		# delete old infos
		self.buildings_fife_instances.clear()
		self.buildings_missing_resources.clear()

		settlement = None # init here so we can access it below loop
		needed_resources = {}
		# check if the buildings are buildable and color them appropriately
		for i, building in enumerate(self.buildings):
			# get gfx for the building
			# workaround for buildings like settler, that don't use the current level of
			# the player, but always start at a certain lvl
			level = self._class.get_initial_level(self.session.world.player)

			if self._class.id == BUILDINGS.TREE and not building.buildable:
				continue # Tree/ironmine that is not buildable, don't preview
			else:
				fife_instance, action_set_id = \
					self._class.getInstance(self.session, building.position.origin.x,
								            building.position.origin.y, rotation=building.rotation,
								            action=building.action, level=level,
								            action_set_id=self.buildings_action_set_ids[i])
				self.buildings_fife_instances[building] = fife_instance
				# remember action sets per order of occurrence
				# (this is far from good when building lines, but suffices for our purposes, which is mostly single build)
				self.buildings_action_set_ids[i] = action_set_id

			settlement = self.session.world.get_settlement(building.position.origin)
			if settlement is not None and settlement.owner != self.session.world.player:
				settlement = None # no fraternizing with the enemy, else there would be peace

			if self._class.id != BUILDINGS.WAREHOUSE:
				# Player shouldn't be allowed to build in this case, else it can trigger
				# a new_settlement notification
				if settlement is None:
					building.buildable = False


			# check required resources
			(enough_res, missing_res) = Build.check_resources(needed_resources, self._class.costs,
			                                                  self.session.world.player, [settlement, self.ship])
			if building.buildable and not enough_res:
					# make building red
					self.renderer.addColored(self.buildings_fife_instances[building],
					                         *self.not_buildable_color)
					building.buildable = False
					# set missing info for gui
					self.buildings_missing_resources[building] = missing_res

			# color this instance with fancy stuff according to buildability

			# this order determines highlight priority
			# draw ordinary ranges first, then later color related buildings (they are more important)
			self._make_surrounding_transparent(building)
			self._color_preview_building(building)
			if building.buildable:
				self._draw_preview_building_range(building, settlement)
			self._highlight_related_buildings_in_range(building, settlement)
			self._highlight_inversely_related_buildings(building, settlement)

		self.session.ingame_gui.resource_overview.set_construction_mode(
			self.ship if self.ship is not None else settlement,
		  needed_resources
		)
		self._add_listeners(self.ship if self.ship is not None else settlement)
    def preview_build(self, point1, point2, force=False):
        """Display buildings as preview if build requirements are met"""
        #self.session.view.renderer['InstanceRenderer'].removeAllColored()
        self.log.debug("BuildingTool: preview build at %s, %s", point1, point2)
        new_buildings = self._class.check_build_line(self.session,
                                                     point1,
                                                     point2,
                                                     rotation=self.rotation,
                                                     ship=self.ship)
        # optimisation: If only one building is in the preview and the position hasn't changed
        # => don't preview. Otherwise the preview is redrawn on every mouse move
        if not force and len(new_buildings) == len(self.buildings) == 1 and \
           new_buildings[0] == self.buildings[0]:
            return  # we don't want to redo the preview

        # remove old fife instances and coloring
        self._remove_building_instances()

        # get new ones
        self.buildings = new_buildings
        # delete old infos
        self.buildings_fife_instances.clear()
        self.buildings_missing_resources.clear()

        settlement = None  # init here so we can access it below loop
        neededResources, usableResources = {}, {}
        # check if the buildings are buildable and color them appropriatly
        for building in self.buildings:
            # make surrounding transparent
            self._make_surrounding_transparent(building.position)

            # get gfx for the building
            # workaround for buildings like settler, that don't use the current level of
            # the player, but always start at a certain lvl
            level = self.session.world.player.settler_level if \
                  not hasattr(self._class, "default_level_on_build") else \
                  self._class.default_level_on_build

            if self._class.id == BUILDINGS.TREE_CLASS and not building.buildable:
                continue  # Tree/ironmine that is not buildable, don't preview
            else:
                self.buildings_fife_instances[building] = \
                    self._class.getInstance(self.session, building.position.origin.x, \
                                            building.position.origin.y, rotation=building.rotation,
                                            action=building.action, level=level)

            if self._class.id == BUILDINGS.BRANCH_OFFICE_CLASS:
                settlement = self.session.world.get_settlement(
                    building.position.center())
            else:
                # Player shouldn't be allowed to build in this case, else it can trigger
                # a new_settlement notificaition
                settlement = self.session.world.get_settlement(
                    building.position.origin)
                if settlement is None:
                    building.buildable = False

            if building.buildable:
                # building seems to buildable, check res too now
                (enough_res, missing_res) = Build.check_resources(
                    neededResources, self._class.costs,
                    self.session.world.player, [settlement, self.ship])
                if not enough_res:
                    # make building red
                    self.renderer.addColored(
                        self.buildings_fife_instances[building],
                        *self.not_buildable_color)
                    building.buildable = False
                    # set missing info for gui
                    self.buildings_missing_resources[building] = missing_res
                    # building isn't buildable after all, assemble strange dict values for gui
                    for resource in self._class.costs:
                        usableResources[resource] = usableResources.get(resource, 0) + \
                                       self._class.costs[resource]

            if building.buildable:
                # Tile might still have not buildable color -> remove it
                self.renderer.removeColored(
                    self.buildings_fife_instances[building])
                self.renderer.addOutlined(self.buildings_fife_instances[building], \
                                          self.buildable_color[0], self.buildable_color[1],\
                                          self.buildable_color[2], 1)
                # draw radius in a moment, and not always immediately, since it's expensive
                if hasattr(self._class, "select_building"):
                    callback = Callback(self._class.select_building, self.session, \
                                        building.position, settlement)
                    ExtScheduler().rem_all_classinst_calls(self)
                    delay = 0.10  # Wait delay seconds
                    ExtScheduler().add_new_object(callback, self, delay)

            else:  # not buildable
                self.renderer.addColored(self.buildings_fife_instances[building], \
                                         *self.not_buildable_color)
        self.session.ingame_gui.resourceinfo_set( \
           self.ship if self.ship is not None else settlement, neededResources, usableResources, \
           res_from_ship = bool(self.ship))
        self._add_listeners(self.ship if self.ship is not None else settlement)
Example #14
0
	def preview_build(self, point1, point2, force=False):
		"""Display buildings as preview if build requirements are met"""
		#self.session.view.renderer['InstanceRenderer'].removeAllColored()
		self.log.debug("BuildingTool: preview build at %s, %s", point1, point2)
		new_buildings = self._class.check_build_line(self.session, point1, point2,
		                                             rotation = self.rotation, ship=self.ship)
		# optimisation: If only one building is in the preview and the position hasn't changed
		# => don't preview. Otherwise the preview is redrawn on every mouse move
		if not force and len(new_buildings) == len(self.buildings) == 1 and \
		   new_buildings[0] == self.buildings[0]:
			return # we don't want to redo the preview

		# remove old fife instances and coloring
		self._remove_building_instances()

		# get new ones
		self.buildings = new_buildings
		# delete old infos
		self.buildings_fife_instances.clear()
		self.buildings_missing_resources.clear()

		settlement = None # init here so we can access it below loop
		neededResources, usableResources = {}, {}
		# check if the buildings are buildable and color them appropriatly
		for building in self.buildings:
			# make surrounding transparent
			self._make_surrounding_transparent(building.position)

			# get gfx for the building
			# workaround for buildings like settler, that don't use the current level of
			# the player, but always start at a certain lvl
			level = self.session.world.player.settler_level if \
			      not hasattr(self._class, "default_level_on_build") else \
			      self._class.default_level_on_build

			if self._class.id == BUILDINGS.TREE_CLASS and not building.buildable:
				continue # Tree/ironmine that is not buildable, don't preview
			else:
				self.buildings_fife_instances[building] = \
				    self._class.getInstance(self.session, building.position.origin.x, \
				                            building.position.origin.y, rotation=building.rotation,
				                            action=building.action, level=level)

			if self._class.id == BUILDINGS.BRANCH_OFFICE_CLASS:
				settlement = self.session.world.get_settlement(building.position.center())
			else:
				# Player shouldn't be allowed to build in this case, else it can trigger
				# a new_settlement notificaition
				settlement = self.session.world.get_settlement(building.position.origin)
				if settlement is None:
					building.buildable = False

			if building.buildable:
				# building seems to buildable, check res too now
				(enough_res, missing_res) = Build.check_resources(neededResources, self._class.costs,
				                                                  self.session.world.player, [settlement, self.ship])
				if not enough_res:
					# make building red
					self.renderer.addColored(self.buildings_fife_instances[building],
					                         *self.not_buildable_color)
					building.buildable = False
					# set missing info for gui
					self.buildings_missing_resources[building] = missing_res
					# building isn't buildable after all, assemble strange dict values for gui
					for resource in self._class.costs:
						usableResources[resource] = usableResources.get(resource, 0) + \
						               self._class.costs[resource]

			if building.buildable:
				# Tile might still have not buildable color -> remove it
				self.renderer.removeColored(self.buildings_fife_instances[building])
				self.renderer.addOutlined(self.buildings_fife_instances[building], \
				                          self.buildable_color[0], self.buildable_color[1],\
				                          self.buildable_color[2], 1)
				# draw radius in a moment, and not always immediately, since it's expensive
				if hasattr(self._class, "select_building"):
					callback = Callback(self._class.select_building, self.session, \
					                    building.position, settlement)
					ExtScheduler().rem_all_classinst_calls(self)
					delay = 0.10 # Wait delay seconds
					ExtScheduler().add_new_object(callback, self, delay)

			else: # not buildable
				self.renderer.addColored(self.buildings_fife_instances[building], \
				                         *self.not_buildable_color)
		self.session.ingame_gui.resourceinfo_set( \
		   self.ship if self.ship is not None else settlement, neededResources, usableResources, \
		   res_from_ship = bool(self.ship))
		self._add_listeners(self.ship if self.ship is not None else settlement)
Example #15
0
	def have_resources(cls, inventory_holders, owner):
		return Build.check_resources({}, cls.costs, owner, inventory_holders)[0]
Example #16
0
	def check_resources(self):
		return Build.check_resources({}, self.total_cost, self.instance.owner,
		                             [self.instance.settlement])
	def check_resources(self):
		return Build.check_resources({}, self.total_cost, self.instance.owner,
		                             [self.instance.settlement])
Example #18
0
	def preview_build(self, point1, point2, force=False):
		"""Display buildings as preview if build requirements are met"""
		#self.session.view.renderer['InstanceRenderer'].removeAllColored()
		self.log.debug("BuildingTool: preview build at %s, %s", point1, point2)
		new_buildings = self._class.check_build_line(self.session, point1, point2,
				                                     rotation = self.rotation, ship=self.ship)
		# optimisation: If only one building is in the preview and the position hasn't changed
		# => don't preview. Otherwise the preview is redrawn on every mouse move
		if not force and len(new_buildings) == len(self.buildings) == 1 and \
		   new_buildings[0] == self.buildings[0]:
			return # we don't want to redo the preview

		# remove old fife instances and coloring
		self._remove_building_instances()

		# get new ones
		self.buildings = new_buildings
		# resize list of action set ids to new buildings
		self.buildings_action_set_ids = self.buildings_action_set_ids + ([None] * (len(self.buildings) - len(self.buildings_action_set_ids)))
		self.buildings_action_set_ids = self.buildings_action_set_ids[ : len(self.buildings) ]
		# delete old infos
		self.buildings_fife_instances.clear()
		self.buildings_missing_resources.clear()

		settlement = None # init here so we can access it below loop
		neededResources, usableResources = {}, {}
		# check if the buildings are buildable and color them appropriatly
		i = -1
		for building in self.buildings:
			i += 1
			# make surrounding transparent
			self._make_surrounding_transparent(building.position)

			# get gfx for the building
			# workaround for buildings like settler, that don't use the current level of
			# the player, but always start at a certain lvl
			level = self.session.world.player.settler_level if \
				not hasattr(self._class, "default_level_on_build") else \
				self._class.default_level_on_build

			if self._class.id == BUILDINGS.TREE_CLASS and not building.buildable:
				continue # Tree/ironmine that is not buildable, don't preview
			else:
				fife_instance, action_set_id = \
					self._class.getInstance(self.session, building.position.origin.x, \
								            building.position.origin.y, rotation=building.rotation,
								            action=building.action, level=level,
								            action_set_id=self.buildings_action_set_ids[i])
				self.buildings_fife_instances[building] = fife_instance
				# remember action sets per order of occurence
				# (this is far from good when building lines, but suffices for our purposes, which is mostly single build)
				self.buildings_action_set_ids[i] = action_set_id


			if self._class.id == BUILDINGS.BRANCH_OFFICE_CLASS:
				settlement = self.session.world.get_settlement(building.position.center())
			else:
				# Player shouldn't be allowed to build in this case, else it can trigger
				# a new_settlement notificaition
				settlement = self.session.world.get_settlement(building.position.origin)
				if settlement is None:
					building.buildable = False

			if building.buildable:
				# building seems to buildable, check res too now
				(enough_res, missing_res) = Build.check_resources(neededResources, self._class.costs,
				                                    self.session.world.player, [settlement, self.ship])
				if not enough_res:
					# make building red
					self.renderer.addColored(self.buildings_fife_instances[building],
										     *self.not_buildable_color)
					building.buildable = False
					# set missing info for gui
					self.buildings_missing_resources[building] = missing_res
					# building isn't buildable after all, assemble strange dict values for gui
					for resource in self._class.costs:
						usableResources[resource] = usableResources.get(resource, 0) + \
							self._class.costs[resource]

			if building.buildable:
				# Tile might still have not buildable color -> remove it
				self.renderer.removeColored(self.buildings_fife_instances[building])
				self.renderer.addOutlined(self.buildings_fife_instances[building], \
				                          self.buildable_color[0], self.buildable_color[1],\
				                          self.buildable_color[2], 1)
				if hasattr(self._class, "select_building"):
					self._class.select_building(self.session, building.position, settlement)
			else: # not buildable
				# must remove other highlight, fife does not support both
				self.renderer.removeOutlined(self.buildings_fife_instances[building])
				self.renderer.addColored(self.buildings_fife_instances[building], \
				                         *self.not_buildable_color)
		self.session.ingame_gui.resourceinfo_set( \
			self.ship if self.ship is not None else settlement, neededResources, usableResources, \
			res_from_ship = bool(self.ship))
		self._add_listeners(self.ship if self.ship is not None else settlement)