Skip to content

Commit e3605fc

Browse files
committed
Further refactoring + optimizations
1 parent 9aee60a commit e3605fc

14 files changed

+275
-444
lines changed

gamedata/configs/misc/script_sound.ltx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ idle = 1,1
630630
type = actor
631631
path = vehicles\helicopter\hit_
632632
shuffle = rnd
633-
idle = 3,4
633+
idle = 4,8 ; SRP: increased from 3,4 -> 4,8 to obsolete the timer in heli_binder:on_hit()
634634

635635
[alarm]
636636
type = 3d

gamedata/scripts/actor_reaction.script

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ function action_actor_reaction:execute()
3333
db.actor:run_talk_dialog(npc, true)
3434
end
3535
self.comed = true
36-
else
37-
if self.scene_manager.stand_still then
38-
npc:set_dest_level_vertex_id(db.actor:level_vertex_id())
39-
state_mgr.set_state(npc, "raid", nil, nil, {look_position = actor_pos}, {animation = true}) -- vanilla uses {look_object = db.actor}, sometimes causes jerky NPC head movement
40-
end
36+
elseif self.scene_manager.actor_enemy then
37+
state_mgr.set_state(npc, "threat_fire", nil, nil, {look_position = actor_pos})
38+
elseif self.scene_manager.stand_still then
39+
npc:set_dest_level_vertex_id(db.actor:level_vertex_id())
40+
state_mgr.set_state(npc, "raid", nil, nil, {look_position = actor_pos}, {animation = true}) -- vanilla uses {look_object = db.actor}, sometimes causes jerky NPC head movement
4141
end
4242
end
4343
end

gamedata/scripts/bind_heli.script

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ function heli_binder:__init(obj) super(obj) -- called 2nd
1212
self.heli_fire = heli_fire.get_heli_firer(obj)
1313
self.heliObject = obj:get_helicopter()
1414
self.st = {combat = heli_combat.heli_combat(obj, self.heliObject)}
15-
self.last_hit_snd_timeout = 0
1615
self.flame_start_health = utils.cfg_get_number(system_ini(), "helicopter", "flame_start_health", obj, true)
1716
end
1817

@@ -112,12 +111,7 @@ function heli_binder:on_hit(power, impulse, hit_type, enemy_id)
112111
end
113112
end
114113

115-
local global_time = time_global()
116-
117-
if self.last_hit_snd_timeout < global_time then
118-
xr_sound.set_sound_play(self.object:id(), "heli_hit")
119-
self.last_hit_snd_timeout = global_time + math.random(4000, 8000)
120-
end
114+
xr_sound.set_sound_play(self.object:id(), "heli_hit")
121115
end
122116

123117
function heli_binder:on_point(distance, position, path_idx)

gamedata/scripts/pda.script

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ function dialog_wnd_showed()
201201
if npc_st.meet and npc_st.robbery_zone_id == nil then
202202
db.actor:allow_break_talk_dialog(npc_st.meet.allow_break)
203203
end
204-
local sound_theme = xr_sound.sound_table[npc_id]
205-
if sound_theme and sound_theme.reset then
206-
sound_theme:reset(npc_id)
204+
local sound = xr_sound.sound_table[npc_id]
205+
if sound then
206+
sound:reset(npc_id)
207207
end
208208
break
209209
end

gamedata/scripts/sim_board.script

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,6 @@ function sim_board:__init()
195195
self.squads = {}
196196
self.id_generator = object_collection.object_collection()
197197
self.spawn_smarts = {}
198-
-- self.tmp_assigned_squad = {}
199-
-- self.tmp_entered_squad = {}
200198
end
201199

202200
function sim_board:start_sim()
@@ -262,27 +260,7 @@ function sim_board:register_smart(smart)
262260
self.spawn_smarts[smart_id] = smart.respawn_sector
263261
end
264262
end
265-
--[[
266-
function sim_board:init_smart(smart) -- called (only) from smart_terrain.on_register()
267-
local smart_id = smart.id
268-
local temp_squad_table = self.tmp_assigned_squad[smart_id]
269-
if temp_squad_table ~= nil then
270-
for _, squad in pairs (temp_squad_table) do
271-
-- dbglog("Delay-assigning %s squad with ID '%s' to smart '%s'", game.translate_string(squad.player_id), tostring(squad.squad_id), smart_names.get_smart_terrain_name(smart))
272-
self:assign_squad_to_smart(squad, smart_id) -- delay-assigns squads from early instances of assign_squad_to_smart(squad, smart) where self.smarts[smart.id] was still nil
273-
end
274-
self.tmp_assigned_squad[smart_id] = nil
275-
end
276-
temp_squad_table = self.tmp_entered_squad[smart_id]
277-
if temp_squad_table ~= nil then
278-
for _, squad in pairs (temp_squad_table) do
279-
-- dbglog("Delay-entering %s squad with ID '%s' to smart '%s'", game.translate_string(squad.player_id), tostring(squad.squad_id), smart_names.get_smart_terrain_name(smart))
280-
self:enter_smart(squad, smart_id) -- delay-enters squads from early instances of enter_smart(squad, smart_id) where self.smarts[smart_id] was still nil
281-
end
282-
self.tmp_entered_squad[smart_id] = nil
283-
end
284-
end
285-
]]
263+
286264
function sim_board:unregister_smart(smart)
287265
if self.smarts[smart.id] == nil then
288266
abort("[sim_board:unregister_smart]: Tried to unregister a smart not registered: '%s'.", smart:name())
@@ -413,14 +391,6 @@ function sim_board:assign_squad_to_smart(squad, new_smart_id) -- first unassigns
413391
local new_smart_info = self.smarts[new_smart_id]
414392

415393
if new_smart_info == nil then
416-
--[[
417-
local tmp_assigned_tbl = self.tmp_assigned_squad
418-
if tmp_assigned_tbl[new_smart_id] == nil then
419-
tmp_assigned_tbl[new_smart_id] = {}
420-
end
421-
table.insert(tmp_assigned_tbl[new_smart_id], squad)
422-
return
423-
]]
424394
abort("[sim_board:assign_squad_to_smart]: Tried to assign squad with ID '%s' to unregistered smart with ID '%s'.", tostring(squad.squad_id), tostring(new_smart_id))
425395
end
426396

@@ -518,14 +488,6 @@ function sim_board:enter_smart(squad, smart_id, after_load)
518488
local smart_info = self.smarts[smart_id]
519489

520490
if smart_info == nil then
521-
--[[
522-
local tmp_entered_tbl = self.tmp_entered_squad
523-
if tmp_entered_tbl[smart_id] == nil then
524-
tmp_entered_tbl[smart_id] = {}
525-
end
526-
table.insert(tmp_entered_tbl[smart_id], squad)
527-
return
528-
]]
529491
abort("[sim_board:enter_smart]: Squad with ID '%s' tried to enter unregistered smart with ID '%s'.", tostring(squad.squad_id), tostring(smart_id))
530492
end
531493

gamedata/scripts/smart_terrain.script

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ local table_sort = table.sort
1111
class "se_smart_terrain" (cse_alife_smart_zone)
1212

1313
function se_smart_terrain:__init(section) super(section)
14-
-- self.b_registred = false
1514
self.need_init_npc = false
16-
-- self.npc_to_register = {}
1715
self.npc_by_job_section = {}
1816
self.dead_time = {}
1917
self.npc_info = {}
@@ -70,18 +68,12 @@ function se_smart_terrain:on_register()
7068
end
7169
end
7270

73-
-- self.b_registred = true
74-
7571
self:load_jobs()
7672

