|
| 1 | +--[[============================================================ |
| 2 | +--= |
| 3 | +--= LuaPreprocess example: Optimize data access. |
| 4 | +--= |
| 5 | +--= Here we have all data defined in one single place in the |
| 6 | +--= metaprogram, then we export parts of the data into multiple |
| 7 | +--= smaller tables that are easy and fast to access in the |
| 8 | +--= final program. |
| 9 | +--= |
| 10 | +--============================================================]] |
| 11 | + |
| 12 | +!( |
| 13 | +local IS_DEVELOPER = false |
| 14 | + |
| 15 | +local CHARACTERS = { |
| 16 | + {id="war1", type="warrior", race="human", name="Steve", unlockedByDefault=true, devOnly=false}, |
| 17 | + {id="war2", type="warrior", race="orc", name="Bog", unlockedByDefault=false, devOnly=false}, |
| 18 | + {id="mage1", type="mage", race="human", name="Elise", unlockedByDefault=true, devOnly=false}, |
| 19 | + {id="mage2", type="mage", race="elf", name="Cyan", unlockedByDefault=false, devOnly=false}, |
| 20 | + {id="arch1", type="archer", race="elf", name="Di", unlockedByDefault=true, devOnly=false}, |
| 21 | + {id="arch2", type="archer", race="monster", name="#&%€", unlockedByDefault=false, devOnly=false}, |
| 22 | + {id="dev", type="dev", race="human", name="Dev", unlockedByDefault=true, devOnly=true}, |
| 23 | +} |
| 24 | +) |
| 25 | + |
| 26 | +-- Array of character IDs, excluding special characters if we're not in developer mode. |
| 27 | +CHARACTER_IDS = { |
| 28 | + !for _, char in ipairs(CHARACTERS) do |
| 29 | + !if not char.devOnly or IS_DEVELOPER then |
| 30 | + !(char.id), |
| 31 | + !end |
| 32 | + !end |
| 33 | +} |
| 34 | + |
| 35 | +-- Maps between character IDs and other parameters. |
| 36 | +CHARACTER_NAMES = { |
| 37 | + !for _, char in ipairs(CHARACTERS) do |
| 38 | + !!(char.id) = !(char.name), |
| 39 | + !end |
| 40 | +} |
| 41 | +CHARACTER_TYPES = { |
| 42 | + !for _, char in ipairs(CHARACTERS) do |
| 43 | + !!(char.id) = !(char.type), |
| 44 | + !end |
| 45 | +} |
| 46 | +CHARACTERS_UNLOCKED_BY_DEFAULT = { |
| 47 | + !for _, char in ipairs(CHARACTERS) do |
| 48 | + !if char.unlockedByDefault then |
| 49 | + !!(char.id) = true, |
| 50 | + !end |
| 51 | + !end |
| 52 | +} |
| 53 | + |
| 54 | +-- Instead of iterating over the CHARACTERS array until we find |
| 55 | +-- the character with the specified ID, we use the maps above to |
| 56 | +-- get the information we want through a single table lookup. |
| 57 | +function getCharacterName(charId) |
| 58 | + return CHARACTER_NAMES[charId] |
| 59 | +end |
| 60 | +function getCharacterType(charId) |
| 61 | + return CHARACTER_TYPES[charId] |
| 62 | +end |
| 63 | +function isCharacterUnlockedByDefault(charId) |
| 64 | + return CHARACTERS_UNLOCKED_BY_DEFAULT[charId] == true |
| 65 | +end |
| 66 | + |
| 67 | +function printCharacterInfo() |
| 68 | + for _, charId in ipairs(CHARACTER_IDS) do |
| 69 | + print(getCharacterName(charId)) |
| 70 | + print(" Type ...... "..getCharacterType(charId)) |
| 71 | + print(" Unlocked .. "..tostring(isCharacterUnlockedByDefault(charId))) |
| 72 | + end |
| 73 | +end |
| 74 | + |
| 75 | +printCharacterInfo() |
| 76 | +print("Type of 'war1': "..getCharacterType("war1")) |
| 77 | +print("Is 'mage2' unlocked by default: "..tostring(isCharacterUnlockedByDefault("mage2"))) |
0 commit comments