Package horizons :: Package ai :: Package aiplayer :: Package building :: Module field
[hide private]
[frames] | no frames]

Source Code for Module horizons.ai.aiplayer.building.field

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  import math 
 23   
 24  from horizons.ai.aiplayer.basicbuilder import BasicBuilder 
 25  from horizons.ai.aiplayer.building import AbstractBuilding 
 26  from horizons.ai.aiplayer.constants import BUILD_RESULT, BUILDING_PURPOSE 
 27  from horizons.constants import BUILDINGS, RES 
28 29 30 -class AbstractField(AbstractBuilding):
31 - def get_expected_cost(self, resource_id, production_needed, settlement_manager):
32 total_cost = 0 33 extra_fields_needed = int(math.ceil(max(0.0, production_needed / self.get_expected_production_level(resource_id)))) 34 field_spots_available = len(settlement_manager.production_builder.unused_fields[self.get_purpose(resource_id)]) 35 if field_spots_available >= extra_fields_needed: 36 return extra_fields_needed * self.get_expected_building_cost() 37 else: 38 total_cost += field_spots_available * self.get_expected_building_cost() 39 extra_fields_needed -= field_spots_available 40 41 fields_per_farm = AbstractBuilding.buildings[BUILDINGS.FARM].get_max_fields(settlement_manager) 42 if fields_per_farm == 0: 43 return 1e100 44 45 # TODO: fix the resource gathering code to request resources in larger chunks so this hack doesn't have to be used 46 # use fractional farm costs to give farms a chance to picked 47 extra_farms_needed = float(extra_fields_needed) / fields_per_farm 48 #extra_farms_needed = int(math.ceil(float(extra_fields_needed) / fields_per_farm)) 49 50 total_cost += self.get_expected_building_cost() * extra_fields_needed 51 total_cost += AbstractBuilding.buildings[BUILDINGS.FARM].get_expected_building_cost() * extra_farms_needed 52 return total_cost
53 54 @classmethod
55 - def get_purpose(cls, resource_id):
63 64 @classmethod
65 - def get_higher_level_resource(cls, resource_id):
73
74 - def build(self, settlement_manager, resource_id):
75 production_builder = settlement_manager.production_builder 76 purpose = self.get_purpose(resource_id) 77 if not production_builder.unused_fields[purpose]: 78 return (BUILD_RESULT.NEED_PARENT_FIRST, None) 79 if not self.have_resources(settlement_manager): 80 return (BUILD_RESULT.NEED_RESOURCES, None) 81 82 assert production_builder.unused_fields[purpose], 'expected field spot to be available' 83 coords = production_builder.unused_fields[purpose][0] 84 building = BasicBuilder(self.id, coords, 0).execute(settlement_manager.land_manager) 85 assert building 86 87 production_builder.unused_fields[purpose].popleft() 88 production_builder.register_change_list([coords], purpose, None) 89 return (BUILD_RESULT.OK, building)
90 91 @classmethod
92 - def register_buildings(cls):
98 99 100 AbstractField.register_buildings() 101