Advertisement
rockbandcheeseman

Custom Weapon Set

Feb 18th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.09 KB | None | 0 0
  1. -- Custom Weapon Spawns
  2.  
  3. weaps = {}
  4.  
  5. -- Weapon set (you can add more slots if you so choose; it should still work)
  6.  
  7. weaps[0] = "weapons\\needler\\mp_needler"
  8. weaps[1] = "weapons\\flamethrower\\flamethrower"
  9. weaps[2] = nil
  10. weaps[3] = nil
  11.  
  12. auto_grenade_types = true  -- If this value is true, grenades players spawn with and grenades placed around the map will be determined by the theme of weapons listed above.  If it is false, grenades will be placed as defined by the gametype running.
  13.  
  14. --[[
  15.     List of valid weapon tags
  16.    
  17.     Weapon Name      Tag Name
  18.    
  19.     Assault Rifle:   "weapons\\assault rifle\\assault rifle"
  20.     Flamethrower:    "weapons\\flamethrower\\flamethrower"
  21.     Needler:         "weapons\\needler\\mp_needler"
  22.     Pistol:          "weapons\\pistol\\pistol"
  23.     Plasma Pistol:   "weapons\\plasma pistol\\plasma pistol"
  24.     Plasma Rifle:    "weapons\\plasma rifle\\plasma rifle"
  25.     Fuel Rod Gun:    "weapons\\plasma_cannon\\plasma_cannon"
  26.     Rocket Launcher: "weapons\\rocket launcher\\rocket launcher"
  27.     Shotgun:         "weapons\\shotgun\\shotgun"
  28.     Sniper Rifle:    "weapons\\sniper rifle\\sniper rifle"
  29. --]]
  30.  
  31. function GetRequiredVersion()
  32.  
  33.     return 10058
  34. end
  35.  
  36. function OnScriptLoad(process)
  37.  
  38.  
  39. end
  40.  
  41. function OnScriptUnload()
  42.  
  43.    
  44. end
  45.  
  46. function OnNewGame(map)
  47.  
  48.  
  49. end
  50.  
  51. function OnGameEnd(mode)
  52.  
  53.  
  54. end
  55.  
  56. function OnServerChat(player, chattype, message)
  57.  
  58.     return 1
  59. end
  60.  
  61. function OnServerCommand(player, command)
  62.  
  63.     return 1
  64. end
  65.  
  66. function OnTeamDecision(team)
  67.  
  68.     return team
  69. end
  70.  
  71. function OnPlayerJoin(player, team)
  72.  
  73.    
  74. end
  75.  
  76. function OnPlayerLeave(player, team)
  77.  
  78.  
  79. end
  80.  
  81. function OnPlayerKill(killer, victim, mode)
  82.  
  83.  
  84. end
  85.  
  86. function OnKillMultiplier(player, multiplier)
  87.  
  88.  
  89. end
  90.  
  91. function OnPlayerSpawn(player, m_objId)
  92.  
  93.     if auto_grenade_types then
  94.         if infinite_grenades == 0 then
  95.             local m_object = getobject(m_objId)
  96.             if weaps[0] then
  97.                 if table.find(attributes[weaps[0]], "Human") then
  98.                     writebyte(m_object, 0x31E, 2)
  99.                     writebyte(m_object, 0x31F, 0)
  100.                 else
  101.                     writebyte(m_object, 0x31E, 0)
  102.                     writebyte(m_object, 0x31F, 2)
  103.                 end
  104.             end
  105.         end
  106.     end
  107. end
  108.  
  109. function OnPlayerSpawnEnd(player, m_objId)
  110.  
  111.    
  112. end
  113.  
  114. function OnTeamChange(relevant, player, cur_team, dest_team)
  115.  
  116.     return 1
  117. end
  118.  
  119. function OnObjectCreation(m_objId, player, tagName)
  120.  
  121.     if new_object_orientation then
  122.         rotateobject(m_objId, new_object_orientation)
  123.         new_object_orientation = nil
  124.     end
  125.    
  126.     local tagType, tagName = getobjecttag(m_objId)
  127.     if tagType == "weap" then
  128.         if attributes[tagName] then
  129.             if not table.find(weaps, tagName) then
  130.                 local x, y, z = getobjectcoords(m_objId)
  131.                 movobjcoords(m_objId, 0, 0, -100)
  132.                 local newtagName = similar(tagName)
  133.                 local orientation = getobjectorientation(m_objId)
  134.                 createobject("weap", newtagName, 0, 45, true, x, y, z, orientation)
  135.             end
  136.         end
  137.     end
  138.    
  139.     if auto_grenade_types then
  140.         if grenadetype == 0 then
  141.             if tagType == "eqip" then
  142.                 if tagName == "weapons\\plasma grenade\\plasma grenade" then
  143.                     local x, y, z = getobjectcoords(m_objId)
  144.                     movobjcoords(m_objId, 0, 0, -100)
  145.                     local orientation = getobjectorientation(m_objId)
  146.                     createobject("eqip", "weapons\\frag grenade\\frag grenade", 0, 45, true, x, y, z, orientation)
  147.                 end
  148.             end
  149.         elseif grenadetype == 1 then
  150.             if tagType == "eqip" then
  151.                 if tagName == "weapons\\frag grenade\\frag grenade" then
  152.                     local x, y, z = getobjectcoords(m_objId)
  153.                     movobjcoords(m_objId, 0, 0, -100)
  154.                     local orientation = getobjectorientation(m_objId)
  155.                     createobject("eqip", "weapons\\plasma grenade\\plasma grenade", 0, 45, true, x, y, z, orientation)
  156.                 end
  157.             end
  158.         end
  159.     end
  160. end
  161.  
  162. function OnObjectInteraction(player, m_objId, tagType, tagName)
  163.    
  164.     return 1
  165. end
  166.  
  167. function OnWeaponAssignment(player, m_objId, slot, tagName)
  168.    
  169.     if weaps[slot] then
  170.         return lookuptag("weap", weaps[slot])
  171.     else
  172.         return lookuptag("weap", weaps[slot - 1])
  173.     end
  174.    
  175.     return 0
  176. end
  177.  
  178. function OnWeaponReload(player, m_weapId)
  179.  
  180.     return 1
  181. end
  182.  
  183. function OnDamageLookup(receiver, causer, tagData, tagName)
  184.  
  185.  
  186. end
  187.  
  188. function OnVehicleEntry(relevant, player, m_vehicleId, tagName, seat)
  189.    
  190.     return 1
  191. end
  192.  
  193. function OnVehicleEject(player, forced)
  194.  
  195.     return 1
  196. end
  197.  
  198. function OnClientUpdate(player, m_objId)
  199.  
  200.  
  201. end
  202.  
  203. -- Createobject override
  204.  
  205. phasor_createobject = createobject
  206. new_object_orientation = nil
  207.  
  208. spawns = {}
  209. objects = {}
  210.  
  211. registertimer(10, "ActiveObjectTimer")
  212.  
  213. function ActiveObjectTimer(id, count)
  214.  
  215.     for k,v in pairs(objects) do
  216.         if v.tagType == "weap" then
  217.             -- Player holding weapon?
  218.             local carried
  219.             for i = 0,15 do
  220.                 if gethash(i) then
  221.                     local m_player = getplayer(i)
  222.                     local m_objId = readdword(m_player, 0x34)
  223.                     local m_object = getobject(m_objId)
  224.                     if m_object then
  225.                         for x = 0,3 do
  226.                             local m_weapId = readdword(m_object, 0x2F8 + (x * 4))
  227.                             if m_weapId == k then
  228.                                 carried = true
  229.                                 break
  230.                             end
  231.                         end
  232.                        
  233.                         if carried then break end
  234.                     end
  235.                 end
  236.             end
  237.            
  238.             -- Determine if it is active or not
  239.             if not carried then
  240.                 if objects[k].active then
  241.                     objects[k].active = false
  242.                     local timerid = registertimer(spawns[objects[k].spawn].respawn_time * 1000, "DespawnTimer", k)
  243.                     objects[k].desptimer = timerid
  244.                 end
  245.             else
  246.                 if not objects[k].active then
  247.                     objects[k].active = true
  248.                     removetimer(objects[k].desptimer)
  249.                 end
  250.             end
  251.         elseif v.tagType == "vehi" then
  252.             local active
  253.             for i = 0,15 do
  254.                 if gethash(i) then
  255.                     local m_player = getplayer(i)
  256.                     local m_objId = readdword(m_player, 0x34)
  257.                     local m_object = getobject(m_objId)
  258.                     if m_object then
  259.                         local m_vehicleId = readdword(m_object, 0x11C)
  260.                         if m_vehicleId == k then
  261.                             active = true
  262.                             break
  263.                         end
  264.                     end
  265.                 end
  266.             end
  267.            
  268.             if active then
  269.                 if not objects[k].active then
  270.                     objects[k].active = true
  271.                     removetimer(objects[k].desptimer)
  272.                     removetimer(spawns[objects[k].spawn].resptimer)
  273.                 end
  274.             else
  275.                 if objects[k].active then
  276.                     objects[k].active = false
  277.                     local desptimerid = registertimer(spawns[objects[k].spawn].respawn_time * 1000, "DespawnTimer", k)
  278.                     local resptimerid = registertimer(spawns[objects[k].spawn].respawn_time * 1000, "RespawnTimer", objects[k].spawn)
  279.                     objects[k].desptimer = desptimerid
  280.                     spawns[objects[k].spawn].resptimer = resptimerid
  281.                 end
  282.             end
  283.         end
  284.     end
  285.    
  286.     return 1
  287. end
  288.  
  289. function createobject(tagType, tagName, parentId, respawn_time, respawn_bool, x, y, z, orientation)
  290.  
  291.     local m_objId
  292.    
  293.     -- Nil checks
  294.     x = x or 0
  295.     y = y or 0
  296.     z = z or 0
  297.     orientation = orientation or 0
  298.    
  299.     if respawn_time > 0 then
  300.         new_object_orientation = orientation
  301.         m_objId = phasor_createobject(tagType, tagName, parentId, 0, false, x, y, z)
  302.         if m_objId then
  303.             rotateobject(m_objId, orientation)
  304.             objects[m_objId] = {}
  305.             objects[m_objId].tagType = tagType
  306.             objects[m_objId].tagName = tagName
  307.             objects[m_objId].active = false
  308.             local desptimerid = registertimer(respawn_time * 1000, "DespawnTimer", m_objId)
  309.             local resptimerid
  310.             if respawn_bool then
  311.                 resptimerid = registertimer(respawn_time * 1000, "RespawnTimer", #spawns + 1)
  312.             end
  313.             local temp = {}
  314.             temp.tagType = tagType
  315.             temp.tagName = tagName
  316.             temp.parentId = parentId
  317.             temp.respawn_time = respawn_time
  318.             temp.respawn_bool = true
  319.             temp.x = x
  320.             temp.y = y
  321.             temp.z = z
  322.             temp.orientation = orientation
  323.             temp.resptimer = resptimerid
  324.             table.insert(spawns, temp)
  325.             objects[m_objId].spawn = #spawns
  326.             objects[m_objId].desptimer = desptimerid
  327.         else
  328.             hprintf("Invalid tag")
  329.         end
  330.     else
  331.         new_object_orientation = orientation
  332.         m_objId = phasor_createobject(tagType, tagName, parentId, 0, false, x, y, z)
  333.        
  334.         if not m_objId then
  335.             hprintf("Invalid tag")
  336.         end
  337.     end
  338.  
  339.     return m_objId
  340. end
  341.  
  342. function respawnobject(key)
  343.  
  344.     if spawns[key] then
  345.         new_object_orientation = spawns[key].orientation
  346.         local m_objId = phasor_createobject(spawns[key].tagType, spawns[key].tagName, spawns[key].parentId, spawns[key].respawn_time, spawns[key].respawn_bool, spawns[key].x, spawns[key].y, spawns[key].z)
  347.         rotateobject(m_objId, spawns[key].orientation)
  348.         local timerid = registertimer(spawns[key].respawn_time * 1000, "DespawnTimer", m_objId)
  349.         objects[m_objId] = {}
  350.         objects[m_objId].tagType = spawns[key].tagType
  351.         objects[m_objId].tagName = spawns[key].tagName
  352.         objects[m_objId].active = false
  353.         objects[m_objId].spawn = key
  354.         objects[m_objId].desptimer = timerid
  355.     end
  356. end
  357.  
  358. function rotateobject(m_objId, orientation)
  359.  
  360.     local m_object = getobject(m_objId)
  361.     if m_object then
  362.         local x_comp = -math.sin(orientation)
  363.         local y_comp = -math.cos(orientation)
  364.         writebit(m_object, 0x10, 2, 0)
  365.         writefloat(m_object, 0x74, x_comp)
  366.         writefloat(m_object, 0x78, y_comp)
  367.     end
  368. end
  369.  
  370. function getobjectorientation(m_objId)
  371.  
  372.     local m_object = getobject(m_objId)
  373.     local x_vector = readfloat(m_object, 0x74)
  374.     return -math.asin(x_vector)
  375. end
  376.  
  377. function RespawnTimer(id, count, spawn)
  378.  
  379.     respawnobject(spawn)
  380.     return 1
  381. end
  382.  
  383. function DespawnTimer(id, count, m_objId)
  384.  
  385.     if getobject(m_objId) then
  386.         destroyobject(m_objId)
  387.         objects[m_objId] = nil
  388.     end
  389.    
  390.     return 0
  391. end
  392.  
  393. function getobjecttag(m_objId)
  394.  
  395.     local m_object = getobject(m_objId)
  396.     local object_map_id = readdword(m_object, 0x0)
  397.  
  398.     local map_pointer = 0x460678
  399.     local map_base = readdword(map_pointer, 0x0)
  400.     local map_tag_count = todec(endian(map_base, 0xC, 0x3))
  401.     local tag_table_base = map_base + 0x28
  402.     local tag_table_size = 0x20
  403.  
  404.     for i = 0, (map_tag_count - 1) do
  405.         local tag_id = todec(endian(tag_table_base, 0xC + (tag_table_size * i), 0x3))
  406.  
  407.         if tag_id == object_map_id then
  408.             local tag_class = readstring(tag_table_base, (tag_table_size * i), 0x3, 1)
  409.             local tag_name_address = endian(tag_table_base, 0x10 + (tag_table_size * i), 0x3)
  410.             local tag_name = readtagname("0x" .. tag_name_address)
  411.  
  412.             return tag_class, tag_name
  413.         end
  414.     end
  415. end
  416.  
  417. function readstring(address, offset, length, endian)
  418.  
  419.     local char_table = {}
  420.     local string = ""
  421.     for i=0,length do  
  422.         if readbyte(address, (offset + (0x1 * i))) ~= 0 then       
  423.             table.insert(char_table, string.char(readbyte(address, (offset + (0x1 * i)))))         
  424.         end    
  425.     end
  426.     for k,v in pairs(char_table) do
  427.         if endian == 1 then    
  428.             string = v .. string           
  429.         else       
  430.             string = string .. v           
  431.         end    
  432.     end
  433.  
  434.     return string
  435. end
  436.  
  437. function readtagname(address)
  438.  
  439.     local char_table = {}
  440.     local i = 0
  441.     local string = ""
  442.     while readbyte(address, (0x1 * i)) ~= 0 do 
  443.         table.insert(char_table, string.char(readbyte(address, (0x1 * i))))
  444.         i = i + 1      
  445.     end
  446.     for k,v in pairs(char_table) do
  447.         string = string .. v       
  448.     end
  449.  
  450.     return string
  451. end
  452.  
  453. function endian(address, offset, length)
  454.  
  455.     local data_table = {}
  456.     local data = ""
  457.     for i=0,length do
  458.         local hex = string.format("%X", readbyte(address, offset + (0x1 * i)))
  459.         if tonumber(hex, 16) < 16 then     
  460.             hex = 0 .. hex         
  461.         end
  462.         table.insert(data_table, hex)
  463.     end
  464.     for k,v in pairs(data_table) do
  465.         data = v .. data
  466.     end
  467.  
  468.     return data
  469. end
  470.  
  471. function tohex(number)
  472.  
  473.     return string.format("%X", number)
  474. end
  475.  
  476. function todec(number)
  477.  
  478.     return tonumber(number, 16)
  479. end
  480.  
  481. function opairs(t)
  482.    
  483.     local keys = {}
  484.     for k,v in pairs(t) do
  485.         table.insert(keys, k)
  486.     end
  487.     table.sort(keys,
  488.     function(a,b)
  489.         if type(a) == "number" and type(b) == "number" then
  490.             return a < b
  491.         end
  492.         an = string.lower(tostring(a))
  493.         bn = string.lower(tostring(b))
  494.         if an ~= bn then
  495.             return an < bn
  496.         else
  497.             return tostring(a) < tostring(b)
  498.         end
  499.     end)
  500.     local count = 1
  501.     return function()
  502.         if unpack(keys) then
  503.             local key = keys[count]
  504.             local value = t[key]
  505.             count = count + 1
  506.             return key,value
  507.         end
  508.     end
  509. end
  510.  
  511. function table.find(t, val)
  512.  
  513.     for k,v in pairs(t) do
  514.         if v == val then
  515.             return k
  516.         end
  517.     end
  518. end
  519.  
  520. math.inf = 1 / 0
  521.  
  522. attributes = {}
  523.  
  524. attributes["weapons\\assault rifle\\assault rifle"] = {"Short-Range", "Rifle", "Human"}
  525. attributes["weapons\\pistol\\pistol"] = {"Mid-Range", "Accurate", "Human"}
  526. attributes["weapons\\shotgun\\shotgun"] = {"Short-Range", "Inaccurate", "Human"}
  527. attributes["weapons\\sniper rifle\\sniper rifle"] = {"Long-Range", "Accurate", "Human"}
  528. attributes["weapons\\flamethrower\\flamethrower"] = {"Short-Range", "Heavy", "Human"}
  529. attributes["weapons\\rocket launcher\\rocket launcher"] = {"Mid-Range", "Heavy", "Human"}
  530. attributes["weapons\\plasma pistol\\plasma pistol"] = {"Mid-Range", "Accurate", "Covenant"}
  531. attributes["weapons\\plasma rifle\\plasma rifle"] = {"Mid-Range", "Rifle", "Covenant"}
  532. attributes["weapons\\needler\\mp_needler"] = {"Mid-Range", "Inaccurate", "Covenant"}
  533. attributes["weapons\\plasma_cannon\\plasma_cannon"] = {"Mid-Range", "Heavy", "Covenant"}
  534.  
  535. function similar(tagName)
  536.  
  537.     local max = -math.inf
  538.     local newtag = ""
  539.     for k,v in opairs(weaps) do
  540.         local count = 0
  541.         for _,a in ipairs(attributes[tagName]) do
  542.             if table.find(attributes[v], a) then
  543.                 count = count + 1
  544.             end
  545.         end
  546.        
  547.         if count > max then
  548.             newtag = v
  549.             max = count
  550.         end
  551.     end
  552.    
  553.     return newtag
  554. end
  555.  
  556. function getgrenadetype()
  557.  
  558.     local human, covenant
  559.     for k,v in pairs(weaps) do
  560.         if table.find(attributes[v], "Human") then
  561.             human = true
  562.         else
  563.             covenant = true
  564.         end
  565.     end
  566.    
  567.     -- Human and Covenant = 2; Covenant = 1; Human = 0
  568.     if human and covenant then
  569.         return 2
  570.     elseif not human and covenant then
  571.         return 1
  572.     else
  573.         return 0
  574.     end
  575. end
  576.  
  577. function formatbinary(num, digits)
  578.  
  579.     local binary = convertbase(num, 2)
  580.     while string.len(binary) < digits do
  581.         binary = "0" .. binary
  582.     end
  583.    
  584.     return binary
  585. end
  586.  
  587. function convertbase(input, base)
  588.  
  589.     if not base or base == 10 then return tostring(input) end
  590.  
  591.     local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  592.     local answer = {}
  593.  
  594.     repeat
  595.         local digit = (input % base) + 1
  596.         input = math.floor(input / base)
  597.         table.insert(answer, 1, string.sub(digits, digit, digit))
  598.     until input == 0
  599.  
  600.     return table.concat(answer, "")
  601. end
  602.  
  603. grenadetype = getgrenadetype()
  604. parameters = formatbinary(readbyte(0x671340, 0x38), 8)
  605. infinite_grenades = tonumber(string.sub(parameters, 6, 6))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement