-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmod.lua
More file actions
276 lines (236 loc) · 10.4 KB
/
Copy pathmod.lua
File metadata and controls
276 lines (236 loc) · 10.4 KB
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
TheFixesPreventer = TheFixesPreventer or {}
TheFixesPreventer.crash_ver_char_send_basenetwork = true
if not ThirdPerson then
ThirdPerson = {}
ThirdPerson.mod_path = ModPath
ThirdPerson.save_path = SavePath
ThirdPerson.mod_instance = ModInstance
ThirdPerson.unit = nil
ThirdPerson.fp_unit = nil
ThirdPerson.delayed_events = {}
ThirdPerson.settings = {
start_in_tp = true,
cam_x = 80,
cam_y = 120,
cam_z = 15,
first_person_on_steelsight = true,
first_person_on_downed = false,
third_person_crosshair = true,
third_person_crosshair_style = 1,
third_person_crosshair_size = 32,
immersive_first_person = false,
custom_weapons = false
}
function ThirdPerson:log(...)
local str = "[ThirdPerson] "
table.for_each_value({ ... }, function (v)
str = str .. tostring(v) .. " "
end)
log(str)
print(str)
end
local unit_ids = Idstring("unit")
local husk_names = {
wild = "units/pd2_dlc_wild/characters/npc_criminals_wild_1/player_criminal_wild_husk",
joy = "units/pd2_dlc_joy/characters/npc_criminals_joy_1/player_criminal_joy_husk"
} -- thanks Overkill >.<
function ThirdPerson:setup_unit(unit)
local player = unit or managers.player:local_player()
if not player then
ThirdPerson:log("ERROR: Could not find player unit!")
return
end
local player_peer = player:network():peer()
local player_movement = player:movement()
local pos = player_movement:m_pos()
local rot = player_movement:m_head_rot()
local char_id = player_peer:character_id()
local unit_name = husk_names[char_id] or tweak_data.blackmarket.characters[char_id].npc_unit:gsub("(.+)/npc_", "%1/player_") .. "_husk"
local unit_name_ids = Idstring(unit_name)
if not DB:has(unit_ids, unit_name_ids) then
ThirdPerson:log("ERROR: Could not find player husk unit for " .. char_id .. "! Assumed " .. unit_name)
self.fp_unit = nil
self.unit = nil
return
end
self.fp_unit = player
self.unit = alive(self.unit) and self.unit or World:spawn_unit(unit_name_ids, pos, rot)
self.head_obj = self.unit:get_object(Idstring("Head"))
Network:detach_unit(self.unit)
-- The third person unit should be destroyed whenever the first person unit is destroyed
player:base().pre_destroy = function (self, ...)
if alive(ThirdPerson.unit) then
World:delete_unit(ThirdPerson.unit)
end
PlayerBase.pre_destroy(self, ...)
end
local unit_base = self.unit:base()
local unit_movement = self.unit:movement()
local unit_inventory = self.unit:inventory()
local unit_sound = self.unit:sound()
-- Hook some functions
unit_base.pre_destroy = function (self, unit)
self._unit:movement():pre_destroy(unit)
self._unit:inventory():pre_destroy(self._unit)
UnitBase.pre_destroy(self, unit)
end
-- No contours
self.unit:contour().add = function () end
-- No revive SO
unit_movement._register_revive_SO = function () end
unit_movement.set_need_assistance = function (self, need_assistance) self._need_assistance = need_assistance end
unit_movement.set_need_revive = function (self, need_revive) self._need_revive = need_revive end
local look_vec_modified = Vector3()
unit_movement.update = function (self, ...)
HuskPlayerMovement.update(self, ...)
if alive(ThirdPerson.fp_unit) then
-- correct aiming direction so that lasers are approximately the same in first and third person
mvector3.set(look_vec_modified, ThirdPerson.fp_unit:camera():forward())
mvector3.rotate_with(look_vec_modified, Rotation(ThirdPerson.fp_unit:camera():rotation():z(), 1))
self:set_look_dir_instant(look_vec_modified)
end
end
unit_movement.sync_action_walk_nav_point = function (self, pos, speed, action)
self._movement_path = self._movement_path or {}
self._movement_history = self._movement_history or {}
local state = alive(ThirdPerson.fp_unit) and ThirdPerson.fp_unit:movement():current_state()
if not state then
return
end
local path_len = #self._movement_path
pos = pos or path_len > 0 and self._movement_path[path_len].pos or mvector3.copy(self:m_pos())
local type = state._state_data.on_zipline and "zipline" or (not state._gnd_ray or state._is_jumping) and "air" or "ground"
local node = {
pos = pos,
speed = speed or 1,
type = type,
action = {action}
}
table.insert(self._movement_path, node)
table.insert(self._movement_history, node)
local len = #self._movement_history
if len > 1 then
self:_determine_node_action(#self._movement_history, node)
end
for i = 1, #self._movement_history - tweak_data.network.player_path_history, 1 do
table.remove(self._movement_history, 1)
end
end
local hide_vec = Vector3(0, 0, -10000)
unit_movement.set_position = function (self, pos)
if alive(ThirdPerson.fp_unit) and ThirdPerson.fp_unit:camera():first_person() then
self._unit:set_position(hide_vec)
else
-- partial fix for movement sync, not perfect as it overrides some movement animations like jumping
pos = alive(ThirdPerson.fp_unit) and ThirdPerson.fp_unit:movement():m_pos() or pos
self._unit:set_position(pos)
mvector3.set(self._m_pos, pos)
end
end
unit_movement.set_head_visibility = function (self, visible)
self._obj_visibilities = self._obj_visibilities or {}
local char_name = managers.criminals:character_name_by_unit(self._unit)
local new_char_name = managers.criminals.convert_old_to_new_character_workname(char_name)
-- Disable head and hair objects - many thanks for being inconsistent with naming your objects, Overkill
local try_names = { "g_vest_neck", "g_head", "g_head_%s", "g_%s_mask_off", "g_%s_mask_on", "g_hair", "g_%s_hair", "g_hair_mask_on", "g_hair_mask_off" }
local obj, key
for _, v in ipairs(try_names) do
obj = char_name and self._unit:get_object(Idstring(v:format(char_name))) or new_char_name and self._unit:get_object(Idstring(v:format(new_char_name)))
if obj then
key = obj:name():key()
self._obj_visibilities[key] = self._obj_visibilities[key] or obj:visibility()
obj:set_visibility(visible and self._obj_visibilities[key])
end
end
self._mask_visibility = self._mask_visibility or self._unit:inventory()._mask_visibility
self._unit:inventory():set_mask_visibility(visible and self._mask_visibility)
end
unit_inventory.set_mask_visibility = function (self, state)
HuskPlayerInventory.set_mask_visibility(self, not ThirdPerson.settings.immersive_first_person and state)
end
-- adjust weapon switch to support custom weapons
local default_weaps = { "wpn_fps_pis_g17_npc", "wpn_fps_ass_amcar_npc" }
unit_inventory._perform_switch_equipped_weapon = function (self, weap_index, blueprint_string, cosmetics_string, peer)
self._checked_weapons = self._checked_weapons or {}
if not self._checked_weapons[weap_index] then
local equipped = ThirdPerson.fp_unit:inventory():equipped_unit()
local checked_weap = {
name = equipped:base()._factory_id and equipped:base()._factory_id .. "_npc" or "wpn_fps_ass_amcar_npc",
cosmetics_string = cosmetics_string or self:cosmetics_string_from_peer(peer, checked_weap.name),
blueprint_string = blueprint_string or equipped:blueprint_to_string()
}
local factory_weapon = tweak_data.weapon.factory[checked_weap.name]
if not factory_weapon or factory_weapon.custom and not ThirdPerson.settings.custom_weapons then
local weapon = tweak_data.weapon[managers.weapon_factory:get_weapon_id_by_factory_id(equipped:base()._factory_id or "wpn_fps_ass_amcar_npc")]
local based_on = weapon and weapon.based_on and tweak_data.weapon[weapon.based_on]
local based_on_name = based_on and tweak_data.upgrades.definitions[weapon.based_on] and tweak_data.upgrades.definitions[weapon.based_on].factory_id
local new_name = based_on_name and based_on.use_data.selection_index == weapon.use_data.selection_index and (based_on_name .. "_npc") or weapon and default_weaps[weapon.use_data.selection_index] or default_weaps[1]
ThirdPerson:log("Replaced custom weapon " .. checked_weap.name .. " with " .. new_name)
checked_weap.name = new_name
checked_weap.blueprint_string = managers.weapon_factory:blueprint_to_string(checked_weap.name, managers.weapon_factory:get_default_blueprint_by_factory_id(checked_weap.name))
checked_weap.cosmetics_string = "nil-1-0"
end
self._checked_weapons[weap_index] = checked_weap
end
local checked_weap = self._checked_weapons[weap_index]
if checked_weap then
self:add_unit_by_factory_name(checked_weap.name, true, true, checked_weap.blueprint_string, checked_weap.cosmetics_string)
end
end
-- We don't want our third person unit to make any sound, so we're plugging empty functions here
unit_sound.say = function () end
unit_sound.play = function () end
unit_sound._play = function () end
-- Setup some stuff
unit_inventory:set_melee_weapon(player_peer:melee_id(), true)
self.unit:damage():run_sequence_simple(managers.blackmarket:character_sequence_by_character_id(player_peer:character_id(), player_peer:id()))
unit_movement:set_character_anim_variables()
if ThirdPerson.settings.immersive_first_person then
unit_movement:set_head_visibility(false)
end
local level_data = managers.job and managers.job:current_level_data()
if level_data and level_data.player_sequence then
self.unit:damage():run_sequence_simple(level_data.player_sequence)
end
-- Call missed events
local handler = managers.network and managers.network._handlers and managers.network._handlers.unit
if handler then
for _, v in ipairs(self.delayed_events) do
if handler[v.func] then
handler[v.func](handler, self.unit, unpack(v.params))
end
end
self.delayed_events = {}
end
if self.settings.start_in_tp then
player:camera():set_third_person()
else
player:camera()._toggled_fp = true
end
-- Unregister from groupai manager so it doesnt count as an actual criminal
managers.groupai:state():unregister_criminal(self.unit)
end
function ThirdPerson:save()
local file = io.open(self.save_path .. "third_person.txt", "w+")
if file then
file:write(json.encode(self.settings))
file:close()
end
end
function ThirdPerson:load()
local file = io.open(self.save_path .. "third_person.txt", "r")
if file then
local data = json.decode(file:read("*all")) or {}
file:close()
for k, v in pairs(data) do
self.settings[k] = v
end
end
end
end
if RequiredScript then
local fname = ThirdPerson.mod_path .. RequiredScript:gsub(".+/(.+)", "lua/%1.lua")
if io.file_is_readable(fname) then
dofile(fname)
end
end