using System;
using System.Net;
namespace OpenSim.Framework.Types
{
///
/// A class for manipulating RegionHandle coordinates
///
class RegionHandle
{
private UInt64 handle;
///
/// Initialises a new grid-aware RegionHandle
///
/// IP Address of the Grid Server for this region
/// Grid X Coordinate
/// Grid Y Coordinate
public RegionHandle(string ip, short x, short y)
{
IPAddress addr = IPAddress.Parse(ip);
long baseHandle = addr.Address;
// Split the IP address in half
short a = (short)((baseHandle << 16) & 0xFFFF);
short b = (short)((baseHandle << 0) & 0xFFFF);
// Raise the bounds a little
uint nx = (uint)x;
uint ny = (uint)y;
// Multiply grid coords to get region coords
nx *= 256;
ny *= 256;
// Stuff the IP address in too
nx = (uint)a << 16;
ny = (uint)b << 16;
handle = ((UInt64)nx << 32) | (uint)ny;
}
///
/// Initialises a new RegionHandle that is not inter-grid aware
///
/// Grid X Coordinate
/// Grid Y Coordinate
public RegionHandle(uint x, uint y)
{
handle = ((x * 256) << 32) | (y * 256);
}
///
/// Initialises a new RegionHandle from an existing value
///
/// A U64 RegionHandle
public RegionHandle(UInt64 Region)
{
handle = Region;
}
///
/// Returns the Grid Masked RegionHandle - For use in Teleport packets and other packets where sending the grid IP address may be handy.
///
/// Do not use for SimulatorEnable packets. The client will choke.
/// Region Handle including IP Address encoding
public UInt64 getTeleportHandle()
{
return handle;
}
///
/// Returns a RegionHandle which may be used for SimulatorEnable packets. Removes the IP address encoding and returns the lower bounds.
///
/// A U64 RegionHandle for use in SimulatorEnable packets.
public UInt64 getNeighbourHandle()
{
UInt64 mask = 0x0000FFFF0000FFFF;
return handle | mask;
}
///
/// Returns the IP Address of the GridServer from a Grid-Encoded RegionHandle
///
/// Grid Server IP Address
public IPAddress getGridIP()
{
uint a = (uint)((handle >> 16) & 0xFFFF);
uint b = (uint)((handle >> 48) & 0xFFFF);
return new IPAddress((long)(a << 16) | (long)b);
}
///
/// Returns the X Coordinate from a Grid-Encoded RegionHandle
///
/// X Coordinate
public uint getGridX()
{
uint x = (uint)((handle >> 32) & 0xFFFF);
return x;
}
///
/// Returns the Y Coordinate from a Grid-Encoded RegionHandle
///
/// Y Coordinate
public uint getGridY()
{
uint y = (uint)((handle >> 0) & 0xFFFF);
return y;
}
}
}