feat: add location-based food storage loop
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
class_name NPCStateRecord
|
||||
extends RefCounted
|
||||
|
||||
const SCHEMA_VERSION := 2
|
||||
const SCHEMA_VERSION := 3
|
||||
const LEGACY_SCHEMA_VERSION := 1
|
||||
const PREVIOUS_SCHEMA_VERSION := 2
|
||||
|
||||
var data: Dictionary
|
||||
|
||||
@@ -36,6 +37,7 @@ static func capture(npc: SimNPC) -> NPCStateRecord:
|
||||
npc.travel_target_position.z
|
||||
],
|
||||
"has_travel_target": npc.has_travel_target,
|
||||
"inventory": npc.inventory.duplicate(true),
|
||||
"last_task": String(npc.last_task),
|
||||
"random_seed": str(npc.random_source.seed),
|
||||
"random_state": str(npc.random_source.state)
|
||||
@@ -43,8 +45,8 @@ static func capture(npc: SimNPC) -> NPCStateRecord:
|
||||
|
||||
static func from_dictionary(record_data: Dictionary) -> NPCStateRecord:
|
||||
var version := int(record_data.get("schema_version", -1))
|
||||
if version == LEGACY_SCHEMA_VERSION:
|
||||
record_data = _migrate_v1(record_data)
|
||||
if version in [LEGACY_SCHEMA_VERSION, PREVIOUS_SCHEMA_VERSION]:
|
||||
record_data = _migrate_legacy(record_data, version)
|
||||
elif version != SCHEMA_VERSION:
|
||||
return null
|
||||
if not record_data.has_all([
|
||||
@@ -52,7 +54,7 @@ static func from_dictionary(record_data: Dictionary) -> NPCStateRecord:
|
||||
"intelligence", "current_task", "task_state", "position",
|
||||
"is_starving", "starvation_ticks", "starvation_death_threshold",
|
||||
"is_dead", "task_duration", "task_progress", "task_complete",
|
||||
"target_id", "travel_target_position", "has_travel_target",
|
||||
"target_id", "travel_target_position", "has_travel_target", "inventory",
|
||||
"last_task", "random_seed", "random_state"
|
||||
]):
|
||||
return null
|
||||
@@ -65,6 +67,8 @@ static func from_dictionary(record_data: Dictionary) -> NPCStateRecord:
|
||||
or saved_target_position.size() != 3
|
||||
):
|
||||
return null
|
||||
if not record_data["inventory"] is Dictionary:
|
||||
return null
|
||||
var profession_id := StringName(record_data["profession"])
|
||||
if SimulationDefinitions.get_profession(profession_id) == null:
|
||||
return null
|
||||
@@ -85,14 +89,19 @@ static func from_dictionary(record_data: Dictionary) -> NPCStateRecord:
|
||||
return null
|
||||
return NPCStateRecord.new(record_data)
|
||||
|
||||
static func _migrate_v1(legacy_data: Dictionary) -> Dictionary:
|
||||
static func _migrate_legacy(
|
||||
legacy_data: Dictionary,
|
||||
version: int
|
||||
) -> Dictionary:
|
||||
var migrated := legacy_data.duplicate(true)
|
||||
migrated["schema_version"] = SCHEMA_VERSION
|
||||
migrated["travel_target_position"] = legacy_data.get(
|
||||
"position",
|
||||
[0.0, 0.0, 0.0]
|
||||
)
|
||||
migrated["has_travel_target"] = false
|
||||
if version == LEGACY_SCHEMA_VERSION:
|
||||
migrated["travel_target_position"] = legacy_data.get(
|
||||
"position",
|
||||
[0.0, 0.0, 0.0]
|
||||
)
|
||||
migrated["has_travel_target"] = false
|
||||
migrated["inventory"] = {}
|
||||
return migrated
|
||||
|
||||
func restore(debug_logs: bool) -> SimNPC:
|
||||
@@ -131,6 +140,7 @@ func restore(debug_logs: bool) -> SimNPC:
|
||||
float(saved_target_position[2])
|
||||
)
|
||||
npc.has_travel_target = bool(data["has_travel_target"])
|
||||
npc.inventory = data["inventory"].duplicate(true)
|
||||
npc.last_task = StringName(data["last_task"])
|
||||
npc.random_source.state = String(data["random_state"]).to_int()
|
||||
npc.debug_logs = debug_logs
|
||||
|
||||
@@ -2,12 +2,14 @@ class_name SimulationStateRecord
|
||||
extends RefCounted
|
||||
|
||||
const SCHEMA_NAME := "the_steward.simulation"
|
||||
const SCHEMA_VERSION := 1
|
||||
const SCHEMA_VERSION := 2
|
||||
const LEGACY_SCHEMA_VERSION := 1
|
||||
|
||||
var simulation: Dictionary
|
||||
var village: VillageStateRecord
|
||||
var npcs: Array[NPCStateRecord] = []
|
||||
var resources: Array[ResourceStateRecord] = []
|
||||
var storages: Array[StorageStateRecord] = []
|
||||
|
||||
func to_dictionary() -> Dictionary:
|
||||
var npc_data: Array[Dictionary] = []
|
||||
@@ -17,6 +19,9 @@ func to_dictionary() -> Dictionary:
|
||||
var resource_data: Array[Dictionary] = []
|
||||
for resource_record in resources:
|
||||
resource_data.append(resource_record.to_dictionary())
|
||||
var storage_data: Array[Dictionary] = []
|
||||
for storage_record in storages:
|
||||
storage_data.append(storage_record.to_dictionary())
|
||||
|
||||
return {
|
||||
"schema": SCHEMA_NAME,
|
||||
@@ -24,7 +29,8 @@ func to_dictionary() -> Dictionary:
|
||||
"simulation": simulation.duplicate(true),
|
||||
"village": village.to_dictionary(),
|
||||
"npcs": npc_data,
|
||||
"resources": resource_data
|
||||
"resources": resource_data,
|
||||
"storages": storage_data
|
||||
}
|
||||
|
||||
func to_json() -> String:
|
||||
@@ -39,10 +45,13 @@ static func from_json(json_text: String) -> SimulationStateRecord:
|
||||
static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
if record_data.get("schema", "") != SCHEMA_NAME:
|
||||
return null
|
||||
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
||||
var version := int(record_data.get("schema_version", -1))
|
||||
if version == LEGACY_SCHEMA_VERSION:
|
||||
record_data = _migrate_v1(record_data)
|
||||
elif version != SCHEMA_VERSION:
|
||||
return null
|
||||
if not record_data.has_all([
|
||||
"simulation", "village", "npcs", "resources"
|
||||
"simulation", "village", "npcs", "resources", "storages"
|
||||
]):
|
||||
return null
|
||||
|
||||
@@ -72,7 +81,12 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
|
||||
var npc_data = record_data["npcs"]
|
||||
var resource_data = record_data["resources"]
|
||||
if not npc_data is Array or not resource_data is Array:
|
||||
var storage_data = record_data["storages"]
|
||||
if (
|
||||
not npc_data is Array
|
||||
or not resource_data is Array
|
||||
or not storage_data is Array
|
||||
):
|
||||
return null
|
||||
|
||||
var record := SimulationStateRecord.new()
|
||||
@@ -105,4 +119,30 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
resource_ids[resource_id] = true
|
||||
record.resources.append(resource_record)
|
||||
|
||||
var storage_ids := {}
|
||||
for item in storage_data:
|
||||
if not item is Dictionary:
|
||||
return null
|
||||
var storage_record := StorageStateRecord.from_dictionary(item)
|
||||
if storage_record == null:
|
||||
return null
|
||||
var storage_id := storage_record.get_storage_id()
|
||||
if storage_ids.has(storage_id):
|
||||
return null
|
||||
storage_ids[storage_id] = true
|
||||
record.storages.append(storage_record)
|
||||
|
||||
return record
|
||||
|
||||
static func _migrate_v1(legacy_data: Dictionary) -> Dictionary:
|
||||
var migrated := legacy_data.duplicate(true)
|
||||
migrated["schema_version"] = SCHEMA_VERSION
|
||||
var village_data: Dictionary = legacy_data.get("village", {})
|
||||
var initial_food := float(village_data.get("food", 0.0))
|
||||
migrated["storages"] = [
|
||||
StorageStateRecord.create(
|
||||
SimulationIds.STORAGE_VILLAGE_PANTRY,
|
||||
{String(SimulationIds.RESOURCE_FOOD): initial_food}
|
||||
).to_dictionary()
|
||||
]
|
||||
return migrated
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
class_name StorageStateRecord
|
||||
extends RefCounted
|
||||
|
||||
const SCHEMA_VERSION := 1
|
||||
|
||||
var data: Dictionary
|
||||
|
||||
func _init(record_data: Dictionary = {}) -> void:
|
||||
data = record_data.duplicate(true)
|
||||
|
||||
static func create(
|
||||
storage_id: StringName,
|
||||
initial_amounts: Dictionary,
|
||||
capacity: float = 100.0
|
||||
) -> StorageStateRecord:
|
||||
return StorageStateRecord.new({
|
||||
"schema_version": SCHEMA_VERSION,
|
||||
"storage_id": String(storage_id),
|
||||
"amounts": initial_amounts.duplicate(true),
|
||||
"capacity": capacity
|
||||
})
|
||||
|
||||
static func from_dictionary(record_data: Dictionary) -> StorageStateRecord:
|
||||
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
||||
return null
|
||||
if not record_data.has_all(["storage_id", "amounts", "capacity"]):
|
||||
return null
|
||||
if String(record_data["storage_id"]).is_empty():
|
||||
return null
|
||||
if not record_data["amounts"] is Dictionary:
|
||||
return null
|
||||
var normalized := record_data.duplicate(true)
|
||||
normalized["schema_version"] = SCHEMA_VERSION
|
||||
normalized["capacity"] = float(record_data["capacity"])
|
||||
var normalized_amounts := {}
|
||||
for item_id in record_data["amounts"]:
|
||||
normalized_amounts[String(item_id)] = float(
|
||||
record_data["amounts"][item_id]
|
||||
)
|
||||
normalized["amounts"] = normalized_amounts
|
||||
return StorageStateRecord.new(normalized)
|
||||
|
||||
func get_storage_id() -> StringName:
|
||||
return StringName(data["storage_id"])
|
||||
|
||||
func get_amount(item_id: StringName) -> float:
|
||||
return float(data["amounts"].get(String(item_id), 0.0))
|
||||
|
||||
func get_total_amount() -> float:
|
||||
var total := 0.0
|
||||
for amount in data["amounts"].values():
|
||||
total += float(amount)
|
||||
return total
|
||||
|
||||
func deposit(item_id: StringName, requested_amount: float) -> float:
|
||||
var accepted := minf(
|
||||
maxf(requested_amount, 0.0),
|
||||
maxf(float(data["capacity"]) - get_total_amount(), 0.0)
|
||||
)
|
||||
if accepted <= 0.0:
|
||||
return 0.0
|
||||
data["amounts"][String(item_id)] = get_amount(item_id) + accepted
|
||||
return accepted
|
||||
|
||||
func withdraw(item_id: StringName, requested_amount: float) -> float:
|
||||
var removed := minf(maxf(requested_amount, 0.0), get_amount(item_id))
|
||||
if removed <= 0.0:
|
||||
return 0.0
|
||||
data["amounts"][String(item_id)] = get_amount(item_id) - removed
|
||||
return removed
|
||||
|
||||
func to_dictionary() -> Dictionary:
|
||||
return data.duplicate(true)
|
||||
@@ -0,0 +1 @@
|
||||
uid://cc5uvn2eengiw
|
||||
Reference in New Issue
Block a user