feat: add better priorities setting
This commit is contained in:
+104
-20
@@ -1,6 +1,8 @@
|
||||
class_name SimNPC
|
||||
extends RefCounted
|
||||
|
||||
@export var DEBUG_LOGS := true
|
||||
|
||||
const TASK_STATE_IDLE := "idle"
|
||||
const TASK_STATE_TRAVELING := "traveling"
|
||||
const TASK_STATE_WORKING := "working"
|
||||
@@ -79,34 +81,116 @@ func choose_new_task(village: SimVillage) -> void:
|
||||
set_task("rest", 4.0)
|
||||
return
|
||||
|
||||
if village.food < 15.0:
|
||||
set_task("gather_food", 5.0)
|
||||
return
|
||||
var best_task := choose_best_work_task(village)
|
||||
|
||||
if village.wood < 10.0:
|
||||
set_task("gather_wood", 5.0)
|
||||
return
|
||||
|
||||
if village.safety < 40.0 and profession == "guard":
|
||||
set_task("patrol", 6.0)
|
||||
return
|
||||
|
||||
if village.knowledge < 10.0 and profession == "scholar":
|
||||
set_task("study", 6.0)
|
||||
return
|
||||
|
||||
match profession:
|
||||
"farmer":
|
||||
match best_task:
|
||||
"gather_food":
|
||||
set_task("gather_food", 5.0)
|
||||
"woodcutter":
|
||||
"gather_wood":
|
||||
set_task("gather_wood", 5.0)
|
||||
"guard":
|
||||
"patrol":
|
||||
set_task("patrol", 6.0)
|
||||
"scholar":
|
||||
"study":
|
||||
set_task("study", 6.0)
|
||||
_:
|
||||
set_task("wander", 4.0)
|
||||
|
||||
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
|
||||
|
||||
score += 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
|
||||
|
||||
Reference in New Issue
Block a user