feat: introduce identity-backed goat
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
class_name AnimalStateRecord
|
||||
extends RefCounted
|
||||
|
||||
signal changed(state: AnimalStateRecord)
|
||||
signal fed(state: AnimalStateRecord, actor_id: int)
|
||||
|
||||
const SCHEMA_VERSION := 1
|
||||
const HUNGER_PER_TICK := 0.125
|
||||
const FEED_THRESHOLD := 65.0
|
||||
const HUNGER_RELIEF := 55.0
|
||||
const NEVER_FED_TICK := -1
|
||||
|
||||
var data: Dictionary
|
||||
|
||||
|
||||
func _init(record_data: Dictionary = {}) -> void:
|
||||
data = record_data.duplicate(true)
|
||||
|
||||
|
||||
static func create_from_node(node: AnimalNode) -> AnimalStateRecord:
|
||||
return (
|
||||
AnimalStateRecord
|
||||
. new(
|
||||
{
|
||||
"schema_version": SCHEMA_VERSION,
|
||||
"animal_id": String(node.animal_id),
|
||||
"display_name": node.display_name,
|
||||
"species_id": String(node.species_id),
|
||||
"position":
|
||||
[node.global_position.x, node.global_position.y, node.global_position.z],
|
||||
"hunger": node.initial_hunger,
|
||||
"last_fed_tick": NEVER_FED_TICK,
|
||||
"reserved_by": -1,
|
||||
"enabled": node.initial_enabled,
|
||||
"can_npcs_feed": node.can_npcs_feed,
|
||||
"can_player_feed": node.can_player_feed,
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
static func from_dictionary(record_data: Dictionary) -> AnimalStateRecord:
|
||||
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
||||
return null
|
||||
if not (
|
||||
record_data
|
||||
. has_all(
|
||||
[
|
||||
"animal_id",
|
||||
"display_name",
|
||||
"species_id",
|
||||
"position",
|
||||
"hunger",
|
||||
"last_fed_tick",
|
||||
"reserved_by",
|
||||
"enabled",
|
||||
"can_npcs_feed",
|
||||
"can_player_feed",
|
||||
]
|
||||
)
|
||||
):
|
||||
return null
|
||||
var animal_id := String(record_data["animal_id"])
|
||||
var display_name := String(record_data["display_name"])
|
||||
var species_id := String(record_data["species_id"])
|
||||
var saved_position = record_data["position"]
|
||||
var hunger := float(record_data["hunger"])
|
||||
var last_fed_tick := int(record_data["last_fed_tick"])
|
||||
var reserved_by := int(record_data["reserved_by"])
|
||||
if animal_id.is_empty() or display_name.is_empty() or species_id.is_empty():
|
||||
return null
|
||||
if StringName(species_id) != SimulationIds.SPECIES_GOAT:
|
||||
return null
|
||||
if not saved_position is Array or saved_position.size() != 3:
|
||||
return null
|
||||
for component in saved_position:
|
||||
if not is_finite(float(component)):
|
||||
return null
|
||||
if not is_finite(hunger) or hunger < 0.0 or hunger > 100.0:
|
||||
return null
|
||||
if last_fed_tick < NEVER_FED_TICK or reserved_by < -1:
|
||||
return null
|
||||
if reserved_by >= 0 and hunger < FEED_THRESHOLD:
|
||||
return null
|
||||
|
||||
var normalized := record_data.duplicate(true)
|
||||
normalized["schema_version"] = SCHEMA_VERSION
|
||||
normalized["animal_id"] = animal_id
|
||||
normalized["display_name"] = display_name
|
||||
normalized["species_id"] = species_id
|
||||
normalized["position"] = [
|
||||
float(saved_position[0]), float(saved_position[1]), float(saved_position[2])
|
||||
]
|
||||
normalized["hunger"] = hunger
|
||||
normalized["last_fed_tick"] = last_fed_tick
|
||||
normalized["reserved_by"] = reserved_by
|
||||
normalized["enabled"] = bool(record_data["enabled"])
|
||||
normalized["can_npcs_feed"] = bool(record_data["can_npcs_feed"])
|
||||
normalized["can_player_feed"] = bool(record_data["can_player_feed"])
|
||||
return AnimalStateRecord.new(normalized)
|
||||
|
||||
|
||||
func apply_definition(node: AnimalNode) -> bool:
|
||||
if node == null or node.animal_id != get_animal_id():
|
||||
return false
|
||||
return (
|
||||
node.display_name == get_display_name()
|
||||
and node.species_id == get_species_id()
|
||||
and node.can_npcs_feed == can_npc_feed()
|
||||
and node.can_player_feed == can_player_feed_animal()
|
||||
)
|
||||
|
||||
|
||||
func get_animal_id() -> StringName:
|
||||
return StringName(data["animal_id"])
|
||||
|
||||
|
||||
func get_display_name() -> String:
|
||||
return String(data["display_name"])
|
||||
|
||||
|
||||
func get_species_id() -> StringName:
|
||||
return StringName(data["species_id"])
|
||||
|
||||
|
||||
func get_position() -> Vector3:
|
||||
var saved_position: Array = data["position"]
|
||||
return Vector3(float(saved_position[0]), float(saved_position[1]), float(saved_position[2]))
|
||||
|
||||
|
||||
func get_hunger() -> float:
|
||||
return float(data["hunger"])
|
||||
|
||||
|
||||
func get_last_fed_tick() -> int:
|
||||
return int(data["last_fed_tick"])
|
||||
|
||||
|
||||
func get_reserved_by() -> int:
|
||||
return int(data["reserved_by"])
|
||||
|
||||
|
||||
func is_enabled() -> bool:
|
||||
return bool(data["enabled"])
|
||||
|
||||
|
||||
func can_npc_feed() -> bool:
|
||||
return bool(data["can_npcs_feed"])
|
||||
|
||||
|
||||
func can_player_feed_animal() -> bool:
|
||||
return bool(data["can_player_feed"])
|
||||
|
||||
|
||||
func needs_feed() -> bool:
|
||||
return is_enabled() and get_hunger() >= FEED_THRESHOLD
|
||||
|
||||
|
||||
func is_available_for(agent_id: int) -> bool:
|
||||
return needs_feed() and (get_reserved_by() == -1 or get_reserved_by() == agent_id)
|
||||
|
||||
|
||||
func reserve(agent_id: int) -> bool:
|
||||
if agent_id < 0 or not is_available_for(agent_id):
|
||||
return false
|
||||
data["reserved_by"] = agent_id
|
||||
changed.emit(self)
|
||||
return true
|
||||
|
||||
|
||||
func release(agent_id: int) -> void:
|
||||
if get_reserved_by() != agent_id:
|
||||
return
|
||||
data["reserved_by"] = -1
|
||||
changed.emit(self)
|
||||
|
||||
|
||||
func advance_tick() -> void:
|
||||
if not is_enabled():
|
||||
return
|
||||
var next_hunger := minf(get_hunger() + HUNGER_PER_TICK, 100.0)
|
||||
if is_equal_approx(next_hunger, get_hunger()):
|
||||
return
|
||||
data["hunger"] = next_hunger
|
||||
changed.emit(self)
|
||||
|
||||
|
||||
func feed(actor_id: int, current_tick: int) -> bool:
|
||||
if not needs_feed() or current_tick < 0:
|
||||
return false
|
||||
data["hunger"] = maxf(get_hunger() - HUNGER_RELIEF, 0.0)
|
||||
data["last_fed_tick"] = current_tick
|
||||
data["reserved_by"] = -1
|
||||
changed.emit(self)
|
||||
fed.emit(self, actor_id)
|
||||
return true
|
||||
|
||||
|
||||
func set_position(value: Vector3) -> void:
|
||||
if not value.is_finite() or value.is_equal_approx(get_position()):
|
||||
return
|
||||
data["position"] = [value.x, value.y, value.z]
|
||||
changed.emit(self)
|
||||
|
||||
|
||||
func set_hunger(value: float) -> void:
|
||||
if not is_finite(value):
|
||||
return
|
||||
data["hunger"] = clampf(value, 0.0, 100.0)
|
||||
if not needs_feed():
|
||||
data["reserved_by"] = -1
|
||||
changed.emit(self)
|
||||
|
||||
|
||||
func set_enabled(value: bool) -> void:
|
||||
data["enabled"] = value
|
||||
if not value:
|
||||
data["reserved_by"] = -1
|
||||
changed.emit(self)
|
||||
|
||||
|
||||
func to_dictionary() -> Dictionary:
|
||||
return data.duplicate(true)
|
||||
@@ -0,0 +1 @@
|
||||
uid://c8rgw7h25bcmr
|
||||
@@ -158,5 +158,7 @@ func description(npc_names: Dictionary = {}) -> String:
|
||||
return "%s could not complete %s" % [actor_name, action_name]
|
||||
"resource_depleted":
|
||||
return "%s was depleted" % source
|
||||
"animal_fed":
|
||||
return "%s fed %s %.0f %s" % [actor_name, destination, amount, item]
|
||||
_:
|
||||
return "tick %d: %s" % [int(data["tick"]), event_type]
|
||||
|
||||
@@ -2,7 +2,7 @@ class_name SimulationStateRecord
|
||||
extends RefCounted
|
||||
|
||||
const SCHEMA_NAME := "the_steward.simulation"
|
||||
const SCHEMA_VERSION := 9
|
||||
const SCHEMA_VERSION := 10
|
||||
const LEGACY_SCHEMA_VERSION := 1
|
||||
const EVENT_LEGACY_SCHEMA_VERSION := 2
|
||||
const RELATIONSHIP_LEGACY_SCHEMA_VERSION := 3
|
||||
@@ -10,11 +10,13 @@ const KNOWLEDGE_LEGACY_SCHEMA_VERSION := 4
|
||||
const PROVENANCE_LEGACY_SCHEMA_VERSION := 5
|
||||
const RETENTION_LEGACY_SCHEMA_VERSION := 6
|
||||
const OPPORTUNITY_LEGACY_SCHEMA_VERSION := 8
|
||||
const ANIMAL_LEGACY_SCHEMA_VERSION := 9
|
||||
const PREVIOUS_SCHEMA_VERSION := 7
|
||||
|
||||
var simulation: Dictionary
|
||||
var village: VillageStateRecord
|
||||
var npcs: Array[NPCStateRecord] = []
|
||||
var animals: Array[AnimalStateRecord] = []
|
||||
var resources: Array[ResourceStateRecord] = []
|
||||
var storages: Array[StorageStateRecord] = []
|
||||
var economic_events: Array[EconomicEventRecord] = []
|
||||
@@ -28,6 +30,9 @@ func to_dictionary() -> Dictionary:
|
||||
for npc_record in npcs:
|
||||
npc_data.append(npc_record.to_dictionary())
|
||||
|
||||
var animal_data: Array[Dictionary] = []
|
||||
for animal_record in animals:
|
||||
animal_data.append(animal_record.to_dictionary())
|
||||
var resource_data: Array[Dictionary] = []
|
||||
for resource_record in resources:
|
||||
resource_data.append(resource_record.to_dictionary())
|
||||
@@ -53,6 +58,7 @@ func to_dictionary() -> Dictionary:
|
||||
"simulation": simulation.duplicate(true),
|
||||
"village": village.to_dictionary(),
|
||||
"npcs": npc_data,
|
||||
"animals": animal_data,
|
||||
"resources": resource_data,
|
||||
"storages": storage_data,
|
||||
"economic_events": event_data,
|
||||
@@ -88,6 +94,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
RETENTION_LEGACY_SCHEMA_VERSION,
|
||||
PREVIOUS_SCHEMA_VERSION,
|
||||
OPPORTUNITY_LEGACY_SCHEMA_VERSION,
|
||||
ANIMAL_LEGACY_SCHEMA_VERSION,
|
||||
]
|
||||
):
|
||||
record_data = _migrate_legacy(record_data, version)
|
||||
@@ -100,6 +107,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
"simulation",
|
||||
"village",
|
||||
"npcs",
|
||||
"animals",
|
||||
"resources",
|
||||
"storages",
|
||||
"economic_events",
|
||||
@@ -166,6 +174,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
return null
|
||||
|
||||
var npc_data = record_data["npcs"]
|
||||
var animal_data = record_data["animals"]
|
||||
var resource_data = record_data["resources"]
|
||||
var storage_data = record_data["storages"]
|
||||
var event_data = record_data["economic_events"]
|
||||
@@ -174,6 +183,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
var opportunity_data = record_data["opportunities"]
|
||||
if (
|
||||
not npc_data is Array
|
||||
or not animal_data is Array
|
||||
or not resource_data is Array
|
||||
or not storage_data is Array
|
||||
or not event_data is Array
|
||||
@@ -217,6 +227,51 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
resource_records_by_id[resource_id] = resource_record
|
||||
record.resources.append(resource_record)
|
||||
|
||||
var animal_ids := {}
|
||||
var animal_records_by_id := {}
|
||||
for item in animal_data:
|
||||
if not item is Dictionary:
|
||||
return null
|
||||
var animal_record := AnimalStateRecord.from_dictionary(item)
|
||||
if animal_record == null:
|
||||
return null
|
||||
var animal_id := String(animal_record.data["animal_id"])
|
||||
if animal_ids.has(animal_id) or resource_ids.has(animal_id):
|
||||
return null
|
||||
var reserved_by := animal_record.get_reserved_by()
|
||||
if reserved_by >= 0:
|
||||
var reserving_npc := npc_records_by_id.get(reserved_by) as NPCStateRecord
|
||||
if (
|
||||
reserving_npc == null
|
||||
or (
|
||||
StringName(reserving_npc.data["current_task"])
|
||||
!= SimulationIds.ACTION_FEED_ANIMAL
|
||||
)
|
||||
or StringName(reserving_npc.data["target_id"]) != animal_record.get_animal_id()
|
||||
or (
|
||||
StringName(reserving_npc.data["task_state"])
|
||||
not in [SimNPC.TASK_STATE_TRAVELING, SimNPC.TASK_STATE_WORKING]
|
||||
)
|
||||
):
|
||||
return null
|
||||
animal_ids[animal_id] = true
|
||||
animal_records_by_id[animal_id] = animal_record
|
||||
record.animals.append(animal_record)
|
||||
for npc_record in record.npcs:
|
||||
if (
|
||||
StringName(npc_record.data["current_task"]) != SimulationIds.ACTION_FEED_ANIMAL
|
||||
or (
|
||||
StringName(npc_record.data["task_state"])
|
||||
not in [SimNPC.TASK_STATE_TRAVELING, SimNPC.TASK_STATE_WORKING]
|
||||
)
|
||||
):
|
||||
continue
|
||||
var target_animal := (
|
||||
animal_records_by_id.get(StringName(npc_record.data["target_id"])) as AnimalStateRecord
|
||||
)
|
||||
if target_animal == null or target_animal.get_reserved_by() != int(npc_record.data["id"]):
|
||||
return null
|
||||
|
||||
var storage_ids := {}
|
||||
var storage_records_by_id := {}
|
||||
for item in storage_data:
|
||||
@@ -226,7 +281,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
if storage_record == null:
|
||||
return null
|
||||
var storage_id := storage_record.get_storage_id()
|
||||
if storage_ids.has(storage_id):
|
||||
if storage_ids.has(storage_id) or animal_ids.has(String(storage_id)):
|
||||
return null
|
||||
storage_ids[storage_id] = true
|
||||
storage_records_by_id[storage_id] = storage_record
|
||||
@@ -235,6 +290,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
var event_ids := {}
|
||||
var event_records_by_id := {}
|
||||
var highest_event_id := -1
|
||||
var latest_feed_ticks := {}
|
||||
for item in event_data:
|
||||
if not item is Dictionary:
|
||||
return null
|
||||
@@ -247,9 +303,33 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
event_ids[event_id] = true
|
||||
event_records_by_id[event_id] = event_record
|
||||
highest_event_id = maxi(highest_event_id, event_id)
|
||||
if StringName(event_record.data["event_type"]) == SimulationIds.EVENT_ANIMAL_FED:
|
||||
if not _is_valid_animal_feed_event(
|
||||
event_record,
|
||||
npc_ids,
|
||||
animal_records_by_id,
|
||||
storage_records_by_id,
|
||||
int(record.simulation["tick_count"])
|
||||
):
|
||||
return null
|
||||
var fed_animal_id := StringName(event_record.data["destination_id"])
|
||||
latest_feed_ticks[fed_animal_id] = maxi(
|
||||
int(latest_feed_ticks.get(fed_animal_id, AnimalStateRecord.NEVER_FED_TICK)),
|
||||
int(event_record.data["tick"])
|
||||
)
|
||||
record.economic_events.append(event_record)
|
||||
if int(record.simulation["next_event_id"]) <= highest_event_id:
|
||||
return null
|
||||
for animal_record in record.animals:
|
||||
if (
|
||||
animal_record.get_last_fed_tick()
|
||||
!= int(
|
||||
latest_feed_ticks.get(
|
||||
animal_record.get_animal_id(), AnimalStateRecord.NEVER_FED_TICK
|
||||
)
|
||||
)
|
||||
):
|
||||
return null
|
||||
|
||||
var knowledge_keys := {}
|
||||
var knowledge_records_by_key := {}
|
||||
@@ -359,6 +439,31 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
return record
|
||||
|
||||
|
||||
static func _is_valid_animal_feed_event(
|
||||
event: EconomicEventRecord,
|
||||
npc_ids: Dictionary,
|
||||
animal_records_by_id: Dictionary,
|
||||
storage_records_by_id: Dictionary,
|
||||
current_tick: int
|
||||
) -> bool:
|
||||
var actor_id := int(event.data["actor_id"])
|
||||
var definition := SimulationDefinitions.get_action(SimulationIds.ACTION_FEED_ANIMAL)
|
||||
var animal := (
|
||||
animal_records_by_id.get(StringName(event.data["destination_id"])) as AnimalStateRecord
|
||||
)
|
||||
return (
|
||||
(actor_id == -1 or npc_ids.has(actor_id))
|
||||
and int(event.data["tick"]) <= current_tick
|
||||
and animal != null
|
||||
and StringName(event.data["source_id"]) == SimulationIds.STORAGE_VILLAGE_PANTRY
|
||||
and storage_records_by_id.has(SimulationIds.STORAGE_VILLAGE_PANTRY)
|
||||
and StringName(event.data["item_id"]) == SimulationIds.RESOURCE_FOOD
|
||||
and definition != null
|
||||
and definition.has_completion_cost()
|
||||
and is_equal_approx(float(event.data["amount"]), definition.completion_cost_amount)
|
||||
)
|
||||
|
||||
|
||||
static func _is_valid_knowledge_provenance(
|
||||
known_event: KnownEventStateRecord,
|
||||
npc_ids: Dictionary,
|
||||
@@ -571,6 +676,7 @@ 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
|
||||
migrated["animals"] = []
|
||||
if version == LEGACY_SCHEMA_VERSION:
|
||||
var village_data: Dictionary = legacy_data.get("village", {})
|
||||
var initial_food := float(village_data.get("food", 0.0))
|
||||
@@ -621,7 +727,7 @@ static func _migrate_legacy(legacy_data: Dictionary, version: int) -> Dictionary
|
||||
migrated.get("economic_events", []),
|
||||
int((migrated.get("simulation", {}) as Dictionary).get("tick_count", 0))
|
||||
)
|
||||
if version != OPPORTUNITY_LEGACY_SCHEMA_VERSION:
|
||||
if version not in [OPPORTUNITY_LEGACY_SCHEMA_VERSION, ANIMAL_LEGACY_SCHEMA_VERSION]:
|
||||
migrated["opportunities"] = []
|
||||
var opportunity_simulation_data: Dictionary = migrated.get("simulation", {})
|
||||
opportunity_simulation_data["next_opportunity_id"] = 0
|
||||
|
||||
Reference in New Issue
Block a user