SCRMaps
/
Sign in

One data block instead of six arrays

If adding one hero means editing six arrays together, an index bug is a matter of time. Write the data once and let the compiler make the arrays.

14 min read

Most maps grow into the same shape: a HeroName array, a HeroUnit array, a HeroSlot array, a HeroInitRank array and two more for rank increments. Adding one hero means editing six places together, and nothing checks that the six stayed in step.

epScript can run Python at compile time, so the data can be written once as a list of dictionaries and the arrays generated from it.

const HERO_DATA = py_eval('[ \
    {"name": "손책", "unit": 48, "slot": 0, "ranks": [1321, 1322, 1323]}, \
    {"name": "황개", "unit": 23, "slot": 1, "ranks": [1304, 1307, 1310]}, \
]');

const HERO_COUNT    = py_len(HERO_DATA);
const HeroNames     = EUDArray(py_eval('[EPD(Db(x["name"])) for x in HERO_DATA]'));
const HERO_UNITS    = py_list(py_map(py_eval('lambda x: x["unit"]'), HERO_DATA));
const HeroUnitTypes = EUDArray(HERO_UNITS);
const HeroSlots     = EUDArray(py_eval('[x["slot"] for x in HERO_DATA]'));
Adding a hero is one line, and nothing can fall out of step because there is only one list.

And the part most worth taking even if you keep your arrays as they are comes next: a rule your data has to satisfy can be made into a compile error.

py_assert(HERO_COUNT == py_len(py_set(HERO_UNITS)), "영웅 유닛 중복");
py_assert(py_eval('all(len(x["ranks"]) == 1+MAX_RANK for x in HERO_DATA)'), "계급 개수 불일치");
py_assert(py_eval('all(0 <= x["slot"] < SLOT_COUNT for x in HERO_DATA)'), "슬롯 범위 초과");
Two heroes sharing a unit id, a row with only two ranks, a slot number past the end of the slot table - all three are silent in game, and all three fail the build here.

It is the counterpart of ep_assert, which proves a property of the code - “this construct emits no triggers”. py_assert checks that your data makes sense.

Packing several small values into one cell is also a compile-time job, and it removes two of the three arrays.

const HeroRankData = EUDArray(py_eval(
    '[(x["ranks"][0] - 1302) + ((x["ranks"][1] - 1302) << 8)'
    ' + ((x["ranks"][2] - 1302) << 16) for x in HERO_DATA]'));

const rank = bread_epd(HeroRankData + heroIndex, currentRank);   // currentRank번째 바이트
The shifting happens in Python, so it costs nothing in game. bread_epd takes a variable as its second argument, so choosing which of the three you want is one read - where the three-array version needed a branch per case.

When a whole data table has to be swapped - a game mode, say - give the variable an array type and repoint it.

var HeroRanksData: EUDArray = HeroRankDataMod0;
// ...
HeroRanksData = HeroRankDataMod1;                 // 표 전체를 교체 / swap the whole table
for (var i = 0; i < HERO_COUNT; i += 1) updateHeroRank(i);
The mode table is generated at compile time from a Python dict of only the rows that differ, merged over the base. What it replaced was a pair of hundred-line functions assigning array cells one at a time.

For an array of units, type the EUDVArray as CUnit. What comes out is a CUnit, the offset arithmetic disappears, and the death check reads like the sentence it is.

const HeroCUnit = EUDVArray(HERO_COUNT, CUnit)();

foreach(hero : HeroCUnit) {
    if (hero == 0) continue;
    if (hero.hp <= 0) hero.order = "Die";   // 먼저 / first
    if (hero.order == "Die") { /* ... */ } // 그 다음 / then
}
The order matters: set order from hp first and then test order, and one pass handles both “killed by damage” and “already dying”.

Looking at the same clean-up carried all the way through a real map, three more things come with it beyond merging the arrays. The record is Artanis rewriting a 61-hero Three Kingdoms map end to end.

What follows after merging the arrays
Store it rather than counting itThe hero's current rank was being recovered by counting how many rank-marker units existed. Storing it in a HeroCurrentRank array and leaving the unit as display only deleted an entire difference-calculating function and turned three arrays into one.
Write only the differencesThe map had two rank systems and two enormous functions that edited dozens of entries by hand. Keep only the differing ranks in a Python dict, merge with the base at compile time into a second array, and switching becomes two lines: repoint the variable and re-run the update for everyone.
Initialise on creation, not on deathInitialising the rank in the death handler leaves a newly created hero wrong on its first frame. Initialise on creation and it is right from the start.

And one expectation to set: doing all of this leaves the line count about where it was. Shrinking the code is not the point - making the index mistakes structurally impossible is.

One thing is easy to get backwards, so be explicit. "Merge the parallel arrays" means merge the data a person edits, not make the arrays the game reads into nested ones. For values that are always used together, like a coordinate, two arrays - one of x, one of y - beat an array of [x, y] pairs: one index fetches both, and nothing is spent on storing the inner containers.

What a nested array actually costs
Space15 pairs as an array of arrays is 15 outer cells plus 15 times 2 inner, so 45. Flat, it is 30.
TriggersThe bracket syntax also emits triggers that fill the values at game start: for 15 entries, fifteen two-action triggers plus one fifteen-action trigger. Written as EUDArray(list(...)) the constants go straight into the map file and no trigger is added.
ReadingA helper like arr2d(arr, i, j) called twice with the same i reads the inner array twice; written out by hand it reads once.

EUDArray cannot be subscripted twice. If you genuinely need nesting, use a container that takes an element type, such as EUDVArray(n, EUDArray).

Types and scopeReading what a unit is doing