Ejemplo n.º 1
0
        private BlockState Check(IBlockAccess world, BlockCoordinates position, BlockCoordinates updatedBlock, BlockState current)
        {
            var neighbor = world.GetBlockState(updatedBlock);

            var facePos = updatedBlock - position;
            var fp      = new Vector3(facePos.X, facePos.Y, facePos.Z);

            fp.Normalize();

            var face       = new Vector3(fp.X, fp.Y, fp.Z).GetBlockFace();
            var faceString = face.ToString().ToLower();

            current.TryGetValue(faceString, out var currentValue);

            if (CanAttach(face, neighbor.Block))
            {
                if (currentValue != "true")
                {
                    return(current.WithProperty(faceString, "true"));
                    //world.SetBlockState(position, state);
                }
            }
            else
            {
                if (currentValue != "false")
                {
                    return(current.WithProperty(faceString, "false"));
                    //world.SetBlockState(position, state);
                }
            }

            return(current);
        }
Ejemplo n.º 2
0
        public ChunkColumn GenerateChunkColumn(ChunkCoordinates chunkCoordinates)
        {
            ChunkColumn column = new ChunkColumn();

            column.X = chunkCoordinates.X;
            column.Z = chunkCoordinates.Z;

            for (int x = 0; x < 16; x++)
            {
                for (int z = 0; z < 16; z++)
                {
                    column.SetBlockState(x, 0, z, Bedrock);
                    if (column.X == 1 && column.Z == 1)
                    {
                        for (int y = 1; y < 2; y++)
                        {
                            column.SetBlockState(x, y, z, Water.WithProperty("level", "8"));
                        }
                        column.SetBlockState(x, 3, z, Water.WithProperty("level", "3"));
                    }
                    else
                    {
                        column.SetBlockState(x, 1, z, Dirt);
                        column.SetBlockState(x, 2, z, Dirt);
                        column.SetBlockState(x, 3, z, Grass);

                        if (x == 8 && z == 8)
                        {
                            column.SetBlockState(x, 5, z, Slab.WithProperty("type", "bottom"));
                        }
                        else if (x == 8 && z == 7)
                        {
                            column.SetBlockState(x, 5, z, Slab.WithProperty("type", "top"));
                        }
                    }

                    column.SetSkyLight(x, 0, z, 15);
                    column.SetSkyLight(x, 1, z, 15);
                    column.SetSkyLight(x, 2, z, 15);
                    column.SetSkyLight(x, 3, z, 15);
                    column.SetSkyLight(x, 4, z, 15);
                }
            }

            return(column);
        }
Ejemplo n.º 3
0
        public override BlockState BlockPlaced(IBlockAccess world, BlockState state, BlockCoordinates position)
        {
            if (UpdateState(world, state, position, position + BlockCoordinates.Forwards, out state, true) ||
                UpdateState(world, state, position, position + BlockCoordinates.Backwards, out state, true) ||
                UpdateState(world, state, position, position + BlockCoordinates.Left, out state, true) ||
                UpdateState(world, state, position, position + BlockCoordinates.Right, out state, true))
            {
                return(state);
            }

            return(state.WithProperty("shape", "straight"));
        }
Ejemplo n.º 4
0
        private BlockState Update(World world, BlockState blockState, BlockCoordinates coordinates, BlockCoordinates updated)
        {
            var updatedBlock = world.GetBlockState(updated);

            if (!(updatedBlock.Block is Door))
            {
                return(blockState);
            }

            bool isUpper = false;

            if (blockState.TryGetValue("half", out string half))
            {
                isUpper = half.Equals("upper", StringComparison.InvariantCultureIgnoreCase);
            }

            if (updated == coordinates + BlockCoordinates.Up && !isUpper)
            {
                if (updatedBlock.TryGetValue("hinge", out var hingeValue))
                {
                    blockState = blockState.WithProperty("hinge", hingeValue, false, "half", "open", "facing");
                }
            }
            else if (updated == coordinates + BlockCoordinates.Down && isUpper)
            {
                if (updatedBlock.TryGetValue("open", out string open))
                {
                    blockState = blockState.WithProperty("open", open, false, "half", "hinge");
                }

                if (updatedBlock.TryGetValue("facing", out var facing))
                {
                    blockState = blockState.WithProperty("facing",
                                                         facing, false, "half", "hinge", "open");
                }
            }

            return(blockState);
        }
