aboutsummaryrefslogtreecommitdiff
path: root/isrock.py
blob: 085b25b812850a5cdf867e62eccb6aceee468681 (plain)
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
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])