Files
zzz_REBIRTH__Utils/Scripts/Blocks/BlockRespawnRebirth.cs
2025-06-04 16:44:53 +09:30

300 lines
12 KiB
C#

using System.Globalization;
using UnityEngine.Scripting;
[Preserve]
public class BlockRespawnRebirth : BlockPlant
{
public BlockRespawnRebirth()
{
this.fertileLevel = 5;
}
public override void LateInit()
{
base.LateInit();
int vehicleRespawn = RebirthVariables.customVehicleRespawn;
if (this.Properties.Values.ContainsKey(BlockRespawnRebirth.PropGrowingNextPlant))
{
this.nextPlant = ItemClass.GetItem(this.Properties.Values[BlockRespawnRebirth.PropGrowingNextPlant], false).ToBlockValue();
if (vehicleRespawn == 0)
{
this.nextPlant = ItemClass.GetItem("FuriousRamsayAir", false).ToBlockValue();
}
if (this.nextPlant.Equals(BlockValue.Air))
{
throw new Exception("Block with name '" + this.Properties.Values[BlockRespawnRebirth.PropGrowingNextPlant] + "' not found!");
}
}
this.growOnTop = BlockValue.Air;
if (this.Properties.Values.ContainsKey(BlockRespawnRebirth.PropGrowingIsGrowOnTopEnabled) && StringParsers.ParseBool(this.Properties.Values[BlockRespawnRebirth.PropGrowingIsGrowOnTopEnabled], 0, -1, true))
{
this.bGrowOnTopEnabled = true;
if (this.Properties.Values.ContainsKey(BlockRespawnRebirth.PropGrowingGrowOnTop))
{
this.growOnTop = ItemClass.GetItem(this.Properties.Values[BlockRespawnRebirth.PropGrowingGrowOnTop], false).ToBlockValue();
if (this.growOnTop.Equals(BlockValue.Air))
{
throw new Exception("Block with name '" + this.Properties.Values[BlockRespawnRebirth.PropGrowingGrowOnTop] + "' not found!");
}
}
}
if (this.Properties.Values.ContainsKey(BlockRespawnRebirth.PropGrowingGrowthRate))
{
this.growthRate = StringParsers.ParseFloat(this.Properties.Values[BlockRespawnRebirth.PropGrowingGrowthRate], 0, -1, NumberStyles.Any);
if (vehicleRespawn > 0)
{
this.growthRate = vehicleRespawn * 60;
}
}
if (this.Properties.Values.ContainsKey(BlockRespawnRebirth.PropGrowingGrowthDeviation))
{
this.growthDeviation = StringParsers.ParseFloat(this.Properties.Values[BlockRespawnRebirth.PropGrowingGrowthDeviation], 0, -1, NumberStyles.Any);
}
if (this.Properties.Values.ContainsKey(BlockRespawnRebirth.PropGrowingFertileLevel))
{
this.fertileLevel = int.Parse(this.Properties.Values[BlockRespawnRebirth.PropGrowingFertileLevel]);
}
if (this.Properties.Values.ContainsKey(BlockRespawnRebirth.PropGrowingLightLevelStay))
{
this.lightLevelStay = int.Parse(this.Properties.Values[BlockRespawnRebirth.PropGrowingLightLevelStay]);
}
if (this.Properties.Values.ContainsKey(BlockRespawnRebirth.PropGrowingLightLevelGrow))
{
this.lightLevelGrow = int.Parse(this.Properties.Values[BlockRespawnRebirth.PropGrowingLightLevelGrow]);
}
if (this.Properties.Values.ContainsKey(BlockRespawnRebirth.PropGrowingGrowIfAnythinOnTop))
{
this.isPlantGrowingIfAnythingOnTop = StringParsers.ParseBool(this.Properties.Values[BlockRespawnRebirth.PropGrowingGrowIfAnythinOnTop], 0, -1, true);
}
if (this.Properties.Values.ContainsKey(BlockRespawnRebirth.PropGrowingIsRandom))
{
this.isPlantGrowingRandom = StringParsers.ParseBool(this.Properties.Values[BlockRespawnRebirth.PropGrowingIsRandom], 0, -1, true);
}
if (this.growthRate > 0f)
{
this.BlockTag = BlockTags.GrowablePlant;
this.IsRandomlyTick = true;
return;
}
this.IsRandomlyTick = false;
}
public override bool CanPlaceBlockAt(WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, bool _bOmitCollideCheck = false)
{
if (GameManager.Instance.IsEditMode())
{
return true;
}
if (!base.CanPlaceBlockAt(_world, _clrIdx, _blockPos, _blockValue, _bOmitCollideCheck))
{
return false;
}
Vector3i blockPos = _blockPos + Vector3i.up;
ChunkCluster chunkCluster = _world.ChunkClusters[_clrIdx];
if (chunkCluster != null)
{
byte light = chunkCluster.GetLight(blockPos, Chunk.LIGHT_TYPE.SUN);
if ((int)light < this.lightLevelStay || (int)light < this.lightLevelGrow)
{
return false;
}
}
return true;
}
public override bool CanGrowOn(WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValueOfPlant)
{
return true;
}
public override void PlaceBlock(WorldBase _world, BlockPlacement.Result _result, EntityAlive _ea)
{
base.PlaceBlock(_world, _result, _ea);
if (_ea is EntityPlayerLocal)
{
_ea.Progression.AddLevelExp((int)_result.blockValue.Block.blockMaterial.Experience, "_xpOther", Progression.XPTypes.Other, true);
}
}
public override void OnBlockAdded(WorldBase _world, Chunk _chunk, Vector3i _blockPos, BlockValue _blockValue)
{
base.OnBlockAdded(_world, _chunk, _blockPos, _blockValue);
if (!_world.IsRemote())
{
this.addScheduledTick(_world, _chunk.ClrIdx, _blockPos);
}
}
protected virtual void addScheduledTick(WorldBase _world, int _clrIdx, Vector3i _blockPos)
{
if (!this.isPlantGrowingRandom)
{
_world.GetWBT().AddScheduledBlockUpdate(_clrIdx, _blockPos, this.blockID, this.GetTickRate());
return;
}
int num = (int)this.GetTickRate();
int num2 = (int)((float)num * this.growthDeviation);
int num3 = num / 2;
int max = num + num3;
GameRandom gameRandom = _world.GetGameRandom();
int num4;
int num5;
do
{
float randomGaussian = gameRandom.RandomGaussian;
num4 = Mathf.RoundToInt((float)num + (float)num2 * randomGaussian);
num5 = Mathf.Clamp(num4, num3, max);
}
while (num5 != num4);
_world.GetWBT().AddScheduledBlockUpdate(_clrIdx, _blockPos, this.blockID, (ulong)((long)num5));
}
public override ulong GetTickRate()
{
ulong rate = (ulong)(this.growthRate * 20f * 60f);
if (RebirthVariables.testVehicleRespawns)
{
rate = 1u;
}
return rate;
}
public override bool UpdateTick(WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, bool _bRandomTick, ulong _ticksIfLoaded, GameRandom _rnd)
{
if (this.nextPlant.isair)
{
return false;
}
if (!this.CheckPlantAlive(_world, _clrIdx, _blockPos, _blockValue))
{
return true;
}
if (_bRandomTick)
{
this.addScheduledTick(_world, _clrIdx, _blockPos);
return true;
}
ChunkCluster chunkCluster = _world.ChunkClusters[_clrIdx];
if (chunkCluster == null)
{
return true;
}
Vector3i blockPos = _blockPos + Vector3i.up;
if ((int)chunkCluster.GetLight(blockPos, Chunk.LIGHT_TYPE.SUN) < this.lightLevelGrow)
{
this.addScheduledTick(_world, _clrIdx, _blockPos);
return true;
}
BlockValue block = _world.GetBlock(_clrIdx, _blockPos + Vector3i.up);
if (!this.isPlantGrowingIfAnythingOnTop && !block.isair)
{
return true;
}
BlockPlant blockPlant = this.nextPlant.Block as BlockPlant;
if (blockPlant != null && !blockPlant.CanGrowOn(_world, _clrIdx, _blockPos + Vector3i.down, this.nextPlant))
{
return true;
}
_blockValue.type = this.nextPlant.type;
BiomeDefinition biome = ((World)_world).GetBiome(_blockPos.x, _blockPos.z);
if (biome != null && biome.Replacements.ContainsKey(_blockValue.type))
{
_blockValue.type = biome.Replacements[_blockValue.type];
}
BlockValue blockValue = BlockPlaceholderMap.Instance.Replace(_blockValue, _world.GetGameRandom(), _blockPos.x, _blockPos.z, false);
blockValue.rotation = _blockValue.rotation;
blockValue.meta = _blockValue.meta;
blockValue.meta2 = 0;
_blockValue = blockValue;
if (this.bGrowOnTopEnabled)
{
_blockValue.meta = ((byte)(_blockValue.meta + 1 & 15));
}
if (this.isPlantGrowingRandom || _ticksIfLoaded <= this.GetTickRate() || !_blockValue.Block.UpdateTick(_world, _clrIdx, _blockPos, _blockValue, false, _ticksIfLoaded - this.GetTickRate(), _rnd))
{
_world.SetBlockRPC(_clrIdx, _blockPos, _blockValue);
}
if (!this.growOnTop.isair && _blockPos.y + 1 < 255 && block.isair)
{
_blockValue.type = this.growOnTop.type;
_blockValue = _blockValue.Block.OnBlockPlaced(_world, _clrIdx, _blockPos, _blockValue, _rnd);
Block block2 = _blockValue.Block;
if (_blockValue.damage >= block2.blockMaterial.MaxDamage)
{
_blockValue.damage = block2.blockMaterial.MaxDamage - 1;
}
if (this.isPlantGrowingRandom || _ticksIfLoaded <= this.GetTickRate() || !block2.UpdateTick(_world, _clrIdx, _blockPos + Vector3i.up, _blockValue, false, _ticksIfLoaded - this.GetTickRate(), _rnd))
{
_world.SetBlockRPC(_clrIdx, _blockPos + Vector3i.up, _blockValue);
}
}
return true;
}
public BlockValue ForceNextGrowStage(World _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue)
{
BlockValue block = _world.GetBlock(_clrIdx, _blockPos + Vector3i.up);
if (!this.isPlantGrowingIfAnythingOnTop && !block.isair)
{
return _blockValue;
}
_blockValue.type = this.nextPlant.type;
BiomeDefinition biome = _world.GetBiome(_blockPos.x, _blockPos.z);
if (biome != null && biome.Replacements.ContainsKey(_blockValue.type))
{
_blockValue.type = biome.Replacements[_blockValue.type];
}
BlockValue blockValue = BlockPlaceholderMap.Instance.Replace(_blockValue, _world.GetGameRandom(), _blockPos.x, _blockPos.z, false);
blockValue.rotation = _blockValue.rotation;
blockValue.meta = _blockValue.meta;
blockValue.meta2 = 0;
_blockValue = blockValue;
if (this.bGrowOnTopEnabled)
{
_blockValue.meta = ((byte)(_blockValue.meta + 1 & 15));
}
if (!this.growOnTop.isair && _blockPos.y + 1 < 255 && block.isair)
{
_blockValue.type = this.growOnTop.type;
_blockValue = _blockValue.Block.OnBlockPlaced(_world, _clrIdx, _blockPos, _blockValue, _world.GetGameRandom());
Block block2 = _blockValue.Block;
if (_blockValue.damage >= block2.blockMaterial.MaxDamage)
{
_blockValue.damage = block2.blockMaterial.MaxDamage - 1;
}
_world.SetBlockRPC(_clrIdx, _blockPos + Vector3i.up, _blockValue);
}
return _blockValue;
}
public virtual float GetGrowthRate()
{
return this.growthRate;
}
protected static string PropGrowingNextPlant = "PlantGrowing.Next";
protected static string PropGrowingGrowthRate = "PlantGrowing.GrowthRate";
protected static string PropGrowingGrowthDeviation = "PlantGrowing.GrowthDeviation";
protected static string PropGrowingFertileLevel = "PlantGrowing.FertileLevel";
protected static string PropGrowingGrowOnTop = "PlantGrowing.GrowOnTop";
protected static string PropGrowingIsGrowOnTopEnabled = "PlantGrowing.IsGrowOnTopEnabled";
protected static string PropGrowingLightLevelStay = "PlantGrowing.LightLevelStay";
protected static string PropGrowingLightLevelGrow = "PlantGrowing.LightLevelGrow";
protected static string PropGrowingIsRandom = "PlantGrowing.IsRandom";
protected static string PropGrowingGrowIfAnythinOnTop = "PlantGrowing.GrowIfAnythinOnTop";
protected BlockValue nextPlant;
protected BlockValue growOnTop;
protected bool bGrowOnTopEnabled;
protected float growthRate;
protected float growthDeviation = 0.25f;
protected int lightLevelGrow = 8;
protected bool isPlantGrowingRandom = true;
protected bool isPlantGrowingIfAnythingOnTop = true;
}