From f8acdeb437ff18762399c519e2416aceb2149609 Mon Sep 17 00:00:00 2001
From: Teravus Ovares
Date: Mon, 6 Oct 2008 00:00:55 +0000
Subject: * Green dots on the mainmap for avatar. * Initial implementation *
 You'll only be able to seen green dots on regions on the map that have been
 updated.

---
 .../Region/ClientStack/LindenUDP/LLClientView.cs   |  34 +++++
 .../Environment/Modules/World/NPC/NPCAvatar.cs     |   6 +
 .../Modules/World/WorldMap/WorldMapModule.cs       | 168 +++++++++++++++++++++
 .../Region/Examples/SimpleModule/MyNpcCharacter.cs |   6 +
 4 files changed, 214 insertions(+)

(limited to 'OpenSim/Region')

diff --git a/OpenSim/Region/ClientStack/LindenUDP/LLClientView.cs b/OpenSim/Region/ClientStack/LindenUDP/LLClientView.cs
index 2187bc8..92275e8 100644
--- a/OpenSim/Region/ClientStack/LindenUDP/LLClientView.cs
+++ b/OpenSim/Region/ClientStack/LindenUDP/LLClientView.cs
@@ -262,6 +262,8 @@ namespace OpenSim.Region.ClientStack.LindenUDP
 
         private DirPlacesQuery handlerDirPlacesQuery = null;
 
+        private MapItemRequest handlerMapItemRequest = null;
+
         //private TerrainUnacked handlerUnackedTerrain = null;
 
         //**
@@ -988,6 +990,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
 
         public event DirPlacesQuery OnDirPlacesQuery;
 
+        public event MapItemRequest OnMapItemRequest;
 
         // voire si c'est necessaire
         public void ActivateGesture(UUID assetId, UUID gestureId)
@@ -6215,6 +6218,16 @@ namespace OpenSim.Region.ClientStack.LindenUDP
                     //break;
                     case PacketType.MapItemRequest:
                         // TODO: handle this packet
+                        MapItemRequestPacket mirpk = (MapItemRequestPacket)Pack;
+                        //System.Console.WriteLine(mirpk.ToString());
+                        handlerMapItemRequest = OnMapItemRequest;
+                        if (handlerMapItemRequest != null)
+                        {
+                            handlerMapItemRequest(this,mirpk.AgentData.Flags, mirpk.AgentData.EstateID,
+                                    mirpk.AgentData.Godlike,mirpk.RequestData.ItemType,
+                                    mirpk.RequestData.RegionHandle);
+
+                        }
                         //m_log.Warn("[CLIENT]: unhandled MapItemRequest packet");
                         break;
                     case PacketType.TransferAbort:
@@ -6756,6 +6769,27 @@ namespace OpenSim.Region.ClientStack.LindenUDP
 
             OutPacket(packet, ThrottleOutPacketType.Task);
         }
