z_mirror/Krastorio2

git clone https://git.raiguard.me/z_mirror/Krastorio2

Caleb HeuerChange to four-space indentation07409ca6

four-space-indentation
3.1 KiB99 linesraw
1local flib_bounding_box = require("__flib__.bounding-box")
2local flib_position = require("__flib__.position")
3
4local util = require("scripts.util")
5
6local max_reach = 15
7
8--- @param e EventData.on_player_selected_area
9local function on_player_selected_area(e)
10    if e.item ~= "kr-jackhammer" then
11        return
12    end
13
14    local player = game.get_player(e.player_index)
15    if not player then
16        return
17    end
18
19    local player_pos = player.position
20
21    local items_to_give = {}
22    local tiles_to_set = {}
23    local i = 0
24    for _, tile in pairs(e.tiles) do
25        if flib_position.distance(tile.position, player_pos) > max_reach then
26            goto continue
27        end
28
29        local mineable = tile.prototype.mineable_properties
30        local products = mineable.products
31        if not mineable or not products or not mineable.minable then
32            goto continue
33        end
34
35        i = i + 1
36        tiles_to_set[i] = { name = tile.hidden_tile or "landfill", position = tile.position }
37        for _, product_ident in pairs(products) do
38            local probability = product_ident.probability
39            if product_ident.type == "item" and not probability or math.random() <= probability then
40                local amount = product_ident.amount
41                if not amount then
42                    amount = math.random(product_ident.amount_min, product_ident.amount_max)
43                end
44                local name = product_ident.name
45                items_to_give[name] = (items_to_give[name] or 0) + amount
46            end
47        end
48
49        ::continue::
50    end
51
52    if i > 0 then
53        -- Check if we can insert all of the items
54        local inventory = player.get_main_inventory()
55        if not inventory then
56            return
57        end
58        for name, count in pairs(items_to_give) do
59            -- If we can't fit all of the items, print a message and don't take any tiles
60            if not inventory.can_insert({ name = name, count = count }) then
61                util.flying_text_with_sound(
62                    player,
63                    { "message.kr-inventory-is-full" },
64                    { position = flib_bounding_box.center(e.area) }
65                )
66                return
67            end
68        end
69
70        -- Insert all of the items
71        local inserted_count = 0
72        for name, count in pairs(items_to_give) do
73            inventory.insert({ name = name, count = count })
74            inserted_count = inserted_count + count
75        end
76
77        -- Set the tiles
78        e.surface.set_tiles(tiles_to_set)
79
80        -- FX
81        player.play_sound({ path = "kr-jackhammer", volume_modifier = 1 })
82    else
83        util.flying_text_with_sound(
84            player,
85            { "message.kr-no-tiles-in-selection" },
86            { position = flib_bounding_box.center(e.area) }
87        )
88    end
89end
90
91--- @class JackhammerHandler : event_handler
92local jackhammer = {}
93
94jackhammer.events = {
95    [defines.events.on_player_selected_area] = on_player_selected_area,
96    [defines.events.on_player_alt_selected_area] = on_player_selected_area,
97}
98
99return jackhammer