plantbase.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. class PlantBase extends ItemBase
  2. {
  3. // Plant states
  4. static const int STATE_DRY = 0;
  5. static const int STATE_GROWING = 1;
  6. static const int STATE_MATURE = 2;
  7. static const int STATE_SPOILED = 3;
  8. private float m_SprayUsage; // How much spray is needed to stop infestation of plant
  9. private float m_InfestationChance;
  10. private int m_GrowthStagesCount;
  11. private int m_CropsCount;
  12. private bool m_HasCrops;
  13. private string m_CropsType;
  14. private float m_PlantMaterialMultiplier;
  15. private int m_PlantState;
  16. private int m_PlantStateIndex;
  17. private float m_CurrentPlantMaterialQuantity;
  18. private bool m_IsInfested;
  19. private float m_SprayQuantity;
  20. bool m_MarkForDeletion = false;
  21. int m_DeleteDryPlantTime; // For how long in seconds can an unwatered plant exist before it disappears
  22. int m_SpoiledRemoveTime; // For how long in seconds a spoiled plant will exist
  23. int m_FullMaturityTime; // How much time needs plant to be full grown in seconds
  24. int m_SpoilAfterFullMaturityTime; // How long in seconds it takes for plant to be spoiled after it is full grown
  25. int m_StateChangeTime; // For how long in seconds will plant stay in one state before its going to next state
  26. ref Timer m_GrowthTimer = NULL;
  27. ref Timer m_InfestationTimer = NULL;
  28. ref Timer m_SpoilAfterFullMaturityTimer = NULL;
  29. ref Timer m_SpoiledRemoveTimer = NULL;
  30. ref Timer m_DeleteDryPlantTimer = NULL;
  31. private GardenBase m_GardenBase = NULL;
  32. private ref Slot m_Slot = NULL;
  33. private PluginHorticulture m_ModuleHorticulture;
  34. private const float SPOIL_AFTER_MATURITY_TIME = 14400; //The time it takes for a fully grown plant to spoil, in seconds
  35. void PlantBase()
  36. {
  37. m_ModuleHorticulture = PluginHorticulture.Cast( GetPlugin( PluginHorticulture ) );
  38. m_SprayUsage = 5;
  39. m_DeleteDryPlantTime = (60 * 10) + Math.RandomInt(0, 60 * 2);
  40. m_SpoiledRemoveTime = (60 * 20) + Math.RandomInt(0, 60 * 5);
  41. //Must be between 0 and 1
  42. m_InfestationChance = 0.2; // Temporarily disabled until its fixed. Infestation is not visualy persistent over server restarts and m_SpoiledRemoveTimer crashes when it's meant to delete the plant.
  43. string plant_type = this.GetType();
  44. m_GrowthStagesCount = GetGame().ConfigGetInt( "cfgVehicles " + plant_type + " Horticulture GrowthStagesCount" );
  45. m_CropsCount = GetGame().ConfigGetInt( "cfgVehicles " + plant_type + " Horticulture CropsCount" );
  46. GetGame().ConfigGetText( "cfgVehicles " + plant_type + " Horticulture CropsType", m_CropsType );
  47. m_PlantStateIndex = -1;
  48. m_CurrentPlantMaterialQuantity = 0;
  49. m_IsInfested = false;
  50. m_SprayQuantity = 0.0;
  51. m_HasCrops = true;
  52. SetTakeable( false );
  53. RegisterNetSyncVariableBool("m_HasCrops");
  54. RegisterNetSyncVariableInt("m_PlantState");
  55. RegisterNetSyncVariableInt("m_PlantStateIndex");
  56. }
  57. void ~PlantBase()
  58. {
  59. if (!m_MarkForDeletion)
  60. {
  61. DestroyPlant();
  62. }
  63. }
  64. void Init( GardenBase garden_base, float fertility, float harvesting_efficiency, float water )
  65. {
  66. m_GardenBase = garden_base;
  67. m_FullMaturityTime += Math.RandomInt(-60,180);
  68. float divided = /*(float) ((60 * 5) + Math.RandomInt(0, 60 * 1)) / fertility;*/ m_FullMaturityTime;
  69. //divided = (float)((60 * 30) + Math.RandomInt(0, 60 * 30)) * fertility;
  70. m_SpoilAfterFullMaturityTime = SPOIL_AFTER_MATURITY_TIME; //divided;
  71. divided = (float)((float)m_FullMaturityTime / ((float)m_GrowthStagesCount - 2.0));
  72. m_StateChangeTime = divided;
  73. float count = m_CropsCount * fertility * harvesting_efficiency;
  74. m_CropsCount = (int)Math.Ceil( count );
  75. m_PlantMaterialMultiplier = 0.1 * harvesting_efficiency;
  76. float rain_intensity = GetGame().GetWeather().GetRain().GetActual();
  77. if ( rain_intensity > 0.0 )
  78. {
  79. CheckWater();
  80. }
  81. else
  82. {
  83. CheckWater();
  84. if ( NeedsWater() )
  85. {
  86. SetPlantState(STATE_DRY);
  87. if (GetGame().IsServer())
  88. {
  89. m_DeleteDryPlantTimer = new Timer( CALL_CATEGORY_SYSTEM );
  90. m_DeleteDryPlantTimer.Run( m_DeleteDryPlantTime, this, "DeleteDryPlantTick", NULL, false );
  91. }
  92. }
  93. }
  94. }
  95. override bool OnStoreLoad( ParamsReadContext ctx, int version )
  96. {
  97. if ( !super.OnStoreLoad( ctx, version ) )
  98. return false;
  99. //Print("Plant - OnStoreLoad - ");
  100. GardenBase garden = GardenBase.Cast( GetHierarchyParent() );
  101. //Print(garden);
  102. int slot_index = -1;
  103. ctx.Read( slot_index );
  104. //Print(slot_index);
  105. Slot slot = garden.GetSlotByIndex(slot_index);
  106. //Print(slot);
  107. SetSlot(slot);
  108. if ( !OnStoreLoadCustom( ctx, version ) )
  109. return false;
  110. return true;
  111. }
  112. override void OnStoreSave( ParamsWriteContext ctx )
  113. {
  114. super.OnStoreSave( ctx );
  115. Slot slot = GetSlot();
  116. if (slot)
  117. {
  118. int slot_index = slot.GetSlotIndex();
  119. slot.SetPlant(this); // hack
  120. ctx.Write( slot_index );
  121. OnStoreSaveCustom( ctx );
  122. }
  123. else
  124. {
  125. GetGame().ObjectDelete(this); // Plants that exist without a garden must be deleted. Otherwise they might cause problems.
  126. Print("Warning! A plant existed without a garden. Therefore it was deleted from the world to prevent issues!");
  127. }
  128. }
  129. string GetCropsType()
  130. {
  131. return m_CropsType;
  132. }
  133. bool OnStoreLoadCustom( ParamsReadContext ctx, int version )
  134. {
  135. int loadInt;
  136. if ( !ctx.Read( loadInt ) )
  137. loadInt = 0;
  138. m_SprayUsage = loadInt;
  139. loadInt = 0;
  140. if ( !ctx.Read( loadInt ) )
  141. loadInt = 5;
  142. m_DeleteDryPlantTime = loadInt;
  143. loadInt = 0;
  144. if ( !ctx.Read( loadInt ) )
  145. loadInt = 5;
  146. m_SpoiledRemoveTime = loadInt;
  147. loadInt = 0;
  148. if ( !ctx.Read( loadInt ) )
  149. loadInt = 300;
  150. m_FullMaturityTime = loadInt;
  151. loadInt = 0;
  152. if ( !ctx.Read( loadInt ) )
  153. loadInt = 300;
  154. m_SpoilAfterFullMaturityTime = loadInt;
  155. loadInt = 0;
  156. if ( !ctx.Read( loadInt ) )
  157. return false;
  158. m_StateChangeTime = loadInt;
  159. float loadFloat = 0.0;
  160. if ( !ctx.Read( loadFloat ) )
  161. loadFloat = 0;
  162. m_InfestationChance = loadFloat;
  163. loadInt = 0;
  164. if ( !ctx.Read( loadInt ) )
  165. return false;
  166. m_GrowthStagesCount = loadInt;
  167. loadInt = 0;
  168. if ( !ctx.Read( loadInt ) )
  169. loadInt = 1;
  170. m_CropsCount = loadInt;
  171. string loadString = "";
  172. if ( !ctx.Read( loadString ) )
  173. return false;
  174. m_CropsType = loadString;
  175. loadFloat = 0.0;
  176. if ( !ctx.Read( loadFloat ) )
  177. loadFloat = 1;
  178. m_PlantMaterialMultiplier = loadFloat;
  179. loadInt = 0;
  180. if ( !ctx.Read( loadInt ) )
  181. loadInt = 1;
  182. m_PlantState = loadInt;
  183. loadInt = 0;
  184. if ( !ctx.Read( loadInt ) )
  185. loadInt = 0;
  186. m_PlantStateIndex = loadInt;
  187. loadFloat = 0.0;
  188. if ( !ctx.Read( loadFloat ) )
  189. loadFloat = 1;
  190. m_CurrentPlantMaterialQuantity = loadFloat;
  191. bool loadBool = false;
  192. if ( !ctx.Read( loadBool ) )
  193. loadBool = false;
  194. m_IsInfested = loadBool;
  195. loadFloat = 0.0;
  196. if ( !ctx.Read( loadFloat ) )
  197. loadFloat = 0;
  198. m_SprayQuantity = loadFloat;
  199. loadBool = false;
  200. if ( ctx.Read( loadBool ) )
  201. {
  202. if ( loadBool )
  203. {
  204. if (GetGame().IsServer())
  205. {
  206. m_GrowthTimer = new Timer( CALL_CATEGORY_SYSTEM );
  207. m_GrowthTimer.Run( m_StateChangeTime, this, "GrowthTimerTick", NULL, true );
  208. }
  209. }
  210. }
  211. else
  212. {
  213. return false;
  214. }
  215. loadFloat = 0.0;
  216. if ( ctx.Read( loadFloat ) )
  217. {
  218. if ( loadFloat > 0.0 )
  219. {
  220. if (GetGame().IsServer())
  221. {
  222. m_InfestationTimer = new Timer( CALL_CATEGORY_SYSTEM );
  223. m_InfestationTimer.Run( loadFloat, this, "InfestationTimerTick", NULL, false );
  224. }
  225. }
  226. }
  227. else
  228. {
  229. return false;
  230. }
  231. loadFloat = 0.0;
  232. if ( ctx.Read( loadFloat ) )
  233. {
  234. if ( loadFloat > 0.0 )
  235. {
  236. if (GetGame().IsServer())
  237. {
  238. m_SpoilAfterFullMaturityTimer = new Timer( CALL_CATEGORY_SYSTEM );
  239. m_SpoilAfterFullMaturityTimer.Run( loadFloat, this, "SetSpoiled", NULL, false );
  240. }
  241. }
  242. }
  243. else
  244. {
  245. return false;
  246. }
  247. loadFloat = 0.0;
  248. if ( ctx.Read( loadFloat ) )
  249. {
  250. if ( loadFloat > 0.0 )
  251. {
  252. if (GetGame().IsServer())
  253. {
  254. if (!m_SpoiledRemoveTimer)
  255. m_SpoiledRemoveTimer = new Timer( CALL_CATEGORY_SYSTEM );
  256. m_SpoiledRemoveTimer.Run( loadFloat, this, "SpoiledRemoveTimerTick", NULL, false );
  257. }
  258. }
  259. }
  260. else
  261. {
  262. return false;
  263. }
  264. loadFloat = 0.0;
  265. if ( ctx.Read( loadFloat ) )
  266. {
  267. if ( loadFloat > 0.0 )
  268. {
  269. if (GetGame().IsServer())
  270. {
  271. m_DeleteDryPlantTimer = new Timer( CALL_CATEGORY_SYSTEM );
  272. m_DeleteDryPlantTimer.Run( loadFloat, this, "DeleteDryPlantTick", NULL, false );
  273. }
  274. }
  275. }
  276. else
  277. {
  278. return false;
  279. }
  280. UpdatePlant();
  281. return true;
  282. }
  283. void OnStoreSaveCustom( ParamsWriteContext ctx )
  284. {
  285. ctx.Write( m_SprayUsage );
  286. ctx.Write( m_DeleteDryPlantTime );
  287. ctx.Write( m_SpoiledRemoveTime );
  288. ctx.Write( m_FullMaturityTime );
  289. ctx.Write( m_SpoilAfterFullMaturityTime );
  290. ctx.Write( m_StateChangeTime );
  291. ctx.Write( m_InfestationChance );
  292. ctx.Write( m_GrowthStagesCount );
  293. ctx.Write( m_CropsCount );
  294. ctx.Write( m_CropsType );
  295. ctx.Write( m_PlantMaterialMultiplier );
  296. ctx.Write( m_PlantState );
  297. ctx.Write( m_PlantStateIndex );
  298. ctx.Write( m_CurrentPlantMaterialQuantity );
  299. ctx.Write( m_IsInfested );
  300. ctx.Write( m_SprayQuantity );
  301. bool saveBool = false;
  302. if ( m_GrowthTimer != NULL )
  303. {
  304. saveBool = true;
  305. }
  306. ctx.Write( saveBool );
  307. float saveFloat = 0.0;
  308. if ( m_InfestationTimer != NULL )
  309. {
  310. saveFloat = m_InfestationTimer.GetRemaining();
  311. }
  312. ctx.Write( saveFloat );
  313. saveFloat = 0.0;
  314. if ( m_SpoilAfterFullMaturityTimer != NULL )
  315. {
  316. saveFloat = m_SpoilAfterFullMaturityTimer.GetRemaining();
  317. }
  318. ctx.Write( saveFloat );
  319. saveFloat = 0.0;
  320. if ( m_SpoiledRemoveTimer != NULL )
  321. {
  322. saveFloat = m_SpoiledRemoveTimer.GetRemaining();
  323. }
  324. ctx.Write( saveFloat );
  325. saveFloat = 0.0;
  326. if ( m_DeleteDryPlantTimer != NULL )
  327. {
  328. saveFloat = m_DeleteDryPlantTimer.GetRemaining();
  329. }
  330. ctx.Write( saveFloat );
  331. }
  332. void PrintValues()
  333. {
  334. Print("PRINT ALL VALUES OF PLANT...");
  335. Print(this);
  336. Print(m_HasCrops);
  337. Print(m_PlantState);
  338. Print(m_PlantStateIndex);
  339. Print(m_CurrentPlantMaterialQuantity);
  340. Print(m_IsInfested);
  341. Print(m_SprayQuantity);
  342. Print(m_Slot);
  343. Print(m_GardenBase);
  344. Print("----------------------------------------------------------");
  345. }
  346. override bool CanPutInCargo( EntityAI parent )
  347. {
  348. return super.CanPutInCargo(parent);
  349. }
  350. override bool CanPutIntoHands( EntityAI player )
  351. {
  352. return super.CanPutIntoHands(parent);
  353. }
  354. override bool CanRemoveFromHands( EntityAI player )
  355. {
  356. return false;
  357. }
  358. void ChangeInfestation( bool is_infested )
  359. {
  360. m_IsInfested = is_infested;
  361. string plant_type = GetType();
  362. PlantMaterialHealth material = m_ModuleHorticulture.GetPlantMaterial( plant_type );
  363. if ( m_IsInfested )
  364. {
  365. if ( material.m_InfestedTex != "" )
  366. {
  367. SetObjectTexture( 0, material.m_InfestedTex );
  368. }
  369. if ( material.m_InfestedMat != "" )
  370. {
  371. SetObjectMaterial( 0, material.m_InfestedMat );
  372. }
  373. }
  374. else
  375. {
  376. if ( material.m_HealthyTex != "" )
  377. {
  378. SetObjectTexture( 0, material.m_HealthyTex );
  379. }
  380. if ( material.m_HealthyMat != "" )
  381. {
  382. SetObjectMaterial( 0, material.m_HealthyMat );
  383. }
  384. }
  385. }
  386. void UpdatePlant()
  387. {
  388. if ( m_PlantStateIndex > 0 )
  389. {
  390. string plant_state_index = m_PlantStateIndex.ToStringLen(2);
  391. string prev_plant_state_index = ( m_PlantStateIndex - 1 ).ToStringLen( 2 );
  392. // HIDING PREVIOUS PLANT STATE AND SHOWING THE CURRENT ONE
  393. ShowSelection( "plantStage_" + plant_state_index ); // SHOW!
  394. HideSelection( "plantStage_" + prev_plant_state_index ); // HIDE!
  395. // HIDING PREVIOUS CROPS STATE AND SHOWING THE CURRENT ONE
  396. if ( HasCrops() )
  397. {
  398. ShowSelection( "plantStage_" + plant_state_index + "_crops" ); // SHOW!
  399. HideSelection( "plantStage_" + prev_plant_state_index + "_crops" ); // HIDE!
  400. }
  401. else
  402. {
  403. HideSelection( "plantStage_" + plant_state_index + "_crops" ); // HIDE!
  404. HideSelection( "plantStage_" + prev_plant_state_index + "_crops" ); // HIDE!
  405. }
  406. // HIDING PREVIOUS SHADOW STATE AND SHOWING THE CURRENT ONE
  407. ShowSelection( "plantStage_" + plant_state_index + "_shadow" ); // SHOW!
  408. HideSelection( "plantStage_" + prev_plant_state_index + "_shadow" ); // HIDE!
  409. }
  410. float float_plant_state_index = (float)m_PlantStateIndex;
  411. m_CurrentPlantMaterialQuantity = m_PlantMaterialMultiplier * float_plant_state_index;
  412. }
  413. void GrowthTimerTick()
  414. {
  415. if ( IsGrowing() )
  416. {
  417. if ( m_PlantStateIndex < m_GrowthStagesCount - 2 )
  418. {
  419. m_PlantStateIndex++;
  420. UpdatePlant();
  421. SetSynchDirty();
  422. if ( m_PlantStateIndex == 0 )
  423. {
  424. float infestation_time_min = (float)m_FullMaturityTime * 0.2;
  425. int int_infestation_time_min = (int)infestation_time_min;
  426. float infestation_time_max = (float)m_FullMaturityTime * 0.6;
  427. int int_infestation_time_max = (int)infestation_time_max;
  428. if (GetGame().IsServer())
  429. {
  430. if (!m_InfestationTimer)
  431. m_InfestationTimer = new Timer( CALL_CATEGORY_SYSTEM );
  432. m_InfestationTimer.Run( Math.RandomInt(int_infestation_time_min, int_infestation_time_max), this, "InfestationTimerTick", NULL, false );
  433. }
  434. }
  435. if ( m_PlantStateIndex == m_GrowthStagesCount - 2 )
  436. {
  437. if ( m_IsInfested )
  438. {
  439. SetSpoiled();
  440. }
  441. else
  442. {
  443. SetPlantState(STATE_MATURE);
  444. }
  445. }
  446. }
  447. }
  448. else if ( IsMature() )
  449. {
  450. if (GetGame().IsServer())
  451. {
  452. if (!m_SpoilAfterFullMaturityTimer)
  453. m_SpoilAfterFullMaturityTimer = new Timer( CALL_CATEGORY_SYSTEM );
  454. if ( !m_SpoilAfterFullMaturityTimer.IsRunning() )
  455. m_SpoilAfterFullMaturityTimer.Run( m_SpoilAfterFullMaturityTime, this, "SetSpoiled", NULL, false );
  456. }
  457. }
  458. }
  459. void InfestationTimerTick()
  460. {
  461. float infestation_rnd = Math.RandomFloat01();
  462. if ( m_InfestationChance > infestation_rnd )
  463. {
  464. ChangeInfestation( true );
  465. }
  466. }
  467. void SpoiledRemoveTimerTick()
  468. {
  469. if ( m_GrowthTimer != NULL )
  470. {
  471. m_GrowthTimer.Stop();
  472. }
  473. RemoveSlot();
  474. }
  475. void DeleteDryPlantTick()
  476. {
  477. /*if ( IsDry() )
  478. {
  479. RemoveSlot();
  480. }*/
  481. }
  482. void SetSpoiled()
  483. {
  484. if ( IsSpoiled() == false )
  485. {
  486. m_PlantStateIndex++;
  487. SetPlantState(STATE_SPOILED);
  488. UpdatePlant();
  489. SetSynchDirty();
  490. if (GetGame().IsServer())
  491. {
  492. if (!m_SpoiledRemoveTimer)
  493. m_SpoiledRemoveTimer = new Timer( CALL_CATEGORY_SYSTEM );
  494. if (!m_SpoiledRemoveTimer.IsRunning())
  495. m_SpoiledRemoveTimer.Run( m_SpoiledRemoveTime, this, "SpoiledRemoveTimerTick", NULL, false );
  496. }
  497. }
  498. }
  499. void CheckWater()
  500. {
  501. if ( !IsMature() && !NeedsWater() )
  502. {
  503. if ( m_DeleteDryPlantTimer )
  504. {
  505. m_DeleteDryPlantTimer.Stop();
  506. }
  507. SetPlantState(STATE_GROWING);
  508. if (GetGame().IsServer())
  509. {
  510. m_GrowthTimer = new Timer( CALL_CATEGORY_SYSTEM );
  511. m_GrowthTimer.Run( m_StateChangeTime, this, "GrowthTimerTick", NULL, true );
  512. }
  513. }
  514. }
  515. //NEW METHOD FOR PLANT SPRAYING
  516. void SprayPlant( float consumed_quantity )
  517. {
  518. //Rework this to have something smooth
  519. m_SprayQuantity += consumed_quantity;
  520. if ( !NeedsSpraying() )
  521. {
  522. if ( m_InfestationTimer != NULL )
  523. {
  524. m_InfestationTimer.Stop();
  525. }
  526. m_IsInfested = false;
  527. m_InfestationChance = 0;
  528. ChangeInfestation( false );
  529. UpdatePlant();
  530. }
  531. }
  532. //DEPRECATED
  533. string StopInfestation( float consumed_quantity )
  534. {
  535. m_SprayQuantity += consumed_quantity;
  536. if ( !NeedsSpraying() )
  537. {
  538. if ( m_InfestationTimer != NULL )
  539. {
  540. m_InfestationTimer.Stop();
  541. }
  542. m_IsInfested = false;
  543. m_InfestationChance = 0;
  544. ChangeInfestation( false );
  545. UpdatePlant();
  546. return "I've sprayed the plant a bit. Now it is enough spayed.";
  547. }
  548. else
  549. {
  550. return "I've sprayed the plant a bit.";
  551. }
  552. }
  553. //DEPRECATED
  554. void RemovePlant()
  555. {
  556. if ( GetGame() && GetGame().IsServer() )
  557. {
  558. UnlockFromParent();
  559. if ( m_CurrentPlantMaterialQuantity > 0.0 )
  560. {
  561. vector pos = GetPosition();
  562. ItemBase item = ItemBase.Cast( GetGame().CreateObjectEx( "PlantMaterial", pos, ECE_PLACE_ON_SURFACE ) );
  563. item.SetQuantity( m_CurrentPlantMaterialQuantity * 1000.0 );
  564. }
  565. RemoveSlot();
  566. }
  567. }
  568. void RemovePlantEx( vector pos )
  569. {
  570. if ( GetGame() && GetGame().IsServer() )
  571. {
  572. UnlockFromParent();
  573. if ( m_CurrentPlantMaterialQuantity > 0.0 )
  574. {
  575. ItemBase item = ItemBase.Cast( GetGame().CreateObjectEx( "PlantMaterial", pos, ECE_PLACE_ON_SURFACE ) );
  576. item.SetQuantity( m_CurrentPlantMaterialQuantity * 1000.0 );
  577. }
  578. RemoveSlot();
  579. }
  580. }
  581. void DestroyPlant()
  582. {
  583. if ( GetGame() && GetGame().IsServer() )
  584. {
  585. UnlockFromParent();
  586. RemoveSlot();
  587. }
  588. }
  589. void Harvest( PlayerBase player )
  590. {
  591. //TODO Boris: Add soft skill 2.0
  592. //PluginExperience module_exp = GetPlugin(PluginExperience);
  593. //float harvesting_efficiency = module_exp.GetExpParamNumber(player, PluginExperience.EXP_FARMER_HARVESTING, "efficiency");
  594. //m_CropsCount = m_CropsCount * harvesting_efficiency;
  595. if ( !IsSpoiled() )
  596. {
  597. for ( int i = 0; i < m_CropsCount; i++ )
  598. {
  599. vector pos = player.GetPosition();
  600. ItemBase item = ItemBase.Cast( GetGame().CreateObjectEx( m_CropsType, pos, ECE_PLACE_ON_SURFACE ) );
  601. item.SetQuantity( item.GetQuantityMax() );
  602. }
  603. }
  604. m_HasCrops = false;
  605. SetSynchDirty();
  606. UpdatePlant();
  607. }
  608. void SetPlantState(int state)
  609. {
  610. m_PlantState = state;
  611. SetSynchDirty();
  612. }
  613. int GetPlantState()
  614. {
  615. return m_PlantState;
  616. }
  617. int GetPlantStateIndex()
  618. {
  619. return m_PlantStateIndex;
  620. }
  621. float GetWater()
  622. {
  623. if ( GetSlot() )
  624. return GetSlot().GetWater();
  625. return 0;
  626. }
  627. float GetWaterMax()
  628. {
  629. if ( GetSlot() )
  630. return GetSlot().GetWaterUsage();
  631. return 0;
  632. }
  633. bool NeedsWater()
  634. {
  635. Slot slotPlant = m_Slot;
  636. if ( IsDry() && slotPlant && slotPlant.GetWater() < slotPlant.GetWaterUsage() )
  637. {
  638. return true;
  639. }
  640. else
  641. {
  642. return false;
  643. }
  644. }
  645. bool NeedsSpraying()
  646. {
  647. if ( m_SprayQuantity < m_SprayUsage )
  648. {
  649. return true;
  650. }
  651. else
  652. {
  653. return false;
  654. }
  655. }
  656. float GetSprayQuantity()
  657. {
  658. return m_SprayQuantity;
  659. }
  660. float GetSprayUsage()
  661. {
  662. return m_SprayUsage;
  663. }
  664. void RemoveSlot()
  665. {
  666. GardenBase garden = GardenBase.Cast( GetHierarchyParent() );
  667. if ( garden )
  668. {
  669. if (m_SpoiledRemoveTimer)
  670. {
  671. m_SpoiledRemoveTimer.Stop();
  672. m_SpoiledRemoveTimer = NULL;
  673. }
  674. garden.RemoveSlotPlant( this );
  675. }
  676. }
  677. void SetSlot(Slot slot)
  678. {
  679. if ( slot )
  680. {
  681. m_Slot = slot;
  682. }
  683. }
  684. Slot GetSlot()
  685. {
  686. return m_Slot;
  687. }
  688. GardenBase GetGarden()
  689. {
  690. return m_GardenBase;
  691. }
  692. bool IsDry()
  693. {
  694. if ( GetPlantState() == STATE_DRY )
  695. {
  696. return true;
  697. }
  698. else
  699. {
  700. return false;
  701. }
  702. }
  703. bool IsGrowing()
  704. {
  705. if ( GetPlantState() == STATE_GROWING )
  706. {
  707. return true;
  708. }
  709. else
  710. {
  711. return false;
  712. }
  713. }
  714. bool IsMature()
  715. {
  716. if ( GetPlantState() == STATE_MATURE )
  717. {
  718. return true;
  719. }
  720. else
  721. {
  722. return false;
  723. }
  724. }
  725. bool IsSpoiled()
  726. {
  727. if ( GetPlantState() == STATE_SPOILED )
  728. {
  729. return true;
  730. }
  731. else
  732. {
  733. return false;
  734. }
  735. }
  736. bool HasCrops()
  737. {
  738. return m_HasCrops;
  739. }
  740. override void SetActions()
  741. {
  742. super.SetActions();
  743. AddAction(ActionHarvestCrops);
  744. AddAction(ActionRemovePlant);
  745. }
  746. }