-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathConstructor.lua
3705 lines (3308 loc) · 183 KB
/
Constructor.lua
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
-- Constructor
-- by Hexarobi
-- A Lua Script for the Stand mod menu for GTA5
-- Allows for constructing custom vehicles and maps
-- https://github.com/hexarobi/stand-lua-constructor
local SCRIPT_VERSION = "0.51.6"
---
--- Auto-Updater
---
local auto_update_config = {
source_url="https://raw.githubusercontent.com/hexarobi/stand-lua-constructor/main/Constructor.lua",
script_relpath=SCRIPT_RELPATH,
project_url="https://github.com/hexarobi/stand-lua-constructor",
branch="main",
dependencies={
"lib/constructor/constants.lua",
"lib/constructor/constructor_lib.lua",
"lib/constructor/convertors.lua",
"lib/constructor/curated_attachments.lua",
"lib/constructor/translations.lua",
"lib/constructor/constructor_logo.png",
"lib/constructor/objects_complete.txt",
"lib/inspect.lua",
"lib/quaternionLib.lua",
"lib/ScaleformLib.lua",
"lib/iniparser.lua",
"lib/xml2lua.lua",
"lib/xmlhandler/tree.lua",
"lib/natives-3095a/g.lua",
"lib/natives-3095a/g.source.lua",
"lib/natives-3095a/init.lua",
"lib/natives-3095a/init.source.lua",
}
}
-- If loading from Stand repository, then rely on it for updates and skip auto-updater
local is_from_repository = false
util.ensure_package_is_installed('lua/auto-updater')
local auto_updater = require('auto-updater')
if auto_updater == true and not is_from_repository then
auto_updater.run_auto_update(auto_update_config)
end
---
--- Config
---
local config = {
source_code_branch = "main",
edit_offset_step = 10,
edit_rotation_step = 15,
add_attachment_gun_active = false,
create_construct_gun_active = false,
show_previews = true,
preview_camera_distance = 3,
preview_bounding_box_color = {r=255,g=0,b=255,a=255},
deconstruct_all_spawned_constructs_on_unload = true,
drive_spawned_vehicles = true,
wear_spawned_peds = true,
focus_menu_on_spawned_constructs = true,
preview_display_delay = 500,
max_search_results = 100,
spawn_entity_delay = 0,
is_final_cleanup = false,
clean_up_distance = 500,
num_allowed_spawned_constructs_per_player = 1,
chat_spawnable_dir = "spawnable",
debug_mode = false,
auto_update = true,
auto_update_check_interval = 86400,
freecam_speed = 1,
change_parent_keep_position = true,
}
-- Long global alias
CONSTRUCTOR_CONFIG = config
local state = {}
---
--- Dependencies
---
util.require_natives("3095a")
local inspect = require("inspect")
local scaleform = require("ScaleformLib")
local constants = require("constructor/constants")
local constructor_lib = require("constructor/constructor_lib")
local convertors = require("constructor/convertors")
local curated_attachments = require("constructor/curated_attachments")
local translations = require("constructor/translations")
---
--- Debug Log
---
local function debug_log(message, additional_details)
if CONSTRUCTOR_CONFIG.debug_mode then
if CONSTRUCTOR_CONFIG.debug_mode == 2 and additional_details ~= nil then
message = message .. "\n" .. inspect(additional_details)
end
util.log("[Constructor] "..message)
end
end
---
--- Translations
-- Shorthand wrapper for translation function
local function t(text)
if type(CONSTRUCTOR_TRANSLATE_FUNCTION) == "function" then
return CONSTRUCTOR_TRANSLATE_FUNCTION(text)
else
return text
end
end
---
--- Data
---
local constructor = {}
local PROPS_PATH = filesystem.scripts_dir().."lib/constructor/objects_complete.txt"
local VERSION_STRING = SCRIPT_VERSION.." / "..constructor_lib.LIB_VERSION .. " / " .. convertors.SCRIPT_VERSION
local CONSTRUCTS_DIR = filesystem.stand_dir() .. 'Constructs\\'
filesystem.mkdirs(CONSTRUCTS_DIR)
local JACKZ_BUILD_DIR = filesystem.stand_dir() .. 'Builds\\'
local STAND_GARAGE_DIR = filesystem.stand_dir() .. 'Vehicles\\'
local spawned_constructs = {}
local last_spawned_construct
local menus = {
children = {}
}
local original_player_skin
local player_construct
--local example_construct = {
-- name="My Construct", -- Name for this attachment
-- handle=5678, -- Handle for this attachment (Nonserializable)
-- root={}, -- Pointer to root construct. Root will point to itself. (Nonserializable)
-- parent={}, -- Pointer to parent construct. Root will point to itself. (Nonserializable)
-- position = { x=0, y=0, z=0 }, -- World position coords
-- offset = { x=0, y=0, z=0 }, -- Offset coords from parent
-- rotation = { x=0, y=0, z=0 },-- Rotation from parent
-- children = {
-- -- Other constructs / attachments
-- },
-- options = {
-- is_visible = true,
-- has_collision = true,
-- has_gravity = true,
-- etc...
-- },
-- is_preview = false,
-- Other meta flags used for processing...
--}
local ENTITY_TYPES = {"PED", "VEHICLE", "OBJECT"}
local SIRENS_OFF = 1
local SIRENS_LIGHTS_ONLY = 2
local SIRENS_ALL_ON = 3
---
--- Utilities
---
local function get_player_vehicle_handles()
local player_vehicle_handles = {}
for _, pid in pairs(players.list()) do
local player_ped = PLAYER.GET_PLAYER_PED(pid)
local veh = PED.GET_VEHICLE_PED_IS_IN(player_ped, false)
if not ENTITY.IS_ENTITY_A_VEHICLE(veh) then
veh = PED.GET_VEHICLE_PED_IS_IN(player_ped, true)
end
if not ENTITY.IS_ENTITY_A_VEHICLE(veh) then
veh = 0
end
if veh then
player_vehicle_handles[pid] = veh
end
end
return player_vehicle_handles
end
local function is_entity_occupied(entity, type, player_vehicle_handles)
local occupied = false
if type == "VEHICLE" then
for _, vehicle_handle in pairs(player_vehicle_handles) do
if entity == vehicle_handle then
occupied = true
end
end
end
return occupied
end
local function delete_entities_by_range(my_entities, range, type)
local player_vehicle_handles = get_player_vehicle_handles()
local player_pos = ENTITY.GET_ENTITY_COORDS(PLAYER.GET_PLAYER_PED_SCRIPT_INDEX(players.user()), 1)
local count = 0
for _, entity in ipairs(my_entities) do
local entity_pos = ENTITY.GET_ENTITY_COORDS(entity, 1)
local dist = SYSTEM.VDIST(player_pos.x, player_pos.y, player_pos.z, entity_pos.x, entity_pos.y, entity_pos.z)
if dist <= range then
if not is_entity_occupied(entity, type, player_vehicle_handles) then
entities.delete_by_handle(entity)
count = count + 1
end
end
end
return count
end
local function clear_references(attachment)
attachment.root = nil
attachment.parent = nil
if attachment.children then
for _, child_attachment in pairs(attachment.children) do
clear_references(child_attachment)
end
end
end
local function copy_construct_plan(construct_plan)
local is_root = construct_plan == construct_plan.parent
clear_references(construct_plan)
local construct = constructor_lib.table_copy(construct_plan)
if is_root then
construct.root = construct
construct.parent = construct
end
--constructor_lib.default_attachment_attributes(construct)
return construct
end
local function add_attachment_to_construct(attachment)
debug_log("Adding attachment to construct "..tostring(attachment.name), attachment)
constructor_lib.serialize_vehicle_attributes(attachment)
constructor_lib.add_attachment_to_construct(attachment)
menus.rebuild_attachment_menu(attachment)
attachment.parent.functions.refresh()
attachment.functions.focus()
end
local function delete_menu_list(menu_list)
if type(menu_list) ~= "table" then return end
for k, h in pairs(menu_list) do
if h:isValid() then
menu.delete(h)
end
menu_list[k] = nil
end
end
local function scale_input_color(input_color)
if input_color == nil then
return 0
end
if input_color > 1 then
return input_color / 255
else
return input_color
end
end
local function color_menu_input(input_color)
local color = {
r = scale_input_color(input_color.r),
g = scale_input_color(input_color.g),
b = scale_input_color(input_color.b),
a = 1
}
return color
end
local function color_menu_output(output_color)
return { r=math.floor(output_color.r * 255), g=math.floor(output_color.g * 255), b=math.floor(output_color.b * 255) }
end
---
--- Item Browser
---
local browser = {}
browser.state = {
item_counter = 0,
search_menu_counter = 0
}
browser.get_unique_item_id = function()
browser.state.item_counter = browser.state.item_counter + 1
return browser.state.item_counter
end
browser.table_copy = function(obj)
if type(obj) ~= 'table' then return obj end
local res = setmetatable({}, getmetatable(obj))
for k, v in pairs(obj) do res[browser.table_copy(k)] = browser.table_copy(v) end
return res
end
browser.search = function(search_params)
if search_params.page_size == nil then search_params.page_size = config.max_search_results end
if search_params.page_number == nil then search_params.page_number = 0 end
if search_params.menus == nil then search_params.menus = {} end
if search_params.results == nil then search_params.results = {} end
local results = search_params.query_function(search_params)
local more_results_available = false
local first_result_index = (search_params.page_size*search_params.page_number)+1
local last_result_index = search_params.page_size*(search_params.page_number+1)
for i = first_result_index, last_result_index do
if results[i] then
local search_result_menu = search_params.add_item_menu_function(search_params, results[i])
table.insert(search_params.results, search_result_menu)
end
more_results_available = (results[i+1] ~= nil)
end
if search_params.menus.search_add_more ~= nil and search_params.menus.search_add_more:isValid() then
menu.delete(search_params.menus.search_add_more)
end
if more_results_available then
search_params.menus.search_add_more = menu.action(search_params.menus.root, "[More]", {}, "", function()
local more_search_params = search_params
more_search_params.page_number = more_search_params.page_number + 1
browser.search(more_search_params)
end)
table.insert(search_params.results, search_params.menus.search_add_more)
end
end
browser.search_items = function(folder, query, results)
if results == nil then results = {} end
if #results > config.max_search_results then return results end
for _, item in folder.items do
if item.items ~= nil then
browser.search_items(item, query, results)
else
if type(item.name) == "string" then
if string.match(item.name:lower(), query:lower()) then
table.insert(results, item)
end
else
util.log("Warning: Item skipped from search due to invalid name field "..inspect(item))
end
end
end
return results
end
browser.browse_item = function(parent_menu, this_item, add_item_menu_function, browse_params)
if browse_params == nil then browse_params = {} end
if this_item.items ~= nil then
-- Expand model name strings into item tables
for key, item in this_item.items do
if type(item) == "string" then this_item.items[key] = { name=item, model=item } end
end
local menu_list = parent_menu:list(
this_item.name.." ("..#this_item.items..")",
{},
this_item.description or ""
)
browser.state.search_menu_counter = browser.state.search_menu_counter + 1
local search_command = "search"..browser.state.search_menu_counter
local search_menu = menu_list:list("Search", {}, "Search this folder and sub-folders", function() menu.show_command_box(search_command.." ") end)
search_menu:text_input("Search", {search_command}, "", function(query)
delete_menu_list(browser.state.search_results_menus)
browser.state.search_results_menus = {}
browser.search({
this_item=this_item,
query=query,
results=browser.state.search_results_menus,
menus={
root=search_menu,
},
query_function=function(search_params)
if browse_params.query_function ~= nil then
return browse_params.query_function(search_params)
else
return browser.search_items(search_params.this_item, search_params.query)
end
end,
add_item_menu_function=function(search_params, item)
if add_item_menu_function ~= nil then
if item.item_id == nil then item.item_id = browser.get_unique_item_id() end
return add_item_menu_function(search_params.menus.root, item)
end
end,
})
end)
--if browse_params.additional_page_menus ~= nil then
-- browse_params.additional_page_menus(browse_params, parent_menu)
--end
menu_list:divider("Browse")
for _, item in pairs(this_item.items) do
if type(item) == "string" then item = { name=item, model=item } end
if type(item) == "table" then
if item.items ~= nil then
browser.browse_item(menu_list, item, add_item_menu_function)
else
if add_item_menu_function ~= nil then
add_item_menu_function(menu_list, item)
end
end
end
end
return menu_list
end
end
---
--- ScaleformLib
---
local sf = scaleform('instructional_buttons')
local function hud_hide()
HUD.HIDE_HUD_COMPONENT_THIS_FRAME(6)
HUD.HIDE_HUD_COMPONENT_THIS_FRAME(7)
HUD.HIDE_HUD_COMPONENT_THIS_FRAME(8)
HUD.HIDE_HUD_COMPONENT_THIS_FRAME(9)
---@diagnostic disable-next-line: param-type-mismatch
memory.write_int(memory.script_global(1645739+1121), 1)
sf.CLEAR_ALL()
sf.TOGGLE_MOUSE_BUTTONS(false)
end
local function sf_free_edit()
hud_hide()
sf.SET_DATA_SLOT(0,PAD.GET_CONTROL_INSTRUCTIONAL_BUTTONS_STRING(0, 33, true) , t("Forward"))
sf.SET_DATA_SLOT(1,PAD.GET_CONTROL_INSTRUCTIONAL_BUTTONS_STRING(0, 32, true), t('Back'))
sf.SET_DATA_SLOT(2,PAD.GET_CONTROL_INSTRUCTIONAL_BUTTONS_STRING(0, 34, true), t('Left'))
sf.SET_DATA_SLOT(3,PAD.GET_CONTROL_INSTRUCTIONAL_BUTTONS_STRING(0, 35, true) , t("Right"))
sf.SET_DATA_SLOT(4,PAD.GET_CONTROL_INSTRUCTIONAL_BUTTONS_STRING(0, 22, true) , t("Up"))
sf.SET_DATA_SLOT(5,PAD.GET_CONTROL_INSTRUCTIONAL_BUTTONS_STRING(0, 36, true) , t("Down"))
sf.DRAW_INSTRUCTIONAL_BUTTONS()
sf:draw_fullscreen()
end
local function sf_gizmo_edit()
hud_hide()
sf.SET_DATA_SLOT(0,PAD.GET_CONTROL_INSTRUCTIONAL_BUTTONS_STRING(0, 238, true) , t("Select gizmo arrow"))
sf.SET_DATA_SLOT(1,PAD.GET_CONTROL_INSTRUCTIONAL_BUTTONS_STRING(0, 237, true), t('Hold to spin camera'))
sf.DRAW_INSTRUCTIONAL_BUTTONS()
sf:draw_fullscreen()
end
---
--- Player Construct
---
local function save_original_player_skin()
debug_log("Saving original player skin")
original_player_skin = constructor_lib.table_copy(constructor_lib.construct_base)
original_player_skin.handle = players.user_ped()
original_player_skin.name = "Original Player Skin"
original_player_skin.type = "PED"
original_player_skin.is_player=true
original_player_skin.root = original_player_skin
original_player_skin.parent = original_player_skin
constructor_lib.serialize_ped_attributes(original_player_skin)
--debug_log("Saved original player skin "..inspect(original_player_skin))
end
local function restore_original_player_skin()
debug_log("Restoring original player skin")
if original_player_skin ~= nil then
constructor_lib.deserialize_ped_attributes(original_player_skin)
end
end
local function save_player_construct(construct)
save_original_player_skin()
player_construct = construct
end
local function create_player_construct()
local new_player_construct = constructor_lib.table_copy(constructor_lib.construct_base)
new_player_construct.handle=players.user_ped()
new_player_construct.type="PED"
new_player_construct.name="Player"
new_player_construct.is_player=true
new_player_construct.root = new_player_construct
new_player_construct.parent = new_player_construct
save_player_construct(new_player_construct)
end
local function remove_player_construct()
if player_construct == nil then return end
constructor.delete_spawned_construct(player_construct)
restore_original_player_skin()
player_construct = nil
end
local function get_player_construct()
if player_construct == nil then
create_player_construct()
end
return player_construct
end
---
--- Construct Plan Description
---
local function get_type(attachment)
local child_type = attachment.type
if child_type == nil then child_type = "OBJECT" end
return child_type
end
local function count_construct_children(construct_plan, counter)
if counter == nil then counter = {["OBJECT"]=0, ["PED"]=0, ["VEHICLE"]=0, ["PARTICLE"]=0, ["TOTAL"]=0} end
for _, child_attachment in pairs(construct_plan.children) do
local child_type = get_type(child_attachment)
if counter[child_type] == nil then error("Invalid type "..tostring(child_type)) end
counter[child_type] = counter[child_type] + 1
counter["TOTAL"] = counter["TOTAL"] + 1
count_construct_children(child_attachment, counter)
end
return counter
end
local function get_construct_plan_description(construct_plan)
debug_log("Building construct plan description "..tostring(construct_plan.name), construct_plan)
local descriptions = {}
if construct_plan.name ~= nil then table.insert(descriptions, construct_plan.name) end
if construct_plan.model ~= nil then table.insert(descriptions, construct_plan.model) end
table.insert(descriptions, t(get_type(construct_plan)))
if construct_plan.temp.source_file_type ~= nil then table.insert(descriptions, t(construct_plan.temp.source_file_type)) end
if construct_plan.author ~= nil then table.insert(descriptions, t("Created By: ")..construct_plan.author) end
if construct_plan.description ~= nil then table.insert(descriptions, construct_plan.description) end
local counter = count_construct_children(construct_plan)
if counter["TOTAL"] > 0 then
table.insert(descriptions,
counter["TOTAL"].." "..t("attachments").." ("..counter["PED"].." "..t("peds")..", "..counter["OBJECT"].." "..t("objects")..", "..counter["VEHICLE"].." "..t("vehicles")..")")
end
if construct_plan.temp.filepath ~= nil then table.insert(descriptions, construct_plan.temp.filepath) end
local description_string = ""
for _, description in pairs(descriptions) do
description_string = description_string .. description .. "\n"
end
return description_string
end
---
--- Preview Camera
---
local minVec = v3.new()
local maxVec = v3.new()
local function rotation_to_direction(rotation)
local adjusted_rotation =
{
x = (math.pi / 180) * rotation.x,
y = (math.pi / 180) * rotation.y,
z = (math.pi / 180) * rotation.z
}
local direction =
{
x = -math.sin(adjusted_rotation.z) * math.abs(math.cos(adjusted_rotation.x)),
y = math.cos(adjusted_rotation.z) * math.abs(math.cos(adjusted_rotation.x)),
z = math.sin(adjusted_rotation.x)
}
return direction
end
local function get_offset_from_camera(distance)
if type(distance) ~= "table" then
distance = {x=distance, y=distance, z=distance}
end
local cam_rot = CAM.GET_FINAL_RENDERED_CAM_ROT(0)
local cam_pos = CAM.GET_FINAL_RENDERED_CAM_COORD()
local direction = rotation_to_direction(cam_rot)
local destination = {
x = cam_pos.x + (direction.x * distance.x),
y = cam_pos.y + (direction.y * distance.y),
z = cam_pos.z + (direction.z * distance.z)
}
return destination
end
local function calculate_model_size(model)
MISC.GET_MODEL_DIMENSIONS(model, minVec, maxVec)
return (maxVec:getX() - minVec:getX()), (maxVec:getY() - minVec:getY()), (maxVec:getZ() - minVec:getZ())
end
local function calculate_construct_size(construct, child_attachment)
if construct.dimensions == nil then construct.dimensions = {l=0, w=0, h=0, min_vec={x=0,y=0,z=0}, max_vec={x=0,y=0,z=0}} end
if child_attachment == nil then child_attachment = construct end
if child_attachment.offset == nil then child_attachment.offset = {x=0,y=0,z=0} end
if child_attachment.hash == nil and child_attachment.model ~= nil then
child_attachment.hash = util.joaat(child_attachment.model)
end
if child_attachment.hash ~= nil then
constructor_lib.load_hash_for_attachment(child_attachment)
MISC.GET_MODEL_DIMENSIONS(child_attachment.hash, minVec, maxVec)
--debug_log("Calc size "..inspect(child_attachment))
construct.dimensions.min_vec.x = math.min(construct.dimensions.min_vec.x, minVec:getX() + child_attachment.offset.x)
construct.dimensions.min_vec.y = math.min(construct.dimensions.min_vec.y, minVec:getY() + child_attachment.offset.y)
construct.dimensions.min_vec.z = math.min(construct.dimensions.min_vec.z, minVec:getZ() + child_attachment.offset.z)
construct.dimensions.max_vec.x = math.max(construct.dimensions.max_vec.x, maxVec:getX() + child_attachment.offset.x)
construct.dimensions.max_vec.y = math.max(construct.dimensions.max_vec.y, maxVec:getY() + child_attachment.offset.y)
construct.dimensions.max_vec.z = math.max(construct.dimensions.max_vec.z, maxVec:getZ() + child_attachment.offset.z)
end
if child_attachment.children then
for _, child in pairs(child_attachment.children) do
calculate_construct_size(construct, child)
end
end
construct.dimensions.l = (construct.dimensions.max_vec.x - construct.dimensions.min_vec.x)
construct.dimensions.w = (construct.dimensions.max_vec.y - construct.dimensions.min_vec.y)
construct.dimensions.h = (construct.dimensions.max_vec.z - construct.dimensions.min_vec.z)
end
local function calculate_camera_distance(attachment)
if attachment.hash == nil then attachment.hash = util.joaat(attachment.model) end
constructor_lib.load_hash_for_attachment(attachment)
local l, w, h = calculate_model_size(attachment.hash)
attachment.camera_distance = math.max(l, w, h) + config.preview_camera_distance
calculate_construct_size(attachment)
attachment.camera_distance = math.max(attachment.dimensions.l, attachment.dimensions.w, attachment.dimensions.h) + config.preview_camera_distance
end
---
--- Preview
---
local spawned_previews = {}
local current_preview
local next_preview
local image_preview
local function cleanup_previews_tick()
--debug_log("Cleanup previews tick. Checking "..#spawned_previews.." spawned previews.")
constructor_lib.array_remove(spawned_previews, function(tbl, i)
local spawned_preview = tbl[i]
if spawned_preview ~= current_preview then
--debug_log("Removing preview "..tostring(spawned_preview.name))
constructor_lib.remove_attachment(spawned_preview)
return false
else
ENTITY.FREEZE_ENTITY_POSITION(spawned_preview.handle, true)
end
return true
end)
end
local function remove_preview(construct_plan)
next_preview = nil
image_preview = nil
if construct_plan ~= nil then
--debug_log("Removing preview "..tostring(construct_plan.name))
if current_preview == construct_plan then
current_preview = nil
end
else
current_preview = nil
end
--debug_log("Current Preview. current_preview "..inspect(current_preview))
end
local function add_preview(construct_plan, preview_image_path)
if config.show_previews == false then return end
remove_preview(construct_plan)
if construct_plan == nil then return end
debug_log("Adding preview for "..tostring(construct_plan.name), construct_plan)
if constructor_lib.is_spawn_mode_position(construct_plan) then
if filesystem.exists(preview_image_path) then image_preview = directx.create_texture(preview_image_path) end
return
end
next_preview = construct_plan
util.yield(config.preview_display_delay)
if next_preview == construct_plan then
local attachment = copy_construct_plan(construct_plan)
if construct_plan.type == "PED" then
constructor_lib.use_player_ped_attributes_as_base(attachment)
elseif construct_plan.type == "PARTICLE" then
attachment = {model="ng_proc_cigbuts02a", children={attachment}}
end
attachment.root = attachment
attachment.parent = attachment
attachment.is_preview = true
constructor_lib.default_attachment_attributes(attachment)
calculate_camera_distance(attachment)
attachment.position = get_offset_from_camera(attachment.camera_distance)
local spawned_preview = constructor_lib.create_entity_with_children(attachment)
if not spawned_preview then
util.toast("There was a problem loading construct preview", TOAST_ALL)
return
end
util.yield_once()
if next_preview == construct_plan then
current_preview = spawned_preview
if construct_plan.load_menu and construct_plan.load_menu:isValid() then
menu.set_help_text(construct_plan.load_menu, get_construct_plan_description(current_preview))
end
end
table.insert(spawned_previews, spawned_preview)
end
end
---
--- Tick Handlers
---
local function update_preview_tick()
if current_preview ~= nil then
--debug_log("Update preview tick")
current_preview.position = get_offset_from_camera(current_preview.camera_distance)
current_preview.rotation.z = current_preview.rotation.z + 2
constructor_lib.attach_entity(current_preview)
constructor_lib.update_attachment_position(current_preview)
constructor_lib.draw_bounding_box(current_preview.handle, config.preview_bounding_box_color)
constructor_lib.draw_bounding_box_with_dimensions(current_preview.handle, config.preview_bounding_box_color, current_preview.dimensions.min_vec, current_preview.dimensions.max_vec)
constructor_lib.completely_disable_attachment_collision(current_preview)
constructor_lib.update_particles_tick(current_preview)
end
if image_preview ~= nil then
directx.draw_texture(image_preview, 0.10, 0.10, 0.5, 0.5, 0.5, 0.5, 0, 1, 1, 1, 1)
end
cleanup_previews_tick()
end
local function update_attachment_tick(attachment)
--debug_log("Updating attachment tick "..attachment.name)
constructor_lib.update_attachment_tick(attachment)
for _, child_attachment in pairs(attachment.children) do
if child_attachment == attachment then error("Invalid child attachment") end
update_attachment_tick(child_attachment)
end
end
local function update_constructs_tick()
for _, spawned_construct in pairs(spawned_constructs) do
update_attachment_tick(spawned_construct)
end
end
local function get_aim_info()
local outptr = memory.alloc(4)
local success = PLAYER.GET_ENTITY_PLAYER_IS_FREE_AIMING_AT(players.user(), outptr)
local aim_info = {handle=0}
if success then
local handle = memory.read_int(outptr)
if ENTITY.DOES_ENTITY_EXIST(handle) then
aim_info.handle = handle
end
if ENTITY.GET_ENTITY_TYPE(handle) == 1 then
local vehicle = PED.GET_VEHICLE_PED_IS_IN(handle, false)
if vehicle ~= 0 then
if VEHICLE.GET_PED_IN_VEHICLE_SEAT(vehicle, -1) then
handle = vehicle
aim_info.handle = handle
end
end
end
aim_info.hash = ENTITY.GET_ENTITY_MODEL(handle)
aim_info.model = util.reverse_joaat(aim_info.hash)
aim_info.health = ENTITY.GET_ENTITY_HEALTH(handle)
aim_info.type = ENTITY_TYPES[ENTITY.GET_ENTITY_TYPE(handle)]
end
--memory.free(outptr)
return aim_info
end
local was_key_down = false
local function aim_info_tick()
if not (config.add_attachment_gun_active or config.create_construct_gun_active) then return end
--debug_log("Attachment gun tick")
local info = get_aim_info()
if info.handle ~= 0 then
local text = "Shoot (or press J) to add " .. info.type .. " `" .. info.model .. "` to construct " -- .. config.add_attachment_gun_recipient.name or ""
directx.draw_text(0.501, 0.301, text, 5, 0.5, {r=0,g=0,b=0,a=0.3}, true)
directx.draw_text(0.499, 0.299, text, 5, 0.5, {r=0,g=0,b=0,a=0.3}, true)
directx.draw_text(0.501, 0.299, text, 5, 0.5, {r=0,g=0,b=0,a=0.3}, true)
directx.draw_text(0.499, 0.301, text, 5, 0.5, {r=0,g=0,b=0,a=0.3}, true)
directx.draw_text(0.5, 0.3, text, 5, 0.5, {r=1,g=1,b=1,a=1}, true)
constructor_lib.draw_bounding_box(info.handle, config.preview_bounding_box_color)
if util.is_key_down(0x4A) or PED.IS_PED_SHOOTING(players.user_ped()) then
if was_key_down == false then
if config.create_construct_gun_active then
util.toast("Creating "..info.model)
local construct = constructor.create_construct_from_handle(info.handle)
construct.name = info.model
if construct then
menus.rebuild_attachment_menu(construct)
construct.functions.refresh()
if menu.is_ref_valid(construct.menus.info) then
menu.focus(construct.menus.info)
end
end
elseif config.add_attachment_gun_active then
util.toast("Attaching "..info.model)
add_attachment_to_construct({
parent=config.add_attachment_gun_recipient,
root=config.add_attachment_gun_recipient.root,
hash=info.hash,
model=info.model,
})
config.add_attachment_gun_recipient.root.functions.refresh()
end
end
was_key_down = true
else
was_key_down = false
end
end
end
local function set_attachment_edit_menu_sensitivity(attachment, offset_step, rotation_step)
if attachment.menus ~= nil then
if attachment == attachment.root then
if attachment.menus.edit_position_x ~= nil then
menu.set_step_size(attachment.menus.edit_position_x, offset_step)
menu.set_step_size(attachment.menus.edit_position_y, offset_step)
menu.set_step_size(attachment.menus.edit_position_z, offset_step)
menu.set_step_size(attachment.menus.edit_world_rotation_x, rotation_step)
menu.set_step_size(attachment.menus.edit_world_rotation_y, rotation_step)
menu.set_step_size(attachment.menus.edit_world_rotation_z, rotation_step)
end
else
if attachment.menus.edit_offset_x ~= nil then
menu.set_step_size(attachment.menus.edit_offset_x, offset_step)
menu.set_step_size(attachment.menus.edit_offset_y, offset_step)
menu.set_step_size(attachment.menus.edit_offset_z, offset_step)
menu.set_step_size(attachment.menus.edit_rotation_x, rotation_step)
menu.set_step_size(attachment.menus.edit_rotation_y, rotation_step)
menu.set_step_size(attachment.menus.edit_rotation_z, rotation_step)
end
end
end
for _, child_attachment in pairs(attachment.children) do
if child_attachment == attachment then error("Invalid child attachment") end
set_attachment_edit_menu_sensitivity(child_attachment, offset_step, rotation_step)
end
end
local edit_sensitivity_state = "normal"
local function sensitivity_modifier_check_tick()
if util.is_key_down(0x10) then
if edit_sensitivity_state ~= "fine" then
for _, construct in pairs(spawned_constructs) do
set_attachment_edit_menu_sensitivity(construct, 1, 1)
end
edit_sensitivity_state = "fine"
end
elseif util.is_key_down(0x11) then
if edit_sensitivity_state ~= "coarse" then
for _, construct in pairs(spawned_constructs) do
set_attachment_edit_menu_sensitivity(construct, config.edit_offset_step * 10, config.edit_rotation_step * 10)
end
edit_sensitivity_state = "coarse"
end
else
if edit_sensitivity_state ~= "normal" then
for _, construct in pairs(spawned_constructs) do
set_attachment_edit_menu_sensitivity(construct, config.edit_offset_step, config.edit_rotation_step)
end
edit_sensitivity_state = "normal"
end
end
end
local function draw_editing_bounding_box(attachment)
if attachment.is_editing and menu.is_open() then
--debug_log("Drawing bounding box tick "..attachment.name)
if attachment.type ~= "PARTICLE" then
constructor_lib.draw_bounding_box(attachment.handle, config.preview_bounding_box_color)
end
end
for _, child_attachment in pairs(attachment.children) do
if child_attachment == attachment then error("Invalid child attachment") end
draw_editing_bounding_box(child_attachment)
end
end
local function draw_editing_attachment_bounding_box_tick()
for _, construct in pairs(spawned_constructs) do
draw_editing_bounding_box(construct)
end
end
---
--- Gizmo Edit
---
local gizmo_scale = 0.02
local colour = {r = 255, g = 0, b = 255, a = 255}
local current_gizmo_entity = -1
local grabbed_gizmo_index = -1
local function gizmo_edit_mode_tick()
if not state.gizmo_edit_mode then return end
sf_gizmo_edit()
GRAPHICS.SET_DEPTHWRITING(true)
HUD.SET_MOUSE_CURSOR_THIS_FRAME()
PAD.DISABLE_CONTROL_ACTION(2, 25, true) --aim
PAD.DISABLE_CONTROL_ACTION(2, 24, true) --attack
PAD.DISABLE_CONTROL_ACTION(2, 257, true) --attack2
if PAD.IS_DISABLED_CONTROL_PRESSED(2, 25) then return true end
PAD.DISABLE_CONTROL_ACTION(2, 1, true) --look lr
PAD.DISABLE_CONTROL_ACTION(2, 2, true) --look ud
local mouse_dir = constructor_lib.get_mouse_cursor_dir()
if current_gizmo_entity ~= -1 then
local cam_dir = CAM.GET_FINAL_RENDERED_CAM_ROT(2):toDir()
local gizmos = constructor_lib.get_gizmos(current_gizmo_entity)
local hovered_gizmo = constructor_lib.get_gizmo_hovered(gizmos, gizmo_scale)
if hovered_gizmo.index ~= -1 and PAD.IS_DISABLED_CONTROL_JUST_PRESSED(2, 237) then
grabbed_gizmo_index = hovered_gizmo.index
elseif PAD.IS_DISABLED_CONTROL_JUST_RELEASED(2, 237) then
grabbed_gizmo_index = -1
end
for i, g in ipairs(gizmos) do
g.colour = colour
if i == hovered_gizmo.index or i == grabbed_gizmo_index then
g.colour = {r = 255, g = 255, b = 255, a = 255}
end
end
if grabbed_gizmo_index ~= -1 then
gizmos = {gizmos[grabbed_gizmo_index]}
local grabbed_gizmo = gizmos[1]
local gizmo_dir = grabbed_gizmo.rot:mul_v3(v3.new(0, 0, 1))
local point = constructor_lib.closest_point_on_lines(gizmo_dir, mouse_dir, grabbed_gizmo.pos, CAM.GET_FINAL_RENDERED_CAM_COORD())
ENTITY.SET_ENTITY_COORDS_NO_OFFSET(current_gizmo_entity,point.x - grabbed_gizmo.offset.x, point.y - grabbed_gizmo.offset.y, point.z - grabbed_gizmo.offset.z, false, false, false)
end
constructor_lib.draw_all_gizmos(gizmos, gizmo_scale)
end
if grabbed_gizmo_index == -1 and PAD.IS_DISABLED_CONTROL_JUST_PRESSED(2, 237) then
--ENTITY.FREEZE_ENTITY_POSITION(current_gizmo_entity, false)
PHYSICS.ACTIVATE_PHYSICS(current_gizmo_entity)
current_gizmo_entity = constructor_lib.get_ent_clicked_on(mouse_dir)
--ENTITY.FREEZE_ENTITY_POSITION(current_gizmo_entity, true)
end
GRAPHICS.SET_DEPTHWRITING(false)
return true
end
---
--- Free Edit
---
local free_edit_cam
local world_up = v3.new(0, 0, 1)
local function get_cam_vectors()
local cam_rot = CAM.GET_FINAL_RENDERED_CAM_ROT(2)
local forward = v3.toDir(cam_rot)
local right = v3.crossProduct(forward, world_up)
right:normalise()
local up = v3.crossProduct(right, forward)
return forward, right, up
end
local free_edit_mode_tick = function()
if not config.free_edit_mode then return true end
sf_free_edit()
local attachment = config.free_edit_attachment
local forward, right, up = get_cam_vectors()
--local pos = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(free_edit_cam, 0, -2, -2)
--attachment.position = {x=pos.x, y=pos.y, z=pos.z}
--attachment.position = get_offset_from_camera({x=0, y=2, z=2})
local camera_sensitivity = 2
local cam_pos = CAM.GET_FINAL_RENDERED_CAM_COORD()
attachment.position = {
x = cam_pos.x + (forward.x * camera_sensitivity) + (up.x * camera_sensitivity),
y = cam_pos.y + (forward.y * camera_sensitivity) + (up.x * camera_sensitivity),
z = cam_pos.z + (forward.z * camera_sensitivity) + (up.x * camera_sensitivity)
}
--debug_log("Setting pos "..attachment.name.." to "..inspect(attachment.position))
constructor_lib.move_attachment(attachment)
local new_cam_pos
local sensitivity = 0.3
if PAD.IS_DISABLED_CONTROL_PRESSED(2, 32) then
--local offset = get_offset_from_cam_in_world_coords(cam, {x=1,y=0,z=0})