def test_save_chunk_to_tag(self): chunk = Chunk(1, 2) tag = self.s._save_chunk_to_tag(chunk) self.assertTrue("xPos" in tag["Level"]) self.assertTrue("zPos" in tag["Level"]) self.assertEqual(tag["Level"]["xPos"].value, 1) self.assertEqual(tag["Level"]["zPos"].value, 2)
def load_chunk(self, x, z): """ Retrieve a ``Chunk`` synchronously. This method does lots of automatic caching of chunks to ensure that disk I/O is kept to a minimum. """ if (x, z) in self.chunk_cache: return self.chunk_cache[x, z] elif (x, z) in self.dirty_chunk_cache: return self.dirty_chunk_cache[x, z] chunk = Chunk(x, z) self.serializer.load_chunk(chunk) if chunk.populated: self.chunk_cache[x, z] = chunk else: self.populate_chunk(chunk) chunk.populated = True chunk.dirty = True self.dirty_chunk_cache[x, z] = chunk self.postprocess_chunk(chunk) return chunk
def test_load_chunk_first(self): """ Loading a non-existent chunk raises an SRE. """ self.assertRaises(SerializerReadException, self.s.load_chunk, Chunk(0, 0))
def setUp(self): self.f = FallablesMockFactory() self.p = retrieve_plugins(IDigHook, parameters={"factory": self.f}) if "alpha_sand_gravel" not in self.p: raise unittest.SkipTest("Plugin not present") self.hook = self.p["alpha_sand_gravel"] self.c = Chunk(0, 0)
def empty_chunk(): before = time.time() for i in range(10): Chunk(i, i) after = time.time() return after - before
def repeated_seeds(p): before = time.time() for i in range(10): chunk = Chunk(i, i) p.populate(chunk, 0) after = time.time() return after - before
def sequential_seeded(p): before = time.time() for i in range(10): chunk = Chunk(i, i) p.populate(chunk, i) after = time.time() return after - before
def test_ascend_one_up(self): """ ``ascend()`` moves players upwards. """ self.p.location.pos = self.p.location.pos._replace(y=16) c = Chunk(0, 0) c.set_block((0, 0, 0), 1) c.set_block((0, 1, 0), 1) self.p.chunks[0, 0] = c self.p.ascend(1) self.assertEqual(self.p.location.pos.y, 32)
def test_ascend_zero(self): """ ``ascend()`` can take a count of zero to ensure that the client is standing on solid ground. """ self.p.location.pos = self.p.location.pos._replace(y=16) c = Chunk(0, 0) c.set_block((0, 0, 0), 1) self.p.chunks[0, 0] = c self.p.ascend(0) self.assertEqual(self.p.location.pos.y, 16)
def test_ascend_zero_up(self): """ Even with a zero count, ``ascend()`` will move the player to the correct elevation. """ self.p.location.pos = self.p.location.pos._replace(y=16) c = Chunk(0, 0) c.set_block((0, 0, 0), 1) c.set_block((0, 1, 0), 1) self.p.chunks[0, 0] = c self.p.ascend(0) self.assertEqual(self.p.location.pos.y, 32)
def pipeline(): generators = configuration.getlist("bravo", "generators") generators = retrieve_named_plugins(ITerrainGenerator, generators) before = time.time() for i in range(10): chunk = Chunk(i, i) for generator in generators: generator.populate(chunk, 0) after = time.time() return after - before
def pipeline(): generators = beta["generators"] generators = retrieve_named_plugins(ITerrainGenerator, generators) before = time.time() for i in range(10): chunk = Chunk(i, i) for generator in generators: generator.populate(chunk, 0) after = time.time() return after - before
def load_chunk(self, x, z): name = name_for_anvil(x, z) fp = self.folder.child("region").child(name) region = Region(fp) chunk = Chunk(x, z) try: data = region.get_chunk(x, z) tag = NBTFile(buffer=StringIO(data)) self._load_chunk_from_tag(chunk, tag) except MissingChunk: raise SerializerReadException("No chunk %r in region" % chunk) except Exception, e: raise SerializerReadException("%r couldn't be loaded: %s" % (chunk, e))
def make_chunk(self, x, z, seed, generators): """ Create a chunk using the given parameters. """ generators = retrieve_sorted_plugins(ITerrainGenerator, generators) chunk = Chunk(x, z) for stage in generators: stage.populate(chunk, seed) return { "blocks": chunk.blocks.tostring(), "metadata": chunk.metadata.tostring(), "skylight": chunk.skylight.tostring(), "blocklight": chunk.blocklight.tostring(), "heightmap": chunk.heightmap.tostring(), }
def setUp(self): self.f = FallablesMockFactory() self.p = retrieve_plugins(IDigHook, factory=self.f) self.hook = self.p["alpha_sand_gravel"] self.c = Chunk(0, 0)
def repeated_seeds(i, p): chunk = Chunk(i, i) p.populate(chunk, 0)
def request_chunk(self, x, z): """ Request a ``Chunk`` to be delivered later. :returns: Deferred that will be called with the Chunk """ if not async: return deferLater(reactor, 0.000001, self.factory.world.load_chunk, x, z) if (x, z) in self.chunk_cache: return succeed(self.chunk_cache[x, z]) elif (x, z) in self.dirty_chunk_cache: return succeed(self.dirty_chunk_cache[x, z]) elif (x, z) in self._pending_chunks: # Rig up another Deferred and wrap it up in a to-go box. return fork_deferred(self._pending_chunks[x, z]) chunk = Chunk(x, z) self.serializer.load_chunk(chunk) if chunk.populated: self.chunk_cache[x, z] = chunk return succeed(chunk) d = deferToAMPProcess(MakeChunk, x=x, z=z, seed=self.seed, generators=configuration.getlist( "bravo", "generators")) self._pending_chunks[x, z] = d def pp(kwargs): chunk.blocks = fromstring(kwargs["blocks"], dtype=uint8).reshape(chunk.blocks.shape) chunk.heightmap = fromstring(kwargs["heightmap"], dtype=uint8).reshape( chunk.heightmap.shape) chunk.metadata = fromstring( kwargs["metadata"], dtype=uint8).reshape(chunk.metadata.shape) chunk.skylight = fromstring( kwargs["skylight"], dtype=uint8).reshape(chunk.skylight.shape) chunk.blocklight = fromstring(kwargs["blocklight"], dtype=uint8).reshape( chunk.blocklight.shape) chunk.populated = True chunk.dirty = True self.postprocess_chunk(chunk) self.dirty_chunk_cache[x, z] = chunk del self._pending_chunks[x, z] return chunk # Set up callbacks. d.addCallback(pp) # Multiple people might be subscribed to this pending callback. We're # going to keep it for ourselves and fork off another Deferred for our # caller. return fork_deferred(d)
def sequential_seeded(i, p): chunk = Chunk(i, i) p.populate(chunk, i)
def empty_chunk(i): Chunk(i, i)
def request_chunk(self, x, z): """ Request a ``Chunk`` to be delivered later. :returns: ``Deferred`` that will be called with the ``Chunk`` """ if (x, z) in self.chunk_cache: returnValue(self.chunk_cache[x, z]) elif (x, z) in self.dirty_chunk_cache: returnValue(self.dirty_chunk_cache[x, z]) elif (x, z) in self._pending_chunks: # Rig up another Deferred and wrap it up in a to-go box. retval = yield self._pending_chunks[x, z].deferred() returnValue(retval) try: chunk = yield maybeDeferred(self.serializer.load_chunk, x, z) except SerializerReadException: # Looks like the chunk wasn't already on disk. Guess we're gonna # need to keep going. chunk = Chunk(x, z) if chunk.populated: self.chunk_cache[x, z] = chunk self.postprocess_chunk(chunk) if self.factory: self.factory.scan_chunk(chunk) returnValue(chunk) if self. async: from ampoule import deferToAMPProcess from bravo.remote import MakeChunk generators = [plugin.name for plugin in self.pipeline] d = deferToAMPProcess(MakeChunk, x=x, z=z, seed=self.level.seed, generators=generators) # Get chunk data into our chunk object. def fill_chunk(kwargs): chunk.blocks = array("B") chunk.blocks.fromstring(kwargs["blocks"]) chunk.heightmap = array("B") chunk.heightmap.fromstring(kwargs["heightmap"]) chunk.metadata = array("B") chunk.metadata.fromstring(kwargs["metadata"]) chunk.skylight = array("B") chunk.skylight.fromstring(kwargs["skylight"]) chunk.blocklight = array("B") chunk.blocklight.fromstring(kwargs["blocklight"]) return chunk d.addCallback(fill_chunk) else: # Populate the chunk the slow way. :c for stage in self.pipeline: stage.populate(chunk, self.level.seed) chunk.regenerate() d = succeed(chunk) # Set up our event and generate our return-value Deferred. It has to # be done early becaues PendingEvents only fire exactly once and it # might fire immediately in certain cases. pe = PendingEvent() # This one is for our return value. retval = pe.deferred() # This one is for scanning the chunk for automatons. if self.factory: pe.deferred().addCallback(self.factory.scan_chunk) self._pending_chunks[x, z] = pe def pp(chunk): chunk.populated = True chunk.dirty = True self.postprocess_chunk(chunk) self.dirty_chunk_cache[x, z] = chunk del self._pending_chunks[x, z] return chunk # Set up callbacks. d.addCallback(pp) d.chainDeferred(pe) # Because multiple people might be attached to this callback, we're # going to do something magical here. We will yield a forked version # of our Deferred. This means that we will wait right here, for a # long, long time, before actually returning with the chunk, *but*, # when we actually finish, we'll be ready to return the chunk # immediately. Our caller cannot possibly care because they only see a # Deferred either way. retval = yield retval returnValue(retval)
def setUp(self): self.chunk = Chunk(0, 0) self.p = bravo.plugin.retrieve_plugins(bravo.ibravo.ITerrainGenerator)
def setUp(self): self.hook = Winter() self.c = Chunk(0, 0)
def setUp(self): self.c = Chunk(0, 0)
def request_chunk(self, x, z): """ Request a ``Chunk`` to be delivered later. :returns: ``Deferred`` that will be called with the ``Chunk`` """ if (x, z) in self.chunk_cache: returnValue(self.chunk_cache[x, z]) elif (x, z) in self.dirty_chunk_cache: returnValue(self.dirty_chunk_cache[x, z]) elif (x, z) in self._pending_chunks: # Rig up another Deferred and wrap it up in a to-go box. retval = yield fork_deferred(self._pending_chunks[x, z]) returnValue(retval) chunk = Chunk(x, z) yield maybeDeferred(self.serializer.load_chunk, chunk) if chunk.populated: self.chunk_cache[x, z] = chunk self.postprocess_chunk(chunk) returnValue(chunk) if self. async: from ampoule import deferToAMPProcess from bravo.remote import MakeChunk d = deferToAMPProcess(MakeChunk, x=x, z=z, seed=self.seed, generators=configuration.getlist( self.config_name, "generators")) self._pending_chunks[x, z] = d # Get chunk data into our chunk object. def fill_chunk(kwargs): chunk.blocks = fromstring( kwargs["blocks"], dtype=uint8).reshape(chunk.blocks.shape) chunk.heightmap = fromstring(kwargs["heightmap"], dtype=uint8).reshape( chunk.heightmap.shape) chunk.metadata = fromstring(kwargs["metadata"], dtype=uint8).reshape( chunk.metadata.shape) chunk.skylight = fromstring(kwargs["skylight"], dtype=uint8).reshape( chunk.skylight.shape) chunk.blocklight = fromstring(kwargs["blocklight"], dtype=uint8).reshape( chunk.blocklight.shape) return chunk d.addCallback(fill_chunk) else: self.populate_chunk(chunk) d = succeed(chunk) self._pending_chunks[x, z] = d def pp(chunk): chunk.populated = True chunk.dirty = True self.postprocess_chunk(chunk) self.dirty_chunk_cache[x, z] = chunk del self._pending_chunks[x, z] return chunk # Set up callbacks. d.addCallback(pp) # Multiple people might be subscribed to this pending callback. We're # going to keep it for ourselves and fork off another Deferred for our # caller. retval = yield fork_deferred(d) returnValue(retval)