From 8a75b66908a949852bf45848f513ff1cb8d50474 Mon Sep 17 00:00:00 2001 From: grilledham Date: Thu, 26 Jul 2018 01:00:05 +0100 Subject: [PATCH] crash site market --- map_gen/presets/crash_site.lua | 60 +++-- map_gen/presets/crash_site/market.lua | 228 ++++++++++++++++++ .../presets/crash_site/outpost_builder.lua | 8 +- .../outpost_data/small_circuit_factory.lua | 27 +-- 4 files changed, 282 insertions(+), 41 deletions(-) create mode 100644 map_gen/presets/crash_site/market.lua diff --git a/map_gen/presets/crash_site.lua b/map_gen/presets/crash_site.lua index aec82108..eb27d48d 100644 --- a/map_gen/presets/crash_site.lua +++ b/map_gen/presets/crash_site.lua @@ -45,23 +45,6 @@ local half_grid_size = grid_size * 0.5 local et = OutpostBuilder.empty_template -local base_templates = { - test = { - {[22] = {entity = {name = 'stone-furnace'}}}, - {[22] = {entity = {name = 'assembling-machine-2'}}}, - {[22] = {entity = {name = 'oil-refinery'}}} - } -} - -local templates = { - {medium_gun_turrets_player}, - {gear_factory[1]}, - {gear_factory[2]}, - {gear_factory[3]} - --{gear_factory[2]} - --{base_templates.test[1]} -} - local small_gear_factory = require 'map_gen.presets.crash_site.outpost_data.small_gear_factory' local medium_gear_factory = require 'map_gen.presets.crash_site.outpost_data.medium_gear_factory' local big_gear_factory = require 'map_gen.presets.crash_site.outpost_data.big_gear_factory' @@ -83,7 +66,7 @@ for r = 1, 100 do local row = {} pattern[r] = row for c = 1, 100 do - row[c] = outpost_builder:do_outpost(big_circuit_factory) + row[c] = outpost_builder:do_outpost(small_circuit_factory) end end @@ -92,18 +75,47 @@ outposts = b.if_else(outposts, b.full_shape) local thin_walls_player = OutpostBuilder.extend_walls(thin_walls, {force = 'player'}) +local market = { + callback = outpost_builder.market_set_items_callback, + data = { + { + name = 'copper-cable', + price = 50 / 200, + distance_factor = 1 / 200 / 32, + min_price = 5 / 200 + }, + { + name = 'electronic-circuit', + price = 200 / 200, + distance_factor = 1 / 200 / 32, + min_price = 10 / 200 + }, + { + name = 'advanced-circuit', + price = 2000 / 200, + distance_factor = 1 / 200 / 32, + min_price = 100 / 200 + } + } +} + +--[[ for i = 4, 1000 do + market.data[i] = market.data[1] +end ]] + local outpost = outpost_builder.to_shape( { - size = 2, - laser_turrets_player[1][1], - gear_factory[1], - gear_factory[2] + size = 1, + { + market = market, + [15] = {entity = {name = 'market', callback = 'market'}} + } } ) local map = b.change_tile(outposts, true, 'grass-1') --return b.full_shape -return map ---return outpost +--return map +return outpost diff --git a/map_gen/presets/crash_site/market.lua b/map_gen/presets/crash_site/market.lua new file mode 100644 index 00000000..88f2b217 --- /dev/null +++ b/map_gen/presets/crash_site/market.lua @@ -0,0 +1,228 @@ +local Gui = require 'utils.gui' +local Event = require 'utils.event' +local Global = require 'utils.global' + +local Public = {} + +local markets = {} + +Global.register( + {markets = markets}, + function(tbl) + markets = tbl.markets + end +) + +local market_frame_name = Gui.uid_name() +local item_button_name = Gui.uid_name() +local count_slider_name = Gui.uid_name() +local count_text_name = Gui.uid_name() + +local function redraw_market_items(data) + local grid = data.grid + + Gui.clear(grid) + + local count = data.count + local market_items = data.market_items + + for i, item in ipairs(market_items) do + local name = item.name + local price = item.price + + local price_per_item = string.format('%.2f', price) + + local button = + grid.add({type = 'flow'}).add { + type = 'sprite-button', + name = item_button_name, + sprite = 'item/' .. name, + number = count, + tooltip = table.concat {name, ' price: ', price_per_item} + } + button.style = 'slot_button' + + Gui.set_data(button, {index = i, data = data}) + + local message = math.ceil(price * count) + if message == 1 then + message = message .. ' coin' + else + message = message .. ' coins' + end + + local label = grid.add {type = 'label', caption = message} + local label_style = label.style + label_style.width = 80 + label_style.font = 'default-bold' + end +end + +local function do_coin_label(player, label) + local coin_count = player.get_item_count('coin') + + if coin_count == 1 then + label.caption = coin_count .. ' coin available' + else + label.caption = coin_count .. ' coins available' + end +end + +local function draw_market_frame(player, market_items) + local frame = + player.gui.center.add {type = 'frame', name = market_frame_name, caption = 'Market', direction = 'vertical'} + + local scroll_pane = frame.add {type = 'scroll-pane'} + local scroll_style = scroll_pane.style + scroll_style.maximal_height = 600 + + local grid = scroll_pane.add {type = 'table', column_count = 10} + + local data = { + grid = grid, + count = 1, + market_items = market_items + } + + redraw_market_items(data) + + local coin_label = frame.add {type = 'label'} + do_coin_label(player, coin_label) + coin_label.style.font = 'default-bold' + data.coin_label = coin_label + + local count_flow = frame.add {type = 'flow'} + + local count_slider = + count_flow.add {type = 'slider', name = count_slider_name, minimum_value = 1, maximum_value = 7, value = 1} + local count_text = count_flow.add {type = 'text-box', name = count_text_name, text = '1'} + + count_slider.style.width = 100 + count_text.style.width = 60 + + data.slider = count_slider + data.text = count_text + + Gui.set_data(count_slider, data) + Gui.set_data(count_text, data) + + return frame +end + +local function gui_opened(event) + if not event.gui_type == defines.gui_type.entity then + return + end + + local player = game.players[event.player_index] + if not player or not player.valid then + return + end + + local entity = event.entity + if not entity or not entity.valid then + return + end + + local pos = entity.position + local market_data = markets[pos.x .. ',' .. pos.y] + if not market_data then + return + end + + local frame = draw_market_frame(player, market_data) + + player.opened = frame +end + +function Public.add_market(position, data) + markets[position.x .. ',' .. position.y] = data +end + +Event.add(defines.events.on_gui_opened, gui_opened) + +Gui.on_custom_close( + market_frame_name, + function(event) + local element = event.element + Gui.destroy(element) + end +) + +Gui.on_value_changed( + count_slider_name, + function(event) + local element = event.element + local data = Gui.get_data(element) + + local value = math.floor(element.slider_value) + local count + if value % 2 == 0 then + count = 10 ^ (value * 0.5) * 0.5 + else + count = 10 ^ ((value - 1) * 0.5) + end + + data.count = count + data.text.text = count + + redraw_market_items(data) + end +) + +Gui.on_text_changed( + count_text_name, + function(event) + local element = event.element + local data = Gui.get_data(element) + + local count = tonumber(element.text) + + if count then + count = math.floor(count) + count = math.clamp(count, 1, 1000) + data.count = count + data.text.text = count + else + data.count = 1 + end + + redraw_market_items(data) + end +) + +Gui.on_click( + item_button_name, + function(event) + local player = event.player + local element = event.element + local button_data = Gui.get_data(element) + local data = button_data.data + + local item = data.market_items[button_data.index] + + local name = item.name + local price = item.price + local count = data.count + + local cost = math.ceil(price * count) + local coin_count = player.get_item_count('coin') + + if cost > coin_count then + player.print('Insufficient coins') + else + local inserted = player.insert {name = name, count = count} + if inserted < count then + player.print('Insufficient inventory space') + if inserted > 0 then + player.remove_item {name = name, count = inserted} + end + else + player.remove_item {name = 'coin', count = cost} + do_coin_label(player, data.coin_label) + end + end + end +) + +return Public diff --git a/map_gen/presets/crash_site/outpost_builder.lua b/map_gen/presets/crash_site/outpost_builder.lua index 3b86a6b8..aac4f5ea 100644 --- a/map_gen/presets/crash_site/outpost_builder.lua +++ b/map_gen/presets/crash_site/outpost_builder.lua @@ -3,6 +3,7 @@ local Token = require 'utils.global_token' local Global = require 'utils.global' local Event = require 'utils.event' local Task = require 'utils.Task' +local Market = require 'map_gen.presets.crash_site.market' local b = require 'map_gen.shared.builders' @@ -1045,7 +1046,8 @@ Public.market_set_items_callback = local x, y = p.x, p.y local d = math.sqrt(x * x + y * y) - for _, item in ipairs(data) do + local market_data = {} + for i, item in ipairs(data) do local price = item.price local df = item.distance_factor or 0 local min_price = item.min_price or 1 @@ -1053,8 +1055,10 @@ Public.market_set_items_callback = local count = price - d * df count = math.max(count, min_price) - entity.add_market_item({price = {{item.name, count}}, offer = item.offer}) + market_data[i] = {name = item.name, price = count} end + + Market.add_market(entity.position, market_data) end ) diff --git a/map_gen/presets/crash_site/outpost_data/small_circuit_factory.lua b/map_gen/presets/crash_site/outpost_data/small_circuit_factory.lua index 28c30f72..f7096d71 100644 --- a/map_gen/presets/crash_site/outpost_data/small_circuit_factory.lua +++ b/map_gen/presets/crash_site/outpost_data/small_circuit_factory.lua @@ -23,25 +23,22 @@ local market = { callback = ob.market_set_items_callback, data = { { - offer = {type = 'give-item', item = 'copper-cable', count = 200}, - name = 'coin', - price = 50, - distance_factor = 1 / 32, - min_price = 5 + name = 'copper-cable', + price = 50 / 200, + distance_factor = 1 / 200 / 32, + min_price = 5 / 200 }, { - offer = {type = 'give-item', item = 'electronic-circuit', count = 200}, - name = 'coin', - price = 200, - distance_factor = 1 / 32, - min_price = 10 + name = 'electronic-circuit', + price = 200 / 200, + distance_factor = 1 / 200 / 32, + min_price = 10 / 200 }, { - offer = {type = 'give-item', item = 'advanced-circuit', count = 200}, - name = 'coin', - price = 2000, - distance_factor = 1 / 32, - min_price = 100 + name = 'advanced-circuit', + price = 2000 / 200, + distance_factor = 1 / 200 / 32, + min_price = 100 / 200 } } }