Advertisement
billysback

Unlocks

Dec 9th, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.13 KB | None | 0 0
  1.  
  2. local function split(str, pat)
  3.     local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  4.     if str ~= nil then
  5.        local fpat = "(.-)" .. pat
  6.        local last_end = 1
  7.        local s, e, cap = str:find(fpat, 1)
  8.        while s do
  9.           if s ~= 1 or cap ~= "" then
  10.          table.insert(t,cap)
  11.           end
  12.           last_end = e+1
  13.           s, e, cap = str:find(fpat, last_end)
  14.        end
  15.        if last_end <= #str then
  16.           cap = str:sub(last_end)
  17.           table.insert(t, cap)
  18.        end
  19.     else
  20.         print("##ERROR failed to split ["..str.."] by:"..pat)
  21.     end
  22.     return t
  23. end
  24.  
  25. local function getKey(name)
  26.     name = string.upper(name)
  27.     local nameToKeyConv = { LBUTTON = 1, RBUTTON = 2, CANCEL = 3, MBUTTON = 4, BACK = 8, TAB = 9, CLEAR = 12, RETURN = 13, SHIFT = 16, CONTROL = 17, MENU = 18, PAUSE = 19, CAPITAL = 20, ESCAPE = 27, SPACE = 32, PRIOR = 33, NEXT = 34, END = 35, HOME = 36, LEFT = 37, UP = 38, RIGHT = 39, DOWN = 40, SELECT = 41, PRINT = 42, EXECUTE = 43, SNAPSHOT = 44, INSERT = 45, DELETE = 46, HELP = 47, ["0"] = 48, ["1"] = 49, ["2"] = 50, ["3"] = 51, ["4"] = 52, ["5"] = 53, ["6"] = 54, ["7"] = 55, ["8"] = 56, ["9"] = 57, A = 65, B = 66, C = 67, D = 68, E = 69, F = 70, G = 71, H = 72, I = 73, J = 74, K = 75, L = 76, M = 77, N = 78, O = 79, P = 80, Q = 81, R = 82, S = 83, T = 84, U = 85, V = 86, W = 87, X = 88, Y = 89, Z = 90, LWIN = 91, RWIN = 92, APPS = 93, NUMPAD0 = 96, NUMPAD1 = 97, NUMPAD2 = 98, NUMPAD3 = 99, NUMPAD4 = 100, NUMPAD5 = 101, NUMPAD6 = 102, NUMPAD7 = 103, NUMPAD8 = 104, NUMPAD9 = 105, MULTIPLY = 106, ADD = 107, SEPARATOR = 108, SUBTRACT = 109, DECIMAL = 110, DIVIDE = 111, F1 = 112, F2 = 113, F3 = 114, F4 = 115, F5 = 116, F6 = 117, F7 = 118, F8 = 119, F9 = 120, F10 = 121, F11 = 122, F12 = 123, F13 = 124, F14 = 125, F15 = 126, F16 = 127, F17 = 128, F18 = 129, F19 = 130, F20 = 131, F21 = 132, F22 = 133, F23 = 134, F24 = 135, NUMLOCK = 144, SCROLL = 145, LSHIFT = 160, LCONTROL = 162, LMENU = 164, RSHIFT = 161, RCONTROL = 163, RMENU = 165}
  28.     return nameToKeyConv[name]
  29. end
  30.  
  31. Unlocks = {}
  32. Unlocks.on = false
  33. Unlocks.ALPHA = 100
  34. Unlocks.maxLevel = 30
  35. Unlocks.xpKill = 10
  36. Unlocks.lvlMod = 10
  37. Unlocks.lvlModAmt = 2
  38. Unlocks.lvlCostMod = 1
  39. Unlocks.startLevel = 0
  40. Unlocks.options = 3
  41. Unlocks.aiChance = 80
  42. Unlocks.keys = {
  43.     toggle = "O",
  44.     options = {
  45.         [1] = "V",
  46.         [2] = "B",
  47.         [3] = "N",
  48.         [4] = "M"
  49.     }
  50. }
  51. Unlocks.weapons = {
  52.     [1] = {
  53.         "ammoLargeDouble",
  54.         "flashbang",
  55.         "sluggerPipe",
  56.         "sphereGrenade",
  57.         "sluggerRevolver"
  58.     },
  59.     [2] = {
  60.         "klonk",
  61.         "spikedMine",
  62.         "spikeCrossbow",
  63.         "sluggerSemi",
  64.         "ammoLargeDouble"
  65.     },
  66.     [3] = {
  67.         "sluggerSub",
  68.         "matterPistol",
  69.         "ammoLargeDouble",
  70.         "plasmaPistol",
  71.         "sluggerShotgun",
  72.     },
  73.     [4] = {
  74.         "grenadeGun",
  75.         "sluggerGun",
  76.         "beamGun",
  77.         "ammoLargeDouble",
  78.         "gyrojetGun"
  79.     },
  80.     [5] = {
  81.         "plasmaGun",
  82.         "homingLauncer",
  83.         "matterBlaster",
  84.         "ammoLargeDouble",
  85.         "plasmaAssault"
  86.     }
  87. }
  88.  
  89.  
  90.  
  91. local function getLines(dir)
  92.     local lines = {}
  93.     local file = io.open(dir)
  94.     if file ~= nil then
  95.         for line in file:lines() do lines[#lines + 1] = line end
  96.         file:close()
  97.     end
  98.     return lines
  99. end
  100.  
  101. local function cloneTable(table)
  102.     local ntable = {}
  103.     for k,v in pairs(table) do
  104.         ntable[k] = v
  105.     end
  106.     return ntable
  107. end
  108.  
  109. local function removeIndex(table, index)
  110.     local ntable = {}
  111.     for i=1,#table do
  112.         if i ~= index then ntable[#ntable + 1] = table[i] end
  113.     end
  114.     return ntable
  115. end
  116.  
  117. local function getGuns(lvl, nGuns)
  118.     local tier = 1
  119.     for i=1,#Unlocks.weapons do
  120.         if (lvl >= (i-1)*5 and lvl < (i*5)) or (lvl > (i*5) and i == #Unlocks.weapons) then
  121.             tier = i
  122.         end
  123.     end
  124.     local tWeps = cloneTable(Unlocks.weapons[tier])
  125.     local weps = {}
  126.     if nGuns > #tWeps then nGuns = #tWeps end
  127.     for i=1,nGuns do
  128.         local index = math.random(#tWeps)
  129.         weps[#weps + 1] = {tWeps[index], tier}
  130.         tWeps = removeIndex(tWeps, index)
  131.     end
  132.     weps[#weps + 1] = {"CANCEL", 0}
  133.     return weps
  134. end
  135.  
  136. local function addWeapon(name, actor)
  137.     local wep = Object.new(Object,name,actor.x,actor.y,actor.map)
  138.     actor.map:addObject(wep)
  139.     wep:propel(0,1)
  140. end
  141.  
  142. local function pickWeapon(player, option)
  143.     if player.unlockOptions ~= nil and player.xp_level ~= nil then
  144.         local wep = player.unlockOptions[option]
  145.         if wep ~= nil then
  146.             local tWep = wep[1]
  147.             if tWep ~= "CANCEL" then
  148.                 local cost = wep[2]*Unlocks.lvlCostMod
  149.                 local xpCost = (((cost*Unlocks.lvlModAmt)*Unlocks.lvlMod)+Unlocks.lvlMod)
  150.                 if player.xp_level >= cost and player.xp >= xpCost then
  151.                     player.xp_level = player.xp_level - cost
  152.                     player.xp = player.xp - xpCost
  153.                     addWeapon(tWep, player:getActor())
  154.                     if player:isAi() == false then
  155.                         print("Added weapon FOR HUMAN: "..tWep)
  156.                     else
  157.                         print("Added weapon FOR AI: "..tWep)
  158.                     end
  159.                 end
  160.             end
  161.             if player.xp_level == 0 then
  162.                 player.unlockOptions = nil
  163.             else
  164.                 player.unlockOptions = getGuns(player.xp_level, Unlocks.options)
  165.             end
  166.         end
  167.     end
  168. end
  169.  
  170.  
  171. local rPlayer = nil
  172. local dPlayers = {}
  173.  
  174. local function contains(table, value)
  175.     for k,v in pairs(table) do
  176.         if v == value then return true end
  177.     end
  178.     return false
  179. end
  180.  
  181. local function checkLevel(player)
  182.     if player ~= nil and player.xp ~= nil and player.xp_level ~= nil then
  183.         local ok = false
  184.         for i=1,Unlocks.maxLevel do
  185.             if player.xp >= ((i*Unlocks.lvlModAmt)*Unlocks.lvlMod)+Unlocks.lvlMod and player.xp_level < i then
  186.                 player.xp_level = i
  187.                 ok = true
  188.             end
  189.         end
  190.         if ok and player.unlockOptions == nil then
  191.             player.unlockOptions = getGuns(player.xp_level, Unlocks.options)
  192.            
  193.             if player:isAi() == true and player.unlockOptions ~= nil then
  194.                 if math.random(100) < Unlocks.aiChance then pickWeapon(player, math.random(#player.unlockOptions - 1)) end
  195.             elseif (player:isAi() == false) and player.unlockOptions ~= nil then
  196.                 rPlayer = player
  197.             end
  198.         end
  199.     end
  200.     if (player:isAi() == false) then rPlayer = player end
  201. end
  202.  
  203. local converter = {
  204.         ["XP"] = video.createSpriteState("hud_know_destroyed","../hud.dat"),
  205.         ["LVL"] = video.createSpriteState("hud_know_destroyed","../hud.dat"),
  206.         ["NAME"] = "name",
  207.         ["BOX"] = "box"
  208.     }
  209.  
  210. local function getImage(name)
  211.    
  212.     local id = converter[string.upper(name)]
  213.     return id
  214. end
  215.  
  216. local function getWeaponImage(wName)
  217.     local icName = ITEMS[wName].iconName
  218.     --print(wName..": "..icName)
  219.     --local icon = video.createSpriteState(tostring(ITEMS[wName].iconName), "../hud.dat")
  220.     icon = video.createSpriteState("ammoLargeDouble_icon", '../hud.dat')
  221.     return icon
  222. end
  223.  
  224. function UdrawBox(x, y, w, h, text, imageID, col, col2)
  225.     if video ~= nil and Unlocks.on == true then
  226.         if imageID ~= nil then
  227.             video.renderSpriteStateFreeShape(imageID,
  228.                 x,  y,
  229.                 x + w,  y,
  230.                 x,  y + h,
  231.                 x + w, y + h,
  232.                 col2[1] - Unlocks.ALPHA, col2[2], col2[3], col2[4])
  233.         end
  234.         if text ~= nil then
  235.             video.renderText(text, x + 5, y, 0, "../boldFont.fnt", col[1] - Unlocks.ALPHA, col[2], col[3], col[4])
  236.         end
  237.     end
  238. end
  239.  
  240. local YESYES = 0
  241. function myRender()
  242.    
  243.    
  244.     if Unlocks.on == true and rPlayer ~= nil then
  245.        
  246.         local name = rPlayer:getName()
  247.         if name == nil then name = "Player" end
  248.         if name:gsub(" ", "") == "" then name = "Player" end
  249.         if name == "<unknown>" then name = "Player" end
  250.         local xp = rPlayer.xp
  251.         if xp == nil then xp = 0 end
  252.         local lvl = rPlayer.xp_level
  253.         if lvl == nil then lvl = 0 end
  254.         local options = rPlayer.unlockOptions
  255.         UdrawBox(3, 0, 20, 20, name, nil, {255, 30, 150, 255}, {255, 50, 50, 50})
  256.         UdrawBox(3, 14, 20, 20, xp, getImage("xp"), {255, 30, 150, 255}, {255, 50, 50, 50})
  257.         UdrawBox(60, 14, 20, 20, lvl, getImage("lvl"), {255, 30, 150, 255}, {255, 50, 50, 50})
  258.         if options ~= nil then
  259.             for i=1,#options do
  260.                 local wep = options[i][1]
  261.                 if wep ~= "CANCEL" then
  262.                     id = video.createSpriteState(ITEMS[wep].iconName, '../hud.dat')
  263.                     UdrawBox(60+(i*47), 14, 20, 20, nil, id, {255, 30, 150, 255}, {255, 255, 255, 255})
  264.                     if YESYES < 5 then
  265.                         print(wep..": "..id)
  266.                         YESYES = YESYES + 1
  267.                     end
  268.                 end
  269.             end
  270.         end
  271.        
  272.     end
  273.    
  274. end
  275. hook.add("frameRender", myRender)
  276.  
  277.  
  278.  
  279.  
  280. local function onKeyPress(key)
  281.     local ok = true
  282.     if Unlocks.on == true and rPlayer ~= nil then
  283.         for i=1,#Unlocks.keys.options do
  284.             local k = Unlocks.keys.options[i]
  285.             if type(k) == "string" and tonumber(k) == nil then k = getKey(k) end
  286.             if type(k) == "string" then k = tonumber(k) end
  287.             if k == key then
  288.                 pickWeapon(rPlayer, i)
  289.                 print("Selected weapon")
  290.                 ok = false
  291.             end
  292.         end
  293.     end
  294.     local togk = Unlocks.keys.toggle
  295.     if type(togk) == "string" and tonumber(togk) == nil then togk = getKey(togk) end
  296.     if type(togk) == "string" then togk = tonumber(togk) end
  297.     if not ok then
  298.     elseif key == togk then
  299.         local onOff = "on"
  300.         if Unlocks.on == true then
  301.             Unlocks.on = false
  302.             for k,v in pairs(dPlayers) do
  303.                 v.xp = nil
  304.                 v.xp_level = nil
  305.                 v.unlockOptions = nil
  306.             end
  307.             dPlayers = {}
  308.             onOff = "off"
  309.         else
  310.             Unlocks.on = true
  311.             onOff = "on"
  312.         end
  313.         print("Toggled Unlocks ("..onOff..")")
  314.     else
  315.         --print("Unknown key")
  316.     end
  317. end
  318. hook.add("keyPress", onKeyPress)
  319.  
  320.  
  321. local function strContains(line, str)
  322.     for i=1,string.len(line) do
  323.         if i < (string.len(line) - string.len(str) + 1) then
  324.             local s = string:sub(i, (i+string.len(str))-1)
  325.             if s == str then return true end
  326.         end
  327.     end
  328.     return false
  329. end
  330.  
  331. local function init()
  332.  
  333.     local lines = getLines("../controls/unlocks_config.txt")
  334.     if #lines > 0 then
  335.         for i=1,#lines do
  336.             local line = lines[i]
  337.             if line ~= "" and strContains(line, "==") then
  338.                 local data = split(line, "==")
  339.                 if data ~= nil then
  340.                     if #data > 1 then
  341.                         local key = string.lower(data[1])
  342.                         if key == "alpha" then
  343.                             local old = Unlocks.ALPHA
  344.                             Unlocks.ALPHA = tonumber(data[2])
  345.                             if Unlocks.ALPHA == nil then Unlocks.ALPHA = old end
  346.                         elseif key:sub(1, 5) == "tier_" then
  347.                             local splt = split(key, "_")
  348.                             local tier = tonumber(splt[2])
  349.                             if tier ~= nil then
  350.                                 Unlocks.weapons[tier][#Unlocks.weapons[tier] + 1] = string:gsub(data[2], " ", "")
  351.                             end
  352.                         elseif key == "reset_tier" then
  353.                             local tier = tonumber(data[2])
  354.                             Unlocks.weapons[tier] = {}
  355.                         elseif key == "max_level" then
  356.                             local lvl = tonumber(data[2])
  357.                             Unlocks.maxlvl = lvl
  358.                         elseif key == "xp_kill" then
  359.                             local xp = tonumber(data[2])
  360.                             Unlocks.xpKill = xp
  361.                         elseif key == "lvl_mod" then
  362.                             local lvl = tonumber(data[2])
  363.                             Unlocks.lvlMod = lvl
  364.                         elseif key == "lvl_mod_amount" or key == "lvl_mod_amt" then
  365.                             local lvl = tonumber(data[2])
  366.                             Unlocks.lvlModAmt = lvl
  367.                         elseif key == "starting_lvl" then  
  368.                             local lvl = tonumber(data[2])
  369.                             Unlocks.startlvl = lvl
  370.                         elseif key == "lvl_cost_mod" then
  371.                             local lvl = tonumber(data[2])
  372.                             Unlocks.lvlCostMod = lvl
  373.                         elseif key == "toggle_key" then
  374.                             local k = tonumber(data[2])
  375.                             Unlocks.keys.toggle = k
  376.                         elseif key == "ai_chance" then
  377.                             local chance = tonumber(data[2])
  378.                             Unlocks.aiChance = chance
  379.                         elseif key:sub(1, string.len("option_key_")) == "option_key_" then
  380.                             local op = tonumber(key:sub(string.len("option_key_")+1, -1))
  381.                             local k = tonumber(data[2])
  382.                             if op ~= nil then
  383.                                 Unlocks.keys.options[op] = k
  384.                             end
  385.                         elseif key == "options" then
  386.                             local n = tonumber(data[2])
  387.                             Unlocks.options = n
  388.                         end
  389.                     end
  390.                 end
  391.             end
  392.         end
  393.     end
  394.    
  395.     local addStat = Player.addStat
  396.     function Player:addStat(stat,num)
  397.         if not contains(dPlayers, self) then
  398.             dPlayers[#dPlayers + 1] = self
  399.         end
  400.         if stat == "kills" and Unlocks.on == true then
  401.             if self.xp == nil then self.xp = num*Unlocks.xpKill else
  402.             self.xp = self.xp + num*Unlocks.xpKill end
  403.             if self.xp_level == nil then self.xp_level = Unlocks.startLevel end
  404.         end
  405.         checkLevel(self)
  406.         return addStat(self,stat,num)
  407.     end
  408.    
  409.     oldInit = Mode.onInitedMapBehaviours
  410.     function Mode:onInitedMapBehaviours(a,s,d,e,f)
  411.         for k,v in pairs(dPlayers) do
  412.             v.xp = nil
  413.             v.xp_level = nil
  414.             v.unlockOptions = nil
  415.         end
  416.         return oldInit(self,a,s,d,e,f)
  417.     end
  418. end
  419. hook.add("gameInit",init)
  420.  
  421. icon = video.createSpriteState("ammoLargeDouble_icon", '../hud.dat')
  422. print(icon)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement