refactor: separate action system responsibilities
This commit is contained in:
@@ -55,56 +55,6 @@ func _init(
|
||||
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 != SimulationIds.ACTION_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 = SimulationIds.ACTION_DEAD
|
||||
@@ -116,30 +66,6 @@ func die_from_starvation() -> void:
|
||||
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(SimulationIds.ACTION_EAT, 1.0)
|
||||
else:
|
||||
set_task(SimulationIds.ACTION_GATHER_FOOD, 4.0)
|
||||
return
|
||||
|
||||
if hunger > 75.0:
|
||||
if village.food > 0:
|
||||
set_task(SimulationIds.ACTION_EAT)
|
||||
else:
|
||||
set_task(SimulationIds.ACTION_GATHER_FOOD)
|
||||
return
|
||||
|
||||
if energy < 25.0:
|
||||
set_task(SimulationIds.ACTION_REST)
|
||||
return
|
||||
|
||||
set_task(choose_best_work_task(village))
|
||||
|
||||
func should_eat_immediately_after_task() -> bool:
|
||||
return (
|
||||
current_task == SimulationIds.ACTION_GATHER_FOOD
|
||||
@@ -147,105 +73,6 @@ func should_eat_immediately_after_task() -> bool:
|
||||
and not is_dead
|
||||
)
|
||||
|
||||
func choose_best_work_task(village: SimVillage) -> StringName:
|
||||
var food_score := calculate_task_score(
|
||||
SimulationIds.ACTION_GATHER_FOOD,
|
||||
village.food_priority,
|
||||
village.food,
|
||||
15.0,
|
||||
30.0,
|
||||
3.0,
|
||||
1.5
|
||||
)
|
||||
|
||||
var wood_score := calculate_task_score(
|
||||
SimulationIds.ACTION_GATHER_WOOD,
|
||||
village.wood_priority,
|
||||
village.wood,
|
||||
10.0,
|
||||
20.0,
|
||||
2.5,
|
||||
1.0
|
||||
)
|
||||
|
||||
var safety_score := calculate_task_score(
|
||||
SimulationIds.ACTION_PATROL,
|
||||
village.safety_priority,
|
||||
village.safety,
|
||||
30.0,
|
||||
50.0,
|
||||
3.0,
|
||||
1.5
|
||||
)
|
||||
|
||||
var knowledge_score := calculate_task_score(
|
||||
SimulationIds.ACTION_STUDY,
|
||||
village.knowledge_priority,
|
||||
village.knowledge,
|
||||
10.0,
|
||||
25.0,
|
||||
1.5,
|
||||
0.75
|
||||
)
|
||||
|
||||
var best_task := SimulationIds.ACTION_GATHER_FOOD
|
||||
var best_score := food_score
|
||||
|
||||
if wood_score > best_score:
|
||||
best_task = SimulationIds.ACTION_GATHER_WOOD
|
||||
best_score = wood_score
|
||||
|
||||
if safety_score > best_score:
|
||||
best_task = SimulationIds.ACTION_PATROL
|
||||
best_score = safety_score
|
||||
|
||||
if knowledge_score > best_score:
|
||||
best_task = SimulationIds.ACTION_STUDY
|
||||
best_score = knowledge_score
|
||||
|
||||
return best_task
|
||||
|
||||
func calculate_task_score(
|
||||
action_id: StringName,
|
||||
base_priority: float,
|
||||
resource_amount: float,
|
||||
critical_threshold: float,
|
||||
low_threshold: float,
|
||||
critical_bonus: float,
|
||||
low_bonus: float
|
||||
) -> float:
|
||||
var score := base_priority
|
||||
|
||||
if resource_amount < critical_threshold:
|
||||
score += critical_bonus
|
||||
elif resource_amount < low_threshold:
|
||||
score += low_bonus
|
||||
|
||||
var definition := SimulationDefinitions.get_action(action_id)
|
||||
if (
|
||||
definition != null
|
||||
and not definition.preferred_profession_id.is_empty()
|
||||
and profession == definition.preferred_profession_id
|
||||
):
|
||||
score += 2.0
|
||||
|
||||
if action_id == last_task:
|
||||
score -= 1.5
|
||||
|
||||
score += random_source.randf_range(0.0, 0.5)
|
||||
|
||||
if debug_logs:
|
||||
print(
|
||||
"[SimNPC] ",
|
||||
npc_name,
|
||||
" score ",
|
||||
action_id,
|
||||
" = ",
|
||||
score
|
||||
)
|
||||
|
||||
return score
|
||||
|
||||
func set_task(action_id: StringName, duration: float = -1.0) -> void:
|
||||
var definition := SimulationDefinitions.get_action(action_id)
|
||||
if definition == null:
|
||||
|
||||
Reference in New Issue
Block a user