bool TryUpdateExistingCreature(CreatureEntity[] herdsFinds) { if (herdsFinds.Length == 1) { CreatureEntity oldCreature = herdsFinds[0]; bool sanityFail = false; #region SANITY_CHECKS string sanityFailReason = null; // Verifying if creature parents match. // Wurm trivia: // If a creature has a mother name or a father name, these names cannot change. // However when parent dies, Wurm loses reference and the name is no longer in the log event! // father checks if (String.IsNullOrEmpty(oldCreature.FatherName) && !String.IsNullOrEmpty(creatureBuffer.FatherName)) { sanityFail = true; if (sanityFailReason == null) { sanityFailReason = "Old father was blank but new data has a father name"; } } if (!String.IsNullOrEmpty(oldCreature.FatherName) && !String.IsNullOrEmpty(creatureBuffer.FatherName) && oldCreature.FatherName != creatureBuffer.FatherName) { sanityFail = true; if (sanityFailReason == null) { sanityFailReason = "Old data father name was different than new father name"; } } // mother checks if (String.IsNullOrEmpty(oldCreature.MotherName) && !String.IsNullOrEmpty(creatureBuffer.MotherName)) { sanityFail = true; if (sanityFailReason == null) { sanityFailReason = "Old mother was blank but new data has a mother name"; } } if (!String.IsNullOrEmpty(oldCreature.MotherName) && !String.IsNullOrEmpty(creatureBuffer.MotherName) && oldCreature.MotherName != creatureBuffer.MotherName) { sanityFail = true; if (sanityFailReason == null) { sanityFailReason = "Old data mother name was different than new mother name"; } } // Verifying if creature traits match. // Have to take into account current AH level of the player, // as well as the level this creature has been previously inspected at. if (oldCreature.TraitsInspectedAtSkill.HasValue) { // Skip this check if creature had genesis cast within last 1 hour. // Genesis clears some negative traits. debugLogger.Log(string.Format("Checking creature for Genesis cast (creature name: {0}", creatureBuffer.Name)); if (!parentModule.Settings.HasGenesisCast(creatureBuffer.Name)) { debugLogger.Log("No genesis cast found"); var lowskill = Math.Min(oldCreature.TraitsInspectedAtSkill.Value, creatureBuffer.InspectSkill); CreatureTrait[] certainTraits = CreatureTrait.GetTraitsUpToSkillLevel(lowskill, oldCreature.EpicCurve ?? false); var oldCreatureTraits = oldCreature.Traits.ToArray(); var newCreatureTraits = creatureBuffer.Traits.ToArray(); foreach (var trait in certainTraits) { if (oldCreatureTraits.Contains(trait) != newCreatureTraits.Contains(trait)) { sanityFail = true; if (sanityFailReason == null) { sanityFailReason = "Trait mismatch below inspect skill treshhold (" + lowskill + "): " + trait.ToCompactString(); } break; } } } else { debugLogger.Log("Genesis cast found, skipping trait sanity check"); parentModule.Settings.RemoveGenesisCast(creatureBuffer.Name); debugLogger.Log(string.Format("Removed cached genesis cast data for {0}", creatureBuffer.Name)); } } #endregion if (sanityFail) { debugLogger.Log("sanity check failed for creature update: " + oldCreature + ". Reason: " + sanityFailReason); trayPopups.Schedule("There was data mismatch when trying to update creature, reason: " + sanityFailReason, "ERROR AT UPDATE CREATURE", 8000); } else { oldCreature.Age = creatureBuffer.Age; oldCreature.TakenCareOfBy = creatureBuffer.CaredBy; oldCreature.BrandedFor = creatureBuffer.BrandedBy; if (creatureBuffer.HasFatherName) { oldCreature.FatherName = creatureBuffer.FatherName; } if (creatureBuffer.HasMotherName) { oldCreature.MotherName = creatureBuffer.MotherName; } oldCreature.ServerName = creatureBuffer.Server.ServerName.Original; if (oldCreature.TraitsInspectedAtSkill <= creatureBuffer.InspectSkill || creatureBuffer.InspectSkill > CreatureTrait.GetFullTraitVisibilityCap(oldCreature.EpicCurve ?? false)) { oldCreature.Traits = creatureBuffer.Traits; oldCreature.TraitsInspectedAtSkill = creatureBuffer.InspectSkill; } else { debugLogger.Log("old creature data had more accurate trait info, skipping"); } oldCreature.SetTag("dead", false); oldCreature.SetSecondaryInfoTag(creatureBuffer.SecondaryInfo); oldCreature.IsMale = creatureBuffer.IsMale; oldCreature.PregnantUntil = creatureBuffer.PregnantUntil; if (oldCreature.Name != creatureBuffer.Name) { if (NameIsUniqueInHerd(creatureBuffer.Name, oldCreature.Herd)) { trayPopups.Schedule(String.Format("Updating name of creature {0} to {1}", oldCreature.Name, creatureBuffer.Name), "CREATURE NAME UPDATE"); oldCreature.Name = creatureBuffer.Name; } else { trayPopups.Schedule(String.Format("Could not update name of creature {0} to {1}, " + "because herd already has a creature with such name.", oldCreature.Name, creatureBuffer.Name), "WARNING"); } } oldCreature.SmilexamineLastDate = DateTime.Now; if (creatureBuffer.HasColorWurmLogText && grangerSettings.UpdateCreatureColorOnSmilexamines) { oldCreature.CreatureColorId = creatureColorDefinitions.GetColorIdByWurmLogText(creatureBuffer.ColorWurmLogText); } context.SubmitChanges(); debugLogger.Log("successfully updated creature in db"); trayPopups.Schedule(String.Format("Updated creature: {0}", oldCreature), "CREATURE UPDATED"); } debugLogger.Log("processor buffer cleared"); return(true); } return(false); }