diff --git a/entities/entities/q_crafting_station/shared.lua b/entities/entities/q_crafting_station/shared.lua index 0ed8dda..ae1d1b6 100644 --- a/entities/entities/q_crafting_station/shared.lua +++ b/entities/entities/q_crafting_station/shared.lua @@ -8,7 +8,7 @@ ENT.Type = "anim" ENT.Base = "base_entity" -ENT.PrintName = "Quantum Item" +ENT.PrintName = "Quantum Station" ENT.Author = "AlmTech" ENT.Contact = "elias@almtech.se" ENT.Spawnable = false diff --git a/entities/entities/q_node/cl_init.lua b/entities/entities/q_node/cl_init.lua new file mode 100644 index 0000000..d615b1b --- /dev/null +++ b/entities/entities/q_node/cl_init.lua @@ -0,0 +1,12 @@ +-- __ _ _______ _ __ +-- / / /\ | | |__ __| | | \ \ +-- / / / \ | |_ __ ___ | | ___ ___| |__ \ \ +-- < < / /\ \ | | '_ ` _ \| |/ _ \/ __| '_ \ > > +-- \ \ / ____ \| | | | | | | | __/ (__| | | | / / +-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ + +include( "shared.lua" ) + +function ENT:Draw() + self:DrawModel() +end \ No newline at end of file diff --git a/entities/entities/q_node/init.lua b/entities/entities/q_node/init.lua new file mode 100644 index 0000000..bfe91e3 --- /dev/null +++ b/entities/entities/q_node/init.lua @@ -0,0 +1,44 @@ +-- __ _ _______ _ __ +-- / / /\ | | |__ __| | | \ \ +-- / / / \ | |_ __ ___ | | ___ ___| |__ \ \ +-- < < / /\ \ | | '_ ` _ \| |/ _ \/ __| '_ \ > > +-- \ \ / ____ \| | | | | | | | __/ (__| | | | / / +-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ + +AddCSLuaFile( "cl_init.lua" ) +AddCSLuaFile( "shared.lua" ) + +include( "shared.lua" ) + +function ENT:Initialize() + + self:PhysicsInit( SOLID_BSP ) + self:SetMoveType( MOVETYPE_VPHYSICS ) + self:SetSolid( SOLID_VPHYSICS ) + self:SetCollisionGroup( COLLISION_GROUP_NONE ) + + local physObj = self:GetPhysicsObject() + if( IsValid( physObj ) ) then + physObj:EnableMotion( false ) -- dont want it to move + end + +end + +function ENT:InitializeNode( nodeid, pos, ang ) + if( pos == nil || ang == nil ) then return end + + local nodeTbl = Quantum.node.Get( nodeid ) + + if( nodeTbl != nil ) then + self:SetModel( nodeTbl.model ) + self.nodeid = nodeid + + self:SetNWString( "q_node_id", nodeid ) + + self:SetPos( pos ) + self:SetAngles( ang ) + else + Quantum.Error( "Node Table could not be found '" .. nodeid .. "'!" ) + self:Remove() + end +end \ No newline at end of file diff --git a/entities/entities/q_node/shared.lua b/entities/entities/q_node/shared.lua new file mode 100644 index 0000000..6be8e3b --- /dev/null +++ b/entities/entities/q_node/shared.lua @@ -0,0 +1,15 @@ +-- __ _ _______ _ __ +-- / / /\ | | |__ __| | | \ \ +-- / / / \ | |_ __ ___ | | ___ ___| |__ \ \ +-- < < / /\ \ | | '_ ` _ \| |/ _ \/ __| '_ \ > > +-- \ \ / ____ \| | | | | | | | __/ (__| | | | / / +-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ + +ENT.Type = "anim" +ENT.Base = "base_entity" + +ENT.PrintName = "Quantum Node" +ENT.Author = "AlmTech" +ENT.Contact = "elias@almtech.se" +ENT.Spawnable = false +ENT.AdminSpawnable = false \ No newline at end of file diff --git a/gamemode/cl_init.lua b/gamemode/cl_init.lua index af68375..8c7fcf4 100644 --- a/gamemode/cl_init.lua +++ b/gamemode/cl_init.lua @@ -51,6 +51,7 @@ if CLIENT then include( "settings/sh_effects.lua" ) include( "settings/sh_crafting_stations.lua" ) include( "settings/sh_recipes.lua" ) + include( "settings/sh_nodes.lua" ) end local function loadPlugins() diff --git a/gamemode/engine/lib/server/sv_notify.lua b/gamemode/engine/lib/server/sv_notify.lua index 3da208e..cc17e25 100644 --- a/gamemode/engine/lib/server/sv_notify.lua +++ b/gamemode/engine/lib/server/sv_notify.lua @@ -39,6 +39,19 @@ function Quantum.Notify.ItemCrafted( pl, item, amount ) pl:SendLua( luaFunc ) end +function Quantum.Notify.ItemGathered( pl, item, amount ) + local amtStr = "" + if( amount > 1 ) then amtStr = tostring(amount) .. "x " end + + local itemColor = item.rarity.color || baseClr + local itemName = item.name || "[ERROR name=nil]" + + local luaArgs = makeColorAString(baseClr) .. ",'You gathered '," .. makeColorAString(baseClr) .. ",'" .. tostring(amtStr) .. "'," .. makeColorAString(itemColor) .. "," .. "'" .. tostring(itemName) .. "'" + local luaFunc = "chat.AddText(" .. luaArgs .. ")" + + pl:SendLua( luaFunc ) +end + function Quantum.Notify.Deny( pl, text ) local luaArgs = makeColorAString( Color( 245, 20, 20 ) ) .. ",'" .. tostring( text ) .. "'" diff --git a/gamemode/engine/lib/sh_node.lua b/gamemode/engine/lib/sh_node.lua new file mode 100644 index 0000000..5b1ec69 --- /dev/null +++ b/gamemode/engine/lib/sh_node.lua @@ -0,0 +1,133 @@ +-- __ _ _______ _ __ +-- / / /\ | | |__ __| | | \ \ +-- / / / \ | |_ __ ___ | | ___ ___| |__ \ \ +-- < < / /\ \ | | '_ ` _ \| |/ _ \/ __| '_ \ > > +-- \ \ / ____ \| | | | | | | | __/ (__| | | | / / +-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ + +Quantum.Node = {} -- lib +Quantum.Nodes = {} -- container for vars + +function Quantum.Node.Create( nodeid, tbl ) + local node = { + name = tbl.name || "Unknown Node", + model = tbl.model, + toolids = tbl.toolids || { "q_hands" }, + give = tbl.give || {}, + giveprobability = tbl.giveprobability || 1 + } + + node.id = nodeid + Quantum.Nodes[nodeid] = node + return node +end + +function Quantum.Node.Get( nodeid ) + return Quantum.Nodes[nodeid] +end + +if SERVER then + + function Quantum.Node.Spawn( nodeid, vec, ang, respawndelay, probability ) + local node = Quantum.Node.Get( nodeid ) + + local ent = ents.Create( "q_node" ) + ent.node = node + ent.respawndelay = respawndelay || 30 + ent.probability = probability || 1 + ent:SetModel( node.model ) + ent:SetPos( vec ) + ent:SetAngles( ang ) + + ent:Spawn() + end + + function Quantum.Node.Remove( ent ) + local nodeTbl = ent.node + + if( ent.node != nil ) then + local nodeid = nodeTbl.id + + timer.Simple( ent.respawndelay, function() + Quantum.Node.Spawn( nodeid, ent:GetPos(), ent:GetAngles(), ent.respawndelay, ent.probability ) -- respawn it after x seconds + end) + + ent:Remove() -- remove the node + else + Quantum.Error( "Node table is nil! Aborting..." ) + return + end + end + + function Quantum.Node.RemoveAll() + for k, node in pairs( ents.FindByClass("q_node") ) do + Quantum.Node.Remove( node ) + end + end + + function Quantum.Node.Register( nodeid, vec, ang1 ) + Quantum.Nodes[ #Quantum.Nodes + 1 ] = { id = nodeid, pos = vec, ang = ang1 } + end + + function Quantum.Node.SpawnAllRegistered() + local nodeTbl + for k, v in pairs( Quantum.Nodes ) do + nodeTbl = Quantum.Node.Get( v.id ) + if( nodeTbl != nil ) then + Quantum.Node.Spawn( v.id, v.pos, v.ang, nodeTbl.respawndelay, nodeTbl.probability ) + else + Quantum.Error( "Tried to spawn an invalid node ('" .. v.id .. "')!" ) + end + end + end + + hook.Add( "PlayerInitialSpawn", "Quantum_Init_Nodes_Load", function() + Quantum.Debug( "Spawning registered nodes..." ) + + if( #player.GetAll() == 1 ) then -- spawn the stations when the first player joins + Quantum.Node.SpawnAllRegistered() + end + end) + + local function randomizeLootTable( tbl, prob ) + local n = math.Rand( 0.00000000001, 1 ) + Quantum.Debug( "Probability: " .. n .. " <= " .. prob ) + if( n <= prob ) then + local index = table.random( 1, #tbl ) + return tbl[index].item, tbl[index].amount || 1 + end + end + + function Quantum.Node.Gather( pl, tool, ent ) + local nodeTbl = ent.node + + if( ent.node != nil ) then + local nodeid = nodeTbl.id + local toolids = nodeTbl.toolids + + if( #toolids > 1 ) then + local canGather = false + + for i, t in pairs( toolids ) do + if( tool == t ) then + canGather = true + break + end + end + + if( canGather ) then + local loot, amount = randomizeLootTable( nodeTbl.give, nodeTbl.giveprobability ) + + if( loot != nil ) then + Quantum.Server.Inventory.GiveItem( pl, loot, amount ) + local itemTbl = Quantum.Item.Get( loot ) + Quantum.Notify.ItemGathered( pl, itemTbl.name, amount ) + else + return + end + end + + end + end + end +end \ No newline at end of file diff --git a/gamemode/init.lua b/gamemode/init.lua index f8859f8..80d6592 100644 --- a/gamemode/init.lua +++ b/gamemode/init.lua @@ -16,6 +16,7 @@ if SERVER then AddCSLuaFile( "settings/sh_effects.lua" ) AddCSLuaFile( "settings/sh_recipes.lua" ) AddCSLuaFile( "settings/sh_crafting_stations.lua" ) + AddCSLuaFile( "settings/sh_nodes.lua" ) AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) @@ -120,6 +121,7 @@ if SERVER then include( "settings/sh_crafting_stations.lua" ) include( "settings/sh_recipes.lua" ) include( "settings/sv_crafting_stations_locations.lua" ) + include( "settings/sh_nodes.lua" ) Quantum.Server.Station.UpdateAll() end diff --git a/gamemode/settings/sh_nodes.lua b/gamemode/settings/sh_nodes.lua new file mode 100644 index 0000000..7657a07 --- /dev/null +++ b/gamemode/settings/sh_nodes.lua @@ -0,0 +1,16 @@ +-- __ _ _______ _ __ +-- / / /\ | | |__ __| | | \ \ +-- / / / \ | |_ __ ___ | | ___ ___| |__ \ \ +-- < < / /\ \ | | '_ ` _ \| |/ _ \/ __| '_ \ > > +-- \ \ / ____ \| | | | | | | | __/ (__| | | | / / +-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ + +Quantum.Node.Create( "stone", { + name = "Stone", + model = "", + toolids = {"q_hands"}, + give = { + { item = "test2", amount = 1 } + }, + giveprobability = 1/2 +} ) \ No newline at end of file