/// <summary>
        /// Create an uninitialized LSAgent
        /// </summary>
        /// <returns>The raw agent.</returns>
        /// <param name="agentCode">Agent code.</param>
        /// <param name="isBare">If set to <c>true</c> is bare.</param>
        public static LSAgent CreateRawAgent(string agentCode)
        {
            if (!IsValidAgentCode(agentCode))
            {
                throw new System.ArgumentException(string.Format("Agent code '{0}' not found.", agentCode));
            }
            FastStack <LSAgent> cache    = CachedAgents [agentCode];
            LSAgent             curAgent = null;

            if (cache.IsNotNull() && cache.Count > 0)
            {
                curAgent = cache.Pop();
                ushort agentCodeID = AgentController.GetAgentCodeIndex(agentCode);
                Debug.Log(curAgent.TypeIndex);
                TypeAgentsActive [agentCodeID] [curAgent.TypeIndex] = true;
            }
            else
            {
                IAgentData interfacer = AgentController.CodeInterfacerMap [agentCode];

                curAgent = GameObject.Instantiate(AgentController.GetAgentTemplate(agentCode).gameObject).GetComponent <LSAgent> ();
                curAgent.Setup(interfacer);

                RegisterRawAgent(curAgent);
            }
            return(curAgent);
        }
        public static void DestroyAgent(LSAgent agent, bool Immediate = false)
        {
            agent.Deactivate(Immediate);

            ushort agentCodeID = AgentController.GetAgentCodeIndex(agent.MyAgentCode);

            TypeAgentsActive[agentCodeID][agent.TypeIndex] = false;

            ChangeController(agent, null);
        }
        public LSAgent CreateAgent(
            string agentCode,
            Vector2d?position = null,  //nullable position
            Vector2d?rotation = null   //Nullable rotation for default parametrz
            )
        {
            Vector2d pos = position != null ? position.Value : new Vector2d(0, 0);
            Vector2d rot = rotation != null ? rotation.Value : Vector2d.radian0;


            if (!IsValidAgentCode(agentCode))
            {
                throw new System.ArgumentException(string.Format("Agent code '{0}' not found.", agentCode));
            }


            FastStack <LSAgent> cache    = CachedAgents [agentCode];
            LSAgent             curAgent = null;
            ushort agentCodeID           = AgentController.GetAgentCodeIndex(agentCode);

            if (cache.IsNotNull() && cache.Count > 0)
            {
                curAgent = cache.Pop();

                TypeAgentsActive[agentCodeID][curAgent.TypeIndex] = true;
            }
            else
            {
                IAgentData interfacer = AgentController.CodeInterfacerMap [agentCode];

                curAgent = GameObject.Instantiate(interfacer.GetAgent().gameObject).GetComponent <LSAgent>();
                curAgent.Setup(interfacer);


                FastList <bool> typeActive;
                if (!AgentController.TypeAgentsActive.TryGetValue(agentCodeID, out typeActive))
                {
                    typeActive = new FastList <bool>();
                    TypeAgentsActive.Add(agentCodeID, typeActive);
                }
                FastList <LSAgent> typeAgents;
                if (!TypeAgents.TryGetValue(agentCodeID, out typeAgents))
                {
                    typeAgents = new FastList <LSAgent>();
                    TypeAgents.Add(agentCodeID, typeAgents);
                }

                curAgent.TypeIndex = (ushort)typeAgents.Count;
                typeAgents.Add(curAgent);
                typeActive.Add(true);
            }
            InitializeAgent(curAgent, pos, rot);
            return(curAgent);
        }
        private static void DestroyAgentBuffer(DeactivationData data)
        {
            LSAgent agent = data.Agent;

            if (agent.IsActive == false)
            {
                return;
            }
            bool immediate = data.Immediate;

            agent.Deactivate(immediate);

            ushort agentCodeID = AgentController.GetAgentCodeIndex(agent.MyAgentCode);

            TypeAgentsActive [agentCodeID] [agent.TypeIndex] = false;

            ChangeController(agent, null);
        }
        public static void RegisterRawAgent(LSAgent agent)
        {
            var             agentCodeID = AgentController.GetAgentCodeIndex(agent.MyAgentCode);
            FastList <bool> typeActive;

            if (!AgentController.TypeAgentsActive.TryGetValue(agentCodeID, out typeActive))
            {
                typeActive = new FastList <bool> ();
                TypeAgentsActive.Add(agentCodeID, typeActive);
            }
            FastList <LSAgent> typeAgents;

            if (!TypeAgents.TryGetValue(agentCodeID, out typeAgents))
            {
                typeAgents = new FastList <LSAgent> ();
                TypeAgents.Add(agentCodeID, typeAgents);
            }

            //TypeIndex of ushort.MaxValue means that this agent isn't registered for the pool
            agent.TypeIndex = (ushort)(typeAgents.Count);
            typeAgents.Add(agent);
            typeActive.Add(true);
        }