Ejemplo n.º 5
0
        private bool UpdateState(IBlockAccess world,
                                 BlockState state,
                                 BlockCoordinates position,
                                 BlockCoordinates updatedBlock,
                                 out BlockState result, bool checkCorners)
        {
            result = state;
            var block = world.GetBlockState(updatedBlock).Block;

            if (!(block is Stairs))
            {
                return(false);
            }

            var myHalf = GetHalf(state);

            var blockState = block.BlockState;

            if (myHalf != GetHalf(blockState))
            {
                return(false);
            }

            var facing         = GetFacing(state);
            var neighborFacing = GetFacing(blockState);

            if (checkCorners)
            {
                BlockCoordinates offset1 = facing.GetVector3();

                if (neighborFacing != facing && neighborFacing != facing.Opposite() &&
                    updatedBlock == position + offset1)
                {
                    if (neighborFacing == BlockModel.RotateDirection(
                            facing, 1, BlockModel.FACE_ROTATION, BlockModel.INVALID_FACE_ROTATION))
                    {
                        if (facing == BlockFace.North || facing == BlockFace.South)
                        {
                            result = state.WithProperty("shape", "outer_right");
                        }
                        else
                        {
                            result = state.WithProperty("shape", "outer_right");
                        }

                        return(true);
                    }

                    if (facing == BlockFace.North || facing == BlockFace.South)
                    {
                        result = state.WithProperty("shape", "outer_left");
                    }
                    else
                    {
                        result = state.WithProperty("shape", "outer_left");
                    }

                    return(true);
                }

                BlockCoordinates offset2 = facing.Opposite().GetVector3();

                if (neighborFacing != facing && neighborFacing != facing.Opposite() &&
                    updatedBlock == position + offset2)
                {
                    if (neighborFacing == BlockModel.RotateDirection(
                            facing, 1, BlockModel.FACE_ROTATION, BlockModel.INVALID_FACE_ROTATION))
                    {
                        if (facing == BlockFace.North || facing == BlockFace.South)
                        {
                            result = state.WithProperty("shape", "inner_right");
                        }
                        else
                        {
                            result = state.WithProperty("shape", "inner_right");
                        }

                        return(true);
                    }

                    if (facing == BlockFace.North || facing == BlockFace.South)
                    {
                        result = state.WithProperty("shape", "inner_left");
                    }
                    else
                    {
                        result = state.WithProperty("shape", "inner_left");
                    }

                    return(true);
                }
            }
            else
            {
                if (facing == neighborFacing)
                {
                    result = state.WithProperty("shape", "straight");

                    return(true);
                }
            }


            return(false);
        }
