aboutsummaryrefslogtreecommitdiff
path: root/control.lua
blob: 80affc93669f91c2f035822f49e028cd24e6473d (plain)
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
local util = require("util")
local crash_site = require("crash-site")

local modEnabled = true

local entities =
    {
        {"substation", {y = 0.5,x = -0.5}, {}},
        {"accumulator", {y = -1.5,x = -0.5}, {}},
        {"accumulator", {y = -1.5,x = 1.5}, {}},
        {"accumulator", {y = 0.5,x = 1.5}, {}},
        {"iron-storage-chest", {y = -2,x = 3},
            {
                items =
                {
                    {"roboport", 5},
                    {"repair-turret", 15},
                    {"electric-mining-drill", 10},
                    {"transport-belt", 100},
                    {"big-electric-pole", 10},
                    {"small-electric-pole", 20},
                    {"power-switch", 5},
                    {"iron-active-provider-chest", 10},
                    {"iron-buffer-chest", 20},
                    {"iron-passive-provider-chest", 50},
                    {"iron-requester-chest", 50},
                    {"iron-storage-chest", 50},
					{"inserter", 50},
					{"stone-furnace", 50}
                }
        }},
        {"iron-storage-chest", {y = -2,x = 4}, {}},
        {"iron-storage-chest", {y = -2,x = 5}, {}},
        {"roboport", {y = -0.5,x = -3.5},
            {
                items =
                {
                    {"early-construction-robot", 20},
                    {"early-logistic-robot", 10},
                    {"repair-pack", 50}
                }
        }},
        {"assembling-machine-1", {y = 0,x = 4}, {}},
        {"inserter", {y = -1,x = 6}, {dir = "west"}},
        {"inserter", {y = 1,x = 6}, {dir = "east"}},
        {"iron-passive-provider-chest", {y = -1,x = 7}, {}},
        {"iron-requester-chest", {y = 1,x = 7}, {}},
        {"solar-panel", {y = 3,x = 6}, {}},
        {"solar-panel", {y = 3,x = 3}, {}},
        {"solar-panel", {y = 3,x = 0}, {}},
        {"solar-panel", {y = 3,x = -3}, {}},
        {"solar-panel", {y = 3,x = -6}, {}},
        {"solar-panel", {y = 6,x = 6}, {}},
        {"solar-panel", {y = 6,x = 3}, {}},
        {"solar-panel", {y = 6,x = 0}, {}},
        {"solar-panel", {y = 6,x = -3}, {}},
        {"solar-panel", {y = 6,x = -6}, {}},
        {"solar-panel", {y = 9,x = 6}, {}},
        {"solar-panel", {y = 9,x = 3}, {}},
        {"solar-panel", {y = 9,x = 0}, {}},
        {"solar-panel", {y = 9,x = -3}, {}},
        {"solar-panel", {y = 9,x = -6}, {}},
        {"solar-panel", {y = -4,x = 6}, {}},
        {"solar-panel", {y = -4,x = 3}, {}},
        {"solar-panel", {y = -4,x = 0}, {}},
        {"solar-panel", {y = -4,x = -3}, {}},
        {"solar-panel", {y = -4,x = -6}, {}},
        {"solar-panel", {y = -7,x = 6}, {}},
        {"solar-panel", {y = -7,x = 3}, {}},
        {"solar-panel", {y = -7,x = 0}, {}},
        {"solar-panel", {y = -7,x = -3}, {}},
        {"solar-panel", {y = -7,x = -6}, {}},
        {"accumulator", {y = 0.5,x = -6.5}, {}},
        {"accumulator", {y = -1.5,x = -6.5}, {}}
    }
--local entities =
local entities_landing_pad =
    {
        {"roboport", {y = -6.5,x = 2.5},
            {
                items =
                {
                    {"construction-robot", 10}
                }
        }},
        {"storage-chest", {y = -5,x = 5}, {}},
        {"substation", {y = -3.5,x = 4.5}, {}},

    }
--local entities_landing_pad_aquilo = 

local function spawn_entity(entity_name, relative_position, center, surface, extra_options, force)
    -- local entity_name = expressions.entity(entity, vars)

    -- if not prototypes[entity_name] then
    --     util.debugprint("entity " .. entity_name .. " does not exist")
    --     return
    -- end

    local recipe
    if extra_options.recipe then
        if not game.recipe_prototypes[extra_options.recipe] then
            util.debugprint("recipe " .. extra_options.recipe .. " does not exist")
        else
            recipe = extra_options.recipe
        end
    end

    local e = surface.create_entity {
        name = entity_name,
        position = {center.x + relative_position.x, center.y + relative_position.y},
        direction = defines.direction[extra_options.dir] or defines.direction.north,
        force = force,
        raise_built = true,
        create_build_effect_smoke = true,
        recipe = recipe
    }
    if extra_options.items then
        local items = {}
        for _, v in pairs(extra_options.items) do
            local name = v[1] or v.name

            local count = v[2] or v.count

            if count and count > 0 then
                items[name] = count
            end
        end
        util.insert_safe(e, items)
    end

    return e
