Skip to content

Commit 51ae024

Browse files
committed
optimized memory and speed consumption
1 parent c6ccfa7 commit 51ae024

1 file changed

Lines changed: 38 additions & 71 deletions

File tree

defgraph/defgraph.lua

Lines changed: 38 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,8 @@ local function fetch_path(change_number, from_id, to_id)
554554
-- check for existing cache
555555
if pathfinder_cache[from_id] then
556556
local cache = pathfinder_cache[from_id][to_id]
557-
if cache then
558-
if cache.change_number == change_number then
559-
return cache
560-
end
557+
if cache and cache.change_number == change_number then
558+
return cache
561559
end
562560
end
563561

@@ -572,7 +570,6 @@ local function fetch_path(change_number, from_id, to_id)
572570
if not pathfinder_cache[path[index].id] then
573571
pathfinder_cache[path[index].id] = {}
574572
end
575-
576573
pathfinder_cache[path[index].id][to_id] = {
577574
change_number = change_number,
578575
distance = path[index].distance,
@@ -588,63 +585,49 @@ end
588585
---- Calculate path curvature.
589586
local function process_path_curvature(before, current, after, roundness, settings_path_curve_tightness,
590587
settings_path_curve_max_distance_from_corner)
591-
local new_position_list = {}
592-
593588
local Q_before = (settings_path_curve_tightness - 1) / settings_path_curve_tightness * before + current / settings_path_curve_tightness
594589
local R_before = before / settings_path_curve_tightness + (settings_path_curve_tightness - 1) / settings_path_curve_tightness * current
595590
local Q_after = (settings_path_curve_tightness - 1) / settings_path_curve_tightness * current + after / settings_path_curve_tightness
596591
local R_after = current / settings_path_curve_tightness + (settings_path_curve_tightness - 1) / settings_path_curve_tightness * after
597592

598593
if distance(Q_before, before) > settings_path_curve_max_distance_from_corner then
599-
Q_before = vmath.lerp(settings_path_curve_max_distance_from_corner/distance(before, current), before, current)
594+
Q_before = vmath.lerp(settings_path_curve_max_distance_from_corner / distance(before, current), before, current)
600595
end
601596
if distance(R_before, current) > settings_path_curve_max_distance_from_corner then
602-
R_before = vmath.lerp(settings_path_curve_max_distance_from_corner/distance(before, current), current, before)
597+
R_before = vmath.lerp(settings_path_curve_max_distance_from_corner / distance(before, current), current, before)
603598
end
604599
if distance(Q_after, current) > settings_path_curve_max_distance_from_corner then
605-
Q_after = vmath.lerp(settings_path_curve_max_distance_from_corner/distance(current, after), current, after)
600+
Q_after = vmath.lerp(settings_path_curve_max_distance_from_corner / distance(current, after), current, after)
606601
end
607602
if distance(R_after, after) > settings_path_curve_max_distance_from_corner then
608-
R_after = vmath.lerp(settings_path_curve_max_distance_from_corner/distance(current, after), after, current)
603+
R_after = vmath.lerp(settings_path_curve_max_distance_from_corner / distance(current, after), after, current)
609604
end
610605

611606
if roundness ~= 1 then
612-
local new_list_before = process_path_curvature(Q_before, R_before, Q_after, roundness - 1, settings_path_curve_tightness, settings_path_curve_max_distance_from_corner)
613-
local new_list_after = process_path_curvature(R_before, Q_after, R_after, roundness - 1, settings_path_curve_tightness, settings_path_curve_max_distance_from_corner)
607+
local list_before = process_path_curvature(Q_before, R_before, Q_after, roundness - 1, settings_path_curve_tightness,
608+
settings_path_curve_max_distance_from_corner)
609+
local list_after = process_path_curvature(R_before, Q_after, R_after, roundness - 1, settings_path_curve_tightness,
610+
settings_path_curve_max_distance_from_corner)
614611

615-
for key, value in pairs(new_list_before) do
616-
table.insert(new_position_list, value)
617-
end
618-
for key, value in pairs(new_list_after) do
619-
table.insert(new_position_list, value)
612+
for key, value in pairs(list_after) do
613+
table.insert(list_before, value)
620614
end
615+
616+
return list_before, Q_before, R_after
621617
else
622-
table.insert(new_position_list, R_before)
623-
table.insert(new_position_list, Q_after)
618+
return {R_before, Q_after}, Q_before, R_after
624619
end
625-
return new_position_list, Q_before, R_after
626620
end
627621

628622
---- Initialize moves from source position to a node with an destination node inside the created map.
629623
local function move_internal_initialize(source_position, move_data)
630624
local near_result = calculate_to_nearest_route(source_position)
631625
if not near_result or #move_data.destination_list == 0 then
632626
-- stay until something changes
633-
return {
634-
change_number = map_change_iterator,
635-
destination_list = move_data.destination_list,
636-
destination_index = move_data.destination_index,
637-
route_type = move_data.route_type,
638-
path_index = 0,
639-
path = {},
640-
initial_face_vector = move_data.initial_face_vector,
641-
current_face_vector = move_data.current_face_vector,
642-
settings_gameobject_threshold = move_data.settings_gameobject_threshold,
643-
settings_path_curve_tightness = move_data.settings_path_curve_tightness,
644-
settings_path_curve_roundness = move_data.settings_path_curve_roundness,
645-
settings_allow_enter_on_route = move_data.settings_allow_enter_on_route,
646-
settings_path_curve_max_distance_from_corner = move_data.settings_path_curve_max_distance_from_corner
647-
}
627+
move_data.change_number = map_change_iterator
628+
move_data.path_index = 0
629+
move_data.path = {}
630+
return move_data
648631
else
649632
local from_path = nil
650633
local to_path = nil
@@ -691,47 +674,37 @@ local function move_internal_initialize(source_position, move_data)
691674
end
692675
end
693676

694-
local new_position_list = {}
695677
if move_data.settings_path_curve_roundness ~= 0 then
696-
table.insert(new_position_list, position_list[1])
678+
move_data.path = {}
679+
680+
table.insert(move_data.path, position_list[1])
697681

698682
for i = 2, #position_list - 1 do
699-
local partial_position_list, Q_before, R_after = process_path_curvature(position_list[i - 1], position_list[i], position_list[i + 1]
700-
, move_data.settings_path_curve_roundness, move_data.settings_path_curve_tightness, move_data.settings_path_curve_max_distance_from_corner)
683+
local partial_position_list, Q_before, R_after = process_path_curvature(position_list[i - 1], position_list[i], position_list[i + 1],
684+
move_data.settings_path_curve_roundness, move_data.settings_path_curve_tightness,
685+
move_data.settings_path_curve_max_distance_from_corner)
701686

702687
if i == 2 then
703-
table.insert(new_position_list, Q_before)
688+
table.insert(move_data.path, Q_before)
704689
end
705690

706691
for key, value in pairs(partial_position_list) do
707-
table.insert(new_position_list, value)
692+
table.insert(move_data.path, value)
708693
end
709694

710695
if i == #position_list - 1 then
711-
table.insert(new_position_list, R_after)
696+
table.insert(move_data.path, R_after)
712697
end
713698
end
714699

715-
table.insert(new_position_list, position_list[#position_list])
700+
table.insert(move_data.path, position_list[#position_list])
716701
else
717-
new_position_list = position_list
702+
move_data.path = position_list
718703
end
719704

720-
return {
721-
change_number = map_change_iterator,
722-
destination_list = move_data.destination_list,
723-
destination_index = move_data.destination_index,
724-
route_type = move_data.route_type,
725-
path_index = 1,
726-
path = new_position_list,
727-
initial_face_vector = move_data.initial_face_vector,
728-
current_face_vector = move_data.current_face_vector,
729-
settings_gameobject_threshold = move_data.settings_gameobject_threshold,
730-
settings_path_curve_tightness = move_data.settings_path_curve_tightness,
731-
settings_path_curve_roundness = move_data.settings_path_curve_roundness,
732-
settings_allow_enter_on_route = move_data.settings_allow_enter_on_route,
733-
settings_path_curve_max_distance_from_corner = move_data.settings_path_curve_max_distance_from_corner
734-
}
705+
move_data.change_number = map_change_iterator
706+
move_data.path_index = 1
707+
return move_data
735708
end
736709
end
737710

@@ -814,12 +787,10 @@ function M.move_player(current_position, speed, move_data)
814787
move_data = move_internal_initialize(current_position, move_data)
815788
end
816789

817-
local rotation
790+
local rotation = nil
818791
-- stand still if no route found
819792
if move_data.path_index == 0 then
820-
if not move_data.initial_face_vector then
821-
rotation = nil
822-
else
793+
if move_data.initial_face_vector then
823794
rotation = vmath.quat_rotation_z(atan2(move_data.current_face_vector.y, move_data.current_face_vector.x) - atan2(move_data.initial_face_vector.y, move_data.initial_face_vector.x))
824795
end
825796
return move_data, {
@@ -834,9 +805,7 @@ function M.move_player(current_position, speed, move_data)
834805
while distance(current_position, move_data.path[move_data.path_index]) <= move_data.settings_gameobject_threshold + 1 do
835806
if move_data.path_index == #move_data.path then
836807
-- reached next path node
837-
if not move_data.initial_face_vector then
838-
rotation = nil
839-
else
808+
if move_data.initial_face_vector then
840809
rotation = vmath.quat_rotation_z(atan2(move_data.current_face_vector.y, move_data.current_face_vector.x) - atan2(move_data.initial_face_vector.y, move_data.initial_face_vector.x))
841810
end
842811

@@ -886,15 +855,13 @@ function M.move_player(current_position, speed, move_data)
886855
local direction_vector = move_data.path[move_data.path_index] - current_position
887856
direction_vector.z = 0
888857
direction_vector = vmath.normalize(direction_vector)
889-
if not move_data.initial_face_vector then
890-
rotation = nil
891-
else
858+
if move_data.initial_face_vector then
892859
local rotation_vector = vmath.lerp(0.2 * speed, move_data.current_face_vector, direction_vector)
893860
rotation = vmath.quat_rotation_z(atan2(rotation_vector.y, rotation_vector.x) - atan2(move_data.initial_face_vector.y, move_data.initial_face_vector.x))
894861
move_data.current_face_vector = rotation_vector
895862
end
896863
return move_data, {
897-
position = (current_position + direction_vector * speed),
864+
position = current_position + direction_vector * speed,
898865
rotation = rotation,
899866
is_reached = false,
900867
destination_id = move_data.destination_list[move_data.destination_index]

0 commit comments

Comments
 (0)