All pastes #1058691 Raw Edit

ground.py

public python v1 · immutable
#1058691 ·published 2008-06-30 02:35 UTC
rendered paste body
#!/usr/bin/env pythonimport loggingfrom google.appengine.api import memcacheimport datamodeldef CreateAcre(acre, land):  for r in range(0, 7):    for c in range(0, 7):      newland = datamodel.Land()      newland.coords = acre + chr(ord('a') + r) + chr(ord('1') + c)      newland.landtype = land      newland.x, newland.y = CoordsToXY(newland.coords)      newland.put()def CreateWorld(world, land):  for r in range(0, 7):    for c in range(0, 7):      acre = world + chr(ord('a') + r) + chr(ord('1') + c)      CreateAcre(acre, land)def CreateContinent(continent):  for r in range(0, 7):    for c in range(0, 7):      world = continent + chr(ord('a') + r) + chr(ord('1') + c)            land = 1      if (r == 0 and c == 0) or (r == 0 and c == 6) or (r == 6 and c == 0) or (r == 6 and c == 6):        land = 2      CreateWorld(world, land)def GroundType(coords):  try:    land = datamodel.Land.all().filter('coords = ', coords).get()    return land.landtype  except:    return Nonedef GroundTile(coords):  tile = None  try:    tile = memcache.get(coords)  except:    pass  if tile is not None:    a = tile    overlay = a[0]    tooltip = a[1]    obj = a[2]      else:    overlay = ''    tooltip = ''    obj = ''    groundtype = GroundType(coords)    # base ground look    if groundtype == 1:      overlay = ''    elif groundtype == 2:      overlay = 'Sand01'    elif groundtype == 3:      overlay = 'Snow6'    elif groundtype == 4:      overlay = 'MarbleFix'    else:      overlay = 'Ocean'      obj = 'Ocean'    statue = datamodel.Statue.all().filter('coords =', coords).get()    if statue:      overlay = statue.image      tooltip = 'Placed by ' + statue.placedby.username      obj = 'Statue'    memcache.add(coords, [overlay, tooltip, obj], 600)      return overlay, tooltip, objdef CoordsToXY(coords):  YLet = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7}  Y1 = coords[0:1]  if Y1 == 'd':    Y2 = coords[2:3]    Y3 = coords[4:5]    Y4 = coords[6:7]    Y5 = coords[8:9]    Y1 = YLet[Y1]    Y2 = YLet[Y2]    Y3 = YLet[Y3]    Y4 = YLet[Y4]    Y5 = YLet[Y5]        X1 = int(coords[1:2])    X2 = int(coords[3:4])    X3 = int(coords[5:6])    X4 = int(coords[7:8])    X5 = int(coords[9:10])        YY = (Y1 * 2401) + (Y2 * 343) + (Y3 * 49) + (Y4 * 7) + Y5    XX = (X1 * 2401) + (X2 * 343) + (X3 * 49) + (X4 * 7) + X5          YY = 11204 - YY    XX = XX - 10861          return [XX, YY]def CoordsOffset(C, r, c):  ZR = C[0:1]  MR = C[2:3]  WR = C[4:5]  AR = C[6:7]  GR = C[8:9]    row = (ord(ZR)-ord('a'))*2401 + \        (ord(MR)-ord('a'))*343 + \	(ord(WR)-ord('a'))*49 + \	(ord(AR)-ord('a'))*7 + \	(ord(GR)-ord('a')) + r  ZC = int(C[1:2])  MC = int(C[3:4])  WC = int(C[5:6])  AC = int(C[7:8])  GC = int(C[9:10])    col = (ZC-1)*2401 + \	(MC-1)*343 + \	(WC-1)*49 + \	(AC-1)*7 + \	(GC-1) + c  if row < 0 or col < 0 or row >= 16807 or col >= 16807:    return ''  ZR = int(row / 2401)  row = row % 2401    MR = int(row / 343)  row = row % 343    WR = int(row / 49)  row = row % 49    AR = int(row / 7)  row = row % 7  ZC = int(col / 2401)  col = col % 2401    MC = int(col / 343)  col = col % 343    WC = int(col / 49)  col = col % 49    AC = int(col / 7)  col = col % 7  return chr(ZR +ord('a')) + chr(ZC +ord('1')) + \         chr(MR +ord('a')) + chr(MC +ord('1')) + \         chr(WR +ord('a')) + chr(WC +ord('1')) + \         chr(AR +ord('a')) + chr(AC +ord('1')) + \         chr(row+ord('a')) + chr(col+ord('1'))def CoordsArrayFromCenter(C):  a = range(0,49)    for r in range(0, 7):    for c in range(0, 7):      a[r*7 + c] = CoordsOffset(C, r-3, c-3)  return a