feat: define regional world state

This commit is contained in:
Rijad Zuzo
2026-08-12 21:22:04 +02:00
parent 8707942c75
commit c5978a9807
22 changed files with 2703 additions and 0 deletions
@@ -0,0 +1,129 @@
class_name DiplomaticRelationRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func create(
relation_id: StringName,
first_polity_id: StringName,
second_polity_id: StringName,
stance_id: StringName,
trust: float,
started_tick: int,
history_event_ids: Array = []
) -> DiplomaticRelationRecord:
var first := String(first_polity_id)
var second := String(second_polity_id)
if second < first:
var swapped := first
first = second
second = swapped
var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids(
history_event_ids
)
if normalized_history == null:
return null
var record := (
DiplomaticRelationRecord
. new(
{
"schema_version": SCHEMA_VERSION,
"relation_id": String(relation_id),
"first_polity_id": first,
"second_polity_id": second,
"stance_id": String(stance_id),
"trust": trust,
"started_tick": started_tick,
"history_event_ids": normalized_history,
}
)
)
return record if record.is_valid() else null
static func from_dictionary(record_data: Dictionary) -> DiplomaticRelationRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not (
record_data
. has_all(
[
"relation_id",
"first_polity_id",
"second_polity_id",
"stance_id",
"trust",
"started_tick",
"history_event_ids",
]
)
):
return null
if not record_data["history_event_ids"] is Array:
return null
return create(
StringName(record_data["relation_id"]),
StringName(record_data["first_polity_id"]),
StringName(record_data["second_polity_id"]),
StringName(record_data["stance_id"]),
float(record_data["trust"]),
int(record_data["started_tick"]),
record_data["history_event_ids"]
)
func is_valid() -> bool:
return (
not get_relation_id().is_empty()
and not get_first_polity_id().is_empty()
and not get_second_polity_id().is_empty()
and String(get_first_polity_id()) < String(get_second_polity_id())
and not get_stance_id().is_empty()
and is_finite(get_trust())
and get_trust() >= -1.0
and get_trust() <= 1.0
and get_started_tick() >= 0
)
func get_relation_id() -> StringName:
return StringName(data["relation_id"])
func get_first_polity_id() -> StringName:
return StringName(data["first_polity_id"])
func get_second_polity_id() -> StringName:
return StringName(data["second_polity_id"])
func get_stance_id() -> StringName:
return StringName(data["stance_id"])
func get_trust() -> float:
return float(data["trust"])
func get_started_tick() -> int:
return int(data["started_tick"])
func get_history_event_ids() -> Array[int]:
return RegionalStateSupport.copy_int_array(data["history_event_ids"])
func get_pair_key() -> String:
return RegionalStateSupport.pair_key(get_first_polity_id(), get_second_polity_id())
func to_dictionary() -> Dictionary:
return data.duplicate(true)
@@ -0,0 +1 @@
uid://cd7j3wipdurdt
+110
View File
@@ -0,0 +1,110 @@
class_name LocationStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func create(
location_id: StringName,
world_id: StringName,
location_type: StringName,
display_name: String,
address: SpatialAddress,
history_event_ids: Array = []
) -> LocationStateRecord:
var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids(
history_event_ids
)
var normalized_address: Variant = RegionalStateSupport.normalize_address(address)
if normalized_history == null or normalized_address == null:
return null
var record := (
LocationStateRecord
. new(
{
"schema_version": SCHEMA_VERSION,
"location_id": String(location_id),
"world_id": String(world_id),
"location_type": String(location_type),
"display_name": display_name,
"address": normalized_address,
"history_event_ids": normalized_history,
}
)
)
return record if record.is_valid() else null
static func from_dictionary(record_data: Dictionary) -> LocationStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not (
record_data
. has_all(
[
"location_id",
"world_id",
"location_type",
"display_name",
"address",
"history_event_ids",
]
)
):
return null
if not record_data["address"] is Dictionary or not record_data["history_event_ids"] is Array:
return null
return create(
StringName(record_data["location_id"]),
StringName(record_data["world_id"]),
StringName(record_data["location_type"]),
String(record_data["display_name"]),
SpatialAddress.from_dictionary(record_data["address"]),
record_data["history_event_ids"]
)
func is_valid() -> bool:
return (
not get_location_id().is_empty()
and not get_world_id().is_empty()
and not get_location_type().is_empty()
and not get_display_name().is_empty()
and RegionalStateSupport.address_matches(
data.get("address"), get_world_id(), get_location_id()
)
)
func get_location_id() -> StringName:
return StringName(data["location_id"])
func get_world_id() -> StringName:
return StringName(data["world_id"])
func get_location_type() -> StringName:
return StringName(data["location_type"])
func get_display_name() -> String:
return String(data["display_name"])
func get_address() -> SpatialAddress:
return RegionalStateSupport.address_from_data(data["address"])
func get_history_event_ids() -> Array[int]:
return RegionalStateSupport.copy_int_array(data["history_event_ids"])
func to_dictionary() -> Dictionary:
return data.duplicate(true)
@@ -0,0 +1 @@
uid://cu05dxii51mnv
+406
View File
@@ -0,0 +1,406 @@
class_name MobileGroupStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func create_at_location(
group_id: StringName,
group_type: StringName,
display_name: String,
polity_id: StringName,
address: SpatialAddress,
detailed_member_ids: Array = [],
cohort_counts: Dictionary = {},
cargo_ledger: Dictionary = {},
cargo_capacity: float = 0.0,
last_cargo_event_id: int = -1,
history_event_ids: Array = []
) -> MobileGroupStateRecord:
return _create(
group_id,
group_type,
display_name,
polity_id,
address,
&"",
&"",
&"",
-1,
-1,
detailed_member_ids,
cohort_counts,
cargo_ledger,
cargo_capacity,
last_cargo_event_id,
history_event_ids
)
static func create_on_route(
group_id: StringName,
group_type: StringName,
display_name: String,
polity_id: StringName,
route_id: StringName,
departure_location_id: StringName,
arrival_location_id: StringName,
departure_tick: int,
arrival_tick: int,
detailed_member_ids: Array = [],
cohort_counts: Dictionary = {},
cargo_ledger: Dictionary = {},
cargo_capacity: float = 0.0,
last_cargo_event_id: int = -1,
history_event_ids: Array = []
) -> MobileGroupStateRecord:
return _create(
group_id,
group_type,
display_name,
polity_id,
null,
route_id,
departure_location_id,
arrival_location_id,
departure_tick,
arrival_tick,
detailed_member_ids,
cohort_counts,
cargo_ledger,
cargo_capacity,
last_cargo_event_id,
history_event_ids
)
static func _create(
group_id: StringName,
group_type: StringName,
display_name: String,
polity_id: StringName,
address: Variant,
route_id: StringName,
departure_location_id: StringName,
arrival_location_id: StringName,
departure_tick: int,
arrival_tick: int,
detailed_member_ids: Array,
cohort_counts: Dictionary,
cargo_ledger: Dictionary,
cargo_capacity: float,
last_cargo_event_id: int,
history_event_ids: Array
) -> MobileGroupStateRecord:
var normalized_members: Variant = RegionalStateSupport.normalize_id_array(detailed_member_ids)
var normalized_cohorts: Variant = RegionalStateSupport.normalize_count_ledger(cohort_counts)
var normalized_cargo: Variant = RegionalStateSupport.normalize_amount_ledger(cargo_ledger)
var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids(
history_event_ids
)
var normalized_address: Variant = null
if address != null:
normalized_address = RegionalStateSupport.normalize_address(address)
if (
normalized_members == null
or normalized_cohorts == null
or normalized_cargo == null
or normalized_history == null
or (address != null and normalized_address == null)
):
return null
var record := (
MobileGroupStateRecord
. new(
{
"schema_version": SCHEMA_VERSION,
"group_id": String(group_id),
"group_type": String(group_type),
"display_name": display_name,
"polity_id": String(polity_id),
"location_address": normalized_address,
"route_id": String(route_id),
"departure_location_id": String(departure_location_id),
"arrival_location_id": String(arrival_location_id),
"departure_tick": departure_tick,
"arrival_tick": arrival_tick,
"detailed_member_ids": normalized_members,
"cohort_counts": normalized_cohorts,
"cargo_ledger": normalized_cargo,
"cargo_capacity": cargo_capacity,
"last_cargo_event_id": last_cargo_event_id,
"history_event_ids": normalized_history,
}
)
)
return record if record.is_valid() else null
static func from_dictionary(record_data: Dictionary) -> MobileGroupStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not (
record_data
. has_all(
[
"group_id",
"group_type",
"display_name",
"polity_id",
"location_address",
"route_id",
"departure_location_id",
"arrival_location_id",
"departure_tick",
"arrival_tick",
"detailed_member_ids",
"cohort_counts",
"cargo_ledger",
"cargo_capacity",
"last_cargo_event_id",
"history_event_ids",
]
)
):
return null
if (
(
record_data["location_address"] != null
and not record_data["location_address"] is Dictionary
)
or not record_data["detailed_member_ids"] is Array
or not record_data["cohort_counts"] is Dictionary
or not record_data["cargo_ledger"] is Dictionary
or not record_data["history_event_ids"] is Array
):
return null
return _create(
StringName(record_data["group_id"]),
StringName(record_data["group_type"]),
String(record_data["display_name"]),
StringName(record_data["polity_id"]),
record_data["location_address"],
StringName(record_data["route_id"]),
StringName(record_data["departure_location_id"]),
StringName(record_data["arrival_location_id"]),
int(record_data["departure_tick"]),
int(record_data["arrival_tick"]),
record_data["detailed_member_ids"],
record_data["cohort_counts"],
record_data["cargo_ledger"],
float(record_data["cargo_capacity"]),
int(record_data["last_cargo_event_id"]),
record_data["history_event_ids"]
)
func is_valid() -> bool:
if (
get_group_id().is_empty()
or get_group_type().is_empty()
or get_display_name().is_empty()
or get_polity_id().is_empty()
or not is_finite(get_cargo_capacity())
or get_cargo_capacity() < 0.0
or RegionalStateSupport.amount_ledger_total(data["cargo_ledger"]) > get_cargo_capacity()
or get_last_cargo_event_id() < -1
or (
get_last_cargo_event_id() >= 0
and get_last_cargo_event_id() not in get_history_event_ids()
)
):
return false
if is_at_location():
return (
get_route_id().is_empty()
and get_departure_location_id().is_empty()
and get_arrival_location_id().is_empty()
and get_departure_tick() == -1
and get_arrival_tick() == -1
)
return (
is_on_route()
and not get_departure_location_id().is_empty()
and not get_arrival_location_id().is_empty()
and get_departure_location_id() != get_arrival_location_id()
and get_departure_tick() >= 0
and get_arrival_tick() > get_departure_tick()
)
func is_at_location() -> bool:
return data["location_address"] is Dictionary
func is_on_route() -> bool:
return data["location_address"] == null and not get_route_id().is_empty()
func get_group_id() -> StringName:
return StringName(data["group_id"])
func get_group_type() -> StringName:
return StringName(data["group_type"])
func get_display_name() -> String:
return String(data["display_name"])
func get_polity_id() -> StringName:
return StringName(data["polity_id"])
func get_location_address() -> SpatialAddress:
return RegionalStateSupport.address_from_data(data["location_address"])
func get_current_location_id() -> StringName:
var address := get_location_address()
return address.get_location_id() if address != null else &""
func get_route_id() -> StringName:
return StringName(data["route_id"])
func get_departure_location_id() -> StringName:
return StringName(data["departure_location_id"])
func get_arrival_location_id() -> StringName:
return StringName(data["arrival_location_id"])
func get_departure_tick() -> int:
return int(data["departure_tick"])
func get_arrival_tick() -> int:
return int(data["arrival_tick"])
func get_detailed_member_ids() -> Array[StringName]:
return RegionalStateSupport.copy_string_array(data["detailed_member_ids"])
func get_cohort_counts() -> Dictionary:
return data["cohort_counts"].duplicate(true)
func get_cargo_ledger() -> Dictionary:
return data["cargo_ledger"].duplicate(true)
func get_cargo_amount(item_id: StringName) -> float:
return float(data["cargo_ledger"].get(String(item_id), 0.0))
func get_total_cargo_amount() -> float:
return RegionalStateSupport.amount_ledger_total(data["cargo_ledger"])
func get_cargo_capacity() -> float:
return float(data["cargo_capacity"])
func get_last_cargo_event_id() -> int:
return int(data["last_cargo_event_id"])
func get_history_event_ids() -> Array[int]:
return RegionalStateSupport.copy_int_array(data["history_event_ids"])
func get_headcount() -> int:
var headcount := get_detailed_member_ids().size()
for count: Variant in data["cohort_counts"].values():
headcount += int(count)
return headcount
func with_location(
address: SpatialAddress, cause_event_id: int = -1
) -> MobileGroupStateRecord:
var history_event_ids := _history_with_event(cause_event_id)
return create_at_location(
get_group_id(),
get_group_type(),
get_display_name(),
get_polity_id(),
address,
data["detailed_member_ids"],
data["cohort_counts"],
data["cargo_ledger"],
get_cargo_capacity(),
get_last_cargo_event_id(),
history_event_ids
)
func with_route_journey(
route_id: StringName,
departure_location_id: StringName,
arrival_location_id: StringName,
departure_tick: int,
arrival_tick: int,
cause_event_id: int = -1
) -> MobileGroupStateRecord:
var history_event_ids := _history_with_event(cause_event_id)
return create_on_route(
get_group_id(),
get_group_type(),
get_display_name(),
get_polity_id(),
route_id,
departure_location_id,
arrival_location_id,
departure_tick,
arrival_tick,
data["detailed_member_ids"],
data["cohort_counts"],
data["cargo_ledger"],
get_cargo_capacity(),
get_last_cargo_event_id(),
history_event_ids
)
func with_cargo_ledger(cargo_ledger: Dictionary, cargo_event_id: int) -> MobileGroupStateRecord:
var history_event_ids := _history_with_event(cargo_event_id)
return _create(
get_group_id(),
get_group_type(),
get_display_name(),
get_polity_id(),
data["location_address"],
get_route_id(),
get_departure_location_id(),
get_arrival_location_id(),
get_departure_tick(),
get_arrival_tick(),
data["detailed_member_ids"],
data["cohort_counts"],
cargo_ledger,
get_cargo_capacity(),
cargo_event_id,
history_event_ids
)
func _history_with_event(event_id: int) -> Array:
var history_event_ids: Array = data["history_event_ids"].duplicate()
if event_id >= 0 and event_id not in history_event_ids:
history_event_ids.append(event_id)
return history_event_ids
func to_dictionary() -> Dictionary:
return data.duplicate(true)
@@ -0,0 +1 @@
uid://jyqncykmnbfi
+183
View File
@@ -0,0 +1,183 @@
class_name PersonStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func create_at_location(
person_id: StringName,
display_name: String,
role_id: StringName,
polity_id: StringName,
home_location_id: StringName,
address: SpatialAddress,
history_event_ids: Array = []
) -> PersonStateRecord:
return _create(
person_id,
display_name,
role_id,
polity_id,
home_location_id,
address,
&"",
history_event_ids
)
static func create_in_group(
person_id: StringName,
display_name: String,
role_id: StringName,
polity_id: StringName,
home_location_id: StringName,
mobile_group_id: StringName,
history_event_ids: Array = []
) -> PersonStateRecord:
return _create(
person_id,
display_name,
role_id,
polity_id,
home_location_id,
null,
mobile_group_id,
history_event_ids
)
static func _create(
person_id: StringName,
display_name: String,
role_id: StringName,
polity_id: StringName,
home_location_id: StringName,
address: Variant,
mobile_group_id: StringName,
history_event_ids: Array
) -> PersonStateRecord:
var normalized_address: Variant = null
if address != null:
normalized_address = RegionalStateSupport.normalize_address(address)
var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids(
history_event_ids
)
if normalized_history == null or (address != null and normalized_address == null):
return null
var record := (
PersonStateRecord
. new(
{
"schema_version": SCHEMA_VERSION,
"person_id": String(person_id),
"display_name": display_name,
"role_id": String(role_id),
"polity_id": String(polity_id),
"home_location_id": String(home_location_id),
"location_address": normalized_address,
"mobile_group_id": String(mobile_group_id),
"history_event_ids": normalized_history,
}
)
)
return record if record.is_valid() else null
static func from_dictionary(record_data: Dictionary) -> PersonStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not (
record_data
. has_all(
[
"person_id",
"display_name",
"role_id",
"polity_id",
"home_location_id",
"location_address",
"mobile_group_id",
"history_event_ids",
]
)
):
return null
if (
(
record_data["location_address"] != null
and not record_data["location_address"] is Dictionary
)
or not record_data["history_event_ids"] is Array
):
return null
return _create(
StringName(record_data["person_id"]),
String(record_data["display_name"]),
StringName(record_data["role_id"]),
StringName(record_data["polity_id"]),
StringName(record_data["home_location_id"]),
record_data["location_address"],
StringName(record_data["mobile_group_id"]),
record_data["history_event_ids"]
)
func is_valid() -> bool:
return (
not get_person_id().is_empty()
and not get_display_name().is_empty()
and not get_role_id().is_empty()
and not get_polity_id().is_empty()
and not get_home_location_id().is_empty()
and (is_at_location() != is_in_mobile_group())
)
func get_person_id() -> StringName:
return StringName(data["person_id"])
func get_display_name() -> String:
return String(data["display_name"])
func get_role_id() -> StringName:
return StringName(data["role_id"])
func get_polity_id() -> StringName:
return StringName(data["polity_id"])
func get_home_location_id() -> StringName:
return StringName(data["home_location_id"])
func is_at_location() -> bool:
return data["location_address"] is Dictionary and get_mobile_group_id().is_empty()
func is_in_mobile_group() -> bool:
return data["location_address"] == null and not get_mobile_group_id().is_empty()
func get_location_address() -> SpatialAddress:
return RegionalStateSupport.address_from_data(data["location_address"])
func get_mobile_group_id() -> StringName:
return StringName(data["mobile_group_id"])
func get_history_event_ids() -> Array[int]:
return RegionalStateSupport.copy_int_array(data["history_event_ids"])
func to_dictionary() -> Dictionary:
return data.duplicate(true)
@@ -0,0 +1 @@
uid://bfotwdi60mdnm
+98
View File
@@ -0,0 +1,98 @@
class_name PolityStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func create(
polity_id: StringName,
display_name: String,
capital_location_id: StringName,
settlement_ids: Array = [],
history_event_ids: Array = []
) -> PolityStateRecord:
var normalized_settlements: Variant = RegionalStateSupport.normalize_id_array(settlement_ids)
var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids(
history_event_ids
)
if normalized_settlements == null or normalized_history == null:
return null
var record := (
PolityStateRecord
. new(
{
"schema_version": SCHEMA_VERSION,
"polity_id": String(polity_id),
"display_name": display_name,
"capital_location_id": String(capital_location_id),
"settlement_ids": normalized_settlements,
"history_event_ids": normalized_history,
}
)
)
return record if record.is_valid() else null
static func from_dictionary(record_data: Dictionary) -> PolityStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not (
record_data
. has_all(
[
"polity_id",
"display_name",
"capital_location_id",
"settlement_ids",
"history_event_ids",
]
)
):
return null
if not record_data["settlement_ids"] is Array or not record_data["history_event_ids"] is Array:
return null
return create(
StringName(record_data["polity_id"]),
String(record_data["display_name"]),
StringName(record_data["capital_location_id"]),
record_data["settlement_ids"],
record_data["history_event_ids"]
)
func is_valid() -> bool:
return (
not get_polity_id().is_empty()
and not get_display_name().is_empty()
and not get_capital_location_id().is_empty()
)
func get_polity_id() -> StringName:
return StringName(data["polity_id"])
func get_display_name() -> String:
return String(data["display_name"])
func get_capital_location_id() -> StringName:
return StringName(data["capital_location_id"])
func get_settlement_ids() -> Array[StringName]:
return RegionalStateSupport.copy_string_array(data["settlement_ids"])
func get_history_event_ids() -> Array[int]:
return RegionalStateSupport.copy_int_array(data["history_event_ids"])
func to_dictionary() -> Dictionary:
return data.duplicate(true)
@@ -0,0 +1 @@
uid://b1hsgds86deba
+147
View File
@@ -0,0 +1,147 @@
class_name PopulationCohortRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func create_at_location(
cohort_id: StringName,
cohort_type: StringName,
polity_id: StringName,
count: int,
location_id: StringName,
history_event_ids: Array = []
) -> PopulationCohortRecord:
return _create(cohort_id, cohort_type, polity_id, count, location_id, &"", history_event_ids)
static func create_in_group(
cohort_id: StringName,
cohort_type: StringName,
polity_id: StringName,
count: int,
mobile_group_id: StringName,
history_event_ids: Array = []
) -> PopulationCohortRecord:
return _create(
cohort_id, cohort_type, polity_id, count, &"", mobile_group_id, history_event_ids
)
static func _create(
cohort_id: StringName,
cohort_type: StringName,
polity_id: StringName,
count: int,
location_id: StringName,
mobile_group_id: StringName,
history_event_ids: Array
) -> PopulationCohortRecord:
var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids(
history_event_ids
)
if normalized_history == null:
return null
var record := (
PopulationCohortRecord
. new(
{
"schema_version": SCHEMA_VERSION,
"cohort_id": String(cohort_id),
"cohort_type": String(cohort_type),
"polity_id": String(polity_id),
"count": count,
"location_id": String(location_id),
"mobile_group_id": String(mobile_group_id),
"history_event_ids": normalized_history,
}
)
)
return record if record.is_valid() else null
static func from_dictionary(record_data: Dictionary) -> PopulationCohortRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not (
record_data
. has_all(
[
"cohort_id",
"cohort_type",
"polity_id",
"count",
"location_id",
"mobile_group_id",
"history_event_ids",
]
)
):
return null
if not record_data["history_event_ids"] is Array:
return null
return _create(
StringName(record_data["cohort_id"]),
StringName(record_data["cohort_type"]),
StringName(record_data["polity_id"]),
int(record_data["count"]),
StringName(record_data["location_id"]),
StringName(record_data["mobile_group_id"]),
record_data["history_event_ids"]
)
func is_valid() -> bool:
return (
not get_cohort_id().is_empty()
and not get_cohort_type().is_empty()
and not get_polity_id().is_empty()
and get_count() > 0
and (is_at_location() != is_in_mobile_group())
)
func get_cohort_id() -> StringName:
return StringName(data["cohort_id"])
func get_cohort_type() -> StringName:
return StringName(data["cohort_type"])
func get_polity_id() -> StringName:
return StringName(data["polity_id"])
func get_count() -> int:
return int(data["count"])
func is_at_location() -> bool:
return not get_location_id().is_empty() and get_mobile_group_id().is_empty()
func is_in_mobile_group() -> bool:
return get_location_id().is_empty() and not get_mobile_group_id().is_empty()
func get_location_id() -> StringName:
return StringName(data["location_id"])
func get_mobile_group_id() -> StringName:
return StringName(data["mobile_group_id"])
func get_history_event_ids() -> Array[int]:
return RegionalStateSupport.copy_int_array(data["history_event_ids"])
func to_dictionary() -> Dictionary:
return data.duplicate(true)
@@ -0,0 +1 @@
uid://di6mlijatrbgu
+117
View File
@@ -0,0 +1,117 @@
class_name RouteStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func create(
route_id: StringName,
from_location_id: StringName,
to_location_id: StringName,
travel_ticks: int,
bidirectional: bool = true,
history_event_ids: Array = []
) -> RouteStateRecord:
var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids(
history_event_ids
)
if normalized_history == null:
return null
var record := (
RouteStateRecord
. new(
{
"schema_version": SCHEMA_VERSION,
"route_id": String(route_id),
"from_location_id": String(from_location_id),
"to_location_id": String(to_location_id),
"travel_ticks": travel_ticks,
"bidirectional": bidirectional,
"history_event_ids": normalized_history,
}
)
)
return record if record.is_valid() else null
static func from_dictionary(record_data: Dictionary) -> RouteStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not (
record_data
. has_all(
[
"route_id",
"from_location_id",
"to_location_id",
"travel_ticks",
"bidirectional",
"history_event_ids",
]
)
):
return null
if not record_data["bidirectional"] is bool or not record_data["history_event_ids"] is Array:
return null
return create(
StringName(record_data["route_id"]),
StringName(record_data["from_location_id"]),
StringName(record_data["to_location_id"]),
int(record_data["travel_ticks"]),
bool(record_data["bidirectional"]),
record_data["history_event_ids"]
)
func is_valid() -> bool:
return (
not get_route_id().is_empty()
and not get_from_location_id().is_empty()
and not get_to_location_id().is_empty()
and get_from_location_id() != get_to_location_id()
and get_travel_ticks() > 0
)
func get_route_id() -> StringName:
return StringName(data["route_id"])
func get_from_location_id() -> StringName:
return StringName(data["from_location_id"])
func get_to_location_id() -> StringName:
return StringName(data["to_location_id"])
func get_travel_ticks() -> int:
return int(data["travel_ticks"])
func is_bidirectional() -> bool:
return bool(data["bidirectional"])
func get_history_event_ids() -> Array[int]:
return RegionalStateSupport.copy_int_array(data["history_event_ids"])
func connects(first_location_id: StringName, second_location_id: StringName) -> bool:
if first_location_id == get_from_location_id() and second_location_id == get_to_location_id():
return true
return (
is_bidirectional()
and first_location_id == get_to_location_id()
and second_location_id == get_from_location_id()
)
func to_dictionary() -> Dictionary:
return data.duplicate(true)
+1
View File
@@ -0,0 +1 @@
uid://dw73sybt7s5cx
+166
View File
@@ -0,0 +1,166 @@
class_name SettlementStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func create(
settlement_id: StringName,
location_id: StringName,
display_name: String,
polity_id: StringName,
tier: int,
founding_group_id: StringName,
founded_tick: int,
stockpile_ledger: Dictionary = {},
history_event_ids: Array = []
) -> SettlementStateRecord:
var normalized_stockpile: Variant = RegionalStateSupport.normalize_amount_ledger(
stockpile_ledger
)
var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids(
history_event_ids
)
if normalized_stockpile == null or normalized_history == null:
return null
var record := (
SettlementStateRecord
. new(
{
"schema_version": SCHEMA_VERSION,
"settlement_id": String(settlement_id),
"location_id": String(location_id),
"display_name": display_name,
"polity_id": String(polity_id),
"tier": tier,
"founding_group_id": String(founding_group_id),
"founded_tick": founded_tick,
"stockpile_ledger": normalized_stockpile,
"history_event_ids": normalized_history,
}
)
)
return record if record.is_valid() else null
static func from_dictionary(record_data: Dictionary) -> SettlementStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not (
record_data
. has_all(
[
"settlement_id",
"location_id",
"display_name",
"polity_id",
"tier",
"founding_group_id",
"founded_tick",
"stockpile_ledger",
"history_event_ids",
]
)
):
return null
if (
not record_data["stockpile_ledger"] is Dictionary
or not record_data["history_event_ids"] is Array
):
return null
return create(
StringName(record_data["settlement_id"]),
StringName(record_data["location_id"]),
String(record_data["display_name"]),
StringName(record_data["polity_id"]),
int(record_data["tier"]),
StringName(record_data["founding_group_id"]),
int(record_data["founded_tick"]),
record_data["stockpile_ledger"],
record_data["history_event_ids"]
)
func is_valid() -> bool:
return (
not get_settlement_id().is_empty()
and not get_location_id().is_empty()
and not get_display_name().is_empty()
and not get_polity_id().is_empty()
and not get_founding_group_id().is_empty()
and get_tier() >= 0
and get_founded_tick() >= 0
)
func get_settlement_id() -> StringName:
return StringName(data["settlement_id"])
func get_location_id() -> StringName:
return StringName(data["location_id"])
func get_display_name() -> String:
return String(data["display_name"])
func get_polity_id() -> StringName:
return StringName(data["polity_id"])
func get_tier() -> int:
return int(data["tier"])
func get_founding_group_id() -> StringName:
return StringName(data["founding_group_id"])
func get_founded_tick() -> int:
return int(data["founded_tick"])
func get_stockpile_ledger() -> Dictionary:
return data["stockpile_ledger"].duplicate(true)
func get_stockpile_amount(item_id: StringName) -> float:
return float(data["stockpile_ledger"].get(String(item_id), 0.0))
func get_total_stockpile_amount() -> float:
return RegionalStateSupport.amount_ledger_total(data["stockpile_ledger"])
func get_history_event_ids() -> Array[int]:
return RegionalStateSupport.copy_int_array(data["history_event_ids"])
func with_stockpile_ledger(
stockpile_ledger: Dictionary, cause_event_id: int = -1
) -> SettlementStateRecord:
var history_event_ids: Array = data["history_event_ids"].duplicate()
if cause_event_id >= 0 and cause_event_id not in history_event_ids:
history_event_ids.append(cause_event_id)
return create(
get_settlement_id(),
get_location_id(),
get_display_name(),
get_polity_id(),
get_tier(),
get_founding_group_id(),
get_founded_tick(),
stockpile_ledger,
history_event_ids
)
func to_dictionary() -> Dictionary:
return data.duplicate(true)
@@ -0,0 +1 @@
uid://8ofxqx8bfmyg