Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.90 KB | None | 0 0
  1. --[[
  2. gamemodes/egmrp/gamemode/shared.lua
  3. --]]
  4. ---------------------------------------
  5.  
  6. ---------------------------------------
  7.  
  8. -- Einfach Gaming Roleplay --
  9.  
  10. -- (EGM:RP) --
  11.  
  12. -- --
  13.  
  14. -- Created by --
  15.  
  16. -- Pascal 'P4sca1' Sthamer, --
  17.  
  18. -- Jan 'Oninoni' Ziegler, --
  19.  
  20. -- Konstantin 'Airfox' Zisiadis, --
  21.  
  22. -- Mattis 'Mattzi' Krämer --
  23.  
  24. -- --
  25.  
  26. -- This software is only allowed --
  27.  
  28. -- to be used with a valid license --
  29.  
  30. -- --
  31.  
  32. -- Copyright © 2018 Pascal Sthamer --
  33.  
  34. ---------------------------------------
  35.  
  36. ---------------------------------------
  37.  
  38.  
  39.  
  40. ---------------------------------------
  41.  
  42. -- Shared | Initialization --
  43.  
  44. ---------------------------------------
  45.  
  46.  
  47.  
  48. DeriveGamemode("sandbox")
  49.  
  50.  
  51.  
  52. -- Gamemode variables
  53.  
  54. GM.Name = "EGM:RP"
  55.  
  56. GM.Author = "P4sca1, Oninoni, Mattzi, Konsti"
  57.  
  58. GM.Email = "admin@einfach-gaming.de"
  59.  
  60. GM.TeamBased = false
  61.  
  62. GM.IsSandboxDerived = true
  63.  
  64. GM.Website = "www.einfach-gaming.de"
  65.  
  66. GM.Version = "1.13.0"
  67.  
  68. GM.LoadedModules = {}
  69.  
  70.  
  71.  
  72. include("eula.lua")
  73.  
  74.  
  75.  
  76. -- Enter your License Key here.
  77.  
  78. GM.LicenseKey = "LCwY8UaEglKy6BaEjLOCj5WmHpC6u8atStJilTTE3J3FD2elBcgBqr0wSOKANGE1"
  79.  
  80.  
  81.  
  82. -- Load required files
  83.  
  84. if SERVER then
  85.  
  86. AddCSLuaFile("sh_config.lua")
  87.  
  88. AddCSLuaFile("sh_utils.lua")
  89.  
  90.  
  91.  
  92. include("sh_config.lua")
  93.  
  94. include("sh_utils.lua")
  95.  
  96. include("sv_content.lua")
  97.  
  98.  
  99.  
  100. LogInfo("Loading DRM System ...")
  101.  
  102. require("egmrp_drm")
  103.  
  104. LogInfo("DRM System Loaded!")
  105.  
  106.  
  107.  
  108. require("mysqloo")
  109.  
  110. end
  111.  
  112.  
  113.  
  114. if CLIENT then
  115.  
  116. include("sh_config.lua")
  117.  
  118. include("sh_utils.lua")
  119.  
  120. end
  121.  
  122.  
  123.  
  124. -------------------------------------------------------------------------------------------------------------------------------
  125.  
  126. -- Important notice for using the GM and GAMEMODE table inside egmrp:
  127.  
  128. --
  129.  
  130. -- 1) The GM table only exists when a gamemode is currently being loaded (also on file refresh).
  131.  
  132. -- It contains the gamemode table of the gamemode that is currently being loaded.
  133.  
  134. -- 2) The GAMEMODE table does only exist after the active gamemode is loaded (PostGamemodeLoaded).
  135.  
  136. -- It always contains the gamemode table of the active gamemode.
  137.  
  138. -- 3) When calling a gamemode function, the one of the active gamemode is always used (not the one of any deriving gamemode),
  139.  
  140. -- BUT when loading a derived gamemode, all it's gamemode functions are pushed to the active gamemode.
  141.  
  142. -- So when refreshing a file in egmrp, all GM functions are pushed to GAMEMODE.
  143.  
  144. -- Therefore we need to use GM for gamemode functions when a file got changed.
  145.  
  146. -- The problem is that we can't use GM on initialize because it does not exist, because we are loading egmrp
  147.  
  148. -- after the deriving gamemode (because the deriving gamemode contains info about which configs / modules to use).
  149.  
  150. -- We have to use GAMEMODE for gamemode functions on initialize. That is the reason why we have to use
  151.  
  152. -- `GM or GAMEMODE` when writing gamemode functions in egmrp.
  153.  
  154. --
  155.  
  156. -- => When GM exists, use this table because changes are pushed to GAMEMODE.
  157.  
  158. -- Otherwise use GAMEMODE, because changes are not overwritten.
  159.  
  160. --------------------------------------------------------------------------------------------------------------------------------
  161.  
  162.  
  163.  
  164. GM.DRMLoadingTries = 0
  165.  
  166.  
  167.  
  168. -- Load the gamemode including all core modules and activated modules.
  169.  
  170. -- Called after the deriving gamemode was loaded (PostGamemodeLoaded) or
  171.  
  172. -- when there was a file refresh.
  173.  
  174. local function LoadGamemode()
  175.  
  176. // This loads the DRM. The DRM Contains vital Functions so if it is not successfull the Gamemode cannot run and loading is aborted.
  177.  
  178. if SERVER then
  179.  
  180. local success, e = InitDRM()
  181.  
  182.  
  183.  
  184. if not success then
  185.  
  186. // Override Check Password to prevent Joining while loading Gamemode.
  187.  
  188. GAMEMODE.OldCheckPassword = GAMEMODE.CheckPassword
  189.  
  190. GAMEMODE.CheckPassword = function()
  191.  
  192. return false, "Server startet noch!"
  193.  
  194. end
  195.  
  196.  
  197.  
  198. // If there are more than 10 tries sth is really going wrong.
  199.  
  200. if GAMEMODE.DRMLoadingTries > 10 then
  201.  
  202. LogWarning("DRM could not be Initialized. Please Contact our Support.")
  203.  
  204. LogError(e)
  205.  
  206. end
  207.  
  208.  
  209.  
  210. GAMEMODE.DRMLoadingTries = GAMEMODE.DRMLoadingTries +1
  211.  
  212. timer.Simple(1, LoadGamemode)
  213.  
  214.  
  215.  
  216. return
  217.  
  218. end
  219.  
  220.  
  221.  
  222. // Reset to old Function
  223.  
  224. if isfunction(GAMEMODE.OldCheckPassword) then
  225.  
  226. GAMEMODE.CheckPassword = GAMEMODE.OldCheckPassword
  227.  
  228. end
  229.  
  230. end
  231.  
  232.  
  233.  
  234. local folderName = GAMEMODE.FolderName
  235.  
  236. local configPath = folderName .. "/gamemode/config/"
  237.  
  238.  
  239.  
  240. -- Load gamemode config.
  241.  
  242. if SERVER then
  243.  
  244. AddCSLuaFile(configPath .. "gamemode.lua")
  245.  
  246. include(configPath .. "gamemode.lua")
  247.  
  248. else
  249.  
  250. include(configPath .. "gamemode.lua")
  251.  
  252. end
  253.  
  254.  
  255.  
  256. -- Load Localization first.
  257.  
  258. LoadModule("localization", true)
  259.  
  260.  
  261.  
  262. -- Load core modules.
  263.  
  264. LoadModule("sql", true)
  265.  
  266. LoadModule("ui", true)
  267.  
  268. LoadModule("scoreboard", true)
  269.  
  270. LoadModule("notify", true)
  271.  
  272. LoadModule("keybinds", true)
  273.  
  274. LoadModule("eventlog", true)
  275.  
  276. LoadModule("loading", true)
  277.  
  278. LoadModule("propertymodel", true)
  279.  
  280. LoadModule("player", true)
  281.  
  282. LoadModule("chat", true)
  283.  
  284. LoadModule("factionrank", true)
  285.  
  286. LoadModule("character", true)
  287.  
  288. LoadModule("deathsystem", true)
  289.  
  290. LoadModule("whitelist", true)
  291.  
  292. LoadModule("settings", true)
  293.  
  294. LoadModule("joinmanager", true)
  295.  
  296.  
  297.  
  298. -- Load activated modules.
  299.  
  300. for module, activated in pairs(Config.Modules) do
  301.  
  302. if activated then
  303.  
  304. LoadModule(module)
  305.  
  306. end
  307.  
  308. end
  309.  
  310.  
  311.  
  312. -- Overwrite module configs.
  313.  
  314. local files = file.Find(configPath .. "*.lua", "LUA")
  315.  
  316.  
  317.  
  318. for k, fileName in pairs(files) do
  319.  
  320. local split = string.Split(string.StripExtension(fileName), "_")
  321.  
  322. local realm = table.remove(split, 1)
  323.  
  324. local module = string.Implode("", split)
  325.  
  326.  
  327.  
  328. if module and IsModuleLoaded(module) then
  329.  
  330. if realm == "sv" then
  331.  
  332. if SERVER then
  333.  
  334. include(configPath .. fileName)
  335.  
  336. end
  337.  
  338. elseif realm == "sh" then
  339.  
  340. if SERVER then
  341.  
  342. AddCSLuaFile(configPath .. fileName)
  343.  
  344. end
  345.  
  346.  
  347.  
  348. include(configPath .. fileName)
  349.  
  350. elseif realm == "cl" then
  351.  
  352. if SERVER then
  353.  
  354. AddCSLuaFile(configPath .. fileName)
  355.  
  356. end
  357.  
  358.  
  359.  
  360. if CLIENT then
  361.  
  362. include(configPath .. fileName)
  363.  
  364. end
  365.  
  366. end
  367.  
  368. end
  369.  
  370. end
  371.  
  372.  
  373.  
  374. -- Load workshop content file.
  375.  
  376. if file.Exists("gamemodes/" .. GAMEMODE.BaseClass.FolderName .. "/gamemode/sv_content.lua", "GAME") then
  377.  
  378. include(GAMEMODE.BaseClass.FolderName .. "/gamemode/sv_content.lua")
  379.  
  380. end
  381.  
  382.  
  383.  
  384. if file.Exists("gamemodes/" .. GAMEMODE.FolderName .. "/gamemode/sv_content.lua", "GAME") then
  385.  
  386. include(GAMEMODE.FolderName .. "/gamemode/sv_content.lua")
  387.  
  388. end
  389.  
  390.  
  391.  
  392. hook.Run("EGMRP.Loaded")
  393.  
  394. end
  395.  
  396.  
  397.  
  398. -- Initialize the deriving gamemode after it finished loading.
  399.  
  400. hook.Add("PostGamemodeLoaded", "EGMRP.Load", function()
  401.  
  402. LoadGamemode()
  403.  
  404. end)
  405.  
  406.  
  407.  
  408. -- Support file system refresh.
  409.  
  410. -- Detect whether this is a file refresh or the first initialize (GAMEMODE will not be set on first initialize).
  411.  
  412. if GAMEMODE then
  413.  
  414. -- Set LoadedModules to an empty table, to force a refresh of all
  415.  
  416. -- modules when reinitializing the derived gamemode.
  417.  
  418. GAMEMODE.LoadedModules = {}
  419.  
  420. LoadGamemode()
  421.  
  422. end
  423.  
  424.  
  425.  
  426. -- Lag fix. For more info: https://github.com/Facepunch/garrysmod-issues/issues/2902
  427.  
  428. hook.Remove("PlayerTick", "TickWidgets")
  429.  
  430.  
  431.  
  432. -- Another lag fix. For more info: https://steamcommunity.com/sharedfiles/filedetails/?id=622814666
  433.  
  434. local table_insert = table.insert
  435.  
  436.  
  437.  
  438. hook.Add( "OnEntityCreated", "seats_network_optimizer", function( seat )
  439.  
  440. if seat:GetClass()=="prop_vehicle_prisoner_pod" then
  441.  
  442. seat:AddEFlags( EFL_NO_THINK_FUNCTION ) -- disable seat's Think
  443.  
  444. seat.seats_network_optimizer = true -- Now we know that this seat has been processed by this addon.
  445.  
  446. end
  447.  
  448. end )
  449.  
  450.  
  451.  
  452. local i
  453.  
  454. local seats
  455.  
  456. local last_enabled -- previously processed seat
  457.  
  458. -- This function enables the Think of one seat during each frame to save network traffic.
  459.  
  460. -- The Think of seats is only needed for animation handling.
  461.  
  462. hook.Add( "Think", "seats_network_optimizer", function()
  463.  
  464. -- Make list:
  465.  
  466. if not seats or not seats[i] then
  467.  
  468. -- Begin a new loop, with a new seats list.
  469.  
  470. i = 1
  471.  
  472. seats = {}
  473.  
  474. for _,seat in ipairs( ents.FindByClass( "prop_vehicle_prisoner_pod" ) ) do
  475.  
  476. if seat.seats_network_optimizer then
  477.  
  478. table.insert( seats, seat )
  479.  
  480. end
  481.  
  482. end
  483.  
  484. end
  485.  
  486. -- Find a valid seat:
  487.  
  488. while seats[i] and not IsValid( seats[i] ) do
  489.  
  490. -- Jump to the next valid seat.
  491.  
  492. i = i+1
  493.  
  494. end
  495.  
  496. local seat = seats[i]
  497.  
  498. -- Disable the previously processed seat's Think if ready:
  499.  
  500. if last_enabled~=seat and IsValid( last_enabled ) then -- ignore a seat's Think that is gonna be re-enabled
  501.  
  502. -- last_enabled's Think is kept enabled until m_bEnterAnimOn and m_bExitAnimOn are reset.
  503.  
  504. local saved = last_enabled:GetSaveTable()
  505.  
  506. if not saved["m_bEnterAnimOn"] and not saved["m_bExitAnimOn"] then
  507.  
  508. last_enabled:AddEFlags( EFL_NO_THINK_FUNCTION ) -- disable last_enabled's Think
  509.  
  510. last_enabled = nil
  511.  
  512. end
  513.  
  514. end
  515.  
  516. -- Enable a seat's Think:
  517.  
  518. if IsValid( seat ) then
  519.  
  520. -- seat's Think is enabled, letting the values m_bEnterAnimOn and m_bExitAnimOn being updated.
  521.  
  522. seat:RemoveEFlags( EFL_NO_THINK_FUNCTION )
  523.  
  524. last_enabled = seat
  525.  
  526. end
  527.  
  528. i = i+1
  529.  
  530. end )
  531.  
  532.  
  533.  
  534. local function EnteredOrLeaved( ply, seat )
  535.  
  536. if IsValid( seat ) and seat.seats_network_optimizer then
  537.  
  538. table.insert( seats, i, seat ) -- seat's Think will be enabled on next game's Think
  539.  
  540. end
  541.  
  542. end
  543.  
  544. hook.Add( "PlayerEnteredVehicle", "seats_network_optimizer", EnteredOrLeaved )
  545.  
  546. hook.Add( "PlayerLeaveVehicle", "seats_network_optimizer", EnteredOrLeaved )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement