def generate(self,generator,positionX,positionY): debug.debugMessage(3," Generating chunk...") rmatrix=[0,0,0,0] #for z in range(0,2): # if z==0: # a=generator.big_feature # a=generator.small_feature # for x in range(0,32): # for y in range(0,32): #print "blah" for x in range(0,32): self.mapChunk+=[[]] self.tileVariation+=[[]] for y in range(0,32): tilevar=random.randint(0,4) tile=0 nx1 = (x+positionX*32) / float(generator.b) ny1 = (y+positionY*32) / float(generator.b) nx2 = (x+positionX*32) / float(generator.s) ny2 = (y+positionY*32) / float(generator.s) nx3 = (x+positionX*32) / float(generator.l) ny3 = (y+positionY*32) / float(generator.l) v1=generator.big_feature.value_at( (nx1,ny1) ) v2=generator.small_feature.value_at( (nx2,ny2) ) v3=generator.lakes.value_at( (nx3,ny3) ) #Actual generation code if v3 < -0.4: if(v2 < 0.32 ): tile=3 else: tile=1 elif 0.45 > v3 > 0.38: if(v2 < 0.32 ): tile=4 tilevar=random.randint(2,4) else: tile=1 elif v3 > 0.45: if(v2 < 0.32 ): tilevar=random.randint(0,2) tile=4 else: tile=1 else: if( v1 < -0.15 ): #open air if( v2 > 0.4 ): tile=2 else: tile=1 else: if( v2 < -0.1 ): tile=1 else: tile=2 #Actual generation code end self.mapChunk[x]+=[tile] self.tileVariation[x]+=[tilevar] debug.debugMessage(4," Chunk generated("+str(positionX)+","+str(positionY)+")")
def __init__(self,parent,big,small,climate,lake): self.chunkCollection={} self.objCache={} self.parent=parent self.generator=[] debug.debugMessage(3," Initializing world map...") self.chunkCollection={} self.generator=generator.GENERATOR(big,small,climate,lake) debug.debugMessage(3," World map initialized")
def __init__(self,datapath,filename): self.objectDefinition = [] #self.graphics = "" self.scripts = {} #self.scripts['move'] = "def execute(self): return 1" #exec(objectset.scripts[object[i].props.scripts['move']]+chr(10)+execute(props)) debug.debugMessage(5," Loading object data from file...") self.fromFile(datapath,filename) debug.debugMessage(4," Object data loaded succesfully!")
def requestChunkImage(self,tileset): if self.cachedimage == "NULL": self.cachedimage=pygame.Surface((1024,1024)) for x in range(0,32): for y in range(0,32): pos=(x*32,y*32) clip=tileset.tileDefinition[self.mapChunk[x][y]].images[self.tileVariation[x][y]] self.cachedimage.blit(tileset.tileset,pos,clip) debug.debugMessage(3," Drawn a new chunk!") #genedchunks+=1 return self.cachedimage
def main(self): debug.debugMessage(5," Running main loop...") while(self.run): self.clock.tick(self.maxfps) self.players[0].camPosition=(self.objects[0].position[0],self.objects[0].position[1]) self.checkEvents() #if self.mapregen==1: self.mapregen=0; self.gamemap=map.GLOBALMAP(self,72,32,48,16) self.keyinput.keyInputRepeat(self) for p in self.players: p.update() self.screen.blit(self.viewport.requestVisuals(),(0,0)) #self.gamemap.drawMap(self.screen,self.tileset,self.players[0],self.objects, self.objectset) #self.screen.blit(self.tileset.tileset,(0,0),self.tileset.tileDefinition[1].images[0]) pygame.display.flip() debug.debugMessage(3, " Main loop off.")
def __init__(self,big,small,climate,lake): self.s=small #Small scale feautures - tunnels, pillars, etc. self.b=big #Big scale features - large caves, layers of solid rock self.l=lake #Lakes - used for generation of lakes and - sometimes - misc stuff self.c=climate #Climate - Large scale noise, used for determining climate type of certain point. self.s_scale=1 self.b_scale=2 self.l_scale=1 self.c_scale=4 debug.debugMessage(3," Initializing generator...") self.big_feature =perlin.PerlinNoise((big,big),perlin.ease_interpolation) self.small_feature =perlin.PerlinNoise((small,small),perlin.ease_interpolation) self.climate =perlin.PerlinNoise((climate,climate),perlin.ease_interpolation) self.lakes =perlin.PerlinNoise((lake,lake),perlin.ease_interpolation) debug.debugMessage(4," Generator initialized!")
def uncacheChunks(self,position): a = self.parent.settings.chunkCacheRange b=0 for x in range(position[0]-a,position[0]+a): b+=self.requestChunkDeletion((x , -a + position[1])) b+=self.requestChunkDeletion((x , a + position[1])) for y in range(position[1]-a+1,position[1]+a-1): b+=self.requestChunkDeletion((-a + position[0] , y)) b+=self.requestChunkDeletion((a + position[0] , y)) a2 = self.parent.settings.imageCacheRange b2=0 for x in range(position[0]-a2,position[0]+a2): b2+=self.requestChunkImageDeletion((x , -a2 + position[1])) b2+=self.requestChunkImageDeletion((x , a2 + position[1])) for y in range(position[1]-a2+1,position[1]+a-1): b2+=self.requestChunkImageDeletion((-a2 + position[0] , y)) b2+=self.requestChunkImageDeletion((a2 + position[0] , y)) if(b>0 or b2>0): debug.debugMessage(5, "Deleted " + str(b) +" chunks and " +str(b+b2) + " cached images!") return 1
def loadScripts(self,datapath,filename): if not os.path.exists(os.path.join(datapath, filename)): debug.debugMessage(0," Object script list file not located!") FILE=open(os.path.join(datapath, filename),"r") full=FILE.read()+chr(10) FILE.close() mode=0 script="" cchar='' current='' for cchar in full: if mode==0: if cchar=="=": script=current current='' mode=1 else: current+=cchar else: if cchar==chr(10): value=current debug.debugMessage(3,"Loading script "+script+" from: "+os.path.join(datapath, "scripts", value)) if not os.path.exists(os.path.join(datapath, "scripts", value)): debug.debugMessage(1," Script '"+ script + "' file not located!") FILE2=open(os.path.join(datapath, "scripts", value),"r") self.scripts[script]=FILE2.read() FILE2.close() mode=0 current='' else: current+=cchar
def load_image(datapath,name): fullname = os.path.join(datapath, name) debug.debugMessage(5, ' Trying to load image file: '+fullname) try: image = pygame.image.load(fullname) debug.debugMessage(4, ' Image loaded succesfully!') except pygame.error, message: debug.debugMessage(1, 'Cannot load image: ' + fullname) raise SystemExit, message
def __init__(self): self.mapregen=0 self.maxpfs=60 self.fullscreen=0 self.gameversion="" self.run = 0 debug.debugMessage(5,"Starting the game") pygame.init() pygame.mouse.set_visible(0) self.run=1 debug.debugMessage(3," Game started") self.clock = pygame.time.Clock() self.settings = SETTINGS() f=open(os.path.join(datapath, "config.txt")) ff=f.read() f.close() try: exec(ff) except: debug.debugMessage(1,"Something is wrong with config file!") if self.fullscreen==1: self.screen = pygame.display.set_mode(self.resolution,pygame.FULLSCREEN) else: self.screen = pygame.display.set_mode(self.resolution) self.keyinput = keyinput.KEYMAP() self.keyinput.fromFile(datapath,"keymap") self.players = [PLAYER(self)] self.players[0].screenSize=(640,480) self.objectset= objects.OBJECTSET(datapath,"objectset") self.objects = [] self.objects += [objects.OBJECT(self,1)] self.players[0].camFocus=self.objects[0] self.viewport = gui.VIEWPORT((640,480),self,{'player':self.players[0],'datapath':datapath}) self.tileset = tileset.TILESET() self.gamemap = map.GLOBALMAP(self,72,8,64,24) self.tileset.fromFile(datapath,"tileset") pygame.display.set_caption("Usumora["+self.gameversion+"]")
def __init__(self): debug.debugMessage(3, "Creating tileset object...") self.tileset = [] self.tileDefinition = [] debug.debugMessage(4, " Tileset created!")
def execAction(self,action,props): if not self.world.objectset.objectDefinition[self.type].props.scripts.has_key(action): debug.debugMessage(2,"Object does not have script for action: '"+action+"'") else: a= "props="+str(props)+chr(10)+self.world.objectset.scripts[self.world.objectset.objectDefinition[self.type].props.scripts[action]] exec(a)
def imageFromFile(self,datapath,filename): debug.debugMessage(5," Opening objects image file.") self.graphics = utilities.load_image(datapath,filename)
def fromFile(self,datapath,filename): debug.debugMessage(5," Loading object set from file.") if not os.path.exists(os.path.join(datapath, filename)): debug.debugMessage(0," Objectset file not located") FILE=open(os.path.join(datapath, filename),"r") full=FILE.read() cchar='' current='' mode=-2 curObject=0 prop="" numbers =['0','1','2','3','4','5','6','7','8','9'] letters =[' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] funcchars=['#','=','-',':','+',';'] endl =[chr(10)] for i in range(0,len(full)): cchar=full[i] if cchar in numbers: a=1 elif cchar in letters: a=2 elif cchar in funcchars: a=3 elif cchar in endl: a=4 else: a=0 if mode == -2: if a==4: self.graphics=utilities.load_image(datapath,current) mode=-1 current='' else: current+=cchar elif mode == -1: if a==4: debug.debugMessage(5,"Loading object action scripts") self.loadScripts(datapath,current) mode=0 current='' else: current+=cchar elif mode == 0: #This reads number of object, like "#134#" if a==1: current+=cchar elif a==4: curObject=int(current) while(len(self.objectDefinition)<curObject+1): self.objectDefinition+= [OBJECTDEF()] debug.debugMessage(3,"Loading tile "+current) mode=1 current='' elif mode == 1: #This reads name of the property, like: "name=" if a == 1 or a == 2: current+=cchar elif a==3: if cchar == '-' or cchar == '+': current+=cchar elif cchar == '#': current='' cchar='' mode=0 else: prop=current.lower() if prop in ["name","corpse","image"]: mode = 2 else: mode = 3 current='' elif mode==2: #This reads string value for the property and assigns it if a == 1 or a == 2 or a == 3 or a == 0: current+=cchar elif a == 4: if prop == "name": self.objectDefinition[curObject].name=current elif prop == "corpse": objectset.props.corpse=current elif prop == "image": b=[] curpr="" for o in range(0,len(current)): if current[o] == ',' or current[o] == ';': b+=[int(curpr)] curpr='' else: curpr+=current[o] self.objectDefinition[curObject].image=pygame.Rect( b[0], b[1], b[2], b[3]) #print prop,"=",current current='' mode=1 elif mode == 3: #This reads integer(s) value for the property and assigns it if a == 1 or a == 2: current+=cchar elif a == 4: if prop == "weight": self.objectDefinition[curObject].props.weight = int(current) elif prop == "speed": self.objectDefinition[curObject].props.speed = int(current) elif prop == "terrainattackmin": self.objectDefinition[curObject].props.terrainAttack[0] = int(current) elif prop == "terrainattackmax": self.objectDefinition[curObject].props.terrainAttack[1] = int(current) elif prop == "maxhp": self.objectDefinition[curObject].props.maxhp = int(current) elif prop == "magicemit": self.objectDefinition[curObject].props.magic_emit = int(current) elif prop == "movemode": self.objectDefinition[curObject].props.moveMode = int(current) elif prop == "movemode2": self.objectDefinition[curObject].props.moveMode2 = int(current) elif prop[0:5] == "sprop": self.objectDefinition[curObject].props.sprops[prop[5:len(prop)]] = int(current) elif prop[0:6] == "script": self.objectDefinition[curObject].props.scripts[prop[6:len(prop)]] = current current='' mode=1
def fromFile(self,datapath, filename): debug.debugMessage(3,"Trying to load keymap from file: "+os.path.join(datapath,filename)) if not os.path.exists(os.path.join(datapath, filename)): debug.debugMessage(0," Keymap file " + str(os.path.join(datapath, filename)) + " does not exist!") FILE=open(os.path.join(datapath, filename),"r") full=FILE.read() FILE.close() legitchars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9',':'] funcchars = [',','.','=','-'] endl = [chr(10),';'] key = 0 mod = 0 action = [] mode = 0 current='' norepeat = 0 for i in range(0,len(full)): cchar=full[i] if cchar == '#': mode=-1 if mode == -1: #Comment rea... ignore mode :> if cchar in endl: mode=0; current='' elif mode == 0: #key read mode if cchar in funcchars: mode=1 key=int(current) current='' elif cchar in legitchars: current+=cchar elif mode == 1: #Modifier key read mode if cchar in funcchars: mod=int(current) if mod & 4096: mod-=4096 if self.monomodkeys==1: if mod & 2: mod-=1 if mod & 128: mod-=64 if mod & 16384: mod-=16128 current='' if cchar==',': mode = 2 else: mode = 3 elif cchar in legitchars: current+=cchar elif mode == 2: if cchar in funcchars: mode = 3 current='' norepeat=1 elif cchar in legitchars: current+=cchar elif mode == 3: if cchar in endl: action+=[current] #print str(key)+","+str(mod)+"="+str(action) self.bindKey(key,mod,action, norepeat) action=[] mod=0 norepeat=0 key=0 current='' mode=0 elif cchar in funcchars: action+=[current] current='' elif cchar in legitchars: current+=cchar
def fromFile(self, datapath, filename): debug.debugMessage(3, " Trying to load tileset from file: " + os.path.join(datapath, filename)) if not os.path.exists(os.path.join(datapath, filename)): debug.debugMessage(0, " Tileset file not located") FILE = open(os.path.join(datapath, filename), "r") full = FILE.read() cchar = "" current = "" mode = -1 curTile = 0 prop = "" numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] letters = [ " ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ] funcchars = ["#", "=", "-", ":", "+", ";"] endl = [chr(10)] for i in range(0, len(full)): cchar = full[i] if cchar in numbers: a = 1 elif cchar in letters: a = 2 elif cchar in funcchars: a = 3 elif cchar in endl: a = 4 else: a = 0 if mode == -1: if a == 4: self.tileset = utilities.load_image(datapath, current) mode = 0 current = "" else: current += cchar elif mode == 0: # This reads number of tile, like "#133#" if a == 1: current += cchar elif a == 4: curTile = int(current) while len(self.tileDefinition) < curTile + 1: self.tileDefinition += [TILE()] debug.debugMessage(3, "Loading tile " + current) mode = 1 current = "" elif mode == 1: # This reads name of the property, like: "name=" if a == 1 or a == 2: current += cchar elif a == 3: if cchar == "-" or cchar == "+": current += cchar elif cchar == "#": current = "" cchar = "" mode = 0 else: prop = current if prop in ["name", "remains"] or prop[0:5] == "image": mode = 2 else: mode = 3 current = "" elif mode == 2: # This reads string value for the property and assigns it if a == 1 or a == 2 or a == 3 or a == 0: current += cchar elif a == 4: if prop == "name": self.tileDefinition[curTile].name = current elif prop == "remains": self.tileDefinition[curTile].remains = current elif prop[0:5] == "image": b = [] curpr = "" for o in range(0, len(current)): if current[o] == "," or current[o] == ";": b += [int(curpr)] curpr = "" else: curpr += current[o] for i2 in range(int(prop[5]) - 1, int(prop[7]) - 1): if prop[6] == "-": self.tileDefinition[curTile].images[i2] = pygame.Rect((b[0]) * 32, b[1] * 32, 32, 32) elif prop[6] == "+": self.tileDefinition[curTile].images[i2] = pygame.Rect( (b[0] + i2) * 32, b[1] * 32, 32, 32 ) current = "" mode = 1 elif mode == 3: # This reads integer(s) value for the property and assigns it if a == 1: current += cchar elif a == 4: if prop == "flammable": self.tileDefinition[curTile].flammable = int(current) elif prop == "strengthdec": self.tileDefinition[curTile].strengthdec = int(current) elif prop == "strength": self.tileDefinition[curTile].strength = int(current) elif prop == "lemitR": self.tileDefinition[curTile].lemit[0] = int(current) elif prop == "lemitG": self.tileDefinition[curTile].lemit[1] = int(current) elif prop == "lemitB": self.tileDefinition[curTile].lemit[2] = int(current) elif prop == "transparency": self.tileDefinition[curTile].transparency = int(current) elif prop == "magic": self.tileDefinition[curTile].magic = int(current) elif prop == "breaks": self.tileDefinition[curTile].breaksInto = int(current) elif prop == "collision": self.tileDefinition[curTile].collision = int(current) current = "" mode = 1 FILE.close() for i in range(0, 0): # self.tileDefinition: print "flammable", i.flammable print "strengthdec", i.strengthdec print "strength", i.strength print "lemitR", i.lemit[0] print "lemitG", i.lemit[1] print "lemitB", i.lemit[2] print "transparency", i.transparency print "magic", i.magic print "breaks", i.breaksInto print "collision", i.collision
pygame.display.flip() debug.debugMessage(3, " Main loop off.") def checkEvents(self): for event in pygame.event.get(): if event.type == QUIT: return elif event.type == KEYDOWN: #print event.key,pygame.key.get_mods() self.keyinput.keyInput ( event.key, pygame.key.get_mods() , self ) self.keyinput.keyInputActivate ( event.key, pygame.key.get_mods() ) elif event.type == KEYUP: self.keyinput.keyInputDeactivate( event.key, pygame.key.get_mods() ) elif event.type == MOUSEBUTTONDOWN: continue elif event.type == MOUSEBUTTONUP: continue def receiveInput(self,action): #print "Received following input: "+str(action) if (action[0] == 'game'): if action[1]=='exit': self.run=0 elif action[1]=='world': if action[2]=='regen': self.gamemap=map.GLOBALMAP(self,72,32,48,16) else: self.viewport.getInput(action) #elif (action[0] == 'object'): self.viewport.getInput(action) if __name__ == '__main__': game=GAME() game.main() debug.debugMessage(5,"Game ended") #, generated " +str(genedchunks) +" in total