-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsavefile.awk
More file actions
196 lines (166 loc) · 4.58 KB
/
savefile.awk
File metadata and controls
196 lines (166 loc) · 4.58 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
function save_game( i, x, y, hp, type, save_file) {
save_file = "savefile.sav"
# Save general game settings
print "" > save_file
print "[SAVE GENERAL]" > save_file
print "WORLD_WIDTH=" world_width > save_file
print "WORLD_HEIGHT=" world_height > save_file
print "GAME_LEVEL=" GAME_LEVEL > save_file
print "CURRENT_LEVEL=" CURRENT_LEVEL > save_file
print "CURRENT_EXPERIENCE=" CURRENT_EXPERIENCE > save_file
# Save game entities
print "" > save_file
print "[SAVE ENTITIES]" > save_file
print "id, type, x, y, hp" > save_file
count = 0
for (i in ENTITIES) {
type = ENTITIES[i]["type"]
x = ENTITIES[i]["x"]
y = ENTITIES[i]["y"]
hp = ENTITIES[i]["hp"]
print count "," type "," x "," y "," hp > save_file
count++
}
# Save game items
print "" > save_file
print "[SAVE ITEMS]" > save_file
print "id, type, x, y, picked_up" > save_file
count = 0
for (i in ITEMS) {
type = ITEMS[i]["type"]
x = ITEMS[i]["x"]
y = ITEMS[i]["y"]
picked_up = ITEMS[i]["picked_up"]
print count "," type "," x "," y "," picked_up > save_file
count++
}
# Save game tiles
print "" > save_file
print "[SAVE TILES]" > save_file
for(y=0;y<world_height;y++) {
line = ""
for (x=1;x<world_width;x++) {
line = line WORLD_MAP[x][y]
}
print line > save_file
}
# Save memory map
print "" > save_file
print "[SAVE MEMORY]" > save_file
for(y=0;y<world_height;y++) {
line = ""
for (x=1;x<world_width;x++) {
line = line MEMORY_MAP[x][y]
}
print line > save_file
}
# Save player inventory
print "" > save_file
print "[SAVE INVENTORY]" > save_file
line = INVENTORY[1]
for (i=2; i<=length(INVENTORY);i++) {
line = line "," INVENTORY[i]
}
print line > save_file
# Save player equipment
print "" > save_file
print "[SAVE EQUIPMENT]" > save_file
print "melee_weapon=" EQUIPMENT["melee_weapon"] > save_file
print "ranged_weapon=" EQUIPMENT["ranged_weapon"] > save_file
print "feat_wear=" EQUIPMENT["feat_wear"] > save_file
print "hand_wear=" EQUIPMENT["hand_wear"] > save_file
print "head_wear=" EQUIPMENT["head_wear"] > save_file
print "torso_wear=" EQUIPMENT["torso_wear"] > save_file
print "leg_wear=" EQUIPMENT["leg_wear"] > save_file
print "" > save_file
add_message("Your progress is saved.")
}
function load_csv_savefile(storage_array) {
getline
split($0,headers,",")
do {
EOF = !getline
if (EOF) {
exit
}
for (i in headers) {
headers[i] = trim(headers[i])
}
if (length($0) != 0) {
split($0,a,",")
id = trim(a[1])
for (i in headers) {
storage_array[id][headers[i]] = a[i]
}
}
} while (trim($0) != "")
}
function load_save_map(storage_array, chars) {
map_width = 0
map_height = 0
while(readline()) {
line = $0
if (map_width == 0) {
map_width = length(line)+1
for (x=0; x < map_width; x++) {
storage_array[x][map_height] = substr(line, x, 1)
}
map_height++
} else if (map_width != 0 && length(line)+1 == map_width) {
for (x=0; x < map_width; x++) {
storage_array[x][map_height] = substr(line, x, 1)
}
map_height++
} else {
print "Game map is jagged, please check " FILENAME " for errors"
system("sleep 5")
}
}
}
function savefile() {
for(i in ARGV) {
if (ARGV[i] == "savefile.sav") {
return 1
}
}
return 0
}
/^\[SAVE GENERAL\]/ {
load_block(lines)
for (i in lines) {
split(lines[i],a,"=")
key = trim(a[1])
value = trim(a[2])
globals[key] = value
}
world_width = globals["WORLD_WIDTH"]
world_height = globals["WORLD_HEIGHT"]
GAME_LEVEL = globals["GAME_LEVEL"]
CURRENT_LEVEL = globals["CURRENT_LEVEL"]
CURRENT_EXPERIENCE = globals["CURRENT_EXPERIENCE"]
}
/^\[SAVE ENTITIES\]/ {
load_csv_savefile(ENTITIES)
}
/^\[SAVE ITEMS\]/ {
load_csv_savefile(ITEMS)
}
/^\[SAVE INVENTORY\]/ {
getline
split($0,INVENTORY,",")
}
/^\[SAVE EQUIPMENT\]/ {
load_block(lines)
for (i in lines) {
split(lines[i],a,"=")
category = trim(a[1])
item_id = trim(a[2])
EQUIPMENT[category] = item_id
}
}
/^\[SAVE TILES\]/ {
load_save_map(WORLD_MAP)
}
/^\[SAVE MEMORY\]/ {
load_save_map(MEMORY_MAP)
}