12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094 |
- class GardenBase extends ItemBase //BuildingSuper
- {
- // Paths to slot textures. Slots can have multiple states, so multiple textures must be generated
- static const string SLOT_TEXTURE_DIGGED_WET_LIME = "dz\\gear\\cultivation\\data\\soil_digged_wet_lime_CO.paa";
- static const string SLOT_TEXTURE_DIGGED_WET_PLANT = "dz\\gear\\cultivation\\data\\soil_digged_wet_plant_CO.paa";
-
- // Wet/dry material
- static const string SLOT_MATERIAL_WET = "dz\\gear\\cultivation\\data\\soil_cultivated_wet.rvmat";
- static const string SLOT_MATERIAL_DRY = "dz\\gear\\cultivation\\data\\soil_cultivated.rvmat";
-
- static const string SLOT_MATERIAL_LIMED_WET = "dz\\gear\\cultivation\\data\\soil_cultivated_limed_wet.rvmat";
- static const string SLOT_MATERIAL_LIMED_DRY = "dz\\gear\\cultivation\\data\\soil_cultivated_limed.rvmat";
- static const string SLOT_MATERIAL_COMPOST_WET = "dz\\gear\\cultivation\\data\\soil_cultivated_compost_wet.rvmat";
- static const string SLOT_MATERIAL_COMPOST_DRY = "dz\\gear\\cultivation\\data\\soil_cultivated_compost.rvmat";
-
- // slot names -> MUST BE LOWERCASE
- private static const string SLOT_SELECTION_DIGGED_PREFIX = "seedbase_";
- private static const string SLOT_SELECTION_COVERED_PREFIX = "slotCovered_";
- private static const string SLOT_MEMORY_POINT_PREFIX = "slot_";
- private static const string SLOT_SEEDBASE_PREFIX = "seedbase_";
-
- private static const int CHECK_RAIN_INTERVAL = 15;
- private static const float CREATE_PLANT_DELAY = 1000.0;
- private static const float RAIN_INTENSITY_THRESHOLD = 0.05;
-
- protected ref array<ref Slot> m_Slots;
- protected int m_SlotFertilityState = 0; //Used to store fertility state of all slots
- protected int m_SlotWateredState = 0; //Used to store watered state of all slots
- protected int m_MaxWateredStateVal = 0; //Used to store water max state of all slots
-
- protected int m_SlotState = 0; //Used to handle and sync slot states
-
- protected int m_SlotWaterBitmap0; // Slots 1–4
- protected int m_SlotWaterBitmap1; // Slots 5–8
- protected int m_SlotWaterBitmap2; // Slot 9 (and could hold up to 3 more if needed)
-
- protected int m_SlotFertilizerBitmap0; // Slots 1–4
- protected int m_SlotFertilizerBitmap1; // Slots 5–8
- protected int m_SlotFertilizerBitmap2; // Slot 9 (and could hold up to 3 more if needed)
-
- protected float m_DefaultFertility = 1;
- ref Timer m_CheckRainTimer;
-
- private static ref map<string,string> m_map_slots; // For the 'attachment slot -> plant slot' conversion. It is possible that this will be removed later.
-
- void GardenBase()
- {
- RegisterNetSyncVariableInt("m_SlotState");
- RegisterNetSyncVariableInt("m_SlotFertilityState");
- RegisterNetSyncVariableInt("m_SlotWateredState");
-
- RegisterNetSyncVariableInt("m_SlotWaterBitmap0");
- RegisterNetSyncVariableInt("m_SlotWaterBitmap1");
- RegisterNetSyncVariableInt("m_SlotWaterBitmap2");
-
- RegisterNetSyncVariableInt("m_SlotFertilizerBitmap0");
- RegisterNetSyncVariableInt("m_SlotFertilizerBitmap1");
- RegisterNetSyncVariableInt("m_SlotFertilizerBitmap2");
-
- m_map_slots = new map<string,string>;
-
- SetEventMask(EntityEvent.INIT); // Enable EOnInit event
-
- // Prepare m_map_slots
- for (int i = 1; i <= GetGardenSlotsCount(); ++i)
- {
- // m_map_slots is supposed to be: <input, output>
- string input = SLOT_SEEDBASE_PREFIX + i.ToString();
- string output = SLOT_MEMORY_POINT_PREFIX;
-
- if (i < 10)
- output = output + "0"; // Example: '1' changes to '01'
-
- output = output + i.ToString();
-
- m_map_slots.Set(input, output);
- }
-
- InitializeSlots();
- SetMaxWaterStateVal();
-
- if (GetGame().IsServer())
- {
- CheckRainStart();
- }
- }
-
- void ~GardenBase()
- {
- if (GetGame() && m_CheckRainTimer)
- {
- m_CheckRainTimer.Stop();
- m_CheckRainTimer = null;
- }
- }
- override void OnVariablesSynchronized()
- {
- super.OnVariablesSynchronized();
-
- UpdateSlots();
- }
-
- void UpdateSlots()
- {
- if (!m_Slots || m_Slots.Count() == 0)
- return;
-
- //! Set relevant slot values from synced bitmaps
- int slotsCount = GetGardenSlotsCount();
- for (int i = 0; i < slotsCount; i++)
- {
- Slot slot = m_Slots.Get(i);
-
- // Read relevant bits and set the synced values on the slots
- int fertilityState = (m_SlotFertilityState >> i) & 1;
- slot.SetFertilityState(fertilityState);
-
- int wateredState = (m_SlotWateredState >> i) & 1;
- slot.SetWateredState(wateredState);
-
- int waterQuantity = GetWaterQuantity(i);
- slot.SetWater(waterQuantity);
-
- int fertilizerQuantity = GetFertilizerQuantity(i);
- slot.SetFertilizerQuantity(fertilizerQuantity);
-
- int slotState = GetSlotState(i);
- slot.SetState(slotState);
- }
- }
-
- override bool HasProxyParts()
- {
- return true;
- }
-
- override int GetHideIconMask()
- {
- return EInventoryIconVisibility.HIDE_VICINITY;
- }
-
- void SetBaseFertility(float value)
- {
- m_DefaultFertility = value;
- }
-
- float GetBaseFertility()
- {
- return m_DefaultFertility;
- }
-
- override void EOnInit(IEntity other, int extra)
- {
- CheckRainTick();
- }
- void InitializeSlots()
- {
- m_Slots = new array<ref Slot>;
- int slots_count = GetGardenSlotsCount();
-
- for (int i = 0; i < slots_count; i++)
- {
- Slot slot = new Slot(GetBaseFertility());
- slot.SetSlotIndex(i);
- int i1 = i + 1;
- string name = "SeedBase_" + i1;
- int slotID = InventorySlots.GetSlotIdFromString(name);
- slot.SetSlotId(slotID);
- slot.SetGarden(this);
- slot.SetState(eGardenSlotState.STATE_DIGGED);
- m_Slots.Insert(slot);
-
- SetWaterQuantity(i, 0);
- SetFertilizerQuantity(i, 0);
- }
-
- UpdateSlots();
- }
-
- void SetMaxWaterStateVal()
- {
- int state = 0;
-
- for ( int i = 0; i < m_Slots.Count(); i++ )
- {
- state += 1 * Math.Pow( 2, i );
- }
-
- m_MaxWateredStateVal = state;
- }
-
- int GetMaxWaterStateVal()
- {
- return m_MaxWateredStateVal;
- }
-
- override bool OnStoreLoad(ParamsReadContext ctx, int version)
- {
- if (version <= 118)
- return true;
-
- if (!super.OnStoreLoad(ctx, version))
- return false;
-
- if (version < 102)
- {
- float some_value;
- ctx.Read(some_value); // compatibility check
- }
-
- int slots_count = GetGardenSlotsCount();
- for (int i = 0; i < slots_count; i++)
- {
- Slot slot = m_Slots.Get(i);
- if (!slot.OnStoreLoadCustom(ctx, version))
- return false;
-
- float waterQuantity = slot.GetWater();
- SetWaterQuantity(i, waterQuantity);
-
- float fertilizerQuantity = slot.GetFertilizerQuantity();
- SetFertilizerQuantity(i, fertilizerQuantity);
- }
-
- if ( version >= 119 )
- {
- if(!ctx.Read( m_SlotFertilityState ))
- return false;
- }
-
- if ( version >= 120 )
- {
- if (!ctx.Read( m_SlotWateredState ))
- return false;
- }
-
- return true;
- }
-
- override void EEOnAfterLoad()
- {
- super.EEOnAfterLoad();
- GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(SyncSlots, 500, false, this);
- }
-
- void SyncSlots()
- {
- if (g_Game.IsServer() || !g_Game.IsMultiplayer())
- {
- UpdateSlots();
-
- int slotsCount = GetGardenSlotsCount();
- for (int i = 0; i < slotsCount; ++i)
- {
- UpdateSlotTexture(i);
- }
-
- if (g_Game.IsDedicatedServer())
- SetSynchDirty();
- }
- }
- override void OnStoreSave( ParamsWriteContext ctx )
- {
- super.OnStoreSave( ctx );
-
- int slots_count = GetGardenSlotsCount();
-
- for ( int i = 0; i < slots_count; i++ )
- {
- Slot slot = m_Slots.Get( i );
-
- slot.OnStoreSaveCustom( ctx );
- }
-
- ctx.Write(m_SlotFertilityState);
-
- ctx.Write( m_SlotWateredState );
- }
-
- void PrintSlots()
- {
- Debug.Log("PRINT ALL SLOTS FROM...");
- Debug.Log("" + this + " | Position: " + GetPosition());
- int slots = GetInventory().GetAttachmentSlotsCount();
- Debug.Log("Nb Slots : " + slots);
-
- for ( int i = 0; i < slots ; i++ )
- {
- Slot slot = m_Slots.Get(i);
- Debug.Log("i : " + i);
- Debug.Log("Slot : " + slot);
- float slot_fertility = slot.GetFertility();
- float slot_fertility_usage = slot.GetFertilityMax();
- string slot_fertility_type = slot.GetFertilityType();
- float slot_water = slot.GetWater();
- float slot_water_usage = slot.GetWaterUsage();
- ItemBase slot_seed = slot.GetSeed();
- ItemBase slot_plant = slot.GetPlant();
- float slot_state= slot.GetState();
- float slot_slot_Index = slot.GetSlotIndex();
- int slot_slot_ID = slot.GetSlotId();
- int slot_wateredState = slot.GetWateredState();
- int slot_FertilityState = slot.GetFertilityState();
-
- Debug.Log("Fertility : " + slot_fertility);
- Debug.Log("Fertility Usage : " + slot_fertility_usage);
- Debug.Log("Fertility Type : " + slot_fertility_type);
- Debug.Log("Fertility State : " + slot_FertilityState);
- Debug.Log("Water : " + slot_water);
- Debug.Log("Water Usage : " + slot_water_usage);
- Debug.Log("Watered State : " + typename.EnumToString(eWateredState, slot_wateredState));
- Debug.Log("Seed : " + slot_seed);
- Debug.Log("Plant : " + slot_plant);
- if (slot_plant && PlantBase.Cast(slot_plant))
- PlantBase.Cast(slot_plant).PrintValues();
- Debug.Log("State : " + slot_state);
- Debug.Log("Slot Index : " + slot_slot_Index);
- Debug.Log("Slot ID : " + slot_slot_ID);
- Debug.Log("///////////////////////////");
- }
-
- Debug.Log("END OF ALL SLOTS FOR...");
- Debug.Log("" + this);
- }
-
- override bool CanPutInCargo( EntityAI parent )
- {
- if ( !super.CanPutInCargo(parent) ) {return false;}
- return false;
- }
- override bool CanPutIntoHands( EntityAI parent )
- {
- if ( !super.CanPutIntoHands( parent ) )
- {
- return false;
- }
- return false;
- }
- override bool CanRemoveFromHands( EntityAI parent )
- {
- return false;
- }
- int GetGardenSlotsCount()
- {
- return 0;
- }
- bool CanPlantSeed(string selection_component)
- {
- Slot slot = GetSlotBySelection(selection_component);
- if (slot != NULL && slot.GetState() == eGardenSlotState.STATE_DIGGED)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- // Converts attachment slot name into plant slot name. Example: 'seedbase_1' -> 'component02'
- string ConvertAttSlotToPlantSlot(string attach_slot)
- {
- // Give result
- if ( m_map_slots.Contains(attach_slot) )
- {
- string return_value = m_map_slots.Get(attach_slot);
- return return_value;
- }
-
- return "";
- }
-
- override void EEItemAttached(EntityAI item, string slot_name)
- {
- super.EEItemAttached(item, slot_name);
-
- string path = string.Format("CfgVehicles %1 Horticulture PlantType", item.GetType());
- bool IsItemSeed = GetGame().ConfigIsExisting(path); // Is this item a seed?
-
- if (IsItemSeed)
- {
- string converted_slot_name;
-
- vector pos = GetPosition();
- int index = GetSlotIndexByAttachmentSlot(slot_name);
-
- if (index < 10)
- {
- converted_slot_name = SLOT_MEMORY_POINT_PREFIX + "0" + index.ToString();
- }
- else
- {
- converted_slot_name = SLOT_MEMORY_POINT_PREFIX + index.ToString();
- }
-
- if (g_Game.IsServer() || !g_Game.IsMultiplayer())
- PlantSeed(ItemBase.Cast(item), converted_slot_name);
- }
- else if (g_Game.IsClient())
- {
- Slot slot = GetSlotByIndex(GetSlotIndexByAttachmentSlot(slot_name) - 1);
- if (slot)
- {
- slot.SetPlant(PlantBase.Cast(item));
- slot.SetState(eGardenSlotState.STATE_PLANTED);
- }
- }
- }
-
- override void EEItemDetached(EntityAI item, string slot_name)
- {
- super.EEItemDetached(item, slot_name);
-
- slot_name.ToLower();
- string path = string.Format("CfgVehicles %1 Horticulture PlantType", item.GetType());
- bool IsItemSeed = GetGame().ConfigIsExisting(path); // Is this item a seed?
-
- string converted_slot_name = ConvertAttSlotToPlantSlot(slot_name);
- Slot slot = GetSlotBySelection(converted_slot_name);
-
- if (slot)
- {
- if (IsItemSeed)
- {
- slot.SetSeed(NULL);
- }
-
- slot.SetState(eGardenSlotState.STATE_DIGGED);
- }
- }
-
- // Plants the seed into slot (selection_component)
- void PlantSeed( ItemBase seed, string selection_component )
- {
- int slot_index = GetSlotIndexBySelection( selection_component );
-
- if ( slot_index != -1 )
- {
- PluginHorticulture module_horticulture = PluginHorticulture.Cast( GetPlugin( PluginHorticulture ) );
- string plant_type = module_horticulture.GetPlantType( seed );
-
- Slot slot = m_Slots.Get( slot_index );
- slot.SetState(eGardenSlotState.STATE_PLANTED);
- slot.m_PlantType = plant_type;
- slot.SetSeed(seed);
-
- if ( !slot.NeedsWater() || slot.GetWateredState() == eWateredState.WET)
- {
- //! Adjust slot water
- if (slot.GetWateredState() == eWateredState.WET && slot.NeedsWater())
- {
- int waterMax = slot.GetWaterMax();
- slot.SetWater(waterMax);
- }
-
- seed.LockToParent();
- //Take some small amount of time before making a plant out of seeds
- GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(CreatePlant, CREATE_PLANT_DELAY, false, slot);
- }
- }
- }
-
- // Creates a plant
- void CreatePlant(Slot slot)
- {
- if (g_Game.IsServer() || !g_Game.IsMultiplayer())
- {
- ItemBase seed = slot.GetSeed();
- if (seed)
- {
- seed.UnlockFromParent();
- GetGame().ObjectDelete(seed);
-
- PlantBase plant = PlantBase.Cast( GetInventory().CreateAttachmentEx(slot.m_PlantType, slot.GetSlotId()));
- int slot_index = slot.GetSlotIndex();
- slot.SetPlant(plant);
- slot.SetState(eGardenSlotState.STATE_PLANTED);
- plant.Init(this, slot.GetFertility(), slot.m_HarvestingEfficiency, slot.GetWater());
- plant.LockToParent();
- }
- }
- }
-
- void SetSlotState(int slotIndex, int value)
- {
- // value should be 1 or 2
- int shift = slotIndex * 2;
- int mask = 3 << shift;
- int currentState = (m_SlotState & mask) >> shift;
-
- // Clear out the bits for this slot
- m_SlotState &= ~mask;
-
- // Set the new value
- m_SlotState |= (value << shift);
- if (g_Game.IsServer() && currentState != value)
- SetSynchDirty();
- }
-
- int GetSlotState(int slotIndex)
- {
- int shift = slotIndex * 2;
- int mask = 3 << shift;
- int value = (m_SlotState & mask) >> shift;
- return value; // Will be 0, 1, or 2
- }
-
- void SlotWaterStateUpdate( Slot slot )
- {
- int idx = slot.GetSlotIndex();
- int mask = 1 << idx;
- int currentState = (m_SlotWateredState >> idx) & 1;
- int newState = slot.GetWateredState();
- // Clear the relevant bit first
- m_SlotWateredState &= ~mask;
-
- // Set the bit state (1 for WET, 0 for DRY)
- m_SlotWateredState |= (newState << idx);
-
- if ((g_Game.IsServer() || !g_Game.IsMultiplayer()) && currentState != newState)
- {
- UpdateSlotTexture(slot.GetSlotIndex());
-
- if (g_Game.IsServer())
- SetSynchDirty();
- }
- }
-
- void SlotFertilityStateUpdate(Slot slot)
- {
- int idx = slot.GetSlotIndex();
- int mask = 1 << idx;
- int currentState = (m_SlotFertilityState >> idx) & 1;
- int newState = slot.GetFertilityState();
-
- // Clear the relevant bit first
- m_SlotFertilityState &= ~mask;
-
- // Set the bit state (1 for FERTILIZED, 0 for NONE)
- m_SlotFertilityState |= (newState << idx);
-
- if ((g_Game.IsServer() || !g_Game.IsMultiplayer()) && currentState != newState)
- {
- UpdateSlotTexture(slot.GetSlotIndex());
-
- if (g_Game.IsServer())
- SetSynchDirty();
- }
- }
-
- //! This should be only called on server as the selections are synced to clients in C++
- void UpdateSlotTexture( int slot_index )
- {
- if (!m_Slots || m_Slots.Count() - 1 < slot_index)
- return;
-
- Slot slot = m_Slots.Get( slot_index );
-
- // Show / Hide selections according to DIGGED or COVERED states.
- if ( slot.IsDigged() || slot.IsPlanted() )
- {
- string str_hide = SLOT_SELECTION_COVERED_PREFIX + (slot_index + 1).ToStringLen(2);
- string str_show = SLOT_SELECTION_DIGGED_PREFIX + (slot_index + 1).ToStringLen(1);
-
- HideSelection( str_hide );
- ShowSelection( str_show );
- }
-
- if ( slot.GetFertilityState() == eFertlityState.FERTILIZED && slot.GetFertilityType() != "" )
- {
- SetSlotTextureFertilized( slot_index, slot.GetFertilityType() );
- }
- else
- {
- SetSlotTextureDigged( slot_index );
- }
- }
- void SetSlotTextureDigged( int slot_index )
- {
- TStringArray textures = GetHiddenSelectionsTextures();
-
- string str_digged = SLOT_SELECTION_DIGGED_PREFIX + (slot_index + 1).ToStringLen(1);
-
- ShowSelection( str_digged );
- string texture = textures.Get(0);
- SetObjectTexture( slot_index, texture );
-
- Slot slot = m_Slots.Get( slot_index );
-
- if ( slot.GetWateredState() == 0 )
- {
- // Set dry material
- SetObjectMaterial( slot_index, SLOT_MATERIAL_DRY );
- }
- else
- {
- // Set wet material
- SetObjectMaterial( slot_index, SLOT_MATERIAL_WET );
- }
- }
- void SetSlotTextureFertilized( int slot_index, string item_type )
- {
- TStringArray textures = GetHiddenSelectionsTextures();
-
- int tex_id = GetGame().ConfigGetInt( string.Format("cfgVehicles %1 Horticulture TexId", item_type) );
-
- string str_show = SLOT_SELECTION_DIGGED_PREFIX + (slot_index + 1).ToStringLen(2);
-
- ShowSelection( str_show );
- SetObjectTexture( slot_index, textures.Get(tex_id) );
-
- Slot slot = m_Slots.Get( slot_index );
-
- int slot_index_offset = 0;
-
- // Set material according to dry / wet states
- if ( slot.GetWateredState() == 0 )
- {
- // Set dry material for garden lime
- if ( slot.GetFertilityType() == "GardenLime" )
- {
- SetObjectMaterial( slot_index + slot_index_offset, SLOT_MATERIAL_LIMED_DRY );
- }
- else if ( slot.GetFertilityType() == "PlantMaterial" )
- {
- SetObjectMaterial( slot_index + slot_index_offset, SLOT_MATERIAL_COMPOST_DRY );
- }
- }
- else
- {
- // Set dry material for compost
- if ( slot.GetFertilityType() == "GardenLime" )
- {
- SetObjectMaterial( slot_index + slot_index_offset, SLOT_MATERIAL_LIMED_WET );
- }
- else if ( slot.GetFertilityType() == "PlantMaterial" )
- {
- SetObjectMaterial( slot_index + slot_index_offset, SLOT_MATERIAL_COMPOST_WET );
- }
- }
- }
-
- void RemoveSlot(int index)
- {
- if (m_Slots)
- {
- Slot slot = m_Slots.Get(index);
- PlantBase plant = slot.GetPlant();
-
- if (plant && !plant.IsPendingDeletion())
- {
- plant.UnlockFromParent();
- plant.m_MarkForDeletion = true;
- plant.Delete();
- }
-
- slot.Init(GetBaseFertility());
-
- int idx = slot.GetSlotIndex();
- int mask = 1 << idx;
-
- // Clear the relevant bits from bitmaps
- m_SlotFertilityState &= ~mask;
- m_SlotWateredState &= ~mask;
-
- // Reset states
- m_SlotFertilityState |= (slot.GetFertilityState() << idx);
- m_SlotWateredState |= (slot.GetWateredState() << idx);
-
- // Reset stol quantities
- SetWaterQuantity(index, 0);
- SetFertilizerQuantity(index, 0);
-
- UpdateSlots();
-
- HideSelection(SLOT_SELECTION_COVERED_PREFIX + (index + 1).ToStringLen(2));
- UpdateSlotTexture(idx);
-
- SetSynchDirty();
- }
- }
- void RemoveSlotPlant( Object plant )
- {
- int index = GetSlotIndexByPlant( plant );
- if ( index >= 0 )
- {
- RemoveSlot( index );
- }
- }
- Slot GetSlotBySelection( string selection_component )
- {
- int slot_index = GetSlotIndexBySelection( selection_component );
-
- if ( slot_index > -1 )
- {
- return m_Slots.Get( slot_index );
- }
- else
- {
- return NULL;
- }
- }
- // Returns slot array index by selection, starting from 0 as the first one.
- int GetSlotIndexBySelection( string selection_component )
- {
- int slot_index = -1;
-
- if ( m_Slots != NULL )
- {
- string selection_component_lower = selection_component;
- selection_component_lower.ToLower();
-
- int start = selection_component_lower.IndexOf( SLOT_MEMORY_POINT_PREFIX );
-
- if ( start > -1 )
- {
- start += SLOT_MEMORY_POINT_PREFIX.Length();
- int end = start + 2;
- int length = selection_component.Length();
-
- if ( length >= end )
- {
- int length_add = length - end; // Hack-fix for inconsistent component names in p3d
- int length_from_end = 2 + length_add;
- string num_str = selection_component.Substring( start, length_from_end );
- slot_index = num_str.ToInt();
-
- slot_index = slot_index - 1;
- }
- }
- }
-
- return slot_index;
- }
-
- int GetSlotIndexByAttachmentSlot( string att_slot )
- {
- int slot_index = -1;
-
- int start = "SeedBase_".Length();
- int end = att_slot.Length();//start + 2;
- int len = end - start;
-
- string num_str = att_slot.Substring( start, len );
- slot_index = num_str.ToInt();
-
- return slot_index;
- }
- int GetSlotIndexByPlant( Object plant )
- {
- if ( m_Slots != NULL )
- {
- for ( int i = 0; i < m_Slots.Count(); i++ )
- {
- PlantBase found_plant = m_Slots.Get(i).GetPlant();
-
- if ( found_plant == plant )
- {
- return i;
- }
- }
- }
-
- return -1;
- }
-
- int GetNearestSlotIDByState(vector position, int slot_state)
- {
- float nearest_distance = 1000.0;
- int nearest_slot_index = -1;
- int slots_count = GetGardenSlotsCount();
- for (int i = 0; i < slots_count; i++)
- {
- Slot slot = m_Slots.Get(i); // Move this line by a scope higher in this function after debugging
- vector slot_pos = GetSlotPosition(i);
- float current_distance = vector.Distance(position, slot_pos);
-
- if (current_distance < nearest_distance)
- {
- if (slot != NULL && slot.GetState() == slot_state)
- {
- nearest_distance = current_distance;
- nearest_slot_index = i;
- }
- }
- }
-
- return nearest_slot_index;
- }
- vector GetSlotPosition( int index )
- {
- string memory_point = SLOT_MEMORY_POINT_PREFIX + (index + 1).ToStringLen(2);
- vector pos = this.GetSelectionPositionMS( memory_point );
-
- return this.ModelToWorld( pos );
- }
-
- void CheckRainStart()
- {
- if ( !m_CheckRainTimer )
- m_CheckRainTimer = new Timer( CALL_CATEGORY_SYSTEM );
-
- m_CheckRainTimer.Run( CHECK_RAIN_INTERVAL, this, "CheckRainTick", NULL, true );
- }
-
- void CheckRainTick()
- {
- float rainIntensity = GetGame().GetWeather().GetRain().GetActual();
- float wetness = rainIntensity * 20 * CHECK_RAIN_INTERVAL;
- if (rainIntensity > 1 || rainIntensity < 0)
- wetness = 0; // hackfix for weird values returned by weather system
-
- if (wetness == 0)
- wetness = -0.1 * CHECK_RAIN_INTERVAL;
-
- // At the moment we dont want to remove water from the slot when it is not raining so we check and manipulate water quantity only when we add to it
- if (rainIntensity > RAIN_INTENSITY_THRESHOLD)
- {
- if (wetness != 0) // only change slot water quantity when wetness value can actually add/deduct something
- {
- int slotsCount = GetGardenSlotsCount();
- for (int i = 0; i < slotsCount; i++)
- {
- if (m_Slots)
- {
- Slot slot = m_Slots.Get(i);
- if (slot)
- {
- slot.GiveWater(wetness * Math.RandomFloat01());
- }
- }
- }
- }
- }
- }
-
- array<ref Slot> GetSlots()
- {
- return m_Slots;
- }
-
- Slot GetSlotByIndex( int index )
- {
- return m_Slots.Get(index);
- }
-
- override void SetActions()
- {
- AddAction(ActionHarvestCrops);
- AddAction(ActionRemovePlant);
- AddAction(ActionRemoveSeed);
- }
-
- void SetFertilizerBitmapByIndex(int index, int value)
- {
- switch (index)
- {
- case 0:
- m_SlotFertilizerBitmap0 = value;
- break;
- case 1:
- m_SlotFertilizerBitmap1 = value;
- break;
- case 2:
- m_SlotFertilizerBitmap2 = value;
- break;
- }
- }
-
- int GetFertilizerBitmapByIndex(int index)
- {
- switch (index)
- {
- case 0:
- return m_SlotFertilizerBitmap0;
- case 1:
- return m_SlotFertilizerBitmap1;
- case 2:
- return m_SlotFertilizerBitmap2;
- }
- return 0;
- }
-
- void SetWaterBitmapByIndex(int index, int value)
- {
- switch (index)
- {
- case 0:
- m_SlotWaterBitmap0 = value;
- break;
- case 1:
- m_SlotWaterBitmap1 = value;
- break;
- case 2:
- m_SlotWaterBitmap2 = value;
- break;
- }
- }
-
- int GetWaterBitmapByIndex(int index)
- {
- switch (index)
- {
- case 0:
- return m_SlotWaterBitmap0;
- case 1:
- return m_SlotWaterBitmap1;
- case 2:
- return m_SlotWaterBitmap2;
- }
- return 0;
- }
-
- void SetWaterQuantity(int slotIndex, int value)
- {
- int bitsPerSlot = 8;
- int slotsPerInt = 32 / bitsPerSlot;
-
- int groupIndex = slotIndex / slotsPerInt;
- int bitOffset = (slotIndex % slotsPerInt) * bitsPerSlot;
-
- int mask = 255 << bitOffset;
- value = Math.Clamp(value, 0, 200);
-
- int temp = GetWaterBitmapByIndex(groupIndex);
- temp &= ~mask;
- temp |= (value << bitOffset);
- SetWaterBitmapByIndex(groupIndex, temp);
- }
-
- int GetWaterQuantity(int slotIndex)
- {
- int bitsPerSlot = 8;
- int slotsPerInt = 32 / bitsPerSlot;
-
- int groupIndex = slotIndex / slotsPerInt;
- int bitOffset = (slotIndex % slotsPerInt) * bitsPerSlot;
-
- int value = GetWaterBitmapByIndex(groupIndex);
- return (value >> bitOffset) & 255;
- }
-
- void SetFertilizerQuantity(int slotIndex, int value)
- {
- int bitsPerSlot = 8;
- int slotsPerInt = 32 / bitsPerSlot;
-
- int groupIndex = slotIndex / slotsPerInt;
- int bitOffset = (slotIndex % slotsPerInt) * bitsPerSlot;
-
- int mask = 255 << bitOffset;
- value = Math.Clamp(value, 0, 200);
-
- int temp = GetFertilizerBitmapByIndex(groupIndex);
- temp &= ~mask;
- temp |= (value << bitOffset);
- SetFertilizerBitmapByIndex(groupIndex, temp);
- }
-
- int GetFertilizerQuantity(int slotIndex)
- {
- int bitsPerSlot = 8;
- int slotsPerInt = 32 / bitsPerSlot;
-
- int groupIndex = slotIndex / slotsPerInt;
- int bitOffset = (slotIndex % slotsPerInt) * bitsPerSlot;
-
- int value = GetFertilizerBitmapByIndex(groupIndex);
- return (value >> bitOffset) & 255;
- }
-
- //! DEPRECATED
- [Obsolete("No replacement")]
- bool IsCorrectFertilizer( ItemBase item, string selection_component )
- {
- Slot slot = GetSlotBySelection( selection_component );
-
- if ( slot != NULL )
- {
- string item_type = item.GetType();
-
- if ( slot.GetFertilityType() == "" || slot.GetFertilityType() == item_type )
- {
- return true;
- }
- }
-
- return false;
- }
-
- [Obsolete("No replacement")]
- void UpdateTexturesOnAllSlots()
- {
- int slots_count = GetGardenSlotsCount();
- for (int i = 0; i < slots_count; ++i)
- {
- UpdateSlotTexture(i);
- }
- }
-
- [Obsolete("No replacement")]
- bool NeedsFertilization(string selection_component)
- {
- Slot slot = GetSlotBySelection(selection_component);
- if (slot)
- {
- if (slot.GetFertilityState() == eFertlityState.NONE)
- {
- return true;
- }
- }
-
- return false;
- }
- [Obsolete("CAContinuousFertilizeGardenSlot::FertilizeSlot is used now instead.")]
- void Fertilize( PlayerBase player, ItemBase item, float consumed_quantity, string selection_component )
- {
- Slot slot = GetSlotBySelection( selection_component );
-
- if ( slot != NULL )
- {
- string item_type = item.GetType();
-
- if ( slot.GetFertilityType() == "" || slot.GetFertilityType() == item_type )
- {
- slot.SetFertilityType(item_type);
-
- float add_energy_to_slot = GetGame().ConfigGetFloat( string.Format("cfgVehicles %1 Horticulture AddEnergyToSlot", item_type) );
- slot.m_FertilizerUsage = GetGame().ConfigGetFloat( string.Format("cfgVehicles %1 Horticulture ConsumedQuantity", item_type) );
-
- float coef = Math.Clamp( consumed_quantity / slot.m_FertilizerUsage, 0.0, 1.0 );
- add_energy_to_slot = coef * add_energy_to_slot;
-
- slot.m_FertilizerQuantity += consumed_quantity;
- slot.m_Fertility += add_energy_to_slot;
-
- if ( slot.GetFertilizerQuantity() >= slot.GetFertilizerQuantityMax() )
- {
- int slot_index = slot.GetSlotIndex();
-
- if (slot_index > -1)
- {
- UpdateSlotTexture( slot_index );
- slot.SetFertilityState(eFertlityState.FERTILIZED);
- // Set relevant bit
- m_SlotFertilityState |= slot.GetFertilityState() << slot.GetSlotIndex();
- }
- }
- }
- else
- {
- slot.SetFertilizerQuantity(0);
- slot.SetFertilityType("");
- slot.SetFertilityState(eFertlityState.NONE);
- }
- SetSynchDirty();
- }
- }
- [Obsolete("No replacement")]
- void WaterAllSlots()
- {
- for (int i = 0; i < m_Slots.Count(); i++)
- {
- Slot slot = m_Slots[i];
- if (!slot)
- continue;
- slot.SetWateredState(eWateredState.WET);
- UpdateSlotTexture(slot.GetSlotIndex());
- m_SlotWateredState |= eWateredState.WET << slot.GetSlotIndex();
- }
- }
- }
|