This repository has been archived by the owner on Dec 28, 2023. It is now read-only.
/
ogse_bind_light.script
527 lines (491 loc) · 18.8 KB
/
ogse_bind_light.script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
-- -*- mode: lua; coding: windows-1251-dos -*-
-------------------------------------------------------------------------------
--| ogse_bind_light.script |--
--| Áèíäåð èñòî÷íèêîâ ñâåòà: ëàìïû è ïðîæåêòîðà |--
--| Íóæåí äëÿ ïîääåðæêè ðàáîòû ïî ðàñïèñàíèþ, |--
--| òàêæå ïîääåðæèâàåò âñòðîåííóþ èãðîâóþ ëîãèêó |--
--| Malandrinus, OGS Evolution Team, 2012 |--
-------------------------------------------------------------------------------
local def_light_ini = dsh.cached_ini_file( "scripts\\lights\\def_light.ltx" )
local def_light_off_ini = dsh.cached_ini_file(
"scripts\\lights\\def_light_off.ltx"
)
local def_street_light_ini = dsh.cached_ini_file(
"scripts\\lights\\def_street_light.ltx"
)
local ignore_visuals = {
[ "equipments\\projector.ogf" ] = true,
[ "physics\\decor\\tv_1" ] = true,
}
local ini_by_name = {
[ "atp_for_test22" ] = {
[ "_lights_hanging_lamp_0011" ] = def_street_light_ini,
[ "_lights_hanging_lamp_0014" ] = def_street_light_ini,
[ "_search_light" ] = "scripts\\lights\\atp_search_light.ltx",
[ "_search_light_0000" ] = "scripts\\lights\\atp_search_light2.ltx",
},
[ "l01_escape" ] = {
-- äåðåâíÿ íîâè÷êîâ
[ "light_alarm_glass_0002" ] = def_street_light_ini,
},
[ "l02_garbage" ] = {
-- ëàìïî÷êà â àíãàðå, íà êîëîííå
[ "light_wire_1_0000" ] = def_street_light_ini,
},
[ "l06_rostok" ] = {
-- ëàìïî÷êà â âàãîí÷èêå
[ "light_alarm_glass_0002" ] = def_street_light_ini,
},
[ "l07_military" ] = {
-- áàçà Ñâîáîäû íà ÀÑ
[ "light_alarm_glass_0007" ] = def_street_light_ini,
[ "light_alarm_glass_0010" ] = def_street_light_ini,
[ "light_alarm_glass_0011" ] = def_street_light_ini,
[ "light_alarm_glass_0012" ] = def_street_light_ini,
[ "light_alarm_glass_0013" ] = def_street_light_ini,
[ "light_alarm_glass_0015" ] = def_street_light_ini,
-- áàçà Ñâîáîäû íà ÀÑ
[ "mil_search_light" ] = "scripts\\lights\\mil_search_light.ltx",
},
[ "l05_bar" ] = {
[ "light_wire_1_0004" ] = def_street_light_ini,
},
[ "l04_darkvalley" ] = {
[ "_lights_hanging_lamp_0000" ] = def_street_light_ini,
[ "_lights_hanging_lamp_0001" ] = def_street_light_ini,
[ "_lights_hanging_lamp_0010" ] = def_street_light_ini,
-- ñòðàííàÿ ëàìïî÷êà â òóàëåòå
[ "_lights_hanging_lamp_0014" ] = def_light_off_ini,
},
[ "l10_radar" ] = {
[ "light_wire_1_0000" ] = def_street_light_ini,
},
[ "l03_agroprom" ] = {
[ "light_galogen_1_0018" ] = def_street_light_ini,
},
[ "red_forest" ] = {
-- ëàìïî÷êà íà ïóëüòå óïðàâëåíèÿ ìîñòîì
[ "light_alarm_glass_0001" ] = def_street_light_ini,
},
}
function init( obj )
local ini = obj:spawn_ini()
local dsh_sect = "ogse_bind_light." .. level.name()
if sys_ini:section_exist( dsh_sect ) then
local cfg = get_string( dsh_sect, obj:name() )
if cfg then
ini = dsh.cached_ini_file( cfg )
end
end
if ini_by_name[ level.name() ] then
local cfg = ini_by_name[ level.name() ][ obj:name() ]
if cfg then
if type( cfg ) == "string" then
ini = dsh.cached_ini_file( cfg )
else
ini = cfg
end
end
end
if not ini then
if obj:clsid() == clsid.projector then
bind_physic_object.init( obj )
else
ogse_campfire.bind_unknown_light( obj )
end
return
end
local has_light_config = ini:section_exist( "light_config" )
local has_logic = ini:section_exist( "logic" )
local has_fade_params = ini:section_exist( "fade_params" )
if not ( has_light_config or has_logic or has_fade_params ) then
if not ignore_visuals[ obj:get_visual_name() ] then
ini = def_light_ini
has_light_config = ini:section_exist( "light_config" )
has_logic = ini:section_exist( "logic" )
has_fade_params = ini:section_exist( "fade_params" )
end
end
if has_light_config or has_logic or has_fade_params then
local new_binder = light_binder(
obj, has_light_config, has_logic, has_fade_params, ini
)
obj:bind_object( new_binder )
else
bind_physic_object.init( obj )
end
end
class "light_binder" ( object_binder )
function light_binder:__init( obj, has_light_config, has_logic, has_fade_params, ini ) super( obj )
self.initialized = false
self.loaded = false
self.has_light_config = has_light_config
self.has_logic = has_logic
self.has_fade_params = has_fade_params
self.spawn_ini = ini
end
function light_binder:reinit()
self.object:set_callback( callback.hit, self.hit_callback, self )
self.object:set_callback( callback.use_object, self.use_callback, self )
db.storage[ self.object:id() ] = {}
self.st = db.storage[ self.object:id() ]
end
-- MARKER: update
function light_binder:update( delta )
if not self.initialized and db.actor then
self.initialized = true
xr_logic.initialize_obj(
self.object, self.st, self.loaded, db.actor, modules.stype_item
)
-- âûïîëíÿåì èíèöèàëèçàöèþ ëàìïû ïðè óñëîâèè íàëè÷èÿ êîíôèãà ëàìïû
if self.has_light_config then
-- ÷èòàåì ñîõðàí¸ííîå ñîñòîÿíèå
self.is_on = xr_logic.pstor_retrieve( self.object, "is_on", true )
self.is_broken = xr_logic.pstor_retrieve( self.object, "is_broken", false )
local str_rt = xr_logic.pstor_retrieve(
self.object, "repair_time", "0,0,0,0,0,0,0"
) -- "y,m,d,h,min,s,ms"
self.repair_time = unpack_time_from_string( str_rt )
if game.get_game_time() > self.repair_time then
self.is_broken = false
end
-- èíèöèàëèçèðóåì îñòàëüíîå
local light_config_fname = get_string(
"light_config", "cfg", nil, self.spawn_ini
)
ASSERT(
light_config_fname,
"[light_binder:update] parameter 'cfg' in 'light_config' section in object '%s' is not defined",
self.object:name()
)
self.light_config = dsh.cached_ini_file( light_config_fname )
self.st.light_config = {}
ASSERT(
self.light_config,
"[light_binder:update] can not create config file '%s'",
light_config_fname
)
-- åñëè ïðèñóòñòâóåò ñîáñòâåííîå ðàñïèñàíèå
if self.light_config:line_exist( "light_config", "schedule" ) then
-- åñëè åñòü ðàñïèñàíèå, òî íóæåí óíèêàëüíûé ñèãíàë. Â ýòîì
-- ñëó÷àå ïàðàìåòð "signal_name" áóäåò ïðîèãíîðèðîâàí.
self.signal_name = self.object:name()
self.has_own_schedule = true
-- òî äîáàâëÿåì åãî â ìåíåäæåð îñâåùåíèÿ
local switch_times = get_numbers(
"light_config", "schedule", self.light_config
)
ogse_lights_mgr.add_light_schedule( self.signal_name, switch_times )
else
-- åñëè ñâîåãî ðàñïèñàíèÿ íåò, òî âîçìîæíî åñòü âíåøíåå
-- ðàñïèñàíèå
self.signal_name = get_string(
"light_config", "signal_name", nil, self.light_config
)
if self.signal_name == "auto" then
-- çàïðîñ íà ãåíåðàöèþ óíèêàëüíîãî èìåíè ñèãíàëà
self.signal_name = self.object:name()
end
end
self.st.light_config.signal_name = self.signal_name
if self.signal_name then -- åñëè åñòü ñèãíàë (ñâîé èëè âíåøíèé, íåâàæíî)
-- òî ïîäïèñûâàåìñÿ íà íåãî
self.switch_signal_desc = {
signal = self.signal_name, self = self, fun = self.on_switch
}
-- ðåãèñòðèðóåì ñåáÿ â ìåíåäæåðå ñèãíàëîâ
ogse_signals.get_mgr():subscribe( self.switch_signal_desc )
end
self.particle_name = get_string(
"light_config", "particle", nil, self.light_config
)
if self.particle_name then
self.particle_offset = get_vector(
"light_config", "particle_offset", vector():set( 0, 0, 0 ),
self.light_config
)
self.particle_bone = get_string(
"light_config", "particle_bone", nil, self.light_config
)
end
-- åñëè åñòü ñèãíàë îò ðàñïèñàíèÿ
if
self.signal_name
and ogse_lights_mgr.schedule_for_signal_exist( self.signal_name )
then
-- çàïðàøèâàåì ñèãíàë èíèöèàëèçàöèè
ogse_lights_mgr.request_last_signal( self.signal_name )
else
self:on_switch( self.is_on )
end
self.repair_interval = get_float(
"light_config", "repair_time", 3.0, self.light_config
)
if self.object:is_projector() then
if self.light_config:line_exist( "light_config", "angles" ) then
local yaw, pitch = get_vector2(
"light_config", "angles", nil, self.light_config
)
ASSERT(
yaw and pitch,
"[light_binder:update] cannot read parameter 'angles' from section 'light_config' in file '%s' for object '%s'",
light_config_fname, self.object:name()
)
self.angles = { yaw, pitch }
elseif self.light_config:line_exist( "light_config", "aimpoint" ) then
self.aimpoint = get_vector(
"light_config", "aimpoint", nil, self.light_config
)
ASSERT(
self.aimpoint,
"[light_binder:update] cannot read parameter 'aimpoint' from section 'light_config' in file '%s' for object '%s'",
light_config_fname, self.object:name()
)
elseif self.light_config:line_exist( "light_config", "aim_path" ) then
local path_section = get_string(
"light_config", "aim_path", nil, self.light_config
)
ASSERT(
self.light_config:section_exist( path_section ),
"[light_binder:update] no aim_path section '%' in the file '%'",
path_section, light_config_fname
)
self.aim_path = {}
self.current_point = 1
local default_aim_pause = get_float(
"light_config", "default_aim_pause", 2.0, self.light_config
)
for line = 0, self.light_config:line_count( path_section ) - 1 do
local res,key,sline = self.light_config:r_line( path_section, line )
local ap_data = parse_nums( sline )
ASSERT(
#ap_data == 3 or #ap_data == 4,
"[light_binder:update] line %d of aim path '%s' in the file '%s' has incorrect format",
line, path_section, light_config_fname
)
local aim_point_item = {
p = { ap_data[ 1 ], ap_data[ 2 ], ap_data[ 3 ] },
t = ap_data[ 4 ] or default_aim_pause,
}
table.insert( self.aim_path, aim_point_item )
end
end
end
end -- if self.has_light_config
if self.has_fade_params then
local lamp = self.object:get_hanging_lamp()
if lamp then
local ini = self.spawn_ini
local lsf_speed = ini:r_float( "fade_params", "speed" );
local lsf_amount = ini:r_float( "fade_params", "amount" );
local lsf_jitter = ini:r_float( "fade_params", "smap_jitter" );
lamp:set_lsf_params( lsf_speed, lsf_amount, lsf_jitter )
local cond = get_string( "fade_params", "cond", nil, ini );
if
cond
and xr_logic.pick_section_from_condlist(
db.actor, self.object,
xr_logic.parse_condlist( self.object, "fade_params", "cond", cond )
) == nil
then
-- log2(
-- "[%s]: turn off %s by cond: %s",
-- script_name(), self.object:name(), cond
-- )
lamp:turn_off()
end
end
end
if self.object:is_projector() then
self:set_projector_angles(
xr_logic.pstor_retrieve( self.object, "current_yaw", 0.0 ),
xr_logic.pstor_retrieve( self.object, "current_pitch", 0.0 ),
xr_logic.pstor_retrieve( self.object, "target_yaw", 0.0 ),
xr_logic.pstor_retrieve( self.object, "target_pitch", 0.0 )
)
end
-- íàïðàâëÿåì ïðîæåêòîð
if self.angles then
self:set_projector_angles(
self.angles[ 1 ], self.angles[ 2 ], self.angles[ 1 ], self.angles[ 2 ] )
elseif self.aimpoint then
local lk = look( self.aimpoint, 0.5, 0.5 )
local act = entity_action()
act:set_action( lk )
self.object:script( true, "test" )
self.object:reset_action_queue()
self.object:command( act, true )
end
end -- if not self.initialized
if self.is_on and self.aim_path then
local new_aim_point
local dir = self.object:get_current_direction()
if not self.last_dir then -- åù¸ íå çàäàâàëè íàïðàâëåíèå
self.current_point = 1
new_aim_point = self.aim_path[ self.current_point ].p
else
if self.pause_start_time then -- ðåæèì ïàóçû
local tdif = ( time_global() - self.pause_start_time ) / 1.0e3
if tdif > self.aim_path[ self.current_point ].t then -- ïàóçà çàêîí÷èëàñü
self.pause_start_time = nil -- ñíèìàåì ïàóçó
-- íàçíà÷àåì ñëåäóþùóþ öåëåâóþ òî÷êó
self.current_point = self.current_point + 1
if self.current_point > #self.aim_path then
self.current_point = 1
end
new_aim_point = self.aim_path[ self.current_point ].p
end
else -- ðåæèì âðàùåíèÿ
local dif = vector():sub( dir, self.last_dir ):magnitude()
if dif < 0.001 then -- íàïðàâëåíèå óñòàêàíèëîñü
self.pause_start_time = time_global() -- íà÷èíàåì ïàóçó
end
end
end
self.last_dir = dir
if new_aim_point then
local lk = look( vector():set( unpack( new_aim_point ) ), 0.5, 0.5 )
local act = entity_action()
act:set_action( lk )
self.object:script( true, "test" )
self.object:reset_action_queue()
self.object:command( act, true )
end
end
-- îáíîâëÿåì ïîëîæåíèå ïàðòèêëà
if self.is_on and self.particle then
-- Çàïóñêàåì àññîöèèðîâàííûé ñ îáúåêòîì ïàðòèêë.
local pos = self:particle_position()
self.particle:move_to( pos, pos )
end
if self.st.active_section then
xr_logic.issue_event(
self.object, self.st[ self.st.active_scheme ], "update", delta
)
end
end
function light_binder:set_projector_angles( current_yaw, current_pitch, target_yaw, target_pitch )
ASSERT(
self.object:is_projector(),
"[%s]: not a projector", script_name(), self.object:name()
)
local obj = self.object:get_projector()
obj.current_yaw = current_yaw
obj.current_pitch = current_pitch
obj.target_yaw = target_yaw
obj.target_pitch = target_pitch
end
function light_binder:particle_position()
local pos
if self.particle_bone then
pos = self.object:bone_position( self.particle_bone )
else
pos = self.object:position()
end
return pos:add( self.particle_offset )
end
function light_binder:on_switch( turn_on )
self.is_on = ( turn_on == true )
local lamp = self.object:get_hanging_lamp()
if lamp then
if self.is_on and not self.is_broken then
lamp:turn_on()
else
lamp:turn_off()
end
elseif self.object:is_projector() then
if self.is_on and not self.is_broken then
self.object:projector_switch( true )
self.object:set_bone_visible( "bone_light", 1 )
self.object:set_bone_visible( "bone_cone", 1 )
else
self.object:projector_switch( false )
-- KRodin: â ñòàðîì äâèæêå òóò êîñòè íå ñêðûâàëèñü, è ïðîáëåì íå
-- áûëî. Ñåé÷àñ æå, åñëè çäåñü êîñòè íå ñêðûâàòü, îíè áóäóò
-- âèäèìû ïîñòîÿííî.
self.object:set_bone_visible( "bone_light", 0 )
self.object:set_bone_visible( "bone_cone", 0 )
end
end
if self.particle_name then
if self.is_on then
if self.particle and self.particle:playing() then return end
-- Çàïóñêàåì àññîöèèðîâàííûé ñ îáúåêòîì ïàðòèêë.
self.particle = particles_object( self.particle_name )
self.particle:play_at_pos( self:particle_position() )
else
if self.particle then
self.particle:stop()
self.particle = nil
end
end
end
end
function light_binder:net_destroy()
db.storage[ self.object:id() ] = nil
if self.particle ~= nil then
self.particle:stop()
end
if self.switch_signal_desc then -- åñëè áûëè ïîäïèñàíû íà ñèãíàë
-- îòïèñàòüñÿ îò ñèãíàëà
ogse_signals.get_mgr():unsubscribe( self.switch_signal_desc )
self.switch_signal_desc = nil
end
if self.has_own_schedule then -- åñëè ðàáîòàë ïî ñîáñòâåííîìó ðàñïèñàíèþ
-- òî óäàëèòü ýòî ðàñïèñàíèå èç ìåíåäæåðà îñâåùåíèÿ
ogse_lights_mgr.remove_light_schedule( self.signal_name )
end
end
function light_binder:net_save_relevant()
return true
end
function light_binder:save( packet )
-- ñîõðàíÿåì ñîñòîÿíèå, åñëè åñòü ÷òî ñîõðàíÿòü
if self.has_light_config and self.initialized then
xr_logic.pstor_store( self.object, "is_on", self.is_on )
xr_logic.pstor_store( self.object, "is_broken", self.is_broken )
if self.repair_time then
local str_rt = pack_time_to_string( self.repair_time )
xr_logic.pstor_store( self.object, "repair_time", str_rt )
end
end
if self.object:is_projector() then
local obj = self.object:get_projector()
xr_logic.pstor_store( self.object, "current_yaw", obj.current_yaw )
xr_logic.pstor_store( self.object, "current_pitch", obj.current_pitch )
xr_logic.pstor_store( self.object, "target_yaw", obj.target_yaw )
xr_logic.pstor_store( self.object, "target_pitch", obj.target_pitch )
end
xr_logic.save_obj( self.object, packet, not self.initialized )
end
function light_binder:load( reader )
self.loaded = true
xr_logic.load_obj( self.object, reader )
end
function light_binder:use_callback( obj, who )
if self.st.active_section then
xr_logic.issue_event(
self.object, self.st[ self.st.active_scheme ], "use_callback", obj, who
)
end
end
function light_binder:hit_callback( obj, amount, local_direction, who, bone_index )
if self.st.active_section then
xr_logic.issue_event(
self.object, self.st[ self.st.active_scheme ], "hit_callback", obj,
amount, local_direction, who, bone_index
)
end
if self.has_light_config then
if self.is_broken then
return
end
xr_logic.pstor_store( self.object, "is_broken", true )
self.is_broken = true
self:on_switch( false )
local s, ms = math.modf( self.repair_interval * 3600.0 )
local t = game.CTime()
t:setHMSms( 0, 0, s, ms * 1000 )
self.repair_time = game.CTime( game.get_game_time() + t )
end
end