summaryrefslogtreecommitdiff
path: root/UsefullLuaSnippets
diff options
context:
space:
mode:
Diffstat (limited to 'UsefullLuaSnippets')
-rw-r--r--UsefullLuaSnippets/alternative_paperdoll.lua24
-rw-r--r--UsefullLuaSnippets/gradient.lua26
-rw-r--r--UsefullLuaSnippets/playerbones.txt21
-rw-r--r--UsefullLuaSnippets/rendergrass.lua47
-rw-r--r--UsefullLuaSnippets/shared_inventory.lua65
-rw-r--r--UsefullLuaSnippets/shared_xp.lua91
6 files changed, 274 insertions, 0 deletions
diff --git a/UsefullLuaSnippets/alternative_paperdoll.lua b/UsefullLuaSnippets/alternative_paperdoll.lua
new file mode 100644
index 0000000..7359416
--- /dev/null
+++ b/UsefullLuaSnippets/alternative_paperdoll.lua
@@ -0,0 +1,24 @@
+if SERVER then return end
+
+_PlayerHat = ClientsideModel("models/props_junk/TrafficCone001a.mdl")
+_PlayerHat:SetNoDraw(true)
+
+
+hook.Add("PostPlayerDraw", "hatsss", function( ply )
+ local Bone = ply:LookupBone("ValveBiped.Bip01_Head1")
+ local BonePos , BoneAng = ply:GetBonePosition( Bone )
+
+ local OffsetPos,OffsetAng = Vector(0,0,10),Angle(0,0,0)
+ OffsetPos:Rotate(OffsetAng)
+
+ local OldAng = BoneAng*1
+ BoneAng:RotateAroundAxis(OldAng:Forward(),OffsetAng.r)
+ BoneAng:RotateAroundAxis(OldAng:Right(),OffsetAng.p)
+ BoneAng:RotateAroundAxis(OldAng:Up(),OffsetAng.y)
+
+ _PlayerHat:SetModelScale(0.6, 0)
+ _PlayerHat:SetRenderOrigin( BonePos + OffsetPos )
+ _PlayerHat:SetRenderAngles( BoneAng )
+ _PlayerHat:SetupBones()
+ _PlayerHat:DrawModel()
+end) \ No newline at end of file
diff --git a/UsefullLuaSnippets/gradient.lua b/UsefullLuaSnippets/gradient.lua
new file mode 100644
index 0000000..8da0742
--- /dev/null
+++ b/UsefullLuaSnippets/gradient.lua
@@ -0,0 +1,26 @@
+
+
+local Text = surface.GetTextureID("gui/gradient")
+
+function DrawBoxGradient(x,y,w,h,extw,color, linecolor)
+ surface.SetDrawColor( color.r, color.g, color.b, color.a )
+ surface.DrawRect( x, y, w, h )
+
+ surface.SetTexture(Text)
+ surface.DrawTexturedRectRotated( x-extw/2, y+h/2, extw, h, 180 )
+
+ surface.SetDrawColor( linecolor.r, linecolor.g, linecolor.b, linecolor.a )
+ surface.DrawLine(x-extw,y-1,x+w,y-1)
+ surface.DrawLine(x-extw,y+h,x+w,y+h)
+end
+
+local Text2 = surface.GetTextureID("gui/gradient_down")
+
+function DrawBoxGradientDown(x,y,w,h,color, gradcolor)
+ surface.SetDrawColor( color.r, color.g, color.b, color.a )
+ surface.DrawRect( x, y, w, h )
+
+ surface.SetDrawColor( gradcolor.r, gradcolor.g, gradcolor.b, gradcolor.a )
+ surface.SetTexture(Text2)
+ surface.DrawTexturedRect( x, y, w, h/2 )
+end \ No newline at end of file
diff --git a/UsefullLuaSnippets/playerbones.txt b/UsefullLuaSnippets/playerbones.txt
new file mode 100644
index 0000000..7a2d772
--- /dev/null
+++ b/UsefullLuaSnippets/playerbones.txt
@@ -0,0 +1,21 @@
+--I am using these too much in my gamemodes. Thought it would be nice to have them here
+
+/*
+ "ValveBiped.Bip01_Head1"
+ "ValveBiped.Bip01_R_Hand"
+ "ValveBiped.Bip01_L_Hand"
+ "ValveBiped.Bip01_Pelvis"
+ "ValveBiped.Bip01_R_Clavicle"
+ "ValveBiped.Bip01_L_Clavicle"
+ "ValveBiped.Bip01_Spine1"
+ "ValveBiped.Bip01_Spine2"
+ "ValveBiped.Bip01_Spine3"
+ "ValveBiped.Bip01_Spine4"
+
+ "eyes"
+ "mouth"
+ "chest"
+ "forward"
+ "anim_attachment_RH"
+ "anim_attachment_LH"
+*/
diff --git a/UsefullLuaSnippets/rendergrass.lua b/UsefullLuaSnippets/rendergrass.lua
new file mode 100644
index 0000000..38ed10b
--- /dev/null
+++ b/UsefullLuaSnippets/rendergrass.lua
@@ -0,0 +1,47 @@
+--Snippit for moving sprite textures.. Warning: GLua is slow so using this instead of source engines detail is a stupid idea! Only for small exact things!
+
+
+local SpritePlants = {}
+local Zero = Vector(0,0,0)
+local Dir = Vector(16,0,0)
+local DirH = Vector(0,0,16)
+local TMat = {}
+
+function PlantSpriteAtVector(Origin,BoxSize,SpriteTable,MaxSprites)
+ Zero.z = BoxSize.z
+ SpritePlants = {}
+
+ for k,v in pairs(SpriteTable) do if (!TMat[v]) then TMat[v],Unused = Material(v,"nocull") end end
+
+ for i = 1,MaxSprites do
+
+ local Random = Vector(math.Rand(-1,1),math.Rand(-1,1),0) * BoxSize
+ local Tr = util.TraceLine({
+ start = Origin+Zero+Random,
+ endpos = Origin-Zero+Random,
+ mask = MASK_SOLID_BRUSHONLY,
+ })
+
+ if (Tr.Hit and Tr.HitWorld) then
+ Dir:Rotate(Angle(0,math.random(0,360),0))
+
+ table.insert(SpritePlants,{
+ V1 = Tr.HitPos+Dir,
+ V2 = Tr.HitPos-Dir,
+ Matsy = TMat[SpriteTable[math.random(1,#SpriteTable)]],
+ })
+ end
+ end
+end
+
+hook.Add("PostDrawTranslucentRenderables","RenderPlants",function()
+ for k,v in pairs(SpritePlants) do
+ local MOVE = (v.V1-v.V2):GetNormal()*math.cos(CurTime()*2+k*15)*5
+ render.SetMaterial(v.Matsy)
+ render.DrawQuad(v.V2+DirH+MOVE,v.V1+DirH+MOVE,v.V1,v.V2)
+ end
+end)
+
+/*
+ lua_run_cl PlantSpriteAtVector(player.GetByID(1):GetPos(),Vector(100,100,100),{"gearfox/grass.png",},300)
+*/ \ No newline at end of file
diff --git a/UsefullLuaSnippets/shared_inventory.lua b/UsefullLuaSnippets/shared_inventory.lua
new file mode 100644
index 0000000..124c585
--- /dev/null
+++ b/UsefullLuaSnippets/shared_inventory.lua
@@ -0,0 +1,65 @@
+
+--> Inherited from Old Age 2 scripts made by The Maw. <--
+
+local meta = FindMetaTable("Player")
+local Slots = 16
+
+if (SERVER) then
+ util.AddNetworkString("SetSlotItem")
+ util.AddNetworkString("SetupInventory")
+
+ function meta:AddItem(item)
+ local ID = nil
+ if (!self.Inventory) then self.Inventory = {} ID = 1
+ else for i = 1,Slots do if (!self.Inventory[i]) then ID = i break end end end
+
+ if (ID) then
+ self.Inventory[ID] = item
+
+ net.Start("SetSlotItem")
+ net.WriteEntity(self)
+ net.WriteByte(ID)
+ net.WriteString(item)
+ net.Send(self)
+ else return false end --We are returning false if there isn't any available slots left.
+ end
+
+ function meta:SetupInventory(dat) --We are using a table of items, using the indexes as the slotID
+ self.Inventory = dat
+
+ net.Start("SetupInventory")
+ net.WriteEntity(self)
+ net.WriteTable(dat)
+ net.Send(self)
+ end
+else
+ net.Receive("SetSlotItem",function(size)
+ local Ply = net.ReadEntity()
+
+ if (!Ply.Inventory) then Ply.Inventory = {} end
+ Ply.Inventory[net.ReadByte()] = net.ReadString()
+ end)
+
+ net.Receive("SetupInventory",function(size) net.ReadEntity().Inventory = net.ReadTable() end)
+end
+
+function meta:GetSlot(id)
+ if (!self.Inventory) then return nil end
+ return self.Inventory[id] or nil
+end
+
+function meta:GetInventory(id)
+ return self.Inventory or {}
+end
+
+function meta:HasRoom()
+ if (!self.Inventory) then return true end
+ for i = 1,Slots do if (!self.Inventory[i]) then return true end end
+ return false
+end
+
+
+
+
+
+ \ No newline at end of file
diff --git a/UsefullLuaSnippets/shared_xp.lua b/UsefullLuaSnippets/shared_xp.lua
new file mode 100644
index 0000000..c7e09ea
--- /dev/null
+++ b/UsefullLuaSnippets/shared_xp.lua
@@ -0,0 +1,91 @@
+
+--> Inherited from Old Age 2 scripts made by The Maw. <--
+
+local meta = FindMetaTable("Player")
+
+if (SERVER) then
+ util.AddNetworkString("SetXP")
+ util.AddNetworkString("SetupLeveling")
+ util.AddNetworkString("SetLevel")
+
+ function meta:AddXP(xp)
+ if (!self.xp) then self.xp = 0 end
+ if (!self.Level) then self.Level = 1 end
+
+ self.xp = self.xp + xp
+
+ self:RecalcLevel()
+
+ net.Start("SetXP")
+ net.WriteEntity(self)
+ net.WriteLong(self.xp)
+ net.Broadcast() --Think ill be sending this to everyone :)
+ end
+
+ function meta:SetupPlayer(xp,level)
+ net.Start("SetupLeveling")
+ net.WriteEntity(self)
+ net.WriteLong(xp)
+ net.WriteByte(level)
+ net.Broadcast()
+
+ self.xp = xp
+ self.Level = level
+ end
+
+
+
+ function meta:RecalcLevel()
+ if (!self.xp) then return 1 end
+ if (!self.Level) then self.Level = 1 end
+
+ local Level = self.Level
+
+ repeat
+ local XP = 178 + Level^2 * (22*Level)
+
+ if (self.xp >= XP) then Level = Level+1 self.xp = self.xp-XP end
+ until (self.xp < XP or Level > 90)
+
+ if (Level != self.Level) then
+ self:AddNote("Level up! :D")
+ self.Level = Level
+
+ net.Start("SetLevel")
+ net.WriteEntity(self)
+ net.WriteByte(self.Level)
+ net.Broadcast()
+
+ --LEVELUP!
+ end
+ end
+else
+ net.Receive("SetXP",function(size) net.ReadEntity().xp = net.ReadLong() end)
+ net.Receive("SetLevel",function(size) net.ReadEntity().Level = net.ReadByte() end)
+
+ net.Receive("SetupLeveling",function(size)
+ local Ply = net.ReadEntity()
+
+ Ply.xp = net.ReadLong()
+ Ply.Level = net.ReadByte()
+ end)
+end
+
+function meta:GetXP()
+ return self.xp or 0
+end
+
+function meta:GetLevel()
+ return self.Level or 1
+end
+
+function meta:GetRequiredXP()
+ local Level = self:GetLevel()
+ return (178 + Level^2 * (22*Level))
+end
+
+
+
+
+
+ \ No newline at end of file