Page 1 of 1

Detecting A Mobile

Posted: Sun Jan 04, 2009 2:01 pm
by Cree A'dor
Alright, I am making a script like the one you use on Excelsior.

It is a chair that when double clicked, it will give you a PlayerVendor. The script is only for my personal gain. I am not releasing it to the public or anything, I just want to learn some new things.

I am having a problem, or two:
  1. I need to be able to make the chair detect that there is already a mobile (PlayerVendor) in it.
    I need to make it so that when the PlayerVendor is released, the chair becomes active again.
This is what I have so far:

Code: Select all

using System;
using Server;
using Server.Mobiles;
using Server.Multis;

namespace Server.Items
{
	[Flipable( 0xB4F, 0xB4E, 0xB50, 0xB51 )]
	public class VendorRentalChair : Item
	{
		// public override int LabelNumber{ get{ return 1041243; } } // a contract of employment
		
		public virtual TimeSpan JoinGameAge{ get{ return TimeSpan.FromDays( 14.0 ); } }

		private bool c_InUse = false;
		
		[CommandProperty(AccessLevel.GameMaster)]
		public bool InUse 
		{ 
			get { return c_InUse; } 
			set { c_InUse = value; InvalidateProperties(); } 
		}
		
		[Constructable]
		public VendorRentalChair() : base( 0xB4F )
		{
			Weight = 20.0;
			Movable = false;
			//LootType = LootType.Blessed;
		}

		public VendorRentalChair( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int)0 ); //version
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();
		}

		public override void OnDoubleClick( Mobile from )
		{
			if ( from.InRange( GetWorldLocation(), 2 ) )
				SummonVendor ( from );
			else
				from.SendMessage( "I can't reach that." ); 
		}
		
		public virtual void SummonVendor( Mobile from )
		{
			if ( InUse )
			{
				from.SendMessage( "That chair is already in use by another player. Please try another chair." );
			}
			else if ( ( from.CreationTime + JoinGameAge ) > DateTime.Now )
			{
				from.SendMessage ( "Your account must be at least two weeks (14 days) old in order to place a vendor here." );
			}
			else
			{
				Mobile v = new PlayerVendor( from, BaseHouse.FindHouseAt( from ) );

				v.Direction = from.Direction & Direction.Mask;
				v.MoveToWorld( this.Location, this.Map );
				
				Item chair = new FancyWoodenChairCushion();
				
				chair.MoveToWorld( this.Location, this.Map );
				
				chair.Hue = this.Hue;
				chair.ItemID = this. ItemID;
				chair.Movable = false;
				
				this.Visible = false;
				this.Z--;
				
				this.InUse = true;

				v.SayTo( from, 503246 ); // Ah! it feels good to be working again.
			}
		}
	}
}
It isn't quite as advanced as I wanted it to be, I have yet to add an "Owner" to the chair so it knows who exactly owns the chair. As of right now I went with a simple Boolean property called "InUse". I will the "Owner" property in later.

If I could get some feedback that would be helpful. :D Thank you, in advanced.

Re: Detecting A Mobile

Posted: Sun Jan 04, 2009 2:50 pm
by Undertaker
sounds cool? .... but not so much fun if your not sharing ... =[

Re: Detecting A Mobile

Posted: Sun Jan 04, 2009 4:09 pm
by Cree A'dor
Lol, you guys already have this implemented on your server. I'm just trying to make my own version so I can learn more about scripting.

Re: Detecting A Mobile

Posted: Sun Jan 04, 2009 8:53 pm
by +Colibri
Hehe, no need to use any of that "InUse". Just complicates the thing. Best to keep it as simple as you can.

Use a method that's called something like... this.GetMobilesInRange( 0 ). Returns an arraylist i think. If you have notepad++, just do a search on all the scripts for a "getmobilesinrange" and it will show you examples of use. There's also GetItemsInRange i think.

Also very useful if you download the RunUO source code, then open the Mobile.cs and Item.cs and you'll see what all methods are supported.

Re: Detecting A Mobile

Posted: Sun Jan 04, 2009 9:58 pm
by Cree A'dor
Yeah, I know that I can use the "GetMobilesInRange" but that also gets player mobiles. So, if a player is standing on the chair it might cause it to fail. Then I'd have to go in and manually fix it in-game.

I'm just lazy... lol. I like everything to be self solving. Or, "idiot proof".

When I try to make a script I try to make it in a way that is best for both staff and players. I have played as both and I know the hassels that each has. Players hate carrying so much stuff, they like organization. Staff hates being spammed all the time about, "Oh my gosh! Timmy just took all the reagents off of the Mages in the Mage Shop. Can you refresh them for me because I need to get some regs to train up my Magery... blah blah blah."

Anyways, I actually did try that method that you suggested because I know I used it in a sprinkler I made but I think that it will also notice players. I was just trying to see if there was a way that I could implement something like:

Code: Select all

If ( pv == null )
{
  chair.InUse = false;
}
Although... maybe if I do something like:

Code: Select all

public override OnDoubleClick( Mobile m )
If ( InUse == true )
{
  If ( pv == null )
  {
     chair.InUse = false;
  }
  else
  {
     // Run AddVendor
  }
else
{
  // Run AddVendor
}
I dunno, just some thoughts. I want to try to keep it all simple but I am still learning. I was taking lessons but my tutor ran out of time and into family business.

Thank you for the input. I will give it another shot.

P.S. Interested in a Paintball system? I was going to make (I actually am making them now) some gump arts for paintball guns. My current work-in-progress is coming out REALLY well, to be honest. Let me know!

Re: Detecting A Mobile

Posted: Mon Jan 05, 2009 4:16 am
by +Colibri
Just use something like this:

int num = 0;
foreach ( Mobile m in GetMobilesInRange(0) )
{
if ( m is PlayerMobile ) continue;
num ++;


//can also use:
if ( !m.Player ) num++
}

Re: Detecting A Mobile

Posted: Wed Nov 24, 2010 6:05 am
by Annabelle
Hmmm..Thats looking cool but you should...share this with others
But any how it is your own script you an share or unshare it .

Re: Detecting A Mobile

Posted: Thu Nov 25, 2010 2:59 pm
by Efanchenko_MM
Annabelle wrote:Hmmm..Thats looking cool but you should...share this with others
But any how it is your own script you an share or unshare it .
Cree A'dor already explained this is already on our shard, and that he is learning by copying something we have.