· 8 years ago · Jan 12, 2018, 02:20 PM
1--Set this to false and player scales will be stored to the database but not loaded anymore (defaults to 1.0)
2DisableScaleLoading = false;
3
4
5--Script begin
6
7if SERVER then
8 AddCSLuaFile( "resizer_main.lua" )
9end
10
11--Default data for players
12--accessible server and client side
13Sizing = { }
14Sizing.Default = {
15 StandingHull = {
16 Minimum = Vector( -16, -16, 00 ),
17 Maximum = Vector( 16, 16, 72 ),
18 },
19 DuckingHull = {
20 Minimum = Vector( -16, -16, 00 ),
21 Maximum = Vector( 16, 16, 36 ),
22 },
23 JumpPower = 160,
24 StepSize = 18,
25
26 --MaxRunSpeed = 1500,
27 --MaxWalkSpeed= 1500,
28 MaxStepSize = 500,
29 MaxHullScale = 3,
30
31 Scale = Vector( ),
32 ViewOffset = Vector( 0, 0, 64 ),
33 ViewOffsetDuck = Vector( 0, 0, 28 ),
34 --AdjustToHeight = true,
35 }
36
37
38--server side functions
39if SERVER then
40
41 --Main function - adjusts the player height of player v
42 local function AdjustSc(v)
43 local scale, adjust
44 local md = v:GetModel()
45
46 --detect the model eye height
47 psc = 1.0 --psc = player scale
48 if (EyeAdjustments[md]) then
49 psc=EyeAdjustments[md]
50 end
51
52 --each client has the ability to also set his personal eye scale - he can lower or raise his eye height
53 local eyeheight = v:GetInfo("resize_eyeoffset");
54
55 if eyeheight and tonumber(eyeheight) then
56 local eyescale = tonumber(eyeheight)
57 if(eyescale < 0) then
58 eyescale = 0
59 end;
60 psc = psc * eyescale;
61 end
62
63 --[[this variable decides whether to send adjustment commands for that player
64 (several adjustment commands slow down the server thus they are only called
65 when needed)]]--
66 adjust = false
67
68 --[[v.Progress is a variable for each player describing if a player is currently
69 growing or shrinking - if it is -1 then no process is running]]--
70 if v.Progress!=-1 then
71 adjust = true --in case a progress running adjust is always enabled
72 local add = FrameTime()*100;
73 v.Progress = v.Progress + add; --progress is going on
74 if (v.Progress >= v.EndProgress) then --progress completed
75 v.Scale = v.TargetScale --set player scale to target scale
76 v.Progress = -1; --process completed
77 else
78 if v.ExponentialScaling then
79 --depending on mathematical scaling way do the correct scale calculation
80 v.Scale = v.SourceScale * (1/2)^(v.Progress*v.ScaleSpeed)
81 else
82 v.Scale = v.SourceScale + v.Progress * v.ScaleSpeed
83 end
84 end
85 --store the player scale to a network float so that clients can read it
86 v:SetNetworkedFloat("PlScale",v.Scale);
87 end
88
89 --incase the psc value changed set adjust to true
90 if not v.OldPsc then
91 adjust=true
92 elseif psc!=v.OldPsc then
93 adjust=true
94 end
95
96 --store psc value
97 v.OldPsc=psc
98
99 --set local variable
100 scale = v.Scale
101
102 if adjust then
103 local vsc=psc*scale
104
105 --adjust jump power
106 jumpscale = scale/math.sqrt(psc)
107 if(jumpscale<1) then --jump power shouldn't drop below normal, major issues
108 jumpscale=1
109 end
110 v:SetJumpPower( math.sqrt(jumpscale)*Sizing.Default.JumpPower )
111
112 --step size
113 local ss=scale*Sizing.Default.StepSize
114 if ss>Sizing.Default.MaxStepSize then --too high step size causes server issues!
115 ss=Sizing.Default.MaxStepSize
116 end
117 v:SetStepSize( ss )
118
119 --adjust view offset so that players have the felling of being small/tall
120 v:SetViewOffset( vsc*Sizing.Default.ViewOffset )
121 v:SetViewOffsetDucked( vsc*Sizing.Default.ViewOffsetDuck )
122 v:SetNetworkedFloat("PlViewOffset",vsc) --set a network float for that, the client needs it
123
124 --adjust hull scale
125 local hsc=vsc
126 if(hsc>Sizing.Default.MaxHullScale) then --do not go too high!
127 hsc=Sizing.Default.MaxHullScale
128 end
129 v:SetNetworkedFloat("PlHullScale",hsc) --client needs it!
130 v:SetHull( hsc*Sizing.Default.StandingHull.Minimum , hsc*Sizing.Default.StandingHull.Maximum )
131 v:SetHullDuck( hsc*Sizing.Default.DuckingHull.Minimum , hsc*Sizing.Default.DuckingHull.Maximum )
132 end
133
134
135 end
136
137 --gets executed every tick and runs the
138 --adjust function for all living players
139 local function Tick( )
140 local k, v
141
142 for k, v in pairs( player.GetAll( ) ) do
143 if v:Alive() then --only modify living players
144 AdjustSc(v)
145 end
146 end
147
148 end
149
150 hook.Add( "Tick", "Resizer.Tick", Tick )
151
152
153 --store the target player size into the database
154 function StoreSize(pl, scale)
155 local name,lscale,allow,aallow;
156
157 lscale = pl.TargetScale
158 name = pl:UniqueID()
159 allow = GetAllow(pl);
160
161 if lscale>100 then
162 lscale=100
163 end
164
165 if allow then
166 aallow = 1
167 else
168 aallow = 0
169 end;
170
171 local entry_exists = sql.Query( "SELECT * FROM player_scales WHERE uniqueid = "..pl:UniqueID()..";" )
172
173 if ( !entry_exists ) then
174 sql.Query( "INSERT INTO player_scales VALUES ("..pl:UniqueID()..","..lscale..","..aallow..");");
175 else
176 sql.Query( "UPDATE player_scales SET scale="..lscale..",allowothers="..aallow.." WHERE uniqueid = "..pl:UniqueID()..";");
177 end
178
179 end
180
181 --read the player scale from the database if existing
182 --otherwise set default scales and default vars
183 function SetOrgScale( pl )
184 local name,scale,sztab,allow,result
185 name=pl:UniqueID()
186
187 --CreateTable();
188
189 if DisableScaleLoading then
190 result = false
191 else
192 result = sql.Query("SELECT * FROM player_scales WHERE uniqueid="..name..";");
193 end
194
195 if not result then
196 scale = 1
197 allow = 1
198 else
199 for k,row in pairs(result) do
200 scale = tonumber(row['scale']);
201 if row['allowothers']=="1" then
202 allow = 1
203 else
204 allow = 0
205 end
206 end
207 end
208
209 --set variables
210 pl:SetNetworkedFloat("PlScale",scale);
211 pl:SetNetworkedFloat("PlViewOffset",1);
212 pl:SetNetworkedFloat("PlHullScale",1);
213 pl:ConCommand("resize_allow "..allow);
214 pl.Scale = scale
215 pl.TargetScale = scale
216 pl.ScaleSpeed = 0
217 pl.EndProgress = 0;
218 pl.Progress = 0
219 pl.ExponentialScaling = true
220 AdjustSc(pl)
221 end
222 hook.Add( "PlayerInitialSpawn", "Resizer.InitScale", SetOrgScale );
223
224
225
226 local divider = math.log(1.0/2.0); --for exponential scaling
227
228 --function to call to set a player to wished scale
229 function ScalePlayer(pl, scale, endprogress, exponential, sendingplayer)
230
231
232 --make sure we do not scale anyone to 0
233 if scale <= 0 then
234 scale = 0.00001
235 end
236 --set the variables
237 if not pl.Scale then pl.Scale = scale end
238 pl.SourceScale = pl.Scale
239 pl.TargetScale = scale
240 pl.Progress = 0
241 pl.EndProgress = endprogress
242 pl.ExponentialScaling = exponential
243 if exponential then
244 pl.ScaleSpeed = math.log(pl.TargetScale / pl.Scale) / endprogress / divider;
245 else
246 pl.ScaleSpeed = (pl.TargetScale - pl.Scale) / endprogress;
247 end
248 --store network variables
249 pl:SetNetworkedFloat("PlScale",pl.SourceScale)
250 --store size to database
251 StoreSize(pl,pl.TargetScale)
252 end
253
254 --create database table if required
255 sql.Query("CREATE TABLE IF NOT EXISTS player_scales ( 'uniqueid' INT NOT NULL PRIMARY KEY, 'scale' DOUBLE NOT NULL, 'allowothers' BOOL NOT NULL);");
256end
257
258
259--client specific stuff
260if CLIENT then
261 --size update on client side
262 local function Tick( )
263 local k, v
264
265 for k, v in pairs( player.GetAll( ) ) do
266 --get scale and set model size and render bounds with it
267 local pscale = v:GetNetworkedFloat("PlScale")
268 if not pscale then
269 pscale = 1
270 end;
271 local ScaleVector = pscale * Vector(1,1,1)
272 v:SetModelScale( ScaleVector )
273 v:SetRenderBounds( pscale * Sizing.Default.StandingHull.Minimum, pscale * Sizing.Default.StandingHull.Maximum )
274
275 --get the view offset (shared function but executing it on client side fixes some screen shaking issues)
276 local vsc = v:GetNetworkedFloat("PlViewOffset")
277 if not vsc then
278 vsc = 1
279 end;
280 v:SetViewOffset( vsc*Sizing.Default.ViewOffset )
281 v:SetViewOffsetDucked( vsc*Sizing.Default.ViewOffsetDuck )
282
283 --get the player hull (again shared function but executing it on client side fixes some screen shaking issues)
284 local hsc = v:GetNetworkedFloat("PlHullScale")
285 if not hsc then
286 hsc = 1
287 end;
288 v:SetHull( hsc*Sizing.Default.StandingHull.Minimum , hsc*Sizing.Default.StandingHull.Maximum )
289 v:SetHullDuck( hsc*Sizing.Default.DuckingHull.Minimum , hsc*Sizing.Default.DuckingHull.Maximum )
290
291 end
292 end
293
294 hook.Add( "Tick", "Resizer.ClientTick", Tick )
295end