Files
7d2dXG/Mods/zzz_REBIRTH__Utils/Scripts/Blocks/BlockModelTreeRebirth.cs
Nathaniel Cosford e06f2bd282 Add All Mods
2025-05-29 23:33:28 +09:30

342 lines
14 KiB
C#

using System.Collections.Generic;
public class BlockModelTreeRebirth : BlockPlantGrowing
{
private List<ItemStack> itemsDropped = new List<ItemStack>();
public void DropItemsOnEvent2(WorldBase _world, BlockValue _blockValue, EnumDropEvent _eEvent, float _overallProb, Vector3 _dropPos, Vector3 _randomPosAdd, float _lifetime, int _entityId, bool _bGetSameItemIfNoneFound)
{
//Log.Out("BlockModelTreeRebirth-DropItemsOnEvent2 START");
bool downgradeBlock = RebirthVariables.customReplantTrees;
//Log.Out("DropItemsOnEvent2 flDowngradeBlock: " + flDowngradeBlock);
GameRandom gameRandom = _world.GetGameRandom();
this.itemsDropped.Clear();
List<Block.SItemDropProb> list;
if (!this.itemsToDrop.TryGetValue(_eEvent, out list))
{
if (_bGetSameItemIfNoneFound)
{
ItemValue itemValue = _blockValue.ToItemValue();
this.itemsDropped.Add(new ItemStack(itemValue, 1));
}
}
else
{
for (int i = 0; i < list.Count; i++)
{
//Log.Out("DropItemsOnEvent2 doppedItem: " + list[i].name);
if (!(downgradeBlock && list[i].name.Contains("treePlanted")))
{
Block.SItemDropProb sitemDropProb = list[i];
int num = gameRandom.RandomRange(sitemDropProb.minCount, sitemDropProb.maxCount + 1);
if (num > 0)
{
if (sitemDropProb.stickChance < 0.001f || gameRandom.RandomFloat > sitemDropProb.stickChance)
{
if (sitemDropProb.name.Equals("[recipe]"))
{
List<Recipe> recipes = CraftingManager.GetRecipes(_blockValue.Block.GetBlockName());
if (recipes.Count > 0)
{
for (int j = 0; j < recipes[0].ingredients.Count; j++)
{
if (recipes[0].ingredients[j].count / 2 > 0)
{
ItemStack item = new ItemStack(recipes[0].ingredients[j].itemValue, recipes[0].ingredients[j].count / 2);
this.itemsDropped.Add(item);
}
}
}
}
else
{
ItemValue itemValue2 = sitemDropProb.name.Equals("*") ? _blockValue.ToItemValue() : new ItemValue(ItemClass.GetItem(sitemDropProb.name, false).type, false);
if (!itemValue2.IsEmpty() && sitemDropProb.prob > gameRandom.RandomFloat)
{
this.itemsDropped.Add(new ItemStack(itemValue2, num));
}
}
}
else if (_overallProb >= 0.999f || gameRandom.RandomFloat < _overallProb)
{
BlockValue blockValue = Block.GetBlockValue(sitemDropProb.name, false);
Vector3i vector3i = World.worldToBlockPos(_dropPos);
if (!blockValue.isair && _world.GetBlock(vector3i).isair)
{
_world.SetBlockRPC(vector3i, blockValue);
}
}
}
}
}
}
for (int k = 0; k < this.itemsDropped.Count; k++)
{
if (_overallProb >= 0.999f || gameRandom.RandomFloat < _overallProb)
{
ItemClass itemClass = this.itemsDropped[k].itemValue.ItemClass;
_lifetime = ((_lifetime > 0.001f) ? _lifetime : ((itemClass != null) ? itemClass.GetLifetimeOnDrop() : 0f));
if (_lifetime > 0.001f)
{
_world.GetGameManager().ItemDropServer(this.itemsDropped[k], _dropPos, _randomPosAdd, _entityId, _lifetime, false);
}
}
}
}
public BlockModelTreeRebirth()
{
this.fertileLevel = 1;
}
public override void Init()
{
base.Init();
if (this.Properties.Values.ContainsKey("UpwardsCount"))
{
this.upwardsCount = int.Parse(this.Properties.Values["UpwardsCount"]);
}
if (this.Properties.Values.ContainsKey("FallOver"))
{
this.bFallOver = StringParsers.ParseBool(this.Properties.Values["FallOver"], 0, -1, true);
}
else
{
this.bFallOver = true;
}
this.IsTerrainDecoration = true;
this.CanDecorateOnSlopes = true;
this.CanPlayersSpawnOn = false;
this.CanMobsSpawnOn = false;
}
public override bool UpdateTick(WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, bool _bRandomTick, ulong _ticksIfLoaded, GameRandom _rnd)
{
return !_blockValue.ischild && base.UpdateTick(_world, _clrIdx, _blockPos, _blockValue, _bRandomTick, _ticksIfLoaded, _rnd);
}
public override void OnNeighborBlockChange(WorldBase _world, int _clrIdx, Vector3i _myBlockPos, BlockValue _myBlockValue, Vector3i _blockPosThatChanged, BlockValue _newNeighborBlockValue, BlockValue _oldNeighborBlockValue)
{
////Log.Out("BlockModelTreeRebirth-OnNeighborBlockChange START");
if (!_myBlockValue.ischild)
{
base.OnNeighborBlockChange(_world, _clrIdx, _myBlockPos, _myBlockValue, _blockPosThatChanged, _newNeighborBlockValue, _oldNeighborBlockValue);
}
}
public override void OnBlockValueChanged(WorldBase _world, Chunk _chunk, int _clrIdx, Vector3i _blockPos, BlockValue _oldBlockValue, BlockValue _newBlockValue)
{
////Log.Out("BlockModelTreeRebirth-OnBlockValueChanged START");
if (!_newBlockValue.ischild)
{
base.OnBlockValueChanged(_world, _chunk, _clrIdx, _blockPos, _oldBlockValue, _newBlockValue);
}
}
public override bool CheckPlantAlive(WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue)
{
return this.isPlantGrowingRandom || _world.IsRemote() || base.CheckPlantAlive(_world, _clrIdx, _blockPos, _blockValue);
}
public override bool CanPlaceBlockAt(WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, bool _bOmitCollideCheck = false)
{
//Log.Out("BlockModelTreeRebirth-CanPlaceBlockAt START");
if (!base.CanPlaceBlockAt(_world, _clrIdx, _blockPos, _blockValue, _bOmitCollideCheck))
{
return false;
}
for (int i = _blockPos.x - 3; i <= _blockPos.x + 3; i++)
{
for (int j = _blockPos.z - 3; j <= _blockPos.z + 3; j++)
{
for (int k = _blockPos.y - 6; k <= _blockPos.y + 6; k++)
{
if (_world.GetBlock(_clrIdx, new Vector3i(i, k, j)).Block is BlockModelTreeRebirth)
{
return false;
}
}
}
}
return true;
}
public override void OnBlockRemoved(WorldBase _world, Chunk _chunk, Vector3i _blockPos, BlockValue _blockValue)
{
//Log.Out("BlockModelTreeRebirth-OnBlockRemoved START");
if (!_blockValue.ischild)
{
this.shape.OnBlockRemoved(_world, _chunk, _blockPos, _blockValue);
if (this.isMultiBlock)
{
this.multiBlockPos.RemoveChilds(_world, _blockPos, _blockValue);
return;
}
}
else if (this.isMultiBlock)
{
this.multiBlockPos.RemoveParentBlock(_world, _blockPos, _blockValue);
}
this.removeAllTrunks(_world, _chunk, _blockPos, _blockValue, -1, false);
}
private void removeAllTrunks(WorldBase _world, Chunk _chunk, Vector3i _blockPos, BlockValue _blockValue, int _entityid, bool _bDropItemsAndStartParticle)
{
//Log.Out("BlockModelTreeRebirth-removeAllTrunks START");
if (_chunk == null)
{
//Log.Out("BlockModelTreeRebirth-removeAllTrunks 1");
_chunk = (Chunk)_world.GetChunkFromWorldPos(_blockPos);
}
if (_chunk == null)
{
//Log.Out("BlockModelTreeRebirth-removeAllTrunks 2");
return;
}
if (_bDropItemsAndStartParticle)
{
//Log.Out("BlockModelTreeRebirth-removeAllTrunks 3");
this.dropItems(_world, _blockPos, _blockValue, _entityid);
float lightBrightness = _world.GetLightBrightness(_blockPos);
this.SpawnDestroyParticleEffect(_world, _blockValue, _blockPos, lightBrightness, base.GetColorForSide(_blockValue, BlockFace.Top), _entityid);
}
}
private void dropItems(WorldBase _world, Vector3i _blockPos, BlockValue _blockValue, int _entityId)
{
//Log.Out("BlockModelTreeRebirth-dropItems START");
DropItemsOnEvent2(_world, _blockValue, EnumDropEvent.Destroy, 1f, World.blockToTransformPos(_blockPos), new Vector3(0.5f, 0f, 0.5f), 0f, _entityId, true);
}
public override Block.DestroyedResult OnBlockDestroyedByExplosion(WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, int _playerThatStartedExpl)
{
//Log.Out("BlockModelTreeRebirth-OnBlockDestroyedByExplosion START");
this.startToFall(_world, _clrIdx, _blockPos, _blockValue, -1);
return Block.DestroyedResult.Keep;
}
public override void OnBlockStartsToFall(WorldBase _world, Vector3i _blockPos, BlockValue _blockValue)
{
//Log.Out("BlockModelTreeRebirth-OnBlockStartsToFall START");
if (_blockValue.ischild)
{
return;
}
if (this.OnBlockDestroyedBy(_world, 0, _blockPos, _blockValue, -1, false) != Block.DestroyedResult.Keep)
{
base.OnBlockStartsToFall(_world, _blockPos, _blockValue);
return;
}
float lightBrightness = _world.GetLightBrightness(_blockPos);
this.SpawnDestroyParticleEffect(_world, _blockValue, _blockPos, lightBrightness, base.GetColorForSide(_blockValue, BlockFace.Top), -1);
}
public override void SpawnDestroyParticleEffect(WorldBase _world, BlockValue _blockValue, Vector3i _blockPos, float _lightValue, Color _color, int _entityIdThatCaused)
{
//Log.Out("BlockModelTreeRebirth-SpawnDestroyParticleEffect START");
base.SpawnDestroyParticleEffect(_world, _blockValue, _blockPos, _lightValue, _color, _entityIdThatCaused);
_world.GetGameManager().PlaySoundAtPositionServer(_blockPos.ToVector3(), "trunkbreak", 0, 100);
}
public override bool ShowModelOnFall()
{
return false;
}
public override BlockValue OnBlockPlaced(WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, GameRandom _rnd)
{
//Log.Out("BlockModelTreeRebirth-OnBlockPlaced START");
_blockValue.rotation = BiomeBlockDecoration.GetRandomRotation(_rnd.RandomFloat, 7);
return _blockValue;
}
public override Block.DestroyedResult OnBlockDestroyedBy(WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, int _entityId, bool _bUseHarvestTool)
{
bool downgradeBlock = RebirthVariables.customReplantTrees;
if (!this.bFallOver && downgradeBlock)
{
//Log.Out("OnBlockDestroyedBy 2");
return Block.DestroyedResult.Downgrade;
}
if (!this.startToFall(_world, _clrIdx, _blockPos, _blockValue, _entityId) && downgradeBlock)
{
//Log.Out("OnBlockDestroyedBy 3");
return Block.DestroyedResult.Downgrade;
}
//Log.Out("OnBlockDestroyedBy 4");
return Block.DestroyedResult.Keep;
}
private bool startToFall(WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, int _entityId)
{
//Log.Out("BlockModelTreeRebirth-startToFall START");
Transform transform;
if (!DecoManager.Instance.IsEnabled || !_blockValue.Block.IsDistantDecoration)
{
BlockEntityData blockEntity = ((Chunk)_world.GetChunkFromWorldPos(_blockPos)).GetBlockEntity(_blockPos);
if (blockEntity == null || !blockEntity.bHasTransform)
{
return false;
}
transform = blockEntity.transform;
}
else
{
transform = DecoManager.Instance.GetDecorationTransform(_blockPos, false);
}
if (transform == null)
{
_world.SetBlockRPC(_blockPos, BlockValue.Air);
return false;
}
_blockValue.damage = _blockValue.Block.MaxDamage;
_world.SetBlocksRPC(new List<BlockChangeInfo>
{
new BlockChangeInfo(_blockPos, _blockValue, false, true)
});
Entity entity = _world.GetEntity(_entityId);
EntityCreationData entityCreationData = new EntityCreationData();
entityCreationData.entityClass = "fallingTree".GetHashCode();
entityCreationData.blockPos = _blockPos;
entityCreationData.fallTreeDir = ((entity != null) ? (transform.position + Origin.position - entity.GetPosition()) : new Vector3(_world.GetGameRandom().RandomFloat, 0f, _world.GetGameRandom().RandomFloat));
entityCreationData.fallTreeDir.y = 0f;
entityCreationData.fallTreeDir = entityCreationData.fallTreeDir.normalized;
entityCreationData.pos = transform.position + Origin.position;
entityCreationData.rot = transform.rotation.eulerAngles;
entityCreationData.id = -1;
_world.GetGameManager().RequestToSpawnEntityServer(entityCreationData);
return true;
}
public override bool IsExplosionAffected()
{
return false;
}
private int upwardsCount;
private bool bFallOver;
}