· 6 years ago · Mar 09, 2019, 10:20 PM
1--------------------------
2----------Mining----------
3--------------------------
4
5local PROCESS = {}
6
7function PROCESS:OnStart()
8
9 --Set up progress bar.
10 self.Owner:MakeProcessBar( "Mining from rock...", self.Time, self.Cancel )
11 self.StartTime = CurTime()
12
13 --do function PROCESS:PlaySound()
14 self:PlaySound()
15
16 --If no data for a rocks uses left exists, create it.
17 if ( !self.Data.Entity.Uses ) then
18 self.Data.Entity.Uses = 250
19 end
20
21end
22
23--Process for sound and animation and stuff.
24function PROCESS:PlaySound()
25
26 --If there's no time left don't bother doing function.
27 if ( CurTime() - self.StartTime > self.Time ) then return end
28
29 --Make sure the player is OK to continue with the process.
30 if ( !self.Owner.InProcess || self.StartTime != self.Owner.ProcessTable.StartTime ) then return end
31 if ( self.Owner:Alive() ) then
32 --Do swing animation and make hit sound.
33 self.Owner:GetActiveWeapon():DoEffects( self.Owner:GetEyeTrace() )
34 self.Owner:EmitSound( Sound( "physics/glass/glass_bottle_impact_hard" .. tostring( math.random(3) ) .. ".wav" ) )
35
36 --Repeat if necessary.
37 timer.Simple( 1.5, function()
38 self:PlaySound()
39 end )
40
41 end
42
43end
44
45function PROCESS:OnStop()
46
47 --Pick a random number to decide if the player suceeds.
48 local chance = math.random(100)
49
50 --This is for checking if the player is mining a meteor.
51 local trace = self.Owner:TraceFromEyes( 75000 )
52 local ent = trace.Entity
53 --Meteors should be harder to successfuly mine.
54 if ( ent:GetClass() == "gms_meteorite" ) then
55 chance = chance + 50
56 end
57
58 --The initial amount of resources to give before further math is done.
59 local initamnt = math.random(self.Data.MinAmount+math.Round(self.Owner:GetSkill("Mining")/100),self.Data.MaxAmount+math.Round(self.Owner:GetSkill("Mining")/10))
60
61 --Fail
62 if ( self.Data.Chance + ( self.Owner:GetSkill( "Mining" )/2 ) < chance ) then
63 self.Owner:SendMessage("You accidentally crumble the ore.",3,Color(200,0,0,255))
64 --Succeed
65 else
66
67 --Resources and their rarity.
68 local restbl = {
69 { "Stone", initamnt },
70 { "Copper_Ore", initamnt },
71 { "Iron_Ore", initamnt },
72 --Higher tier ores have diminishing yields.
73 { "Gold_Ore", initamnt/2 },
74 { "Titanium_Ore", initamnt/4 },
75 { "Adamantite_Ore", initamnt/5 },
76 --This set is for players with max smelting.
77 { "Stone", initamnt },
78 { "Copper", initamnt },
79 { "Iron", initamnt },
80 { "Gold", initamnt/4 }, --gold smelts at a 2:1 ratio
81 { "Titanium", initamnt/8 }, --same as gold.
82 { "Adamantite", initamnt/25 }, --addy smelts at a 5:1 ratio
83 --This is just for when mining meteors.
84 { "Meteorite", initamnt/6 } --players shouldn't get much meteor.
85 }
86
87 --Tool info including tiers.
88 local tool = self.Owner:GetActiveWeapon():GetClass()
89 local tooltbl = {}
90 tooltbl[ "gms_fists" ] = 1
91 --Tier 1
92 tooltbl[ "gms_woodenpickaxe" ] = 2
93 tooltbl[ "gms_stonepickaxe" ] = 2
94 --Tier 2
95 tooltbl[ "gms_copperpickaxe" ] = 3
96 tooltbl[ "gms_ironpickaxe" ] = 3
97 tooltbl[ "tp_donatorpickaxe" ] = 3
98 tooltbl[ "tp_donatorpickaxe2" ] = 3
99 tooltbl[ "tp_donatorpickaxe3" ] = 3
100 --Tier 3
101 tooltbl[ "gms_rironpickaxe" ] = 4
102 tooltbl[ "gms_goldpickaxe" ] = 4
103 tooltbl[ "tp_donatorpickaxe4" ] = 4
104 --Tier 4
105 tooltbl[ "gms_steelpickaxe" ] = 5
106 tooltbl[ "gms_tithatchaxe" ] = 5
107 tooltbl[ "tp_donatorpickaxe5" ] = 5
108 --Tier 5
109 tooltbl[ "gms_meteoritepick" ] = 6
110 tooltbl[ "gms_adamantiumpickaxe" ] = 6
111 tooltbl[ "gms_unobtaniumpickaxe" ] = 6
112 tooltbl[ "gms_masterpick"] = 6
113
114 --New res and amount variables. These have to be empty before they're used.
115 local newvar = nil --we need the table as 1 variable so that the servers not applying math.random multiple times for amount and resource when they need to be the same number.
116 local resource = nil
117 local amount = nil
118
119 --Smelting Perk as well as meteor.
120 if ( self.Owner:GetSkill("Smelting") >199 ) then
121 newvar = restbl[math.random(7,tooltbl[tool]+6)]
122 resource = newvar[1]
123 amount = math.Clamp(math.Round(newvar[2]),1,9999)
124 elseif ( ent:GetClass() == "gms_meteorite" ) then
125 newvar = restbl[13]
126 resource = newvar[1]
127 amount = math.Clamp(math.Round(newvar[2]),1,9999)
128 else
129 newvar = restbl[math.random(tooltbl[tool])]
130 resource = newvar[1]
131 amount = math.Clamp(math.Round(newvar[2]),1,9999)
132 end
133
134 --Give Resource.
135 self.Owner:IncResource( resource, amount )
136 local message = string.Replace(resource .. " " .. amount, "_", " " )
137 self.Owner:SendMessage( message , 5, Color( 10, 200, 10, 255 ) )
138
139 --This is commented out because this has to do with rock fade, which shouldn't be enabled.
140 --It is also outdated with old variables, it won't work.
141 /*if ( self.Data.Entity and self.Data.Entity.Uses ) then
142 self.Data.Entity.Uses = self.Data.Entity.Uses - amount
143 end*/
144
145
146 --Pickup Sound
147 self.Owner:EmitSound( Sound( "items/ammo_pickup.wav" ) )
148
149 --XP
150 if ( self.Owner:GetSkill("Mining") < 199 ) then
151 self.Owner:IncXP( "Mining", math.Clamp(math.Round(50/self.Owner:GetSkill("Mining")),1,100))
152 end
153
154 --Loot info
155 local lootchance = math.random(500)
156 local prize = nil
157
158 --Loot Chances
159 if ( lootchance == 500 ) then
160 prize = ents.Create( "gms_exp" )
161 elseif ( lootchance == 1 ) then
162 prize = ents.Create( "gms_ironlockbox" )
163 end
164
165 --Loot Spawn
166 if ( IsValid( prize ) ) then
167 prize:SetPos( self.Owner:GetPose() + Vector( 0, 0, 20 ) )
168 prize:Spawn()
169 --Despawn Uncollected Loot.
170 timer.Simple( 30, function()
171 if ( IsValid( prize ) ) then
172 prize:Fadeout( 2 )
173 end
174 end )
175 end
176
177 end
178
179 --This is commented out because this has to do with rock fade, which shouldn't be enabled.
180 /*if ( GetConVarNumber( "gms_FadeRocks" ) == 1 and self.Data.Entity != NULL ) then
181 if ( self.Data.Entity.Uses <= 0 ) then
182 self.Data.Entity:Fadeout()
183 end
184 end*/
185
186end
187
188GMS.RegisterProcess( "Mining", PROCESS )