Skip to content

Instantly share code, notes, and snippets.

@JElbourne
Last active June 21, 2020 03:47
Show Gist options
  • Save JElbourne/a7e3c4c18d8af0705197fccbac2652f9 to your computer and use it in GitHub Desktop.
Save JElbourne/a7e3c4c18d8af0705197fccbac2652f9 to your computer and use it in GitHub Desktop.
GRID SERIALIZATION CONCEPT FOR SAVING WORLD GRID DATA
/**
* GRID SERIALIZATION CONCEPT FOR SAVING WORLD GRID DATA
*
* Each GRID location will be stored as an signed int32 (eg. -1,999,999,999) - 4 BYTES
* This will be a basic serialization of all the information on the grid.
*
* This information will be stored as follows:
*
* -/+ This acts as a switch and usually doubles the possibilities of each ID
* xxxxxxxxx9 - Ground Layer (ID render 0-9). [20 possibilities]
* xxxxxxxx9x - Ground Modifier ID (ID render 0-9). [20 possibilities] [0 being nothing]
* xxxxxx99xx - Block Type (ID render 0-99). [200 possibilities] [0 being nothing]
* xxx999xxxx - Pickup Item Type (ID render 0-999). [2000 possibilities] [0 being nothing]
* x99xxxxxxx - Pickup Item Quantity (ID render 0-99). [200 max, the -/+ acts as a doubler for this] [starts counting at 0, +1]
* 1xxxxxxxxx - This can only be set to either 1 or 0. It will act as a secondary switch/flag on the block.
*
* There will be a requirement for the provided value to be LESS than 2,000,000,000
*/
#include <iostream>
long int Modulus = 0;
int PlaceModifier = 1;
// Seriazation Technique has a Unique Pattern for the GridID so we must manually set unique Modulus and PlaceModifiers for the index of ID
void SetModulusAndPlaceModifier(int Index, long int &Modulus, int &PlaceModifier)
{
switch (Index)
{
case 6:
{
Modulus = 10;
PlaceModifier = 1;
}
break;
case 5:
{
Modulus = 100;
PlaceModifier = 10;
}
break;
case 4:
{
Modulus = 10000;
PlaceModifier = 100;
}
break;
case 3:
{
Modulus = 10000000;
PlaceModifier = 10000;
}
break;
case 2:
{
Modulus = 1000000000;
PlaceModifier = 10000000;
}
break;
case 1:
{
Modulus = 10000000000;
PlaceModifier = 1000000000;
}
break;
default:
{
Modulus = 0;
PlaceModifier = 1;
}
}
}
// Getting a value from the Grid ID
// It is important we dont use zero as Modulus with the compiler so we check, this happens with the First digit.
int GetValueFromGridId(int GridId, long int Modulus, int PlaceModifier)
{
return (Modulus > 0 && PlaceModifier > 0) ? (GridId % Modulus) / PlaceModifier : (GridId > 0) ? 1 : -1;
}
// Setting the Grid ID
void SetBlockIdInGridId(int &GridId, int ValueToSet[7])
{
// Manual Clamp Values
ValueToSet[0] = (ValueToSet[0] < -1) ? -1 : (1 < ValueToSet[0]) ? 1 : ValueToSet[0];
ValueToSet[1] = (1 < ValueToSet[1]) ? 1 : ValueToSet[1];
ValueToSet[2] = (99 < ValueToSet[2]) ? 99 : ValueToSet[2];
ValueToSet[3] = (999 < ValueToSet[3]) ? 999 : ValueToSet[3];
ValueToSet[4] = (99 < ValueToSet[4]) ? 99 : ValueToSet[4];
ValueToSet[5] = (9 < ValueToSet[5]) ? 9 : ValueToSet[5];
ValueToSet[6] = (9 < ValueToSet[6]) ? 9 : ValueToSet[6];
int NumsToAdd = 0;
for (int i = 1; i < 7 ; i++)
{
SetModulusAndPlaceModifier(i, Modulus, PlaceModifier);
if (ValueToSet[i] < 0)
{
int val = GetValueFromGridId(GridId, Modulus, PlaceModifier);
NumsToAdd += (val * PlaceModifier);
}
else
{
NumsToAdd += (ValueToSet[i] * PlaceModifier);
}
}
if (ValueToSet[0] != 0) NumsToAdd *= ValueToSet[0];
GridId = NumsToAdd;
}
////////////////////////////////////////////////////////////////////////////////
// TESTS
int main()
{
int GridId;
// TESTING SETTING
// Ensure no leading Zero which tells combiler the number is Octal....this will break the ID system.
// Order Of Data: { +/- Switch, 0/1 Flag, Pickup Item Qnt., Pickup Item ID, Block ID, Ground Modifier, Ground Layer ID }
// Basic Setting Value Test
GridId = 1000000009;
int ValueToSet[7] = { 1, 1, 10, 945, 33, 0, 1 };
SetBlockIdInGridId(GridId, ValueToSet);
std::cout << "Test 1 Pass: " << (GridId == 1109453301) << "!\n";
// Convert To negative Test
GridId = 1000000009;
int ValueToSet1[7] = { -1, 1, 10, 945, 33, 0, 1 };
SetBlockIdInGridId(GridId, ValueToSet1);
std::cout << "Test 2 Pass: " << (GridId == -1109453301) << "!\n";
// Test to make sure we can bypass setting a value.
GridId = 1000880009;
int ValueToSet2[7] = { 1, 1, 10, -1, 33, 0, 1 };
SetBlockIdInGridId(GridId, ValueToSet2);
std::cout << "Test 3 Pass: " << (GridId == 1100883301) << "!\n";
// Test to make sure we set Values to stay the same if we wish.
GridId = 1072840009;
int ValueToSet3[7] = { 0, -1, -10, -44444, -33, -1, -1 };
SetBlockIdInGridId(GridId, ValueToSet3);
std::cout << "Test 4 Pass: " << (GridId == 1072840009) << "!\n";
// Test to make sure we can adjust each Value individually.
GridId = 1123456789;
int ValueToSet5a[7] = { 0, -1, -1, -1, -1, -1, 3 };
SetBlockIdInGridId(GridId, ValueToSet5a);
std::cout << "Test 5a Pass: " << (GridId == 1123456783) << "!\n";
GridId = 1123456789;
int ValueToSet5b[7] = { 0, -1, -1, -1, -1, 2, -1 };
SetBlockIdInGridId(GridId, ValueToSet5b);
std::cout << "Test 5b Pass: " << (GridId == 1123456729) << "!\n";
GridId = 1123456789;
int ValueToSet5c[7] = { 0, -1, -1, -1, 99, -1, -1 };
SetBlockIdInGridId(GridId, ValueToSet5c);
std::cout << "Test 5c Pass: " << (GridId == 1123459989) << "!\n";
GridId = 1123456789;
int ValueToSet5d[7] = { 0, -1, -1, 878, -1, -1, -1 };
SetBlockIdInGridId(GridId, ValueToSet5d);
std::cout << "Test 5d Pass: " << (GridId == 1128786789) << "!\n";
GridId = 1123456789;
int ValueToSet5e[7] = { 0, -1, 55, -1, -1, -1, -1 };
SetBlockIdInGridId(GridId, ValueToSet5e);
std::cout << "Test 5e Pass: " << (GridId == 1553456789) << "!\n";
GridId = 1123456789;
int ValueToSet5f[7] = { 0, 0, -1, -1, -1, -1, -1 };
SetBlockIdInGridId(GridId, ValueToSet5f);
std::cout << "Test 5f Pass: " << (GridId == 123456789) << "!\n";
// Test Get Modulus & Modifiers
SetModulusAndPlaceModifier(0, Modulus, PlaceModifier);
std::cout << "Test 6 Pass: " << ((Modulus == 0) && (PlaceModifier == 1)) << "!\n";
SetModulusAndPlaceModifier(1, Modulus, PlaceModifier);
std::cout << "Test 7 Pass: " << ((Modulus == 10000000000) && (PlaceModifier == 1000000000)) << "!\n";
SetModulusAndPlaceModifier(2, Modulus, PlaceModifier);
std::cout << "Test 8 Pass: " << ((Modulus == 1000000000) && (PlaceModifier == 10000000)) << "!\n";
SetModulusAndPlaceModifier(3, Modulus, PlaceModifier);
std::cout << "Test 9 Pass: " << ((Modulus == 10000000) && (PlaceModifier == 10000)) << "!\n";
SetModulusAndPlaceModifier(4, Modulus, PlaceModifier);
std::cout << "Test 10 Pass: " << ((Modulus == 10000) && (PlaceModifier == 100)) << "!\n";
SetModulusAndPlaceModifier(5, Modulus, PlaceModifier);
std::cout << "Test 11 Pass: " << ((Modulus == 100) && (PlaceModifier == 10)) << "!\n";
SetModulusAndPlaceModifier(6, Modulus, PlaceModifier);
std::cout << "Test 12 Pass: " << ((Modulus == 10) && (PlaceModifier == 1)) << "!\n";
// Test Get Values
int val;
GridId = 1100883301;
SetModulusAndPlaceModifier(0, Modulus, PlaceModifier);
val = GetValueFromGridId(GridId, Modulus, PlaceModifier);
std::cout << "Test 13 Pass: " << (val == 1) << "!\n";
GridId = -1100883301;
SetModulusAndPlaceModifier(0, Modulus, PlaceModifier);
val = GetValueFromGridId(GridId, Modulus, PlaceModifier);
std::cout << "Test 14 Pass: " << (val == -1) << "!\n";
GridId = 1100883301;
SetModulusAndPlaceModifier(1, Modulus, PlaceModifier);
val = GetValueFromGridId(GridId, Modulus, PlaceModifier);
std::cout << "Test 15 Pass: " << (val == 1) << "!\n";
SetModulusAndPlaceModifier(2, Modulus, PlaceModifier);
val = GetValueFromGridId(GridId, Modulus, PlaceModifier);
std::cout << "Test 16 Pass: " << (val == 10) << "!\n";
SetModulusAndPlaceModifier(3, Modulus, PlaceModifier);
val = GetValueFromGridId(GridId, Modulus, PlaceModifier);
std::cout << "Test 17 Pass: " << (val == 88) << "!\n";
SetModulusAndPlaceModifier(4, Modulus, PlaceModifier);
val = GetValueFromGridId(GridId, Modulus, PlaceModifier);
std::cout << "Test 18 Pass: " << (val == 33) << "!\n";
SetModulusAndPlaceModifier(5, Modulus, PlaceModifier);
val = GetValueFromGridId(GridId, Modulus, PlaceModifier);
std::cout << "Test 19 Pass: " << (val == 0) << "!\n";
SetModulusAndPlaceModifier(6, Modulus, PlaceModifier);
val = GetValueFromGridId(GridId, Modulus, PlaceModifier);
std::cout << "Test 20 Pass: " << (val == 1) << "!\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment