//*********************************************************************************************************
void CDisplayerVisualActivitySequence::update()
{
	//H_AUTO(R2_CDisplayerVisualActivitySequence_update)
	if (!_Active) return;
	if (!_Touched) return;
	_Touched = false;
	CObjectTable *activities = getProps().toTable("Components");
	if (!activities)
	{
		clear();
		return;
	}
	// get first world object parent to get start position
	nlassert(getDisplayedInstance());
	CInstance *prevZone = NULL;
	CDisplayerVisual *prevDV = getParentDV();
	if (!prevDV)
	{
		clear();
		return;
	}
	//
	clear(false);
	//
	for(uint k = 0; k < activities->getSize(); ++k)
	{
		// search next zone of activity
		CObjectTable *activity = activities->getValue(k)->toTable();
		if (!activity) continue;
		std::string activityStr = getString(activity, "Activity");
		if (activityStr == "Stand Still" || activityStr == "Inactive") continue;
		//
		CInstance *nextZone = NULL;
		std::string zoneId = getString(activity, "ActivityZoneId");
		//
		if (!zoneId.empty())
		{
			_ObserverHandles.push_back(getEditor().addInstanceObserver(zoneId, this));
			nextZone = getEditor().getInstanceFromId(zoneId);
		}

		if (!nextZone) break;
		CDisplayerVisual *nextDV = nextZone->getDisplayerVisual();
		if (!nextDV) break;

		_TraversedPrimInfos.push_back(CTraversedPrimInfo());
		_TraversedPrimInfos.back().PrimDisplay = nextDV;
		_TraversedPrimInfos.back().Visible = nextDV->getActualDisplayMode() != DisplayModeHidden;


		CWorldPosCache wpc;
		wpc.DV = nextDV;
		wpc.WorldPos2f = nextDV->getWorldPos2f();
		_WPCache.push_back(wpc);

		if (nextDV->getActualDisplayMode() != DisplayModeHidden
			&& prevDV->getActualDisplayMode() != DisplayModeHidden)
		{
			// special case for regions
			if (nextZone->isKindOf("Region"))
			{
				// first case : previous zone is not a region
				if (!prevZone || !prevZone->isKindOf("Region"))
				{
					// search shortest distance bewteen last pos and the region
					CVector entryPos;
					if (nextDV->evalEnterPoint(prevDV->evalExitPoint(), entryPos))
					{
						addFootSteps(CLine(prevDV->evalExitPoint(), entryPos));
					}
					else
					{
						addWanderSteps(prevDV->evalExitPoint());
					}
				}
				else
				{
					// region-region footsteps
					// just use the couple of vertices for which the distance is the smallest
					static std::vector<CVector2f> r0;
					static std::vector<CVector2f> r1;
					prevDV->getSonsWorldPos2f(r0);
					nextDV->getSonsWorldPos2f(r1);
					if (!r0.empty() && !r1.empty())
					{
						CVector2f p0, p1;
						float bestDist = FLT_MAX;
						for(uint k = 0; k < r0.size(); ++k)
						{
							for(uint l = 0; l < r0.size(); ++l)
							{
								float dist = (r0[k] - r1[l]).norm();
								if (dist <bestDist)
								{
									bestDist = dist;
									p0 = r0[k];
									p1 = r1[l];
								}
							}
						}
						nlassert(bestDist != FLT_MAX);
						addFootSteps(CLine(p0.asVector(), p1.asVector()));
					}
				}
			}
			else
			{
				// special case if prev zone is a region
				if (prevZone && prevZone->isKindOf("Region"))
				{
					// search shortest distance bewteen last pos and the region
					CVector entryPos;
					if (prevDV->evalEnterPoint(nextDV->evalLinkPoint(), entryPos))
					{
						addFootSteps(CLine(entryPos, nextDV->evalLinkPoint()));
					}
					else
					{
						addWanderSteps(nextDV->evalLinkPoint());
					}
				}
				else
				{
					// simple footsteps between last & new pos
					addFootSteps(CLine(prevDV->evalExitPoint(), nextDV->evalLinkPoint()));
				}
			}
		}
		prevDV   = nextDV;
		prevZone = nextZone;
	}
	//
	CGroupMap *gm = CTool::getWorldMap();
	if (!_AddedToWorldMap && gm)
	{
		gm->addDeco(this);
	}
	if (_AddedToWorldMap)
	{
		setWorldMapNumEdges((uint)_FootSteps.size());
		nlassert(gm);
		onUpdate(*gm);
	}
}
// ************************************************************************
void CObjectRefIdClient::getNameInParent(std::string &name, sint32 &indexInArray) const
{
	//H_AUTO(R2_CObjectRefIdClient_getNameInParent)
	if (_IndexInParent != -1 && _ParentInstance)
	{
		CObjectTable *parentInstanceTable = _ParentInstance->getObjectTable();
		// check that index is still valid (true most of the case unless instance has been moved)
		if (_IndexInParent <= (sint32) parentInstanceTable->getSize())
		{
			if (_IndexInParentArray == -1)
			{
				if (parentInstanceTable->getValue(_IndexInParent) == static_cast<const CObject *>(this))
				{
					name =  parentInstanceTable->getKey(_IndexInParent);
					indexInArray = -1;
					return;
				}
			}
			else
			{
				CObject *subObject = parentInstanceTable->getValue(_IndexInParent);
				if (subObject->isTable())
				{
					CObjectTable *subTable = (CObjectTable *) subObject;
					if (_IndexInParentArray < (sint32) subTable->getSize())
					{
						if (subTable->getValue(_IndexInParentArray) == static_cast<const CObject *>(this))
						{
							name =  parentInstanceTable->getKey(_IndexInParent);
							indexInArray = _IndexInParentArray;
						}
					}
				}
			}
		}
	}
	// must search name in parent (on init or when object is moved)
	updateParentInstancePtr();
	if (!_ParentInstance)
	{
		_IndexInParent = -1;
		_IndexInParentArray = -1;
		name.clear();
		indexInArray = -1;
		return;
	}
	CObjectTable *parentInstanceTable = _ParentInstance->getObjectTable();
	const CObject *ptrInParent = (parentInstanceTable == this->getParent()) ? static_cast<const CObject *>(this) : this->getParent();
	// if instance is the direct parent (e.g object is not in an array of the parent)
	for (uint k = 0; k < parentInstanceTable->getSize(); ++k)
	{
		if (parentInstanceTable->getValue(k) == ptrInParent)
		{
			_IndexInParent = k;
			if (ptrInParent == this)
			{
				_IndexInParentArray = -1;
				indexInArray = -1;
				name = parentInstanceTable->getKey(_IndexInParent);
				return;
			}
			else
			{
				// I'm in an array in my parent, retrieve the index
				for (uint l = 0; l < getParent()->getSize(); ++l)
				{
					if (getParent()->getValue(l) == static_cast<const CObject *>(this))
					{
						name = parentInstanceTable->getKey(_IndexInParent);
						_IndexInParentArray = l;
						return;
					}
				}
			}
		}
	}
	// TMP TMP
	nlwarning("=========================================");
	CLuaIHMRyzom::dumpCallStack();
	nlwarning("=========================================");
	nlwarning("ObservedObject = %s", getValue().c_str());
	CInstance *obsInstance = getEditor().getInstanceFromId(getValue().c_str());
	nlwarning("ObservedObject instance ptr = %p", obsInstance);
	nlwarning("=========================================");
	if (obsInstance)
	{
		obsInstance->getLuaProjection().dump();
		CInstance *parent = obsInstance->getParent();
		nlwarning("ObservedObject parent instance ptr = %p", parent);
		parent->getLuaProjection().dump();
	}
	nlassert(0); // not found in parent
}
//*********************************************************************************************************
void CDisplayerVisualActivitySequence::onPostRender()
{
	//H_AUTO(R2_CDisplayerVisualActivitySequence_onPostRender)
	CDisplayerVisual *entityDV = getParentDV();
	CDisplayerVisual *groupDV = getPossibleGroupDV(entityDV);
	if (!isVisible(groupDV, entityDV))
	{
		removeFromWorldMap();
		return;
	}
	//
	TSequenceState state = Hidden;
	// if this activity sequence is not the selected one in its parent then it is hidden
	updateState(state, groupDV);
	// if current selection is a son of the group...
	static std::vector<CDisplayerVisual *> groupSons;
	groupDV->getSons(groupSons);
	for(uint k = 0; k < groupSons.size(); ++k)
	{
		nlassert(groupSons[k]);
		updateState(state, groupSons[k]);
	}
	// if one of the route is selected or highlighted
	CObjectTable *activities = getProps().toTable("Components");
	if (!activities)
	{
		removeFromWorldMap();
		return;
	}
	// get first world object parent to get start position
	for(uint k = 0; k < activities->getSize(); ++k)
	{
		// search next zone of activity
		CObjectTable *activity = activities->getValue(k)->toTable();
		if (!activity) continue;
		std::string zoneId = getString(activity, "ActivityZoneId");
		if (zoneId.empty()) continue;
		std::string activityStr = getString(activity, "Activity");
		if (activityStr == "Stand Still" || activityStr == "Inactive") continue;
		CInstance *zone = getEditor().getInstanceFromId(zoneId);
		if (!zone) continue;
		CDisplayerVisual *dv = zone->getDisplayerVisual();
		if (!dv) continue;
		updateState(state, dv);
		static std::vector<CDisplayerVisual *> vertices;
		dv->getSons(vertices);
		for (uint l = 0; l < vertices.size(); ++l)
		{
			nlassert(vertices[l]);
			updateState(state, vertices[l]);
		}
	}
	//
	if (state != Hidden && entityDV)
	{
		// see if this sequence is the selected one
		sint index = entityDV->getDisplayedInstance()->getSelectedSequence();
		CObject *sequences = getProps().getParent(); // see what is my index in the sequences list of my parent
		if (sequences)
		{
			clamp(index, (sint) 0, (sint) sequences->getSize() - 1);
			if (index != sequences->findIndex(&getProps()))
			{
				state = Hidden; // not the current selected sequence
			}
		}
	}
	//
	_DecalColor = CV_FootStepDecalSelectedColor.get();
	CRGBA mapColor = CV_FootStepMapSelectedColor.get();
	switch(state)
	{
		case Hidden:
			_DecalColor = CV_FootStepDecalHiddenColor.get();
			mapColor = CV_FootStepMapHiddenColor.get();
		break;
		case HasFocus:
			_DecalColor = CV_FootStepDecalFocusedColor.get();
			mapColor = CV_FootStepMapFocusedColor.get();
		break;
	}
	for(uint k = 0; k < _WPCache.size(); ++k)
	{
		if (_WPCache[k].DV)
		{
			if (_WPCache[k].DV->getWorldPos2f() != _WPCache[k].WorldPos2f)
			{
				touch();
				break;
			}
		}
	}
	update();
	if (_AddedToWorldMap)
	{
		setWorldMapColor(mapColor);
	}
}