Skip to content

Commit

Permalink
update ic/w surface name
Browse files Browse the repository at this point in the history
  • Loading branch information
zzh8829 committed Aug 6, 2024
1 parent bf3724f commit a859ec1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
29 changes: 20 additions & 9 deletions maps/mountain_fortress_v3/ic/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ local function get_owner_car_object(player)
return false
end

local function surface_name_to_unit_number(name)
local split = name:split('_')
local unit_number = split[#split]
return tonumber(unit_number)
end

local function unit_number_to_surface_name(car_type, unit_number)
return car_type .. '_' .. unit_number
end

local function get_entity_from_player_surface(cars, player)
for _, car in pairs(cars) do
if validate_entity(car.entity) then
Expand Down Expand Up @@ -295,9 +305,9 @@ local function replace_surface(surfaces, entity, index)
for k, surface_index in pairs(surfaces) do
local surface = game.surfaces[surface_index]
local unit_number = entity.unit_number
if tostring(index.saved_entity) == surface.name then
if unit_number_to_surface_name(index.name, index.saved_entity) == surface.name then
if validate_entity(surface) then
surface.name = tostring(unit_number)
surface.name = unit_number_to_surface_name(entity.name, unit_number)
surfaces[unit_number] = surface.index
surfaces[k] = nil
end
Expand All @@ -315,7 +325,7 @@ local function replace_surface_entity(cars, entity, index)
local surface_index = car.surface
local surface = game.surfaces[surface_index]
if validate_entity(surface) then
surface.name = tostring(unit_number)
surface.name = unit_number_to_surface_name(entity.name, unit_number)
end
end
end
Expand Down Expand Up @@ -1086,9 +1096,10 @@ function Public.validate_owner(player, entity)
return false
end

function Public.create_room_surface(unit_number)
if game.surfaces[tostring(unit_number)] then
return game.surfaces[tostring(unit_number)].index
function Public.create_room_surface(ce, unit_number)
surface_name = unit_number_to_surface_name(ce.name, unit_number)
if game.surfaces[surface_name] then
return game.surfaces[surface_name].index
end

local map_gen_settings = {
Expand All @@ -1104,7 +1115,7 @@ function Public.create_room_surface(unit_number)
['decorative'] = { treat_missing_as_default = false }
}
}
local surface = game.create_surface(tostring(unit_number), map_gen_settings)
local surface = game.create_surface(surface_name, map_gen_settings)
surface.freeze_daytime = true
surface.daytime = 0.1
surface.request_to_generate_chunks({ 16, 16 }, 1)
Expand Down Expand Up @@ -1288,7 +1299,7 @@ function Public.create_car(event)

car = cars[un]

car.surface = Public.create_room_surface(un)
car.surface = Public.create_room_surface(ce, un)
Public.create_car_room(car)
render_owner_text(renders, player, ce)

Expand Down Expand Up @@ -1326,7 +1337,7 @@ function Public.remove_invalid_cars()
if not validate_entity(surface) then
return
end
if not cars[tonumber(surface.name)] then
if not cars[surface_name_to_unit_number(surface.name)] then
game.delete_surface(surface)
surfaces[k] = nil
end
Expand Down
20 changes: 16 additions & 4 deletions maps/mountain_fortress_v3/icw/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,20 @@ local function validate_entity(entity)
return true
end

local function surface_name_to_unit_number(name)
-- split by _ and get the last element
local split = name:split('_')
local unit_number = split[#split]
return tonumber(unit_number)
end

local function unit_number_to_surface_name(unit_number)
return 'train_' .. unit_number
end

local function delete_empty_surfaces(icw)
for k, surface in pairs(icw.surfaces) do
if not icw.trains[tonumber(surface.name)] then
if not icw.trains[surface_name_to_unit_number(surface.name)] then
game.delete_surface(surface)
icw.surfaces[k] = nil
end
Expand Down Expand Up @@ -554,8 +565,9 @@ function Public.kill_wagon(icw, entity)
end

function Public.create_room_surface(icw, unit_number)
if game.surfaces[tostring(unit_number)] then
return game.surfaces[tostring(unit_number)]
surface_name = unit_number_to_surface_name(unit_number)
if game.surfaces[surface_name] then
return game.surfaces[surface_name]
end
local map_gen_settings = {
['width'] = 2,
Expand All @@ -570,7 +582,7 @@ function Public.create_room_surface(icw, unit_number)
['decorative'] = {treat_missing_as_default = false}
}
}
local surface = game.create_surface(tostring(unit_number), map_gen_settings)
local surface = game.create_surface(surface_name, map_gen_settings)
surface.freeze_daytime = true
surface.daytime = 0.1
surface.request_to_generate_chunks({16, 16}, 1)
Expand Down

0 comments on commit a859ec1

Please sign in to comment.