|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ComponentEnergyManager : Component |
|
{ |
|
protected const float DEFAULT_UPDATE_INTERVAL = 15; |
|
protected static bool m_DebugPlugs = false; |
|
protected Shape m_DebugPlugArrow; |
|
|
|
protected bool m_IsSwichedOn; |
|
protected bool m_IsSwichedOnPreviousState; |
|
protected bool m_IsPassiveDevice; |
|
protected bool m_IsWorking; |
|
protected bool m_CanWork; |
|
protected bool m_CanStopWork; |
|
protected bool m_RestorePlugState; |
|
protected bool m_AutoSwitchOff; |
|
protected bool m_ShowSocketsInInventory; |
|
protected bool m_HasElectricityIcon; |
|
protected bool m_AutoSwitchOffWhenInCargo; |
|
protected bool m_IsPlugged; |
|
protected bool m_ConvertEnergyToQuantity; |
|
|
|
protected int m_MySocketID = -1; |
|
protected int m_PlugType; |
|
protected int m_EnergySourceStorageIDb1; |
|
protected int m_EnergySourceStorageIDb2; |
|
protected int m_EnergySourceStorageIDb3; |
|
protected int m_EnergySourceStorageIDb4; |
|
protected int m_AttachmentActionType; |
|
protected int m_EnergySourceNetworkIDLow = -1; |
|
protected int m_EnergySourceNetworkIDHigh = -1; |
|
|
|
protected float m_EnergyUsage; |
|
protected float m_Energy; |
|
protected float m_EnergyAtSpawn; |
|
protected float m_EnergyStorageMax; |
|
protected float m_ReduceMaxEnergyByDamageCoef; |
|
protected float m_SocketsCount; |
|
protected float m_CordLength; |
|
protected float m_LastUpdateTime; |
|
protected float m_WetnessExposure; |
|
protected float m_UpdateInterval; |
|
|
|
protected string m_CordTextureFile; |
|
|
|
|
|
protected static const string SOCKET_ = "socket_"; |
|
protected static const string _PLUGGED = "_plugged"; |
|
protected static const string _AVAILABLE = "_available"; |
|
static const string SEL_CORD_PLUGGED = "cord_plugged"; |
|
static const string SEL_CORD_FOLDED = "cord_folded"; |
|
|
|
protected ref TIntArray m_CompatiblePlugTypes; |
|
EntityAI m_EnergySource; |
|
ref array<EntityAI> m_PluggedDevices; |
|
ref map<string,EntityAI> m_DeviceByPlugSelection; |
|
|
|
ref Timer m_UpdateTimer; |
|
ref Timer m_UpdateQuantityTimer; |
|
ref Timer m_DebugUpdate; |
|
|
|
const int MAX_SOCKETS_COUNT = 4; |
|
EntityAI m_Sockets[MAX_SOCKETS_COUNT]; |
|
|
|
|
|
|
|
|
|
void ComponentEnergyManager() |
|
{ |
|
|
|
#ifndef DEVELOPER |
|
m_DebugPlugs = false; |
|
#endif |
|
} |
|
|
|
void ~ComponentEnergyManager() |
|
{ |
|
if (m_DebugPlugArrow) |
|
{ |
|
m_DebugPlugArrow.Destroy(); |
|
m_DebugPlugArrow = NULL; |
|
} |
|
} |
|
|
|
|
|
override void Event_OnInit() |
|
{ |
|
m_ThisEntityAI.m_EM = this; |
|
GetGame().GameScript.CallFunction(m_ThisEntityAI, "OnInitEnergy", NULL, 0); |
|
} |
|
|
|
|
|
void DebugUpdate() |
|
{ |
|
if ( GetDebugPlugs() ) |
|
{ |
|
if ( GetGame().IsMultiplayer() && GetGame().IsServer() ) |
|
{ |
|
if (m_DebugUpdate) |
|
m_DebugUpdate.Stop(); |
|
|
|
return; |
|
} |
|
|
|
if (m_DebugPlugArrow) |
|
{ |
|
m_DebugPlugArrow.Destroy(); |
|
m_DebugPlugArrow = NULL; |
|
} |
|
|
|
if ( GetEnergySource() ) |
|
{ |
|
vector from = GetEnergySource().GetPosition() + "0 0.1 0"; |
|
vector to = m_ThisEntityAI.GetPosition() + "0 0.1 0"; |
|
|
|
|
|
if ( vector.DistanceSq(from, to) == 0 ) |
|
return; |
|
|
|
if ( m_ThisEntityAI.GetType() == "BarbedWire" ) |
|
{ |
|
EntityAI BBB = m_ThisEntityAI.GetHierarchyParent(); |
|
|
|
if ( BBB && BBB.GetType() == "Fence" ) |
|
{ |
|
to = to + "0 -1.3 0"; |
|
} |
|
} |
|
|
|
m_DebugPlugArrow = DrawArrow( from, to ); |
|
} |
|
} |
|
} |
|
|
|
Shape DrawArrow(vector from, vector to, float size = 0.5, int color = 0xFFFFFFFF, float flags = 0) |
|
{ |
|
vector dir = to - from; |
|
dir.Normalize(); |
|
vector dir1 = dir * size; |
|
size = size * 0.5; |
|
|
|
vector dir2 = dir.Perpend() * size; |
|
|
|
vector pts[5]; |
|
pts[0] = from; |
|
pts[1] = to; |
|
pts[2] = to - dir1 - dir2; |
|
pts[3] = to - dir1 + dir2; |
|
pts[4] = to; |
|
|
|
return Shape.CreateLines(color, flags, pts, 5); |
|
} |
|
|
|
EntityAI GetThisEntityAI() |
|
{ |
|
return m_ThisEntityAI; |
|
} |
|
|
|
|
|
override void Event_OnAwake() |
|
{ |
|
string cfg_item = "CfgVehicles " + m_ThisEntityAI.GetType(); |
|
string cfg_energy_manager = cfg_item + " EnergyManager "; |
|
|
|
|
|
m_EnergyUsage = GetGame().ConfigGetFloat(cfg_energy_manager + "energyUsagePerSecond"); |
|
bool switch_on = GetGame().ConfigGetFloat(cfg_energy_manager + "switchOnAtSpawn"); |
|
m_AutoSwitchOff = GetGame().ConfigGetFloat(cfg_energy_manager + "autoSwitchOff"); |
|
m_HasElectricityIcon = GetGame().ConfigGetFloat(cfg_energy_manager + "hasIcon"); |
|
m_AutoSwitchOffWhenInCargo = GetGame().ConfigGetFloat(cfg_energy_manager + "autoSwitchOffWhenInCargo"); |
|
|
|
m_EnergyAtSpawn = GetGame().ConfigGetFloat(cfg_energy_manager + "energyAtSpawn"); |
|
m_Energy = m_EnergyAtSpawn; |
|
m_EnergyStorageMax = GetGame().ConfigGetFloat(cfg_energy_manager + "energyStorageMax"); |
|
m_ReduceMaxEnergyByDamageCoef = GetGame().ConfigGetFloat(cfg_energy_manager + "reduceMaxEnergyByDamageCoef"); |
|
m_SocketsCount = GetGame().ConfigGetFloat(cfg_energy_manager + "powerSocketsCount"); |
|
|
|
m_IsPassiveDevice = GetGame().ConfigGetFloat(cfg_energy_manager + "isPassiveDevice"); |
|
m_CordLength = GetGame().ConfigGetFloat(cfg_energy_manager + "cordLength"); |
|
m_PlugType = GetGame().ConfigGetFloat(cfg_energy_manager + "plugType"); |
|
|
|
m_AttachmentActionType = GetGame().ConfigGetFloat(cfg_energy_manager + "attachmentAction"); |
|
m_WetnessExposure = GetGame().ConfigGetFloat(cfg_energy_manager + "wetnessExposure"); |
|
|
|
float update_interval = GetGame().ConfigGetFloat(cfg_energy_manager + "updateInterval"); |
|
|
|
m_ConvertEnergyToQuantity = GetGame().ConfigGetFloat(cfg_energy_manager + "convertEnergyToQuantity"); |
|
|
|
|
|
|
|
float cfg_max_quantity = GetGame().ConfigGetFloat (cfg_item + " varQuantityMax"); |
|
|
|
if (m_ConvertEnergyToQuantity && cfg_max_quantity <= 0) |
|
{ |
|
string error = "Error! Item " + m_ThisEntityAI.GetType() + " has invalid configuration of the energy->quantity conversion feature. To fix this, add 'varQuantityMax' parameter with value higher than 0 to the item's config. Then make sure to re-build the PBO containing this item!"; |
|
Error(error); |
|
m_ConvertEnergyToQuantity = false; |
|
} |
|
else |
|
{ |
|
if (m_ConvertEnergyToQuantity) |
|
{ |
|
if (!m_UpdateQuantityTimer) |
|
m_UpdateQuantityTimer = new Timer( CALL_CATEGORY_SYSTEM ); |
|
|
|
m_UpdateQuantityTimer.Run( 0.3 , this, "OnEnergyAdded", NULL, false); |
|
} |
|
} |
|
|
|
|
|
if ( update_interval <= 0 ) |
|
update_interval = DEFAULT_UPDATE_INTERVAL; |
|
|
|
SetUpdateInterval( update_interval ); |
|
|
|
|
|
string cfg_check_energy_limit = cfg_energy_manager + "energyStorageMax"; |
|
|
|
if ( !GetGame().ConfigIsExisting (cfg_check_energy_limit) && m_Energy > 0 ) |
|
{ |
|
m_EnergyStorageMax = m_Energy; |
|
} |
|
|
|
|
|
string cfg_check_plug_types = cfg_energy_manager + "compatiblePlugTypes"; |
|
|
|
if ( GetGame().ConfigIsExisting (cfg_check_plug_types) ) |
|
{ |
|
m_CompatiblePlugTypes = new TIntArray; |
|
GetGame().ConfigGetIntArray(cfg_check_plug_types, m_CompatiblePlugTypes); |
|
} |
|
|
|
if (GetSocketsCount() > 0) |
|
m_PluggedDevices = new array<EntityAI>; |
|
|
|
if ( m_CordLength < 0 ) |
|
{ |
|
m_CordLength = 0; |
|
string error_message_cord = "Warning! " + m_ThisEntityAI.GetType() + ": config parameter 'cordLength' is less than 0! Cord length should not be negative!"; |
|
DPrint(error_message_cord); |
|
} |
|
|
|
if (GetSocketsCount() > 0) |
|
{ |
|
m_DeviceByPlugSelection = new map<string,EntityAI>; |
|
|
|
string cfg_animation_sources = "cfgVehicles " + m_ThisEntityAI.GetType() + " " + "AnimationSources "; |
|
int animation_sources_count = GetGame().ConfigGetChildrenCount(cfg_animation_sources); |
|
|
|
for (int i_selection = 0; i_selection < animation_sources_count; i_selection++) |
|
{ |
|
|
|
string selection; |
|
GetGame().ConfigGetChildName(cfg_animation_sources, i_selection, selection); |
|
m_DeviceByPlugSelection.Set(selection, NULL); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
if ( m_SocketsCount > MAX_SOCKETS_COUNT ) |
|
{ |
|
m_SocketsCount = MAX_SOCKETS_COUNT; |
|
string error_message_sockets = "Error! " + m_ThisEntityAI.GetType() + ": config parameter 'powerSocketsCount' is higher than the current limit (" + MAX_SOCKETS_COUNT.ToString() + ")! Raise the limit (constant MAX_SOCKETS_COUNT) or decrease the powerSocketsCount parameter for this device!"; |
|
DPrint(error_message_sockets); |
|
} |
|
|
|
m_Sockets[MAX_SOCKETS_COUNT]; |
|
|
|
GetGame().ConfigGetText(cfg_energy_manager + "cordTextureFile", m_CordTextureFile); |
|
|
|
if ( switch_on ) |
|
{ |
|
SwitchOn(); |
|
} |
|
|
|
for ( int i = 0; i <= GetSocketsCount(); ++i ) |
|
{ |
|
m_ThisEntityAI.HideSelection ( SOCKET_ + i.ToString() + _PLUGGED ); |
|
} |
|
|
|
|
|
m_ShowSocketsInInventory = false; |
|
if ( GetSocketsCount() > 0 && IsPlugCompatible(PLUG_COMMON_APPLIANCE) && m_ThisEntityAI.GetType() != "MetalWire" ) |
|
{ |
|
m_ShowSocketsInInventory = true; |
|
} |
|
|
|
m_CanWork = HasEnoughStoredEnergy(); |
|
|
|
m_ThisEntityAI.HideSelection( SEL_CORD_PLUGGED ); |
|
|
|
|
|
#ifdef DIAG_DEVELOPER |
|
GetGame().m_EnergyManagerArray.Insert( this ); |
|
#endif |
|
} |
|
|
|
|
|
override int GetCompType() |
|
{ |
|
return COMP_TYPE_ENERGY_MANAGER; |
|
} |
|
|
|
|
|
void OnDeviceDestroyed() |
|
{ |
|
bool was_working = m_ThisEntityAI.GetCompEM().IsWorking(); |
|
|
|
SwitchOff(); |
|
UnplugAllDevices(); |
|
UnplugThis(); |
|
SetPowered( false ); |
|
|
|
if ( was_working ) |
|
m_ThisEntityAI.OnWorkStop(); |
|
; |
|
} |
|
|
|
|
|
void RefreshDebug() |
|
{ |
|
if ( m_DebugPlugs ) |
|
{ |
|
if ( !m_DebugUpdate ) |
|
m_DebugUpdate = new Timer( CALL_CATEGORY_SYSTEM ); |
|
|
|
if ( !m_DebugUpdate.IsRunning() ) |
|
m_DebugUpdate.Run(0.01, this, "DebugUpdate", NULL, true); |
|
} |
|
else |
|
{ |
|
if ( m_DebugPlugArrow ) |
|
{ |
|
m_DebugPlugArrow.Destroy(); |
|
m_DebugPlugArrow = NULL; |
|
} |
|
} |
|
} |
|
|
|
bool GetDebugPlugs() |
|
{ |
|
return m_DebugPlugs; |
|
} |
|
|
|
void SetDebugPlugs( bool newVal ) |
|
{ |
|
m_DebugPlugs = newVal; |
|
RefreshDebug(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SwitchOn() |
|
{ |
|
m_IsSwichedOnPreviousState = m_IsSwichedOn; |
|
|
|
if (GetGame().IsServer() || !GetGame().IsMultiplayer()) |
|
{ |
|
if ( CanSwitchOn() ) |
|
{ |
|
m_IsSwichedOn = true; |
|
Synch(); |
|
|
|
DeviceUpdate(); |
|
StartUpdates(); |
|
|
|
|
|
WakeUpWholeBranch( m_ThisEntityAI ); |
|
|
|
UpdateCanWork(); |
|
|
|
|
|
GetGame().GameScript.CallFunction(m_ThisEntityAI, "OnSwitchOn", NULL, 0); |
|
} |
|
} |
|
|
|
if ( !GetGame().IsServer() && GetGame().IsMultiplayer()) |
|
{ |
|
GetGame().GameScript.CallFunction(m_ThisEntityAI, "OnSwitchOn", NULL, 0); |
|
} |
|
} |
|
|
|
|
|
void SwitchOff() |
|
{ |
|
m_IsSwichedOnPreviousState = m_IsSwichedOn; |
|
|
|
if (GetGame().IsServer() || !GetGame().IsMultiplayer()) |
|
{ |
|
if ( CanSwitchOff() ) |
|
{ |
|
m_IsSwichedOn = false; |
|
Synch(); |
|
|
|
if ( IsWorking() ) |
|
{ |
|
StopUpdates(); |
|
DeviceUpdate(); |
|
} |
|
|
|
|
|
WakeUpWholeBranch( m_ThisEntityAI ); |
|
|
|
UpdateCanWork(); |
|
|
|
|
|
GetGame().GameScript.CallFunction(m_ThisEntityAI, "OnSwitchOff", NULL, 0); |
|
} |
|
} |
|
|
|
if ( !GetGame().IsServer() && GetGame().IsMultiplayer() ) |
|
{ |
|
m_IsSwichedOn = false; |
|
GetGame().GameScript.CallFunction(m_ThisEntityAI, "OnSwitchOff", NULL, 0); |
|
} |
|
} |
|
|
|
|
|
void SetPassiveState(bool state = true) |
|
{ |
|
m_IsPassiveDevice = state; |
|
if ( !m_IsPassiveDevice ) |
|
{ |
|
DeviceUpdate(); |
|
} |
|
} |
|
|
|
|
|
void UnplugDevice(EntityAI device_to_unplug) |
|
{ |
|
if ( GetGame() ) |
|
{ |
|
int indexStart = GetPluggedDevicesCount() - 1; |
|
bool deviceFound = false; |
|
|
|
for (int i = indexStart; i >= 0; --i) |
|
{ |
|
EntityAI plugged_device = GetPluggedDevices().Get(i); |
|
|
|
if (plugged_device == device_to_unplug) |
|
{ |
|
GetPluggedDevices().Remove(i); |
|
deviceFound = true; |
|
break; |
|
} |
|
} |
|
|
|
if (deviceFound) |
|
{ |
|
int socket_ID = device_to_unplug.GetCompEM().GetMySocketID(); |
|
UnplugCordFromSocket(socket_ID); |
|
device_to_unplug.GetCompEM().SetEnergySource(null); |
|
device_to_unplug.GetCompEM().DeviceUpdate(); |
|
device_to_unplug.GetCompEM().StartUpdates(); |
|
device_to_unplug.GetCompEM().WakeUpWholeBranch(m_ThisEntityAI); |
|
|
|
if (m_DebugPlugs && m_DebugPlugArrow) |
|
{ |
|
m_DebugPlugArrow.Destroy(); |
|
m_DebugPlugArrow = null; |
|
} |
|
|
|
OnOwnSocketReleased(device_to_unplug); |
|
device_to_unplug.GetCompEM().OnIsUnplugged(m_ThisEntityAI); |
|
device_to_unplug.ShowSelection(SEL_CORD_FOLDED); |
|
device_to_unplug.HideSelection(SEL_CORD_PLUGGED); |
|
} |
|
} |
|
} |
|
|
|
|
|
void UnplugThis() |
|
{ |
|
if (GetGame()) |
|
{ |
|
if (GetEnergySource()) |
|
{ |
|
GetEnergySource().GetCompEM().UnplugDevice(m_ThisEntityAI); |
|
} |
|
} |
|
} |
|
|
|
|
|
void UnplugAllDevices() |
|
{ |
|
if ( GetPluggedDevices() ) |
|
{ |
|
int indexStart = GetPluggedDevicesCount() - 1; |
|
for (int i = indexStart; i >= 0; --i) |
|
{ |
|
UnplugDevice(GetPluggedDevices().Get(i)); |
|
} |
|
} |
|
} |
|
|
|
|
|
void RestorePlugState(bool state) |
|
{ |
|
m_RestorePlugState = state; |
|
} |
|
|
|
|
|
void SetEnergy(float new_energy) |
|
{ |
|
if (GetGame().IsServer() || !GetGame().IsMultiplayer()) |
|
{ |
|
m_ThisEntityAI.SetWeightDirty(); |
|
float old_energy = m_Energy; |
|
m_Energy = new_energy; |
|
|
|
if ( old_energy - GetEnergyUsage() <= 0 || (old_energy != new_energy && Math.Min(old_energy,new_energy) <= 0) ) |
|
{ |
|
UpdateCanWork(); |
|
} |
|
} |
|
} |
|
|
|
|
|
void SetEnergy0To1(float energy01) |
|
{ |
|
SetEnergy( Math.Lerp(0, GetEnergyMax(),energy01)); |
|
} |
|
|
|
|
|
void UpdateSelections() |
|
{ |
|
|
|
int slots_c = GetSocketsCount(); |
|
|
|
for ( int i = 0; i < slots_c; ++i ) |
|
{ |
|
EntityAI plug_owner = GetDeviceBySocketID(i); |
|
|
|
if ( plug_owner ) |
|
{ |
|
string plugged_selection = SOCKET_ + (i+1).ToString() + _PLUGGED; |
|
string available_selection = SOCKET_ + (i+1).ToString() + _AVAILABLE; |
|
m_ThisEntityAI.ShowSelection ( plugged_selection ); |
|
m_ThisEntityAI.HideSelection ( available_selection ); |
|
string texture_path = plug_owner.GetCompEM().GetCordTextureFile(); |
|
int selection_index = m_ThisEntityAI.GetHiddenSelectionIndex( plugged_selection ); |
|
m_ThisEntityAI.SetObjectTexture(selection_index, texture_path ); |
|
} |
|
else |
|
{ |
|
m_ThisEntityAI.ShowSelection ( SOCKET_ + (i+1).ToString() + _AVAILABLE ); |
|
m_ThisEntityAI.HideSelection ( SOCKET_ + (i+1).ToString() + _PLUGGED ); |
|
} |
|
} |
|
|
|
|
|
if ( GetEnergySource() ) |
|
{ |
|
m_ThisEntityAI.ShowSelection ( SEL_CORD_PLUGGED ); |
|
m_ThisEntityAI.HideSelection ( SEL_CORD_FOLDED ); |
|
} |
|
else |
|
{ |
|
m_ThisEntityAI.ShowSelection ( SEL_CORD_FOLDED ); |
|
m_ThisEntityAI.HideSelection ( SEL_CORD_PLUGGED ); |
|
} |
|
} |
|
|
|
|
|
void UpdatePlugState() |
|
{ |
|
if (m_ThisEntityAI.GetCompEM().GetEnergySource()) |
|
{ |
|
EntityAI player = m_ThisEntityAI.GetHierarchyRootPlayer(); |
|
|
|
if (player) |
|
{ |
|
|
|
vector playerPosition = player.GetPosition(); |
|
if (!IsEnergySourceAtReach(playerPosition, 5)) |
|
UnplugThis(); |
|
} |
|
else |
|
{ |
|
|
|
vector itemPosition = m_ThisEntityAI.GetPosition(); |
|
|
|
if (m_ThisEntityAI.GetHierarchyParent()) |
|
itemPosition = m_ThisEntityAI.GetHierarchyParent().GetPosition(); |
|
|
|
if (!IsEnergySourceAtReach(itemPosition)) |
|
UnplugThis(); |
|
} |
|
} |
|
} |
|
|
|
|
|
void GetCompatiblePlugTypes(out TIntArray IDs) |
|
{ |
|
IDs = m_CompatiblePlugTypes; |
|
} |
|
|
|
|
|
void StoreEnergySourceIDs(int b1, int b2, int b3, int b4) |
|
{ |
|
m_EnergySourceStorageIDb1 = b1; |
|
m_EnergySourceStorageIDb2 = b2; |
|
m_EnergySourceStorageIDb3 = b3; |
|
m_EnergySourceStorageIDb4 = b4; |
|
} |
|
|
|
|
|
void SetEnergyMaxPristine(float new_limit) |
|
{ |
|
m_EnergyStorageMax = new_limit; |
|
} |
|
|
|
|
|
void SetCordLength( float new_length ) |
|
{ |
|
m_CordLength = new_length; |
|
} |
|
|
|
|
|
void SetPlugType( int new_type ) |
|
{ |
|
m_PlugType = new_type; |
|
} |
|
|
|
|
|
void SetAttachmentAction( int new_action_type ) |
|
{ |
|
m_AttachmentActionType = new_action_type; |
|
} |
|
|
|
|
|
void SetEnergyUsage( float new_usage ) |
|
{ |
|
m_EnergyUsage = new_usage; |
|
} |
|
|
|
|
|
void ResetEnergyUsage() |
|
{ |
|
string cfg_energy_usage = "CfgVehicles " + m_ThisEntityAI.GetType() + " EnergyManager "; |
|
m_EnergyUsage = GetGame().ConfigGetFloat (cfg_energy_usage + "energyUsagePerSecond"); |
|
} |
|
|
|
|
|
void SetCordTextureFile( string new_path ) |
|
{ |
|
m_CordTextureFile = new_path; |
|
} |
|
|
|
|
|
void SetEnergySourceClient( EntityAI source ) |
|
{ |
|
SetEnergySource(source); |
|
} |
|
|
|
|
|
void SetDeviceBySocketID(int id, EntityAI plugged_device) |
|
{ |
|
m_Sockets[id] = plugged_device; |
|
} |
|
|
|
|
|
|
|
void SetElectricityIconVisibility( bool make_visible ) |
|
{ |
|
m_HasElectricityIcon = make_visible; |
|
} |
|
|
|
|
|
void UpdateCanWork() |
|
{ |
|
if (GetGame().IsServer() || !GetGame().IsMultiplayer()) |
|
{ |
|
bool current_state = CanWork(); |
|
|
|
if (current_state != m_CanWork) |
|
{ |
|
m_CanWork = current_state; |
|
Synch(); |
|
|
|
if ( m_ThisEntityAI && m_ThisEntityAI.GetHierarchyParent() && m_ThisEntityAI.GetHierarchyParent().GetCompEM() ) |
|
{ |
|
m_ThisEntityAI.GetHierarchyParent().GetCompEM().UpdateCanWork(); |
|
} |
|
} |
|
} |
|
} |
|
|
|
void HandleMoveInsideCargo(EntityAI container) |
|
{ |
|
if ( m_AutoSwitchOffWhenInCargo ) |
|
{ |
|
if (IsSwitchedOn()) |
|
{ |
|
SwitchOff(); |
|
} |
|
} |
|
} |
|
|
|
|
|
void SetUpdateInterval( float value ) |
|
{ |
|
m_UpdateInterval = value; |
|
} |
|
|
|
|
|
bool GetRestorePlugState() |
|
{ |
|
return m_RestorePlugState; |
|
} |
|
|
|
|
|
bool PlugThisInto(EntityAI energy_source, int socket_id = -1) |
|
{ |
|
return energy_source.GetCompEM().PlugInDevice(m_ThisEntityAI, socket_id); |
|
} |
|
|
|
|
|
bool CanSwitchOn() |
|
{ |
|
if ( !IsSwitchedOn() ) |
|
{ |
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CanWork( float test_energy = -1) |
|
{ |
|
if ( GetGame().IsMultiplayer() && GetGame().IsClient() ) |
|
{ |
|
return m_CanWork; |
|
} |
|
|
|
if (m_ThisEntityAI && m_ThisEntityAI.IsRuined()) |
|
{ |
|
return false; |
|
} |
|
|
|
|
|
float energy_usage = test_energy; |
|
float gathered_energy = GetEnergy(); |
|
EntityAI energy_source = GetEnergySource(); |
|
|
|
if (energy_usage == -1) |
|
{ |
|
energy_usage = GetEnergyUsage(); |
|
} |
|
|
|
if ( !CheckWetness() ) |
|
{ |
|
return false; |
|
} |
|
|
|
if (gathered_energy <= 0 && energy_usage <= 0) |
|
{ |
|
return false; |
|
} |
|
|
|
int cycle_limit = 500; |
|
|
|
while ( gathered_energy < energy_usage ) |
|
{ |
|
|
|
if (cycle_limit > 0) |
|
{ |
|
cycle_limit--; |
|
} |
|
else |
|
{ |
|
DPrint("Energy Manager ERROR: The 'cycle_limit' safety break had to be activated to prevent possible game freeze. Dumping debug information..."); |
|
|
|
|
|
|
|
|
|
if (energy_source.GetCompEM()) |
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
if (energy_source) |
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
} |
|
|
|
|
|
if ( energy_source && energy_source != m_ThisEntityAI && !energy_source.IsRuined() && energy_source.GetCompEM() && energy_source.GetCompEM().IsSwitchedOn() && energy_source.GetCompEM().CheckWetness() ) |
|
{ |
|
gathered_energy = gathered_energy + energy_source.GetCompEM().GetEnergy(); |
|
energy_source = energy_source.GetCompEM().GetEnergySource(); |
|
} |
|
else |
|
{ |
|
|
|
return false; |
|
} |
|
} |
|
|
|
|
|
return true; |
|
} |
|
|
|
|
|
bool CheckWetness() |
|
{ |
|
return (m_ThisEntityAI.GetWet() <= 1-m_WetnessExposure); |
|
} |
|
|
|
|
|
bool CanSwitchOff() |
|
{ |
|
if ( IsPassive() ) |
|
{ |
|
return false; |
|
} |
|
|
|
return IsSwitchedOn(); |
|
} |
|
|
|
|
|
bool GetPreviousSwitchState() |
|
{ |
|
return m_IsSwichedOnPreviousState; |
|
} |
|
|
|
|
|
bool IsSwitchedOn() |
|
{ |
|
return m_IsSwichedOn; |
|
} |
|
|
|
|
|
bool IsCordFolded() |
|
{ |
|
if ( IsPlugged() ) |
|
return false; |
|
|
|
return true; |
|
} |
|
|
|
|
|
bool IsPassive() |
|
{ |
|
return m_IsPassiveDevice; |
|
} |
|
|
|
|
|
bool IsPlugged() |
|
{ |
|
return m_IsPlugged; |
|
} |
|
|
|
|
|
|
|
bool ConsumeEnergy(float amount) |
|
{ |
|
return FindAndConsumeEnergy(m_ThisEntityAI, amount, true); |
|
} |
|
|
|
|
|
bool IsWorking() |
|
{ |
|
return m_IsWorking; |
|
} |
|
|
|
|
|
bool HasEnoughStoredEnergy() |
|
{ |
|
if ( GetEnergy() > GetEnergyUsage() ) |
|
{ |
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
bool HasFreeSocket( int socket_id = -1 ) |
|
{ |
|
if (socket_id == -1) |
|
{ |
|
int plugged_devices = GetPluggedDevicesCount(); |
|
int plugged_devices_limit = GetSocketsCount(); |
|
|
|
if ( plugged_devices < plugged_devices_limit ) |
|
{ |
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
else |
|
{ |
|
EntityAI device = GetDeviceBySocketID(socket_id); |
|
|
|
if (device) |
|
{ |
|
return false; |
|
} |
|
else |
|
{ |
|
return true; |
|
} |
|
} |
|
} |
|
|
|
|
|
bool IsPlugCompatible(int plug_ID) |
|
{ |
|
if ( plug_ID == PLUG_UNDEFINED ) |
|
{ |
|
return true; |
|
} |
|
|
|
if ( m_CompatiblePlugTypes ) |
|
{ |
|
for ( int i = 0; i < m_CompatiblePlugTypes.Count(); i++ ) |
|
{ |
|
int plug_ID_to_Check = m_CompatiblePlugTypes.Get(i); |
|
|
|
if ( plug_ID_to_Check == plug_ID ) |
|
{ |
|
return true; |
|
} |
|
} |
|
} |
|
else |
|
{ |
|
|
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
bool CanReceivePlugFrom( EntityAI device_to_plug ) |
|
{ |
|
|
|
|
|
if ( HasFreeSocket() && device_to_plug != m_ThisEntityAI) |
|
{ |
|
if ( device_to_plug.GetCompEM().GetEnergySource() != m_ThisEntityAI) |
|
{ |
|
if ( IsPlugCompatible(device_to_plug.GetCompEM().GetPlugType()) ) |
|
{ |
|
if ( device_to_plug.GetCompEM().IsEnergySourceAtReach( device_to_plug.GetPosition(), 0, m_ThisEntityAI.GetPosition() ) ) |
|
{ |
|
return true; |
|
} |
|
} |
|
} |
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
bool CanBePluggedInto( EntityAI potential_energy_provider ) |
|
{ |
|
return potential_energy_provider.GetCompEM().CanReceivePlugFrom( m_ThisEntityAI ); |
|
} |
|
|
|
|
|
bool HasElectricityIcon() |
|
{ |
|
return m_HasElectricityIcon; |
|
} |
|
|
|
|
|
bool HasConversionOfEnergyToQuantity() |
|
{ |
|
return m_ConvertEnergyToQuantity; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool IsEnergySourceAtReach( vector from_position, float add_tolerance = 0, vector override_source_position = "-1 -1 -1" ) |
|
{ |
|
if ( !IsPlugged() && override_source_position == "-1 -1 -1" ) |
|
{ |
|
return false; |
|
} |
|
|
|
if ( GetCordLength() == 0 ) |
|
{ |
|
return true; |
|
} |
|
|
|
vector source_pos; |
|
float distance; |
|
|
|
if ( override_source_position == "-1 -1 -1" ) |
|
{ |
|
EntityAI energy_source = GetEnergySource(); |
|
|
|
if (!energy_source) |
|
return false; |
|
|
|
source_pos = energy_source.GetPosition(); |
|
distance = vector.Distance( from_position, source_pos ); |
|
} |
|
else |
|
{ |
|
source_pos = override_source_position; |
|
distance = vector.Distance( from_position, source_pos ); |
|
} |
|
|
|
if (distance > GetCordLength() + add_tolerance) |
|
{ |
|
return false; |
|
} |
|
else |
|
{ |
|
return true; |
|
} |
|
} |
|
|
|
bool HasVisibleSocketsInInventory() |
|
{ |
|
return m_ShowSocketsInInventory; |
|
} |
|
|
|
|
|
bool IsSelectionAPlug(string selection_to_test ) |
|
{ |
|
if ( GetPluggedDevices() ) |
|
{ |
|
int socket_count = GetSocketsCount(); |
|
|
|
for ( int i = socket_count; i >= 0; --i ) |
|
{ |
|
string real_selection = SOCKET_ + i.ToString() +_PLUGGED; |
|
|
|
if ( selection_to_test == real_selection) |
|
{ |
|
return true; |
|
} |
|
} |
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
int GetSocketsCount() |
|
{ |
|
return m_SocketsCount; |
|
} |
|
|
|
|
|
int GetPlugType() |
|
{ |
|
return m_PlugType; |
|
} |
|
|
|
|
|
int GetAttachmentAction() |
|
{ |
|
return m_AttachmentActionType; |
|
} |
|
|
|
|
|
int GetEnergySourceStorageIDb1() |
|
{ |
|
return m_EnergySourceStorageIDb1; |
|
} |
|
|
|
|
|
int GetEnergySourceStorageIDb2() |
|
{ |
|
return m_EnergySourceStorageIDb2; |
|
} |
|
|
|
|
|
int GetEnergySourceStorageIDb3() |
|
{ |
|
return m_EnergySourceStorageIDb3; |
|
} |
|
|
|
|
|
int GetEnergySourceStorageIDb4() |
|
{ |
|
return m_EnergySourceStorageIDb4; |
|
} |
|
|
|
|
|
int GetEnergySourceNetworkIDLow() |
|
{ |
|
return m_EnergySourceNetworkIDLow; |
|
} |
|
|
|
|
|
int GetEnergySourceNetworkIDHigh() |
|
{ |
|
return m_EnergySourceNetworkIDHigh; |
|
} |
|
|
|
|
|
int GetPluggedDevicesCount() |
|
{ |
|
if ( GetPluggedDevices() ) |
|
{ |
|
return GetPluggedDevices().Count(); |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
|
|
int GetEnergy0To100() |
|
{ |
|
if ( m_EnergyStorageMax > 0 ) |
|
{ |
|
int coef = Math.Round( m_Energy / m_EnergyStorageMax * 100 ); |
|
return coef; |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
|
|
float GetEnergy0To1() |
|
{ |
|
if ( m_EnergyStorageMax > 0 ) |
|
{ |
|
return m_Energy / m_EnergyStorageMax; |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
|
|
float GetUpdateInterval() |
|
{ |
|
#ifdef DIAG_DEVELOPER |
|
if (FeatureTimeAccel.GetFeatureTimeAccelEnabled(ETimeAccelCategories.ENERGY_CONSUMPTION) || (FeatureTimeAccel.GetFeatureTimeAccelEnabled(ETimeAccelCategories.ENERGY_RECHARGE))) |
|
{ |
|
return 1; |
|
} |
|
#endif |
|
return m_UpdateInterval; |
|
} |
|
|
|
|
|
float GetWetnessExposure() |
|
{ |
|
return m_WetnessExposure; |
|
} |
|
|
|
|
|
float GetEnergyUsage() |
|
{ |
|
return m_EnergyUsage; |
|
} |
|
|
|
|
|
float GetEnergy() |
|
{ |
|
return m_Energy; |
|
} |
|
|
|
|
|
float AddEnergy(float added_energy) |
|
{ |
|
if (added_energy != 0) |
|
{ |
|
|
|
#ifdef DIAG_DEVELOPER |
|
if (FeatureTimeAccel.GetFeatureTimeAccelEnabled(ETimeAccelCategories.ENERGY_CONSUMPTION) && added_energy < 0) |
|
{ |
|
float timeAccel = FeatureTimeAccel.GetFeatureTimeAccelValue(); |
|
added_energy *= timeAccel; |
|
} |
|
#endif |
|
|
|
bool energy_was_added = (added_energy > 0); |
|
|
|
float energy_to_clamp = GetEnergy() + added_energy; |
|
float clamped_energy = Math.Clamp( energy_to_clamp, 0, GetEnergyMax() ); |
|
SetEnergy(clamped_energy); |
|
StartUpdates(); |
|
|
|
if (energy_was_added) |
|
OnEnergyAdded(); |
|
else |
|
OnEnergyConsumed(); |
|
|
|
return energy_to_clamp - clamped_energy; |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
|
|
float GetEnergyMax() |
|
{ |
|
float max_health = 0; |
|
|
|
if ( m_ThisEntityAI.HasDamageSystem() ) |
|
max_health = m_ThisEntityAI.GetMaxHealth("",""); |
|
|
|
|
|
|
|
if ( max_health == 0 || m_ReduceMaxEnergyByDamageCoef == 0 ) |
|
return GetEnergyMaxPristine(); |
|
|
|
float health = 100; |
|
|
|
if (GetGame().IsServer() || !GetGame().IsMultiplayer()) |
|
health = m_ThisEntityAI.GetHealth("",""); |
|
|
|
float damage_coef = 1 - (health / max_health); |
|
|
|
return GetEnergyMaxPristine() * (1 - ( damage_coef * m_ReduceMaxEnergyByDamageCoef ) ); |
|
} |
|
|
|
|
|
float GetEnergyMaxPristine() |
|
{ |
|
return m_EnergyStorageMax; |
|
} |
|
|
|
float GetEnergyAtSpawn() |
|
{ |
|
return m_EnergyAtSpawn; |
|
} |
|
|
|
|
|
float GetCordLength() |
|
{ |
|
return m_CordLength; |
|
} |
|
|
|
|
|
EntityAI GetEnergySource() |
|
{ |
|
return m_EnergySource; |
|
} |
|
|
|
|
|
EntityAI GetDeviceBySocketID(int id) |
|
{ |
|
return m_Sockets[id]; |
|
} |
|
|
|
|
|
EntityAI GetPlugOwner(string plug_selection_name) |
|
{ |
|
if ( m_DeviceByPlugSelection.Contains(plug_selection_name) ) |
|
{ |
|
return m_DeviceByPlugSelection.Get(plug_selection_name); |
|
} |
|
|
|
return NULL; |
|
} |
|
|
|
|
|
EntityAI GetPluggedDevice() |
|
{ |
|
if ( GetPluggedDevicesCount() > 0 ) |
|
{ |
|
return GetPluggedDevices().Get(0); |
|
} |
|
|
|
return NULL; |
|
} |
|
|
|
|
|
string GetCordTextureFile() |
|
{ |
|
return m_CordTextureFile; |
|
} |
|
|
|
|
|
array<EntityAI> GetPluggedDevices() |
|
{ |
|
return m_PluggedDevices; |
|
} |
|
|
|
|
|
array<EntityAI> GetPoweredDevices() |
|
{ |
|
array<EntityAI> return_array = new array<EntityAI>; |
|
int plugged_devices_c = GetPluggedDevicesCount(); |
|
for ( int i = 0; i < plugged_devices_c; ++i ) |
|
{ |
|
EntityAI device = GetPluggedDevices().Get(i); |
|
if ( IsSwitchedOn() ) |
|
{ |
|
return_array.Insert(device); |
|
} |
|
} |
|
|
|
return return_array; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OnWork( float consumed_energy ) |
|
{ |
|
m_ThisEntityAI.OnWork(consumed_energy); |
|
} |
|
|
|
|
|
void OnIsPlugged(EntityAI source_device) |
|
{ |
|
if (m_DebugPlugs) |
|
{ |
|
if (!m_DebugUpdate) |
|
m_DebugUpdate = new Timer( CALL_CATEGORY_SYSTEM ); |
|
|
|
if (!m_DebugUpdate.IsRunning()) |
|
m_DebugUpdate.Run(0.01, this, "DebugUpdate", NULL, true); |
|
} |
|
|
|
UpdateCanWork(); |
|
m_ThisEntityAI.OnIsPlugged(source_device); |
|
} |
|
|
|
|
|
void OnIsUnplugged( EntityAI last_energy_source ) |
|
{ |
|
UpdateCanWork(); |
|
m_ThisEntityAI.OnIsUnplugged( last_energy_source ); |
|
} |
|
|
|
|
|
void OnOwnSocketTaken( EntityAI device ) |
|
{ |
|
|
|
if ( device.GetCompEM().GetPlugType() == PLUG_COMMON_APPLIANCE && m_ThisEntityAI.IsInitialized() ) |
|
{ |
|
EffectSound sound_plug; |
|
m_ThisEntityAI.PlaySoundSet( sound_plug, "cablereel_plugin_SoundSet", 0, 0 ); |
|
} |
|
|
|
m_ThisEntityAI.OnOwnSocketTaken(device); |
|
} |
|
|
|
|
|
void OnOwnSocketReleased( EntityAI device ) |
|
{ |
|
|
|
if ( device.GetCompEM().GetPlugType() == PLUG_COMMON_APPLIANCE && m_ThisEntityAI.IsInitialized() ) |
|
{ |
|
EffectSound sound_unplug; |
|
m_ThisEntityAI.PlaySoundSet( sound_unplug, "cablereel_unplug_SoundSet", 0, 0 ); |
|
} |
|
|
|
m_ThisEntityAI.OnOwnSocketReleased( device ); |
|
} |
|
|
|
|
|
|
|
void OnAttachmentAdded(EntityAI elec_device) |
|
{ |
|
int attachment_action_type = GetAttachmentAction(); |
|
|
|
if ( attachment_action_type == PLUG_THIS_INTO_ATTACHMENT ) |
|
{ |
|
if ( elec_device.GetCompEM().CanReceivePlugFrom( m_ThisEntityAI ) ) |
|
{ |
|
PlugThisInto(elec_device); |
|
} |
|
} |
|
else if ( attachment_action_type == PLUG_ATTACHMENTS_INTO_THIS ) |
|
{ |
|
elec_device.GetCompEM().PlugThisInto(m_ThisEntityAI); |
|
} |
|
} |
|
|
|
|
|
void OnAttachmentRemoved(EntityAI elec_device) |
|
{ |
|
int attachment_action_type = GetAttachmentAction(); |
|
|
|
if ( attachment_action_type == PLUG_THIS_INTO_ATTACHMENT ) |
|
{ |
|
if ( elec_device == GetEnergySource() ) |
|
{ |
|
UnplugThis(); |
|
} |
|
} |
|
else if ( attachment_action_type == PLUG_ATTACHMENTS_INTO_THIS ) |
|
{ |
|
elec_device.GetCompEM().UnplugThis(); |
|
} |
|
} |
|
|
|
|
|
void StartUpdates() |
|
{ |
|
if (!m_IsPassiveDevice) |
|
{ |
|
if (!m_UpdateTimer) |
|
m_UpdateTimer = new Timer(CALL_CATEGORY_SYSTEM); |
|
|
|
if (!m_UpdateTimer.IsRunning()) |
|
{ |
|
m_UpdateTimer.Run(GetUpdateInterval(), this, "DeviceUpdate", null, true); |
|
} |
|
} |
|
} |
|
|
|
|
|
void OnEnergyConsumed() |
|
{ |
|
m_ThisEntityAI.OnEnergyConsumed(); |
|
} |
|
|
|
|
|
void OnEnergyAdded() |
|
{ |
|
if (m_UpdateQuantityTimer) |
|
{ |
|
m_UpdateQuantityTimer.Stop(); |
|
m_UpdateQuantityTimer = NULL; |
|
} |
|
|
|
m_ThisEntityAI.OnEnergyAdded(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void StopUpdates() |
|
{ |
|
if (m_UpdateTimer) |
|
{ |
|
m_UpdateTimer.Stop(); |
|
m_UpdateTimer = NULL; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
void InteractBranch(EntityAI originalCaller, Man player = null, int system = 0) |
|
{ |
|
OnInteractBranch(originalCaller, player, system); |
|
if ( GetSocketsCount() > 0 ) |
|
{ |
|
array<EntityAI> devices = GetPluggedDevices(); |
|
|
|
foreach ( EntityAI device : devices) |
|
{ |
|
if ( device != originalCaller ) |
|
{ |
|
device.GetCompEM().InteractBranch( originalCaller, player, system ); |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
protected void OnInteractBranch(EntityAI originalCaller, Man player, int system) |
|
{ |
|
m_ThisEntityAI.IncreaseLifetime(); |
|
|
|
} |
|
|
|
|
|
protected void WakeUpWholeBranch( EntityAI original_caller ) |
|
{ |
|
if ( GetSocketsCount() > 0 ) |
|
{ |
|
array<EntityAI> plugged_devices = GetPluggedDevices(); |
|
int plugged_devices_c = plugged_devices.Count(); |
|
|
|
for ( int i = 0; i < plugged_devices_c; ++i ) |
|
{ |
|
EntityAI device = plugged_devices.Get(i); |
|
if ( device != original_caller ) |
|
{ |
|
device.GetCompEM().UpdateCanWork(); |
|
device.GetCompEM().DeviceUpdate(); |
|
device.GetCompEM().StartUpdates(); |
|
device.GetCompEM().WakeUpWholeBranch( original_caller ); |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
protected void PlugCordIntoSocket( EntityAI device_to_plug, int socket_id = -1 ) |
|
{ |
|
if (socket_id >= 0) |
|
{ |
|
EntityAI plug_owner_by_socket = GetDeviceBySocketID(socket_id); |
|
|
|
if (!plug_owner_by_socket) |
|
{ |
|
UpdateSocketSelections(socket_id, device_to_plug); |
|
return; |
|
} |
|
} |
|
|
|
int slots_c = GetSocketsCount(); |
|
|
|
for ( int i = 0; i < slots_c; ++i ) |
|
{ |
|
EntityAI plug_owner = GetDeviceBySocketID(i); |
|
|
|
if ( !plug_owner ) |
|
{ |
|
UpdateSocketSelections(i, device_to_plug); |
|
break; |
|
} |
|
} |
|
} |
|
|
|
|
|
protected void UpdateSocketSelections(int socket_id, EntityAI device_to_plug) |
|
{ |
|
SetDeviceBySocketID(socket_id, device_to_plug); |
|
|
|
string plugged_selection = SOCKET_ + (socket_id+1).ToString() + _PLUGGED; |
|
SetPlugOwner( plugged_selection, device_to_plug ); |
|
m_ThisEntityAI.ShowSelection ( plugged_selection ); |
|
|
|
string unplugged_selection = SOCKET_ + (socket_id+1).ToString() + _AVAILABLE; |
|
m_ThisEntityAI.HideSelection ( unplugged_selection ); |
|
string texture_path = device_to_plug.GetCompEM().GetCordTextureFile(); |
|
int selection_index = m_ThisEntityAI.GetHiddenSelectionIndex( plugged_selection ); |
|
m_ThisEntityAI.SetObjectTexture( selection_index, texture_path ); |
|
device_to_plug.GetCompEM().SetMySocketID(socket_id); |
|
} |
|
|
|
|
|
|
|
protected void SetEnergySource( EntityAI source ) |
|
{ |
|
m_EnergySource = source; |
|
|
|
if (source) |
|
{ |
|
m_IsPlugged = true; |
|
StartUpdates(); |
|
} |
|
else |
|
{ |
|
m_IsPlugged = false; |
|
m_EnergySourceNetworkIDLow = -1; |
|
m_EnergySourceNetworkIDHigh = -1; |
|
} |
|
|
|
if (m_EnergySource) |
|
m_EnergySource.GetNetworkID(m_EnergySourceNetworkIDLow, m_EnergySourceNetworkIDHigh); |
|
|
|
Synch(); |
|
} |
|
|
|
|
|
protected bool PlugInDevice(EntityAI device_to_plug, int socket_id = -1) |
|
{ |
|
if (CanReceivePlugFrom(device_to_plug)) |
|
{ |
|
device_to_plug.IncreaseLifetime(); |
|
InteractBranch(m_ThisEntityAI); |
|
if (device_to_plug.GetCompEM().IsPlugged()) |
|
device_to_plug.GetCompEM().UnplugThis(); |
|
|
|
GetPluggedDevices().Insert(device_to_plug); |
|
device_to_plug.GetCompEM().SetEnergySource(m_ThisEntityAI); |
|
|
|
PlugCordIntoSocket(device_to_plug, socket_id); |
|
OnOwnSocketTaken(device_to_plug); |
|
|
|
device_to_plug.GetCompEM().OnIsPlugged(m_ThisEntityAI); |
|
WakeUpWholeBranch( m_ThisEntityAI ); |
|
|
|
if (GetGame().IsServer() || !GetGame().IsMultiplayer()) |
|
{ |
|
device_to_plug.HideSelection(SEL_CORD_FOLDED); |
|
device_to_plug.ShowSelection(SEL_CORD_PLUGGED); |
|
} |
|
|
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
protected void SetPlugOwner(string selection_name, EntityAI device) |
|
{ |
|
if ( m_DeviceByPlugSelection.Contains(selection_name) ) |
|
{ |
|
m_DeviceByPlugSelection.Set(selection_name, device); |
|
} |
|
} |
|
|
|
|
|
|
|
protected void UnplugCordFromSocket( int socket_to_unplug_ID ) |
|
{ |
|
EntityAI plug_owner = GetDeviceBySocketID(socket_to_unplug_ID); |
|
|
|
if ( plug_owner ) |
|
{ |
|
SetDeviceBySocketID(socket_to_unplug_ID, NULL); |
|
string unplugged_selection = SOCKET_ + (socket_to_unplug_ID+1).ToString() + _AVAILABLE; |
|
m_ThisEntityAI.ShowSelection ( unplugged_selection ); |
|
|
|
string plugged_selection = SOCKET_ + (socket_to_unplug_ID+1).ToString() + _PLUGGED; |
|
m_ThisEntityAI.HideSelection ( plugged_selection ); |
|
SetPlugOwner( plugged_selection, NULL ); |
|
plug_owner.GetCompEM().SetMySocketID(-1); |
|
} |
|
} |
|
|
|
|
|
protected void SetPowered( bool state ) |
|
{ |
|
m_IsWorking = state; |
|
} |
|
|
|
|
|
protected bool FindAndConsumeEnergy(EntityAI original_caller, float amount, bool ignore_switch_state = false) |
|
{ |
|
if ( (ignore_switch_state || IsSwitchedOn()) && !m_ThisEntityAI.IsRuined() ) |
|
{ |
|
float available_energy = AddEnergy(-amount); |
|
|
|
if ( available_energy < 0 && IsPlugged() ) |
|
{ |
|
|
|
EntityAI next_power_source = GetEnergySource(); |
|
|
|
if (next_power_source && next_power_source != original_caller) |
|
{ |
|
return next_power_source.GetCompEM().FindAndConsumeEnergy( original_caller, -available_energy ); |
|
} |
|
} |
|
|
|
if ( available_energy >= 0) |
|
{ |
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
else |
|
{ |
|
return false; |
|
} |
|
} |
|
|
|
|
|
protected int GetMySocketID() |
|
{ |
|
return m_MySocketID; |
|
} |
|
|
|
|
|
protected void SetMySocketID( int slot_ID ) |
|
{ |
|
m_MySocketID = slot_ID; |
|
} |
|
|
|
void Synch() |
|
{ |
|
m_ThisEntityAI.SetSynchDirty(); |
|
} |
|
|
|
void ClearLastUpdateTime() |
|
{ |
|
m_LastUpdateTime = 0; |
|
} |
|
|
|
void RememberLastUpdateTime() |
|
{ |
|
m_LastUpdateTime = GetCurrentUpdateTime(); |
|
} |
|
|
|
float GetCurrentUpdateTime() |
|
{ |
|
return GetGame().GetTime(); |
|
} |
|
|
|
|
|
void DeviceUpdate() |
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( !m_IsPassiveDevice ) |
|
{ |
|
|
|
if ( m_ThisEntityAI && this && IsSwitchedOn() && !m_ThisEntityAI.IsRuined() && CheckWetness() && m_CanWork && !GetGame().IsMissionMainMenu() ) |
|
{ |
|
bool was_powered = IsWorking(); |
|
float consumed_energy_coef; |
|
|
|
|
|
if ( m_LastUpdateTime == 0 ) |
|
{ |
|
RememberLastUpdateTime(); |
|
consumed_energy_coef = 1.0; |
|
} |
|
else |
|
{ |
|
float updatetime = GetCurrentUpdateTime(); |
|
float time = updatetime - m_LastUpdateTime; |
|
consumed_energy_coef = time / 1000; |
|
} |
|
|
|
if (consumed_energy_coef > 0) |
|
{ |
|
m_LastUpdateTime = GetCurrentUpdateTime(); |
|
float consume_energy = GetEnergyUsage() * consumed_energy_coef; |
|
bool has_consumed_enough = true; |
|
|
|
if (GetGame().IsServer() || !GetGame().IsMultiplayer()) |
|
has_consumed_enough = ConsumeEnergy( consume_energy ); |
|
|
|
SetPowered( has_consumed_enough ); |
|
|
|
if ( has_consumed_enough ) |
|
{ |
|
if ( !was_powered ) |
|
{ |
|
m_CanStopWork = true; |
|
WakeUpWholeBranch(m_ThisEntityAI); |
|
GetGame().GameScript.CallFunction(m_ThisEntityAI, "OnWorkStart", NULL, 0); |
|
UpdateCanWork(); |
|
} |
|
|
|
OnWork( consume_energy ); |
|
} |
|
else |
|
{ |
|
if ( was_powered ) |
|
{ |
|
if (m_CanStopWork) |
|
{ |
|
m_CanStopWork = false; |
|
ClearLastUpdateTime(); |
|
GetGame().GameScript.CallFunction(m_ThisEntityAI, "OnWorkStop", NULL, 0); |
|
UpdateCanWork(); |
|
|
|
if (m_AutoSwitchOff) |
|
{ |
|
SwitchOff(); |
|
} |
|
} |
|
} |
|
|
|
StopUpdates(); |
|
} |
|
} |
|
else |
|
{ |
|
ClearLastUpdateTime(); |
|
} |
|
} |
|
else if (this && m_ThisEntityAI) |
|
{ |
|
SetPowered( false ); |
|
StopUpdates(); |
|
|
|
if (m_CanStopWork) |
|
{ |
|
m_CanStopWork = false; |
|
ClearLastUpdateTime(); |
|
GetGame().GameScript.CallFunction(m_ThisEntityAI, "OnWorkStop", NULL, 0); |
|
UpdateCanWork(); |
|
|
|
if (m_AutoSwitchOff) |
|
{ |
|
SwitchOff(); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |