Skip to content

Commit

Permalink
Clean data mappers (#53)
Browse files Browse the repository at this point in the history
* Simplify enum data mapper

* Revert cast logic

* Switch to key for data mapper

* Simplify inner mapper
  • Loading branch information
VDuchauffour committed Jun 10, 2024
1 parent 4fd4c70 commit b00e877
Show file tree
Hide file tree
Showing 11 changed files with 2,402 additions and 2,809 deletions.
11 changes: 6 additions & 5 deletions Assets/Python/Civilizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from CoreData import civilization, civilizations
from CoreFunctions import get_civ_by_id
from CoreStructures import human, make_unit, make_units, team, teamtype, year, plots
from CoreTypes import Civ, Technology, Unit
from CoreTypes import Area, Civ, InitialCondition, Technology, Unit
from LocationsData import CIV_CAPITAL_LOCATIONS
from MiscData import REVEAL_DATE_TECHNOLOGY
from RFCUtils import change_attitude_extra_between_civ
Expand All @@ -24,14 +24,14 @@ def set_starting_gold():
for civ in civilizations():
condition = civ.scenario.get("condition")
if condition is not None:
civ.player.changeGold(condition.gold)
civ.player.changeGold(condition[InitialCondition.GOLD])


def set_starting_faith():
for civ in civilizations():
condition = civ.scenario.get("condition")
if condition is not None:
civ.player.setFaith(condition.faith)
civ.player.setFaith(condition[InitialCondition.FAITH])


def create_starting_workers(iCiv, tPlot):
Expand Down Expand Up @@ -137,15 +137,16 @@ def set_initial_contacts(iCiv, bMeet=True):


def reveal_rectangle(iCiv, area):
for plot in plots().rectangle(area.tile_min, area.tile_max).entities():
for plot in plots().rectangle(area[Area.TILE_MIN], area[Area.TILE_MAX]).entities():
plot.setRevealed(teamtype(iCiv), True, False, -1)


def reveal_areas(iCiv):
for area in civilization(iCiv).location.visible_area:
reveal_rectangle(iCiv, area)


def has_date_revealed(identifier=None):
if team(identifier).isHasTech(REVEAL_DATE_TECHNOLOGY):
return True
return False
return False
14 changes: 7 additions & 7 deletions Assets/Python/Locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from CoreFunctions import location as _location
from CoreData import civilizations
from CoreStructures import plots
from CoreTypes import Civ, PlagueType, ProvinceType, Religion, Technology
from CoreTypes import Area, AreaType, Civ, PlagueType, ProvinceType, Religion, Technology
from LocationsData import LAKE_LOCATIONS
from CityMapData import CITIES_MAP
from ProvinceMapData import PROVINCES_MAP
Expand Down Expand Up @@ -95,12 +95,12 @@ def update_lake_id():

def update_core():
for civ in civilizations().majors():
core_tile_min = civ.location.area.core.tile_min
core_tile_max = civ.location.area.core.tile_max
core_additional_tiles = civ.location.area.core.additional_tiles
normal_tile_min = civ.location.area.normal.tile_min
normal_tile_max = civ.location.area.normal.tile_max
normal_exception_tiles = civ.location.area.normal.exception_tiles
core_tile_min = civ.location.area[AreaType.CORE][Area.TILE_MIN]
core_tile_max = civ.location.area[AreaType.CORE][Area.TILE_MAX]
core_additional_tiles = civ.location.area[AreaType.CORE][Area.ADDITIONAL_TILES]
normal_tile_min = civ.location.area[AreaType.NORMAL][Area.TILE_MIN]
normal_tile_max = civ.location.area[AreaType.NORMAL][Area.TILE_MAX]
normal_exception_tiles = civ.location.area[AreaType.NORMAL][Area.EXCEPTION_TILES]
gc.setCoreNormal(
civ.id,
core_tile_min[0],
Expand Down
34 changes: 26 additions & 8 deletions Assets/Python/components/Resurrection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@
plots,
cities,
)
from CoreTypes import Building, Civ, StabilityCategory, Technology, Unit
from CoreTypes import (
Area,
AreaType,
Building,
Civ,
LeaderType,
StabilityCategory,
Technology,
Unit,
)
from PyUtils import percentage, percentage_chance, rand
from RFCUtils import (
calculateDistance,
Expand Down Expand Up @@ -66,14 +75,17 @@ def findCivToResurect(iGameTurn, bSpecialRespawn, iDeadCiv):
and iGameTurn > civilization(iDeadCiv).date.birth + 25
and iGameTurn > getLastTurnAlive(iDeadCiv) + 10
): # Sedna17: Allow re-spawns only 10 turns after death and 25 turns after birth
tile_min = civilization(iDeadCiv).location.area.normal.tile_min
tile_max = civilization(iDeadCiv).location.area.normal.tile_max
tile_min = civilization(iDeadCiv).location.area[AreaType.NORMAL][Area.TILE_MIN]
tile_max = civilization(iDeadCiv).location.area[AreaType.NORMAL][Area.TILE_MAX]

for city in (
plots()
.rectangle(tile_min, tile_max)
.filter(
lambda p: p not in civilization(iDeadCiv).location.area.normal.exception_tiles
lambda p: p
not in civilization(iDeadCiv).location.area[AreaType.NORMAL][
Area.EXCEPTION_TILES
]
)
.cities()
.entities()
Expand Down Expand Up @@ -213,7 +225,7 @@ def resurectCiv(iDeadCiv):

# Absinthe: we shouldn't get a previous leader on respawn - would be changed to a newer one in a couple turns anyway
# instead we have a random chance to remain with the leader before the collapse, or to switch to the next one
leaders = civilization(iDeadCiv).leaders.late
leaders = civilization(iDeadCiv).leaders[LeaderType.LATE]
if leaders:
# no change if we are already at the last leader
for leader in leaders[:-1]:
Expand Down Expand Up @@ -424,10 +436,16 @@ def makeResurectionUnits(iPlayer, iX, iY):
def convertBackCulture(iCiv):
# 3Miro: same as Normal Areas in Resurrection
# Sedna17: restored to be normal areas, not core
tile_min = civilization(iCiv).location.area.normal.tile_min
tile_max = civilization(iCiv).location.area.normal.tile_max
# collect all the cities in the region
for city in plots().rectangle(tile_min, tile_max).cities().entities():
for city in (
plots()
.rectangle(
civilization(iCiv).location.area[AreaType.NORMAL][Area.TILE_MIN],
civilization(iCiv).location.area[AreaType.NORMAL][Area.TILE_MAX],
)
.cities()
.entities()
):
for plot in plots().surrounding(location(city)).entities():
iCivCulture = plot.getCulture(iCiv)
iLoopCivCulture = 0
Expand Down
67 changes: 32 additions & 35 deletions Assets/Python/components/RiseAndFall.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@

from MiscData import PLAGUE_IMMUNITY
from CoreTypes import (
Area,
AreaType,
Building,
Civ,
LeaderType,
PlayerType,
Scenario,
Religion,
Expand Down Expand Up @@ -231,7 +234,7 @@ def flipPopup(self, iNewCiv, tTopLeft, tBottomRight):
for city in (
plots()
.rectangle(tTopLeft, tBottomRight)
.add(civilization(iNewCiv).location.area.core.additional_tiles)
.add(civilization(iNewCiv).location.area[AreaType.CORE][Area.ADDITIONAL_TILES])
.cities()
.filter(lambda c: c.getOwner() == iHuman and not c.isCapital())
.entities()
Expand Down Expand Up @@ -261,7 +264,7 @@ def eventApply7615(self, popupReturn):
for city in (
plots()
.rectangle(tTopLeft, tBottomRight)
.add(civilization(iNewCivFlip).location.area.core.additional_tiles)
.add(civilization(iNewCivFlip).location.area[AreaType.CORE][Area.ADDITIONAL_TILES])
.cities()
.filter(lambda c: c.getOwner() == iHuman and not c.isCapital())
.entities()
Expand Down Expand Up @@ -504,8 +507,8 @@ def setupRespawnTurns(self):

def setEarlyLeaders(self):
for civ in civilizations().majors().ai():
if civ.leaders.early != civ.leaders.primary:
leader = civ.leaders.early
if civ.leaders[LeaderType.EARLY] != civ.leaders[LeaderType.PRIMARY]:
leader = civ.leaders[LeaderType.EARLY]
civ.player.setLeader(leader)

def setWarOnSpawn(self):
Expand Down Expand Up @@ -627,14 +630,12 @@ def reduceCity(self, tPlot):
)

pPlot.eraseCityDevelopment()
pPlot.setImprovementType(
Improvement.TOWN
) # Improvement Town instead of the city
pPlot.setImprovementType(Improvement.TOWN) # Improvement Town instead of the city
pPlot.setRouteType(0) # Also adding a road there

def checkPlayerTurn(self, iGameTurn, iPlayer):
# Absinthe & Merijn: leader switching with any number of leaders
late_leaders = civilization(iPlayer).leaders.late
late_leaders = civilization(iPlayer).leaders[LeaderType.LATE]
if late_leaders:
for tLeader in reversed(late_leaders):
if iGameTurn >= year(tLeader[1]):
Expand All @@ -654,9 +655,7 @@ def checkPlayerTurn(self, iGameTurn, iPlayer):
if pPlot.isCity():
if pPlot.getPlotCity().getOwner() == Civ.BARBARIAN:
pDublin = pPlot.getPlotCity()
cultureManager(
tDublin, 50, Civ.ENGLAND, Civ.BARBARIAN, False, True, True
)
cultureManager(tDublin, 50, Civ.ENGLAND, Civ.BARBARIAN, False, True, True)
flipUnitsInCityBefore(tDublin, Civ.ENGLAND, Civ.BARBARIAN)
self.setTempFlippingCity(tDublin)
flipCity(
Expand Down Expand Up @@ -756,8 +755,8 @@ def fragmentBarbarians(self, iGameTurn):
for city in (
plots()
.rectangle(
civilization(iDeadCiv).location.area.normal.tile_min,
civilization(iDeadCiv).location.area.normal.tile_max,
civilization(iDeadCiv).location.area[AreaType.NORMAL][Area.TILE_MIN],
civilization(iDeadCiv).location.area[AreaType.NORMAL][Area.TILE_MAX],
)
.cities()
.owner(Civ.BARBARIAN)
Expand All @@ -773,9 +772,7 @@ def fragmentBarbarians(self, iGameTurn):
+ 1
)
if iDivideCounter % 4 in [0, 1]:
cultureManager(
tCity, 50, iNewCiv, Civ.BARBARIAN, False, True, True
)
cultureManager(tCity, 50, iNewCiv, Civ.BARBARIAN, False, True, True)
flipUnitsInCityBefore(tCity, iNewCiv, Civ.BARBARIAN)
self.setTempFlippingCity(tCity)
flipCity(
Expand All @@ -789,10 +786,10 @@ def initBirth(self, iCurrentTurn, iBirthYear, iCiv):
iHuman = human()
if iCurrentTurn == iBirthYear - 1 + self.getSpawnDelay(iCiv) + self.getFlipsDelay(iCiv):
tCapital = civilization(iCiv).location.capital
core_tile_min = civilization(iCiv).location.area.core.tile_min
core_tile_max = civilization(iCiv).location.area.core.tile_max
broader_tile_min = civilization(iCiv).location.area.broader.tile_min
broader_tile_max = civilization(iCiv).location.area.broader.tile_max
core_tile_min = civilization(iCiv).location.area[AreaType.CORE][Area.TILE_MIN]
core_tile_max = civilization(iCiv).location.area[AreaType.CORE][Area.TILE_MAX]
broader_tile_min = civilization(iCiv).location.area[AreaType.BROADER][Area.TILE_MIN]
broader_tile_max = civilization(iCiv).location.area[AreaType.BROADER][Area.TILE_MAX]
if self.getFlipsDelay(iCiv) == 0: # city hasn't already been founded

# Absinthe: for the human player, kill all foreign units on the capital plot - this probably fixes a couple instances of the -1 turn autoplay bug
Expand Down Expand Up @@ -913,7 +910,7 @@ def birthInFreeRegion(self, iCiv, tCapital, tTopLeft, tBottomRight):
for tPlot in (
plots()
.rectangle(tTopLeft, tBottomRight)
.add(civilization(iCiv).location.area.core.additional_tiles)
.add(civilization(iCiv).location.area[AreaType.CORE][Area.ADDITIONAL_TILES])
.apply(location) # TODO fix this with _keyify by default
):
if tPlot in lSurroundingPlots2:
Expand Down Expand Up @@ -947,7 +944,7 @@ def birthInFreeRegion(self, iCiv, tCapital, tTopLeft, tBottomRight):
tTopLeft, tBottomRight, iCiv, Civ.BARBARIAN, False, True
) # remaining barbs in the region now belong to the new civ
flipUnitsInPlots(
civilization(iCiv).location.area.core.additional_tiles,
civilization(iCiv).location.area[AreaType.CORE][Area.ADDITIONAL_TILES],
iCiv,
Civ.BARBARIAN,
False,
Expand All @@ -959,7 +956,7 @@ def birthInFreeRegion(self, iCiv, tCapital, tTopLeft, tBottomRight):
tTopLeft, tBottomRight, iCiv, iIndyCiv, False, False
) # remaining independents in the region now belong to the new civ
flipUnitsInPlots(
civilization(iCiv).location.area.core.additional_tiles,
civilization(iCiv).location.area[AreaType.CORE][Area.ADDITIONAL_TILES],
iCiv,
iIndyCiv,
False,
Expand Down Expand Up @@ -1000,7 +997,7 @@ def birthInForeignBorders(
# so if all flipped cities are outside of the core area (they are in the "exceptions"), the civ will start without it's starting units and techs
plotList = squareSearch(tTopLeft, tBottomRight, ownedCityPlots, iCiv)
# Absinthe: add the exception plots
for plot in civilization(iCiv).location.area.core.additional_tiles:
for plot in civilization(iCiv).location.area[AreaType.CORE][Area.ADDITIONAL_TILES]:
plot = gc.getMap().plot(*plot)
if plot.getOwner() == iCiv:
if plot.isCity():
Expand All @@ -1017,7 +1014,7 @@ def birthInForeignBorders(
tTopLeft, tBottomRight, iCiv, Civ.BARBARIAN, False, True
) # remaining barbs in the region now belong to the new civ
flipUnitsInPlots(
civilization(iCiv).location.area.core.additional_tiles,
civilization(iCiv).location.area[AreaType.CORE][Area.ADDITIONAL_TILES],
iCiv,
Civ.BARBARIAN,
False,
Expand All @@ -1028,7 +1025,7 @@ def birthInForeignBorders(
tTopLeft, tBottomRight, iCiv, iIndyCiv, False, False
) # remaining independents in the region now belong to the new civ
flipUnitsInPlots(
civilization(iCiv).location.area.core.additional_tiles,
civilization(iCiv).location.area[AreaType.CORE][Area.ADDITIONAL_TILES],
iCiv,
iIndyCiv,
False,
Expand All @@ -1040,7 +1037,7 @@ def birthInForeignBorders(
# so if all flipped cities are outside of the core area (they are in the "exceptions"), the civ will start without it's starting units and techs
plotList = squareSearch(tTopLeft, tBottomRight, goodPlots, [])
# Absinthe: add the exception plots
for plot in civilization(iCiv).location.area.core.additional_tiles:
for plot in civilization(iCiv).location.area[AreaType.CORE][Area.ADDITIONAL_TILES]:
plot = gc.getMap().plot(*plot)
if (plot.isHills() or plot.isFlatlands()) and not plot.isImpassable():
if not plot.isUnit():
Expand Down Expand Up @@ -1078,7 +1075,7 @@ def birthInForeignBorders(
tTopLeft, tBottomRight, iCiv, Civ.BARBARIAN, True, True
) # remaining barbs in the region now belong to the new civ
flipUnitsInPlots(
civilization(iCiv).location.area.core.additional_tiles,
civilization(iCiv).location.area[AreaType.CORE][Area.ADDITIONAL_TILES],
iCiv,
Civ.BARBARIAN,
True,
Expand All @@ -1089,7 +1086,7 @@ def birthInForeignBorders(
tTopLeft, tBottomRight, iCiv, iIndyCiv, True, False
) # remaining independents in the region now belong to the new civ
flipUnitsInPlots(
civilization(iCiv).location.area.core.additional_tiles,
civilization(iCiv).location.area[AreaType.CORE][Area.ADDITIONAL_TILES],
iCiv,
iIndyCiv,
True,
Expand All @@ -1108,7 +1105,7 @@ def convertSurroundingCities(self, iCiv, tTopLeft, tBottomRight):
for city in (
plots()
.rectangle(tTopLeft, tBottomRight)
.add(civilization(iCiv).location.area.core.additional_tiles)
.add(civilization(iCiv).location.area[AreaType.CORE][Area.ADDITIONAL_TILES])
.cities()
.not_owner(iCiv)
.entities()
Expand Down Expand Up @@ -1165,7 +1162,7 @@ def convertSurroundingPlotCulture(self, iCiv, tTopLeft, tBottomRight):
for plot in (
plots()
.rectangle(tTopLeft, tBottomRight)
.add(civilization(iCiv).location.area.core.additional_tiles)
.add(civilization(iCiv).location.area[AreaType.CORE][Area.ADDITIONAL_TILES])
.filter(lambda p: not p.isCity())
.entities()
):
Expand Down Expand Up @@ -1286,8 +1283,8 @@ def canSpecialRespawn(self, iPlayer, iGameTurn, iLastAliveInterval=10):
def initMinorBetrayal(self, iCiv):
iHuman = human()
plotList = squareSearch(
civilization(iCiv).location.area.core.tile_min,
civilization(iCiv).location.area.core.tile_max,
civilization(iCiv).location.area[AreaType.CORE][Area.TILE_MIN],
civilization(iCiv).location.area[AreaType.CORE][Area.TILE_MAX],
outerInvasion,
[],
)
Expand All @@ -1297,8 +1294,8 @@ def initMinorBetrayal(self, iCiv):
self.unitsBetrayal(
iCiv,
iHuman,
civilization(iCiv).location.area.core.tile_min,
civilization(iCiv).location.area.core.tile_max,
civilization(iCiv).location.area[AreaType.CORE][Area.TILE_MIN],
civilization(iCiv).location.area[AreaType.CORE][Area.TILE_MAX],
tPlot,
)

Expand Down
Loading

0 comments on commit b00e877

Please sign in to comment.