37610242bf
Replace implicit randomness with seeded per-NPC RandomNumberGenerator streams so the same seed always produces the same scenario outcome. - Add SimulationClock (RefCounted): explicit fixed-step clock that converts frame delta into simulation ticks while preserving sub-tick remainder; replaces raw _process tick_timer accumulation - Add simulation_seed export (int=1337) to SimulationManager; drive per-NPC RNG streams via seed + npc_id + stream_id derivation (stream 0=init, stream 1=wander) - Add get_wander_offset(npc_id) to SimulationManager so wander targets are deterministic per seed; WorldViewManager calls it instead of local randf_range() - Add get_state_snapshot() and get_state_checksum() to SimulationManager for headless verification and future save/load - Convert SimNPC/SimVillage const DEBUG_LOGS to instance var debug_logs so headless tests can silence output without recompilation - Pass RandomNumberGenerator through SimNPC._init() instead of using global randf_range() for hunger/energy/position/scoring noise - Remove obsolete farm_zone and forest_zone exports from WorldViewManager and their marker/tree scene children from main.tscn; NPC gather tasks use ResourceNode exclusively - Rename TaskZone container to ActivityMarkers in main.tscn and update all scene paths (player, WVM, tests) to match - Add headless deterministic_simulation_test.gd: verifies clock accuracy, same-seed equality, different-seed divergence, and 24-tick scenario checksum without loading main.tscn
271 lines
5.2 KiB
GDScript
271 lines
5.2 KiB
GDScript
class_name SimNPC
|
|
extends RefCounted
|
|
|
|
const TASK_STATE_IDLE := "idle"
|
|
const TASK_STATE_TRAVELING := "traveling"
|
|
const TASK_STATE_WORKING := "working"
|
|
const TASK_STATE_COMPLETE := "complete"
|
|
|
|
var id: int
|
|
var npc_name: String
|
|
var profession: String
|
|
var hunger: float
|
|
var energy: float
|
|
var strength: float
|
|
var intelligence: float
|
|
var current_task: String = "idle"
|
|
var task_state: String = TASK_STATE_IDLE
|
|
var position: Vector3
|
|
|
|
var is_starving := false
|
|
var starvation_ticks := 0
|
|
var starvation_death_threshold := 20
|
|
var is_dead := false
|
|
|
|
var task_duration := 0.0
|
|
var task_progress := 0.0
|
|
var task_complete := true
|
|
var target_id: StringName = &""
|
|
var last_task := ""
|
|
var random_source: RandomNumberGenerator
|
|
var debug_logs := true
|
|
|
|
func _init(
|
|
_id: int,
|
|
_npc_name: String,
|
|
_profession: String,
|
|
_strength: float,
|
|
_intelligence: float,
|
|
_random_source: RandomNumberGenerator = null
|
|
) -> void:
|
|
id = _id
|
|
npc_name = _npc_name
|
|
profession = _profession
|
|
strength = _strength
|
|
intelligence = _intelligence
|
|
random_source = _random_source
|
|
if random_source == null:
|
|
random_source = RandomNumberGenerator.new()
|
|
random_source.seed = id + 1
|
|
hunger = random_source.randf_range(20.0, 80.0)
|
|
energy = random_source.randf_range(40.0, 100.0)
|
|
position = Vector3(
|
|
random_source.randf_range(-8.0, 8.0),
|
|
0.0,
|
|
random_source.randf_range(-8.0, 8.0)
|
|
)
|
|
|
|
func simulate_tick(village: SimVillage) -> void:
|
|
if is_dead:
|
|
return
|
|
|
|
update_needs(village)
|
|
|
|
if is_dead:
|
|
return
|
|
|
|
if task_state == TASK_STATE_IDLE or task_state == TASK_STATE_COMPLETE:
|
|
choose_new_task(village)
|
|
return
|
|
|
|
if task_state == TASK_STATE_TRAVELING:
|
|
return
|
|
|
|
if task_state == TASK_STATE_WORKING:
|
|
task_progress += 1.0
|
|
|
|
if task_progress >= task_duration:
|
|
task_complete = true
|
|
task_state = TASK_STATE_COMPLETE
|
|
|
|
func update_needs(village: SimVillage) -> void:
|
|
hunger += 3.0 * village.food_modifier
|
|
|
|
match task_state:
|
|
TASK_STATE_IDLE:
|
|
energy -= 0.5
|
|
TASK_STATE_TRAVELING:
|
|
energy -= 2.0
|
|
TASK_STATE_WORKING:
|
|
if current_task != "rest":
|
|
energy -= 3.0
|
|
TASK_STATE_COMPLETE:
|
|
energy -= 0.25
|
|
|
|
hunger = clamp(hunger, 0.0, 100.0)
|
|
energy = clamp(energy, 0.0, 100.0)
|
|
|
|
is_starving = hunger >= 90.0
|
|
|
|
if is_starving:
|
|
starvation_ticks += 1
|
|
else:
|
|
starvation_ticks = 0
|
|
|
|
if starvation_ticks >= starvation_death_threshold:
|
|
die_from_starvation()
|
|
|
|
func die_from_starvation() -> void:
|
|
is_dead = true
|
|
current_task = "dead"
|
|
task_state = TASK_STATE_IDLE
|
|
task_progress = 0.0
|
|
task_duration = 0.0
|
|
task_complete = true
|
|
|
|
if debug_logs:
|
|
print("[SimNPC] ", npc_name, " died from starvation.")
|
|
|
|
func choose_new_task(village: SimVillage) -> void:
|
|
if is_dead:
|
|
return
|
|
|
|
if is_starving:
|
|
if village.food > 0:
|
|
set_task("eat", 1.0)
|
|
else:
|
|
set_task("gather_food", 4.0)
|
|
return
|
|
|
|
if hunger > 75.0:
|
|
if village.food > 0:
|
|
set_task("eat", 2.0)
|
|
else:
|
|
set_task("gather_food", 5.0)
|
|
return
|
|
|
|
if energy < 25.0:
|
|
set_task("rest", 4.0)
|
|
return
|
|
|
|
var best_task := choose_best_work_task(village)
|
|
|
|
match best_task:
|
|
"gather_food":
|
|
set_task("gather_food", 5.0)
|
|
"gather_wood":
|
|
set_task("gather_wood", 5.0)
|
|
"patrol":
|
|
set_task("patrol", 6.0)
|
|
"study":
|
|
set_task("study", 6.0)
|
|
_:
|
|
set_task("wander", 4.0)
|
|
|
|
func should_eat_immediately_after_task() -> bool:
|
|
return current_task == "gather_food" and is_starving and not is_dead
|
|
|
|
func choose_best_work_task(village: SimVillage) -> String:
|
|
var food_score := calculate_task_score(
|
|
"gather_food",
|
|
village.food_priority,
|
|
village.food,
|
|
15.0,
|
|
30.0,
|
|
3.0,
|
|
1.5,
|
|
"farmer"
|
|
)
|
|
|
|
var wood_score := calculate_task_score(
|
|
"gather_wood",
|
|
village.wood_priority,
|
|
village.wood,
|
|
10.0,
|
|
20.0,
|
|
2.5,
|
|
1.0,
|
|
"woodcutter"
|
|
)
|
|
|
|
var safety_score := calculate_task_score(
|
|
"patrol",
|
|
village.safety_priority,
|
|
village.safety,
|
|
30.0,
|
|
50.0,
|
|
3.0,
|
|
1.5,
|
|
"guard"
|
|
)
|
|
|
|
var knowledge_score := calculate_task_score(
|
|
"study",
|
|
village.knowledge_priority,
|
|
village.knowledge,
|
|
10.0,
|
|
25.0,
|
|
1.5,
|
|
0.75,
|
|
"scholar"
|
|
)
|
|
|
|
var best_task := "gather_food"
|
|
var best_score := food_score
|
|
|
|
if wood_score > best_score:
|
|
best_task = "gather_wood"
|
|
best_score = wood_score
|
|
|
|
if safety_score > best_score:
|
|
best_task = "patrol"
|
|
best_score = safety_score
|
|
|
|
if knowledge_score > best_score:
|
|
best_task = "study"
|
|
best_score = knowledge_score
|
|
|
|
return best_task
|
|
|
|
func calculate_task_score(
|
|
task_name: String,
|
|
base_priority: float,
|
|
resource_amount: float,
|
|
critical_threshold: float,
|
|
low_threshold: float,
|
|
critical_bonus: float,
|
|
low_bonus: float,
|
|
preferred_profession: String
|
|
) -> float:
|
|
var score := base_priority
|
|
|
|
if resource_amount < critical_threshold:
|
|
score += critical_bonus
|
|
elif resource_amount < low_threshold:
|
|
score += low_bonus
|
|
|
|
if profession == preferred_profession:
|
|
score += 2.0
|
|
|
|
if task_name == last_task:
|
|
score -= 1.5
|
|
|
|
score += random_source.randf_range(0.0, 0.5)
|
|
|
|
if debug_logs:
|
|
print(
|
|
"[SimNPC] ",
|
|
npc_name,
|
|
" score ",
|
|
task_name,
|
|
" = ",
|
|
score
|
|
)
|
|
|
|
return score
|
|
|
|
func set_task(task: String, duration: float) -> void:
|
|
current_task = task
|
|
task_duration = duration
|
|
task_progress = 0.0
|
|
task_complete = false
|
|
task_state = TASK_STATE_TRAVELING
|
|
|
|
func start_working() -> void:
|
|
if task_state != TASK_STATE_TRAVELING:
|
|
return
|
|
|
|
task_state = TASK_STATE_WORKING
|
|
task_progress = 0.0
|
|
task_complete = false
|