Exemplo n.º 1
0
        private static WadBone LoadBone(ChunkReader chunkIO, WadMoveable mov, List <WadMesh> meshes)
        {
            WadBone bone = new WadBone();

            chunkIO.ReadChunks((id, chunkSize) =>
            {
                if (id == Wad2Chunks.MoveableBoneName)
                {
                    bone.Name = chunkIO.ReadChunkString(chunkSize);
                    //Console.WriteLine("Processing " + bone.Name);
                }
                else if (id == Wad2Chunks.MoveableBoneTranslation)
                {
                    bone.Translation = chunkIO.ReadChunkVector3(chunkSize);
                }
                else if (id == Wad2Chunks.MoveableBoneMeshPointer)
                {
                    bone.Mesh = meshes[chunkIO.ReadChunkInt(chunkSize)];
                }
                else if (id == Wad2Chunks.MoveableBone)
                {
                    bone.Children.Add(LoadBone(chunkIO, mov, meshes));
                }
                else
                {
                    return(false);
                }
                return(true);
            });

            foreach (var childBone in bone.Children)
            {
                childBone.Parent = bone;
            }

            return(bone);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        private static WadMesh LoadMesh(ChunkReader chunkIO, long chunkSize, Dictionary <long, WadTexture> textures)
        {
            var  mesh          = new WadMesh();
            long obsoleteIndex = 0;

            chunkIO.ReadChunks((id2, chunkSize2) =>
            {
                if (id2 == Wad2Chunks.MeshIndex)
                {
                    obsoleteIndex = chunkIO.ReadChunkLong(chunkSize2);
                }
                else if (id2 == Wad2Chunks.MeshName)
                {
                    mesh.Name = chunkIO.ReadChunkString(chunkSize2);
                }
                else if (id2 == Wad2Chunks.MeshSphere)
                {
                    // Read bounding sphere
                    float radius   = 0;
                    Vector3 center = Vector3.Zero;
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshSphereCenter)
                        {
                            center = chunkIO.ReadChunkVector3(chunkSize3);
                        }
                        else if (id3 == Wad2Chunks.MeshSphereRadius)
                        {
                            radius = chunkIO.ReadChunkFloat(chunkSize3);
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                    mesh.BoundingSphere = new BoundingSphere(center, radius);
                }
                else if (id2 == Wad2Chunks.MeshBoundingBox)
                {
                    // Read bounding box
                    Vector3 min = Vector3.Zero;
                    Vector3 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);
                    });
                    mesh.BoundingBox = new BoundingBox(min, max);
                }
                else if (id2 == Wad2Chunks.MeshVertexPositions)
                {
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshVertexPosition)
                        {
                            mesh.VerticesPositions.Add(chunkIO.ReadChunkVector3(chunkSize3));
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                }
                else if (id2 == Wad2Chunks.MeshVertexNormals)
                {
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshVertexNormal)
                        {
                            mesh.VerticesNormals.Add(chunkIO.ReadChunkVector3(chunkSize3));
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                }
                else if (id2 == Wad2Chunks.MeshVertexShades)
                {
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshVertexShade)
                        {
                            mesh.VerticesShades.Add(chunkIO.ReadChunkShort(chunkSize3));
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                }
                else if (id2 == Wad2Chunks.MeshPolygons)
                {
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshQuad ||
                            id3 == Wad2Chunks.MeshTriangle)
                        {
                            var polygon    = new WadPolygon();
                            polygon.Shape  = id3 == Wad2Chunks.MeshQuad ? WadPolygonShape.Quad : WadPolygonShape.Triangle;
                            polygon.Index0 = LEB128.ReadInt(chunkIO.Raw);
                            polygon.Index1 = LEB128.ReadInt(chunkIO.Raw);
                            polygon.Index2 = LEB128.ReadInt(chunkIO.Raw);
                            if (id3 == Wad2Chunks.MeshQuad)
                            {
                                polygon.Index3 = LEB128.ReadInt(chunkIO.Raw);
                            }
                            polygon.ShineStrength = LEB128.ReadByte(chunkIO.Raw);

                            TextureArea textureArea = new TextureArea();
                            textureArea.Texture     = textures[LEB128.ReadInt(chunkIO.Raw)];
                            textureArea.TexCoord0   = chunkIO.Raw.ReadVector2();
                            textureArea.TexCoord1   = chunkIO.Raw.ReadVector2();
                            textureArea.TexCoord2   = chunkIO.Raw.ReadVector2();
                            if (id3 == Wad2Chunks.MeshQuad)
                            {
                                textureArea.TexCoord3 = chunkIO.Raw.ReadVector2();
                            }
                            else
                            {
                                textureArea.TexCoord3 = textureArea.TexCoord2;
                            }
                            textureArea.BlendMode   = (BlendMode)LEB128.ReadLong(chunkIO.Raw);
                            textureArea.DoubleSided = chunkIO.Raw.ReadBoolean();
                            polygon.Texture         = textureArea;

                            chunkIO.ReadChunks((id4, chunkSize4) =>
                            {
                                return(false);
                            });

                            mesh.Polys.Add(polygon);
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                }
                else
                {
                    return(false);
                }

                return(true);
            });

            return(mesh);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        private static bool LoadSoundInfo(ChunkReader chunkIO, Wad2 wad, Dictionary <long, WadSample> samples,
                                          out WadSoundInfo soundInfo, out long index)
        {
            var   tempSoundInfo = new WadSoundInfo(0);
            long  tempIndex     = 0;
            float volume        = 0;
            float chance        = 0;
            float pitch         = 0;
            float range         = 0;

            chunkIO.ReadChunks((id2, chunkSize2) =>
            {
                // XML_SOUND_SYSTEM
                if (id2 == Wad2Chunks.SoundInfoIndex)
                {
                    tempIndex = chunkIO.ReadChunkLong(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoVolume)
                {
                    volume = chunkIO.ReadChunkFloat(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoRange)
                {
                    range = chunkIO.ReadChunkFloat(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoPitch)
                {
                    pitch = chunkIO.ReadChunkFloat(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoChance)
                {
                    chance = chunkIO.ReadChunkFloat(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoDisablePanning)
                {
                    tempSoundInfo.DisablePanning = chunkIO.ReadChunkBool(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoRandomizePitch)
                {
                    tempSoundInfo.RandomizePitch = chunkIO.ReadChunkBool(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoRandomizeVolume)
                {
                    tempSoundInfo.RandomizeVolume = chunkIO.ReadChunkBool(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoLoopBehaviour)
                {
                    tempSoundInfo.LoopBehaviour = (WadSoundLoopBehaviour)(3 & chunkIO.ReadChunkByte(chunkSize2));
                }
                else if (id2 == Wad2Chunks.SoundInfoName || id2 == Wad2Chunks.SoundInfoNameObsolete)
                {
                    tempSoundInfo.Name = chunkIO.ReadChunkString(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoSampleIndex)
                {
                    tempSoundInfo.Samples.Add(samples[chunkIO.ReadChunkInt(chunkSize2)]); // Legacy
                }
                else
                {
                    return(false);
                }
                return(true);
            });

            // Convert from floats to ints
            tempSoundInfo.Volume         = (int)Math.Round(volume * 100.0f);
            tempSoundInfo.RangeInSectors = (int)range;
            tempSoundInfo.Chance         = (int)Math.Round(chance * 100.0f);
            tempSoundInfo.PitchFactor    = (int)Math.Round((pitch - 1.0f) * 100.0f);

            // Try to get the old ID
            tempSoundInfo.Id = TrCatalog.TryGetSoundInfoIdByDescription(wad.GameVersion, tempSoundInfo.Name);

            if (string.IsNullOrWhiteSpace(tempSoundInfo.Name))
            {
                tempSoundInfo.Name = TrCatalog.GetOriginalSoundName(wad.GameVersion, unchecked ((uint)tempIndex));
            }

            index     = tempIndex;
            soundInfo = tempSoundInfo;

            return(true);
        }
Exemplo n.º 6
0
        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);
        }