File size: 21,368 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
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;
	
	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 fertility state of all slots
	protected int 	m_MaxWateredStateVal = 0; 	//Used to store fertility state of all slots
	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_SlotFertilityState");
		RegisterNetSyncVariableInt("m_SlotWateredState");
		
		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);
		}
		
		if ( GetGame().IsServer() )
		{
			CheckRainStart();
		}
		
		InitializeSlots();
		
		SetMaxWaterStateVal();
	}
	
	override void OnVariablesSynchronized()
	{
		super.OnVariablesSynchronized();
		
		SyncSlots();
	}
	
	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();
		UpdateTexturesOnAllSlots();
	}

	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 slot_id = InventorySlots.GetSlotIdFromString(name);
			slot.SetSlotId(slot_id);
			slot.SetGarden(this);
			slot.m_State = Slot.STATE_DIGGED;
			m_Slots.Insert( slot );
		}
	}
	
	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;
	}
	
	void UpdateTexturesOnAllSlots()
	{
		int slots_count = GetGardenSlotsCount();
		
		for ( int i = 0; i < slots_count; i++ )
		{
			UpdateSlotTexture(i);
		}
	}

	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;

			//Slot textures will be updated after store load
			//UpdateSlotTexture( i );
		}
		
		if ( version >= 119 )
			ctx.Read( m_SlotFertilityState );
		
		if ( version >= 120 )
			ctx.Read( m_SlotWateredState );
		
		return true;
	}
	
	override void AfterStoreLoad()
	{
		super.AfterStoreLoad();
	}
	
	override void EEOnAfterLoad()
	{
		GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( SyncSlots, 500, false, this );
		super.EEOnAfterLoad();
	}
	
	void SyncSlots()
	{
		for ( int i = 0; i < GetGardenSlotsCount(); i++ )
		{
			// Read relevant bit
			int fertilityState = (m_SlotFertilityState >> i) & 1;
			m_Slots[i].SetFertilityState(fertilityState);
			
			int wateredState = (m_SlotWateredState >> i) & 1;
			m_Slots[i].SetWateredState( wateredState );
			
			if ( fertilityState == eFertlityState.NONE )
			{
				m_Slots[i].SetFertilityType( "" );
				m_Slots[i].SetFertilizerQuantity( 0 );
			}
			
			if ( wateredState == eWateredState.DRY )
			{
				m_Slots[i].SetWater( 0 );
			}
			
			UpdateSlotTexture( i );
		}
		
		if ( GetGame().IsServer() )
		{
			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);
		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();
			float 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 : " + slot_wateredState);
			Debug.Log("Seed : " + slot_seed);
			Debug.Log("Plant : " + slot_plant);
			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 player )
	{
		if ( !super.CanPutIntoHands( parent ) )
		{
			return false;
		}
		return false;
	}

	override bool CanRemoveFromHands( EntityAI player )
	{
		return false;
	}

	int GetGardenSlotsCount()
	{
		return 0;
	}

	bool CanPlantSeed( string selection_component )
	{
		Slot slot = GetSlotBySelection( selection_component );
		
		if ( slot != NULL && slot.m_State == Slot.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?
		
		int slot_id = InventorySlots.GetSlotIdFromString(slot_name);
		
		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();
			}
			
			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.m_State = Slot.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(Slot.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(Slot.STATE_PLANTED);
			slot.m_PlantType = plant_type;
			slot.SetSeed(seed);
			
			if ( !slot.NeedsWater() )
			{
				seed.LockToParent();
				//Take some small amount of time before making a plant out of seeds
				Timer growthTimer = NULL;
				growthTimer = new Timer( CALL_CATEGORY_SYSTEM );
				Param createPlantParam = new Param1<Slot>(slot);
				growthTimer.Run( 0.1, this, "CreatePlant", createPlantParam, false ); //Use a const for timer delay
			}
		}
	}
	
	// Creates a plant
	void CreatePlant(Slot slot )
	{
		if (g_Game.IsServer())
		{
			ItemBase seed = slot.GetSeed();
			seed.UnlockFromParent();
			GetGame().ObjectDelete(seed);

			PlantBase plant = PlantBase.Cast( GetInventory().CreateAttachmentEx( slot.m_PlantType, slot.GetSlotId()) );
		
			int slot_index = slot.GetSlotIndex();
			slot.SetPlant(plant);						
			plant.Init( this, slot.GetFertility(), slot.m_HarvestingEfficiency, slot.GetWater() );
			//ShowSelection(SLOT_SELECTION_COVERED_PREFIX + (slot_index + 1).ToStringLen(2));
				
			plant.LockToParent();
		}
	}
		
	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();
						
						//TODO Boris: Add soft skill 2.0
						//PluginExperience module_exp = GetPlugin(PluginExperience);
						//slot.m_HarvestingEfficiency = module_exp.GetExpParamNumber(player, PluginExperience.EXP_FARMER_FERTILIZATION, "efficiency");
					}
				}
			}
			else
			{
				slot.SetFertilizerQuantity(0);
				slot.SetFertilityType("");
				slot.SetFertilityState(eFertlityState.NONE);
			}
			SetSynchDirty();
		}
	}

	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;
	}

	bool NeedsFertilization( string selection_component )
	{
		Slot slot = GetSlotBySelection( selection_component );
		
		if ( slot )
		{
			if ( slot.GetFertilityState() == eFertlityState.NONE )
			{
				return true;
			}
		}
		
		return false;
	}
	
	void SlotWaterStateUpdate( Slot slot )
	{
		// Set relevant bit
		m_SlotWateredState |= slot.GetWateredState() << slot.GetSlotIndex();
		SetSynchDirty();
	}

	void UpdateSlotTexture( int slot_index )
	{
		// TO DO: Fix DAYZ-30633 here!
		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.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 != NULL )
		{	
			Slot slot = m_Slots.Get( index );
			PlantBase plant = slot.GetPlant();
			
			if ( plant )
			{
				plant.UnlockFromParent();
				plant.m_MarkForDeletion = true;
				GetGame().ObjectDelete( plant );
			}
			
			slot.Init( GetBaseFertility() );
			
			// Clear relevant bit
			slot.SetFertilityState(eFertlityState.NONE);
			m_SlotFertilityState &= ~(1 << slot.GetSlotIndex());
			
			slot.SetWateredState( eWateredState.DRY );
			m_SlotWateredState &= ~(1 << slot.GetSlotIndex());
			
			SetSynchDirty();
			
			HideSelection( SLOT_SELECTION_COVERED_PREFIX + (index + 1).ToStringLen(2) );
			UpdateSlotTexture( index );
		}
	}

	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.m_State == 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 rain_intensity = GetGame().GetWeather().GetRain().GetActual();
			
		float wetness = rain_intensity * 20 * CHECK_RAIN_INTERVAL;
		
		if (rain_intensity > 1  ||  rain_intensity < 0)
			wetness = 0; // hackfix for weird values returned by weather system
		
		if (wetness == 0)
			wetness = -0.1 * CHECK_RAIN_INTERVAL;
		
		int slots_count = GetGardenSlotsCount();
		
		if ( rain_intensity > 0 )
		{
			WaterAllSlots();
			SetSynchDirty();
		}
		
		for ( int i = 0; i < slots_count; i++ )
		{
			if ( m_Slots )
			{
				Slot slot = m_Slots.Get( i );
				
				if ( slot )
				{
					slot.GiveWater( wetness * Math.RandomFloat01() );
				}
			}
		}
	}
	
	//Used to update
	void WaterAllSlots()
	{
		int state = 0;
		
		for ( int i = 0; i < m_Slots.Count(); i++ )
		{
			state += 1 * Math.Pow( 2, i );
		}
		
		SetSlotWateredState( state );
	}
	
	array<ref Slot> GetSlots()
	{
		return m_Slots;
	}
	
	Slot GetSlotByIndex( int index )
	{
		return m_Slots.Get(index);
	}
	
	int GetSlotWateredState()
	{
		return m_SlotWateredState;
	}
	
	void SetSlotWateredState( int newState )
	{
		m_SlotWateredState = newState;
	}
	
	override void SetActions()
	{
		AddAction(ActionHarvestCrops);
		AddAction(ActionRemovePlant);
		AddAction(ActionRemoveSeed);
	}
}