from hashlib import md5 from sorter import Sorter rockmodp = Sorter() #positions of rockmod rockmodv = [] #values of rockmod rockmodt = [] #rock type (to include portals) def modrock(dim, state, type='n'): #type is by default normal; p is portal if state and type == 'n': type = 'n' if dim in rockmodp: loc = rockmodp.index(dim) rockmodv[loc] = state rockmodt[loc] = type else: loc = rockmodp.insert(dim) rockmodv.insert(loc, state) rockmodt.insert(loc, type) def isrock(density, dim, typereq=False): #if typereq, returns if a rock or portal x, y = dim[0], dim[1] m = md5() m.update(intbyt(x)) m.update(intbyt(y)) rockpres = (bytint(m.digest()[:2])/(256**2) < density) if dim in rockmodp: loc = rockmodp.index(dim) rockpres = rockmodv[loc] if typereq: return (rockpres, rockmodt[loc]) if typereq: return (rockpres, 'n') return rockpres def intbyt(num): return int(num).to_bytes(8, 'big', signed=True) def bytint(byt): return int.from_bytes(byt, byteorder='big') def save(): outtext = '' for mvind in range(len(rockmodp)): outtext += str(rockmodp[mvind][0]) + ' ' + str(rockmodp[mvind][1]) + ' ' + str(rockmodv[mvind]) + ' ' + str(rockmodt[mvind]) + '\n' return outtext def load(data): global rockmodp, rockmodv, rockmodt rockmodp, rockmodv, rockmodt = Sorter(), [], [] for line in data: line = line.split(' ') ind = rockmodp.insert((int(line[0]), int(line[1]))) rockmodv.insert(ind, line[2] == 'True') rockmodt.insert(ind, line[3])