func (w *World) Block(bc coords.Block) mapgen.Block { chunk := w.chunks[bc.Chunk()] if chunk == nil { return mapgen.BLOCK_NIL } return chunk.Block(bc.Offset()) }
func (b *Box) inSolid(world mapgen.BlockSource) bool { blockCollide := func(x, y, z int) bool { blockCoord := coords.Block{x, y, z} if world.Block(blockCoord).Solid() { center := blockCoord.Center() blockBox := NewBox(center.Vec3(), vmath.Vec3{0.5, 0.5, 0.5}) return b.Collides(blockBox) } return false } f := func(n float64) int { return int(math.Floor(n)) } xs := f(b.xs) xe := f(b.xe) ys := f(b.ys) ye := f(b.ye) zs := f(b.zs) ze := f(b.ze) for x := xs; x <= xe; x++ { for y := ys; y <= ye; y++ { for z := zs; z <= ze; z++ { if blockCollide(x, y, z) { return true } } } } return false }
func (w *World) ChangeBlock(bc coords.Block, newBlock mapgen.Block) { chunk := w.Chunk(bc.Chunk()) oc := bc.Offset() block := chunk.Block(oc) chunk.SetBlock(oc, newBlock) w.blockListeners.FireEvent("BlockChanged", bc, block, newBlock) }
func (p *persister) BlockChanged(bc coords.Block, old mapgen.Block, new mapgen.Block) { cc := bc.Chunk() chunk, err := p.loadChunk(cc) if err != nil { log.Println("Applying block change: ", err) return } chunk.SetBlock(bc.Offset(), new) err = p.saveChunk(cc, chunk) if err != nil { log.Println("Applying block change: ", err) return } }
func (ct *CaveTest) block(bc coords.Block, height int) mapgen.Block { if bc.X == 0 && bc.Y == 16 && bc.Z == 0 { return mapgen.BLOCK_SPAWN } if bc.Y > height { return mapgen.BLOCK_AIR } // Calculate a cave xf, yf, zf := bc.Float64() ridge1 := ct.ridgedFilter.Filter(xf, yf, zf, ct.simplexNoise) ridge2 := ct.ridgedFilter.Filter(xf, yf, zf, ct.simplexNoise2) if ridge1 >= 0.95 { ridge1 = 1 } else { ridge1 = 0 } if ridge2 >= 0.95 { ridge2 = 1 } else { ridge2 = 0 } if ridge1*ridge2 == 1 { if DEBUG { return mapgen.BLOCK_STONE } return mapgen.BLOCK_AIR } if bc.Y == height { if DEBUG { return mapgen.BLOCK_GLASS } return mapgen.BLOCK_GRASS } if DEBUG { return mapgen.BLOCK_AIR } if bc.Y < height-3 { return mapgen.BLOCK_STONE } if height > bc.Y { return mapgen.BLOCK_DIRT } return mapgen.BLOCK_AIR }
// Applies the given block change to the currently queued chunks // Returns true if the change was applied (the block has not yet been // sent), false if it should be sent to the client instead (they already // have the chunk). // This is needed to avoid sending stale chunks to the client. func (cm *ChunkManager) ApplyBlockChange(bc coords.Block, b mapgen.Block) bool { cm.mutex.Lock() defer cm.mutex.Unlock() cc := bc.Chunk() status, exists := cm.chunks[cc] if !exists { // We don't techincally apply the block change to ourselves if the // chunk does not exist in our list, but we don't want it to be sent // to the client, so we return false regardless. return false } if status.sent { return false } if status.data == nil { log.Println("WARN: Recieved block change for chunk that is not loaded (this probably shouldn't happen?)") return false } oc := bc.Offset() status.data.SetBlock(oc, b) return true }