z_mirror/CursorEnhancements

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

raiguardCleanup326e445

proxies
4.4 KiB131 linesraw
1local cursor = {}
2
3local event = require("__flib__.event")
4
5local player_data = require("scripts.player-data")
6
7function cursor.check_stack(cursor_stack, cursor_ghost)
8  if cursor_stack and cursor_stack.valid_for_read then
9    return cursor_stack.name
10  elseif cursor_ghost then
11    return cursor_ghost.name
12  else
13    return
14  end
15end
16
17function cursor.set_stack(player, cursor_stack, player_table, item_name)
18  local is_cheating = player.cheat_mode or (player.controller_type == defines.controllers.editor)
19  local spawn_item = player_table.settings.spawn_items_when_cheating
20
21  -- space exploration - don't spawn items when in the satellite view
22  if script.active_mods["space-exploration"] and player.controller_type == defines.controllers.god then
23    spawn_item = false
24  end
25
26  -- main inventory
27  player_data.ensure_valid_inventory(player, player_table)
28  local main_inventory = player_table.main_inventory
29
30  -- set cursor stack
31  local item_stack, item_stack_index = main_inventory.find_item_stack(item_name)
32  if item_stack and item_stack.valid then
33    if player.clean_cursor() then
34      -- actually transfer from the main inventory, then set the hand location
35      cursor_stack.transfer_stack(item_stack)
36      player.hand_location = {inventory=main_inventory.index, slot=item_stack_index}
37    end
38    return true
39  elseif spawn_item and is_cheating then
40    local stack_spec = {name=item_name, count=game.item_prototypes[item_name].stack_size}
41    -- insert into main inventory first, then transfer and set the hand location
42    if main_inventory.can_insert(stack_spec) and player.clean_cursor() then
43      main_inventory.insert(stack_spec)
44      local new_stack, new_stack_index = main_inventory.find_item_stack(item_name)
45      cursor_stack.transfer_stack(new_stack)
46      player.hand_location = {inventory=main_inventory.index, slot=new_stack_index}
47    else
48      player.print{"cen-message.main-inventory-full"}
49    end
50    return true
51  elseif player.clean_cursor() then
52    player_table.last_item = item_name
53    player.cursor_ghost = item_name
54    return true
55  end
56end
57
58function cursor.scroll(player_index, direction)
59  local player = game.get_player(player_index)
60  local player_table = global.players[player_index]
61  local cursor_stack = player.cursor_stack
62  local current_item = cursor.check_stack(cursor_stack, player.cursor_ghost)
63
64  if current_item then
65    -- check personal registry, then the global registry
66    local registry = player_table.registry[current_item]
67    if not registry or not registry[direction] then
68      registry = global.registry[current_item]
69    end
70    -- set stack
71    if registry and registry[direction] then
72      cursor.set_stack(player, cursor_stack, player_table, registry[direction])
73    end
74  end
75end
76
77function cursor.create_proxy(player, selected, item)
78  -- pass through request proxy
79  if selected.type == "item-request-proxy" then
80    selected = selected.proxy_target
81    if not selected then return end
82  end
83  -- item info
84  local prototype = game.item_prototypes[item]
85  local item_name = prototype.localised_name
86  local stack_size = prototype.stack_size
87  -- flying text content
88  local text
89  -- proxy
90  local proxy = global.proxies.by_target[selected.unit_number]
91
92  -- create / don't create proxy
93  if selected.can_insert{name=item} then
94    if proxy then
95      -- update proxy's request count
96      local requests = proxy.item_requests
97      local request_count = stack_size + (requests[item] or 0)
98      proxy.item_requests = {table.unpack(requests), [item]=request_count}
99      -- flying text
100      text = {"cen-message.updated-request-count", request_count, item_name}
101    else
102      -- create the proxy
103      proxy = selected.surface.create_entity{
104        name = "item-request-proxy",
105        position = selected.position,
106        force = selected.force,
107        target = selected,
108        modules = {[item]=stack_size}
109      }
110      -- store global references
111      global.proxies.by_target[selected.unit_number] = proxy
112      global.proxies.by_proxy[proxy.unit_number] = selected.unit_number
113      -- register to on_entity_destroyed
114      event.register_on_entity_destroyed(proxy)
115      -- flying text
116      text = {"cen-message.requesting-from-network", stack_size, item_name}
117    end
118  else
119    -- flying text
120    text = {"cen-message.cannot-request", item_name}
121    -- play sound
122    player.play_sound{path="utility/cannot_build"}
123  end
124  -- create flying text
125  player.create_local_flying_text{
126    text = text,
127    position = selected.position,
128  }
129end
130
131return cursor