Posts: 175
Thanks: 175 in 1 Posts
4 Achievements
The code of the interaction. Here's the complete code, for the context:
Code:
public class Haunt : Interaction<Sim, Sim>, IHauntInteraction
{
public class Definition : SoloSimInteractionDefinition<Haunt>
{
public override bool Test(Sim a, Sim target, bool isAutonomous, ref GreyedOutTooltipCallback greyedOutTooltipCallback)
{
if (target is IRockingRider && !a.SimDescription.ChildOrBelow)
{
return false;
}
if (a.SimDescription.IsGhost && !a.Autonomy.Motives.HasMotive(CommodityKind.BeAngryGhost))
{
if (!a.CanHauntObjects)
{
greyedOutTooltipCallback = InteractionInstance.CreateTooltipCallback(Sims3.Gameplay.Actors.Sim.LocalizeString("RecentlyExorcised"));
return false;
}
return true;
}
return false;
}
}
[TunableComment("Description: Number of sim minutes a ghost sim will be unable to haunt after being exorcised")]
[Tunable]
public static float kExorcisedHauntTimeout = 60f;
public static ISoloInteractionDefinition Singleton = new Definition();
public GameObject mObject;
public ObjectSound hauntLoop;
public Sim mExorcisingSim;
public GameObject ObjectBeingHaunted
{
get
{
return mObject;
}
}
public override bool Run()
{
if (Actor.IsHorse)
{
mObject = GetHorseHauntableObjects();
}
if (mObject == null)
{
mObject = GetObjectForHaunting(Actor.LotCurrent);
}
if (mObject == null || !Actor.RouteToObjectRadiusAndCheckInUse(mObject, 1f))
{
return false;
}
Sims3.Gameplay.Actors.Sim.ExorciseObject.Definition definition = new Sims3.Gameplay.Actors.Sim.ExorciseObject.Definition(this);
mObject.AddInteraction(definition);
mObject.AddToUseList(Actor);
StateMachineClient stateMachineClient = StateMachineClient.Acquire(Actor, "Haunt");
stateMachineClient.SetActor("x", Actor);
stateMachineClient.SetActor("object", mObject);
if (mObject is IRockingChair)
{
stateMachineClient.SetParameter("ObjectType", HauntedObjectType.RockingChair);
}
else if (mObject is IRockingRider)
{
stateMachineClient.SetParameter("ObjectType", HauntedObjectType.RockingRider);
}
else if (mObject is IBedSingle || mObject is IAltar)
{
stateMachineClient.SetParameter("ObjectType", HauntedObjectType.SingleBed);
}
else
{
stateMachineClient.SetParameter("ObjectType", HauntedObjectType.Generic);
}
stateMachineClient.AddOneShotScriptEventHandler(1001u, EventCallbackStartSound);
stateMachineClient.AddOneShotScriptEventHandler(1002u, EventCallbackStopSound);
stateMachineClient.EnterState("x", "Enter");
stateMachineClient.RequestState("x", "HauntLoop");
BeginCommodityUpdates();
PetStartleBehavior.CheckForStartle(Actor, StartleType.GhostHaunting);
bool flag = DoTimedLoop(RandomUtil.GetFloat(3f, 10f), ExitReason.Default, 30);
stateMachineClient.RequestState("x", "Exit");
if (flag)
{
switch (Actor.SimDescription.DeathStyle)
{
case SimDescription.DeathType.Burn:
mObject.Charred = true;
break;
case SimDescription.DeathType.Drown:
{
RepairableComponent repairable2 = mObject.Repairable;
if (repairable2 != null && repairable2.ObjectRepairableType == RepairableComponent.RepairableType.Plumbing)
{
repairable2.BreakObject();
}
else
{
Sims3.Gameplay.Controllers.PuddleManager.AddPuddle(mObject.Position);
}
break;
}
case SimDescription.DeathType.Electrocution:
{
RepairableComponent repairable = mObject.Repairable;
if (repairable != null && repairable.ObjectRepairableType == RepairableComponent.RepairableType.Electrical)
{
repairable.BreakObject();
}
break;
}
}
}
if (Actor.HasExitReason(ExitReason.CanceledByScript))
{
if (mExorcisingSim != null)
{
Actor.PlayReaction(ReactionTypes.Startled, ReactionSpeed.AfterInteraction);
ThoughtBalloonManager.BalloonData balloonData = new ThoughtBalloonManager.BalloonData(mExorcisingSim.GetThumbnailKey());
balloonData.LowAxis = ThoughtBalloonAxis.kDislike;
Actor.ThoughtBalloonManager.ShowBalloon(balloonData);
Actor.ResetHauntTimeout(kExorcisedHauntTimeout);
}
}
else if (mExorcisingSim != null && mExorcisingSim.InteractionQueue.HasInteractionOfTypeAndTarget(definition, mObject))
{
mExorcisingSim.AddExitReason(ExitReason.CanceledByScript);
}
mObject.RemoveFromUseList(Actor);
mObject.RemoveInteractionByType(definition);
EndCommodityUpdates(true);
return true;
}
public void EventCallbackStartSound(StateMachineClient sender, IEvent evt)
{
hauntLoop = new ObjectSound(mObject.ObjectId, "ghost_haunt_obj_float_obj");
hauntLoop.StartLoop();
}
public void EventCallbackStopSound(StateMachineClient sender, IEvent evt)
{
hauntLoop.Dispose();
hauntLoop = null;
}
public bool ObjectTest(GameObject obj)
{
if (obj is INotHauntable)
{
IMaybeHauntable maybeHauntable = obj as IMaybeHauntable;
if (maybeHauntable != null)
{
return maybeHauntable.CanHaunt();
}
return false;
}
return true;
}
public bool OutsideObjectTest(GameObject obj)
{
if (obj.IsOutside)
{
return ObjectTest(obj);
}
return false;
}
public GameObject GetHorseHauntableObjects()
{
IHorseHauntableObject[] objects = Actor.LotCurrent.GetObjects<IHorseHauntableObject>();
if (objects.Length > 0)
{
return RandomUtil.GetRandomObjectFromList(objects) as GameObject;
}
GameObject[] objects2 = Actor.LotCurrent.GetObjects<GameObject>();
return GameObject.GetObjectForCommonInteraction(objects2, OutsideObjectTest);
}
public GameObject GetObjectForHaunting(Lot lot)
{
GameObject[] objects = lot.GetObjects<GameObject>();
GameObject[] array = objects;
foreach (GameObject gameObject in array)
{
IHauntedGnome hauntedGnome = gameObject as IHauntedGnome;
if (hauntedGnome != null && RandomUtil.RandomChance(hauntedGnome.GnomeHauntingChance) && hauntedGnome.CanHaunt())
{
return gameObject;
}
}
return GameObject.GetObjectForCommonInteraction(objects, ObjectTest);
}
public void ObjectExorcised()
{
Actor.AddExitReason(ExitReason.CanceledByScript);
}
public bool CanExorcise()
{
return mExorcisingSim == null;
}
public bool StartExorcise(Sim sim)
{
if (!CanExorcise())
{
return false;
}
mExorcisingSim = sim;
return true;
}
public override void Cleanup()
{
mExorcisingSim = null;
base.Cleanup();
}
}