From e1ef031c4d32a52a7ed66e968690b9014fb97099 Mon Sep 17 00:00:00 2001 From: YUCLing Date: Sun, 17 Jul 2022 20:48:13 +0800 Subject: [PATCH] add: first steps of multithreading --- gamemodes/zombiesurvival/gamemode/init.lua | 2 + .../zombiesurvival/gamemode/multithread.lua | 53 +++++++++++++++++++ readme.md | 2 + 3 files changed, 57 insertions(+) create mode 100644 gamemodes/zombiesurvival/gamemode/multithread.lua diff --git a/gamemodes/zombiesurvival/gamemode/init.lua b/gamemodes/zombiesurvival/gamemode/init.lua index 751e434..9037456 100644 --- a/gamemodes/zombiesurvival/gamemode/init.lua +++ b/gamemodes/zombiesurvival/gamemode/init.lua @@ -82,6 +82,8 @@ AddCSLuaFile("vgui/zshealtharea.lua") AddCSLuaFile("vgui/zsstatusarea.lua") AddCSLuaFile("vgui/zsgamestate.lua") +include("multithread.lua") + include("sh_globals.lua") include("obj_entity_extend_sv.lua") diff --git a/gamemodes/zombiesurvival/gamemode/multithread.lua b/gamemodes/zombiesurvival/gamemode/multithread.lua new file mode 100644 index 0000000..a279157 --- /dev/null +++ b/gamemodes/zombiesurvival/gamemode/multithread.lua @@ -0,0 +1,53 @@ +require("effil") + +GM.MT = {} +local m = GM.MT + +local charset = {} + +for i = 48, 57 do table.insert(charset, string.char(i)) end +for i = 65, 90 do table.insert(charset, string.char(i)) end +for i = 97, 122 do table.insert(charset, string.char(i)) end + +function randStr(length) + math.randomseed(os.time()) + + if length > 0 then + return randomStr(length - 1) .. charset[math.random(1, #charset)] + else + return "" + end +end + +function m:WaitForAll(threads) + local results = {} + for i=1, #threads do + results[i] = threads[i]:get() + end + return results +end + +function m:MarkEntity(ent) + if not ent or not ent:IsValid() then return nil end + ent._MT_ID = ent._MT_ID or randStr(5) + return {ent:GetClass(), ent._MT_ID} +end + +function m:FindEnt(id) + if not id then return nil end + for _,v in pairs(ents.FindByClass(id[1])) do + if v._MT_ID == id[2] then return v end + end + return nil +end + +hook.Add("PlayerSpawn", "aa", function(ply) + effil.thread(function()print("we are on the other thread")end)() + local id = GAMEMODE.MT.MarkEntity(ply) + PrintTable(id) + print("main") + --[[effil.thread(function(t) + local p = GAMEMODE.MT.FindEnt(t) + print("t p", p) + end)]] +end) \ No newline at end of file diff --git a/readme.md b/readme.md index 4f0553b..c6762b6 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,7 @@ English | [中文(简体)](readme_zh-CN.md) +**WARNING: This edition of Zombie Survival requires extra library on server.** + # Zombie Survival The definitive zombie experience. A gamemode for Garry's Mod.