# Lua Programming Examples

## Execute Lua Online

local PlayerGold = 999999999

function OnPlayerTurnActivated( player, bIsFirstTime )

local currentTurn = Game.GetCurrentGameTurn();
local SpawnTurn = 0;
if currentTurn == GameConfiguration.GetStartTurn() then
local pPlayer = Players[player]
if pPlayer:IsHuman() then
pPlayer:GetTreasury():SetGoldBalance(PlayerGold);
end
end
end

function Initialize()
end

Initialize();

## Execute Lua Online

alphabet = "0123456789AB"

function getDigitValue(digit)
for i = 1, 12 do
if digit == string.sub(alphabet, i, i) then return (i-1) end
end
return -1
end

function decToDuodec(dec)
duodec = ""
while dec > 0 do
duodec = string.sub(alphabet, (dec%12)+1, (dec%12)+1) .. duodec
dec = math.floor(dec / 12)
end
return duodec
end

function duodecToDec(duodec)
dec = 0
for i = #duodec, 1, -1 do
value = getDigitValue(string.sub(duodec, i, i))
if value == -1 then
return -1
else
dec = dec + value*math.pow(12, #duodec-i)
end
end
return math.floor(dec)
end

function calcNextValue(currVal)
currDec = duodecToDec(currVal)
prod = 1
for i = 1, #currVal do
value = getDigitValue(string.sub(currVal, i, i))
if value == -1 then
return -1
end
if value ~= 0 then
prod = prod * value
end
end
return decToDuodec(currDec + prod)
end

currVal = "X" --zamień X na poprawną liczbę (bez usuwania cudzysłowu)
nextVal = calcNextValue(currVal)
if nextVal == -1 then
io.write("Wprowadzony ciąg nie jest poprawną liczbą w systemie dwunastkowym.")
else
io.write("Następna liczba to: "..nextVal..".")
end

function RollADie(side)
if side==1 then
return "1"
elseif side==2 then
return "2"
elseif side==3 then
return "3"
elseif side==4 then
return "4"
elseif side==5 then
return "5"
elseif side==6 then
return "6"
end
end
print(RollADie(math.random(1,6)))

## Execute Lua Online

player = {
identify = {
fistName = "Gregory",
lastName = "Black",
job = "diamond_riders",
-- https://stackoverflow.com/questions/11171524/how-can-i-reach-a-class-variable-from-another-class-in-lua
},
inventory = {
{"cash", 2600},
{"dirty cash", 500}
}
}

jobs = {
winemaker = {
type = "work",
label = "Winemaker"
},
diamond_riders = {
type = "organization",
label = "diamond_riders",
zones = {
tissue = {
type = "work",
item = {
name = "tissue",
db_name = "tissue",
max = 100,
remove = 0,
time = 360,
cash = 200, -- if type of zone is "work", then "cash" represent how many cash you pay for item to next stage of production
requires = "nothing",
communiqueBefore = "You started negotiations",
communiqueAfter = "Negotiations successful!"
}
},
tobacco = {
type = "work",
name = "Tobacco harvesting",
item = {
name = "tobacco",
db_name = "tobacco",
max = 100,
remove = 0,
time = 360,
cash = 0,
requires = "nothing",
communiqueBefore = "You started harvesting",
communiqueAfter = "Harvesting successful!"
}
}
}
}
}

function onDuty(onDuty, job) -- does player be "on duty"
if onDuty == true then
return true
elseif onDuty == false and jobs[job].type == "work" then
print("You have to wear your worker suits first!")
return false
elseif onDuty == false and jobs[job].type == "organization" then
print("You need to start your activity in cloak room!")
return false
else
print("Oppss, something wrong with function name onDuty, contact with administrator")
return false
end
end

function isCompanyVehNear(requiredDistance, distance, licensePlate, licensePlateOwn, job) -- does player have company car near
if jobs[job].type == "organization" then
return true
elseif distance > requiredDistance then
print("Yout vehicle is too far!")
return false
elseif
return false
else
print("OK, you have car near you") -- dv info
return true
end
end

function processing(job, zone)
-- Check is player on duty and have own company vehicle. If something is false then brake function and give proper information
if onDuty(true, "diamond_riders") == false or isCompanyVehNear(20, 15, "AW 30", "AW 30", "diamond_riders") ==  false then
return false
--elseif jobs[job].zones[zone].requires == "nothing" then
elseif jobs[job].zones[zone].type == "work" then
print("something")
end
end

-- function givePlayerItem(itemName, Qty) // removePlayerItem

-- sprawdzanie funkcji
--isCompanyVehNear(20, 15, "AW 30", "AW 30", "diamond_riders")
processing("diamond_riders", "tobacco")



## Execute Lua Online

print("Hello World!")
array1={1,2,3,4,5}
int array2= {1,2,3,5,4}
for a=1,5
do
if(array1[i] = array2[i])
{
print("Match")
}
else
{
print ("array1 Element"+array1[i] + "array2 Element"+array1[i])
}
end

## Execute Lua Online

print("Hello World!")
array1={1,2,3,4,5}
int array2= {1,2,3,5,4}
for a=1,5
do
if(array1[i] = array2[i])
{
print("Match")
}
else
{
print ("array1 Element"+array1[i] + "array2 Element"+array1[i])
}
end

## quartic

local function IsZero(d)
local eps = 1e-9
return (d > -eps and d < eps)
end

local s0, s1
local p, q, D
local num

-- normal form: x^2 + px + q = 0
p = c1 / (2 * c0)
q = c2 / c0

D = p * p - q

if IsZero(D) then
s0 = -p
num = 1
elseif (D < 0) then
num = 0
else
local sqrt_D = math.sqrt(D)

s0 = sqrt_D - p
s1 = -sqrt_D - p
num = 2
end

return num, s0, s1
end

function solveCubic(c0, c1, c2, c3)
local s0, s1, s2
local num
local sub
local A, B, C
local sq_A, p, q
local cb_p, D

-- normal form: x^3 + Ax^2 + Bx + C = 0
A = c1 / c0
B = c2 / c0
C = c3 / c0

-- substitute x = y - A/3 to eliminate quadric term: x^3 + px + q = 0
sq_A = A * A
p = (1 / 3) * (-(1 / 3) + sq_A + B)
q = (1 / 3) * ((2 / 27) * A * sq_A - (1 / 3) * A * B + C)

-- use Cardano's formula
cb_p = p * p * p
D = q * q + cb_p

if IsZero(D) then
if IsZero(q) then -- one triple solution
s0 = 0
num = 1
else -- one single and one double solution
local u = -q ^ (1 / 3)
s0 = 2 * u
s1 = -u
num = 2
end
elseif (D < 0) then -- casus irreducibilis
local phi = (1 / 3) * math.acos(-q / math.sqrt(-cb_p))
local t = 2 * math.sqrt(-p)

s0 = t * math.cos(phi)
s1 = -t * math.cos(phi + math.pi / 3)
s2 = -t * math.cos(phi - math.pi / 3)
num = 3
else -- one real solution
local sqrt_D = math.sqrt(D)
local u = (sqrt_D - q) ^ (1 / 3)
local v = (sqrt_D + q) ^ (1 / 3)

s0 = u + v
num = 1
end

-- resubstitute
sub = (1 / 3) * A
if (num > 0) then s0 = s0 - sub end
if (num > 1) then s1 = s1 - sub end
if (num > 2) then s2 = s2 - sub end

return num, s0, s1, s2
end

function solveQuartic(c0, c1, c2, c3, c4)
local s0, s1, s2, s3

local coeffs = {}
local z, u, v, sub
local A, B, C, D
local sq_A, p, q, r
local num

A = c1 / c0
B = c2 / c0
C = c3 / c0
D = c4 / c0

sq_A = A * A
p = -(3 / 8) * sq_A + B
q = (1 / 8) * sq_A * A - (1 / 2) * A * B + C
r = -(3 / 256) * sq_A * sq_A + (1 / 16) * sq_A * B - (1 / 4) * A * C + D

if IsZero(r) then
coeffs[4] = q
coeffs[3] = p
coeffs[2] = 0
coeffs[1] = 1

num, s0, s1, s2 = solveCubic(coeffs[1], coeffs[2], coeffs[3], coeffs[4])
else
coeffs[4] = (1 / 2) * r * p - (1 / 8) * q * q
coeffs[3] = -r
coeffs[2] = -(1 / 2) * p
coeffs[1] = 1

local drop
drop, s0, s1, s2 = solveCubic(coeffs[1], coeffs[2], coeffs[3], coeffs[4])
drop = nil

z = s0

u = z * z - r
v = 2 * z - p

if IsZero(u) then
u = 0
elseif (u > 0) then
u = math.sqrt(u)
else
num = 0
end

if IsZero(v) then
v = 0
elseif (v > 0) then
v = math.sqrt(v)
else
num = 0
end

coeffs[3] = z - u
coeffs[2] = (q < 0) and -v or v
coeffs[1] = 1

num, s0, s1 = solveQuadric(coeffs[1], coeffs[2], coeffs[3])

coeffs[3] = z + u
coeffs[2] = (q < 0) and v or -v
coeffs[1] = 1

if (num == 0) then num = num + solveQuadric(coeffs[1], coeffs[2], coeffs[3]) end
if (num == 1) then num = num + solveQuadric(coeffs[1], coeffs[2], coeffs[3]) end
if (num == 2) then num = num + solveQuadric(coeffs[1], coeffs[2], coeffs[3]) end
end

-- resubstitute
sub = (1 / 4) * A

if (num > 0) then s0 = s0 - sub end
if (num > 1) then s1 = s1 - sub end
if (num > 2) then s2 = s2 - sub end
if (num > 3) then s3 = s3 - sub end

return num, s0, s1, s2, s3
end

return solveQuartic(5, -3, 6, 9, -4)

## autochess

function AddAChessToChessPool(chess)
local maxcount = 1

if string.find(chess,'11') ~= nil and (string.find(chess,'tp') ~= nil or string.find(chess,'eh') ~= nil) then
chess = string.sub(chess,1,-3)
maxcount = 4

elseif string.find(chess,'1') ~= nil and (string.find(chess,'tp') ~= nil or string.find(chess,'eh') ~= nil) then
chess = string.sub(chess,1,-2)
maxcount = 2

elseif string.find(chess,'11') ~= nil then
chess = string.sub(chess,1,-3)
maxcount = 9

elseif string.find(chess,'1') ~= nil then
chess = string.sub(chess,1,-2)
maxcount = 3
end

return chess, maxcount
end

a = 'chess_cm11'

print(AddAChessToChessPool('chess_ld'))

## Ohfn

gg.alert(os.date("Today: %Y/%m/%d Time: %H:%M:%S\nu4minext Simple (10.5)"))
gg.alert("u4minext Simple v0.1 \nDont Forget To Connect VPN \nAll Anti Ban Files Uploaded On My Telegram Channel \n t.m/u4minext")

function Main()

START = gg.choice({
' WallCol',
' Recoil',
' Antenna',
' Micro Speed',
' Black Sky',
' Exit',

}, nil, 'Wellcome To u4minext Simple v0.1\nYouTube And Telegram: u4minext \nKill Under 15')
if START == 1 then WHCOL() end
if START == 2 then REC() end
if START == 3 then MHS() end
if START == 4 then ANT() end
if START == 5 then SPD() end
if START == 6 then BSY() end
if START == 7 then Exit() end
XGCK=-1
end

function WHCOL()
gg.searchNumber("2.718519e-43F;3.7615819e-37F;2.0F;0.00999999978F::200", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.searchNumber("2", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.getResults(30)
gg.editAll("130", gg.TYPE_FLOAT)
gg.searchNumber("5.8013756e-42F;-5.5695588e-40F;2.0F::100", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.searchNumber("2", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.getResults(30)
gg.editAll("130", gg.TYPE_FLOAT)
gg.clearResults()
gg.setVisible(false)
gg.searchNumber("69,778D;1,669,332,992D;11D", gg.TYPE_DWORD, false, gg.SIGN_EQUAL, 0, -1)
gg.setVisible(false)
gg.searchNumber("11", gg.TYPE_DWORD, false, gg.SIGN_EQUAL, 0, -1)
gg.getResults(100)
gg.editAll("32777", gg.TYPE_DWORD)
gg.toast("WallCol ACTIVATED")
end

function REC()
gg.setRanges(gg.REGION_ANONYMOUS)
gg.searchNumber("1,084,227,584D;1D;0.64999997616F;1.2520827e-32F", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.searchNumber("1.2520827e-32", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.getResults(100)
gg.editAll("1.4012985e-43", gg.TYPE_FLOAT)
gg.setRanges(gg.REGION_ANONYMOUS)
gg.searchNumber("0.2~0.3;53;30;1::", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.searchNumber("0.2~0.3;1::", gg.TYPE_FLOAT)
gg.getResults(200)
gg.editAll("1.4012985e-45", gg.TYPE_FLOAT)
gg.toast("NO RECOIL ACTIVATED")
end

function MHS()
gg.clearResults()
gg.searchNumber("-88.66608428955;26:512", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.searchNumber("26", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.getResults(2)
gg.editAll("-460", gg.TYPE_FLOAT)
gg.clearResults()
gg.searchNumber("-88.73961639404;28:512", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.searchNumber("28", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.getResults(2)
gg.editAll("-560", gg.TYPE_FLOAT)
gg.clearResults()
gg.setRanges(gg.REGION_ANONYMOUS)
gg.searchNumber("9.201618;30.5;25", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.searchNumber("25;30.5", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.getResults(10)
gg.editAll("450", gg.TYPE_FLOAT)
gg.clearResults()
gg.setRanges(gg.REGION_C_BSS)
gg.searchNumber("2048D;1F", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.searchNumber("1", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.getResults(100)
gg.editAll("0.07", gg.TYPE_FLOAT)
gg.clearResults()
end

function ANT()
gg.setRanges(gg.REGION_ANONYMOUS)
gg.searchNumber("0.53446006775F;-1.68741035461F:501", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.searchNumber("-1.68741035461", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.getResults(995)
gg.editAll("9999", gg.TYPE_FLOAT)
gg.searchNumber("18.38612365723F;0.54026412964F:5", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.searchNumber("18.38612365723F;0.54026412964F:5", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.getResults(995)
gg.editAll("9999", gg.TYPE_FLOAT)
gg.clearResults()
gg.toast("ANTENNA ACTIVATED")
end

function SPD()
gg.setRanges(gg.REGION_ANONYMOUS)
gg.searchNumber("0;7.0064923e-45;1;100;1;2,500,000,000.0;0.10000000149;88", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.searchNumber("1", gg.TYPE_FLOAT, false, gg.SIGN_EQUAL, 0, -1)
gg.getResults(100)
gg.editAll("1.7", gg.TYPE_FLOAT)
gg.toast("SPEED ACTIVATED")
end

function BSY()
gg.searchNumber("100F;1F;1,008,981,770D:99", gg.TYPE_FLOAT, false)
gg.searchNumber("100", gg.TYPE_FLOAT, false)
gg.getResults(100)
gg.editAll("-9999", gg.TYPE_FLOAT)
gg.toast("BLACK SKY ACTIVATED!")
end

function Exit()
print('Thank You')
print('Dont Forget To Subscribe YouTube Channel')                                                                              os.exit()
end

cs = "Don't Copy My Script Mother Fucker"
while true do
if gg.isVisible(true) then
XGCK = 1
gg.setVisible(false)
end
gg.clearResults()
if XGCK == 1 then
Main()
end
end


## Execute Lua Online

local test = true and true or false