From 4adec24c3a2a53f518d817ae8a6ddd0c4c47422c Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Wed, 25 May 2016 22:06:26 -0400 Subject: more work on the structure system --- gamemode/structuresystem/common_smelt.lua | 83 ++++++++++++++++++++++ gamemode/structuresystem/loadstructures.lua | 34 +++++++++ .../structures/aaaStructureExample.lua | 22 ++++++ .../structuresystem/structures/stonefurnace.lua | 31 ++++++++ 4 files changed, 170 insertions(+) create mode 100644 gamemode/structuresystem/common_smelt.lua create mode 100644 gamemode/structuresystem/loadstructures.lua create mode 100644 gamemode/structuresystem/structures/aaaStructureExample.lua create mode 100644 gamemode/structuresystem/structures/stonefurnace.lua (limited to 'gamemode/structuresystem') diff --git a/gamemode/structuresystem/common_smelt.lua b/gamemode/structuresystem/common_smelt.lua new file mode 100644 index 0000000..bb34331 --- /dev/null +++ b/gamemode/structuresystem/common_smelt.lua @@ -0,0 +1,83 @@ + +if SERVER then + util.AddNetworkString( "makerecipe" ) +end +function genericMakeFurnace(tbl) + local oldusefunc = tbl.onUse + local overrideuse = function(self, ply) + if SERVER or ply != LocalPlayer() then return end + print("I am the local player!") + local DermaPanel = vgui.Create( "DFrame" ) + DermaPanel:SetPos( 100, 100 ) + DermaPanel:SetSize( ScrW() / 1.3, ScrH() / 1.4 ) + DermaPanel:SetTitle( tbl.Name ) + DermaPanel:SetDraggable( true ) + DermaPanel:Center() + local Grid = vgui.Create("DGrid",DermaPanel) + Grid:SetPos(10,30) + Grid:SetColWide(128) + Grid:SetRowHeight(128) + for k,v in pairs(tbl.Recipes) do + local testbut = vgui.Create("DButton") + testbut:SetText(v.Name) + testbut:SetSize(128,128) + testbut.DoClick = function(button) + net.Start("makerecipe") + net.WriteString(tbl.Name) + if(tbl.uniquedata) then net.WriteUInt(self:EntIndex(), GMS.NETINT_BITCOUNT) end + net.WriteUInt(k, GMS.NETINT_BITCOUNT) + net.SendToServer() + end + Grid:AddItem(testbut) + DermaPanel:MakePopup() + end + oldusefunc(self,ply) + end + tbl.onUse = overrideuse +end +net.Receive( "makerecipe", function(ln,ply) + local tblname = net.ReadString() + local tbl = GMS.Structures[tblname] + print("Table is:") + PrintTable(tbl) + print("Recipes table is:") + PrintTable(tbl.Recipes) + assert(tbl != nil,"Maybe someone's trying to hack lol") + if tbl.uniquedata then + local entnum = net.ReadUInt(GMS.NETINT_BITCOUNT) + tbl = GMS.UniqueStructures[entnum] + end + local recipenum = net.ReadUInt(GMS.NETINT_BITCOUNT) + print("Useing recipes:") + PrintTable(tbl.Recipes) + print("And recipenum:") + PrintTable(tbl.Recipes[recipenum]) + assert(tbl.Recipes != nil and tbl.Recipes[recipenum] != nil, "Invalid recpie!") + print("Attempting to craft recpie:") + PrintTable(tbl.Recipes[recipenum]) + local recipe = tbl.Recipes[recipenum] + local numrequired = 1 + for k, v in pairs( recipe.Req ) do + numrequired = numrequired + v + print("numrequired is now:" .. numrequired) + end + local time = math.pow(numrequired,tbl.timemult) - ((numrequired * tbl.timemult) * math.pow(ply:GetSkill("Smelting"),tbl.skillease)) + time = time * numrequired / 5 + --time = math.max( time - math.floor( ply:GetSkill( "Smelting" ) / 5 ), math.max( timecount * tbl.skillease, 2 ) ) + + print("I think smelting this should take " .. time .. " seconds") + +end) + +function recipieForSmelt(tbl, name, description, required, result, ratio, mults) + for k,v in pairs(mults) do + local thisrecipie = {} + thisrecipie.Name = name .. " x" .. v + thisrecipie.Description = description + thisrecipie.Req = {} + thisrecipie.Req[required] = ratio[1] * v + thisrecipie.Results = {} + thisrecipie.Results[result] = ratio[2] * v + table.insert(tbl,0,thisrecipie) + end +end diff --git a/gamemode/structuresystem/loadstructures.lua b/gamemode/structuresystem/loadstructures.lua new file mode 100644 index 0000000..858d976 --- /dev/null +++ b/gamemode/structuresystem/loadstructures.lua @@ -0,0 +1,34 @@ +print("Loading structures!") +GMS = GMS or {} +GMS.Structures = GMS.Structures or {} +GMS.UniqueStructures = {} + +function registerStructure(tbl) + assert(tbl.Name != nil, "Structure's name is nil!") + GMS.Structures[tbl.Name] = tbl +end + +concommand.Add("gms_spawnstructure",function(ply,cmd,args) + if !ply:IsDeveloper() then return end + assert(args[1] != "","Failed to find structure name") + assert(GMS.Structures[args[1]] != nil, "Structure \"" .. args[1] .. "\" does not exist!") + + local tr = ply:GetEyeTrace() + local e = ents.Create("gms_generic_structure") + + local tbl = GMS.Structures[args[1]] + if tbl.uniquedata then + tbl = table.Copy(tbl) + GMS.UniqueStructures[e:EntIndex()] = tbl + end + + for k,v in pairs(tbl) do + print("Setting " .. k .. " to") + print(v) + e[k] = v + end + e:Spawn() + e:SetPos(tr.HitPos) + SPropProtection.PlayerMakePropOwner( ply, e ) + --e:SetNWString("Owner",ply:Nick()) +end) diff --git a/gamemode/structuresystem/structures/aaaStructureExample.lua b/gamemode/structuresystem/structures/aaaStructureExample.lua new file mode 100644 index 0000000..abe91ba --- /dev/null +++ b/gamemode/structuresystem/structures/aaaStructureExample.lua @@ -0,0 +1,22 @@ +--This file is to help developers add new structures to the game! + +STRUCT = {} + +--A name for this structure, each type of structure must have a different name +STRUCT.Name = "Example Structure" + +--The model for this structure. +STRUCT.Model = "models/props/de_inferno/ClayOven.mdl" + +--The initalize method. Called on both the server and the client +STRUCT.onInitialize = function(self) + print("Initalize called!") +end + +--If this structure is not like every other structures of the same name (for example, if it has an internal inventory) +STRUCT.uniquedata = false + +--Called when a player presses e on this structure, called on both the server and the client. Keep in mind that on the client side, ply may not be the local player! +STRUCT.onUse = function(self,ply) + print("onUse called!") +end diff --git a/gamemode/structuresystem/structures/stonefurnace.lua b/gamemode/structuresystem/structures/stonefurnace.lua new file mode 100644 index 0000000..84c0311 --- /dev/null +++ b/gamemode/structuresystem/structures/stonefurnace.lua @@ -0,0 +1,31 @@ +local STRUCT = {} + +STRUCT.Name = "Stone Furnace" +STRUCT.Model = "models/props/de_inferno/ClayOven.mdl" + +STRUCT.Structure = { + {"models/props/de_inferno/ClayOven.mdl",Vector(0,0,0),Angle(0,0,0)} +} + +STRUCT.onInitialize = function(self) + print("Initalize called!") +end + +STRUCT.uniquedata = false + +STRUCT.onUse = function(self, ply) + print("I am the old use function!") +end + +STRUCT.Recipes = {} + +STRUCT.timemult = 0.5 +STRUCT.skillease = 0.25 + +genericMakeFurnace(STRUCT) +recipieForSmelt(STRUCT.Recipes,"Copper", "Smelt copper ore into copper", "Copper Ore", "Copper", {1,1}, {1,5,10}) + +print("stone furnace's recipies:") +PrintTable(STRUCT.Recipes) + +registerStructure(STRUCT) -- cgit v1.2.3-70-g09d2 From fd04cb695e1c0e6597456f2b1b7b076310fd81bf Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Wed, 25 May 2016 22:39:23 -0400 Subject: Finished the basics of the new structure system --- gamemode/itemsystem/common.lua | 2 +- gamemode/server/player_functions.lua | 4 ---- gamemode/structuresystem/common_smelt.lua | 31 ++++++++++++++++--------------- 3 files changed, 17 insertions(+), 20 deletions(-) (limited to 'gamemode/structuresystem') diff --git a/gamemode/itemsystem/common.lua b/gamemode/itemsystem/common.lua index edaac31..37ae388 100644 --- a/gamemode/itemsystem/common.lua +++ b/gamemode/itemsystem/common.lua @@ -1,7 +1,7 @@ --This file holds a bunch of common functions that happen in items. They're seperated out here so that they're easy to change if there's a bug somewhere. --[[ Provides: - startProcessGeneric(player_player, string_message, number_time, function_ondone) + startProcessGeneric(player_player, string_message, number_time, function_ondone(player_player)) Freezes the player, creates the loading bar, and calls ondone when the timer is up. ]] diff --git a/gamemode/server/player_functions.lua b/gamemode/server/player_functions.lua index 4da63c8..854e649 100644 --- a/gamemode/server/player_functions.lua +++ b/gamemode/server/player_functions.lua @@ -183,10 +183,6 @@ function PlayerMeta:IncResource( resource, int ) if ( !self.Resources[resource] ) then self.Resources[resource] = 0 end local all = self:GetAllResources() local max = self.MaxResources - print("When adding resources, max resource is:") - print(max) - print("All is:") - print(all) if ( all + int > max ) then self.Resources[resource] = self.Resources[resource] + ( max - all ) diff --git a/gamemode/structuresystem/common_smelt.lua b/gamemode/structuresystem/common_smelt.lua index bb34331..43637e1 100644 --- a/gamemode/structuresystem/common_smelt.lua +++ b/gamemode/structuresystem/common_smelt.lua @@ -6,7 +6,6 @@ function genericMakeFurnace(tbl) local oldusefunc = tbl.onUse local overrideuse = function(self, ply) if SERVER or ply != LocalPlayer() then return end - print("I am the local player!") local DermaPanel = vgui.Create( "DFrame" ) DermaPanel:SetPos( 100, 100 ) DermaPanel:SetSize( ScrW() / 1.3, ScrH() / 1.4 ) @@ -24,7 +23,7 @@ function genericMakeFurnace(tbl) testbut.DoClick = function(button) net.Start("makerecipe") net.WriteString(tbl.Name) - if(tbl.uniquedata) then net.WriteUInt(self:EntIndex(), GMS.NETINT_BITCOUNT) end + if tbl.uniquedata then net.WriteUInt(self:EntIndex(), GMS.NETINT_BITCOUNT) end net.WriteUInt(k, GMS.NETINT_BITCOUNT) net.SendToServer() end @@ -38,34 +37,36 @@ end net.Receive( "makerecipe", function(ln,ply) local tblname = net.ReadString() local tbl = GMS.Structures[tblname] - print("Table is:") - PrintTable(tbl) - print("Recipes table is:") - PrintTable(tbl.Recipes) assert(tbl != nil,"Maybe someone's trying to hack lol") if tbl.uniquedata then local entnum = net.ReadUInt(GMS.NETINT_BITCOUNT) tbl = GMS.UniqueStructures[entnum] end local recipenum = net.ReadUInt(GMS.NETINT_BITCOUNT) - print("Useing recipes:") - PrintTable(tbl.Recipes) - print("And recipenum:") - PrintTable(tbl.Recipes[recipenum]) assert(tbl.Recipes != nil and tbl.Recipes[recipenum] != nil, "Invalid recpie!") - print("Attempting to craft recpie:") - PrintTable(tbl.Recipes[recipenum]) local recipe = tbl.Recipes[recipenum] local numrequired = 1 for k, v in pairs( recipe.Req ) do numrequired = numrequired + v - print("numrequired is now:" .. numrequired) end local time = math.pow(numrequired,tbl.timemult) - ((numrequired * tbl.timemult) * math.pow(ply:GetSkill("Smelting"),tbl.skillease)) time = time * numrequired / 5 - --time = math.max( time - math.floor( ply:GetSkill( "Smelting" ) / 5 ), math.max( timecount * tbl.skillease, 2 ) ) - print("I think smelting this should take " .. time .. " seconds") + for k,v in pairs(recipe.Req) do + if ply:GetResource(k) < v then + ply:SendMessage("You don't have enough!", 3, Color(255, 255, 255, 255)) + return + end + end + --We have enough resources! make it! + startProcessGeneric(ply,"Crafting " .. recipe.Name, time, function(player) + for k,v in pairs(recipe.Results) do + player:IncResource(k,v) + end + for k,v in pairs(recipe.Req) do + ply:DecResource(k,v) + end + end) end) -- cgit v1.2.3-70-g09d2