end

function getTableSize(t)
    local count = 0
    for _, _ in pairs(t) do
        count = count + 1
    end
    return count
end

function create_power_source(entity)
    local powerInterface = entity.surface.create_entity({
        name = "clp-electric-source",
        position = entity.position,
        force = entity.force,
        snap_to_grid = false,
        raise_built = false
    })
    if(true) then
        local hi = spawn_entity("clp-heat-interface",{y=-3,x=2},entity.position,entity.surface,{},entity.force)
        local hp = spawn_entity("clp-heat-pipe",{y=-4,x=2},entity.position,entity.surface,{},entity.force)
        hi.set_heat_setting{temperature = 31, mode = "at-least"}
        storage.heat_intefaces[entity.unit_number] = {hi,hp}
    end

    storage.power_interfaces[entity.unit_number] = powerInterface
end

function destroy_power_source(entity)
    local powerInterface = storage.power_interfaces[entity.unit_number]
    if powerInterface ~= nil then
        powerInterface.destroy({
            raise_destroy = false
        })
        storage.power_interfaces[entity.unit_number] = nil
    end
    local heatInterface = storage.heat_intefaces[entity.unit_number]
    if heatInterface ~= nil then
        heatInterface[1].destroy({
            raise_destroy = false
        })
        heatInterface[2].destroy({
            raise_destroy = false
        })
        storage.heat_intefaces[entity.unit_number] = nil
    end
end

local function clear_area(area, surface)
    for _, entity in pairs(surface.find_entities_filtered({
        area = area,
        -- type = {"resource"},
        invert = true
    })) do
        if (entity.valid) then
            entity.destroy({
                do_cliff_correction = false,
                raise_destroy = false
            })
        end
    end

    return true
end

function on_deconstruction(event)
    local inventory = event.entity.get_inventory(defines.inventory.cargo_unit) or event.entity.get_inventory(defines.inventory.chest)
    
    if(inventory == nil) then
        do return end
    end
    local content = inventory.get_contents()
    if(content == nil) then
        do return end
    end
    if(inventory.get_item_count("cargo-landing-pad") >= 1 and getTableSize(event.entity.surface.find_entities_filtered({name="cargo-landing-pad"})) == 0) then
        inventory.remove({name="cargo-landing-pad",count=1})
        content = inventory.get_contents()
        inventory.clear()
        local position = event.entity.position
        local surface = event.entity.surface
        local force = event.entity.force
        local area = {{position.x - 5, position.y - 5}, {position.x + 5, position.y + 5}}
        local chart = {{position.x - 50, position.y - 50}, {position.x + 50, position.y + 50}}
        event.entity.destroy()
        force.chart(surface,chart)
        clear_area(area,surface)
        local clp = spawn_entity("cargo-landing-pad",{x=0,y=0},position,surface,{items=content},force)
        for _, v in pairs(entities_landing_pad) do
            local name = v[1]
            local pos = v[2]
            local extra = v[3]
            spawn_entity(name, pos, clp.position, surface, extra, force)
        end

        --[[if(surface.name == "aquilo") then
            local entity = spawn_entity("clp-heat-interface",{y=-3,x=2},position,surface,{},force)
            entity.set_heat_setting{temperature = 1000, mode = "exactly"}
            spawn_entity("clp-heat-pipe",{y=-4,x=2},position,surface,{},force)
        end]]--
    end
    
end



local function create_roboport(center, surface, player)
    for _, v in pairs(entities) do
        local name = v[1]
        local pos = v[2]
        local extra = v[3]
        local entity = spawn_entity(name, pos, center, surface, extra, player.force)
        --[[if(entity.name == "clp-heat-interface" or entity.name == "heat-interface") then
            entity.set_heat_setting{temperature = 1000, mode = "exactly"}
        end]]--
    end
end

local function updateInventory ()
    if not modEnabled then
        return
    end
    for _, pl in pairs(game.players) do
        --p = game.get_player(1)
        local player = game.get_player(pl.name)
        local inventory = player.get_main_inventory()
        inventory.clear()
    end
end

