e4s-sdk/gamedata/scripts/xr_companion.script
2026-06-17 23:06:51 +03:00

252 lines
No EOL
8.4 KiB
Text

local desired_distance = 1
local min_distance = 1
local keep_state_min_time = 1000 -- ïåðåêëþ÷èâøèñü â ñîñòîÿíèå (áåã, õîäüáà, ñïðèíò), íå ïåðåêëþ÷àòüñÿ â äðóãîå N ms
local dist_walk = 4 -- < dist_run
local dist_run = 20
-- otherwise - sprint
local sound_wait = "weather,state"
-- Ìîäåëè ïîâåäåíèÿ:
beh_walk_simple = 0
beh_walk_near = 1
beh_walk_ignore = 2
beh_wait_simple = 3
beh_wait_near = 4
beh_wait_ignore = 5
local mt_stand = 0
local mt_walk = 1
local mt_run = 2
local mt_sprint = 3
----------------------------------------------------------------------------------------------------------------------
class "evaluator_need_companion" (property_evaluator)
function evaluator_need_companion:__init(storage, name) super (nil, name)
self.st = storage
end
function evaluator_need_companion:evaluate()
return xr_logic.is_active(self.object, self.st)
end
----------------------------------------------------------------------------------------------------------------------
class "action_companion_activity" (action_base)
function action_companion_activity:__init(npc_name, action_name, storage) super (nil, action_name)
self.st = storage
end
function action_companion_activity:initialize()
--printf("_bp: action_companion_activity: initialize")
action_base.initialize(self)
self.object:set_desired_position()
self.object:set_desired_direction()
self.object:enable_talk()
self.assist_point = nil
self.last_state = "guard_na"
state_mgr.set_state(self.object, self.last_state, nil, nil, nil, { animation = true })
self.keep_state_until = time_global()
end
function action_companion_activity:beh_walk_simple()
local actor = db.actor
local select_new_pt = false
local target = nil
local dist_from_self_to_actor = self.object:position():distance_to(actor:position())
local dist_from_assist_pt_to_actor
if self.assist_point then
dist_from_assist_pt_to_actor = level.vertex_position(self.assist_point):distance_to(actor:position())
else
dist_from_assist_pt_to_actor = nil
end
if dist_from_self_to_actor >= desired_distance and
(not dist_from_assist_pt_to_actor or
dist_from_assist_pt_to_actor >= desired_distance * 2) then
select_new_pt = true
end
-- 1. Åñëè ìû íàõîäèìñÿ çà áîëüøèì ðàäèóñîì - íåîáõîäèìî âûáðàòü íîâóþ òî÷êó
if select_new_pt then
self.assist_point = select_position(self.object, self.st)
if not self.assist_point then
return
end
elseif not self.assist_point then
return
end
-- 2. Äâèãàåìñÿ íà òî÷êó: åñëè òî÷êà äàëåêî - áåæèì, èíà÷å èäåì
self.object:set_path_type(game_object.level_path)
self.object:set_dest_level_vertex_id(self.assist_point)
local dist_to_assist_pt = level.vertex_position(self.assist_point):distance_to(self.object:position())
printf("_bp: action_companion_activity:execute(): name='%s', dist_to_assist_pt=%s",
self.object:name(), dist_to_assist_pt)
local new_state
if self.object:level_vertex_id() == self.assist_point then
-- Óæå ïðèøëè íà òî÷êó îòõîäà, ðàçðåøèòü åùå ðàç íà÷àòü îòõîä
-- (ñëó÷àé, êîãäà èãðîê ïîäîøåë ñëèøêîì áëèçêî ê àññèñòåíòó)
--self.dir_approaching = true
new_state = "threat"
target = {look_object = get_story_object("actor")}
else
local t = time_global()
if t >= self.keep_state_until then
self.keep_state_until = t + keep_state_min_time
--printf("_bp: move_mgr: distance to destination waypoint: %d", dist_to_assist_pt)
if dist_to_assist_pt <= dist_walk then
new_state = "raid"
target = {look_object = get_story_object("actor")}
elseif dist_to_assist_pt <= dist_run then
new_state = "rush"
else
new_state = "assault"
end
end
end
if new_state and new_state ~= self.last_state then
state_mgr.set_state(self.object, new_state,
nil, nil, target, { animation = true })
self.last_state = new_state
end
-- 4. Åñëè ñòîèì íà òî÷êå - ïåòü ïåñåíêè è ïðî÷óþ õåðíþ
-- xr_sound.set_sound(self.object, sound_wait)
end
function action_companion_activity:beh_wait_simple()
local actor = db.actor
local new_state = "threat"
if new_state ~= self.last_state then
state_mgr.set_state(self.object, new_state,
nil, nil, {look_object = get_story_object("actor")}, { animation = true })
self.last_state = new_state
end
-- 4. Åñëè ñòîèì íà òî÷êå - ïåòü ïåñåíêè è ïðî÷óþ õåðíþ
-- xr_sound.set_sound(self.object, sound_wait)
end
function action_companion_activity:execute()
action_base.execute(self)
if self.st.behavior == beh_walk_simple then
self:beh_walk_simple()
elseif self.st.behavior == beh_wait_simple then
self:beh_wait_simple()
end
end
function action_companion_activity:finalize()
action_base.finalize(self)
end
--' Âûáîð íîâîé ïîçèöèè
function select_position(npc, st)
local node_1_vertex_id = nil
local node_1_distance = nil
local node_2_vertex_id = nil
local node_2_distance = nil
local actor = db.actor
-- ïðîâåðÿåì òî÷êó ñëåâà îò àêòåðà
desired_direction = vector_rotate_y(actor:direction(), math.random(50,60) )
node_1_vertex_id = level.vertex_in_direction(actor:level_vertex_id(),
desired_direction,
desired_distance)
if npc:accessible(node_1_vertex_id) ~= true or node_1_vertex_id == actor:level_vertex_id() then
node_1_vertex_id = nil
end
-- ïðîâåðÿåì òî÷êó ñïðàâà îò àêòåðà
desired_direction = vector_rotate_y(actor:direction(), -math.random(50,60) )
node_2_vertex_id = level.vertex_in_direction ( actor:level_vertex_id(),
desired_direction,
desired_distance )
if npc:accessible(node_2_vertex_id) ~= true or node_2_vertex_id == actor:level_vertex_id() then
node_2_vertex_id = nil
end
-- Ïðîâåðÿåì ðàñòîÿíèÿ äî òî÷åê
if node_1_vertex_id ~= nil then
node_1_distance = npc:position():distance_to_sqr(level.vertex_position(node_1_vertex_id))
else
node_1_distance = -1
end
if node_2_vertex_id ~= nil then
node_2_distance = npc:position():distance_to_sqr(level.vertex_position(node_2_vertex_id))
else
node_2_distance = -1
end
-- Âûáèðàåì áëèæàéøóþ èç ñóùåñòâóþùèõ
if node_1_distance == -1 and node_2_distance == -1 then
--' Íå ñìîãëè íàéòè òî÷êó, ðóãàòüñÿ
return nil
end
if node_1_distance == -1 then
return node_2_vertex_id
end
if node_2_distance == -1 then
return node_1_vertex_id
end
if node_1_distance < node_2_distance then
return node_1_vertex_id
else
return node_2_vertex_id
end
end
function vector_rotate_y (v, angle)
angle = angle * 0.017453292519943295769236907684886
local c = math.cos (angle)
local s = math.sin (angle)
return vector ():set (v.x * c - v.z * s, v.y, v.x * s + v.z * c)
end
----------------------------------------------------------------------------------------------------------------------
function add_to_binder(npc, ini, scheme, section, storage)
--printf("_bp: add_to_binder (companion)")
local operators = {}
local properties = {}
local manager = npc:motivation_action_manager()
properties["need_companion"] = xr_evaluators_id.zmey_companion_base + 1
properties["state_mgr_logic_active"] = xr_evaluators_id.state_mgr + 4
operators["action_companion"] = xr_actions_id.zmey_companion_base + 1
-- evaluators
manager:add_evaluator (properties["need_companion"], this.evaluator_need_companion (storage, "companion_need_companion"))
local new_action = this.action_companion_activity(npc, "action_companion_activity", storage)
new_action:add_precondition (world_property(stalker_ids.property_alive, true))
new_action:add_precondition (world_property(stalker_ids.property_enemy, false))
new_action:add_precondition (world_property(properties["need_companion"], true))
xr_motivator.addCommonPrecondition(new_action)
new_action:add_effect(world_property(properties["need_companion"], false))
new_action:add_effect (world_property(properties["state_mgr_logic_active"], false))
manager:add_action(operators["action_companion"], new_action)
-- Çàðåãèñòðèðîâàòü âñå actions, â êîòîðûõ äîëæåí áûòü âûçâàí ìåòîä reset_scheme ïðè èçìåíåíèè íàñòðîåê ñõåìû:
xr_logic.subscribe_action_for_events(npc, storage, new_action)
new_action = manager:action(xr_actions_id.alife)
new_action:add_precondition(world_property(properties["need_companion"], false))
end
function set_scheme(object, ini, scheme, section)
local st = xr_logic.assign_storage_and_bind(object, ini, scheme, section)
st.logic = xr_logic.cfg_get_switch_conditions(ini, section, npc)
st.behavior = beh_walk_simple
end