using UnityEngine; namespace UAI { public class UAITaskMoveToExploreV4 : UAITaskMoveToTarget { private static readonly string AdvFeatureClass = "AdvancedTroubleshootingFeatures"; private static readonly string Feature = "UtilityAILoggingMin"; private string _buff; private bool _hadBuff; private Vector3 _vector; public override void initializeParameters() { base.initializeParameters(); if (Parameters.ContainsKey("buff")) _buff = Parameters["buff"]; } public override void Update(Context _context) { // true if you looted it. if (CheckContainer(_context)) { Stop(_context); return; } base.Update(_context); DoorUtils.CheckForClosedDoor(_context); if (PathingUtils.IsBlocked(_context)) { EntityUtilities.Stop(_context.Self.entityId); AdvLogging.DisplayLog(AdvFeatureClass, Feature, $"{GetType()} Am Blocked.: {_context.Self.EntityName} ( {_context.Self.entityId} "); Stop(_context); _context.Self.getNavigator().clearPath(); } } public override void Reset(Context _context) { _hadBuff = false; _vector = Vector3.zero; base.Reset(_context); } public override void Stop(Context _context) { if (CombatUtils.HasBuff(_context, _buff)) { EntityUtilities.Stop(_context.Self.entityId); // Interrupt the buff we are being attacked. if (EntityUtilities.GetAttackOrRevengeTarget(_context.Self.entityId) == null) return; } AdvLogging.DisplayLog(AdvFeatureClass, Feature, $"{GetType()} Stop: {_context.Self.EntityName} ( {_context.Self.entityId} "); _vector = Vector3.zero; base.Stop(_context); } public override void Start(Context _context) { var path = SphereCache.GetPaths(_context.Self.entityId); CombatUtils.SetCrouching(_context); if (path?.Count > 0) { _vector = path[0]; AdvLogging.DisplayLog(AdvFeatureClass, Feature, $"{GetType()} Start Workstation: {_context.Self.EntityName} ( {_context.Self.entityId} Position: {_vector} "); PathingUtils.FindPath(_context, _vector + Vector3.forward, false); //_context.Self.FindPath(_vector + Vector3.forward, _context.Self.GetMoveSpeed(), true, null); _context.ActionData.Started = true; _context.ActionData.Executing = true; return; } // If there's no targets, don't bother. this.Stop(_context); } private void GetItemFromContainer(Context _context, TileEntityComposite tileLootContainer) { var storage = tileLootContainer.GetFeature(); if (storage == null) return; var blockPos = tileLootContainer.ToWorldPos(); if (string.IsNullOrEmpty(storage.lootListName)) return; if (storage.bTouched) return; storage.bTouched = true; storage.bWasTouched = true; if (storage.items == null) return; _context.Self.MinEventContext.TileEntity = tileLootContainer; _context.Self.FireEvent(MinEventTypes.onSelfOpenLootContainer); var lootContainer = LootContainer.GetLootContainer(storage.lootListName); if (lootContainer == null) return; var array = lootContainer.Spawn(_context.Self.rand, storage.items.Length, (float)_context.Self.Progression.GetLevel(), 0f, null, new FastTags(), lootContainer.UniqueItems, false, false); AdvLogging.DisplayLog(AdvFeatureClass, Feature, $"GetItemFromContainers(): {_context.Self.EntityName} ( {_context.Self.entityId}"); for (var i = 0; i < array.Count; i++) _context.Self.bag?.AddItem(array[i].Clone()); _context.Self.FireEvent(MinEventTypes.onSelfLootContainer); } private bool CheckContainer(Context _context) { if (!_context.Self.onGround) return false; // CombatUtils.SetLookPosition(_context, _vector); var lookRay = new Ray(_context.Self.position, _context.Self.GetLookVector()); if (!Voxel.Raycast(_context.Self.world, lookRay, 3f, false, false)) return false; // Not seeing the target. if (!Voxel.voxelRayHitInfo.bHitValid) return false; // Missed the target. Overlooking? // Still too far away. var sqrMagnitude2 = (_vector - _context.Self.position).sqrMagnitude; if (sqrMagnitude2 > 2f) return false; var tileEntity = _context.Self.world.GetTileEntity(new Vector3i(_vector)); switch (tileEntity) { // if the TileEntity is a loot container, then loot it. case TileEntityComposite TileEntityComposite: GetItemFromContainer(_context, TileEntityComposite); break; } // Stop the move helper, so the entity does not slide around. EntityUtilities.Stop(_context.Self.entityId); // Trigger a buff if its defined. if (string.IsNullOrEmpty(_buff) || _hadBuff) return true; AdvLogging.DisplayLog(AdvFeatureClass, Feature, $"ExplorationPoint(): {_context.Self.EntityName} ( {_context.Self.entityId} I am close enough to {tileEntity.ToString()}"); _hadBuff = true; _context.Self.Buffs.AddBuff(_buff); SphereCache.RemovePath(_context.Self.entityId, _vector); return true; } } }