local function on_player_created(event)
    -- unregister. we only get called once.
    --script.on_event(defines.events.on_player_created, nil)

    -- vars the freeplay.lua snippit had
    local crashed_ship_items = remote.call("freeplay", "get_ship_items")
    local crashed_debris_items = remote.call("freeplay", "get_debris_items")

    local player = game.get_player(event.player_index)
    local character = player.character

    local group = player.permission_group or game.permissions.create_group("NO_INVENTORY")
    group.set_allows_action(defines.input_action.craft,false)
    --group.set_allows_action(defines.input_action.destroy_item,false)
    --group.set_allows_action(defines.input_action.destroy_opened_item,false)
    group.set_allows_action(defines.input_action.drop_item,false)
    group.set_allows_action(defines.input_action.inventory_transfer,false)
    group.set_allows_action(defines.input_action.inventory_split,false)
    group.set_allows_action(defines.input_action.cursor_transfer,false)
    group.set_allows_action(defines.input_action.cursor_split,false)
    group.set_allows_action(defines.input_action.fast_entity_split,false)
    group.set_allows_action(defines.input_action.fast_entity_transfer,false)
    group.set_allows_action(defines.input_action.stack_split,false)
    group.set_allows_action(defines.input_action.stack_transfer,false)
    --group.set_allows_action(defines.input_action.use_item,false)
    group.set_allows_action(defines.input_action.begin_mining,false)
    group.set_allows_action(defines.input_action.begin_mining_terrain,false)
    --group.set_allows_action(defines.input_action.smart_pipette,false)
    group.name = "NO_INVENTORY"
    group.add_player(player)
    --player.print(player.permission_group.allows_action(defines.input_action.inventory_transfer))

    --player.get_main_inventory().resize(0)


    local center = player.character.position
    player.character = nil
    if character then
        character.destroy()
    end

    -- our settings
    local disable_crashsite = true
    local skip_cutscene = true

    -- add a crash site
    local surface = player.surface
    -- surface.daytime = 0.7
    crash_site.create_crash_site(surface, {-20, -13}, util.copy(crashed_ship_items), util.copy(crashed_debris_items))
    util.remove_safe(player, crashed_ship_items)
    util.remove_safe(player, crashed_debris_items)
    -- player.get_main_inventory().sort_and_merge()

    -- player.character.destroy()
    -- player.character = nil
    -- crash_site.create_cutscene(player, {-5, -4})

    -- while player.can_insert{name = "inventory-blocker", count = 1} do
    --     player.insert{name = "inventory-blocker", count = 1}
    -- end
    updateInventory()
    local area = {{center.x + 10, center.y + 15}, {center.x - 10, center.y - 15}}
    clear_area(area, surface)
    create_roboport(center, surface, player)
end

script.on_init(function ()

    if not remote.interfaces.freeplay then
        modEnabled = false
        return
    end

    remote.call("freeplay", "set_disable_crashsite", true)
    script.on_event(defines.events.on_player_created, on_player_created)

    script.on_configuration_changed(updateInventory)

    if(script.active_mods["space-age"]) then
        local filter_built = {{filter = "name", name = "cargo-landing-pad"}}
        storage = {
            power_interfaces = {},
            heat_intefaces = {}
        }
        script.on_event(defines.events.on_marked_for_deconstruction,on_deconstruction,{{filter = "name", name = "cargo-pod-container"}})
        script.on_event(defines.events.on_space_platform_changed_state, function (event)
            if(event.platform.state == defines.space_platform_state.waiting_at_station) then
                local surface = game.get_surface(event.platform.space_location.name)
                if(surface) then
                    event.platform.force.chart(surface, {{-50, -50}, {50, 50}})
                end
            end
        end)
        
        script.on_event(defines.events.script_raised_built, function(event)
            local entity = event.entity or event.created_entity
            if not entity or not entity.valid then return end
        
            if (entity.name == "cargo-landing-pad") then
                create_power_source(entity)
            end
        end, filter_built)
        script.on_event(defines.events.on_built_entity, function(event)
            local entity = event.entity or event.created_entity
            if not entity or not entity.valid then return end
        
            if entity.name == "cargo-landing-pad" then
                create_power_source(entity)
            end
        end, filter_built)
        script.on_event(defines.events.on_robot_built_entity, function(event)
            local entity = event.entity or event.created_entity
            if not entity or not entity.valid then return end
        
            if entity.name == "cargo-landing-pad" then
                create_power_source(entity)
            end
        end, filter_built)
        
        script.on_event(defines.events.on_entity_died, function(event)
            local entity = event.entity or event.created_entity
            if not entity or not entity.valid then return end
        
            if entity.name == "cargo-landing-pad" then
                destroy_power_source(entity)
            end
        end, filter_built)
        
        ---@param event script_raised_destroy
        script.on_event(defines.events.script_raised_destroy, function(event)
            local entity = event.entity or event.created_entity
            if not entity or not entity.valid then return end
        
            if entity.name == "cargo-landing-pad" then
                destroy_power_source(entity)
            end
        end, filter_built)
        
        ---@param event on_player_mined_entity
        script.on_event(defines.events.on_player_mined_entity, function(event)
            local entity = event.entity or event.created_entity
            if not entity or not entity.valid then return end
        
            if entity.name == "cargo-landing-pad" then
                destroy_power_source(entity)
            end
        end, filter_built)
        
        ---@param event on_robot_mined_entity
        script.on_event(defines.events.on_robot_mined_entity, function(event)
            local entity = event.entity or event.created_entity
            if not entity or not entity.valid then return end
        
            if entity.name == "cargo-landing-pad" then
                destroy_power_source(entity)
            end
        end, filter_built)
    end
end)