Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[-----------------------------------------------------------------------\\--
- :: PRIVATE / LOCAL ::
- --\\-----------------------------------------------------------------------]]--
- xs_extend.export()
- local tonumber, tostring, sprintf, mfloor, tinsert, tconcat, tremove, mmin
- = tonumber, tostring, string.format, math.floor, table.insert, table.concat, table.remove, math.min
- local localize, alife, system_ini, ini_file
- = game.translate_string, alife, system_ini, ini_file
- local xs_find
- = xs_utils.find
- local issue_condlist = true
- local mechanic_name = ""
- local upgr_ini = ini_file('item_upgrades.ltx')
- local param_ini = ini_file([[misc\stalkers_upgarde_info.ltx]])
- local pfaCache, pfaBuffer = xs_utils.toWeakTable({}, 'kv')
- local pfaMap, qriMap, gtsMap = {}, {}, {}
- local function how_much_repair(item_name, item_condition)
- local sys = system_ini()
- local coef = (sys:r_string(item_name, 'class') == 'E_STLK') and 0.5 or 0.8
- return mfloor(sys:r_u32(item_name, 'cost') * (1 - item_condition) * coef)
- end
- local function pfaGetBuffer()
- if pfaBuffer == nil then
- pfaBuffer = require('buffer').new()
- else
- pfaBuffer:len(0)
- end
- return pfaBuffer
- end
- local function pfaCache_Put(id, result)
- if #pfaCache == 10 then
- tremove(pfaCache, 1)
- end
- tinsert(pfaCache, { [id] = result })
- return result
- end
- local pfaCache_Next = 1
- local function pfaCache_Get(id)
- if pfaCache_Next > #pfaCache then
- pfaCache_Next = 1
- end
- for i = pfaCache_Next, #pfaCache do
- if pfaCache[i][id] ~= nil then
- pfaCache_Next = i + 1
- return pfaCache[i][id]
- end
- end
- for i = 1, #pfaCache do
- if pfaCache[i][id] ~= nil then
- pfaCache_Next = i + 1 > #pfaCache and 1 or i + 1
- return pfaCache[i][id]
- end
- end
- return nil
- end
- local function issue_property(param1, name)
- local stPropName = localize(upgr_ini:r_string(name, 'name'))
- local sectionList = utils.parse_names(param1)
- local value = sectionList[1] ~= nil and upgr_ini:sr_string(sectionList[1], 'value')
- if type(value) == 'string' then
- return stPropName..' '..value:sub(2, -2)
- else
- return stPropName
- end
- end
- --[[-----------------------------------------------------------------------\\--
- :: PUBLIC / GLOBAL / EXPORTED ::
- --\\-----------------------------------------------------------------------]]--
- function precondition_functor_a(param1, section)
- if not upgr_ini:line_exist(section, 'cost') then
- abort("[inventory_upgrades.precondition_functor_a]: No entry 'cost' in section '%s'.", section)
- end
- issue_condlist = true
- local act = db.actor
- if not act then
- return 0
- end
- local upgradeSection = mechanic_name .. '_upgr'
- if param_ini:line_exist(upgradeSection, section) then
- local param = param_ini:r_string(upgradeSection, section)
- if param == nil then
- return 0
- elseif param == "false" then
- issue_condlist = false
- return 2
- end
- -- TODO: rename 't' based on what xr_logic returns...
- local t = xr_logic.parse_condlist(victim, upgradeSection, section, param)
- if not xr_logic.pick_section_from_condlist(act, victim, t) then
- issue_condlist = false
- return 2
- end
- end
- if act:money() < upgr_ini:r_u32(section, 'cost') then
- return 1
- end
- return 0
- end
- function effect_functor_a(param2, section, loading) --( string, string, int )
- if loading == 0 then
- local cost = upgr_ini:r_u32(section, 'cost')
- xr_statistic.inc_spent_money_counter(cost)
- db.actor:give_money(-cost)
- end
- end
- function prereq_functor_a(param3, section)
- if issue_condlist and not db.actor then
- return ' '
- end
- if pfaMap['N/A'] == nil then
- local stCost = localize('st_upgr_cost')
- pfaMap[true] = sprintf('%s: %%d RU', stCost)
- pfaMap[false] = sprintf('%s: %%d RU\\n%s: %%d RU', stCost, localize('ui_inv_not_enought_money'))
- pfaMap['N/A'] = localize('st_upgr_condlist')
- end
- if issue_condlist then
- local cost = upgr_ini:r_u32(section, 'cost')
- local cash = db.actor:money()
- return pfaMap[cash >= cost]:format(cost, cost - cash)
- else
- return pfaMap['N/A']
- end
- end
- function prereq_tooltip_functor_a(param3)
- return true
- end
- -- Creates a human-readable description of an upgrade. Used for installed as well as potential
- -- upgrades.
- -- @param param1 (string: comma separated list of section names) List of 0-n names, where each
- -- name refers to a specific upgrade section of an specific item.
- -- @param property (string) The name of the property in question.
- -- Refers to a section in upgrades_properties.ltx.
- --
- -- Example input: param1 = 'up_sect_h_lr300, up_sect_c_lr300', property = 'prop_recoil'
- function property_functor_a(param1, property)
- if param1 == nil or param1 == '' then -- happens during game init/loading
- return ''
- end
- local cacheId = param1 .. property
- local cachedResult = pfaCache_Get(cacheId)
- if cachedResult ~= nil then
- return cachedResult
- end
- -- local sectionList = xs_map(xs_split(',', param1), xs_trim, ipairs)
- local sectionList = utils.parse_names(param1)
- if #sectionList == 0 then
- return pfaCache_Put(cacheId, '')
- end
- local stProperty = localize(upgr_ini:r_string(property, 'name'))
- if #sectionList == 1 then
- local value1 = upgr_ini:sr_string(sectionList[1], 'value') or ''
- if value1 == '' and property ~= 'prop_armor' then
- return pfaCache_Put(cacheId, stProperty)
- end
- local b = pfaGetBuffer()
- b:append(stProperty)
- :append(' ')
- if #value1 > 0 then
- b:append(value1)
- end
- local value2 = upgr_ini:sr_string(sectionList[1], 'value2')
- if not value2 then
- if property == 'prop_ammo_size' then
- b:append(' '):append(localize('st_upgr_ammunition'))
- elseif property == 'prop_tonnage' then
- b:append(localize('st_kg'))
- else
- b:append('%')
- end
- else
- if property ~= 'prop_armor' then
- b:append('%, ')
- end
- b:append(localize(value2))
- end
- return pfaCache_Put(cacheId, b:tostring())
- else
- local sum, extra = 0, {}
- local value2
- for i = 1, #sectionList do
- local value1 = upgr_ini:sr_s32(sectionList[i], 'value')
- if not value1 and property ~= 'prop_artefact' and property ~= 'prop_armor' then
- return pfaCache_Put(cacheId, stProperty)
- end
- if property == 'prop_artefact' then
- sum = sum + 1
- elseif property ~= 'prop_armor' then
- sum = sum + value1
- end
- value2 = upgr_ini:sr_string(sectionList[i], 'value2')
- if value2 then
- if property == 'prop_armor' then
- sum = sum + 1
- else
- value2 = localize(value2)
- if xs_find(extra, value2, nil, ipairs) == nil then
- tinsert(extra, value2)
- end
- end
- end
- end
- local b = pfaGetBuffer()
- b:append(stProperty)
- if property == 'prop_artefact' then
- b:append(' x')
- :append(tostring(sum))
- return pfaCache_Put(cacheId, b:tostring())
- elseif property == 'prop_armor' then
- value2 = upgr_ini:r_string(sectionList[1], 'value2')
- if value2 ~= nil then
- b:append(' ')
- :append(localize(value2))
- :append(' x')
- :append(tostring(sum))
- return pfaCache_Put(cacheId, b:tostring())
- end
- end
- b:append(sum > 0 and ' +' or ' ')
- :append(tostring(sum))
- if property == 'prop_ammo_size' then
- b:append(' ')
- :append(localize('st_upgr_ammunition'))
- elseif property == 'prop_tonnage' then
- b:append(localize('st_kg'))
- else
- b:append('%')
- if #extra > 0 then
- b:append(', ')
- :append(tconcat(extra, ', '))
- end
- end
- return pfaCache_Put(cacheId, b:tostring())
- end
- end
- function property_functor_b(param1, name)
- return issue_property(param1, name)
- end
- function property_functor_c(param1, name)
- return issue_property(param1, name)
- end
- function need_victim(obj)
- victim = obj
- end
- function can_repair_item(item_name, item_condition, mechanic) --( string, float, string )
- return db.actor:money() >= how_much_repair(item_name, item_condition)
- end
- function question_repair_item(item_name, item_condition, can, mechanic) --( string, float, bool, string )
- if qriMap[false] == nil then
- local stCost = localize('st_upgr_cost')
- qriMap[true] = sprintf('%s: %%d RU. %s?', stCost, localize('ui_inv_repair'))
- qriMap[false] = sprintf('%s: %%d RU\\n: %%d RU', stCost, localize('ui_inv_not_enought_money'))
- end
- local cost = how_much_repair(item_name, item_condition)
- local cash = db.actor:money()
- return qriMap[cash >= cost]:format(cost, cost - cash)
- end
- function effect_repair_item(item_name, item_condition) --( string, float )
- if mechanic_name ~= 'kat_cs_commander' then
- local cost = how_much_repair(item_name, item_condition)
- xr_statistic.inc_spent_money_counter(cost)
- db.actor:give_money(-cost)
- end
- end
- function can_upgrade_item(item_name, mechanic) --( string, string )
- if param_ini:line_exist(mechanic, 'he_upgrade_nothing') or
- not param_ini:line_exist(mechanic, item_name) then
- return false
- end
- mechanic_name = mechanic
- return true
- end
- -- FIXME: xs_fs.listSections requires the file it reads to exist within the OS's file system.
- function get_technic_specialization(name)
- if gtsMap['N/A'] == nil then
- local ini = ini_file([[misc\technics_specialization.ltx]])
- local sectionList = xs_fs.listSections(
- xs_fs.gamePath('$game_config$', [[misc\technics_specialization.ltx]])
- )
- local buffer = require('buffer').new()
- local stHead, stNone
- = localize('st_technic_specialization'),
- localize('st_technic_specialization_none')
- buffer:append(stHead):append('\\n'):append(stNone)
- gtsMap['N/A'] = buffer:tostring()
- local sys = system_ini()
- for i = 1, #sectionList do
- buffer:len(0)
- buffer:append(stHead)
- local lines = ini:line_count(sectionList[i])
- if lines > 0 then
- for l = 0, lines do
- local _, item, _ = ini:r_line(sectionList[i], l, '', '')
- if item:len() > 0 then
- buffer:append('\\n'):append(localize(sys:r_string(item, 'inv_name')))
- end
- end
- else
- buffer:append('\\n'):append(stNone)
- end
- gtsMap[sectionList[i]] = buffer:tostring()
- end
- buffer:free()
- end
- return gtsMap[name] or gtsMap['N/A']
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement