Guest User

SRP v1.1.2 trade_manager.script

a guest
Apr 26th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.18 KB | None | 0 0
  1. -- Decane 10.10.2014: Removed superfluous packet data writes and broken-in-vanilla delays to NPCs adjusting their prices (the wrong inequality sign was used). See also xr_motivator.script.
  2.  
  3. local trade_manager = {}
  4.  
  5. function trade_init(npc, cfg)
  6.  
  7.     local npc_id = npc:id()
  8.     trade_manager[npc_id] = {}
  9.     trade_manager[npc_id].cfg_ltx = cfg
  10.     trade_manager[npc_id].config = ini_file(cfg)
  11.  
  12.     local str = utils.cfg_get_string(trade_manager[npc_id].config, "trader", "buy_condition", npc, true, "")
  13.     if str == nil then
  14.         abort("Incorrect trader settings. Cannot find buy_condition. [%s]->[%s]", npc:name(), cfg)
  15.     end
  16.  
  17.     trade_manager[npc_id].buy_condition = xr_logic.parse_condlist(npc, "trade_manager", "buy_condition", str)
  18.  
  19.     str = utils.cfg_get_string(trade_manager[npc_id].config, "trader", "sell_condition", npc, true, "")
  20.     if str == nil then
  21.         abort("Incorrect trader settings. Cannot find sell_condition. [%s]->[%s]", npc:name(), cfg)
  22.     end
  23.  
  24.     trade_manager[npc_id].sell_condition = xr_logic.parse_condlist(npc, "trade_manager", "sell_condition", str)
  25.  
  26.     str = utils.cfg_get_string(trade_manager[npc_id].config, "trader", "buy_supplies", npc, false, "")
  27.     if str ~= nil then
  28.         trade_manager[npc_id].buy_supplies = xr_logic.parse_condlist(npc, "trade_manager", "buy_supplies", str)
  29.     end
  30. end
  31.  
  32. function update(npc)
  33.  
  34.     local tt = trade_manager[npc:id()]
  35.  
  36.     if tt == nil then
  37.         return
  38.     end
  39. --[[
  40.     local time_now = time_global()
  41.  
  42.     if tt.next_update_time ~= nil and time_now < tt.next_update_time then   -- the vanilla second conjunct is equivalent to 'tt.next_update_time < time_now'
  43.         return
  44.     end
  45.  
  46.     tt.next_update_time = time_now + 1000   -- the next update will be called one thousand milliseconds from now
  47.  
  48.     dbgmsg("[trade_manager.update]: Update() called for NPC with ID "..tostring(npc:id())..".")
  49. ]]
  50.     local act = db.actor
  51.     local str = xr_logic.pick_section_from_condlist(act, npc, tt.buy_condition)
  52.  
  53. --  dbgmsg("[trade_manager.update]: Current buy condition: "..tostring(tt.current_buy_condition)..".")
  54.  
  55.     if tt.current_buy_condition ~= str then
  56.         npc:buy_condition(tt.config, str)
  57.         tt.current_buy_condition = str
  58. --      dbgmsg("[trade_manager.update]: New buy condition: "..str..".")
  59.     end
  60.  
  61.     str = xr_logic.pick_section_from_condlist(act, npc, tt.sell_condition)
  62.  
  63. --  dbgmsg("[trade_manager.update]: Current sell condition: "..tostring(tt.current_sell_condition)..".")
  64.  
  65.     if tt.current_sell_condition ~= str then
  66.         npc:sell_condition(tt.config, str)
  67.         tt.current_sell_condition = str
  68. --      dbgmsg("[trade_manager.update]: New sell condition: "..str..".")
  69.     end
  70.  
  71.     if tt.buy_supplies == nil then
  72.         return
  73.     end
  74.  
  75.     str = xr_logic.pick_section_from_condlist(act, npc, tt.buy_supplies)
  76.  
  77. --  dbgmsg("[trade_manager.update]: Current supplies: "..tostring(tt.current_buy_supplies)..".")
  78.  
  79.     if tt.current_buy_supplies ~= str then
  80. --[[
  81.         local time_now = time_global()
  82.  
  83.         if tt.resupply_time ~= nil and time_now < tt.resupply_time then     -- the vanilla second conjunct is, again, written with the wrong inequality sign, so the vanilla equivalent of this block is bugged
  84.             return
  85.         end
  86.  
  87.         tt.resupply_time = time_now + 600000    -- goods are re-stocked every 10 real minutes = 10 * 60 * 1000 milliseconds = 600000 milliseconds
  88. ]]
  89.         npc:buy_supplies(tt.config, str)
  90.         tt.current_buy_supplies = str
  91.  
  92. --      dbgmsg("[trade_manager.update]: New supplies: "..str..".")
  93.     end
  94. end
  95. --[[
  96. function save(npc, packet)
  97.  
  98.     set_save_marker(packet, "save", false, "trade_manager")
  99.  
  100.     local tt = trade_manager[npc:id()]
  101.  
  102.     if tt == nil then
  103.         packet:w_bool(false)
  104.         return
  105.     else
  106.         packet:w_bool(true)
  107.     end
  108.  
  109.     packet:w_stringZ(tt.cfg_ltx)
  110.  
  111.     if tt.current_buy_condition == nil then
  112.         packet:w_stringZ("")
  113.     else
  114.         packet:w_stringZ(tt.current_buy_condition)
  115.     end
  116.  
  117.     if tt.current_sell_condition == nil then
  118.         packet:w_stringZ("")
  119.     else
  120.         packet:w_stringZ(tt.current_sell_condition)
  121.     end
  122.  
  123.     if tt.current_buy_supplies == nil then
  124.         packet:w_stringZ("")
  125.     else
  126.         packet:w_stringZ(tt.current_buy_supplies)
  127.     end
  128.  
  129.     local time_now = time_global()
  130.  
  131.     if tt.next_update_time == nil then
  132.         packet:w_s32(-1)
  133.     else
  134.         packet:w_s32(tt.next_update_time - time_now)
  135.     end
  136.  
  137.     if tt.resupply_time == nil then
  138.         packet:w_s32(-1)
  139.     else
  140.         packet:w_s32(tt.resupply_time - time_now)
  141.     end
  142.  
  143.     set_save_marker(packet, "save", true, "trade_manager")
  144. end
  145.  
  146. function load(npc, packet)
  147.  
  148.     set_save_marker(packet, "load", false, "trade_manager")
  149.  
  150.     local a = packet:r_bool()
  151.     if a == false then
  152.         return
  153.     end
  154.  
  155.     local npc_id = npc:id()
  156.  
  157.     trade_manager[npc_id] = {}
  158.  
  159.     local tt = trade_manager[npc_id]
  160.  
  161.     tt.cfg_ltx = packet:r_stringZ()
  162.     tt.config = ini_file(tt.cfg_ltx)
  163.  
  164.     a = packet:r_stringZ()
  165.     if a ~= "" then
  166.         tt.current_buy_condition = a
  167.         npc:buy_condition(tt.config, a)
  168.     end
  169.  
  170.     a = packet:r_stringZ()
  171.     if a ~= "" then
  172.         tt.current_sell_condition = a
  173.         npc:sell_condition(tt.config, a)
  174.     end
  175.  
  176.     a = packet:r_stringZ()
  177.     if a ~= "" then
  178.         tt.current_buy_supplies = a
  179.     end
  180.  
  181.     local time_now = time_global()
  182.  
  183.     a = packet:r_s32()
  184.     if a ~= -1 then
  185.         tt.next_update_time = time_now + a
  186.     end
  187.  
  188.     a = packet:r_s32()
  189.     if a ~= -1 then
  190.         tt.resupply_time = time_now + a
  191.     end
  192.  
  193.     set_save_marker(packet, "load", true, "trade_manager")
  194. end
  195. ]]
Add Comment
Please, Sign In to add comment