Ejemplo n.º 6
0
Archivo: Stairs.cs Proyecto: K4mey/Alex
        private bool UpdateState(IBlockAccess world, BlockState state, BlockCoordinates position, BlockCoordinates updatedBlock, out BlockState result)
        {
            result = state;
            var block = world.GetBlockState(updatedBlock).Block;

            if (!(block is Stairs))
            {
                return(false);
            }

            var myHalf = GetHalf(state);

            var blockState = block.BlockState;

            if (myHalf != GetHalf(blockState))
            {
                return(false);
            }

            var facing   = GetFacing(state);
            var neighbor = GetFacing(blockState);

            var myShape       = GetShape(state);
            var neighborShape = GetShape(blockState);

            // int offset = (myHalf == "top") ? -1 : 1;

            //  var innerRight = ""
            if (myHalf == "top")
            {
            }

            //if ()
            {
                // if (neighbor == BlockModel.RotateDirection(facing, 1, BlockModel.FACE_ROTATION,
                //         BlockModel.INVALID_FACE_ROTATION) && neighbor != facing && GetHalf(state) == GetHalf(blockState))
                //if (facing == BlockFace.East && updatedBlock == (position + facing.Opposite().GetBlockCoordinates()))

                BlockCoordinates offset1 = facing.GetVector3();

                if (neighbor != facing && neighbor != facing.Opposite() && updatedBlock == position + offset1)
                {
                    if (neighbor == BlockModel.RotateDirection(facing, 1, BlockModel.FACE_ROTATION,
                                                               BlockModel.INVALID_FACE_ROTATION))
                    {
                        if (facing == BlockFace.North || facing == BlockFace.South)
                        {
                            result = state.WithProperty("shape", "inner_right");
                        }
                        else
                        {
                            result = state.WithProperty("shape", "outer_right");
                        }
                        return(true);
                    }

                    if (facing == BlockFace.North || facing == BlockFace.South)
                    {
                        result = state.WithProperty("shape", "inner_left");
                    }
                    else
                    {
                        result = state.WithProperty("shape", "outer_left");
                    }

                    return(true);
                }

                BlockCoordinates offset2 = facing.Opposite().GetVector3();

                if (neighbor != facing && neighbor != facing.Opposite() && updatedBlock == position + offset2)
                {
                    if (neighbor == BlockModel.RotateDirection(facing, 1, BlockModel.FACE_ROTATION,
                                                               BlockModel.INVALID_FACE_ROTATION))
                    {
                        if (facing == BlockFace.North || facing == BlockFace.South)
                        {
                            result = state.WithProperty("shape", "outer_right");
                        }
                        else
                        {
                            result = state.WithProperty("shape", "inner_right");
                        }
                        return(true);
                    }

                    if (facing == BlockFace.North || facing == BlockFace.South)
                    {
                        result = state.WithProperty("shape", "outer_left");
                    }
                    else
                    {
                        result = state.WithProperty("shape", "inner_left");
                    }
                    return(true);
                }

                /* if (updatedBlock == (position + facing.Opposite().GetBlockCoordinates()))
                 * {
                 *  return state.WithProperty("shape", "outer_left");
                 * }*/
            }

            return(false);
        }
Ejemplo n.º 7
0
        public override BlockState BlockPlaced(IBlockAccess world, BlockState state, BlockCoordinates position)
        {
            var up = position + BlockCoordinates.Up;

            string shape    = GetShape(state);
            var    hasNorth = Check(world, position, BlockFace.North, out var northernYOffset);
            var    hasEast  = Check(world, position, BlockFace.East, out var easternYOffset);
            var    hasSouth = Check(world, position, BlockFace.South, out var southernYOffset);
            var    hasWest  = Check(world, position, BlockFace.West, out var westernYOffset);

            if (hasNorth && northernYOffset == 1)
            {
                shape = "ascending_north";
            }
            else if (hasEast && easternYOffset == 1)
            {
                shape = "ascending_east";
            }
            else if (hasSouth && southernYOffset == 1)
            {
                shape = "ascending_south";
            }
            else if (hasWest && westernYOffset == 1)
            {
                shape = "ascending_west";
            }

            if (hasNorth && hasEast)
            {
                shape = "south_east";
            }
            else if (hasNorth && hasWest)
            {
                //shape = "south_west";
                shape = "north_east";
            }
            else if (hasSouth && hasEast)
            {
                //shape = "north_east";
                shape = "south_west";
            }
            else if (hasSouth && hasWest)
            {
                shape = "north_west";
            }

            return(state.WithProperty("shape", shape));

            /*if (Check(world, up, BlockFace.North))
             * {
             *      shape = "ascending_north";
             * }
             *
             * if (Check(world, up, BlockFace.East))
             * {
             *      shape = "ascending_east";
             * }
             *
             * if (Check(world, up, BlockFace.South))
             * {
             *      shape = "ascending_south";
             * }
             *
             * if (Check(world, up, BlockFace.West))
             * {
             *      shape = "ascending_west";
             * }*/
            //var north = world.GetBlockState(position + BlockCoordinates.North);
            //var east = world.GetBlockState(position + BlockCoordinates.East);
            //var south = world.GetBlockState(position + BlockCoordinates.South);
            //var west = world.GetBlockState(position + BlockCoordinates.West);

            /*if (UpdateState(world, state, position, position + BlockCoordinates.Forwards, out state)
            || UpdateState(world, state, position, position + BlockCoordinates.Backwards, out state)
            || UpdateState(world, state, position, position + BlockCoordinates.Left, out state)
            || UpdateState(world, state, position, position + BlockCoordinates.Right, out state))
            || {
            ||      return state;
            || }
            ||
            || return state;*/
        }
