Advertisement
Guest User

ballserever

a guest
Mar 27th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.15 KB | None | 0 0
  1. -- Ball switcher: assign the ball for home team
  2. -- Custom content is used, not LiveCPK/game: content\ball-server is the root
  3. -- author: zlac, 2017
  4. -- originally posted on evo-web
  5.  
  6.  
  7. local new_ball_path
  8. local ballroot = ".\\content\\ball-server\\"
  9.  
  10. local team_assignment_map = {}
  11. local competition_assignment_map = {}
  12.  
  13. -- override_competitions contains comma-separated ID's of the competitions that allow for team-assigned balls to have precedence before competiton-assigned balls
  14. -- .. e.g. you've assigned an official ball for ALL exhibition mode matches (tid 0) in map_competitions.txt, but you still want to use
  15. -- .. home-team ball for those teams that have it assigned in map_teams.txt -> add 0 in override_competitions
  16. -- initially, only Exhibition mode matches are included in competition overrides
  17. -- BEGIN CUSTOMIZABLE LUA TABLE
  18. local override_competitions = {0,}
  19. -- END CUSTOMIZABLE LUA TABLE
  20.  
  21.  
  22.  
  23. -- remove trailing and leading whitespace from string
  24. local function trim(s)
  25.   return s:gsub("^%s*(.-)%s*$", "%1")
  26. end
  27.  
  28. local function split(s, delim)
  29.    local p = "[^"..table.concat(delim).."]+"
  30.    local fields = {}
  31.    for w in s:gmatch(p) do
  32.       w = trim(w)  
  33.       fields[#fields+1] = w
  34.    end
  35.    return fields
  36. end
  37.  
  38. -- splits multi-line text to individual lines
  39. local function to_lines(s)
  40.   local t = {}
  41.   local function helper(line)
  42.       line = trim(line)
  43.       if line:len() > 0 and line:sub(1, 1) ~= "#" then
  44.      table.insert(t, line)
  45.       end
  46.       return ""
  47.   end
  48.   helper((s:gsub("(.-)\r?\n", helper)))
  49.   return t
  50. end
  51.  
  52. local function load_map_txt(filename)
  53.     local delim = {",", "#"}
  54.     local file = assert(io.open(ballroot .. filename, "r"))
  55.     log(filename .. " found in " .. ballroot .. filename)
  56.     local data = trim(file:read("*all"))
  57.     data = string.gsub(data, "^\239\187\191", "")   -- removes UTF BOM bytes at the beginning of the .txt file
  58.    
  59.     for i, line in pairs(to_lines(data)) do
  60.        local fields = split(line, delim)
  61.        if #fields > 1 then
  62.            for i=1,#fields do
  63.               local field = trim(fields[i])
  64.               if field == "" then
  65.                  field = null
  66.               end
  67.               fields[i] = field
  68.            end
  69.            if fields[1] ~= nil then
  70.               if filename == "map_teams.txt" then
  71.                  team_assignment_map[tonumber(fields[1])] = fields[2]
  72.                  log(string.format(" ==> %s ball assignment::   %s: %s", filename, fields[1], fields[2]))
  73.               end
  74.               if filename == "map_competitions.txt" then
  75.                  competition_assignment_map[tonumber(fields[1])] = {fields[2], fields[3], fields[4]}
  76.                  log(string.format(" ==> %s ball assignment::   %s: %s (for final match: %s | for winter match: %s)", filename, fields[1], fields[2], fields[3], fields[4]))
  77.               end
  78.            end
  79.        end
  80.     end
  81.     file:close()
  82. end
  83.  
  84. local function has_value(tab, val)
  85.     for index, value in ipairs(tab) do
  86.         if value == val then
  87.             return true
  88.         end
  89.     end
  90.     return false
  91. end
  92.  
  93.  
  94. local function which_ball_model(filename)
  95.     return string.match(string.lower(filename), "render\\model\\ball\\ball%d+\\(ball.*%..+)")
  96. end
  97.  
  98. local function which_ball_preview_tex(filename)
  99.     return string.match(string.lower(filename), "render\\thumbnail\\ball\\(ball_%d+%.dds)")
  100. end
  101.  
  102.  
  103. local function make_key(ctx, filename)
  104.     local function get_new_ball_path(ctx)
  105.         local tid = tonumber(ctx.tournament_id)
  106.         local ball_path
  107.        
  108.         if tid then
  109.            if competition_assignment_map[tid] == nil and team_assignment_map[ctx.home_team] ~= nil then
  110.           -- this particular competiton mode does not have the ball assigned via map_competitions.txt
  111.           -- .. individual home team assignments from map_teams.txt will be used here
  112.           ball_path = team_assignment_map[ctx.home_team]
  113.            elseif competition_assignment_map[tid] ~= nil and team_assignment_map[ctx.home_team] ~= nil and has_value(override_competitions, tid) then
  114.           -- this particular competiton mode HAS the ball assigned via map_competitions.txt,
  115.           -- .. but it is also listed in override_competitions, therefore
  116.           -- .. individual home team assignments from map_teams.txt will be used here again
  117.           ball_path = team_assignment_map[ctx.home_team]
  118.            else
  119.           -- nothing else but possible competition assignment in map_competitions.txt
  120.           -- .. ball assignment from map_competitions.txt will be used, if there is any
  121.                   if competition_assignment_map[tid] ~= nil then
  122.                      -- is this the final match of a competition (53)?
  123.                      if ctx.season == 1 then
  124.                         ball_path = competition_assignment_map[tid][3]  -- use the ball assigned for winter season
  125.                      elseif ctx.match_info == 53 then
  126.                         ball_path = competition_assignment_map[tid][2]  -- use the ball assigned for final match; could be nil
  127.                      else
  128.                         ball_path = competition_assignment_map[tid][1]  -- use the regular ball assigned for this competition
  129.                      end  
  130.                   end  
  131.            end
  132.         end
  133.         return ball_path
  134.     end
  135.        
  136.     local bm_fname = which_ball_model(filename)
  137.     if bm_fname then
  138.           new_ball_path = get_new_ball_path(ctx)
  139.           if new_ball_path then
  140.              return new_ball_path .. "\\" .. bm_fname
  141.           end
  142.     end
  143.    
  144.     local bpt_fname = which_ball_preview_tex(filename)
  145.     if bpt_fname then
  146.          new_ball_path = get_new_ball_path(ctx)
  147.          if new_ball_path then
  148.             return new_ball_path .. "\\preview.dds"
  149.          end
  150.     end
  151. end
  152.  
  153. local function get_filepath(ctx, filename, key)
  154.     if new_ball_path then
  155.         log(string.format("Ball assignment for team ID %d (competition ID %d) - %s\\%s", tonumber(ctx.home_team), tonumber(ctx.tournament_id), ballroot, key))
  156.         return string.format("%s\\%s", ballroot, key)
  157.     end
  158. end
  159.  
  160. local function init(ctx)
  161.     if ballroot:sub(1,1)=='.' then
  162.         ballroot = ctx.sider_dir .. ballroot
  163.     end
  164.     load_map_txt("map_teams.txt")
  165.     load_map_txt("map_competitions.txt")
  166.     ctx.register("livecpk_make_key", make_key)
  167.     ctx.register("livecpk_get_filepath", get_filepath)
  168. end
  169.  
  170. return { init = init }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement