private static bool LoadSoundInfos(ChunkReader chunkIO, ChunkId idOuter, Wad2 wad, ref Dictionary <long, WadSoundInfo> outSoundInfos, Dictionary <long, WadSample> samples) { if (idOuter != Wad2Chunks.SoundInfos) { return(false); } var soundInfos = new Dictionary <long, WadSoundInfo>(); chunkIO.ReadChunks((id, chunkSize) => { if (id != Wad2Chunks.SoundInfo) { return(false); } WadSoundInfo soundInfo; long index; LoadSoundInfo(chunkIO, wad, samples, out soundInfo, out index); soundInfos.Add(index, soundInfo); return(true); }); outSoundInfos = soundInfos; return(true); }
/// <summary>Convert a chunk identifier to a string.</summary> /// <param name="id"></param> /// <returns></returns> public static string IdToString(ChunkId id) { if (!typeof(ChunkId).IsEnumDefined(id)) { return(string.Format("{0:X4}h", (int)id)); } return(string.Format("{0}/{1:X4}h", id, (int)id)); }
public override void Write(BinaryWriter writer) { writer.Write(CueId); writer.Write(PlayOrder); writer.Write(ChunkId.ToUInt32()); writer.Write(ChunkStart); writer.Write(BlockStart); writer.Write(SampleOffset); }
/// <summary>Ensure that this chunk has the required id, or report a load error.</summary> /// <param name="requiredId"></param> /// <returns></returns> public bool RequireId(ChunkId requiredId) { if (requiredId == Id) { return(true); } Loader.Errors.Add(new AssetLoadError.InvalidData(Loader, Offset, IdToString(requiredId), IdToString(Id))); return(false); }
public Chunk(ChunkId id, GameObject chunkGameObject) { this.id = id; this.chunkGameObject = chunkGameObject; chunkGameObject.name = $"Chunk({id.id.x}, {id.id.y}, {id.id.z})"; this.meshFilter = chunkGameObject.GetComponent <MeshFilter>(); this.meshCollider = chunkGameObject.GetComponent <MeshCollider>(); this.workState = new WorkState(); }
private static bool LoadSprites(ChunkReader chunkIO, ChunkId idOuter, Wad2 wad, ref Dictionary <long, WadSprite> outSprites) { if (idOuter != Wad2Chunks.Sprites) { return(false); } var sprites = new Dictionary <long, WadSprite>(); long obsoleteIndex = 0; // Move this into each chunk once we got rid of old style *.wad2 files. chunkIO.ReadChunks((id, chunkSize) => { if (id != Wad2Chunks.Sprite) { return(false); } int width = LEB128.ReadInt(chunkIO.Raw); int height = LEB128.ReadInt(chunkIO.Raw); byte[] imageData = null; RectangleInt2 rect = new RectangleInt2(); chunkIO.ReadChunks((id2, chunkSize2) => { if (id2 == Wad2Chunks.SpriteIndex) { obsoleteIndex = chunkIO.ReadChunkLong(chunkSize2); } else if (id2 == Wad2Chunks.SpriteData) { imageData = chunkIO.ReadChunkArrayOfBytes(chunkSize2); } else if (id2 == Wad2Chunks.SpriteSides) { rect.X0 = chunkIO.Raw.ReadInt32(); rect.Y0 = chunkIO.Raw.ReadInt32(); rect.X1 = chunkIO.Raw.ReadInt32(); rect.Y1 = chunkIO.Raw.ReadInt32(); } else { return(false); } return(true); }); sprites.Add(obsoleteIndex++, new WadSprite { Texture = new WadTexture(ImageC.FromByteArray(imageData, width, height)), Alignment = rect }) ; return(true); }); outSprites = sprites; return(true); }
/// <summary>Read the chunk header.</summary> /// <param name="loader"></param> public Chunk(AssetLoader loader) { var reader = loader.Reader; Loader = loader; Offset = reader.BaseStream.Position; Id = (ChunkId)reader.ReadUInt16(); Length = reader.ReadInt32(); }
public static void ProcessFile(string path) { byte[] data = File.ReadAllBytes(path); string extension = string.Empty; string fileName = Path.GetFileName(path); string[] split = fileName.Split('_'); uint filedataId = uint.Parse(split[1]); using (MemoryStream ms = new MemoryStream(data)) using (BinaryReader reader = new BinaryReader(ms)) { ChunkId ChunkId = (ChunkId)reader.ReadUInt32(); switch (ChunkId) { case ChunkId.MD21: ReadM2(path, filedataId); extension = "m2"; break; case ChunkId.BLP2: extension = "blp"; break; case ChunkId.SKIN: extension = "skin"; break; case ChunkId.PHYS: extension = "phys"; break; case ChunkId.OGGS: extension = "ogg"; break; case ChunkId.AUD1: case ChunkId.AUD2: case ChunkId.MVER: case ChunkId.TXVR: break; } ms.Close(); if (extension == "m2") { Console.WriteLine($"FileDataId: {filedataId} ChunkId: {ChunkId} Name: {M2Reader.Names[filedataId]}"); } else { Console.WriteLine($"FileDataId: {filedataId} ChunkId: {ChunkId}"); } NameFiles(path, extension, filedataId); } }
/// <summary>Read a required subchunk, creating a load error if it wasn't present.</summary> /// <param name="subchunk"></param> /// <param name="requiredId"></param> /// <returns></returns> public bool ReadRequiredSubchunk(out Chunk subchunk, ChunkId requiredId) { if (!ReadSubchunk(out subchunk)) { Loader.Errors.Add(new AssetLoadError(Loader, null, "Expected subchunk of type " + IdToString(requiredId) + " but chunk " + IdToString(Id) + " ended.")); return(false); } return(subchunk.RequireId(requiredId)); }
public override int GetHashCode() { unchecked { int hashCode = ChunkId.GetHashCode(); hashCode = (hashCode * 397) ^ DataLength; hashCode = (hashCode * 397) ^ HasSubChunks.GetHashCode(); return(hashCode); } }
public override int GetHashCode() { unchecked { var hashCode = WorldId.GetHashCode(); hashCode = (hashCode * 397) ^ ChunkId.GetHashCode(); hashCode = (hashCode * 397) ^ VoxelId.GetHashCode(); return(hashCode); } }
private void UnloadChunk() { ChunkId playerChunkCoord = GetPlayerChunkCoord(); foreach (ChunkId chunk in chunks.Keys) { if (Utils.Magnitude(chunk.id - playerChunkCoord.id) > WorldSettings.RenderDistanceInChunks) { chunks[chunk].Unload(); } } }
public UInt16 this[int x, int y, int z] { get { var chunk = Chunks[ChunkId.FromWorldPos(x, y, z)]; return(chunk[x & 0xF, y & 0xF, z & 0xF]); } set { var chunk = Chunks[ChunkId.FromWorldPos(x, y, z)]; chunk[x & 0xF, y & 0xF, z & 0xF] = value; } }
private static bool LoadFixedSoundInfos(ChunkReader chunkIO, ChunkId idOuter, Wad2 wad, Dictionary <long, WadSoundInfo> soundInfos) { if (idOuter != Wad2Chunks.FixedSoundInfos) { return(false); } var fixedSoundInfos = new SortedList <WadFixedSoundInfoId, WadFixedSoundInfo>(); chunkIO.ReadChunks((id, chunkSize) => { if (id != Wad2Chunks.FixedSoundInfo) { return(false); } int soundId = -1; int SoundInfoId = -1; chunkIO.ReadChunks((id2, chunkSize2) => { if (id2 == Wad2Chunks.FixedSoundInfoId) { soundId = chunkIO.ReadChunkInt(chunkSize2); } else if (id2 == Wad2Chunks.FixedSoundInfoSoundInfoId) { SoundInfoId = chunkIO.ReadChunkInt(chunkSize2); } else { return(false); } return(true); }); if (soundId == -1 || SoundInfoId == -1) { throw new Exception("Invalid fixed sound info."); } var Id = new WadFixedSoundInfoId(checked ((uint)soundId)); fixedSoundInfos.Add(Id, new WadFixedSoundInfo(Id) { SoundInfo = soundInfos[SoundInfoId] }); return(true); }); wad.FixedSoundInfosObsolete = fixedSoundInfos; return(true); }
public UInt16 this[float x, float y, float z] { get { var chunk = Chunks[ChunkId.FromWorldPos(x, y, z)]; //return chunk[x & 0xF, y & 0xF, z & 0xF]; return(chunk[(int)(x % 4) * 4, (int)(y % 4) * 4, (int)(z % 4) * 4]); } set { var chunk = Chunks[ChunkId.FromWorldPos(x, y, z)]; //chunk[x & 0xF, y & 0xF, z & 0xF] = value; chunk[(int)(x % 4) * 4, (int)(y % 4) * 4, (int)(z % 4) * 4] = value; } }
private static bool LoadTextures(ChunkReader chunkIO, ChunkId idOuter, Wad2 wad, ref Dictionary <long, WadTexture> outTextures) { if (idOuter != Wad2Chunks.Textures) { return(false); } Dictionary <long, WadTexture> textures = new Dictionary <long, WadTexture>(); long obsoleteIndex = 0; // Move this into each chunk once we got rid of old style *.wad2 files. chunkIO.ReadChunks((id, chunkSize) => { if (id != Wad2Chunks.Texture) { return(false); } var width = LEB128.ReadInt(chunkIO.Raw); var height = LEB128.ReadInt(chunkIO.Raw); byte[] textureData = null; chunkIO.ReadChunks((id2, chunkSize2) => { if (id2 == Wad2Chunks.TextureIndex) { obsoleteIndex = chunkIO.ReadChunkLong(chunkSize2); } if (id2 == Wad2Chunks.TextureData) { textureData = chunkIO.ReadChunkArrayOfBytes(chunkSize2); } else { return(false); } return(true); }); var texture = ImageC.FromByteArray(textureData, width, height); texture.ReplaceColor(new ColorC(255, 0, 255, 255), new ColorC(0, 0, 0, 0)); textures.Add(obsoleteIndex++, new WadTexture(texture)); return(true); }); outTextures = textures; return(true); }
private void InitNewChunks() { ChunkId playerChunkCoord = GetPlayerChunkCoord(); for (int x = 0; x < WorldSettings.RenderDistanceInChunks; x++) { for (int y = 0; y < WorldSettings.RenderDistanceInChunks; y++) { for (int z = 0; z < WorldSettings.RenderDistanceInChunks; z++) { ChunkId newChunk = new ChunkId(new int3(x, y, z) + playerChunkCoord.id); if (!chunks.ContainsKey(newChunk) && Utils.Magnitude(newChunk.id - playerChunkCoord.id) <= WorldSettings.RenderDistanceInChunks) { GameObject chunkGameObject = Instantiate(chunkPrefab, newChunk.ToWorldCoord(), Quaternion.identity); Chunk chunk = new Chunk(newChunk, chunkGameObject); chunks.Add(newChunk, chunk); } } } } }
private static bool LoadSpriteSequences(ChunkReader chunkIO, ChunkId idOuter, Wad2 wad, Dictionary <long, WadSprite> sprites) { if (idOuter != Wad2Chunks.SpriteSequences) { return(false); } var spriteSequences = new SortedList <WadSpriteSequenceId, WadSpriteSequence>(); chunkIO.ReadChunks((id, chunkSize) => { if (id != Wad2Chunks.SpriteSequence) { return(false); } var s = new WadSpriteSequence(new WadSpriteSequenceId(LEB128.ReadUInt(chunkIO.Raw))); chunkIO.ReadChunks((id2, chunkSize2) => { if (id2 == Wad2Chunks.SpriteSequenceSpriteIndex) { s.Sprites.Add(sprites[chunkIO.ReadChunkInt(chunkSize2)]); } else { return(false); } return(true); }); spriteSequences.Add(s.Id, s); return(true); }); wad.SpriteSequences = spriteSequences; return(true); }
public List <ChunkId> OverlappingChunks(ChunkId originalChunk) { List <ChunkId> chunks = new List <ChunkId>(); if (!IsBoundary()) { return(chunks); } for (int b = 1; b <= 0b111; b++) { int changed = 1; ChunkId newChunk = new ChunkId(originalChunk.id); for (int i = 0; i < 3; i++) { if (id[i] == 0) { newChunk[i] -= 1; } else if (id[i] == WorldSettings.chunkDimension[i] - 1) { newChunk[i] += 1; } else { changed &= ~(1 << i); } } if (changed == b) { chunks.Add(newChunk); } } return(chunks); }
public bool Equals(VoxelIdentity other) { return(WorldId == other.WorldId && ChunkId.Equals(other.ChunkId) && VoxelId == other.VoxelId); }
/// <summary>Read a required chunk, creating a load error if it wasn't present.</summary> /// <param name="loader"></param> /// <param name="requiredId"></param> /// <param name="chunk"></param> /// <returns></returns> public static bool ReadRequired(AssetLoader loader, ChunkId requiredId, out Chunk chunk) { chunk = new Chunk(loader); return(chunk.RequireId(requiredId)); }
private static bool LoadStatics(ChunkReader chunkIO, ChunkId idOuter, Wad2 wad, /*Dictionary<long, WadMesh> meshes*/ Dictionary <long, WadTexture> textures) { if (idOuter != Wad2Chunks.Statics) { return(false); } chunkIO.ReadChunks((id, chunkSize) => { if (id != Wad2Chunks.Static) { return(false); } var s = new WadStatic(new WadStaticId(LEB128.ReadUInt(chunkIO.Raw))); //s.Mesh = meshes[LEB128.ReadInt(chunkIO.Raw)]; s.Flags = LEB128.ReadShort(chunkIO.Raw); s.LightingType = (WadMeshLightingType)LEB128.ReadShort(chunkIO.Raw); chunkIO.ReadChunks((id2, chunkSize2) => { if (id2 == Wad2Chunks.StaticVisibilityBox) { var min = Vector3.Zero; var max = Vector3.Zero; chunkIO.ReadChunks((id3, chunkSize3) => { if (id3 == Wad2Chunks.MeshBoundingBoxMin) { min = chunkIO.ReadChunkVector3(chunkSize3); } else if (id3 == Wad2Chunks.MeshBoundingBoxMax) { max = chunkIO.ReadChunkVector3(chunkSize3); } else { return(false); } return(true); }); s.VisibilityBox = new BoundingBox(min, max); } else if (id2 == Wad2Chunks.StaticCollisionBox) { var min = Vector3.Zero; var max = Vector3.Zero; chunkIO.ReadChunks((id3, chunkSize3) => { if (id3 == Wad2Chunks.MeshBoundingBoxMin) { min = chunkIO.ReadChunkVector3(chunkSize3); } else if (id3 == Wad2Chunks.MeshBoundingBoxMax) { max = chunkIO.ReadChunkVector3(chunkSize3); } else { return(false); } return(true); }); s.CollisionBox = new BoundingBox(min, max); } else if (id2 == Wad2Chunks.Mesh) { s.Mesh = LoadMesh(chunkIO, chunkSize2, textures); } else if (id2 == Wad2Chunks.StaticAmbientLight) { s.AmbientLight = chunkIO.ReadChunkShort(chunkSize2); } else if (id2 == Wad2Chunks.StaticLight) { var light = new WadLight(); chunkIO.ReadChunks((id3, chunkSize3) => { if (id3 == Wad2Chunks.StaticLightPosition) { light.Position = chunkIO.ReadChunkVector3(chunkSize3); } else if (id3 == Wad2Chunks.StaticLightRadius) { light.Radius = chunkIO.ReadChunkFloat(chunkSize3); } else if (id3 == Wad2Chunks.StaticLightIntensity) { light.Intensity = chunkIO.ReadChunkFloat(chunkSize3); } else { return(false); } return(true); }); s.Lights.Add(light); } else { return(false); } return(true); }); wad.Statics.Add(s.Id, s); return(true); }); return(true); }
/// <summary>Ensure that this chunk has the required id, or report a load error.</summary> /// <param name="requiredId"></param> /// <returns></returns> public bool RequireId(ChunkId requiredId) { if (requiredId == Id) return true; Loader.Errors.Add(new AssetLoadError.InvalidData(Loader, Offset, IdToString(requiredId), IdToString(Id))); return false; }
private static bool LoadMoveables(ChunkReader chunkIO, ChunkId idOuter, Wad2 wad, Dictionary <long, WadSoundInfo> soundInfos, Dictionary <long, WadTexture> textures) { if (idOuter != Wad2Chunks.Moveables) { return(false); } chunkIO.ReadChunks((id, chunkSize) => { if (id != Wad2Chunks.Moveable) { return(false); } uint objTypeId = LEB128.ReadUInt(chunkIO.Raw); var mov = new WadMoveable(new WadMoveableId(objTypeId)); var meshes = new List <WadMesh>(); chunkIO.ReadChunks((id2, chunkSize2) => { if (id2 == Wad2Chunks.Mesh) { var mesh = LoadMesh(chunkIO, chunkSize2, textures); meshes.Add(mesh); } else if (id2 == Wad2Chunks.MoveableBone) { var skeleton = LoadBone(chunkIO, mov, meshes); // Now convert the skeleton in the new (?) format. Ugly system but this is required for // setting exact hardcoded ID for some moveables (i.e. gun mesh of an enemy, the engine // has hardcoded mesh indices for effects) var bones = new List <WadBone>(); var root = new WadBone(); root.Name = skeleton.Name; root.Translation = Vector3.Zero; root.Mesh = skeleton.Mesh; bones.Add(root); BuildNewMeshTree(skeleton, bones); mov.Bones.AddRange(bones); } else if (id2 == Wad2Chunks.MoveableBoneNew) { var bone = new WadBone(); bone.OpCode = (WadLinkOpcode)LEB128.ReadByte(chunkIO.Raw); bone.Name = chunkIO.Raw.ReadStringUTF8(); chunkIO.ReadChunks((id3, chunkSize3) => { if (id3 == Wad2Chunks.MoveableBoneTranslation) { bone.Translation = chunkIO.ReadChunkVector3(chunkSize); } else if (id3 == Wad2Chunks.MoveableBoneMeshPointer) { bone.Mesh = meshes[chunkIO.ReadChunkInt(chunkSize)]; } else { return(false); } return(true); }); mov.Bones.Add(bone); } else if (id2 == Wad2Chunks.AnimationObsolete || id2 == Wad2Chunks.Animation || id2 == Wad2Chunks.Animation2) { var animation = new WadAnimation(); animation.StateId = LEB128.ReadUShort(chunkIO.Raw); animation.EndFrame = LEB128.ReadUShort(chunkIO.Raw); animation.FrameRate = LEB128.ReadByte(chunkIO.Raw); if (id2 == Wad2Chunks.AnimationObsolete) { LEB128.ReadUShort(chunkIO.Raw); LEB128.ReadUShort(chunkIO.Raw); } int oldSpeed, oldAccel, oldLatSpeed, oldLatAccel; oldSpeed = oldAccel = oldLatSpeed = oldLatAccel = 0; if (id2 != Wad2Chunks.Animation2) { // Use old speeds/accels for legacy chunk versions oldSpeed = LEB128.ReadInt(chunkIO.Raw); oldAccel = LEB128.ReadInt(chunkIO.Raw); oldLatSpeed = LEB128.ReadInt(chunkIO.Raw); oldLatAccel = LEB128.ReadInt(chunkIO.Raw); // Correct EndFrame for legacy chunk versions if (animation.EndFrame > 0) { animation.EndFrame--; } } // Fix possibly corrupted EndFrame value which was caused by bug introduced in 1.2.9 if (animation.EndFrame == ushort.MaxValue) { animation.EndFrame = 0; } animation.NextAnimation = LEB128.ReadUShort(chunkIO.Raw); animation.NextFrame = LEB128.ReadUShort(chunkIO.Raw); bool foundNewVelocitiesChunk = false; chunkIO.ReadChunks((id3, chunkSize3) => { if (id3 == Wad2Chunks.AnimationName) { animation.Name = chunkIO.ReadChunkString(chunkSize3); } else if (id3 == Wad2Chunks.AnimationVelocities) { foundNewVelocitiesChunk = true; var velocities = chunkIO.ReadChunkVector4(chunkSize); animation.StartVelocity = velocities.X; animation.EndVelocity = velocities.Y; animation.StartLateralVelocity = velocities.Z; animation.EndLateralVelocity = velocities.W; } else if (id3 == Wad2Chunks.KeyFrame) { var keyframe = new WadKeyFrame(); chunkIO.ReadChunks((id4, chunkSize4) => { if (id4 == Wad2Chunks.KeyFrameOffset) { keyframe.Offset = chunkIO.ReadChunkVector3(chunkSize4); } else if (id4 == Wad2Chunks.KeyFrameBoundingBox) { var kfMin = Vector3.Zero; var kfMax = Vector3.Zero; chunkIO.ReadChunks((id5, chunkSize5) => { if (id5 == Wad2Chunks.MeshBoundingBoxMin) { kfMin = chunkIO.ReadChunkVector3(chunkSize5); } else if (id5 == Wad2Chunks.MeshBoundingBoxMax) { kfMax = chunkIO.ReadChunkVector3(chunkSize5); } else { return(false); } return(true); }); keyframe.BoundingBox = new BoundingBox(kfMin, kfMax); } else if (id4 == Wad2Chunks.KeyFrameAngle) { var angle = new WadKeyFrameRotation(); angle.Rotations = chunkIO.ReadChunkVector3(chunkSize4); keyframe.Angles.Add(angle); } else { return(false); } return(true); }); animation.KeyFrames.Add(keyframe); } else if (id3 == Wad2Chunks.StateChange) { var stateChange = new WadStateChange(); stateChange.StateId = LEB128.ReadUShort(chunkIO.Raw); chunkIO.ReadChunks((id4, chunkSize4) => { if (id4 == Wad2Chunks.Dispatch) { var dispatch = new WadAnimDispatch(); dispatch.InFrame = LEB128.ReadUShort(chunkIO.Raw); dispatch.OutFrame = LEB128.ReadUShort(chunkIO.Raw); dispatch.NextAnimation = LEB128.ReadUShort(chunkIO.Raw); dispatch.NextFrame = LEB128.ReadUShort(chunkIO.Raw); stateChange.Dispatches.Add(dispatch); } else { return(false); } return(true); }); animation.StateChanges.Add(stateChange); } else if (id3 == Wad2Chunks.AnimCommand) { var command = new WadAnimCommand(); long offset = chunkIO.Raw.BaseStream.Position; command.Type = (WadAnimCommandType)LEB128.ReadUShort(chunkIO.Raw); command.Parameter1 = LEB128.ReadShort(chunkIO.Raw); command.Parameter2 = LEB128.ReadShort(chunkIO.Raw); command.Parameter3 = LEB128.ReadShort(chunkIO.Raw); chunkIO.ReadChunks((id4, chunkSize4) => { if (id4 == Wad2Chunks.AnimCommandSoundInfo) { var info = chunkIO.ReadChunkInt(chunkSize4); if (info != -1) { command.SoundInfoObsolete = soundInfos[info]; } return(true); } else { return(false); } }); animation.AnimCommands.Add(command); } else { return(false); } return(true); }); // Legacy code for calculating start and end velocities if (!foundNewVelocitiesChunk) { float acceleration = oldAccel / 65536.0f; animation.StartVelocity = oldSpeed / 65536.0f; animation.EndVelocity = animation.StartVelocity + acceleration * (animation.KeyFrames.Count - 1) * animation.FrameRate; float lateralAcceleration = oldLatAccel / 65536.0f; animation.StartLateralVelocity = oldLatSpeed / 65536.0f; animation.EndLateralVelocity = animation.StartLateralVelocity + lateralAcceleration * (animation.KeyFrames.Count - 1) * animation.FrameRate; } mov.Animations.Add(animation); } else { return(false); } return(true); }); wad.Moveables.Add(mov.Id, mov); return(true); }); return(true); }
IEnumerator BuildWorld() { int i = 0; foreach (var voxel in voxels) { var x = (voxel.Key.x / 4f) + .5f; var y = (voxel.Key.y / 4f) + .5f; var z = (voxel.Key.z / 4f) + .5f; UInt16 voxelType = 1; try { this[x, y, z] = voxelType; Chunk curChunk = Chunks[ChunkId.FromWorldPos(x, y, z)]; //Vector3 pos = new Vector3((x / 4), y / 4, z / 4); Vector3 pos = new Vector3(x, y, z); if (i == 0) { Debug.Log("x: " + x + "y: " + y + "z: " + z); Debug.Log("x / 4: " + x / 4 + "y / 4: " + y / 4 + "z / 4: " + z / 4); i++; } curChunk.chunkData[(int)((x % 4) * 4), (int)((y % 4) * 4), (int)((z % 4) * 4)] = new Block(Block.BlockType.DIRT, pos, curChunk.chunk, curChunk); } catch (KeyNotFoundException e) { //Vector3 chunkPosition = new Vector3((int) (x / 4), (int) (y / 4), (int)(z / 4)); Vector3 chunkPosition = new Vector3(((x - (x % 4)) / 1024), ((y - (y % 4)) / 1024), ((z - (z % 4)) / 1024)); //Vector3 chunkPosition = new Vector3((int)(x - (x % 4) / 32), (int)(y - (y % 4) / 32), (int)(z - (z % 4) / 32)); Vector3 pos = new Vector3(x, y, z); Vector3 diff = pos - chunkPosition; Debug.Log("Coord within chunk: " + diff); Debug.Log("Indices: " + (diff.x % 4) * 4 + ", " + (diff.y % 4) * 4 + ", " + (diff.z % 4) * 4); Chunk c = new Chunk(chunkPosition, textureAtlas); c.chunk.transform.parent = this.transform; c.chunkData = new Block[chunkSize * 4, chunkSize * 4, chunkSize * 4]; Chunks.Add(new ChunkId((int)(x / 4), (int)(y / 4), (int)(z / 4)), c); c.chunkData[(int)((x % 4) * 4), (int)((y % 4) * 4), (int)((z % 4) * 4)] = new Block(Block.BlockType.DIRT, pos, c.chunk, c); } catch (ArgumentException e) { Debug.Log(e.Message); } } foreach (KeyValuePair <ChunkId, Chunk> c in Chunks) { c.Value.DrawChunk(); yield return(null); } /* * for (int z = 0; z < worldSize; z++) * for (int x = 0; x < worldSize; x++) * for (int y = 0; y < columnHeight; y++) * { * Vector3 chunkPosition = new Vector3(x * chunkSize, y * chunkSize, z * chunkSize); * Chunk c = new Chunk(chunkPosition, textureAtlas); * c.chunk.transform.parent = this.transform; * chunks.Add(c.chunk.name, c); * * } * * foreach (KeyValuePair<string, Chunk> c in chunks) * { * c.Value.DrawChunk(); * yield return null; * }*/ }
public float3 ToWorldCoord(ChunkId chunk) { return((float3)id * WorldSettings.voxelSize + chunk.ToWorldCoord()); }
public override int GetHashCode() { return(ChunkId.GetHashCode()); }
private static bool LoadSamples(ChunkReader chunkIO, ChunkId idOuter, Wad2 wad, ref Dictionary <long, WadSample> outSamples, bool obsolete) { if (idOuter != Wad2Chunks.Samples) { return(false); } var samples = new Dictionary <long, WadSample>(); long obsoleteIndex = 0; // Move this into each chunk once we got rid of old style *.wad2 files. chunkIO.ReadChunks((id, chunkSize) => { if (id != Wad2Chunks.Sample) { return(false); } string FilenameObsolete = null; byte[] data = null; chunkIO.ReadChunks((id2, chunkSize2) => { if (id2 == Wad2Chunks.SampleIndex) { obsoleteIndex = chunkIO.ReadChunkLong(chunkSize2); } else if (id2 == Wad2Chunks.SampleFilenameObsolete) { FilenameObsolete = chunkIO.ReadChunkString(chunkSize2); } else if (id2 == Wad2Chunks.SampleData) { data = chunkIO.ReadChunkArrayOfBytes(chunkSize2); } else { return(false); } return(true); }); if (data == null && !string.IsNullOrEmpty(FilenameObsolete)) { string fullPath = Path.Combine(PathC.GetDirectoryNameTry(Assembly.GetEntryAssembly().Location), "Sounds\\TR4\\Samples", FilenameObsolete + ".wav"); data = File.ReadAllBytes(fullPath); } samples.Add(obsoleteIndex++, new WadSample("", WadSample.ConvertSampleFormat(data, sampleRate => obsolete ? new WadSample.ResampleInfo { Resample = false, SampleRate = WadSample.GameSupportedSampleRate } : new WadSample.ResampleInfo { Resample = true, SampleRate = sampleRate }))); return(true); }); outSamples = samples; return(true); }
public VoxelId Translate(ChunkId originalChunk, ChunkId newChunk) { int3 offset = originalChunk.id - newChunk.id; return(new VoxelId(id - offset * (WorldSettings.chunkDimension - new int3(1, 1, 1)))); }
/// <summary>Convert a chunk identifier to a string.</summary> /// <param name="id"></param> /// <returns></returns> public static string IdToString(ChunkId id) { if (!typeof(ChunkId).IsEnumDefined(id)) return string.Format("{0:X4}h", (int)id); return string.Format("{0}/{1:X4}h", id, (int)id); }
/// <summary>Read a required chunk, creating a load error if it wasn't present.</summary> /// <param name="loader"></param> /// <param name="requiredId"></param> /// <param name="chunk"></param> /// <returns></returns> public static bool ReadRequired(AssetLoader loader, ChunkId requiredId, out Chunk chunk) { chunk = new Chunk(loader); return chunk.RequireId(requiredId); }
/// <summary>Read a required subchunk, creating a load error if it wasn't present.</summary> /// <param name="subchunk"></param> /// <param name="requiredId"></param> /// <returns></returns> public bool ReadRequiredSubchunk(out Chunk subchunk, ChunkId requiredId) { if (!ReadSubchunk(out subchunk)) { Loader.Errors.Add(new AssetLoadError(Loader, null, "Expected subchunk of type " + IdToString(requiredId) + " but chunk " + IdToString(Id) + " ended.")); return false; } return subchunk.RequireId(requiredId); }
public static string GetName(ChunkId id) => id switch {
private static bool LoadAdditionalSoundInfos(ChunkReader chunkIO, ChunkId idOuter, Wad2 wad, Dictionary <long, WadSoundInfo> soundInfos, Dictionary <long, WadSample> samples) { if (idOuter == Wad2Chunks.AdditionalSoundInfosObsolete) { chunkIO.ReadChunks((id, chunkSize) => { if (id != Wad2Chunks.SoundInfo) { return(false); } WadSoundInfo soundInfo; long index; LoadSoundInfo(chunkIO, wad, samples, out soundInfo, out index); var wId = new WadAdditionalSoundInfoId("Unnamed " + soundInfo.Name); wad.AdditionalSoundInfosObsolete.Add(wId, new WadAdditionalSoundInfo(wId) { SoundInfo = soundInfo }); return(true); }); return(true); } else if (idOuter == Wad2Chunks.AdditionalSoundInfos) { var additionalSoundInfos = new SortedList <WadAdditionalSoundInfoId, WadAdditionalSoundInfo>(); chunkIO.ReadChunks((id, chunkSize) => { if (id != Wad2Chunks.AdditionalSoundInfo) { return(false); } string soundName = null; int SoundInfoId = -1; chunkIO.ReadChunks((id2, chunkSize2) => { if (id2 == Wad2Chunks.AdditionalSoundInfoName) { soundName = chunkIO.ReadChunkString(chunkSize2); } else if (id2 == Wad2Chunks.AdditionalSoundInfoSoundInfoId) { SoundInfoId = chunkIO.ReadChunkInt(chunkSize2); } else { return(false); } return(true); }); var Id = new WadAdditionalSoundInfoId(soundName); additionalSoundInfos.Add(Id, new WadAdditionalSoundInfo(Id) { SoundInfo = soundInfos[SoundInfoId] }); return(true); }); wad.AdditionalSoundInfosObsolete = additionalSoundInfos; return(true); } return(false); }