Ejemplo n.º 8
0
        private static int LoadModels(IRegistryManager registryManager,
                                      ResourceManager resources,
                                      bool replace,
                                      bool reportMissing,
                                      IProgressReceiver progressReceiver)
        {
            Stopwatch sw  = Stopwatch.StartNew();
            var       raw = ResourceManager.ReadStringResource("Alex.Resources.blockmap.json");

            var mapping = JsonConvert.DeserializeObject <BlockMap>(raw);

            var blockRegistry = registryManager.GetRegistry <Block>();
            //var blockStateRegistry = registryManager.GetRegistry<BlockState>();

            var data          = BlockData.FromJson(ResourceManager.ReadStringResource("Alex.Resources.NewBlocks.json"));
            int total         = data.Count;
            int done          = 0;
            int importCounter = 0;

            void LoadEntry(KeyValuePair <string, BlockData> entry)
            {
                done++;
                if (!resources.TryGetBlockState(entry.Key, out var blockStateResource))
                {
                    if (reportMissing)
                    {
                        Log.Warn($"Could not find blockstate with key: {entry.Key}");
                    }

                    return;
                }

                //double percentage = 100D * ((double) done / (double) total);blockstate variants
                progressReceiver.UpdateProgress(done, total, $"Importing block models...", entry.Key);

                var location     = new ResourceLocation(entry.Key);
                var variantMap   = new BlockStateVariantMapper();
                var defaultState = new BlockState {
                    Name = entry.Key, VariantMapper = variantMap
                };

                defaultState = defaultState.WithLocation(location).Value;

                if (entry.Value.Properties != null)
                {
                    foreach (var property in entry.Value.Properties)
                    {
                        defaultState = (BlockState)defaultState.WithPropertyNoResolve(property.Key, property.Value.FirstOrDefault(), false);
                    }
                }

                defaultState.ModelData = ResolveVariant(blockStateResource, defaultState);

                variantMap.Model       = ResolveModel(resources, blockStateResource, out bool isMultipartModel);
                variantMap.IsMultiPart = isMultipartModel;

                if (variantMap.Model == null)
                {
                    Log.Warn($"No model found for {entry.Key}[{variantMap.ToString()}]");
                }

                foreach (var s in entry.Value.States)
                {
                    if (!replace && RegisteredBlockStates.TryGetValue(s.ID, out BlockState st))
                    {
                        Log.Warn($"Duplicate blockstate id (Existing: {st.Name}[{st.ToString()}]) ");

                        continue;
                    }

                    BlockState variantState = (BlockState)(defaultState).CloneSilent();
                    variantState.ID   = s.ID;
                    variantState.Name = entry.Key;

                    if (s.Properties != null)
                    {
                        foreach (var property in s.Properties)
                        {
                            //if (property.Key.ToLower() == "waterlogged")continue;
                            variantState = (Blocks.State.BlockState)variantState.WithPropertyNoResolve(property.Key, property.Value, false);
                        }
                    }

                    IRegistryEntry <Block> registryEntry;

                    if (!blockRegistry.TryGet(location, out registryEntry))
                    {
                        registryEntry = new UnknownBlock();
                        registryEntry = registryEntry.WithLocation(location);                         // = entry.Key;
                    }
                    else
                    {
                        registryEntry = registryEntry.WithLocation(location);
                    }

                    var block = registryEntry.Value;

                    if (string.IsNullOrWhiteSpace(block.DisplayName))
                    {
                        block.DisplayName = entry.Key;
                    }

                    variantState.ModelData = ResolveVariant(blockStateResource, variantState);

                    variantState.Block = block.Value;
                    block.BlockState   = variantState;

                    //	if (variantState.IsMultiPart) multipartBased++;

                    variantState.Default = s.Default;

                    if (variantMap.TryAdd(variantState))
                    {
                        if (!RegisteredBlockStates.TryAdd(variantState.ID, variantState))
                        {
                            if (replace)
                            {
                                RegisteredBlockStates[variantState.ID] = variantState;
                                importCounter++;
                            }
                            else
                            {
                                Log.Warn($"Failed to add blockstate (variant), key already exists! ({variantState.ID} - {variantState.Name})");
                            }
                        }
                        else
                        {
                            importCounter++;
                        }
                    }
                    else
                    {
                        Log.Warn($"Could not add variant to variant map: {variantState.Name}[{variantState.ToString()}]");
                    }
                }

                if (!BlockStateByName.TryAdd(location, variantMap))
                {
                    if (replace)
                    {
                        BlockStateByName[location] = variantMap;
                    }
                    else
                    {
                        Log.Warn($"Failed to add blockstate, key already exists! ({defaultState.Name})");
                    }
                }
            }

            if (resources.Asynchronous)
            {
                Parallel.ForEach(data, LoadEntry);
            }
            else
            {
                foreach (var entry in data)
                {
                    LoadEntry(entry);
                }
            }

            var blockStateTime = sw.Elapsed;

            var mappings = mapping.GroupBy(x => x.Value.BedrockIdentifier).ToArray();

            int counter = 1;

            sw.Restart();
            Parallel.ForEach(
                mappings, (m) =>
            {
                progressReceiver?.UpdateProgress(counter, mapping.Count, "Mapping blockstates...", m.Key);

                var mapper = new BlockStateVariantMapper();
                bool first = true;

                foreach (var state in m)
                {
                    var match     = _blockMappingRegex.Match(state.Key);
                    var keyMatch  = match.Groups["key"];
                    var dataMatch = match.Groups["data"];

                    if (!keyMatch.Success)
                    {
                        Log.Warn($"Entry without key!");

                        continue;
                    }

                    BlockState pcVariant = GetBlockState(keyMatch.Value);

                    if (pcVariant != null)
                    {
                        pcVariant = pcVariant.Clone();
                        if (dataMatch.Success)
                        {
                            var properties = BlockState.ParseData(dataMatch.Value);

                            var p = properties.ToArray();

                            for (var i = 0; i < p.Length; i++)
                            {
                                var prop = p[i];

                                if (i == p.Length - 1)
                                {
                                    pcVariant = pcVariant.WithProperty(prop.Key, prop.Value);
                                }
                                else
                                {
                                    pcVariant = pcVariant.WithPropertyNoResolve(prop.Key, prop.Value, false);
                                }
                            }
                        }
                    }

                    if (pcVariant == null)
                    {
                        Log.Warn($"Map failed: {match.Groups["key"].Value} -> {state.Value.BedrockIdentifier}");

                        continue;
                    }

                    pcVariant = pcVariant.CloneSilent();

                    PeBlockState bedrockState  = new PeBlockState(pcVariant);
                    bedrockState.Name          = state.Value.BedrockIdentifier;
                    bedrockState.VariantMapper = mapper;
                    //bedrockState.AppliedModels = pcVariant.AppliedModels;
                    //	bedrockState.IsMultiPart = pcVariant.IsMultiPart;
                    //	bedrockState.MultiPartHelper = pcVariant.MultiPartHelper;
                    //	bedrockState.ResolveModel = pcVariant.ResolveModel;
                    //bedrockState.Model = pcVariant.Model;
                    bedrockState.Block   = pcVariant.Block;
                    bedrockState.ID      = (uint)Interlocked.Increment(ref counter);
                    bedrockState.Default = first;

                    first = false;

                    if (state.Value.BedrockStates != null && state.Value.BedrockStates.Count > 0)
                    {
                        foreach (var bs in state.Value.BedrockStates)
                        {
                            bedrockState.Values[bs.Key] = bs.Value.ToString();
                        }
                    }

                    if (!mapper.TryAdd(bedrockState.WithLocation(bedrockState.Name).Value))
                    {
                        Log.Warn($"Failed to add bedrockstate: {state.Value.BedrockIdentifier}");
                    }
                }

                BedrockStates[m.Key] = mapper;
            });

            //Log.Info($"Loaded {multipartBased} multi-part blockstates!");
            Log.Info($"Loaded {BedrockStates.Count} mappings in {sw.ElapsedMilliseconds}ms...");

            return(importCounter);
        }