77-
-- self.board:init_smart(self)
78-
7973
if self.need_init_npc then
8074
self.need_init_npc = false
8175
self:init_npc_after_load()
8276
end
83-
84-
-- self:register_delayed_npc()
8577
--[[
8678
Every faction's squads should be initialized before any faction's targets,
8779
else a squad's relative power may be wrong after load (due to enemy squads
@@ -183,12 +175,7 @@ local function fill_npc_info(se_npc)
183175
end
184176

185177
function se_smart_terrain:register_npc(se_npc)
186-
--[[
187-
if self.b_registred == false then
188-
self.npc_to_register[#self.npc_to_register + 1] = se_npc
189-
return
190-
end
191-
]]
178+
192179
self.npc_info[se_npc.id] = fill_npc_info(se_npc)
193180

194181
se_npc.m_smart_terrain_id = self.id
@@ -197,14 +184,7 @@ function se_smart_terrain:register_npc(se_npc)
197184

198185
self:update_jobs(se_npc)
199186
end
200-
--[[
201-
function se_smart_terrain:register_delayed_npc()
202-
for i = 1, #self.npc_to_register do
203-
self:register_npc(self.npc_to_register[i])
204-
end
205-
self.npc_to_register = {}
206-
end
207-
]]
187+
208188
function se_smart_terrain:unregister_npc(se_npc)
209189

210190
local npc_info = self.npc_info[se_npc.id]

gamedata/scripts/sound_theme.script

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ end
3535

3636
function npc_sound:reset(npc_id)
3737
self.stop_time = nil
38+
self.idle_time = nil
3839
self.played_id = nil
3940
self.can_play_group_sound = true
4041
self.can_play_sound[npc_id] = true
@@ -43,10 +44,7 @@ function npc_sound:reset(npc_id)
4344
npc:set_sound_mask(-1)
4445
npc:set_sound_mask(0)
4546
end
46-
if self.pda_snd_obj ~= nil then
47-
self.pda_snd_obj:stop()
48-
self.pda_snd_obj = nil
49-
end
47+
self:stop()
5048
end
5149

5250
function npc_sound:is_playing(npc_id)
@@ -260,7 +258,7 @@ function npc_sound:select_next_sound(npc_data) -- TODO: Replace with local func
260258
end
261259

262260
function npc_sound:stop()
263-
if self.pda_snd_obj ~= nil and self.pda_snd_obj:playing() then
261+
if self.pda_snd_obj ~= nil then
264262
self.pda_snd_obj:stop()
265263
self.pda_snd_obj = nil
266264
end
@@ -380,6 +378,14 @@ function actor_sound:__init(ini, section)
380378
end
381379
end
382380

381+
function actor_sound:reset()
382+
self.stop_time = nil
383+
self.idle_time = nil
384+
self.played_id = nil
385+
self.can_play_sound = true
386+
self:stop()
387+
end
388+
383389
function actor_sound:callback(obj_id) -- executed on first obj binder update() after sound stops playing
384390

385391
self.stop_time = time_global()
@@ -443,6 +449,7 @@ end
443449
function actor_sound:stop()
444450
if self.snd_obj ~= nil then
445451
self.snd_obj:stop()
452+
self.snd_obj = nil
446453
end
447454
end
448455

@@ -466,6 +473,7 @@ function object_sound:__init(ini, section)
466473

467474
self.class_id = "object_sound"
468475
self.shuffle = utils.cfg_get_string(ini, section, "shuffle", nil, false, "", "rnd")
476+
self.play_always = utils.cfg_get_bool(ini, section, "play_always", nil, false, false)
469477
self.section = section
470478
self.played_id = nil
471479
self.stop_time = nil
@@ -502,11 +510,20 @@ function object_sound:__init(ini, section)
502510
end
503511
end
504512

513+
function object_sound:reset()
514+
self.stop_time = nil
515+
self.idle_time = nil
516+
self.played_id = nil
517+
self.can_play_sound = true
518+
self:stop()
519+
end
520+
505521
function object_sound:callback(obj_id) -- executed on first obj binder update() after sound stops playing
506522

507523
self.stop_time = time_global()
508524
self.idle_time = math.random(self.min_idle, self.max_idle) * 1000
509525
self.snd_obj = nil
526+
self.pda_snd_obj = nil
510527
self.can_play_sound = true
511528

512529
local obj_st = db.storage[obj_id]
@@ -529,6 +546,7 @@ end
529546

530547
function object_sound:is_playing()
531548
return self.snd_obj ~= nil and self.snd_obj:playing()
549+
or self.pda_snd_obj ~= nil and self.pda_snd_obj:playing()
532550
end
533551

534552
function object_sound:play(obj_id, faction, point)
@@ -576,8 +594,9 @@ end
576594
function object_sound:stop()
577595
if self.snd_obj ~= nil then
578596
self.snd_obj:stop()
597+
self.snd_obj = nil
579598
end
580-
if self.pda_snd_obj ~= nil and self.pda_snd_obj:playing() then
599+
if self.pda_snd_obj ~= nil then
581600
self.pda_snd_obj:stop()
582601
self.pda_snd_obj = nil
583602
end
@@ -611,18 +630,17 @@ function looped_sound:__init(ini, section)
611630
end
612631
end
613632

614-
function looped_sound:is_playing()
615-
return self.snd_obj ~= nil and self.snd_obj:playing()
616-
end
617-
618633
function looped_sound:stop()
619634
if self.snd_obj ~= nil then
620635
self.snd_obj:stop()
636+
self.snd_obj = nil
621637
end
622638
end
623639

624640
function looped_sound:set_volume(level)
625-
self.snd_obj.volume = level
641+
if self.snd_obj ~= nil then
642+
self.snd_obj.volume = level
643+
end
626644
end
627645

628646
function looped_sound:play(obj_id)

gamedata/scripts/sr_bloodsucker.script

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ function bloodsucker_attack:update(delta)
125125
self.snd_obj:stop()
126126
end
127127
else
128-
self.snd_obj = xr_sound.set_sound_play(restr_id, st.snd_obj)
128+
local active_snd = xr_sound.set_sound_play(restr_id, st.snd_obj)
129+
self.snd_obj = active_snd and active_snd.snd_obj
129130
if self.snd_obj and self.snd_obj:playing() then
130131
self.snd_obj:set_position(self.current)
131132
end

gamedata/scripts/sr_monster.script

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ function fake_monster:update(delta)
6868
else
6969
self.current = vector():set(target_pos)
7070
end
71-
self.snd_obj = xr_sound.set_sound_play(self.object:id(), self.st.snd_obj)
71+
local active_snd = xr_sound.set_sound_play(self.object:id(), self.st.snd_obj)
72+
self.snd_obj = active_snd and active_snd.snd_obj
7273
if self.snd_obj and self.snd_obj:playing() then
7374
self.snd_obj:set_position(self.current)
7475
end

0 commit comments

Comments
 (0)