Skip to content

Instantly share code, notes, and snippets.

View CaptainPRICE's full-sized avatar
💭
🥚

CaptainPRICE

💭
🥚
View GitHub Profile
@CaptainPRICE
CaptainPRICE / ConvertToUnit.lua
Last active September 3, 2020 11:48
Unit converter made for Garry's Mod.
UNIT = setmetatable({}, {
__index = {
KILOMETRE = 1;
METER = 2;
CENTIMETRE = 3;
MILE = 3;
INCH = 4;
FOOT = 5;
};
__metatable = false;
@CaptainPRICE
CaptainPRICE / __mapped_coloring.txt
Last active March 24, 2016 22:22
Expression 2 - Fading color based on Z (using percentage method; mapped 0% to low, 100% to high and then converted to color - so bottom is colored darker/black and top is being colored brighter/white)
# New version/preview: https://i.imgur.com/xsq3MJ7.png
# Old version/preview: https://i.imgur.com/2F23MlA.png
if (first())
{
function number percentOfValueInRange(Min, Max, Value)
{
return ((Min - Value) / (Min - Max)) * 100
}
--
-- Simple Starfall chip code viewer/hacker (addon: https://github.com/thegrb93/StarfallEx).
--
local sf = SF
if (not istable(sf)) then
return
end
local allInstances = sf.allInstances
if (not istable(allInstances)) then
return
@CaptainPRICE
CaptainPRICE / getPlayersNameSubstr.txt
Created June 3, 2016 11:57
A small helper function (E2) to get an array of all players names with an argument of max. length (player with name greater than Length, will have the name "cuted-down"). Very simple.
function array getPlayersNameSubstr(Length)
{
local Result = array()
local Players = players()
for (Index = 1, Players:count())
{
local Player = Players[Index, entity]
Result:pushString(Player:name():sub(1, Length))
}
return Result
@CaptainPRICE
CaptainPRICE / math_inf_nan.lua
Last active August 20, 2021 22:13
math.finite, math.isinf & math.isnan functions in pure Lua. Completely covered (all tests passed). https://i.imgur.com/1vzo2x9.png
local NAN = 0.0 / 0.0
local NINF = -math.huge
local PINF = math.huge
--- Returns true if given value is a finite number; otherwise false or nil if value is not of type string nor number.
function math.finite(value)
if type(value) == "string" then
value = tonumber(value)
if value == nil then return nil end
elseif type(value) ~= "number" then
-- Localization of used libraries/functions, as an access to local (upvalues) is faster than global.
local file = file
local file_Delete, file_Find = file.Delete, file.Find
local file_DeleteDir -- Separate declaration from definition (since it is a recursive function).
--- A function to recursively remove/delete files and sub-directories.
--- gate_folder_name: Name of the folder for the Editor mode (e.g. expression2).
--- selected_folder_path: Directory (relative to gate_folder_name) to be deleted/removed.
file_DeleteDir = function(gate_folder_name, selected_folder_path)
@CaptainPRICE
CaptainPRICE / GMod - Dollhouse achievement unlocker
Last active August 31, 2016 20:53
I was pissed how Dollhouse achievement is somehow "protected" (the internal function simply didn't work inside a loop/hook/timer and so on), compared to others in achievements library, so I have found a way around it - as usually.
-- Whatever I have tried... It just simply did not increment Dollhouse achievement, it seems it is "protected" in some awkward way.
-- See: https://imgur.com/a/mT7aA
--local DollhouseID = 25
--[[hook.Add("Think", "Dollhouse achievement unlocker", function()
if achievements.GetCount(DollhouseID) < achievements.GetGoal(DollhouseID) then
achievements.SpawnedRagdoll()
print(achievements.GetName(DollhouseID), achievements.GetCount(DollhouseID) .. " / " .. achievements.GetGoal(DollhouseID))
else
local s = [[Hello\u003F World\u0021]]
-------------------------------------------------------------------------------
local string = string
local char, format, gmatch, gsub, sub = string.char, string.format, string.gmatch, string.gsub, string.sub
local tonumber = tonumber
local UESCAPE_PATTERN = "\\u([0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])"
-- Converts a fully uescaped string back to normal.
function string:from_uescape()
local out = ""
for i = 1, #self, 6 do
@CaptainPRICE
CaptainPRICE / print_id.lua
Last active September 13, 2016 15:52
Steam. IDs... IDs... IDs... Let's clean this mess up (GMod).
-- Made/Published to clear up misinformation, for you to possibly learn something new from it. - CaptainPRICE
-- Tell me in the comments (below) if you have spot bug(s), or if you have any suggestions or improvements as I would like to hear them!
-- See my comment here: https://github.com/wiremod/wire/pull/1217#issuecomment-246211364
local game = game
local IS_DEDICATED, IS_SINGLEPLAYER, MAX_NUM_PLAYERS = game.IsDedicated(), game.SinglePlayer(), game.MaxPlayers()
print(CLIENT and "CLIENT" or (SERVER and "SERVER" or "MENU"), IS_SINGLEPLAYER and "SP" or "MP", IS_DEDICATED and "DEDICATED" or "LISTEN", "MAX=" .. MAX_NUM_PLAYERS, "v" .. VERSION, BRANCH)
if not debug then return print("debug library is missing") end
@CaptainPRICE
CaptainPRICE / int32.lua
Last active December 18, 2019 08:49
struct int32 for Lua.
local math = math
local math_floor = math.floor
local error, setmetatable, rawget, tostring, type = error, setmetatable, rawget, tostring, type
int = {}
do
local INT32, INT32_NAME, INT32_SIZE, INT32_DEFAULT, INT32_MAX, INT32_MIN = 3, "int32", 32, 0, 2147483648, -2147483648
local int32 = {
__metatable = false,