hy-farm/godot/FarmGodot/scripts/domain/rules/land_action_rules.gd
2026-05-29 19:54:56 +08:00

138 lines
4.2 KiB
GDScript

class_name LandActionRules
extends RefCounted
const ACTION_PLANT := "plant"
const ACTION_GAIN := "gain"
const ACTION_UPROOT := "uproot"
const ACTION_MANURE := "manure"
const ACTION_WATER := "water"
const ACTION_DIE_BUG := "die_bug"
const ACTION_DIE_GRASS := "die_grass"
const ACTION_STEAL := "steal"
const ACTION_PUT_BUG := "put_bug"
const ACTION_PUT_GRASS := "put_grass"
const INTERACTION_TYPE_STEAL := 0
const INTERACTION_TYPE_BUG := 1
const INTERACTION_TYPE_GRASS := 2
const INTERACTION_TYPE_WATERING := 3
const INTERACTION_TYPE_DISINSECTION := 4
const INTERACTION_TYPE_WEEDING := 5
var _crop_phase_rules: CropPhaseRules
func _init(crop_phase_rules: CropPhaseRules = null) -> void:
_crop_phase_rules = crop_phase_rules
func set_crop_phase_rules(crop_phase_rules: CropPhaseRules) -> void:
_crop_phase_rules = crop_phase_rules
func get_actions(land_data: Dictionary) -> Array[String]:
if int(land_data.get("crop_id", 0)) == 0:
return [ACTION_PLANT]
if int(land_data.get("has_gather", 0)) == 1:
return [ACTION_UPROOT]
if _is_ripe(land_data):
return [ACTION_GAIN]
var actions: Array[String] = [ACTION_UPROOT, ACTION_MANURE]
if int(land_data.get("has_dry", 0)) == 1:
actions.append(ACTION_WATER)
if int(land_data.get("has_bug", 0)) == 1:
actions.append(ACTION_DIE_BUG)
if int(land_data.get("has_grass", 0)) == 1:
actions.append(ACTION_DIE_GRASS)
return actions
func get_friend_actions(land_data: Dictionary) -> Array[String]:
if int(land_data.get("crop_id", 0)) == 0:
return []
if land_data.has("interaction_type_list"):
var actions := _actions_from_interaction_type_list(land_data.get("interaction_type_list", []))
if actions.has(ACTION_STEAL) and _has_protected_output_fields(land_data) and not _has_stealable_output(land_data):
actions.erase(ACTION_STEAL)
return actions
return _fallback_friend_actions(land_data)
func can_friend_steal(land_data: Dictionary) -> bool:
if int(land_data.get("crop_id", 0)) <= 0:
return false
if int(land_data.get("has_gather", 0)) == 1:
return false
if _has_ripe_fields(land_data) and not _is_ripe(land_data):
return false
if _has_protected_output_fields(land_data):
return _has_stealable_output(land_data)
return true
func _is_ripe(land_data: Dictionary) -> bool:
if _crop_phase_rules == null:
return int(land_data.get("is_ripe", 0)) == 1
return _crop_phase_rules.is_ripe(land_data)
func _has_ripe_fields(land_data: Dictionary) -> bool:
return land_data.has("is_ripe") or land_data.has("crop_phase") or land_data.has("crop_gather_time")
func _has_protected_output_fields(land_data: Dictionary) -> bool:
return land_data.has("crop_num") and land_data.has("crop_min_num")
func _has_stealable_output(land_data: Dictionary) -> bool:
return int(land_data.get("crop_num", 0)) > int(land_data.get("crop_min_num", 0))
func _actions_from_interaction_type_list(raw_types: Variant) -> Array[String]:
var actions: Array[String] = []
if not raw_types is Array:
return actions
for raw_type in raw_types:
var action := _action_for_interaction_type(int(raw_type))
if not action.is_empty() and not actions.has(action):
actions.append(action)
return actions
func _fallback_friend_actions(land_data: Dictionary) -> Array[String]:
if int(land_data.get("has_gather", 0)) == 1:
return []
if _is_ripe(land_data):
return [ACTION_STEAL]
var actions: Array[String] = []
if int(land_data.get("has_bug", 0)) == 0 and int(land_data.get("crop_phase", 1)) > 0:
actions.append(ACTION_PUT_BUG)
if int(land_data.get("has_grass", 0)) == 0:
actions.append(ACTION_PUT_GRASS)
if int(land_data.get("has_dry", 0)) == 1:
actions.append(ACTION_WATER)
if int(land_data.get("has_bug", 0)) == 1:
actions.append(ACTION_DIE_BUG)
if int(land_data.get("has_grass", 0)) == 1:
actions.append(ACTION_DIE_GRASS)
return actions
func _action_for_interaction_type(interaction_type: int) -> String:
match interaction_type:
INTERACTION_TYPE_STEAL:
return ACTION_STEAL
INTERACTION_TYPE_BUG:
return ACTION_PUT_BUG
INTERACTION_TYPE_GRASS:
return ACTION_PUT_GRASS
INTERACTION_TYPE_WATERING:
return ACTION_WATER
INTERACTION_TYPE_DISINSECTION:
return ACTION_DIE_BUG
INTERACTION_TYPE_WEEDING:
return ACTION_DIE_GRASS
_:
return ""