NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

resourcewars.py (3962B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\behaviors\actions\resourcewars.py
      2 import random
      3 from behaviors.actions.combattargets import FindTarget
      4 from behaviors.targetEvaluation import ThreatTargetEvaluator
      5 from behaviors.utility.ballparks import get_slim_item, get_ball, is_ship
      6 from behaviors.utility.inventory import get_cargo_used_fraction
      7 from behaviors.utility.item_modules import has_fitted_from_type_list
      8 from behaviors.utility.math import get_normalized_dict
      9 from inventorycommon.const import groupNpcIndustrialCommand, groupMiningDrone
     10 HAULER_GROUP_IDS = []
     11 
     12 class ResourceWarsFindTarget(FindTarget):
     13 
     14     def OnEnter(self):
     15         super(ResourceWarsFindTarget, self).OnEnter()
     16 
     17     def SetTargetEvaluator(self):
     18         if not self.HasContextValue('targetEvaluator'):
     19             self.SetContextValue('targetEvaluator', ThreatTargetEvaluator(self.context, self.attributes))
     20 
     21     def PickTarget(self, target_list):
     22         target_evaluator = self.GetTargetEvaluator()
     23         weight_by_item_id = {item_id:target_evaluator.EvaluateTarget(item_id) for item_id in target_list}
     24         weight_by_item_id = get_normalized_dict(weight_by_item_id)
     25         cargo_fraction_by_hauler_item_id = {item_id:get_cargo_used_fraction(self, item_id) for item_id in target_list if self._is_hauler(item_id)}
     26         if self._should_prioritize_haulers(cargo_fraction_by_hauler_item_id):
     27             self._update_hauler_weights(weight_by_item_id, cargo_fraction_by_hauler_item_id)
     28         elif self._should_prioritize_miners():
     29             self._update_miner_weights(weight_by_item_id)
     30         sorted_target_ids = sorted(weight_by_item_id.keys(), key=lambda item_id: weight_by_item_id[item_id])
     31         return sorted_target_ids[-1]
     32 
     33     def _update_hauler_weights(self, weight_by_item_id, cargo_fraction_by_hauler_item_id):
     34         for item_id, cargo_fraction in cargo_fraction_by_hauler_item_id.iteritems():
     35             weight_by_item_id[item_id] += 1.0 + cargo_fraction
     36 
     37     def _is_hauler(self, item_id):
     38         return get_slim_item(self, item_id).groupID == groupNpcIndustrialCommand
     39 
     40     def _should_prioritize_haulers(self, cargo_fraction_by_hauler_item_id):
     41         if random.random() > self._get_chance_of_preferring_filling_haulers():
     42             return False
     43         cargo_full_fraction_threshold = self._get_cargo_full_fraction_threshold()
     44         return any((cargo_fraction >= cargo_full_fraction_threshold for cargo_fraction in cargo_fraction_by_hauler_item_id.itervalues()))
     45 
     46     def _should_prioritize_miners(self):
     47         return random.random() <= self._get_chance_of_preferring_miners()
     48 
     49     def _update_miner_weights(self, weight_by_item_id):
     50         mining_drone_operators = self._get_mining_drone_operators(weight_by_item_id)
     51         for item_id in weight_by_item_id:
     52             if self._is_miner(item_id) or item_id in mining_drone_operators:
     53                 weight_by_item_id[item_id] += 2.0
     54 
     55     def _get_mining_drone_operators(self, weight_by_item_id):
     56         mining_drone_operators = set()
     57         for item_id in weight_by_item_id:
     58             slim_item = get_slim_item(self, item_id)
     59             if slim_item.groupID != groupMiningDrone:
     60                 continue
     61             ball = get_ball(self, item_id)
     62             mining_drone_operators.add(ball.sourceID)
     63 
     64         return mining_drone_operators
     65 
     66     def _is_miner(self, item_id):
     67         return is_ship(self, item_id) and has_fitted_from_type_list(self, item_id, self.attributes.miningModuleTypeListID)
     68 
     69     def _get_chance_of_preferring_filling_haulers(self):
     70         return self.GetLastBlackboardValue(self.attributes.chanceOfPreferringFillingHaulersAddress)
     71 
     72     def _get_chance_of_preferring_miners(self):
     73         return self.GetLastBlackboardValue(self.attributes.chanceOfPreferringMinersAddress)
     74 
     75     def _get_cargo_full_fraction_threshold(self):
     76         return self.GetLastBlackboardValue(self.attributes.cargoFullFractionThresholdAddress)