using Audio; using System.Collections.Generic; using System.Diagnostics; public class BlockVehicleRebirth : BlockCarExplodeLoot { public GameRandom rng = null; public bool isRepairable = false; public bool isLootable = false; public bool canContainGas = false; public bool canContainOil = false; public bool canContainFLHeadlight = false; public bool canContainFRHeadlight = false; public bool canContainEngine = false; public bool canContainBattery = false; public bool canContainCarburetor = false; public bool canContainAltenator = false; public bool canContainTransmission = false; public bool canContainFLTire = false; public bool canContainFRTire = false; public bool canContainRLTire = false; public bool canContainRRTire = false; public float spawn_min_gas_percent = 0f; public float spawn_max_gas_percent = 0f; public float spawn_min_repaired_percent = 0f; public float spawn_max_repaired_percent = 0f; public float spawn_entity_y_offset = 0f; public string vehicle_entity_class = ""; public override int OnBlockDamaged( WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, int _damagePoints, int _entityIdThatDamaged, ItemActionAttack.AttackHitInfo _attackHitInfo, bool _bUseHarvestTool, bool _bBypassMaxDamage, int _recDepth = 0) { //Log.Out("BlockVehicleRebirth-OnBlockDamaged _bUseHarvestTool: " + _bUseHarvestTool); if (_bUseHarvestTool && _entityIdThatDamaged > 0) { if ((EntityPlayer)_world.GetEntity(_entityIdThatDamaged) is EntityPlayer player && _world.GetTileEntity(_clrIdx, _blockPos) is TileEntityDriveableLootContainer tileEntity) { if (tileEntity.bPlayerStorage && player.inventory.holdingItem.HasAnyTags(FastTags.Parse("salvageTool"))) { return 0; } } } return base.OnBlockDamaged(_world, _clrIdx, _blockPos, _blockValue, _damagePoints, _entityIdThatDamaged, _attackHitInfo, _bUseHarvestTool, _bBypassMaxDamage, _recDepth); } public override void Init() { base.Init(); CanPickup = true; rng = new GameRandom(); if (Properties.Values.ContainsKey("isRepairable")) { isRepairable = Convert.ToBoolean(Properties.Values["isRepairable"]); } if (Properties.Values.ContainsKey("isLootable")) { isLootable = Convert.ToBoolean(Properties.Values["isLootable"]); } if (Properties.Values.ContainsKey("canContainGas")) { canContainGas = Convert.ToBoolean(Properties.Values["canContainGas"]); } if (Properties.Values.ContainsKey("canContainOil")) { canContainOil = Convert.ToBoolean(Properties.Values["canContainOil"]); } if (Properties.Values.ContainsKey("canContainFLHeadlight")) { canContainFLHeadlight = Convert.ToBoolean(Properties.Values["canContainFLHeadlight"]); } if (Properties.Values.ContainsKey("canContainFRHeadlight")) { canContainFRHeadlight = Convert.ToBoolean(Properties.Values["canContainFRHeadlight"]); } if (Properties.Values.ContainsKey("canContainEngine")) { canContainEngine = Convert.ToBoolean(Properties.Values["canContainEngine"]); } if (Properties.Values.ContainsKey("canContainBattery")) { canContainBattery = Convert.ToBoolean(Properties.Values["canContainBattery"]); } if (Properties.Values.ContainsKey("canContainCarburetor")) { canContainCarburetor = Convert.ToBoolean(Properties.Values["canContainCarburetor"]); } if (Properties.Values.ContainsKey("canContainAltenator")) { canContainAltenator = Convert.ToBoolean(Properties.Values["canContainAltenator"]); } if (Properties.Values.ContainsKey("canContainTransmission")) { canContainTransmission = Convert.ToBoolean(Properties.Values["canContainTransmission"]); } if (Properties.Values.ContainsKey("canContainFLTire")) { canContainFLTire = Convert.ToBoolean(Properties.Values["canContainFLTire"]); } if (Properties.Values.ContainsKey("canContainFRTire")) { canContainFRTire = Convert.ToBoolean(Properties.Values["canContainFRTire"]); } if (Properties.Values.ContainsKey("canContainRLTire")) { canContainRLTire = Convert.ToBoolean(Properties.Values["canContainRLTire"]); } if (Properties.Values.ContainsKey("canContainRRTire")) { canContainRRTire = Convert.ToBoolean(Properties.Values["canContainRRTire"]); } if (Properties.Values.ContainsKey("spawn_min_gas_percent")) { spawn_min_gas_percent = Mathf.Clamp01(Convert.ToSingle(Properties.Values["spawn_min_gas_percent"])); } if (Properties.Values.ContainsKey("spawn_max_gas_percent")) { spawn_max_gas_percent = Mathf.Clamp01(Convert.ToSingle(Properties.Values["spawn_max_gas_percent"])); } if (Properties.Values.ContainsKey("spawn_min_repaired_percent")) { spawn_min_repaired_percent = Mathf.Clamp01(Convert.ToSingle(Properties.Values["spawn_min_repaired_percent"])); } if (Properties.Values.ContainsKey("spawn_max_repaired_percent")) { spawn_max_repaired_percent = Mathf.Clamp01(Convert.ToSingle(Properties.Values["spawn_max_repaired_percent"])); } if (Properties.Values.ContainsKey("spawn_entity_y_offset")) { spawn_entity_y_offset = Convert.ToSingle(Properties.Values["spawn_entity_y_offset"]); } if (Properties.Values.ContainsKey("vehicle_entity_class")) { vehicle_entity_class = Properties.Values["vehicle_entity_class"]; } } public override void LateInit() { base.LateInit(); cmds = new BlockActivationCommand[] { new BlockActivationCommand("Search", "loot_sack", false, false), new BlockActivationCommand("Siphon", "gas", false, false), new BlockActivationCommand("Repair", "wrench", false, false), }; } public override BlockActivationCommand[] GetBlockActivationCommands(WorldBase _world, BlockValue _blockValue, int _clrIdx, Vector3i _blockPos, EntityAlive _entityFocusing) { //Log.Out($"BlockVehicleRebirth-OnBlockActivated GetBlockActivationCommands isLootable: {isLootable}"); TileEntityDriveableLootContainer vehicleTE = _world.GetTileEntity(_clrIdx, _blockPos) as TileEntityDriveableLootContainer; string vehicleType = ""; if (Properties.Values.ContainsKey("VehicleType")) { vehicleType = Properties.Values["VehicleType"]; } //bool cantSearch = !RebirthUtilities.CanBeLooted(_blockPos, _blockValue, _cIdx); cmds[0].enabled = false; // !cantSearch && isLootable; cmds[1].enabled = false; // vehicleType != "BicycleRepair" && vehicleTE.GasPerc > 0f; // && spawn_max_repaired_percent > 0f; cmds[2].enabled = isRepairable && !string.IsNullOrEmpty(vehicle_entity_class); return cmds; } public override void OnBlockAdded(WorldBase _world, Chunk _chunk, Vector3i _blockPos, BlockValue _blockValue) { if (!_blockValue.ischild) { shape.OnBlockAdded(_world, _chunk, _blockPos, _blockValue); if (isMultiBlock) { multiBlockPos.AddChilds(_world, _chunk, _blockPos, _blockValue); } if (!string.IsNullOrEmpty(blockAddedEvent)) { GameEventManager.Current.HandleAction(blockAddedEvent, null, null, twitchActivated: false, _blockPos); } } addTileEntity(_world, _chunk, _blockPos, _blockValue); TileEntityDriveableLootContainer vehicleTE = _chunk.GetTileEntity(World.toBlock(_blockPos)) as TileEntityDriveableLootContainer; if (canContainGas) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + (int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount))); vehicleTE.GasPerc = rng.RandomRange(spawn_min_gas_percent, spawn_max_gas_percent); } else { vehicleTE.GasPerc = 0.0f; } if (spawn_max_repaired_percent > 0f) { if (canContainOil) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount)); vehicleTE.OilPerc = rng.RandomRange(0, 1f); } else { vehicleTE.OilPerc = 0.0f; } if (canContainFLHeadlight) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount)); vehicleTE.FLHeadlightPerc = rng.RandomRange(spawn_min_repaired_percent, spawn_max_repaired_percent); } else { vehicleTE.FLHeadlightPerc = 0.0f; } if (canContainFRHeadlight) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount)); vehicleTE.FRHeadlightPerc = rng.RandomRange(spawn_min_repaired_percent, spawn_max_repaired_percent); } else { vehicleTE.FRHeadlightPerc = 0.0f; } if (canContainEngine) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount)); vehicleTE.EnginePerc = rng.RandomRange(spawn_min_repaired_percent, spawn_max_repaired_percent); } else { vehicleTE.EnginePerc = 0.0f; } if (canContainBattery) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount)); vehicleTE.BatteryPerc = rng.RandomRange(spawn_min_repaired_percent, spawn_max_repaired_percent); } else { vehicleTE.BatteryPerc = 0.0f; } if (canContainCarburetor) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount)); vehicleTE.CarburetorPerc = rng.RandomRange(spawn_min_repaired_percent, spawn_max_repaired_percent); } else { vehicleTE.CarburetorPerc = 0.0f; } if (canContainAltenator) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount)); vehicleTE.AltenatorPerc = rng.RandomRange(spawn_min_repaired_percent, spawn_max_repaired_percent); } else { vehicleTE.AltenatorPerc = 0.0f; } if (canContainTransmission) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount)); vehicleTE.TransmissionPerc = rng.RandomRange(spawn_min_repaired_percent, spawn_max_repaired_percent); } else { vehicleTE.TransmissionPerc = 0.0f; } if (canContainFLTire) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount)); vehicleTE.FLTirePerc = rng.RandomRange(spawn_min_repaired_percent, spawn_max_repaired_percent); } else { vehicleTE.FLTirePerc = 0.0f; } if (canContainFRTire) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount)); vehicleTE.FRTirePerc = rng.RandomRange(spawn_min_repaired_percent, spawn_max_repaired_percent); } else { vehicleTE.FRTirePerc = 0.0f; } if (canContainRLTire) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount)); vehicleTE.RLTirePerc = rng.RandomRange(spawn_min_repaired_percent, spawn_max_repaired_percent); } else { vehicleTE.RLTirePerc = 0.0f; } if (canContainRRTire) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount)); vehicleTE.RRTirePerc = rng.RandomRange(spawn_min_repaired_percent, spawn_max_repaired_percent); } else { vehicleTE.RRTirePerc = 0.0f; } } } public override void OnBlockValueChanged(WorldBase _world, Chunk _chunk, int _clrIdx, Vector3i _blockPos, BlockValue _oldBlockValue, BlockValue _newBlockValue) { base.OnBlockValueChanged(_world, _chunk, _clrIdx, _blockPos, _oldBlockValue, _newBlockValue); if (_newBlockValue.isair) { removeTileEntity(_world, _chunk, _blockPos, _newBlockValue); } } public override bool OnBlockActivated(string _commandName, WorldBase _world, int _cIdx, Vector3i _blockPos, BlockValue _blockValue, EntityPlayerLocal _player) { //Log.Out($"BlockVehicleRebirth-OnBlockActivated _commandName: {_commandName}, player: {_player}"); TileEntityDriveableLootContainer vehicleTE = _world.GetTileEntity(_cIdx, _blockPos) as TileEntityDriveableLootContainer; switch (_commandName) { case "Search": //Log.Out("Vehicle was searched!"); if (_player.inventory.IsHoldingItemActionRunning()) { return false; } if (_commandName == "Search" && !RebirthUtilities.CanBeLooted(_blockPos, _blockValue, _cIdx)) { Manager.PlayInsidePlayerHead("ui_denied"); GameManager.ShowTooltip(_player as EntityPlayerLocal, Localization.Get("ttBelongsToTrader"), string.Empty, "ui_denied", null); return false; } _player.AimingGun = false; Vector3i blockPos = vehicleTE.ToWorldPos(); vehicleTE.bWasTouched = vehicleTE.bTouched; _world.GetGameManager().TELockServer(_cIdx, blockPos, vehicleTE.entityId, _player.entityId); return true; /*case "inspect": Log.Out($"Vehicle was inspected!"); Log.Out($"Gas is at {vehicleTE.GasPerc}!"); Log.Out($"Oil is at {vehicleTE.OilPerc}!"); Log.Out($"Front left headlight is at {vehicleTE.FLHeadlightPerc}!"); Log.Out($"Front right headlight is at {vehicleTE.FRHeadlightPerc}!"); Log.Out($"Engine is at {vehicleTE.EnginePerc}!"); Log.Out($"Battery is at {vehicleTE.BatteryPerc}!"); Log.Out($"Carburetor is at {vehicleTE.CarburetorPerc}!"); Log.Out($"Altenator is at {vehicleTE.AltenatorPerc}!"); Log.Out($"Transmission is at {vehicleTE.TransmissionPerc}!"); Log.Out($"Front left tire is at {vehicleTE.FLTirePerc}!"); Log.Out($"Front right tire is at {vehicleTE.FRTirePerc}!"); Log.Out($"Rear left tire is at {vehicleTE.RLTirePerc}!"); Log.Out($"Rear right tire is at {vehicleTE.RRTirePerc}!"); break;*/ case "Repair": if (Properties.Values.ContainsKey("VehicleType")) { string vehicleType = Properties.Values["VehicleType"]; RebirthUtilities.OpenTileEntity(_player, _blockPos, vehicleType, "window" + vehicleType, "UseActions/service_vehicle"); } break; case "Siphon": RebirthUtilities.addToPlayerBag(ItemClass.GetItem("ammoGasCan"), _player, Mathf.RoundToInt(vehicleTE.GasPerc * 500), "useactions/gas_refill"); vehicleTE.GasPerc = 0f; break; } return true; } public override void OnBlockRemoved(WorldBase world, Chunk _chunk, Vector3i _blockPos, BlockValue _blockValue) { base.OnBlockRemoved(world, _chunk, _blockPos, _blockValue); removeTileEntity(world, _chunk, _blockPos, _blockValue); } public override void addTileEntity(WorldBase world, Chunk _chunk, Vector3i _blockPos, BlockValue _blockValue) { TileEntityDriveableLootContainer vehicleTE = new TileEntityDriveableLootContainer(_chunk) { localChunkPos = World.toBlock(_blockPos), lootListName = lootList, }; vehicleTE.SetContainerSize(LootContainer.GetLootContainer(lootList).size); vehicleTE.InitRepairableVehicleParts(); _chunk.AddTileEntity(vehicleTE); } public override void removeTileEntity(WorldBase world, Chunk _chunk, Vector3i _blockPos, BlockValue _blockValue) { _chunk.RemoveTileEntityAt((World)world, World.toBlock(_blockPos)); } } public class TileEntityDriveableLootContainer : TileEntityLootContainer { public Block block = null; public ItemValue[] itemValues = new ItemValue[] { }; public float Durability, baseDurability, maxDurability, vehicleHealth, baseVehicleHealth, GasPerc, OilPerc, FLHeadlightPerc, FRHeadlightPerc, EnginePerc, BatteryPerc, CarburetorPerc, AltenatorPerc, TransmissionPerc, FLTirePerc, FRTirePerc, RLTirePerc, RRTirePerc; public TileEntityDriveableLootContainer(Chunk _chunk) : base(_chunk) { block = base.blockValue.Block; for (int i = 0; i < itemValues.Length; i++) { itemValues[i] = ItemValue.None; } Durability = 0f; baseDurability = 0f; maxDurability = 0f; vehicleHealth = 0f; baseVehicleHealth = 0f; GasPerc = 0f; OilPerc = 0f; FLHeadlightPerc = 0f; FRHeadlightPerc = 0f; EnginePerc = 0f; BatteryPerc = 0f; CarburetorPerc = 0f; AltenatorPerc = 0f; TransmissionPerc = 0f; FLTirePerc = 0f; FRTirePerc = 0f; RLTirePerc = 0f; RRTirePerc = 0f; } public override void read(PooledBinaryReader _br, StreamModeRead _eStreamMode) { base.read(_br, _eStreamMode); try { itemValues = GameUtils.ReadItemValueArray(_br); } catch { //Log.Error("BlockVehicleRebirth-read block: " + this.block.GetBlockName()); return; } Durability = _br.ReadSingle(); baseDurability = _br.ReadSingle(); maxDurability = _br.ReadSingle(); vehicleHealth = _br.ReadSingle(); baseVehicleHealth = _br.ReadSingle(); GasPerc = _br.ReadSingle(); OilPerc = _br.ReadSingle(); FLHeadlightPerc = _br.ReadSingle(); FRHeadlightPerc = _br.ReadSingle(); EnginePerc = _br.ReadSingle(); BatteryPerc = _br.ReadSingle(); CarburetorPerc = _br.ReadSingle(); AltenatorPerc = _br.ReadSingle(); TransmissionPerc = _br.ReadSingle(); FLTirePerc = _br.ReadSingle(); FRTirePerc = _br.ReadSingle(); RLTirePerc = _br.ReadSingle(); RRTirePerc = _br.ReadSingle(); } public override void write(PooledBinaryWriter stream, StreamModeWrite _eStreamMode) { base.write(stream, _eStreamMode); GameUtils.WriteItemValueArray(stream, itemValues); stream.Write(Durability); stream.Write(baseDurability); stream.Write(maxDurability); stream.Write(vehicleHealth); stream.Write(baseVehicleHealth); stream.Write(GasPerc); stream.Write(OilPerc); stream.Write(FLHeadlightPerc); stream.Write(FRHeadlightPerc); stream.Write(EnginePerc); stream.Write(BatteryPerc); stream.Write(CarburetorPerc); stream.Write(AltenatorPerc); stream.Write(TransmissionPerc); stream.Write(FLTirePerc); stream.Write(FRTirePerc); stream.Write(RLTirePerc); stream.Write(RRTirePerc); } public void InitRepairableVehicleParts() { //Log.Out("TileEntityDriveableLootContainer-InitRepairableVehicleParts BEFORE block: " + block.GetBlockName()); block = blockValue.Block as BlockVehicleRebirth; //Log.Out("TileEntityDriveableLootContainer-InitRepairableVehicleParts AFTER block: " + block.GetBlockName()); GameRandom rng = new GameRandom(); if (block.Properties.Values.ContainsKey("VehicleType")) { //Log.Out("TileEntityDriveableLootContainer-InitRepairableVehicleParts VehicleType: " + block.Properties.Values["VehicleType"]); rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount)); int health = 0; if (block.Properties.Values.ContainsKey("vehicle_entity_class")) { ItemValue itemVehicle = ItemClass.GetItem(block.Properties.Values["vehicle_entity_class"] + "Placeable", false); //Log.Out("TileEntityDriveableLootContainer-InitRepairableVehicleParts A"); if (itemVehicle != null) { health = (int)EffectManager.GetValue(PassiveEffects.DegradationMax, itemVehicle, 0f, null, null, (itemVehicle.ItemClass != null) ? itemVehicle.ItemClass.ItemTags : FastTags.none, true, true, true, true, true, 1, false); //Log.Out("TileEntityDriveableLootContainer-InitRepairableVehicleParts health: " + health); } } vehicleHealth = health; // * 0.75f; baseVehicleHealth = vehicleHealth; maxDurability = vehicleHealth; //Log.Out("TileEntityDriveableLootContainer-InitRepairableVehicleParts maxDurability: " + maxDurability); baseDurability = rng.RandomRange(Convert.ToSingle(block.Properties.Values["spawn_min_repaired_percent"]) * vehicleHealth, Convert.ToSingle(block.Properties.Values["spawn_max_repaired_percent"]) * vehicleHealth); Durability = baseDurability; //Log.Out("TileEntityDriveableLootContainer-InitRepairableVehicleParts baseDurability: " + baseDurability); //Log.Out("TileEntityDriveableLootContainer-InitRepairableVehicleParts spawn_max_repaired_percent: " + block.Properties.Values["spawn_max_repaired_percent"]); //Log.Out("TileEntityDriveableLootContainer-InitRepairableVehicleParts baseVehicleHealth: " + baseVehicleHealth); //Log.Out("TileEntityDriveableLootContainer-InitRepairableVehicleParts Durability: " + Durability); List itemValueEnums = RebirthVariables.localVehicleTypes[block.Properties.Values["VehicleType"]]; itemValues = new ItemValue[itemValueEnums.Count]; foreach (RepairableVehicleSlotsEnum part in itemValueEnums) { ItemValue item = ItemClass.GetItem(RebirthVariables.localVehicleParts[part].itemName); DictionarySave propValues = item.ItemClass.Properties.Values; //Log.Out("TileEntityDriveableLootContainer-InitRepairableVehicleParts part: " + part); //Log.Out("TileEntityDriveableLootContainer-InitRepairableVehicleParts item: " + item.ItemClass.GetItemName()); RepairableVehicleSlotsEnum slot = (RepairableVehicleSlotsEnum)Enum.Parse(typeof(RepairableVehicleSlotsEnum), propValues["VehicleSlot"]); for (int i = 0; i < itemValueEnums.Count; i++) { rng.SetSeed((int)((float)Stopwatch.GetTimestamp() + GameManager.frameCount) + i); itemValues[i] = ItemValue.None; } } float increase = 0; foreach (ItemValue item in itemValues) { if (item != ItemValue.None) { string itemName = item.ItemClass.GetItemName(); foreach (RepairableVehicleSlotsEnum part in itemValueEnums) { if (RebirthVariables.localVehicleParts[part].itemName == itemName) { increase += ((item.MaxUseTimes - item.UseTimes) / item.MaxUseTimes) * RebirthVariables.localVehicleParts[part].durabilityPerQuality * item.Quality; //maxDurability += RebirthVariables.localVehicleParts[part].durabilityPerQuality * item.Quality; /*Log.Out("XUiC_RepairableVehicleWindow-BtnHotwire_OnPress itemName: " + itemName); Log.Out("XUiC_RepairableVehicleWindow-BtnHotwire_OnPress PercUsed: " + ((item.MaxUseTimes - item.UseTimes) / item.MaxUseTimes)); Log.Out("XUiC_RepairableVehicleWindow-BtnHotwire_OnPress Quality: " + item.Quality); Log.Out("XUiC_RepairableVehicleWindow-BtnHotwire_OnPress durabilityPerQuality: " + RebirthVariables.localVehicleParts[part].durabilityPerQuality); Log.Out("XUiC_RepairableVehicleWindow-BtnHotwire_OnPress durability: " + durability);*/ break; } } } } vehicleHealth += increase; setModified(); } } public void SetRepairableVehicleParts(ItemValue[] newItemValues) { itemValues = newItemValues; } public ItemValue[] GetRepairableVehicleParts() { return itemValues; } public override TileEntityType GetTileEntityType() { //return (TileEntityType)243; return (TileEntityType)RebirthUtilities.TileEntityRebirth.TileEntityDriveableLootContainer; } }