Пример #1
0
    public override void applyAction(BlockAction action)
    {
        if (!isServerBlock)
        {
            return;
        }
        BlockChanged bc = action as BlockChanged;

        if (bc != null)
        {
            //Debug.Log("Applying " + action);
            Liquid liq = bc.changed as Liquid;
            if (liq != null || bc.changed is Air)
            {
                float otherPressure = liq != null?liq.getPressure() : 0;

                float otherLvl = otherPressure;
                if (bc.changed.getCoordinates().y > coords.y)
                {
                    otherPressure += density;
                }
                if (bc.changed.getCoordinates().y < coords.y)
                {
                    otherPressure -= density;
                }
                float dt    = Math.Min((float)DateTime.Now.Subtract(lastPressureUpdate).TotalSeconds, bc.getAge());
                float delta = Math.Min(0.1f, FLOW_RATE * dt) * (otherPressure - getPressure());
                delta = Math.Max(-(getPressure() - MIN_LEVEL), delta);
                if (delta < 0 && otherLvl - delta <= MIN_LEVEL)
                {
                    delta = -(MIN_LEVEL - otherLvl + 0.001f);
                    if (getPressure() + delta < MIN_LEVEL && coords.y <= bc.changed.getCoordinates().y)
                    {
                        delta = 0;
                    }
                }
                delta = Math.Max(-getPressure(), Math.Min(otherLvl, delta));
                if (Math.Abs(delta) / dt > MIN_CHANGE_PER_SECOND)
                {
                    //Debug.Log("Trying to flow " + delta);
                    if (delta + getPressure() < MIN_LEVEL)
                    {
                        delta = -getPressure();
                    }
                    if (-delta + otherLvl < MIN_LEVEL)
                    {
                        delta = otherLvl;
                    }
                    addPressure(delta);
                    if (liq != null)
                    {
                        liq.addPressure(-delta);
                    }
                    else
                    {
                        liq = spreadTo(bc.changed.getPosition(), -delta) as Liquid;
                    }
                    Debug.Log(this + ": Flown " + delta + " - " + this + " is now " + getPressure() + " " + liq + " is now " + liq.getPressure());
                    if (level < MIN_LEVEL && level > 0.001f)
                    {
                        Debug.LogError("Lost water");
                    }
                }
                else if (level < MIN_LEVEL)
                {
                    if (data.pressure > 0)
                    {
                        Debug.LogError("Lost water " + data.pressure + " in " + this);
                    }
                    //Debug.Log(this + ": Removed placeholder");
                    pos.getChunk().setBlock(pos, new Air(coords, true));
                }
                else
                {
                    //Debug.Log(this + ": " + delta + " / " + dt + " too small for change");
                }
            }
            else
            {
                //Debug.Log("Invalid neighbour");
            }
        }
        else
        {
            Debug.LogError("Unknown action: " + action);
        }
    }