Advertisement
Xetrill

inventory_upgrades.script

Aug 30th, 2013
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.99 KB | None | 0 0
  1. --[[-----------------------------------------------------------------------\\--
  2.     :: PRIVATE / LOCAL ::
  3. --\\-----------------------------------------------------------------------]]--
  4.  
  5. xs_extend.export()
  6.  
  7. local tonumber, tostring, sprintf, mfloor, tinsert, tconcat, tremove, mmin
  8.     = tonumber, tostring, string.format, math.floor, table.insert, table.concat, table.remove, math.min
  9. local localize, alife, system_ini, ini_file
  10.     = game.translate_string, alife, system_ini, ini_file
  11. local xs_find
  12.     = xs_utils.find
  13.  
  14. local issue_condlist = true
  15. local mechanic_name = ""
  16. local upgr_ini  = ini_file('item_upgrades.ltx')
  17. local param_ini = ini_file([[misc\stalkers_upgarde_info.ltx]])
  18.  
  19. local pfaCache, pfaBuffer = xs_utils.toWeakTable({}, 'kv')
  20. local pfaMap, qriMap, gtsMap = {}, {}, {}
  21.  
  22. local function how_much_repair(item_name, item_condition)
  23.     local sys  = system_ini()
  24.     local coef = (sys:r_string(item_name, 'class') == 'E_STLK') and 0.5 or 0.8
  25.     return mfloor(sys:r_u32(item_name, 'cost') * (1 - item_condition) * coef)
  26. end
  27.  
  28. local function pfaGetBuffer()
  29.     if pfaBuffer == nil then
  30.         pfaBuffer = require('buffer').new()
  31.     else
  32.         pfaBuffer:len(0)
  33.     end
  34.     return pfaBuffer
  35. end
  36.  
  37. local function pfaCache_Put(id, result)
  38.     if #pfaCache == 10 then
  39.         tremove(pfaCache, 1)
  40.     end
  41.     tinsert(pfaCache, { [id] = result })
  42.     return result
  43. end
  44.  
  45. local pfaCache_Next = 1
  46. local function pfaCache_Get(id)
  47.     if pfaCache_Next > #pfaCache then
  48.         pfaCache_Next = 1
  49.     end
  50.     for i = pfaCache_Next, #pfaCache do
  51.         if pfaCache[i][id] ~= nil then
  52.             pfaCache_Next = i + 1
  53.             return pfaCache[i][id]
  54.         end
  55.     end
  56.     for i = 1, #pfaCache do
  57.         if pfaCache[i][id] ~= nil then
  58.             pfaCache_Next = i + 1 > #pfaCache and 1 or i + 1
  59.             return pfaCache[i][id]
  60.         end
  61.     end
  62.     return nil
  63. end
  64.  
  65. local function issue_property(param1, name)
  66.     local stPropName  = localize(upgr_ini:r_string(name, 'name'))
  67.     local sectionList = utils.parse_names(param1)
  68.  
  69.     local value = sectionList[1] ~= nil and upgr_ini:sr_string(sectionList[1], 'value')
  70.     if type(value) == 'string' then
  71.         return stPropName..' '..value:sub(2, -2)
  72.     else
  73.         return stPropName
  74.     end
  75. end
  76.  
  77. --[[-----------------------------------------------------------------------\\--
  78.     :: PUBLIC / GLOBAL / EXPORTED ::
  79. --\\-----------------------------------------------------------------------]]--
  80.  
  81. function precondition_functor_a(param1, section)
  82.     if not upgr_ini:line_exist(section, 'cost') then
  83.         abort("[inventory_upgrades.precondition_functor_a]: No entry 'cost' in section '%s'.", section)
  84.     end
  85.  
  86.     issue_condlist = true
  87.  
  88.     local act = db.actor
  89.     if not act then
  90.         return 0
  91.     end
  92.  
  93.     local upgradeSection = mechanic_name .. '_upgr'
  94.     if param_ini:line_exist(upgradeSection, section) then
  95.         local param = param_ini:r_string(upgradeSection, section)
  96.         if param == nil then
  97.             return 0
  98.         elseif param == "false" then
  99.             issue_condlist = false
  100.             return 2
  101.         end
  102.  
  103.         -- TODO: rename 't' based on what xr_logic returns...
  104.         local t = xr_logic.parse_condlist(victim, upgradeSection, section, param)
  105.         if not xr_logic.pick_section_from_condlist(act, victim, t) then
  106.             issue_condlist = false
  107.             return 2
  108.         end
  109.     end
  110.  
  111.     if act:money() < upgr_ini:r_u32(section, 'cost') then
  112.         return 1
  113.     end
  114.  
  115.     return 0
  116. end
  117.  
  118. function effect_functor_a(param2, section, loading) --( string, string, int )
  119.     if loading == 0 then
  120.         local cost = upgr_ini:r_u32(section, 'cost')
  121.         xr_statistic.inc_spent_money_counter(cost)
  122.         db.actor:give_money(-cost)
  123.     end
  124. end
  125.  
  126. function prereq_functor_a(param3, section)
  127.     if issue_condlist and not db.actor then
  128.         return ' '
  129.     end
  130.  
  131.     if pfaMap['N/A'] == nil then
  132.         local stCost = localize('st_upgr_cost')
  133.         pfaMap[true]  = sprintf('%s: %%d RU', stCost)
  134.         pfaMap[false] = sprintf('%s: %%d RU\\n%s: %%d RU', stCost, localize('ui_inv_not_enought_money'))
  135.         pfaMap['N/A'] = localize('st_upgr_condlist')
  136.     end
  137.  
  138.     if issue_condlist then
  139.         local cost = upgr_ini:r_u32(section, 'cost')
  140.         local cash = db.actor:money()
  141.         return pfaMap[cash >= cost]:format(cost, cost - cash)
  142.     else
  143.         return pfaMap['N/A']
  144.     end
  145. end
  146.  
  147. function prereq_tooltip_functor_a(param3)
  148.     return true
  149. end
  150.  
  151. -- Creates a human-readable description of an upgrade. Used for installed as well as potential
  152. -- upgrades.
  153. -- @param param1   (string: comma separated list of section names) List of 0-n names, where each
  154. --  name refers to a specific upgrade section of an specific item.
  155. -- @param property (string) The name of the property in question.
  156. --     Refers to a section in upgrades_properties.ltx.
  157. --
  158. -- Example input: param1 = 'up_sect_h_lr300, up_sect_c_lr300', property = 'prop_recoil'
  159. function property_functor_a(param1, property)
  160.     if param1 == nil or param1 == '' then -- happens during game init/loading
  161.         return ''
  162.     end
  163.  
  164.     local cacheId      = param1 .. property
  165.     local cachedResult = pfaCache_Get(cacheId)
  166.     if cachedResult ~= nil then
  167.         return cachedResult
  168.     end
  169.  
  170.     -- local sectionList = xs_map(xs_split(',', param1), xs_trim, ipairs)
  171.     local sectionList = utils.parse_names(param1)
  172.     if #sectionList == 0 then
  173.         return pfaCache_Put(cacheId, '')
  174.     end
  175.  
  176.     local stProperty = localize(upgr_ini:r_string(property, 'name'))
  177.     if #sectionList == 1 then
  178.         local value1 = upgr_ini:sr_string(sectionList[1], 'value') or ''
  179.         if value1 == '' and property ~= 'prop_armor' then
  180.             return pfaCache_Put(cacheId, stProperty)
  181.         end
  182.  
  183.         local b = pfaGetBuffer()
  184.         b:append(stProperty)
  185.          :append(' ')
  186.         if #value1 > 0 then
  187.             b:append(value1)
  188.         end
  189.  
  190.         local value2 = upgr_ini:sr_string(sectionList[1], 'value2')
  191.         if not value2 then
  192.             if property == 'prop_ammo_size' then
  193.                 b:append(' '):append(localize('st_upgr_ammunition'))
  194.             elseif property == 'prop_tonnage' then
  195.                 b:append(localize('st_kg'))
  196.             else
  197.                 b:append('%')
  198.             end
  199.         else
  200.             if property ~= 'prop_armor' then
  201.                 b:append('%, ')
  202.             end
  203.             b:append(localize(value2))
  204.         end
  205.         return pfaCache_Put(cacheId, b:tostring())
  206.     else
  207.         local sum, extra = 0, {}
  208.         local value2
  209.         for i = 1, #sectionList do
  210.             local value1 = upgr_ini:sr_s32(sectionList[i], 'value')
  211.             if not value1 and property ~= 'prop_artefact' and property ~= 'prop_armor' then
  212.                 return pfaCache_Put(cacheId, stProperty)
  213.             end
  214.  
  215.             if property == 'prop_artefact' then
  216.                 sum = sum + 1
  217.             elseif property ~= 'prop_armor' then
  218.                 sum = sum + value1
  219.             end
  220.  
  221.             value2 = upgr_ini:sr_string(sectionList[i], 'value2')
  222.             if value2 then
  223.                 if property == 'prop_armor' then
  224.                     sum = sum + 1
  225.                 else
  226.                     value2 = localize(value2)
  227.                     if xs_find(extra, value2, nil, ipairs) == nil then
  228.                         tinsert(extra, value2)
  229.                     end
  230.                 end
  231.             end
  232.         end
  233.  
  234.         local b = pfaGetBuffer()
  235.         b:append(stProperty)
  236.  
  237.         if property == 'prop_artefact' then
  238.             b:append(' x')
  239.              :append(tostring(sum))
  240.             return pfaCache_Put(cacheId, b:tostring())
  241.         elseif property == 'prop_armor' then
  242.             value2 = upgr_ini:r_string(sectionList[1], 'value2')
  243.             if value2 ~= nil then
  244.                 b:append(' ')
  245.                  :append(localize(value2))
  246.                  :append(' x')
  247.                  :append(tostring(sum))
  248.                 return pfaCache_Put(cacheId, b:tostring())
  249.             end
  250.         end
  251.  
  252.         b:append(sum > 0 and ' +' or ' ')
  253.          :append(tostring(sum))
  254.         if property == 'prop_ammo_size' then
  255.             b:append(' ')
  256.              :append(localize('st_upgr_ammunition'))
  257.         elseif property == 'prop_tonnage' then
  258.             b:append(localize('st_kg'))
  259.         else
  260.             b:append('%')
  261.             if #extra > 0 then
  262.                 b:append(', ')
  263.                  :append(tconcat(extra, ', '))
  264.             end
  265.         end
  266.         return pfaCache_Put(cacheId, b:tostring())
  267.     end
  268. end
  269.  
  270. function property_functor_b(param1, name)
  271.     return issue_property(param1, name)
  272. end
  273.  
  274. function property_functor_c(param1, name)
  275.     return issue_property(param1, name)
  276. end
  277.  
  278. function need_victim(obj)
  279.     victim = obj
  280. end
  281.  
  282. function can_repair_item(item_name, item_condition, mechanic) --( string, float, string )
  283.     return db.actor:money() >= how_much_repair(item_name, item_condition)
  284. end
  285.  
  286. function question_repair_item(item_name, item_condition, can, mechanic) --( string, float, bool, string )
  287.     if qriMap[false] == nil then
  288.         local stCost = localize('st_upgr_cost')
  289.         qriMap[true]  = sprintf('%s: %%d RU. %s?', stCost, localize('ui_inv_repair'))
  290.         qriMap[false] = sprintf('%s: %%d RU\\n: %%d RU', stCost, localize('ui_inv_not_enought_money'))
  291.     end
  292.     local cost = how_much_repair(item_name, item_condition)
  293.     local cash = db.actor:money()
  294.     return qriMap[cash >= cost]:format(cost, cost - cash)
  295. end
  296.  
  297. function effect_repair_item(item_name, item_condition) --( string, float )
  298.     if mechanic_name ~= 'kat_cs_commander' then
  299.         local cost = how_much_repair(item_name, item_condition)
  300.         xr_statistic.inc_spent_money_counter(cost)
  301.         db.actor:give_money(-cost)
  302.     end
  303. end
  304.  
  305. function can_upgrade_item(item_name, mechanic) --( string, string )
  306.     if param_ini:line_exist(mechanic, 'he_upgrade_nothing') or
  307.        not param_ini:line_exist(mechanic, item_name) then
  308.         return false
  309.     end
  310.     mechanic_name = mechanic
  311.     return true
  312. end
  313.  
  314. -- FIXME: xs_fs.listSections requires the file it reads to exist within the OS's file system.
  315. function get_technic_specialization(name)
  316.     if gtsMap['N/A'] == nil then
  317.         local ini = ini_file([[misc\technics_specialization.ltx]])
  318.         local sectionList = xs_fs.listSections(
  319.             xs_fs.gamePath('$game_config$', [[misc\technics_specialization.ltx]])
  320.         )
  321.         local buffer = require('buffer').new()
  322.  
  323.         local stHead, stNone
  324.             = localize('st_technic_specialization'),
  325.               localize('st_technic_specialization_none')
  326.  
  327.         buffer:append(stHead):append('\\n'):append(stNone)
  328.         gtsMap['N/A'] = buffer:tostring()
  329.  
  330.         local sys = system_ini()
  331.         for i = 1, #sectionList do
  332.             buffer:len(0)
  333.             buffer:append(stHead)
  334.  
  335.             local lines = ini:line_count(sectionList[i])
  336.             if lines > 0 then
  337.                 for l = 0, lines do
  338.                     local _, item, _ = ini:r_line(sectionList[i], l, '', '')
  339.                     if item:len() > 0 then
  340.                         buffer:append('\\n'):append(localize(sys:r_string(item, 'inv_name')))
  341.                     end
  342.                 end
  343.             else
  344.                 buffer:append('\\n'):append(stNone)
  345.             end
  346.  
  347.             gtsMap[sectionList[i]] = buffer:tostring()
  348.         end
  349.         buffer:free()
  350.     end
  351.     return gtsMap[name] or gtsMap['N/A']
  352. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement