Example #1
0
        private void RegenOutlineMesh(KnappingRecipe recipeToOutline, bool[,] Voxels)
        {
            MeshData recipeOutlineMesh = new MeshData(24, 36, false, false, true, false);

            recipeOutlineMesh.SetMode(EnumDrawMode.Lines);

            int greenCol  = (156 << 24) | (100 << 16) | (200 << 8) | (100);
            int orangeCol = (156 << 24) | (226 << 16) | (171 << 8) | (92);

            MeshData greenVoxelMesh  = LineMeshUtil.GetCube(greenCol);
            MeshData orangeVoxelMesh = LineMeshUtil.GetCube(orangeCol);

            for (int i = 0; i < greenVoxelMesh.xyz.Length; i++)
            {
                greenVoxelMesh.xyz[i]  = greenVoxelMesh.xyz[i] / 32f + 1 / 32f;
                orangeVoxelMesh.xyz[i] = orangeVoxelMesh.xyz[i] / 32f + 1 / 32f;
            }
            MeshData voxelMeshOffset = greenVoxelMesh.Clone();

            for (int x = 0; x < 16; x++)
            {
                for (int z = 0; z < 16; z++)
                {
                    bool shouldFill = recipeToOutline.Voxels[x, 0, z];
                    bool didFill    = Voxels[x, z];

                    if (shouldFill == didFill)
                    {
                        continue;
                    }

                    float px = x / 16f;
                    float py = 0.001f;
                    float pz = z / 16f;

                    for (int i = 0; i < greenVoxelMesh.xyz.Length; i += 3)
                    {
                        voxelMeshOffset.xyz[i]     = px + greenVoxelMesh.xyz[i];
                        voxelMeshOffset.xyz[i + 1] = py + greenVoxelMesh.xyz[i + 1];
                        voxelMeshOffset.xyz[i + 2] = pz + greenVoxelMesh.xyz[i + 2];
                    }

                    voxelMeshOffset.Rgba = (shouldFill && !didFill) ? greenVoxelMesh.Rgba : orangeVoxelMesh.Rgba;

                    recipeOutlineMesh.AddMeshData(voxelMeshOffset);
                }
            }

            recipeOutlineMeshRef?.Dispose();
            recipeOutlineMeshRef = null;
            if (recipeOutlineMesh.VerticesCount > 0)
            {
                recipeOutlineMeshRef = api.Render.UploadMesh(recipeOutlineMesh);
            }
        }
Example #2
0
        private bool TrySetSelectedRecipe(int num)
        {
            KnappingRecipe recipe = api.World.KnappingRecipes
                                    .Where(r => r.Ingredient.SatisfiesAsIngredient(BaseMaterial))
                                    .OrderBy(r => r.Output.ResolvedItemstack.GetName())
                                    .ElementAtOrDefault(num)
            ;

            selectedRecipeNumber = new List <KnappingRecipe>(api.World.KnappingRecipes).IndexOf(recipe);
            return(selectedRecipeNumber >= 0);
        }
        public override void OnReceivedClientPacket(IPlayer player, int packetid, byte[] data)
        {
            if (packetid == (int)EnumClayFormingPacket.CancelSelect)
            {
                if (BaseMaterial != null)
                {
                    Api.World.SpawnItemEntity(BaseMaterial, Pos.ToVec3d().Add(0.5));
                }
                Api.World.BlockAccessor.SetBlock(0, Pos);
                Api.World.BlockAccessor.TriggerNeighbourBlockUpdate(Pos);
            }

            if (packetid == (int)EnumClayFormingPacket.SelectRecipe)
            {
                int            recipeid = SerializerUtil.Deserialize <int>(data);
                KnappingRecipe recipe   = Api.GetKnappingRecipes().FirstOrDefault(r => r.RecipeId == recipeid);

                if (recipe == null)
                {
                    Api.World.Logger.Error("Client tried to selected knapping recipe with id {0}, but no such recipe exists!");
                    return;
                }

                selectedRecipeId = recipe.RecipeId;

                // Tell server to save this chunk to disk again
                MarkDirty();
                Api.World.BlockAccessor.GetChunkAtBlockPos(Pos.X, Pos.Y, Pos.Z).MarkModified();
            }

            if (packetid == (int)EnumClayFormingPacket.OnUserOver)
            {
                Vec3i       voxelPos;
                bool        mouseMode;
                BlockFacing facing;
                using (MemoryStream ms = new MemoryStream(data))
                {
                    BinaryReader reader = new BinaryReader(ms);
                    voxelPos  = new Vec3i(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
                    mouseMode = reader.ReadBoolean();
                    facing    = BlockFacing.ALLFACES[reader.ReadInt16()];
                }

                //   api.World.Logger.Notification("ok got use over packet from {0} at pos {1}", player.PlayerName, voxelPos);

                OnUseOver(player, voxelPos, facing, mouseMode);
            }
        }
        public void RegenMesh(bool[,] Voxels, KnappingRecipe recipeToOutline)
        {
            if (workItemMeshRef != null)
            {
                api.Render.DeleteMesh(workItemMeshRef);
                workItemMeshRef = null;
            }

            workItem = new ItemStack(api.World.GetBlock(new AssetLocation("knappingsurface")));
            if (workItem?.Block == null)
            {
                return;
            }

            if (recipeToOutline != null)
            {
                RegenOutlineMesh(recipeToOutline, Voxels);
            }

            MeshData workItemMesh = new MeshData(24, 36, false);

            workItemMesh.Flags = null;
            workItemMesh.Rgba2 = null;

            float subPixelPadding = api.BlockTextureAtlas.SubPixelPadding;

            TextureAtlasPosition tpos            = api.BlockTextureAtlas.GetPosition(workItem.Block, Material);
            MeshData             singleVoxelMesh = CubeMeshUtil.GetCubeOnlyScaleXyz(1 / 32f, 1 / 32f, new Vec3f(1 / 32f, 1 / 32f, 1 / 32f));

            singleVoxelMesh.Rgba = CubeMeshUtil.GetShadedCubeRGBA(ColorUtil.WhiteArgb, CubeMeshUtil.DefaultBlockSideShadings, false);

            texId = tpos.atlasTextureId;

            for (int i = 0; i < singleVoxelMesh.Uv.Length; i += 2)
            {
                singleVoxelMesh.Uv[i]     = tpos.x1 + singleVoxelMesh.Uv[i] * 2f / api.BlockTextureAtlas.Size - subPixelPadding;
                singleVoxelMesh.Uv[i + 1] = tpos.y1 + singleVoxelMesh.Uv[i + 1] * 2f / api.BlockTextureAtlas.Size - subPixelPadding;
            }

            singleVoxelMesh.XyzFaces      = (int[])CubeMeshUtil.CubeFaceIndices.Clone();
            singleVoxelMesh.XyzFacesCount = 6;
            singleVoxelMesh.Tints         = new int[6];
            singleVoxelMesh.Flags         = null;
            singleVoxelMesh.TintsCount    = 6;


            MeshData voxelMeshOffset = singleVoxelMesh.Clone();

            for (int x = 0; x < 16; x++)
            {
                for (int z = 0; z < 16; z++)
                {
                    if (!Voxels[x, z])
                    {
                        continue;
                    }

                    float px = x / 16f;
                    float pz = z / 16f;

                    for (int i = 0; i < singleVoxelMesh.xyz.Length; i += 3)
                    {
                        voxelMeshOffset.xyz[i]     = px + singleVoxelMesh.xyz[i];
                        voxelMeshOffset.xyz[i + 1] = singleVoxelMesh.xyz[i + 1];
                        voxelMeshOffset.xyz[i + 2] = pz + singleVoxelMesh.xyz[i + 2];
                    }

                    float offsetX = (px * 32f) / api.BlockTextureAtlas.Size;
                    float offsetZ = (pz * 32f) / api.BlockTextureAtlas.Size;

                    for (int i = 0; i < singleVoxelMesh.Uv.Length; i += 2)
                    {
                        voxelMeshOffset.Uv[i]     = singleVoxelMesh.Uv[i] + offsetX;
                        voxelMeshOffset.Uv[i + 1] = singleVoxelMesh.Uv[i + 1] + offsetZ;
                    }

                    workItemMesh.AddMeshData(voxelMeshOffset);
                }
            }

            workItemMeshRef = api.Render.UploadMesh(workItemMesh);
        }
 public static void RegisterKnappingRecipe(this ICoreServerAPI api, KnappingRecipe r)
 {
     api.ModLoader.GetModSystem <RecipeRegistrySystem>().RegisterKnappingRecipe(r);
 }
        /// <summary>
        /// Registers a new flint knapping recipe. These are sent to the client during connect, so only need to register them on the server side.
        /// </summary>
        /// <param name="recipe"></param>
        public void RegisterKnappingRecipe(KnappingRecipe recipe)
        {
            recipe.RecipeId = KnappingRecipes.Count + 1;

            KnappingRecipes.Add(recipe);
        }
Example #7
0
        public void RegenMesh(bool[,] Voxels, KnappingRecipe recipeToOutline)
        {
            workItemMeshRef?.Dispose();
            workItemMeshRef = null;

            workItem = new ItemStack(api.World.GetBlock(new AssetLocation("knappingsurface")));
            if (workItem?.Block == null)
            {
                return;
            }

            if (recipeToOutline != null)
            {
                RegenOutlineMesh(recipeToOutline, Voxels);
            }

            MeshData workItemMesh = new MeshData(24, 36, false);
            //workItemMesh.Rgba2 = null;

            float subPixelPaddingx = api.BlockTextureAtlas.SubPixelPaddingX;
            float subPixelPaddingy = api.BlockTextureAtlas.SubPixelPaddingY;

            TextureAtlasPosition tpos = api.BlockTextureAtlas.GetPosition(workItem.Block, Material);

            MeshData singleVoxelMesh = CubeMeshUtil.GetCubeOnlyScaleXyz(1 / 32f, 1 / 32f, new Vec3f(1 / 32f, 1 / 32f, 1 / 32f));

            singleVoxelMesh.Rgba = new byte[6 * 4 * 4].Fill((byte)255);
            CubeMeshUtil.SetXyzFacesAndPacketNormals(singleVoxelMesh);

            texId = tpos.atlasTextureId;

            for (int i = 0; i < singleVoxelMesh.Uv.Length; i += 2)
            {
                singleVoxelMesh.Uv[i]     = tpos.x1 + singleVoxelMesh.Uv[i] * 2f / api.BlockTextureAtlas.Size.Width - subPixelPaddingx;
                singleVoxelMesh.Uv[i + 1] = tpos.y1 + singleVoxelMesh.Uv[i + 1] * 2f / api.BlockTextureAtlas.Size.Height - subPixelPaddingy;
            }

            singleVoxelMesh.XyzFaces           = (byte[])CubeMeshUtil.CubeFaceIndices.Clone();
            singleVoxelMesh.XyzFacesCount      = 6;
            singleVoxelMesh.ClimateColorMapIds = new byte[6];
            singleVoxelMesh.SeasonColorMapIds  = new byte[6];
            singleVoxelMesh.ColorMapIdsCount   = 6;


            MeshData voxelMeshOffset = singleVoxelMesh.Clone();

            for (int x = 0; x < 16; x++)
            {
                for (int z = 0; z < 16; z++)
                {
                    if (!Voxels[x, z])
                    {
                        continue;
                    }

                    float px = x / 16f;
                    float pz = z / 16f;

                    for (int i = 0; i < singleVoxelMesh.xyz.Length; i += 3)
                    {
                        voxelMeshOffset.xyz[i]     = px + singleVoxelMesh.xyz[i];
                        voxelMeshOffset.xyz[i + 1] = singleVoxelMesh.xyz[i + 1];
                        voxelMeshOffset.xyz[i + 2] = pz + singleVoxelMesh.xyz[i + 2];
                    }

                    float offsetX = (px * 32f) / api.BlockTextureAtlas.Size.Width;
                    float offsetZ = (pz * 32f) / api.BlockTextureAtlas.Size.Height;

                    for (int i = 0; i < singleVoxelMesh.Uv.Length; i += 2)
                    {
                        voxelMeshOffset.Uv[i]     = singleVoxelMesh.Uv[i] + offsetX;
                        voxelMeshOffset.Uv[i + 1] = singleVoxelMesh.Uv[i + 1] + offsetZ;
                    }

                    workItemMesh.AddMeshData(voxelMeshOffset);
                }
            }

            workItemMeshRef = api.Render.UploadMesh(workItemMesh);
        }