Table Of Contents
What is data compression?
While there are a number of techniques used for data compression, in this article we will be referring to it as compressing large sets of data with vectors and custom strings. data compression is when larger sets of data are expressed in smaller forms for the computer to store and interpret later, in the case of workshop it will help save on element count.
When to use this?
Saving on element count due to arrays of data is typically for big gamemodes, with lots of custom strings and lots of vectors. This method of compression helps alleviate element count issues related to these things.
How do i compress my vectors?
To compress your vectors and strings, you'll need 3 rules. one to initialize the data and then call your function, 2nd, to decompress, and 3rd to parse each part of the string.
Example
variables
{
global:
26: numbers
27: colors
28: compressedData
29: doors
30: decompressed
31: i
32: scratch
33: param0
34: param1
35: param2
36: param3
37: param4
38: charIndex
}
subroutines
{
0: DecompressString
1: ParseNumber
}
rule("Workshop settings")
{
event
{
Ongoing - Global;
}
actions
{
Global.numbers = Custom String("0123456789ABCDEF");
Global.colors = Array(Color(White), Color(Yellow), Color(Green), Color(Purple), Color(Red), Color(Blue), Color(Aqua), Color(
Orange), Color(Sky Blue), Color(Turquoise), Color(Lime Green), Color(Gray), Color(Violet), Color(Rose), Color(Black));
Global.compressedData = Custom String(
"$Door A,v100,90.2,-80.3,c7,7.6,$Door B,v110,151.2,-60.3,c2,7.0,$Dangerous Door of Death and Doom,v190,40.2,110.3,cF,3.0,$Secr{0}",
Custom String("et Door,v83.2,45.2,19.6,c0,2.8"));
Call Subroutine(DecompressString);
Global.doors = Global.decompressed;
}
}
rule("Decompress string")
{
event
{
Subroutine;
DecompressString;
}
actions
{
Global.decompressed = Mapped Array(Global.compressedData, String Split(Current Array Element, Custom String(",")));
Global.compressedData = Empty Array;
For Global Variable(i, 0, Count Of(Global.decompressed), 1);
Modify Global Variable(compressedData, Append To Array, Global.decompressed[Global.i]);
End;
Global.decompressed = Empty Array;
For Global Variable(i, 0, Count Of(Global.compressedData), 1);
If(Char In String(Global.compressedData[Global.i], 0) == Custom String("v"));
Global.compressedData[Global.i] = String Slice(Global.compressedData[Global.i], 1, 999);
Call Subroutine(ParseNumber);
Global.scratch[0] = Global.param0;
Global.i += 1;
Call Subroutine(ParseNumber);
Global.scratch[1] = Global.param0;
Global.i += 1;
Call Subroutine(ParseNumber);
Global.scratch[2] = Global.param0;
Modify Global Variable(decompressed, Append To Array, Vector(Global.scratch[0], Global.scratch[1], Global.scratch[2]));
Else If(Char In String(Global.compressedData[Global.i], 0) == Custom String("c"));
Modify Global Variable(decompressed, Append To Array, Global.colors[Index Of String Char(Global.numbers, Char In String(
Global.compressedData[Global.i], 1))]);
Else If(Char In String(Global.compressedData[Global.i], 0) == Custom String("$"));
Modify Global Variable(decompressed, Append To Array, String Slice(Global.compressedData[Global.i], 1, 999));
Else;
Call Subroutine(ParseNumber);
Modify Global Variable(decompressed, Append To Array, Global.param0);
End;
End;
Global.compressedData = Null;
}
}
rule("Parse number")
{
event
{
Subroutine;
ParseNumber;
}
actions
{
Global.param0 = 0;
Global.param1 = 0;
Global.param2 = 1;
Global.param3 = 0;
Global.param4 = Null;
For Global Variable(charIndex, 0, String Length(Global.compressedData[Global.i]), 1);
Global.param4 = Char In String(Global.compressedData[Global.i], Global.charIndex);
If(Global.param4 == Custom String("-"));
Global.param2 *= -1;
Else If(Global.param4 == Custom String("."));
Global.param3 = 10;
Else;
Global.param1 = Index Of String Char(Global.numbers, Global.param4);
If(Global.param1 != -1);
If(Global.param3 == 0);
Global.param0 = Global.param0 * 10 + Global.param1;
Else;
Global.param0 += Global.param1 / Global.param3;
Global.param3 *= 10;
End;
End;
End;
End;
Global.param0 *= Global.param2;
}
}