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