153 lines
3.5 KiB
Python
153 lines
3.5 KiB
Python
import sys
|
|
import os
|
|
import sys
|
|
import json
|
|
import curses
|
|
|
|
def fix(x):
|
|
return int(x*65536)
|
|
|
|
class windowDat:
|
|
s = 0
|
|
def __init__(self):
|
|
self.s = curses.initscr()
|
|
curses.noecho()
|
|
curses.cbreak()
|
|
self.s.keypad(True)
|
|
#little trick that allows us to not worry about closing
|
|
def __del__(self):
|
|
curses.nocbreak()
|
|
self.s.keypad(False)
|
|
curses.echo()
|
|
curses.endwin()
|
|
|
|
class raycastMap:
|
|
w = 0
|
|
h = 0
|
|
d = 0
|
|
#provide usable start values to avoid headscratching
|
|
startpos_x = 196608
|
|
startpos_y = 196608
|
|
startpos_z = 0
|
|
startdir_x = 0
|
|
startdir_y = 65536
|
|
startplane_x = 43253
|
|
startplane_y = 0
|
|
floor = []
|
|
wall = []
|
|
sprite_count = 0
|
|
sprites = []
|
|
|
|
def parseout(self):
|
|
out = json.JSONEncoder().encode({\
|
|
"w":self.w,\
|
|
"h":self.h,\
|
|
"d":self.d,\
|
|
"startpos_x":self.startpos_x,\
|
|
"startpos_y":self.startpos_y,\
|
|
"startpos_z":self.startpos_z,\
|
|
"startdir_x":self.startdir_x,\
|
|
"startdir_y":self.startdir_y,\
|
|
"startplane_x":self.startplane_x,\
|
|
"startplane_y":self.startplane_y,\
|
|
"floor":self.floor,\
|
|
"wall":self.wall,\
|
|
"sprite_count":self.sprite_count,\
|
|
"sprites":self.sprites\
|
|
})
|
|
|
|
return out
|
|
|
|
def __init__(self, w, h, d):
|
|
self.w = w
|
|
self.h = h
|
|
self.d = d
|
|
for i in range(0, w*h):
|
|
self.floor.append(0)
|
|
for i in range(0, w*h*d):
|
|
self.wall.append(0)
|
|
|
|
def helppage():
|
|
print("usage : python3 raycastedit.py [options] [file]")
|
|
print("If [file] exists and is in .rcmap format, il will be edited")
|
|
print("Otherwise, the editor will attempt to create [file]")
|
|
print("Options :")
|
|
print("-h : display this page")
|
|
print("-n W H D : Create a new empty map in [file], if it doesn't exist")
|
|
print(" already, of width W, height H and depth D")
|
|
|
|
def seehelppls():
|
|
print('See usage with "--help or "-h"')
|
|
|
|
if len(sys.argv) < 2:
|
|
print("No file provided !")
|
|
seehelppls()
|
|
os._exit(1)
|
|
|
|
def parsin(string):
|
|
data = json.load(string)
|
|
nmap = raycastMap(0, 0, 0)
|
|
nmap.w = data.w
|
|
nmap.h = data.h
|
|
nmap.d = data.d
|
|
nmap.startpos_x = data.startpos_x
|
|
nmap.startpos_y = data.startpos_y
|
|
nmap.startpos_z = data.startpos_z
|
|
nmap.startdir_x = data.startdir_x
|
|
nmap.startdir_y = data.startdir_y
|
|
nmap.startplane_x = data.startplane_x
|
|
nmap.startplane_y = data.startplane_y
|
|
|
|
return nmap
|
|
|
|
def parsefile(filename):
|
|
nmap = parsin(open(filename, "r"))
|
|
return nmap
|
|
|
|
def gen_ne(filename, w, h, d):
|
|
try:
|
|
nfile = open(filename, mode="x+t", buffering=1, encoding="utf-8", newline="\n")
|
|
except:
|
|
print("Error creating file !")
|
|
return 1
|
|
nmap = raycastMap(w, h ,d)
|
|
nfile.write(nmap.parseout())
|
|
nfile.write("\n")
|
|
|
|
if len(sys.argv) > 2:
|
|
for i in range(1, len(sys.argv)-1):
|
|
option_set = -1
|
|
if sys.argv[i] == "-h":
|
|
helppage()
|
|
os._exit(0)
|
|
if sys.argv[i] == "-n":
|
|
i+=3
|
|
try:
|
|
filename = sys.argv[-1]
|
|
w = int(sys.argv[i-2])
|
|
h = int(sys.argv[i-1])
|
|
d = int(sys.argv[i])
|
|
except:
|
|
print("Invalid use of option -n !")
|
|
seehelppls()
|
|
os._exit(1)
|
|
gen_ne(filename, w, h, d)
|
|
os._exit(0)
|
|
if option_set == -1:
|
|
print('Invalid option "'+sys.argv[i]+'" !')
|
|
seehelppls()
|
|
os._exit(1)
|
|
|
|
myscr = windowDat()
|
|
|
|
openmap = parsefile(sys.argv[-1])
|
|
|
|
while True:
|
|
myscr.s.clear()
|
|
|
|
myscr.s.addstr(0, 0, openmap)
|
|
print(openmap)
|
|
|
|
myscr.s.refresh()
|
|
sleep(10)
|