forked from OldGamesLab/Harold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto.py
288 lines (232 loc) · 7.04 KB
/
proto.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
Copyright 2014 darkf
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Parser/converter for Fallout 1 and 2 .PRO files to a JSON format
# Fallout 1 mode
FO1 = True
from io import BufferedReader
import sys, os, struct, json
from typing import Any, Dict
def read16(f):
return struct.unpack("!h", f.read(2))[0]
def read32(f: BufferedReader):
return struct.unpack("!l", f.read(4))[0]
def read16At(buf, idx):
return struct.unpack('!h', buf[idx:idx + 2])[0]
def read32At(buf, idx):
return struct.unpack('!l', buf[idx:idx + 4])[0]
TYPE_ITEM = 0
TYPE_CRITTER = 1
TYPE_SCENERY = 2
TYPE_WALL = 3
TYPE_TILE = 4
TYPE_MISC = 5
SUBTYPE_ARMOR = 0
SUBTYPE_CONTAINER = 1
SUBTYPE_DRUG = 2
SUBTYPE_WEAPON = 3
SUBTYPE_AMMO = 4
SUBTYPE_MISC = 5
SUBTYPE_KEY = 6
SCENERY_DOOR = 0
SCENERY_STAIRS = 1
SCENERY_ELEVATOR = 2
SCENERY_LADDER_BOTTOM = 3
SCENERY_LADDER_TOP = 4
SCENERY_GENERIC = 5
def readScenery(f):
obj = {}
obj["wallLightTypeFlags"] = read16(f)
obj["actionFlags"] = read16(f)
obj["scriptPID"] = read32(f)
obj["subType"] = read32(f)
obj["materialID"] = read32(f)
obj["soundID"] = ord(f.read(1))
if obj["subType"] == SCENERY_DOOR:
obj["walkthroughFlag"] = read32(f)
# 4-byte unknown
elif obj["subType"] == SCENERY_STAIRS:
obj["destination"] = read32(f)
obj["destinationMap"] = read32(f)
elif obj["subType"] == SCENERY_ELEVATOR:
obj["elevatorType"] = read32(f)
obj["elevatorLevel"] = read32(f)
elif obj["subType"] == SCENERY_LADDER_BOTTOM or obj["subType"] == SCENERY_LADDER_TOP:
obj["destination"] = read32(f)
elif obj["subType"] == SCENERY_GENERIC:
pass # only 4-byte unknown
return obj
def readDrugEffect(f):
obj = {}
obj["duration"] = read32(f)
obj["amount0"] = read32(f)
obj["amount1"] = read32(f)
obj["amount2"] = read32(f)
return obj
def readItem(f: BufferedReader):
obj: Dict[str, Any] = {}
flagsExt = repr(f.read(3))
attackMode = ord(f.read(1))
scriptID = read32(f)
objSubType = read32(f)
materialID = read32(f)
size = read32(f)
weight = read32(f)
cost = read32(f)
invFRM = read32(f)
soundID = ord(f.read(1))
# FIXME: `flagsExt` is of `bytes` type that can't represented as JSON without additional conversions.
# obj["flagsExt"] = flagsExt
obj["itemFlags"] = ord(flagsExt[0])
obj["actionFlags"] = ord(flagsExt[1])
obj["weaponFlags"] = ord(flagsExt[2])
obj["attackMode"] = attackMode
obj["scriptID"] = scriptID
obj["subType"] = objSubType
obj["materialID"] = materialID
obj["size"] = size
obj["weight"] = weight
obj["cost"] = cost
obj["invFRM"] = invFRM
obj["soundID"] = soundID
if objSubType == SUBTYPE_WEAPON:
obj["animCode"] = read32(f)
obj["minDmg"] = read32(f)
obj["maxDmg"] = read32(f)
obj["dmgType"] = read32(f)
obj["maxRange1"] = read32(f)
obj["maxRange2"] = read32(f)
obj["projPID"] = read32(f)
obj["minST"] = read32(f)
obj["APCost1"] = read32(f)
obj["APCost2"] = read32(f)
obj["critFail"] = read32(f)
obj["perk"] = read32(f)
obj["rounds"] = read32(f)
obj["caliber"] = read32(f)
obj["ammoPID"] = read32(f)
obj["maxAmmo"] = read32(f)
obj["soundID"] = ord(f.read(1))
elif objSubType == SUBTYPE_AMMO:
obj["caliber"] = read32(f)
obj["quantity"] = read32(f)
obj["AC modifier"] = read32(f)
obj["DR modifier"] = read32(f)
obj["damMult"] = read32(f)
obj["damDiv"] = read32(f)
elif objSubType == SUBTYPE_ARMOR:
obj["AC"] = read32(f)
obj["stats"] = {}
for stat in ["DR Normal", "DR Laser", "DR Fire",
"DR Plasma", "DR Electrical", "DR EMP", "DR Explosive",
"DT Normal", "DT Laser", "DT Fire", "DT Plasma", "DT Electrical",
"DT EMP", "DT Explosive"]:
obj["stats"][stat] = read32(f)
obj["perk"] = read32(f)
obj["maleFID"] = read32(f)
obj["femaleFID"] = read32(f)
elif objSubType == SUBTYPE_DRUG:
obj["stat0"] = read32(f)
obj["stat1"] = read32(f)
obj["stat2"] = read32(f)
obj["amount0"] = read32(f)
obj["amount1"] = read32(f)
obj["amount2"] = read32(f)
obj["firstDelayed"] = readDrugEffect(f)
obj["secondDelayed"] = readDrugEffect(f)
obj["addictionRate"] = read32(f)
obj["addictionEffect"] = read32(f)
obj["addictionOnset"] = read32(f)
#else:
# print "warning: unhandled item subtype", objSubType
return obj
def readCritterStats(f):
stats = {}
for stat in ["STR", "PER", "END", "CHR", "INT", "AGI", "LUK", "HP", "AP",
"AC", "Unarmed", "Melee", "Carry", "Sequence", "Healing Rate",
"Critical Chance", "Better Criticals"]:
stats[stat] = read32(f)
for stat in ["DT Normal", "DT Laser", "DT Fire", "DT Plasma", "DT Electrical",
"DT EMP", "DT Explosive", "DR Normal", "DR Laser", "DR Fire",
"DR Plasma", "DR Electrical", "DR EMP", "DR Explosive",
"DR Radiation", "DR Poison"]:
stats[stat] = read32(f)
return stats
def readCritterSkills(f):
skills = {}
for skill in ["Small Guns", "Big Guns", "Energy Weapons", "Unarmed",
"Melee", "Throwing", "First Aid", "Doctor", "Sneak",
"Lockpick", "Steal", "Traps", "Science", "Repair",
"Speech", "Barter", "Gambling", "Outdoorsman"]:
skills[skill] = read32(f)
return skills
def readCritter(f):
obj = {}
obj["actionFlags"] = read32(f)
obj["scriptID"] = read32(f)
obj["headFID"] = read32(f)
obj["AI"] = read32(f)
obj["team"] = read32(f)
obj["flags"] = read32(f)
obj["baseStats"] = readCritterStats(f)
obj["age"] = read32(f)
obj["gender"] = read32(f)
obj["bonusStats"] = readCritterStats(f)
obj["bonusAge"] = read32(f)
obj["bonusGender"] = read32(f)
obj["skills"] = readCritterSkills(f)
obj["bodyType"] = read32(f)
obj["XPValue"] = read32(f)
obj["killType"] = read32(f)
if FO1 or obj["killType"] in (5, 10): # Robots/Brahmin
obj["damageType"] = None
else:
obj["damageType"] = read32(f)
return obj
def readPRO(f: BufferedReader):
obj = {}
objectTypeAndID = read32(f)
textID = read32(f)
frmTypeAndID = read32(f)
lightRadius = read32(f)
lightIntensity = read32(f)
flags = read32(f)
pid = objectTypeAndID & 0xffff
objType = (objectTypeAndID >> 24) & 0xff
obj["pid"] = pid
obj["textID"] = textID
obj["type"] = objType
obj["flags"] = flags
obj["lightRadius"] = lightRadius
obj["lightIntensity"] = lightIntensity
frmPID = frmTypeAndID & 0xffff
frmType = (frmTypeAndID >> 24) & 0xff
obj["frmPID"] = frmPID
obj["frmType"] = frmType
#print "type:", objType
if objType == TYPE_ITEM:
obj["extra"] = readItem(f)
elif objType == TYPE_CRITTER:
obj["extra"] = readCritter(f)
elif objType == TYPE_SCENERY:
obj["extra"] = readScenery(f)
else:
print(f"unhandled type {objType}")
return obj
def main():
if len(sys.argv) != 2:
print(f"USAGE: {sys.argv[0]} PRO")
sys.exit(1)
with open(sys.argv[1], "rb") as f:
print(json.dumps(readPRO(f)))
if __name__ == '__main__':
main()