Skip to content

Commit

Permalink
Handle lefttop/rightbottom collision_box specifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Suprcheese committed Jan 24, 2018
1 parent cbf49fd commit 91d8298
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions data-updates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ local function exclusion_applies(exclusion)
return true
end
end

-- The apply_when_object_exists object didn't exist (the mod which the exclusion was for is not active).
return false
end


-- Update the exclusion arrays by parsing an exclusion (from config.lua).
local function apply_exclusion(exclusion)

if exclusion.excluded_prototype_names then
for _,n in pairs(exclusion.excluded_prototype_names) do
excluded_prototype_names[n] = true
end
end

if exclusion.excluded_prototype_types then
for _,t in pairs(exclusion.excluded_prototype_types) do
excluded_prototype_types[t] = true
Expand All @@ -61,56 +61,63 @@ end

-- Returns a coordinate reduced where required to form the specified gap between it and the tile boundary.
local function adjust_coordinate_to_form_gap(coordinate, required_gap)

-- Treat all coordinates as positive to simplify calculations.
local is_negative_coordinate = (coordinate < 0)
if is_negative_coordinate then
coordinate = coordinate * -1
end

local tile_width = 0.5

-- Calculate the existing gap (how much space there is to the next tile edge or 0 when the coordinate lies on a tile edge).
local distance_past_last_tile_edge = coordinate % tile_width -- This is how far the collision box extends over any tile edge, and should be 0 for a perfect fit.
local existing_gap = 0
if distance_past_last_tile_edge > 0 then
existing_gap = (tile_width - distance_past_last_tile_edge)
end

-- Reduce the coordinate to make the gap large enough if it is not already.
if existing_gap < required_gap then
coordinate = coordinate + existing_gap - required_gap
if coordinate < 0 then
coordinate = 0
end
end

-- Make the coordinate negative again if it was originally negative.
if is_negative_coordinate then
coordinate = coordinate * -1
end

return coordinate
end


-- Checks all existing prototypes listed in prototype_type_gap_requirements and reduces their collision box to make a gap large enough to walk though if it is not already.
local function adjust_collision_boxes()
for prototype_type, required_gap in pairs(prototype_type_gap_requirements) do
-- Don't shrink prototypes of this type if they've been excluded.

-- Don't shrink prototypes of this type if they've been excluded.
if not prototype_type_excluded(prototype_type) then
for prototype_name, prototype in pairs(data.raw[prototype_type]) do

-- If the prototype is not excluded and has a collision box then resize it.
if (not prototype_name_excluded(prototype_name)) and prototype["collision_box"] then

for y=1,2 do
for x=1,2 do
prototype.collision_box[x][y] = adjust_coordinate_to_form_gap(prototype.collision_box[x][y], required_gap)

if prototype.collision_box.lefttop then
prototype.collision_box.lefttop[1] = adjust_coordinate_to_form_gap(prototype.collision_box.lefttop[1], required_gap)
prototype.collision_box.rightbottom[1] = adjust_coordinate_to_form_gap(prototype.collision_box.rightbottom[1], required_gap)
prototype.collision_box.lefttop[2] = adjust_coordinate_to_form_gap(prototype.collision_box.lefttop[2], required_gap)
prototype.collision_box.rightbottom[2] = adjust_coordinate_to_form_gap(prototype.collision_box.rightbottom[2], required_gap)
else
for y=1,2 do
for x=1,2 do
prototype.collision_box[x][y] = adjust_coordinate_to_form_gap(prototype.collision_box[x][y], required_gap)
end
end
end

end
end
end
Expand Down

0 comments on commit 91d8298

Please sign in to comment.