+        public void SendMapItemReply(mapItemReply[] replies, uint mapitemtype, uint flags)
+        {
+            MapItemReplyPacket mirplk = new MapItemReplyPacket();
+            mirplk.AgentData.AgentID = AgentId;
+            mirplk.RequestData.ItemType = mapitemtype;
+            mirplk.Data = new MapItemReplyPacket.DataBlock[replies.Length];
+            for (int i = 0; i < replies.Length; i++ )
+            {
+                MapItemReplyPacket.DataBlock mrdata = new MapItemReplyPacket.DataBlock();
+                mrdata.X = replies[i].x;
+                mrdata.Y = replies[i].y;
+                mrdata.ID = replies[i].id;
+                mrdata.Extra = replies[i].Extra;
+                mrdata.Extra2 = replies[i].Extra2;
+                mrdata.Name = Utils.StringToBytes(replies[i].name);
+                mirplk.Data[i] = mrdata;
+            }
+            //System.Console.WriteLine(mirplk.ToString());
+            OutPacket(mirplk, ThrottleOutPacketType.Task);
+
+        }
 
         public void KillEndDone()
         {
diff --git a/OpenSim/Region/Environment/Modules/World/NPC/NPCAvatar.cs b/OpenSim/Region/Environment/Modules/World/NPC/NPCAvatar.cs
index 99c953c..ff54ed3 100644
--- a/OpenSim/Region/Environment/Modules/World/NPC/NPCAvatar.cs
+++ b/OpenSim/Region/Environment/Modules/World/NPC/NPCAvatar.cs
@@ -324,6 +324,8 @@ namespace OpenSim.Region.Environment.Modules.World.NPC
 
         public event DirPlacesQuery OnDirPlacesQuery;
 
+        public event MapItemRequest OnMapItemRequest;
+
 #pragma warning restore 67
 
         #endregion
@@ -895,6 +897,10 @@ namespace OpenSim.Region.Environment.Modules.World.NPC
         {
         }
 
+        public void SendMapItemReply(mapItemReply[] replies, uint mapitemtype, uint flags)
+        {
+        }
+
         public void KillEndDone()
         {
         }
diff --git a/OpenSim/Region/Environment/Modules/World/WorldMap/WorldMapModule.cs b/OpenSim/Region/Environment/Modules/World/WorldMap/WorldMapModule.cs
index bc62e07..8091c59 100644
--- a/OpenSim/Region/Environment/Modules/World/WorldMap/WorldMapModule.cs
+++ b/OpenSim/Region/Environment/Modules/World/WorldMap/WorldMapModule.cs
@@ -31,9 +31,11 @@ using System.Collections.Generic;
 using System.Drawing;
 using System.Drawing.Imaging;
 using System.IO;
+using System.Net;
 using System.Reflection;
 using OpenMetaverse;
 using OpenMetaverse.Imaging;
+using OpenMetaverse.StructuredData;
 using log4net;
 using Nini.Config;
 using OpenSim.Framework;
@@ -45,6 +47,9 @@ using OpenSim.Region.Environment.Scenes;
 using OpenSim.Region.Environment.Types;
 using Caps = OpenSim.Framework.Communications.Capabilities.Caps;
 
+using LLSD = OpenMetaverse.StructuredData.LLSD;
+using LLSDMap = OpenMetaverse.StructuredData.LLSDMap;
+using LLSDArray = OpenMetaverse.StructuredData.LLSDArray;
 
 namespace OpenSim.Region.Environment.Modules.World.WorldMap
 {
@@ -85,6 +90,7 @@ namespace OpenSim.Region.Environment.Modules.World.WorldMap
 
 
             m_scene.AddHTTPHandler(regionimage, OnHTTPGetMapImage);
+            m_scene.AddLLSDHandler("/MAP/MapItems/" + scene.RegionInfo.RegionHandle.ToString(),HandleRemoteMapItemRequest);
             //QuadTree.Subdivide();
             //QuadTree.Subdivide();
 
@@ -231,6 +237,7 @@ namespace OpenSim.Region.Environment.Modules.World.WorldMap
 
             //doFriendListUpdateOnline(client.AgentId);
             client.OnRequestMapBlocks += RequestMapBlocks;
+            client.OnMapItemRequest += HandleMapItemRequest;
         }
         private void ClientLoggedOut(UUID AgentId)
         {
@@ -238,6 +245,109 @@ namespace OpenSim.Region.Environment.Modules.World.WorldMap
         }
         #endregion
 
+        public virtual void HandleMapItemRequest(IClientAPI remoteClient, uint flags, 
+            uint EstateID, bool godlike, uint itemtype, ulong regionhandle)
+        {
+            if (itemtype == 6) // we only sevice 6 right now (avatar green dots)
+            {
+                RegionInfo mreg = m_scene.SceneGridService.RequestNeighbouringRegionInfo(regionhandle);
+                if (mreg != null)
+                {
+                    string httpserver = "http://" + mreg.ExternalEndPoint.Address.ToString() + ":" + mreg.HttpPort + "/MAP/MapItems/" + regionhandle.ToString();
+                    List<mapItemReply> returnitems = new List<mapItemReply>();
+                    LLSDMap ramap = RequestMapItems(httpserver);
+                    if (ramap.ContainsKey(itemtype.ToString()))
+                    {
+                        LLSDArray itemarray = (LLSDArray)ramap[itemtype.ToString()];
+                        for (int i = 0; i < itemarray.Count; i++)
+                        {
+                            LLSDMap mapitem = (LLSDMap)itemarray[i];
+                            mapItemReply mi = new mapItemReply();
+                            mi.x = (uint)mapitem["X"].AsInteger();
+                            mi.y = (uint)mapitem["Y"].AsInteger();
+                            mi.id = mapitem["ID"].AsUUID();
+                            mi.Extra = mapitem["Extra"].AsInteger();
+                            mi.Extra2 = mapitem["Extra2"].AsInteger();
+                            mi.name = mapitem["Name"].AsString();
+                            returnitems.Add(mi);
+                        }
+
+                    }
+                    remoteClient.SendMapItemReply(returnitems.ToArray(), itemtype, flags);
+                }
+            }
+
+        }
+
+        private LLSDMap RequestMapItems(string httpserver)
+        {
+            WebRequest mapitemsrequest = WebRequest.Create(httpserver);
+            mapitemsrequest.Method = "POST";
+            mapitemsrequest.ContentType = "application/xml+llsd";
+            LLSDMap RAMap = new LLSDMap();
+
+            string RAMapString = RAMap.ToString();
+            LLSD LLSDofRAMap = RAMap; // RENAME if this works
+
+            byte[] buffer = LLSDParser.SerializeXmlBytes(LLSDofRAMap);
+            LLSDMap responseMap = new LLSDMap();
+            Stream os = null;
+            try
+            { // send the Post
+                mapitemsrequest.ContentLength = buffer.Length;   //Count bytes to send
+                os = mapitemsrequest.GetRequestStream();
+                os.Write(buffer, 0, buffer.Length);         //Send it
+                os.Close();
+                m_log.InfoFormat("[WorldMap]: Getting MapItems from Sim {0}", httpserver);
+            }
+            catch (WebException ex)
+            {
+                m_log.InfoFormat("[WorldMap] Bad send on GetMapItems {0}", ex.Message);
+                responseMap["connect"] = LLSD.FromBoolean(false);
+
+                return responseMap;
+            }
+
+            //m_log.Info("[OGP] waiting for a reply after rez avatar send");
+            string response_mapItems_reply = null;
+            { // get the response
+                try
+                {
+                    WebResponse webResponse = mapitemsrequest.GetResponse();
+                    if (webResponse == null)
+                    {
+                        //m_log.Info("[OGP:] Null reply on rez_avatar post");
+                    }
+
+                    StreamReader sr = new StreamReader(webResponse.GetResponseStream());
+                    response_mapItems_reply = sr.ReadToEnd().Trim();
+                    //m_log.InfoFormat("[OGP]: rez_avatar reply was {0} ", response_mapItems_reply);
+
+                }
+                catch (WebException ex)
+                {
+                    //m_log.InfoFormat("[OGP]: exception on read after send of rez avatar {0}", ex.Message);
+                    responseMap["connect"] = LLSD.FromBoolean(false);
+
+                    return responseMap;
+                }
+                LLSD rezResponse = null;
+                try
+                {
+                    rezResponse = LLSDParser.DeserializeXml(response_mapItems_reply);
+
+                    responseMap = (LLSDMap)rezResponse;
+                }
+                catch (Exception ex)
+                {
+                    //m_log.InfoFormat("[OGP]: exception on parse of rez reply {0}", ex.Message);
+                    responseMap["connect"] = LLSD.FromBoolean(false);
+
+                    return responseMap;
+                }
+            }
+            return responseMap;
+        }
         /// <summary>
         /// Requests map blocks in area of minX, maxX, minY, MaxY in world cordinates
         /// </summary>
@@ -378,5 +488,63 @@ namespace OpenSim.Region.Environment.Modules.World.WorldMap
             }
             return null;
         }
