public static void applyPlantDamage(Plant plantItem, float baseDamage) { // Clamp temps outside of the 0-40, below 0 and above 40 there is spoiling float tempC = PerishableItemsHelpers.unClamp(plantItem.WorldAtmosphere.Temperature - 273.15f, 0.0f, 40.0f); // Clamp again between 0 and 40 tempC = PerishableItemsHelpers.Clamp(tempC, 0, 40); float damage = baseDamage * (tempC / 40.0f); plantItem.DamageState.Brute += 0.010f * damage; plantItem.DamageState.Toxic += 0.001f * damage; plantItem.NutritionValue -= 0.001f * damage; #if DEBUG if (damage > 0.0f) { UnityEngine.Debug.Log( " Plant ID: " + plantItem.ReferenceId + " Name: " + plantItem.DisplayName + " Temp: " + plantItem.WorldAtmosphere.Temperature + " Damage: " + damage + " CO2: " + plantItem.WorldAtmosphere.GasMixture.CarbonDioxide.Quantity ); } #endif }
public static void applyFoodDamage(Food foodItem, float baseDamage) { // TODO: maybe move these settings to the config file? // Clamp the temperature value between 0C and 40C, below 0C there is no spoiling, beyond 40C takes max damage. float tempC = PerishableItemsHelpers.Clamp(foodItem.WorldAtmosphere.Temperature - 273.15f, 0.0f, 40.0f); float damage = baseDamage * (tempC / 40.0f) * Math.Max(foodItem.WorldAtmosphere.ParticalPressureO2, 0.01f) / 1000f; // Damage applied based on the type of food container switch (foodItem.PrefabHash) { // Canned food has a special treatment, it will last longer while the container hasn't been opened, after // that it will be treated as any other food (spoiling time based on the item heatpoints). case 1327248310: // Milk case 791746840: // Cereal Bar case 688734890: // Tomato Soup if (foodItem.RemainingRatio >= 1) { damage *= PerishableItemsPlugin.PluginCannedMultiplier.Value; } break; case 1387403148: // Soy oil damage *= PerishableItemsPlugin.PluginBottledMultiplier.Value; break; } // Add a little toxicity to the food, modified by the nutrition value of the item, the toxicity // will be used later to apply stun damage to the player ingesting this food item. // foodItem.NutritionValue *= 0.98f; // to make food lose nutrients foodItem.DamageState.Brute += 0.002f * damage; foodItem.DamageState.Toxic += 0.001f * damage; foodItem.NutritionValue -= 0.001f * damage; #if DEBUG if (damage > 0.0f) { UnityEngine.Debug.Log( " Food ID: " + foodItem.ReferenceId + " Name: " + foodItem.DisplayName + " Temp: " + foodItem.WorldAtmosphere.Temperature + " Damage: " + damage + " O2: " + foodItem.WorldAtmosphere.GasMixture.Oxygen.Quantity + " O2p: " + foodItem.WorldAtmosphere.ParticalPressureO2 ); } #endif }
static void Prefix(DynamicThing __instance) { // The code has been thought as an -exit as soon as you can- to prevent unnecessary execution since // the Update() function per-item is going to be stressing. try { if (!GameManager.IsServer || WorldManager.IsPaused || WorldManager.Instance.GameMode != GameMode.Survival) { return; } if (__instance == null) { return; } // Ignore anything inside a closed container (not locker) if (PerishableItemsPlugin.PluginAllowSealed.Value == true && __instance.ParentSlot != null && __instance.ParentSlot.Parent.GetType() == typeof(Container) && !__instance.ParentSlot.Parent.IsOpen) { return; } // Despite some having nutritients, Veggies and plants are not foodItems so we need both checks. Plant plantItem = __instance as Plant; Food foodItem = __instance as Food; if (foodItem == null && plantItem == null) { return; } // On the decidated server, the atmosphere wont get the first update until a player logs in. if (__instance.WorldAtmosphere == null) { return; } // From here onwards we might be applying damage, this baseDamage will act as a multiplier. float baseDamage = PerishableItemsPlugin.PluginDecayMultiplier.Value; // Apply x4 decay damage if the item is floating around, in the player hands or in any container that is open if (__instance.ParentSlot == null || __instance.ParentSlot.IsHandSlot || __instance.ParentSlot.Parent.IsOpen) { baseDamage *= PerishableItemsPlugin.PluginUnprotectedMultiplier.Value; } // Finally if (foodItem != null) { PerishableItemsHelpers.applyFoodDamage(foodItem, baseDamage); } if (plantItem != null) { PerishableItemsHelpers.applyPlantDamage(plantItem, baseDamage); } #if DEBUG if (__instance.DamageState.Brute > 0) { float NutritionValue = (foodItem != null) ? foodItem.NutritionValue : plantItem.NutritionValue; UnityEngine.Debug.Log( " ID: " + __instance.ReferenceId + " Name: " + __instance.DisplayName + " Max: " + __instance.DamageState.MaxDamage + " Brute: " + __instance.DamageState.Brute + " Toxic: " + __instance.DamageState.Toxic + " Nutri: " + NutritionValue); } #endif } catch (Exception e) { UnityEngine.Debug.Log( " Inconsistent item damage for instance ReferenceId: " + __instance.ReferenceId + "\n" + e.Message); } }