Last active
October 18, 2021 23:33
-
-
Save person4268/d166a322615f9fe81178bc682dc33ccb to your computer and use it in GitHub Desktop.
@tweet2doom demo converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import struct | |
import sys | |
buffer = open(sys.argv[1], "rb") | |
header = buffer.read(13) | |
header_fmt_string = "ccccccccccccc" | |
header = struct.unpack(header_fmt_string, header) | |
def read_header(pos): | |
return int.from_bytes(header[pos], "big") | |
print("Doom version:", read_header(0)) | |
print("Difficulty:", read_header(1)) | |
print("Episode:", read_header(2)) | |
print("Map:",read_header(3)) | |
print("MpMode:", read_header(4)) | |
print("-respawn", read_header(5)) | |
print("-fast", read_header(6)) | |
print("-nomonsters", read_header(7)) | |
print("Player:", read_header(8)) | |
print("P1 Present", read_header(9)) | |
print("P2 Present", read_header(10)) | |
print("P3 Present", read_header(11)) | |
print("P4 Present", read_header(12)) | |
data = buffer.read() | |
data = data[:len(data)-1] # Remove last 0x80 byte, assuming that's what it is | |
tic_count = int(len(data)/4) | |
print("Tic count:", tic_count) | |
tics = struct.iter_unpack("bbbc", data) | |
tas_string = "" | |
a=open("log.txt", "w") | |
b=open("keys.txt", "w") | |
for tic in tics: | |
ymov = tic[0] | |
xmov = tic[1] | |
rmov = tic[2] | |
special = tic[3] | |
a.write(str(tic)) | |
if special[0] & 0b00000001: | |
a.write(" # Fired") | |
if special[0] & 0b00000010: | |
a.write(" # Door") | |
if special[0] & 0b00000100: | |
slot = (special[0] & 0b00111000) >> 3 | |
a.write(" # Switch slot " + str(slot)) | |
a.write("\n") | |
framekeys = "" | |
if ymov > 0 and ymov < 50: | |
framekeys += "r" | |
if ymov < 0 and ymov > -50: | |
framekeys += "l" | |
if xmov > 0 and xmov < 50: | |
framekeys += "u" | |
if xmov < 0 and xmov > -50: | |
framekeys += "d" | |
if ymov == 50: | |
framekeys += "R" | |
if ymov == -50: | |
framekeys += "L" | |
if xmov == 50: | |
framekeys += "U" | |
if xmov == -50: | |
framekeys += "D" | |
if special[0] & 0b00000001: # Fired | |
framekeys += "f" | |
if special[0] & 0b00000010: # Opened Door | |
framekeys += "p" | |
if special[0] & 0b00000100: # Switch slot | |
slot = (special[0] & 0b00111000) >> 3 | |
framekeys += str(slot) | |
b.write(framekeys) | |
b.write(",") | |
b.close() | |
b = open("keys.txt", "r") | |
# Optimization pass. If there are any issues, it's probably an off by one error here | |
keys = b.readline().split(",") | |
newkeys = "" | |
last_value = keys[0] | |
last_count = 1 | |
for key in keys[1:]: | |
if last_value != key or last_count == 99: | |
if last_count == 1: | |
newkeys += last_value | |
newkeys += "," | |
else: | |
newkeys += str(last_count) # You may need to subtract one from last_count here | |
newkeys += "-" | |
newkeys += last_value | |
newkeys += "," | |
last_count = 0 | |
last_value = key | |
last_count += 1 | |
open("keys-optimized.txt", "w").write(newkeys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment