Esempio n. 1
0
        public OvenItemData(ItemStack stack)
        {
            BakingProperties bakeprops = BakingProperties.ReadFrom(stack);

            this.BrowningPoint = bakeprops.Temp ?? 160;
            this.TimeToBake    = stack.Collectible.CombustibleProps?.MeltingDuration * 10f ?? 150f;
            this.BakedLevel    = bakeprops.LevelFrom;
            this.CurHeightMul  = bakeprops.StartScaleY;
            this.temp          = 20f;
        }
Esempio n. 2
0
        protected override void updateMesh(int index)
        {
            if (Api == null || Api.Side == EnumAppSide.Server)
            {
                return;
            }
            ItemStack stack;
            bool      isWood = false;
            float     scaleY = 0;

            if (index < itemCapacity)
            {
                if (Inventory[index].Empty)
                {
                    meshes[index] = null;
                    return;
                }
                stack = Inventory[index].Itemstack;

                scaleY = bakingData[index].CurHeightMul;
            }
            else
            {
                int count = FuelSlot.Empty ? 0 : FuelSlot.Itemstack.StackSize;
                if (count <= index - itemCapacity)
                {
                    meshes[index] = null;
                    return;
                }
                stack           = FuelSlot.Itemstack.Clone();
                stack.StackSize = 1;
                isWood          = stack.Collectible.Attributes?.IsTrue("isFirewood") == true;
            }

            bool isLargeItem = false;

            if (index == 0)
            {
                BakingProperties props = BakingProperties.ReadFrom(stack);
                if (props == null)
                {
                    return;
                }
                isLargeItem = props.LargeItem;
            }
            MeshData mesh = genMesh(stack);

            if (mesh != null)
            {
                translateMesh(mesh, index, isWood, isLargeItem, scaleY);
                meshes[index] = mesh;
            }
        }
Esempio n. 3
0
        protected virtual void IncrementallyBake(float dt, int slotIndex)
        {
            ItemSlot     slot     = Inventory[slotIndex];
            OvenItemData bakeData = bakingData[slotIndex];

            float targetTemp = bakeData.BrowningPoint;

            if (targetTemp == 0)
            {
                targetTemp = 160f;                   //prevents any possible divide by zero
            }
            float diff       = bakeData.temp / targetTemp;
            float timeFactor = bakeData.TimeToBake;

            if (timeFactor == 0)
            {
                timeFactor = 1;                   //prevents any possible divide by zero
            }
            float delta = GameMath.Clamp((int)diff, 1, 30) * dt / timeFactor;

            float currentLevel = bakeData.BakedLevel;

            if (bakeData.temp > targetTemp)
            {
                currentLevel        = bakeData.BakedLevel + delta;
                bakeData.BakedLevel = currentLevel;
            }

            var   bakeProps      = BakingProperties.ReadFrom(slot.Itemstack);
            float levelFrom      = bakeProps?.LevelFrom ?? 0f;
            float levelTo        = bakeProps?.LevelTo ?? 1f;
            float startHeightMul = bakeProps?.StartScaleY ?? 1f;
            float endHeightMul   = bakeProps?.EndScaleY ?? 1f;

            float progress           = GameMath.Clamp((currentLevel - levelFrom) / (levelTo - levelFrom), 0, 1);
            float heightMul          = GameMath.Mix(startHeightMul, endHeightMul, progress);
            float nowHeightMulStaged = (int)(heightMul * BakingStageThreshold) / (float)BakingStageThreshold;

            bool reDraw = nowHeightMulStaged != bakeData.CurHeightMul;

            bakeData.CurHeightMul = nowHeightMulStaged;

            // see if increasing the partBaked by delta, has moved this stack up to the next "bakedStage", i.e. a different item

            if (currentLevel > levelTo)
            {
                float  nowTemp    = bakeData.temp;
                string resultCode = bakeProps?.ResultCode;

                if (resultCode != null)
                {
                    ItemStack resultStack = null;
                    if (slot.Itemstack.Class == EnumItemClass.Block)
                    {
                        Block block = Api.World.GetBlock(new AssetLocation(resultCode));
                        if (block != null)
                        {
                            resultStack = new ItemStack(block);
                        }
                    }
                    else
                    {
                        Item item = Api.World.GetItem(new AssetLocation(resultCode));
                        if (item != null)
                        {
                            resultStack = new ItemStack(item);
                        }
                    }


                    if (resultStack != null)
                    {
                        var collObjCb = ovenInv[slotIndex].Itemstack.Collectible as IBakeableCallback;

                        if (collObjCb != null)
                        {
                            collObjCb.OnBaked(ovenInv[slotIndex].Itemstack, resultStack);
                        }

                        ovenInv[slotIndex].Itemstack = resultStack;
                        bakingData[slotIndex]        = new OvenItemData(resultStack);
                        bakingData[slotIndex].temp   = nowTemp;

                        reDraw = true;
                    }
                }
                else
                {
                    // Allow the oven also to 'smelt' low-temperature bakeable items which do not have specific baking properties

                    ItemSlot result = new DummySlot(null);
                    if (slot.Itemstack.Collectible.CanSmelt(Api.World, ovenInv, slot.Itemstack, null))
                    {
                        slot.Itemstack.Collectible.DoSmelt(Api.World, ovenInv, ovenInv[slotIndex], result);
                        if (!result.Empty)
                        {
                            ovenInv[slotIndex].Itemstack = result.Itemstack;
                            bakingData[slotIndex]        = new OvenItemData(result.Itemstack);
                            bakingData[slotIndex].temp   = nowTemp;
                            reDraw = true;
                        }
                    }
                }
            }

            if (reDraw)
            {
                updateMesh(slotIndex);
                MarkDirty(true);
            }
        }
Esempio n. 4
0
        protected virtual bool TryPut(ItemSlot slot)
        {
            if (IsBurning || !FuelSlot.Empty)
            {
                return(false);
            }

            BakingProperties bakingProps = BakingProperties.ReadFrom(slot.Itemstack);

            if (bakingProps == null)
            {
                return(false);
            }

            if (slot.Itemstack.Attributes.GetBool("bakeable", true) == false)
            {
                return(false);
            }

            // For large items (pies) check all 4 oven slots are empty before adding the item

            if (bakingProps.LargeItem)
            {
                for (int index = 0; index < itemCapacity; index++)
                {
                    if (!ovenInv[index].Empty)
                    {
                        return(false);
                    }
                }
            }

            for (int index = 0; index < itemCapacity; index++)
            {
                if (ovenInv[index].Empty)
                {
                    int moved = slot.TryPutInto(Api.World, ovenInv[index]);

                    if (moved > 0)
                    {
                        // We store the baked level data into the BlockEntity itself, for continuity and to avoid adding unwanted attributes to the ItemStacks (which e.g. could cause them not to stack)
                        bakingData[index] = new OvenItemData(ovenInv[index].Itemstack);
                        updateMesh(index);

                        MarkDirty(true);
                        lastRemoved = null;
                    }

                    return(moved > 0);
                }
                else if (index == 0)
                {
                    // Disallow other items from being inserted if slot 0 holds a large item (a pie)

                    BakingProperties slot0Props = BakingProperties.ReadFrom(ovenInv[index].Itemstack);
                    if (slot0Props != null && slot0Props.LargeItem)
                    {
                        return(false);
                    }
                }
            }
            return(false);
        }