/// <summary> /// Removes target assignment from vehicle. /// </summary> /// <param name="vehicleId">The vehicle identifier.</param> /// <param name="vehicle">The vehicle data.</param> /// <param name="recall">If set to <c>true</c> recall vehicle to service building.</param> /// <returns> /// The result of the action. /// </returns> public static VehicleResult DeAssign(ushort vehicleId, ref Vehicle vehicle, bool recall = false) { try { // Vehicle has spawned but not moved, just unspawn. Vector3 spawnPos = GetSpawnPosition(vehicleId, vehicle.Info, vehicle.m_sourceBuilding); if ((vehicle.m_frame0.m_position - spawnPos).sqrMagnitude < 1 && (vehicle.m_frame1.m_position - spawnPos).sqrMagnitude < 1 && (vehicle.m_frame2.m_position - spawnPos).sqrMagnitude < 1 && (vehicle.m_frame3.m_position - spawnPos).sqrMagnitude < 1) { Log.Debug(typeof(VehicleHelper), "DeAssign", "DeSpawn", vehicleId, vehicle, vehicle.Info.m_vehicleAI); if ((vehicle.m_flags & Vehicle.Flags.WaitingPath) == Vehicle.Flags.WaitingPath && vehicle.m_path != 0) { Singleton <PathManager> .instance.ReleasePath(vehicle.m_path); vehicle.m_path = 0; vehicle.m_flags &= ~Vehicle.Flags.WaitingPath; } vehicle.m_flags &= ~Vehicle.Flags.WaitingSpace; DeSpawn(vehicleId); return(new VehicleResult(VehicleResult.Result.DeSpawned)); } } catch (Exception ex) { Log.Error(typeof(VehicleHelper), "DeAssign", ex); } VehicleResult result = SetTarget(vehicleId, ref vehicle, 0, 0); if ((vehicle.m_flags & Vehicle.Flags.WaitingTarget) != ~VehicleHelper.VehicleAll) { Global.TransferOffersCleaningNeeded = true; if (recall) { //if (Log.LogALot) //{ // Log.DevDebug(typeof(VehicleHelper), "DeAssign", "Recall", vehicleId, vehicle.m_targetBuilding, vehicle.m_flags); //} vehicle.m_waitCounter = byte.MaxValue - 1; if (result) { result = new VehicleResult(VehicleResult.Result.Recalled); } } } return(result); }
/// <summary> /// Updates the specified vehicle. /// </summary> /// <param name="vehicle">The vehicle.</param> /// <param name="freeToCollect">If set to <c>true</c> the vehicle is free.</param> /// <param name="checkAssignment">If set to <c>true</c> check vehicles assignment and possibly de-assign vehicle].</param> /// <param name="updateValues">If set to <c>true</c> update vehicle values.</param> public VehicleResult Update(ref Vehicle vehicle, bool freeToCollect, bool checkAssignment, bool updateValues) { VehicleResult result = true; if (this.LastSeen != Global.CurrentFrame) { if (updateValues) { string localeKey; int bufCur, bufMax; vehicle.Info.m_vehicleAI.GetBufferStatus(this.VehicleId, ref vehicle, out localeKey, out bufCur, out bufMax); this.CapacityFree = bufMax - bufCur; this.CapacityUsed = (float)bufCur / (float)bufMax; } if (checkAssignment && (vehicle.m_flags & (VehicleHelper.VehicleUnavailable | VehicleHelper.VehicleBusy)) == ~VehicleHelper.VehicleAll && vehicle.m_targetBuilding != 0 && vehicle.m_targetBuilding != this.Target && Global.CurrentFrame - this.LastAssigned > Global.DemandLingerDelay) { if (Log.LogALot) { Log.DevDebug(this, "Update", this.dispatcherType, "CheckAssignment", "DeAssign", this.VehicleId, vehicle.m_targetBuilding, vehicle.m_flags); } result = this.DeAssign(ref vehicle, false, "Update"); } } this.Position = vehicle.GetLastFramePosition(); this.LastSeen = Global.CurrentFrame; this.FreeToCollect = freeToCollect; if (checkAssignment && vehicle.m_targetBuilding != this.Target) { this.Target = 0; } else if (this.Target != 0) { this.LastAssigned = Global.CurrentFrame; } return(result); }
/// <summary> /// De-assign target from vehicle. /// </summary> /// <param name="vehicle">The vehicle.</param> /// <param name="force">If set to <c>true</c> force de-assignment.</param> /// <param name="sourceObject">The source object.</param> /// <param name="sourceBlock">The source block.</param> /// <param name="logMessage">The log message.</param> /// <returns>The result of the action.</returns> public VehicleResult DeAssign(ref Vehicle vehicle, bool force = false, object sourceObject = null, string sourceBlock = null, string logMessage = null) { if (this.lastDeAssignStamp == Global.CurrentFrame) { return(new VehicleResult(vehicle.m_targetBuilding == 0 && this.Target == 0)); } if (force || Global.CurrentFrame - this.LastAssigned > Global.TargetLingerDelay || (vehicle.m_targetBuilding != this.Target && !this.LingerWrongTargets)) { this.lastDeAssignStamp = Global.CurrentFrame; if (vehicle.m_targetBuilding == 0 && this.Target == 0) { return(new VehicleResult(true)); } if (Log.LogALot && (sourceObject != null || sourceBlock != null || logMessage != null)) { Log.DevDebug(this, "DeAssign", sourceBlock, logMessage, this.VehicleId, vehicle.m_targetBuilding, this.Target); } // Set internal target. this.Target = 0; // Unassign the vehicle. ushort serviceBuildingId = vehicle.m_sourceBuilding; VehicleResult result = VehicleHelper.DeAssign(this.VehicleId, ref vehicle); if (result.DeSpawned) { ServiceBuildingInfo building = Global.Buildings.GetServiceBuilding(serviceBuildingId); if (building != null) { building.Vehicles.Remove(this.VehicleId); } } return(result); } return(new VehicleResult(vehicle.m_targetBuilding == 0 && this.Target == 0)); }
/// <summary> /// Sets the target and updates the games vehicle object. /// </summary> /// <param name="targetBuildingId">The target building identifier.</param> /// <param name="vehicle">The vehicle.</param> /// <param name="material">The material.</param> /// <returns>The result of the action.</returns> public VehicleResult SetTarget(ushort targetBuildingId, ref Vehicle vehicle, TransferManager.TransferReason?material) { if (targetBuildingId == 0) { if (this.Target == 0 && vehicle.m_targetBuilding == 0) { return(new VehicleResult(true)); } return(this.DeAssign(ref vehicle, true, "SetTarget")); } if (Log.LogALot) { Log.DevDebug(this, "SetTarget", this.VehicleId, targetBuildingId, this.Target, vehicle.m_targetBuilding, vehicle.m_flags); } VehicleResult result = VehicleHelper.AssignTarget(this.VehicleId, ref vehicle, material, targetBuildingId, 0); if (result) { this.LastAssigned = Global.CurrentFrame; this.FreeToCollect = false; this.Target = targetBuildingId; } else { Log.Debug(this, "SetTarget", "Failed", this.VehicleId, targetBuildingId, this.Target, vehicle.m_targetBuilding, vehicle.m_flags); this.LastAssigned = 0; this.FreeToCollect = false; this.Target = 0; } return(result); }