+        public LLSD HandleRemoteMapItemRequest(string path, LLSD request, string endpoint)
+        {
+            uint xstart = 0;
+            uint ystart = 0;
+            
+            Helpers.LongToUInts(m_scene.RegionInfo.RegionHandle,out xstart,out ystart);
+
+            LLSDMap responsemap = new LLSDMap();
+            List<ScenePresence> avatars = m_scene.GetAvatars();
+            LLSDArray responsearr = new LLSDArray(avatars.Count);
+            LLSDMap responsemapdata = new LLSDMap();
+            int tc = System.Environment.TickCount;
+            /* 
+            foreach (ScenePresence av in avatars)
+            {
+                responsemapdata = new LLSDMap();
+                responsemapdata["X"] = LLSD.FromInteger((int)(xstart + av.AbsolutePosition.X));
+                responsemapdata["Y"] = LLSD.FromInteger((int)(ystart + av.AbsolutePosition.Y));
+                responsemapdata["ID"] = LLSD.FromUUID(UUID.Zero);
+                responsemapdata["Name"] = LLSD.FromString("TH");
+                responsemapdata["Extra"] = LLSD.FromInteger(0);
+                responsemapdata["Extra2"] = LLSD.FromInteger(0);
+                responsearr.Add(responsemapdata);
+            }
+            responsemap["1"] = responsearr;
+            */
+            if (avatars.Count == 0)
+            {
+                responsemapdata = new LLSDMap();
+                responsemapdata["X"] = LLSD.FromInteger((int)(xstart + 1));
+                responsemapdata["Y"] = LLSD.FromInteger((int)(ystart + 1));
+                responsemapdata["ID"] = LLSD.FromUUID(UUID.Zero);
+                responsemapdata["Name"] = LLSD.FromString(Util.Md5Hash(m_scene.RegionInfo.RegionName + tc.ToString()));
+                responsemapdata["Extra"] = LLSD.FromInteger(0);
+                responsemapdata["Extra2"] = LLSD.FromInteger(0);
+                responsearr.Add(responsemapdata);
+                
+                responsemap["6"] = responsearr;
+            }
+            else 
+            {
+                responsearr = new LLSDArray(avatars.Count);
+                foreach (ScenePresence av in avatars)
+                {
+                    responsemapdata = new LLSDMap();
+                    responsemapdata["X"] = LLSD.FromInteger((int)(xstart + av.AbsolutePosition.X));
+                    responsemapdata["Y"] = LLSD.FromInteger((int)(ystart + av.AbsolutePosition.Y));
+                    responsemapdata["ID"] = LLSD.FromUUID(UUID.Zero);
+                    responsemapdata["Name"] = LLSD.FromString(Util.Md5Hash(m_scene.RegionInfo.RegionName + tc.ToString()));
+                    responsemapdata["Extra"] = LLSD.FromInteger(1);
+                    responsemapdata["Extra2"] = LLSD.FromInteger(0);
+                    responsearr.Add(responsemapdata);
+                }
+                responsemap["6"] = responsearr;
+            }
+            return responsemap;
+        }
     }
+    
 }
diff --git a/OpenSim/Region/Examples/SimpleModule/MyNpcCharacter.cs b/OpenSim/Region/Examples/SimpleModule/MyNpcCharacter.cs
index 6fbab0a..60a66aa 100644
--- a/OpenSim/Region/Examples/SimpleModule/MyNpcCharacter.cs
+++ b/OpenSim/Region/Examples/SimpleModule/MyNpcCharacter.cs
@@ -219,6 +219,8 @@ namespace OpenSim.Region.Examples.SimpleModule
 
        public event DirPlacesQuery OnDirPlacesQuery;
 
+        public event MapItemRequest OnMapItemRequest;
+
 
 #pragma warning restore 67
 
@@ -892,6 +894,10 @@ namespace OpenSim.Region.Examples.SimpleModule
         {
         }
 
+        public void SendMapItemReply(mapItemReply[] replies, uint mapitemtype, uint flags)
+        {
+        }
+
         public void KillEndDone()
         {
         }
-- 
cgit v1.1