This repository has been archived by the owner on Dec 28, 2023. It is now read-only.
/
_g.script
3435 lines (3043 loc) · 110 KB
/
_g.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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- -*- mode: lua; coding: windows-1251-dos -*-
---------------------- Èíèöèàëèçàöèÿ ðàñøèðåíèé äâèæêà ------------------------
RvP()
excluded_restrictors = {
["agr_factory_skirmish_defence"] = true,
["pri_followers_wave1_out_restr"] = true,
["pri_followers_wave2_out_restr"] = true,
["pri_followers_wave3_out_restr"] = true,
["pri_followers_wave4_out_restr"] = true,
["pri_followers_wave5_out_restr"] = true,
["pri_followers_wave6_out_restr"] = true,
["pri_followers_wave7_out_restr"] = true,
["agr_nii_sr_building"] = true,
["agr_nii_3_floor"] = true,
["agr_nii_space_restrictor"] = true,
["agr_factory_skirmish"] = true,
["mil_freedom_gulag_defense_res"] = true,
["yantar_bunker_space_restrictor"] = true,
["val_lager_bandits_nedostroyka_restrictor"] = true,
["val_lager_bandits_main_side_restrictor"] = true,
["val_lager_bandits_main_building_restrictor"] = true,
["pri_monolith_kolos_restr"] = true,
["pri_monolith_base_restr"] = true,
["sar_patrol_restrictor"] = true,
["rad_tramplin_sniper_restrictor"] = true,
["bar_arena_restrictor"] = true
}
pairs_old = pairs
pairs = function(t)
if not t or type(t)~="table" then
log1("!! error, try to call 'pairs' with " .. tostring(t) .. " param")
log1(debug.traceback())
return
end
return pairs_old(t)
end
if type(SetLog) == "function" and os and os.clock() < 30 then
local name = getFS():update_path("$logs$", "_ogse.log")
SetLog(name)
log1("--SetLogCallback to file " .. name)
end
--
local dir = "$fs_root$"
local ext_path = "bin\\extensions"
local path = getFS():update_path(dir, ext_path)
package.cpath = package.cpath..";"..path.."\\?.dll"
local function IsDll(fname)
return fname:sub(-4) == ".dll"
end
if type(init_external_libs) == "function" then
init_external_libs()
end
--ëó÷øå íàâåðíîå îñòàâèòü â ãëàâíîì ìîäóëå lfs è ãðóçèòü èì.
if not lfs then
fail("check lfs namespace")
end
for file in lfs.dir(path) do
log1("file: "..file)
if file ~= "." and file ~= ".." then
if IsDll(file) then
local mod = file:sub(1, -5)
log1("--try to load " .. file)
log1("fname: "..mod)
local rez, msg = pcall(require, mod)
if (not(not rez)) then
log1("~~ loaded")
else
log1("!! error, msg : " .. tostring(msg))
end
end
end
end
if os and os.clock() < 30 then
log1("~~ _G : os.clock() = " .. tostring(os.clock()) .. " math.randomseed()")
math.randomseed(os.time())
end
local additional_libraries = {
--["luaXml"] = true
}
prefetch("luaxml")
for k,v in pairs(additional_libraries) do
if v then
log1("--try to load " .. k)
local rez, msg = pcall(require, k)
if (not(not rez)) then
log1("~~ loaded")
else
log1("!! error, msg : " .. tostring(msg))
end
end
end
set_static_rescale_factor(device().height / device().width / 0.75)
-- æóòêàÿ çàòû÷êà äëÿ ïðîáëåìû ñ áîëüøèìè öåëûìè ÷èñëàìè ïðè ñëîæåíèè â Lua
-- ðîäíîå ñëîæåíèå íà÷èíàåò îêðóãëÿòü è òåðÿòü ÷àñòü ñóììû, à ôóíêöèÿ sum_args
-- ïðè òðèâèàëüíîì èñïîëüçîâàíèè â ðÿäå ñëó÷àåâ îòñåêàåò àðãóìåíòû ñî çíà÷åíèÿ 0x80000000
-- çäåñü ñäåëàíî òàê, ÷òî ýòîò ýôôåêò íå ïðîèñõîäèò. Ôóíêöèÿ î÷åíü ìåäëåííàÿ,
-- èñïîëüçîâàòü òîëüêî â ñâÿçêå ñ ôóíêöèåé ïîëó÷åíèÿ çíà÷åíèÿ ïî àáñîëþòíîìó àäðåñó
-- è ïî âîçìîæíîñòè âîîáùå èçáåãàòü.
function sum_args_ex(base, offs)
local base_s = string.format("%08x", base)
local b1_s = base_s:sub(1,4)
--log1(b1_s)
local b2_s = base_s:sub(5,8)
--log1(b2_s)
local b1 = tonumber(b1_s, 16)
local b2 = tonumber(b2_s, 16)
local res = sum_args(bit.lshift(b1, 16), sum_args(b2, offs))
--log3("sum_args_ex: %s, %x, %x", base_s, offs, res)
return res
end
-- FIX Nazgool ------------------------------------------------------------------
-- Âîçìîæíî ïîäîáíûå ôóíêöèè óæå ãäå-òî è åñòü, íî ÿ ïîêà íå íàø¸ë.
-- Ïðîâåðêè
function isTable (obj) return type(obj) == 'table' end
function isBoolean (obj) return type(obj) == 'boolean' end
function isNumber (obj) return type(obj) == 'number' end
function isFunction(obj) return type(obj) == 'function' end
function isString (obj) return type(obj) == 'string' end
function isUserdata(obj) return type(obj) == 'userdata' end
function isThread (obj) return type(obj) == 'thread' end
function isNil (obj) return obj == nil end -- íå false, à èìåííî nil
-- Ïðîâåðêà - ïóñòà ëè òàáëèöà èëè ñòðîêà
-- Åñëè ïàðàìåòð 'space' íå nil/false, òî ñòðîêà, ñîñòîÿùàÿ òîëüêî èç ïðîáåëüíûõ ñèìâîëîâ è(èëè) íåðàçðûâíîãî ïðîáåëà áóäåò ñ÷èòàòüñÿ ïóñòîé.
function isEmpty(obj, space)
if isString(obj) then
if space then return not obj:match('[^%s\160]+') end
return #obj == 0
end
if isTable(obj) then return next(obj) == nil end
return false
end
-- ÿâëÿåòñÿ ëè òàáëèöà ìàññèâîì
function isArray(obj)
if not isTable(obj) then return false end
local n=0
for _ in pairs(obj) do n=n+1 end
return n <= #obj
end
-- Ïðèâåäåíèå çíà÷åíèÿ ïåðåìåííîé 'val' ê áóëåâîìó çíà÷åíèþ
function toBoolean(val)
return not not val
end
if (jit == nil) then
profiler.setup_hook ()
end
function compile_script( filename )
local res, msg = check_module( filename )
local module = _g[ filename ]
ASSERT(
module, "[preload_script]: Failed to load '%s'", filename
)
ASSERT(
type( module ) == "table",
"[preload_script]: Failed to load '%s', must be a table",
filename
)
return module
end
function preload_script( filename )
prefetch( filename )
return compile_script( filename )
end
-- Çàãðóæàåò ñõåìó èç ôàéëà íà äèñêå è àêòèâèðóåò â ìîòèâàòîðå.
-- Çäåñü:
-- filename - èìÿ ôàéëà, â êîòîðîì ðåàëèçîâàíà ñõåìà, áåç ðàñøèðåíèÿ
-- scheme - èìÿ ñõåìû
schemes = {} -- ñîîòâåòñòâèå ñõåì ìîäóëÿì
stypes = {} -- òèïû ñõåì
function load_scheme( filename, scheme, stype )
preload_script( filename )
schemes[ scheme ] = filename
stypes[ scheme ] = stype
end
-------======= ini reading functions by sapsan =======------
iniLines = {
["float"] = {},
["u32"] = {},
["string"] = {},
["string_wq"] = {},
["boolean"] = {},
["line"] = {}
}
iniOtherLines = {}
iniStat = {}
iniCaching = true
--iniStatistic = true
iniStatistic = false
--iniSections = {} -- åñëè ïóñòàÿ - êåøèðóåòñÿ ÂѨ!
iniSections = { -- åñëè ïóñòàÿ - êåøèðóåòñÿ ÂѨ!
["af_ameba_slug "] = true,
["af_blood"] = true,
["af_electra_sparkler"] = true,
["af_gravi"] = true,
["af_medusa"] = true,
["af_vyvert"] = true,
["art_acumm"] = true,
["ammo_11.43x23_hydro"] = true,
["ammo_12x70_buck"] = true,
["ammo_12x76_dart"] = true,
["ammo_12x76_zhekan"] = true,
["ammo_5.45x39_ap"] = true,
["ammo_5.45x39_fmj"] = true,
["ammo_5.56x45_ap"] = true,
["ammo_5.56x45_ss190"] = true,
["ammo_9x18_fmj"] = true,
["ammo_9x18_pmm"] = true,
["ammo_9x19_fmj"] = true,
["ammo_9x19_pbp"] = true,
["ammo_9x39_ap"] = true,
["ammo_9x39_pab9"] = true,
["ammo_9x39_sp5"] = true,
["ammo_m209"] = true,
["ammo_vog-25"] = true,
["ammo_vog-25p"] = true,
["antirad"] = true,
["balalaika_a"] = true,
["bandage"] = true,
["bandit_gaz_outfit_m1"] = true,
["bandit_master_outfit"] = true,
["bandit_outfit"] = true,
["beer_a"] = true,
["bolt"] = true,
["bread"] = true,
["breakable_object"] = true,
["cigara"] = true,
["climable_object"] = true,
["conserva"] = true,
["device_pda"] = true,
["device_torch"] = true,
["energy_drink"] = true,
["flaska"] = true,
--["generic_sell"] = true,
["grenade_f1"] = true,
["grenade_gd-05"] = true,
["grenade_rgd5"] = true,
["guitar_a"] = true,
["harmonica_a"] = true,
["inventory_box"] = true,
["killer_outfit"] = true,
["kolbasa"] = true,
["lights_hanging_lamp"] = true,
["logic"] = true,
["medkit"] = true,
["medkit_army"] = true,
["medkit_scientic"] = true,
["mob_home"] = true,
["monolit_exoskeleton"] = true,
["monolit_gaz_outfit_m1"] = true,
["mutant_dog_tail"] = true,
["mutant_face_tushkano"] = true,
["mutant_spleen_rat"] = true,
["ph_door@locked"] = true,
["ph_door@open"] = true,
["ph_idle@enable"] = true,
["ph_skeleton_object"] = true,
["physic_destroyable_object"] = true,
["physic_object"] = true,
--["random_task"] = true,
--["remark@val_rob_leader_call"] = true,
["respawn"] = true,
["sandwich"] = true,
["smart_terrain"] = true,
["space_restrictor"] = true,
["stalker"] = true,
["stalker_outfit"] = true,
["suvorotka"] = true,
["sweetness"] = true,
["vodka"] = true,
["oceanic815"] = true,
--["walker@val_rob_leader_start"] = true,
["wpn_abakan"] = true,
["wpn_addon_grenade_launcher"] = true,
["wpn_addon_grenade_launcher_m203"] = true,
["wpn_ak47"] = true,
["wpn_ak74 "] = true,
["wpn_ak74u"] = true,
["wpn_beretta_m1"] = true,
["wpn_bm16"] = true,
["wpn_fort"] = true,
["wpn_g36"] = true,
["wpn_groza"] = true,
["wpn_l85"] = true,
["wpn_lr300"] = true,
["wpn_m16a2_sk1"] = true,
["wpn_m4super90"] = true,
["wpn_mp5"] = true,
["wpn_pb"] = true,
["wpn_pm"] = true,
["wpn_tavor"] = true,
["wpn_sig220"] = true,
["wpn_sig550"] = true,
["wpn_spas12"] = true,
["wpn_val"] = true,
["wpn_walther"] = true,
["yad"] = true
}
iniSectionsCount = 0
for sect, val in pairs(iniSections) do
iniSectionsCount = iniSectionsCount + 1
end
-- if not iniLines then iniLines = {} end
-- if not iniOtherLines then iniOtherLines = {} end
--cfg_get_number(char_ini, section, field, object, mandatory, default_val)
--getIniValueFloat(sect, line, default, iniFileName)
--cfg_get_string(char_ini, section, field, object, mandatory, gulag_name, default_val)
--getIniValueString(sect, line, default, iniFileName)
-- getIniValueString("trader", "sell_condition", "", "misc\\trade_generic.ltx")
sys_ini = system_ini()
function getIniValueFloat(sect, line, default, iniFileName)
--get_console():execute("load ~~~ iniCaching getIniValueFloat: "..tostring(sect)..", "..tostring(line)..", "..tostring(default)..", "..tostring(iniFileName))
if sect and line then
if iniStatistic then
if iniStat[sect] then
iniStat[sect] = iniStat[sect] + 1
else
iniStat[sect] = 1
end
end
if iniCaching and (iniSections[sect] or iniSectionsCount == 0) then
if iniFileName and (not iniOtherLines[iniFileName] or not iniOtherLines[iniFileName]["float"] or not iniOtherLines[iniFileName]["float"][sect] or not iniOtherLines[iniFileName]["float"][sect][line]) or
(not iniLines["float"][sect] or not iniLines["float"][sect][line]) then
local ini, sectLines = getIniValuePrepare("float", sect, iniFileName)
if ini and ini:section_exist(sect) and ini:line_exist(sect, line) then
sectLines[line] = ini:r_float(sect, line)
else
sectLines[line] = default
end
end
-- amk.dump_table(iniLines)
if iniFileName then
return iniOtherLines[iniFileName]["float"][sect][line]
else
return iniLines["float"][sect][line]
end
else
local ini
if iniFileName then
ini = ini_file(iniFileName)
else
ini = sys_ini
end
if ini and ini:section_exist(sect) and ini:line_exist(sect, line) then
return ini:r_float(sect, line)
else
return default
end
end
else
return default
end
end
function getIniValueU32(sect, line, default, iniFileName)
--get_console():execute("load ~~~ iniCaching getIniValueU32: "..tostring(sect)..", "..tostring(line)..", "..tostring(default)..", "..tostring(iniFileName))
if sect and line then
if iniStatistic then
if iniStat[sect] then
iniStat[sect] = iniStat[sect] + 1
else
iniStat[sect] = 1
end
end
if iniCaching and (iniSections[sect] or iniSectionsCount == 0) then
if iniFileName and (not iniOtherLines[iniFileName] or not iniOtherLines[iniFileName]["u32"] or not iniOtherLines[iniFileName]["u32"][sect] or not iniOtherLines[iniFileName]["u32"][sect][line]) or
(not iniLines["u32"][sect] or not iniLines["u32"][sect][line]) then
-- amk.dump_table(iniLines)
local ini, sectLines = getIniValuePrepare("u32", sect, iniFileName)
if ini and ini:section_exist(sect) and ini:line_exist(sect, line) then
sectLines[line] = ini:r_u32(sect, line)
else
sectLines[line] = default
end
end
-- amk.dump_table(iniLines)
if iniFileName then
return iniOtherLines[iniFileName]["u32"][sect][line]
else
return iniLines["u32"][sect][line]
end
else
local ini
if iniFileName then
ini = ini_file(iniFileName)
else
ini = sys_ini
end
if ini and ini:section_exist(sect) and ini:line_exist(sect, line) then
return ini:r_u32(sect, line)
else
return default
end
end
else
return default
end
end
function getIniValueString(sect, line, default, iniFileName)
--get_console():execute("load ~~~ iniCaching getIniValueString: "..tostring(sect)..", "..tostring(line)..", "..tostring(default)..", "..tostring(iniFileName))
if sect and line then
if iniStatistic then
if iniStat[sect] then
iniStat[sect] = iniStat[sect] + 1
else
iniStat[sect] = 1
end
end
if iniCaching and (iniSections[sect] or iniSectionsCount == 0) then
if iniFileName and (not iniOtherLines[iniFileName] or not iniOtherLines[iniFileName]["string"] or not iniOtherLines[iniFileName]["string"][sect] or not iniOtherLines[iniFileName]["string"][sect][line]) or
(not iniLines["string"] or not iniLines["string"][sect] or not iniLines["string"][sect][line]) then
local ini, sectLines = getIniValuePrepare("string", sect, iniFileName)
if ini and ini:section_exist(sect) and ini:line_exist(sect, line) then
sectLines[line] = ini:r_string(sect, line)
else
sectLines[line] = default
end
end
-- amk.dump_table(iniLines)
if iniFileName then
return iniOtherLines[iniFileName]["string"][sect][line]
else
return iniLines["string"][sect][line]
end
else
local ini
if iniFileName then
ini = ini_file(iniFileName)
else
ini = sys_ini
end
if ini and ini:section_exist(sect) and ini:line_exist(sect, line) then
return ini:r_string(sect, line)
else
return default
end
end
else
return default
end
end
function getIniValueStringWQ(sect, line, default, iniFileName)
--get_console():execute("load ~~~ iniCaching getIniValueStringWQ: "..tostring(sect)..", "..tostring(line)..", "..tostring(default)..", "..tostring(iniFileName))
if sect and line then
if iniStatistic then
if iniStat[sect] then
iniStat[sect] = iniStat[sect] + 1
else
iniStat[sect] = 1
end
end
if iniCaching and (iniSections[sect] or iniSectionsCount == 0) then
if iniFileName and (not iniOtherLines[iniFileName] or not iniOtherLines[iniFileName]["string_wq"] or not iniOtherLines[iniFileName]["string_wq"][sect] or not iniOtherLines[iniFileName]["string_wq"][sect][line]) or
(not iniLines["string_wq"] or not iniLines["string_wq"][sect] or not iniLines["string_wq"][sect][line]) then
local ini, sectLines = getIniValuePrepare("string_wq", sect, iniFileName)
if ini and ini:section_exist(sect) and ini:line_exist(sect, line) then
sectLines[line] = ini:r_string_wq(sect, line)
else
sectLines[line] = default
end
end
-- amk.dump_table(iniLines)
if iniFileName then
return iniOtherLines[iniFileName]["string_wq"][sect][line]
else
return iniLines["string_wq"][sect][line]
end
else
local ini
if iniFileName then
ini = ini_file(iniFileName)
else
ini = sys_ini
end
if ini and ini:section_exist(sect) and ini:line_exist(sect, line) then
return ini:r_string_wq(sect, line)
else
return default
end
end
else
return default
end
end
function getIniValueBool(sect, line, default, iniFileName)
--get_console():execute("load ~~~ iniCaching getIniValueBool: "..tostring(sect)..", "..tostring(line)..", "..tostring(default)..", "..tostring(iniFileName))
if sect and line then
if iniStatistic then
if iniStat[sect] then
iniStat[sect] = iniStat[sect] + 1
else
iniStat[sect] = 1
end
end
if iniCaching and (iniSections[sect] or iniSectionsCount == 0) then
if iniFileName and (not iniOtherLines[iniFileName] or not iniOtherLines[iniFileName]["boolean"] or not iniOtherLines[iniFileName]["boolean"][sect] or iniOtherLines[iniFileName]["boolean"][sect][line] == nil) or
(not iniLines["boolean"][sect] or iniLines["boolean"][sect][line] == nil) then
local ini, sectLines = getIniValuePrepare("boolean", sect, iniFileName)
if ini and ini:section_exist(sect) and ini:line_exist(sect, line) then
sectLines[line] = ini:r_bool(sect, line)
else
sectLines[line] = default
end
end
-- amk.dump_table(iniLines)
if iniFileName then
return iniOtherLines[iniFileName]["boolean"][sect][line]
else
return iniLines["boolean"][sect][line]
end
else
local ini
if iniFileName then
ini = ini_file(iniFileName)
else
ini = sys_ini
end
if ini and ini:section_exist(sect) and ini:line_exist(sect, line) then
return ini:r_bool(sect, line)
else
return default
end
end
else
return default
end
end
function iniLinesCount(sect, iniFileName)
-- get_console():execute("load ~~~ iniCaching iniLinesCount: "..tostring(sect)..", "..tostring(iniFileName))
if sect then
if iniCaching and (iniSections[sect] or iniSectionsCount == 0) then
if iniFileName and (not iniOtherLines[iniFileName] or not iniOtherLines[iniFileName]["line"] or not iniOtherLines[iniFileName]["line"][sect] or not iniOtherLines[iniFileName]["line"][sect]["count"]) or
(not iniLines["line"] or not iniLines["line"][sect] or not iniLines["line"][sect]["count"]) then
--get_console():execute("load ~~~ iniCaching iniLinesCount: 1")
local ini, sectLines = getIniValuePrepare("line", sect, iniFileName)
if iniLineSectionExist(sect, iniFileName) then
sectLines["count"] = ini:line_count(sect)
else
sectLines["count"] = 0
end
end
-- amk.dump_table(iniLines)
if iniFileName then
return iniOtherLines[iniFileName]["line"][sect]["count"]
else
return iniLines["line"][sect]["count"]
end
else
local ini
if iniFileName then
ini = ini_file(iniFileName)
else
ini = sys_ini
end
if ini and ini:section_exist(sect) then
return ini:line_count(sect)
else
return 0
end
end
else
return 0
end
end
function iniLineSectionExist(sect, iniFileName)
--get_console():execute("load ~~~ iniCaching iniLineSectionExist: "..tostring(sect)..", "..tostring(iniFileName))
if sect then
if iniCaching and (iniSections[sect] or iniSectionsCount == 0) then
if iniFileName and (not iniOtherLines[iniFileName] or not iniOtherLines[iniFileName]["line"] or not iniOtherLines[iniFileName]["line"][sect] or iniOtherLines[iniFileName]["line"][sect]["exist"] == nil) or
(not iniLines["line"] or not iniLines["line"][sect] or iniLines["line"][sect]["exist"] == nil) then
local ini, sectLines = getIniValuePrepare("line", sect, iniFileName)
sectLines["exist"] = (ini and ini:section_exist(sect))
end
-- amk.dump_table(iniLines)
if iniFileName then
return iniOtherLines[iniFileName]["line"][sect]["exist"] -- > -1
else
return iniLines["line"][sect]["exist"] -- > -1
end
else
local ini
if iniFileName then
ini = ini_file(iniFileName)
else
ini = sys_ini
end
return (ini and ini:section_exist(sect))
end
else
return false
end
end
function getIniLine(sect, lineNumber, defaultKey, defaultValue, iniFileName)
--get_console():execute("load ~~~ iniCaching getIniLine: "..tostring(sect)..", "..tostring(lineNumber)..", "..tostring(defaultKey)..", "..tostring(defaultValue)..", "..tostring(iniFileName))
if sect and lineNumber then
if iniStatistic then
if iniStat[sect] then
iniStat[sect] = iniStat[sect] + 1
else
iniStat[sect] = 1
end
end
if iniCaching and (iniSections[sect] or iniSectionsCount == 0) then
if iniFileName and (not iniOtherLines[iniFileName] or not iniOtherLines[iniFileName]["line"] or not iniOtherLines[iniFileName]["line"][sect] or not iniOtherLines[iniFileName]["line"][sect]["lines"] or not iniOtherLines[iniFileName]["line"][sect]["lines"][lineNumber]) or
(not iniLines["line"] or not iniLines["line"][sect] or not iniLines["line"][sect]["lines"] or not iniLines["line"][sect]["lines"][lineNumber]) then
local ini, sectLines = getIniValuePrepare("line", sect, iniFileName)
if not sectLines["lines"] then
sectLines["lines"] = {}
end
local result, key, value = ini:r_line(sect, lineNumber, defaultKey, defaultValue)
sectLines["lines"][lineNumber] = {result, key, value}
end
-- amk.dump_table(iniLines)
local t
if iniFileName then
t = iniOtherLines[iniFileName]["line"][sect]["lines"][lineNumber]
else
t = iniLines["line"][sect]["lines"][lineNumber]
end
return t[1], t[2], t[3]
else
local ini
if iniFileName then
ini = ini_file(iniFileName)
else
ini = sys_ini
end
if ini and ini:section_exist(sect) then
return ini:r_line(sect, lineNumber, defaultKey, defaultValue)
else
return false, defaultKey, defaultValue
end
end
else
return false, defaultKey, defaultValue
end
end
function getIniValuePrepare(valueType, sect, iniFileName)
local ini
local sectLines
if iniFileName then
ini = ini_file(iniFileName)
if not iniOtherLines[iniFileName] then
iniOtherLines[iniFileName] = {}
end
if not iniOtherLines[iniFileName][valueType] then
iniOtherLines[iniFileName][valueType] = {}
end
if not iniOtherLines[iniFileName][valueType][sect] then
iniOtherLines[iniFileName][valueType][sect] = {}
end
sectLines = iniOtherLines[iniFileName][valueType][sect]
else
ini = sys_ini
if not iniLines[valueType] then
iniLines[valueType] = {}
end
if not iniLines[valueType][sect] then
iniLines[valueType][sect] = {}
end
sectLines = iniLines[valueType][sect]
end
return ini, sectLines
end
-------======= / ini reading functions by sapsan =======------
----------------------------------------------------------------------
function printf(fmt,...)
--[[ Ðàñêîììåíòèòü ýòî äëÿ âêëþ÷åíèÿ ðàñøèðåííîãî ëîãà
local con = get_console()
con:execute(string.gsub(string.format(fmt,...), " ", "_"))
con:execute("flush")
--]]
--[[
local arg={...}
if arg==nil or arg[0]==nil then
mylog(fmt)
else
mylog(string.format(fmt,...))
end
--]]
end
local bufferedmessages={}
function mylog(msg)
--[[
if msg==nil then
return
end
if db and db.actor then
if bufferedmessages then
for k,v in ipairs(bufferedmessages) do
db.actor:give_game_news(v, "ui\\ui_iconsTotal", Frect():set(0,0,200,200), 0, 15000)
end
bufferedmessages=nil
end
db.actor:give_game_news(msg, "ui\\ui_iconsTotal", Frect():set(0,0,200,200), 0, 15000)
else
if bufferedmessages then
table.insert(bufferedmessages,msg)
end
end
]]
if get_console() then
get_console():execute("load "..tostring(msg))
get_console():execute("flush")
end
end
function dbglog(fmt,...)
local msg = string.format(fmt, ...)
local msg_no_ws = string.gsub(msg, "%s", "_")
get_console():execute("dbg:" .. msg_no_ws)
end
function validate_enemy_object( enemy )
if not ( enemy and enemy.clsid ) then return false, nil end
if not ( IsStalker( enemy ) or IsMonster( enemy) ) then
return false, nil
end
if enemy.health == nil or enemy.health <= 0 then
return false, nil
end
-- ýòî ãðÿçíûé õàê ÷òîáû âûÿñíèòü íå ïîïàëñÿ ëè íàì ñåðâåðíûé îáúåêò
if enemy.m_story_id ~= nil then
local id = enemy.id
local enemy_gameobj = level.object_by_id( id )
if not enemy_gameobj then
return false, nil
else
return true, enemy_gameobj
end
else
return true, enemy
end
end
function parse_data(str)
local string_parts = {}
if str == nil then
string_parts[1] = nil
return string_parts
end
local str_beg,str_end
str_end = str
local split_pos = find_split_pos(str_end)
while split_pos ~= nil do
str_beg, str_end = split_string(str_end,split_pos)
table.insert(string_parts,str_beg)
split_pos = find_split_pos(str_end)
end
table.insert(string_parts,str_end)
return string_parts
end
function find_split_pos(str)
local split_pos = string.find(str,",")
if split_pos == nil then
return nil
end
local br1_pos = string.find(str,"(",1,true)
local br2_pos = string.find(str,")",1,true)
if br1_pos ~= nil and br2_pos ~= nil and br1_pos < br2_pos and split_pos > br1_pos then
local str_end1 = string.sub(str,(br2_pos +1),string.len(str))
if string.find(str_end1,",") ~= nil then
split_pos = br2_pos + string.find(str_end1,",")
else
split_pos = nil
end
end
return split_pos
end
function split_string(str,split_pos)
local str_beg = (string.sub(str,1,split_pos-1))
local str_end = string.sub(str,(split_pos+1),string.len(str))
return str_beg,str_end
end
----------------------------------------------------------------------
if nil == time_global then
time_global = function () return device():time_global() end
end
function wait_game(time_to_wait)
verify_if_thread_is_running()
if (time_to_wait == nil) then
coroutine.yield()
else
local time_to_stop = game.time() + time_to_wait
while game.time() <= time_to_stop do
coroutine.yield()
end
end
end
function wait(time_to_wait)
verify_if_thread_is_running()
if (time_to_wait == nil) then
coroutine.yield()
else
local time_to_stop = time_global() + time_to_wait
while time_global() <= time_to_stop do
coroutine.yield()
end
end
end
function action(obj,...)
local arg = {...}
local act = entity_action()
local i = 1
while true do
if (arg[i] ~= nil) then
act:set_action(arg[i])
else
break
end
i = i + 1
end
if (obj ~= nil) then
obj:command(act,false)
end
return entity_action(act)
end
function action_first(obj,...)
local arg = {...}
local act = entity_action()
local i = 1
while true do
if (arg[i] ~= nil) then
act:set_action(arg[i])
else
break
end
i = i + 1
end
if (obj ~= nil) then
obj:command(act,true)
end
return entity_action(act)
end
function round (value)
local min = math.floor (value)
local max = min + 1
if value - min > max - value then return max end
return min
end
function debug_get_level_object(obj_name)
local res = level.debug_object(obj_name)
while res == nil do
-- ÍÅ ÇÀÊÎÌÌÅÍÒÀÐÈÂÀÉÒÅ, ÈÍÀ×Å ÍÅ ÁÓÄÅÒ ÂÈÄÍÎ ÎØÈÁÎÊ ÏÐÈ ÐÀÑÑÒÀÍÎÂÊÅ ÎÁÚÅÊÒÎÂ!
printf("get_level_object() is waiting for object \"%s\"", obj_name)
res = level.object(obj_name)
wait ()
end
return res
end
function debug_get_actor()
local res = level.debug_actor()
while res == nil do
res = level.debug_actor()
wait ()
end
return res
end
function distance_between(obj1, obj2)
return obj1:position():distance_to(obj2:position())
end
--// Åñëè îäèí îáúåêò nil, íàïðèìåð íåò àêòåðà, òî ñ÷èòàåì, ÷òî îí äàëåêî
function distance_between_safe(obj1, obj2)
if(obj1 == nil or obj2 == nil) then return 100000 end
return obj1:position():distance_to(obj2:position())
end
--' Ïðîâåðêà íà èíôîïîðøíû, äàæå åñëè èãðîêà íå ñóùåñòâóåò
function has_alife_info(info_id)
local aa = alife()
if aa == nil then
return false
end
return aa:has_info(0, info_id)
end
--------------------------------------------------
-- ³ëàññ LUA ðåàëèçó³ èé Finite State Machine
-- äëÿ óïðàâëåíèÿ ïåðñîíàæàìè
--------------------------------------------------
class 'FSM'
-- èíèöèàëèçàöèÿ ïóòåì çàäàíèÿ îáúåêòà Non-Player Character
function FSM:__init(script_name, npc_obj)
-- óêàçàòåëü íà ïîäêîíòðîëüívé NPC
self.npc = npc_obj
-- ñòðîêà - èìÿ ñêðèïòà, êîòîðvé çàïóñêàåò èãðó
self.script_name = script_name
-- ìàòðèöà, çàäà³ àÿ ãðàô ïåðåõîäîâ ñîñòîÿíèé
-- ðåàëèçóåòñÿ êàê äâóìåðíàÿ òàáëèöà
self.transition_matrix = {[0] = {}}
-- íîìåð òåêó åãî ñîñòîÿíèÿ
self.current_state = 0
-- ôëàã òîãî, ÷òî ìàøèíà âêë³÷åíà
self.machine_running = true
printf("inialization FSM for NPC %s", self.npc:name())
end
function FSM:is_running()
return self.machine_running
end
function FSM:start()
printf("starting FSM for %s", self.npc:name())
self.machine_running = true
end
function FSM:stop()
printf("stoping FSM for %s", self.npc:name())
self.machine_running = false
self:reset_script_control()
end
-- äîáàâëåíèå óêàçàòåëÿ íà áóëåâó
-- ôóíêöè³ óñëîâèÿ ïåðåõîäà
function FSM:set_transition(from_state_num, to_state_num, cond_func)
printf("in setting transition %d, %d ", from_state_num, to_state_num)
if self.transition_matrix[from_state_num] == nil then
self.transition_matrix[from_state_num] = {[to_state_num] = cond_func}
else