feat: let the player live as a citizen with needs and carried inventory
This commit is contained in:
@@ -2,7 +2,7 @@ class_name SimulationStateRecord
|
||||
extends RefCounted
|
||||
|
||||
const SCHEMA_NAME := "the_steward.simulation"
|
||||
const SCHEMA_VERSION := 12
|
||||
const SCHEMA_VERSION := 13
|
||||
const LEGACY_SCHEMA_VERSION := 1
|
||||
const EVENT_LEGACY_SCHEMA_VERSION := 2
|
||||
const RELATIONSHIP_LEGACY_SCHEMA_VERSION := 3
|
||||
@@ -13,6 +13,7 @@ const OPPORTUNITY_LEGACY_SCHEMA_VERSION := 8
|
||||
const ANIMAL_LEGACY_SCHEMA_VERSION := 9
|
||||
const ROUTINE_LEGACY_SCHEMA_VERSION := 10
|
||||
const PLAYER_RELATIONSHIP_SCHEMA_VERSION := 11
|
||||
const PLAYER_NEEDS_SCHEMA_VERSION := 12
|
||||
const PREVIOUS_SCHEMA_VERSION := 7
|
||||
|
||||
var simulation: Dictionary
|
||||
@@ -25,6 +26,7 @@ var economic_events: Array[EconomicEventRecord] = []
|
||||
var relationships: Array[RelationshipStateRecord] = []
|
||||
var event_knowledge: Array[KnownEventStateRecord] = []
|
||||
var opportunities: Array[OpportunityStateRecord] = []
|
||||
var player: PlayerStateRecord
|
||||
|
||||
|
||||
func to_dictionary() -> Dictionary:
|
||||
@@ -66,7 +68,8 @@ func to_dictionary() -> Dictionary:
|
||||
"economic_events": event_data,
|
||||
"relationships": relationship_data,
|
||||
"event_knowledge": knowledge_data,
|
||||
"opportunities": opportunity_data
|
||||
"opportunities": opportunity_data,
|
||||
"player": player.to_dictionary()
|
||||
}
|
||||
|
||||
|
||||
@@ -99,6 +102,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
ANIMAL_LEGACY_SCHEMA_VERSION,
|
||||
ROUTINE_LEGACY_SCHEMA_VERSION,
|
||||
PLAYER_RELATIONSHIP_SCHEMA_VERSION,
|
||||
PLAYER_NEEDS_SCHEMA_VERSION,
|
||||
]
|
||||
):
|
||||
record_data = _migrate_legacy(record_data, version)
|
||||
@@ -118,6 +122,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
"relationships",
|
||||
"event_knowledge",
|
||||
"opportunities",
|
||||
"player",
|
||||
]
|
||||
)
|
||||
):
|
||||
@@ -200,6 +205,13 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
var record := SimulationStateRecord.new()
|
||||
record.simulation = simulation_data.duplicate(true)
|
||||
record.village = village_record
|
||||
var player_data = record_data["player"]
|
||||
if not player_data is Dictionary:
|
||||
return null
|
||||
var player_record := PlayerStateRecord.from_dictionary(player_data)
|
||||
if player_record == null:
|
||||
return null
|
||||
record.player = player_record
|
||||
var npc_ids := {}
|
||||
var npc_records_by_id := {}
|
||||
|
||||
@@ -681,12 +693,23 @@ static func _is_valid_relationship_cause(
|
||||
and StringName(cause_event.data["item_id"]) == SimulationIds.RESOURCE_FOOD
|
||||
and float(cause_event.data["amount"]) > 0.0
|
||||
)
|
||||
if int(cause_event.data["actor_id"]) != SimulationIds.PLAYER_ACTOR_ID:
|
||||
return false
|
||||
if StringName(cause_event.data["item_id"]) != SimulationIds.RESOURCE_FOOD:
|
||||
return false
|
||||
if float(cause_event.data["amount"]) <= 0.0:
|
||||
return false
|
||||
if StringName(cause_event.data["event_type"]) == SimulationIds.EVENT_STORAGE_DEPOSITED:
|
||||
return (
|
||||
StringName(cause_event.data["source_id"]) == SimulationIds.PLAYER_INVENTORY_ID
|
||||
and (
|
||||
StringName(cause_event.data["destination_id"])
|
||||
== SimulationIds.STORAGE_VILLAGE_PANTRY
|
||||
)
|
||||
)
|
||||
return (
|
||||
int(cause_event.data["actor_id"]) == SimulationIds.PLAYER_ACTOR_ID
|
||||
and StringName(cause_event.data["event_type"]) == SimulationIds.EVENT_RESOURCE_EXTRACTED
|
||||
and (StringName(cause_event.data["destination_id"]) == SimulationIds.STORAGE_VILLAGE_PANTRY)
|
||||
and StringName(cause_event.data["item_id"]) == SimulationIds.RESOURCE_FOOD
|
||||
and float(cause_event.data["amount"]) > 0.0
|
||||
StringName(cause_event.data["event_type"]) == SimulationIds.EVENT_RESOURCE_EXTRACTED
|
||||
and StringName(cause_event.data["destination_id"]) == SimulationIds.STORAGE_VILLAGE_PANTRY
|
||||
)
|
||||
|
||||
|
||||
@@ -705,6 +728,8 @@ static func _is_valid_supply_resolution(
|
||||
):
|
||||
return false
|
||||
if event_type == SimulationIds.EVENT_STORAGE_DEPOSITED:
|
||||
if actor_id == SimulationIds.PLAYER_ACTOR_ID:
|
||||
return StringName(event.data["source_id"]) == SimulationIds.PLAYER_INVENTORY_ID
|
||||
return (
|
||||
npc_ids.has(actor_id)
|
||||
and StringName(event.data["source_id"]) == SimulationIds.npc_inventory_id(actor_id)
|
||||
@@ -724,6 +749,8 @@ static func _is_valid_supply_resolution(
|
||||
static func _migrate_legacy(legacy_data: Dictionary, version: int) -> Dictionary:
|
||||
var migrated := legacy_data.duplicate(true)
|
||||
migrated["schema_version"] = SCHEMA_VERSION
|
||||
if not migrated.has("player"):
|
||||
migrated["player"] = PlayerStateRecord.create_default().to_dictionary()
|
||||
if version <= ANIMAL_LEGACY_SCHEMA_VERSION:
|
||||
migrated["animals"] = []
|
||||
if version == LEGACY_SCHEMA_VERSION:
|
||||
@@ -785,6 +812,7 @@ static func _migrate_legacy(legacy_data: Dictionary, version: int) -> Dictionary
|
||||
ANIMAL_LEGACY_SCHEMA_VERSION,
|
||||
ROUTINE_LEGACY_SCHEMA_VERSION,
|
||||
PLAYER_RELATIONSHIP_SCHEMA_VERSION,
|
||||
PLAYER_NEEDS_SCHEMA_VERSION,
|
||||
]
|
||||
):
|
||||
migrated["opportunities"] = []
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
class_name PlayerStateRecord
|
||||
extends RefCounted
|
||||
|
||||
const SCHEMA_VERSION := 1
|
||||
|
||||
var data: Dictionary
|
||||
|
||||
|
||||
func _init(record_data: Dictionary = {}) -> void:
|
||||
data = record_data.duplicate(true)
|
||||
|
||||
|
||||
static func create_default() -> PlayerStateRecord:
|
||||
return (
|
||||
PlayerStateRecord
|
||||
. new(
|
||||
{
|
||||
"schema_version": SCHEMA_VERSION,
|
||||
"hunger": 40.0,
|
||||
"energy": 100.0,
|
||||
"inventory": {},
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
static func from_dictionary(record_data: Dictionary) -> PlayerStateRecord:
|
||||
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
||||
return null
|
||||
if not record_data.has_all(["hunger", "energy", "inventory"]):
|
||||
return null
|
||||
if not record_data["inventory"] is Dictionary:
|
||||
return null
|
||||
var hunger := float(record_data["hunger"])
|
||||
var energy := float(record_data["energy"])
|
||||
if not is_finite(hunger) or not is_finite(energy):
|
||||
return null
|
||||
var normalized_inventory := {}
|
||||
for raw_item_id in record_data["inventory"]:
|
||||
var item_id := String(raw_item_id)
|
||||
var amount := float(record_data["inventory"][raw_item_id])
|
||||
if (
|
||||
item_id.is_empty()
|
||||
or normalized_inventory.has(item_id)
|
||||
or not is_finite(amount)
|
||||
or amount < 0.0
|
||||
):
|
||||
return null
|
||||
normalized_inventory[item_id] = amount
|
||||
return (
|
||||
PlayerStateRecord
|
||||
. new(
|
||||
{
|
||||
"schema_version": SCHEMA_VERSION,
|
||||
"hunger": clampf(hunger, 0.0, 100.0),
|
||||
"energy": clampf(energy, 0.0, 100.0),
|
||||
"inventory": normalized_inventory,
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
func get_hunger() -> float:
|
||||
return clampf(float(data["hunger"]), 0.0, 100.0)
|
||||
|
||||
|
||||
func get_energy() -> float:
|
||||
return clampf(float(data["energy"]), 0.0, 100.0)
|
||||
|
||||
|
||||
func set_hunger(amount: float) -> void:
|
||||
data["hunger"] = clampf(amount, 0.0, 100.0)
|
||||
|
||||
|
||||
func set_energy(amount: float) -> void:
|
||||
data["energy"] = clampf(amount, 0.0, 100.0)
|
||||
|
||||
|
||||
func get_inventory_amount(item_id: StringName) -> float:
|
||||
return float(data["inventory"].get(String(item_id), 0.0))
|
||||
|
||||
|
||||
func get_carried_total() -> float:
|
||||
var total := 0.0
|
||||
for amount in data["inventory"].values():
|
||||
total += float(amount)
|
||||
return total
|
||||
|
||||
|
||||
func add_inventory(item_id: StringName, amount: float) -> void:
|
||||
if item_id.is_empty() or not is_finite(amount) or amount <= 0.0:
|
||||
return
|
||||
data["inventory"][String(item_id)] = get_inventory_amount(item_id) + amount
|
||||
|
||||
|
||||
func remove_inventory(item_id: StringName, requested_amount: float) -> float:
|
||||
if item_id.is_empty() or not is_finite(requested_amount) or requested_amount <= 0.0:
|
||||
return 0.0
|
||||
var removed := minf(requested_amount, get_inventory_amount(item_id))
|
||||
if removed <= 0.0:
|
||||
return 0.0
|
||||
data["inventory"][String(item_id)] = get_inventory_amount(item_id) - removed
|
||||
return removed
|
||||
|
||||
|
||||
func to_dictionary() -> Dictionary:
|
||||
return data.duplicate(true)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dynd78rglkeji
|
||||
Reference in New Issue
Block a user