SHARE
TWEET

Untitled

a guest Jun 22nd, 2015 554 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ------------------------------------------------------------------------------
  2. -- Module API
  3. ------------------------------------------------------------------------------
  4.  
  5. -- Global constants
  6.  
  7. -- Init player
  8. hc.SCRIPT_INIT = 1
  9. hc.PLAYER_JOIN = 2
  10.  
  11.  
  12. function hc.main.init()
  13.  
  14.     hc.players = {}
  15.  
  16.     -- Internal constants and variables
  17.     hc.main.hooks = {}
  18.     hc.main.real_hooks = {}
  19.     --    hc.main.images     = {}
  20.     hc.main.functions = {}
  21.  
  22.     for line in io.lines(hc.DIR .. "/core/version.txt") do
  23.         hc.VERSION = line
  24.     end
  25.  
  26.     print("HC CS2D Admin Script " .. hc.VERSION)
  27.  
  28.     -- Replace some original CS2D functions with our own ones
  29.     hc.main.cs2d_addhook = addhook
  30.     hc.main.cs2d_freehook = freehook
  31.     --[[
  32.         hc.main.cs2d_image     = image
  33.         hc.main.cs2d_freeimage = freeimage
  34.     ]]
  35.  
  36.     addhook = hc.main.add_hook
  37.     freehook = hc.main.free_hook
  38.     --[[
  39.         image     = hc.main.image
  40.         freeimage = hc.main.free_image
  41.     ]]
  42.  
  43.     -- Proxy hooks
  44.     hc.main.add_proxy_hook("join", "hc.main.join_proxy_hook")
  45.     hc.main.add_proxy_hook("team", "hc.main.team_proxy_hook")
  46.     hc.main.add_proxy_hook("spawn", "hc.main.spawn_proxy_hook")
  47.     hc.main.add_proxy_hook("die", "hc.main.die_proxy_hook")
  48.     hc.main.add_proxy_hook("build", "hc.main.build_proxy_hook")
  49.     hc.main.add_proxy_hook("flagcapture", "hc.main.flagcapture_proxy_hook")
  50.     hc.main.add_proxy_hook("dominate", "hc.main.dominate_proxy_hook")
  51.     hc.main.add_proxy_hook("bombplant", "hc.main.bombplant_proxy_hook")
  52.     hc.main.add_proxy_hook("bombdefuse", "hc.main.bombdefuse_proxy_hook")
  53.     hc.main.add_proxy_hook("bombexplode", "hc.main.bombexplode_proxy_hook")
  54.     hc.main.add_proxy_hook("say", "hc.main.say_proxy_hook")
  55.     hc.main.add_proxy_hook("sayteam", "hc.main.sayteam_proxy_hook")
  56.     hc.main.add_proxy_hook("radio", "hc.main.radio_proxy_hook")
  57.     hc.main.add_proxy_hook("endround", "hc.main.endround_proxy_hook")
  58.     hc.main.add_proxy_hook("serveraction", "hc.main.serveraction_proxy_hook")
  59.     hc.main.add_proxy_hook("name", "hc.main.name_proxy_hook")
  60.     hc.main.add_proxy_hook("objectdamage", "hc.main.objectdamage_proxy_hook")
  61.     hc.main.add_proxy_hook("init")
  62.     hc.main.add_proxy_hook("init_player")
  63.     hc.main.add_proxy_hook("delete_player")
  64.     hc.main.add_proxy_hook("post_join")
  65.     hc.main.add_proxy_hook("player_level")
  66.  
  67.     --    hc.main.add_proxy_hook("remove")
  68.  
  69.     -- "Real" hooks
  70.     --    addhook("endround",       "hc.main.endround_hook",      99999)
  71.     addhook("menu", "hc.main.menu_hook", -99999)
  72.     addhook("join", "hc.main.pre_join_hook", -99999)
  73.     addhook("join", "hc.main.post_join_hook", 99999)
  74.     --    addhook("leave",          "hc.main.pre_leave_hook",    -99999)
  75.     addhook("leave", "hc.main.post_leave_hook", 99999)
  76.     addhook("spawn", "hc.main.spawn_hook", 99999)
  77.     addhook("minute", "hc.main.minute_hook", -99999)
  78.     addhook("team", "hc.main.team_hook", -99999)
  79.  
  80.     --    addhook("remove",         "hc.main.remove_hook",        99999)
  81.  
  82.     -- Special hooks
  83.     addhook("init_player", "hc.main.init_player_hook", -99999)
  84. end
  85.  
  86.  
  87. ------------------------------------------------------------------------------
  88. -- API
  89. ------------------------------------------------------------------------------
  90.  
  91. function hc.set_no_real_kill(p)
  92.     hc.players[p].main.real_kill = false
  93. end
  94.  
  95. function hc.set_no_real_death(p)
  96.     hc.players[p].main.real_death = false
  97. end
  98.  
  99. function hc.is_real_kill(p)
  100.     return hc.players[p].main.real_kill == nil
  101. end
  102.  
  103. function hc.is_real_death(p)
  104.     return hc.players[p].main.real_death == nil
  105. end
  106.  
  107. function hc.get_usgn(p)
  108.     if hc.players[p] == nil or hc.players[p].main == nil then
  109.         return player(p, "usgn")
  110.     else
  111.         return hc.players[p].main.usgn
  112.     end
  113. end
  114.  
  115. function hc.show_menu(p, title, ids, command)
  116.     local start
  117.  
  118.     if not ids then
  119.         ids = hc.players[p].main.id_table
  120.         start = hc.players[p].main.start_id
  121.     else
  122.     --if #ids == 0 then
  123.     --    msg2(p, "No entries to show.")
  124.     --end
  125.         hc.players[p].main.id_table = ids
  126.         start = nil
  127.     end
  128.  
  129.     hc.players[p].main.menu_table = {}
  130.  
  131.     local hcmenu = title:gsub(",", ".")
  132.     local menu_id = 1
  133.     local last_id
  134.  
  135.     for id,title in next, ids, start do
  136.         local next_menu_id, next_value = next(ids, id)
  137.         -- and next_menu_id ~= nil
  138.         if menu_id >= hc.NUM_MENU_ITEMS and (next_menu_id ~= nil or start ~= nil) then
  139.             hcmenu = hcmenu .. ",Next >"
  140.             hc.players[p].main.menu_table[menu_id] = 0
  141.             hc.players[p].main.start_id = last_id
  142.             menu_id = menu_id + 1
  143.             break
  144.         else
  145.             if type(title) == "table" then
  146.                 title = title.title
  147.             end
  148.             -- Comma (',') is not allowed in menu titles
  149.             hcmenu = hcmenu .. "," .. title:gsub(",", ".")
  150.             hc.players[p].main.menu_table[menu_id] = id
  151.             menu_id = menu_id + 1
  152.             last_id = id
  153.         end
  154.     end
  155.     if start ~= nil and menu_id < hc.NUM_MENU_ITEMS + 1 then
  156.         hc.players[p].main.menu_table[hc.NUM_MENU_ITEMS] = 0
  157.         hc.players[p].main.start_id = nil
  158.         for i=menu_id,hc.NUM_MENU_ITEMS - 1 do
  159.             hcmenu = hcmenu .. ","
  160.         end
  161.         hcmenu = hcmenu .. ",<< Start"
  162.     end
  163.  
  164.     hc.players[p].main.menu_command = command
  165.     menu(p, hcmenu)
  166. end
  167.  
  168.  
  169. ------------------------------------------------------------------------------
  170. -- CS2D function replacements
  171. ------------------------------------------------------------------------------
  172.  
  173. -- addhook
  174. function hc.main.add_hook(name, func, prio)
  175.     if prio == nil then
  176.         prio = 0
  177.     end
  178.  
  179.     if hc.main.hooks[name] == nil then
  180.     -- Use CS2D's addhook
  181.         hc.main.add_real_hook(name, func, prio)
  182.     else
  183.         local entry = { name = func, prio = prio }
  184.         for i,hk in ipairs(hc.main.hooks[name]) do
  185.             if hk.prio >= prio then
  186.                 table.insert(hc.main.hooks[name], i, entry)
  187.                 return
  188.             end
  189.         end
  190.         table.insert(hc.main.hooks[name], entry)
  191.     end
  192. end
  193.  
  194. -- freehook
  195. function hc.main.free_hook(name, func)
  196.     if hc.main.hooks[name] == nil then
  197.     -- Use CS2D's freehook
  198.         hc.main.free_real_hook(name, func)
  199.     else
  200.         for i,hk in ipairs(hc.main.hooks[name]) do
  201.             if hk.name == func then
  202.                 table.remove(i)
  203.                 return
  204.             end
  205.         end
  206.         print("Error: Hook not found: " .. func)
  207.     end
  208. end
  209.  
  210. --[[
  211. -- image
  212. function hc.main.image(path, x, y, mode)
  213.     local id = hc.main.cs2d_image(path, x, y, mode)
  214.     hc.main.images[id] = true
  215.     return id
  216. end
  217. ]]
  218.  
  219. --[[
  220. -- freeimage
  221. function hc.main.free_image(id)
  222.     if hc.main.images[id] then
  223.         hc.main.images[id] = nil
  224.     else
  225.         print("Error: Image not found: "..id)
  226.     end
  227.     hc.main.cs2d_freeimage(id)
  228. end
  229. ]]
  230.  
  231.  
  232. ------------------------------------------------------------------------------
  233. -- Internal functions
  234. ------------------------------------------------------------------------------
  235.  
  236. function hc.main.add_proxy_hook(hook, func)
  237.     hc.main.hooks[hook] = {}
  238.     if func then
  239.         hc.main.add_real_hook(hook, func)
  240.     end
  241. end
  242.  
  243. function hc.main.call_function(name, ...)
  244.     local ok, result = pcall(hc.main.get_function(name), ...)
  245.  
  246.     if ok then
  247.         return result
  248.     else
  249.         print("Error: " .. result)
  250.     end
  251. end
  252.  
  253. function hc.main.get_function(name)
  254.     if hc.main.functions[name] == nil then
  255.         assert(loadstring("hc.main.get_function_func = " .. name))()
  256.         if hc.main.get_function_func == nil then
  257.             print("Error: Undefined function '" .. name .. "'!")
  258.             hc.main.functions[name] = function() end
  259.         else
  260.             hc.main.functions[name] = hc.main.get_function_func
  261.         end
  262.     end
  263.     return hc.main.functions[name]
  264. end
  265.  
  266. function hc.main.add_real_hook(name, func, prio)
  267.     if hc.main.real_hooks[name] == nil then
  268.         hc.main.real_hooks[name] = {}
  269.     end
  270.     table.insert(hc.main.real_hooks[name], func)
  271.  
  272.     if prio == nil then
  273.         prio = 0
  274.     end
  275.     hc.main.cs2d_addhook(name, func, prio)
  276. end
  277.  
  278. function hc.main.free_real_hook(name, func, prio)
  279.     local id = hc.find_in_table(hc.main.real_hooks[name], func)
  280.     if id > 0 then
  281.         table.remove(hc.main.real_hooks[name], id)
  282.     else
  283.         print("Error: Hook not found: " .. func)
  284.     end
  285. end
  286.  
  287. -- Calls all hooks.
  288. -- Aborts if a hook returns a value other than nil, "" or 0, unless returns_value is set.
  289. -- If it is, all hooks will be called, but only the value from the first hook
  290. -- that returned something will be returned.
  291. function hc.main.call_hook(name, returns_value, ...)
  292.     local result = nil
  293.  
  294.     if hc.main.hooks[name] then
  295.         for _,entry in ipairs(hc.main.hooks[name]) do
  296.             local ret = hc.main.call_function(entry.name, ...)
  297.  
  298.             if not (ret == nil or ret == "" or ret == 0) then
  299.                 if returns_value then
  300.                     if result == nil then
  301.                         result = ret
  302.                     end
  303.                 else
  304.                     return ret
  305.                 end
  306.             end
  307.         end
  308.     end
  309.     return result
  310. end
  311.  
  312. function hc.main.call_acc_hook(name, acc, ...)
  313.     if hc.main.hooks[name] then
  314.         for _,entry in ipairs(hc.main.hooks[name]) do
  315.             acc = hc.main.call_function(entry.name, acc, ...)
  316.         end
  317.     end
  318.     return acc
  319. end
  320.  
  321. function hc.main.init_player(p, reason)
  322. --local old_player = hc.players[p]
  323.     hc.players[p] = {}
  324.     hc.main.call_hook("init_player", false, p, reason)
  325. end
  326.  
  327. function hc.main.init_players(reason)
  328.     for i=1,hc.SLOTS do
  329.         if player(i, "exists") then
  330.             hc.main.init_player(i, reason)
  331.         else
  332.             hc.players[i] = nil
  333.         end
  334.     end
  335. end
  336.  
  337.  
  338. ------------------------------------------------------------------------------
  339. -- Proxy hooks
  340. ------------------------------------------------------------------------------
  341.  
  342. function hc.main.join_proxy_hook(...)
  343.     return hc.main.call_hook("join", false, ...)
  344. end
  345.  
  346. function hc.main.team_proxy_hook(...)
  347.     return hc.main.call_hook("team", false, ...)
  348. end
  349.  
  350. function hc.main.spawn_proxy_hook(...)
  351.     return hc.main.call_hook("spawn", true, ...)
  352. end
  353.  
  354. function hc.main.die_proxy_hook(...)
  355.     return hc.main.call_hook("die", true, ...)
  356. end
  357.  
  358. function hc.main.build_proxy_hook(...)
  359.     return hc.main.call_hook("build", false, ...)
  360. end
  361.  
  362. function hc.main.flagcapture_proxy_hook(...)
  363.     return hc.main.call_hook("flagcapture", false, ...)
  364. end
  365.  
  366. function hc.main.dominate_proxy_hook(...)
  367.     return hc.main.call_hook("dominate", false, ...)
  368. end
  369.  
  370. function hc.main.bombplant_proxy_hook(...)
  371.     return hc.main.call_hook("bombplant", false, ...)
  372. end
  373.  
  374. function hc.main.bombdefuse_proxy_hook(...)
  375.     return hc.main.call_hook("bombdefuse", false, ...)
  376. end
  377.  
  378. function hc.main.bombexplode_proxy_hook(...)
  379.     return hc.main.call_hook("bombexplode", false, ...)
  380. end
  381.  
  382. function hc.main.say_proxy_hook(...)
  383.     return hc.main.call_hook("say", false, ...)
  384. end
  385.  
  386. function hc.main.sayteam_proxy_hook(...)
  387.     return hc.main.call_hook("sayteam", false, ...)
  388. end
  389.  
  390. function hc.main.radio_proxy_hook(...)
  391.     return hc.main.call_hook("radio", false, ...)
  392. end
  393.  
  394. function hc.main.endround_proxy_hook(...)
  395.     return hc.main.call_hook("endround", false, ...)
  396. end
  397.  
  398. function hc.main.serveraction_proxy_hook(...)
  399.     return hc.main.call_hook("serveraction", false, ...)
  400. end
  401.  
  402. function hc.main.name_proxy_hook(...)
  403.     return hc.main.call_hook("name", false, ...)
  404. end
  405.  
  406. function hc.main.objectdamage_proxy_hook(...)
  407.     return hc.main.call_hook("objectdamage", false, ...)
  408. end
  409.  
  410. function hc.main.post_join_proxy_hook(p)
  411.     hc.main.call_hook("post_join", false, tonumber(p))
  412. end
  413.  
  414.  
  415. ------------------------------------------------------------------------------
  416. -- Hooks
  417. ------------------------------------------------------------------------------
  418.  
  419. function hc.main.menu_hook(p, title, button)
  420.     if button == 0 then
  421.     -- Cancel button pressed
  422.         return
  423.     end
  424.  
  425.     if hc.players[p].main.menu_table ~= nil then
  426.         local id = hc.players[p].main.menu_table[button]
  427.         hc.players[p].main.menu_table = nil
  428.         if id == 0 then
  429.         -- Show the next menu
  430.             hc.show_menu(p, title, nil, hc.players[p].main.menu_command)
  431.         else
  432.             local item = hc.players[p].main.id_table[id]
  433.             hc.players[p].main.menu_command(p, id, item)
  434.         end
  435.     end
  436. end
  437.  
  438. --[[
  439. function hc.main.endround_hook(mode)
  440.     -- All images are freed at round end
  441.     hc.main.images = {}
  442. end
  443. ]]
  444.  
  445. function hc.main.init_player_hook(p, reason)
  446.     hc.players[p].main = {}
  447.     hc.players[p].main.usgn = player(p, "usgn")
  448. end
  449.  
  450. function hc.main.pre_join_hook(p)
  451.     hc.info(p, "Welcome, " .. player(p, "name") .. "!")
  452.     if hc.is_moderator(p) then
  453.         if hc.MODERATOR_WELCOME_MSG then
  454.             hc.info(p, hc.MODERATOR_WELCOME_MSG)
  455.         end
  456.     elseif hc.is_vip(p) then
  457.         if hc.VIP_WELCOME_MSG then
  458.             hc.info(p, hc.VIP_WELCOME_MSG)
  459.         end
  460.     else
  461.         if hc.USER_WELCOME_MSG then
  462.             hc.info(p, hc.USER_WELCOME_MSG)
  463.         end
  464.     end
  465.     if hc.COMMON_WELCOME_MSG then
  466.         hc.info(p, hc.COMMON_WELCOME_MSG)
  467.     end
  468.  
  469.     hc.main.init_player(p, hc.PLAYER_JOIN)
  470. end
  471.  
  472. function hc.main.post_join_hook(p)
  473.     timer(500, "hc.main.post_join_proxy_hook", tostring(p))
  474. end
  475.  
  476. function hc.main.team_hook(p)
  477. -- Bots don't join, so need to handle them separately
  478.     if hc.players[p] == nil and player(p, "bot") then
  479.         hc.main.call_hook("join", false, p)
  480.     --hc.main.call_hook("team", false, p, player(p, "team"), 0)
  481.     end
  482. end
  483.  
  484. --[[
  485. function hc.main.pre_leave_hook(p, reason)
  486.     hc.players[p].main.leaving = true
  487. end
  488. ]]
  489.  
  490. function hc.main.post_leave_hook(p, reason)
  491.     if hc.players[p] ~= nil then
  492.         hc.players[p].main.leaving = true
  493.         hc.main.call_hook("delete_player", false, p, reason)
  494.         hc.players[p] = nil
  495.     end
  496. end
  497.  
  498. function hc.main.spawn_hook(p)
  499.     hc.players[p].main.real_kill = nil
  500.     hc.players[p].main.real_death = nil
  501. end
  502.  
  503. function hc.main.minute_hook()
  504.     hc.event(hc.PERIODIC_MSG)
  505. end
  506.  
  507. --function hc.main.remove_hook(mode)
  508. --end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top