void PlanetManagerImplementation::loadPerformanceLocations() {
	info("loading performance locations...", true);

	SortedVector<ManagedReference<SceneObject*> > planetaryLocs;
	planetaryLocs.setNoDuplicateInsertPlan();

	// get hotels
	planetaryLocs = zone->getPlanetaryObjectList("hotel");
	for (int j = 0; j < planetaryLocs.size(); j++) {
		SceneObject* obj = planetaryLocs.get(j);
		addPerformanceLocation(obj);
	}

	// get theaters
	planetaryLocs = zone->getPlanetaryObjectList("guild_theater");
	for (int j = 0; j < planetaryLocs.size(); j++) {
		SceneObject* obj = planetaryLocs.get(j);
		addPerformanceLocation(obj);
	}

	// get cantinas
	planetaryLocs.removeAll();
	planetaryLocs = zone->getPlanetaryObjectList("cantina");
	for (int j = 0; j < planetaryLocs.size(); j++) {
		SceneObject* obj = planetaryLocs.get(j);
		addPerformanceLocation(obj);
	}
}
void FactionManager::awardFactionStanding(CreatureObject* player, const String& factionName) {
	if (player == NULL)
		return;

	ManagedReference<PlayerObject*> ghost = player->getPlayerObject();

	if (!factionMap.contains(factionName))
		return;

	float lose = floor((float)75); //TODO: Find the equation for this.
	float gain = floor((float)lose / 2); //you gain half of what you lose

	Faction faction = factionMap.get(factionName);
	SortedVector<String>* enemies = faction.getEnemies();
	SortedVector<String>* allies = faction.getAllies();

	ghost->decreaseFactionStanding(factionName, lose);

	//Lose faction standing to allies of the creature.
	for (int i = 0; i < allies->size(); ++i) {
		String ally = allies->get(i);
		ghost->decreaseFactionStanding(ally, lose);
	}

	//Gain faction standing to enemies of the creature.
	for (int i = 0; i < enemies->size(); ++i) {
		String enemy = enemies->get(i);
		ghost->increaseFactionStanding(enemy, gain);
	}
}
void ObjectVersionUpdateManager::updateResidences(){
	ObjectDatabase* database = ObjectDatabaseManager::instance()->loadObjectDatabase("sceneobjects", true);

	ObjectDatabaseIterator iterator(database);

	ObjectInputStream objectData(2000);
	uint64 objectID = 0;
	info("---------------Setting residences---------------------",true);
	info("Setting residence values for all active player residences ", true);

	int count = 0;
	try {

		while (iterator.getNextKeyAndValue(objectID, &objectData)) {

			String className;
			uint64 residence = 0;

			try {
				if (!Serializable::getVariable<String>(STRING_HASHCODE("_className"), &className, &objectData) ||
						!Serializable::getVariable<uint64>(STRING_HASHCODE("PlayerObject.declaredResidence"), &residence, &objectData)) {

					objectData.clear();
					continue;
				}
			} catch (...) {
				objectData.clear();
				continue;
			}

			if (className == "PlayerObject") {
			//	info("we found a Player " + String::valueOf(objectID) + " with residence " + String::valueOf(residence),true);
				SortedVector<unsigned long long> structureList;
				uint64 residence = 0;
				count++;
				printf("\r\tUpdating player residence [%d] / [?]\t", count);
				if( Serializable::getVariable< SortedVector<unsigned long long> >(STRING_HASHCODE("PlayerObject.ownedStructures"), &structureList, &objectData) &&
						Serializable::getVariable<uint64>(STRING_HASHCODE("PlayerObject.declaredResidence"), &residence, &objectData)){

					for(int i = 0; i < structureList.size(); i++){
						bool isRes = (structureList.get(i) == residence);
						//info("Owned structure " + String::valueOf(structureList.get(i)) + " residence =  " + String::valueOf(isRes),true);
						setResidence(structureList.get(i),isRes);
					}
				} else {
					info("ERROR unable to get ownedStructures for player " + String::valueOf(objectID),true);
				}

			}

			objectData.clear();
		}
	} catch (Exception& e) {
		error(e.getMessage());
		e.printStackTrace();
	}
	info("\n",true);

}
int ZoneImplementation::getInRangeActiveAreas(float x, float y, float range, SortedVector<ManagedReference<ActiveArea*> >* objects, bool readLockZone) {
	//Locker locker(_this.getReferenceUnsafeStaticCast());

	bool readlock = readLockZone && !_this.getReferenceUnsafeStaticCast()->isLockedByCurrentThread();

	//_this.getReferenceUnsafeStaticCast()->rlock(readlock);
	
	Zone* thisZone = _this.getReferenceUnsafeStaticCast();

	try {
		thisZone->rlock(readlock);
		
		SortedVector<ManagedReference<QuadTreeEntry*> > entryObjects;

		regionTree->inRange(x, y, range, entryObjects);
		
		thisZone->runlock(readlock);

		for (int i = 0; i < entryObjects.size(); ++i) {
			ActiveArea* obj = dynamic_cast<ActiveArea*>(entryObjects.get(i).get());
			objects->put(obj);
		}
	}catch (...) {
//		_this.getReferenceUnsafeStaticCast()->runlock(readlock);

		throw;
	}

//	_this.getReferenceUnsafeStaticCast()->runlock(readlock);

	return objects->size();
}
Reference<SceneObject*> PlanetManagerImplementation::findObjectTooCloseToDecoration(float x, float y, float margin) {
	SortedVector<ManagedReference<QuadTreeEntry* > > closeObjects;

	Vector3 targetPos(x, y,0);

	zone->getInRangeObjects(x, y, 256, &closeObjects, true);

	for (int i = 0; i < closeObjects.size(); ++i) {

		ManagedReference<SceneObject*> obj = cast<SceneObject*>(closeObjects.get(i).get());

		if(obj == NULL || obj->isCreatureObject() || obj->getObjectTemplate() == NULL)
			continue;

		Vector3 objVec(obj->getPositionX(), obj->getPositionY(),0);

		int squaredDistance = (obj->getObjectTemplate()->getNoBuildRadius() + margin) * (obj->getObjectTemplate()->getNoBuildRadius() + margin);

		if(objVec.squaredDistanceTo(targetPos) < squaredDistance){
			return obj;
		}

		if(obj->isStructureObject() && StructureManager::instance()->isInStructureFootprint(cast<StructureObject*>(obj.get()), x, y, margin) ){
				return obj;
		}
	}

	return NULL;

}
void JukeboxImplementation::notifyInsertToZone(Zone* zone) {
	ManagedReference<Jukebox*> jbox = _this.getReferenceUnsafeStaticCast();

	Locker _lock(jbox);

	createChildObjects();

	ManagedReference<JukeboxObserver*> observer = new JukeboxObserver(jbox);

	jbox->registerObserver(ObserverEventType::PARENTCHANGED, observer);

	ManagedReference<SceneObject*> obj = jbox->getRootParent().get();

	if (obj == NULL || !obj->isStructureObject())
		setRadius(100);
	else
		setRadius(30);

	SortedVector<ManagedReference<SceneObject*> >* children = jbox->getChildObjects();

	for (int i = 0; i < children->size(); ++i) {
		ManagedReference<SceneObject*> child = children->get(i);

		if (child != NULL && child->isActiveArea()) {
			ManagedReference<ActiveArea*> area = cast<ActiveArea*>(child.get());
			area->registerObserver(ObserverEventType::ENTEREDAREA, observer);
			area->registerObserver(ObserverEventType::EXITEDAREA, observer);
		}
	}
}
int SceneObjectImplementation::inRangeObjects(unsigned int gameObjectType, float range) {
	if (getZone() == NULL)
		return 0;

	int numberOfObjects = 0;

	Locker zoneLocker(getZone());

	SortedVector<ManagedReference<QuadTreeEntry*> > closeSceneObjects;
	int maxInRangeObjectCount = 0;

	if (closeobjects == NULL) {
		info("Null closeobjects vector in SceneObjectImplementation::inRangeObjects", true);
		zone->getInRangeObjects(getPositionX(), getPositionY(), range, &closeSceneObjects, true);

		maxInRangeObjectCount = closeSceneObjects.size();
	} else
		maxInRangeObjectCount = closeobjects->size();

	for (int i = 0; i < maxInRangeObjectCount; ++i) {
		SceneObject* scno;

		if (closeobjects != NULL)
			scno = cast<SceneObject*>( closeobjects->get(i).get());
		else
			scno = cast<SceneObject*>(closeSceneObjects.get(i).get());

		if (scno->isInRange(_this.get(), range) && scno->getGameObjectType() == gameObjectType)
			++numberOfObjects;
	}

	return numberOfObjects;
}
bool PlanetManagerImplementation::isInObjectsNoBuildZone(float x, float y, float extraMargin) {
	SortedVector<ManagedReference<QuadTreeEntry* > > closeObjects;

	Vector3 targetPos(x, y, zone->getHeight(x, y));

	zone->getInRangeObjects(x, y, 512, &closeObjects, true);

	for (int i = 0; i < closeObjects.size(); ++i) {
		SceneObject* obj = cast<SceneObject*>(closeObjects.get(i).get());

		SharedObjectTemplate* objectTemplate = obj->getObjectTemplate();

		if (objectTemplate != NULL) {
			float radius = objectTemplate->getNoBuildRadius();

			// Only check objects with an actual NoBuildRadius
			if (radius > 0) {
				// Add margin to check
				radius += extraMargin;

				Vector3 objWorldPos = obj->getWorldPosition();

				if (objWorldPos.squaredDistanceTo(targetPos) < radius * radius) {
					return true;
				}
			}
		}
	}

	return false;
}
void JukeboxImplementation::setRadius(float newRadius) {
	ManagedReference<Jukebox*> jbox = _this.getReferenceUnsafeStaticCast();

	SortedVector<ManagedReference<SceneObject*> >* children = jbox->getChildObjects();

	if (newRadius == radius)
		return;

	if (children == NULL)
		return;

	radius = newRadius;

	for (int i = 0; i < children->size(); ++i) {
		ManagedReference<SceneObject*> child = children->get(i);

		if (child != NULL && child->isActiveArea()) {
			ManagedReference<ActiveArea*> area = cast<ActiveArea*>(child.get());

			Locker clocker(area, jbox);
			area->setRadius(newRadius);
			return;
		}
	}
}
Vector<CreatureObject*> TurretDataComponent::getAvailableTargets(bool aggroOnly) {
	Vector<CreatureObject*> targets;

	ManagedReference<TangibleObject*> turret = cast<TangibleObject*>(getParent());

	if (turret == NULL)
		return targets;

	CloseObjectsVector* vec = (CloseObjectsVector*)turret->getCloseObjects();

	SortedVector<QuadTreeEntry*> closeObjects;

	vec->safeCopyTo(closeObjects);

	int targetTotal = 0;

	for (int i = 0; i < closeObjects.size(); ++i) {
		CreatureObject* creo = cast<CreatureObject*>(closeObjects.get(i));

		if (creo != NULL && checkTarget(creo, turret, aggroOnly)) {
			targets.add(creo);
		}
	}

	return targets;
}
bool PlanetManagerImplementation::isSpawningPermittedAt(float x, float y, float margin) {
	SortedVector<ActiveArea*> activeAreas;

	Vector3 targetPos(x, y, zone->getHeight(x, y));

	if (!zone->isWithinBoundaries(targetPos))
		return false;

	zone->getInRangeActiveAreas(x, y, &activeAreas, true);
	zone->getInRangeActiveAreas(x, y, margin + 64.f, &activeAreas, true);

	for (int i = 0; i < activeAreas.size(); ++i) {
		ActiveArea* area = activeAreas.get(i);

		if (area->isRegion() || area->isMunicipalZone() || area->isNoSpawnArea()) {
			return false;
		}
	}

	if (isInObjectsNoBuildZone(x, y, margin)) {
		return false;
	}

	if (isInWater(x, y)) {
		return false;
	}

	if (isInRangeWithPoi(x, y, 150))
		return false;

	if (terrainManager->getHighestHeightDifference(x - 10, y - 10, x + 10, y + 10) > 15.0)
		return false;

	return true;
}
SceneObject* StructureManager::getInRangeParkingGarage(SceneObject* obj, int range) {
	ManagedReference<Zone*> zone = obj->getZone();

	if (zone == NULL)
		return NULL;

	SortedVector<ManagedReference<QuadTreeEntry*> > closeSceneObjects;
	CloseObjectsVector* closeObjectsVector = (CloseObjectsVector*) obj->getCloseObjects();

	if (closeObjectsVector == NULL) {
		zone->getInRangeObjects(obj->getPositionX(), obj->getPositionY(), 128, &closeSceneObjects, true);
	} else {
		closeObjectsVector->safeCopyTo(closeSceneObjects);
	}

	for (int i = 0; i < closeSceneObjects.size(); ++i) {
		SceneObject* scno = cast<SceneObject*>(closeSceneObjects.get(i).get());

		if (scno == obj)
			continue;

		if (scno->isGarage() && scno->isInRange(obj, range))
			return scno;
	}

	return NULL;
}
void InstallationObjectImplementation::broadcastMessage(BasePacket* message, bool sendSelf) {
	Zone* zone = getZone();

	if (zone == NULL)
		return;

	Locker zoneLocker(zone);

	SortedVector<ManagedReference<QuadTreeEntry*> > closeSceneObjects;
	zone->getInRangeObjects(getPositionX(), getPositionY(), 512, &closeSceneObjects, false);

	for (int i = 0; i < closeSceneObjects.size(); ++i) {
		ManagedReference<SceneObject*> scno = cast<SceneObject*>( closeSceneObjects.get(i).get());

		if (!sendSelf && scno == _this.get())
			continue;

		if(!scno->isPlayerCreature())
			continue;

		CreatureObject* creo = cast<CreatureObject*>( scno.get());

		if(isOnAdminList(creo))
			scno->sendMessage(message->clone());
	}

	delete message;
}
bool PlanetManagerImplementation::isBuildingPermittedAt(float x, float y, SceneObject* object, float margin) {
	SortedVector<ActiveArea*> activeAreas;

	Vector3 targetPos(x, y, 0);

	if (!zone->isWithinBoundaries(targetPos))
		return false;

	//targetPos.setZ(zone->getHeight(x, y)); not needed

	zone->getInRangeActiveAreas(x, y, &activeAreas, true);

	for (int i = 0; i < activeAreas.size(); ++i) {
		ActiveArea* area = activeAreas.get(i);

		if (area->isNoBuildArea()) {
			return false;
		}
	}

	if (isInObjectsNoBuildZone(x, y, margin)) {
		return false;
	}

	if (isInWater(x, y)) {
		return false;
	}

	if (isInRangeWithPoi(x, y, 150))
		return false;

	return true;
}
bool PlanetManagerImplementation::isCampingPermittedAt(float x, float y, float margin) {
	SortedVector<ManagedReference<ActiveArea* > > activeAreas;

	Vector3 targetPos(x, y, zone->getHeight(x, y));

	zone->getInRangeActiveAreas(x, y, &activeAreas, true);

	for (int i = 0; i < activeAreas.size(); ++i) {
		ActiveArea* area = activeAreas.get(i);

		// Skip areas explicitly marked as camping allowed
		if (area->isCampingPermitted()) {
			continue;
		}

		// Honor no-build after checking for areas that camping is explicitly allowed
		if (area->isNoBuildArea()) {
				return false;
		}
	}

	if (isInWater(x, y)) {
		return false;
	}

	if (isInRangeWithPoi(x, y, 150))
		return false;

	return true;
}
bool PlanetManagerImplementation::isBuildingPermittedAt(float x, float y, SceneObject* object, float margin) {
	SortedVector<ManagedReference<ActiveArea* > > activeAreas;

	Vector3 targetPos(x, y, zone->getHeight(x, y));

	zone->getInRangeActiveAreas(x, y, &activeAreas, true);

	for (int i = 0; i < activeAreas.size(); ++i) {
		ActiveArea* area = activeAreas.get(i);

		if (area->isNoBuildArea()) {
			return false;
		}
	}

	if (isInObjectsNoBuildZone(x, y, margin)) {
		return false;
	}

	if (isInWater(targetPos.getX(), targetPos.getY())) {
		return false;
	}

	if (isInRangeWithPoi(targetPos.getX(), targetPos.getY(), 150))
		return false;

	return true;
}
void CampTerminalMenuComponent::fillObjectMenuResponse(SceneObject* sceneObject,
		ObjectMenuResponse* menuResponse, CreatureObject* player) const {

	if (!sceneObject->isTerminal())
		return;

	Terminal* terminal = cast<Terminal*>(sceneObject);
	if(terminal == NULL) {
		error("Terminal is null in fillObjectMenuResponse");
		return;
	}

	StructureObject* camp = cast<StructureObject*>(terminal->getControlledObject());
	if(camp == NULL) {
		error("Camp is null in fillObjectMenuResponse");
		return;
	}

	TangibleObjectMenuComponent::fillObjectMenuResponse(sceneObject,
			menuResponse, player);

	/// Get Ghost
	Reference<PlayerObject*> ghost = player->getSlottedObject("ghost").castTo<PlayerObject*>();
	if (ghost == NULL) {
		error("PlayerCreature has no ghost: " + String::valueOf(player->getObjectID()));
		return;
	}

	if (!player->isInRange(terminal, 16)) {
		return;
	}

	menuResponse->addRadialMenuItem(68, 3, "@camp:mnu_status");

	/// Make sure player doesn't already have a camp setup somewhere else
	for (int i = 0; i < ghost->getTotalOwnedStructureCount(); ++i) {
		uint64 oid = ghost->getOwnedStructure(i);

		ManagedReference<StructureObject*> structure = ghost->getZoneServer()->getObject(oid).castTo<StructureObject*>();

		if (structure == camp) {
			menuResponse->addRadialMenuItem(182, 3, "@camp:mnu_disband");
			return;
		}
	}

	SortedVector<ManagedReference<ActiveArea*> >* areas = camp->getActiveAreas();
	ManagedReference<ActiveArea*> area = NULL;
	for (int i = 0; i < areas->size(); ++i) {
		area = areas->get(i);
		if (area->isCampArea()) {
			break;
		}
		area == NULL;
	}

	if (area != NULL && area->isCampArea() && (area.castTo<CampSiteActiveArea*>())->isAbandoned()) {
		menuResponse->addRadialMenuItem(183, 3, "@camp:mnu_assume_ownership");
	}
}
void LairMenuComponent::fillObjectMenuResponse(SceneObject* sceneObject, ObjectMenuResponse* menuResponse, CreatureObject* player) const {

	if (!sceneObject->isTangibleObject())
		return;

	TangibleObject* tano = cast<TangibleObject*>(sceneObject);
	if(tano == NULL)
		return;

	ManagedReference<LairObserver*> lairObserver = NULL;
	SortedVector<ManagedReference<Observer*> > observers = tano->getObservers(ObserverEventType::OBJECTDESTRUCTION);

	for (int i = 0; i < observers.size(); i++) {
		lairObserver = cast<LairObserver*>(observers.get(i).get());

		if (lairObserver != NULL)
			break;
	}

	if(player->hasSkill("outdoors_scout_novice") && player->getDistanceTo(sceneObject) < 8 && lairObserver && lairObserver->getMobType() != LairTemplate::NPC) {
		menuResponse->addRadialMenuItem(50, 3, "@lair_n:search_lair"); //Search Lair
	}

	TangibleObjectMenuComponent::fillObjectMenuResponse(sceneObject, menuResponse, player);

}
bool CollisionManager::checkMovementCollision(CreatureObject* creature, float x, float z, float y, Zone* zone) {
	SortedVector<ManagedReference<QuadTreeEntry*> > closeObjects;
	zone->getInRangeObjects(x, y, 128, &closeObjects, true);

	//Vector3 rayStart(x, z + 0.25, y);
	//Vector3 rayStart(creature->getWorldPositionX(), creature->getWorldPositionZ(), creature->getPos)
	Vector3 rayStart = creature->getWorldPosition();
	rayStart.set(rayStart.getX(), rayStart.getY(), rayStart.getZ() + 0.25f);
	//Vector3 rayEnd(x + System::random(512), z + 0.3f, y + System::random(512));

	/*Vector3 rayEnd;
	rayEnd.set(targetPosition.getX(), targetPosition.getY(), targetPosition.getZ());*/

	Vector3 rayEnd(x, z + 0.25, y);

	float maxDistance = rayEnd.distanceTo(rayStart);

	if (maxDistance == 0)
		return false;

	printf("%f\n", maxDistance);

	SortedVector<IntersectionResult> results;
	results.setAllowDuplicateInsertPlan();

	printf("starting test\n");

	Triangle* triangle;

	for (int i = 0; i < closeObjects.size(); ++i) {
		SceneObject* object = dynamic_cast<SceneObject*>(closeObjects.get(i).get());

		if (object == NULL)
			continue;

		AABBTree* tree = getAABBTree(object, 255);

		if (tree == NULL)
			continue;

		Ray ray = convertToModelSpace(rayStart, rayEnd, object);

		//results.removeAll(10, 10);

		//ordered by intersection distance
		//tree->intersects(ray, maxDistance, results);

		float intersectionDistance;

		if (tree->intersects(ray, maxDistance, intersectionDistance, triangle, true)) {
			String str = object->getObjectTemplate()->getFullTemplateString();

			object->info("intersecting with me " + str, true);
			return true;
		}
	}

	return false;
}
void CampTerminalMenuComponent::disbandCamp(SceneObject* sceneObject,
		CreatureObject* player) const {

	Terminal* terminal = cast<Terminal*>(sceneObject);
	if(terminal == NULL) {
		error("Terminal is null in disbandCamp");
		return;
	}

	if (!player->isInRange(terminal, 16)) {
		return;
	}

	StructureObject* camp = cast<StructureObject*>(terminal->getControlledObject());
	if(camp == NULL) {
		error("Camp is null in disbandCamp");
		return;
	}

	if (camp->getZone() == NULL)
		return;

	PlayerObject* ghost = player->getPlayerObject();

	if (ghost == NULL) {
		return;
	}

	if (!ghost->isOwnedStructure(camp)) {
		return;
	}

	if(player->isSitting()) {
		player->setPosture(CreaturePosture::UPRIGHT, true);
	}

	// Find Camp Area
	SortedVector<ManagedReference<ActiveArea* > >* areas = camp->getActiveAreas();
	ManagedReference<ActiveArea*> area = NULL;
	for(int i = 0; i < areas->size(); ++i) {
		area = areas->get(i);
		if(area->isCampArea()) {
			break;
		}
		area == NULL;
	}

	CampSiteActiveArea* campArea = cast<CampSiteActiveArea*>(area.get());
	if(campArea != NULL && campArea->despawnCamp())
		return;

	StructureManager::instance()->destroyStructure(camp);

	if(campArea != NULL) {
		campArea->destroyObjectFromWorld(true);
		campArea->destroyObjectFromDatabase(true);
	}
}
void ShuttleInstallationZoneComponent::destroyObjectFromWorld(SceneObject* sceneObject, bool sendSelfDestroy) {
	if (sceneObject->isStructureObject()) {

		StructureObject* structureObject = cast<StructureObject*>(sceneObject);
		ManagedReference<Zone*> zone = sceneObject->getZone();

		if (zone != NULL && structureObject->isInstallationObject()) {

			ManagedReference<InstallationObject*> installationObject = cast<InstallationObject*>(structureObject);

			if (installationObject->isShuttleInstallation()) {

				ManagedReference<CityRegion*> cityRegion = structureObject->getCityRegion();
				ManagedReference<PlanetManager*> planetManager = zone->getPlanetManager();

				if (cityRegion != NULL) {

					planetManager->removePlayerCityTravelPoint(cityRegion->getRegionName());

					Locker clocker(cityRegion, sceneObject);

					cityRegion->removeShuttleInstallation();
				}

				SortedVector < ManagedReference<SceneObject*> > *childObjects = structureObject->getChildObjects();

				ManagedReference<CreatureObject*> shuttle = NULL;

				for (int i = 0; i < childObjects->size(); ++i) {

					if (!childObjects->get(i)->isTerminal()) {
						shuttle = cast<CreatureObject*>(childObjects->get(i).get());
						break;
					}
				}

				if (shuttle != NULL)
					planetManager->removeShuttle(shuttle);

			}
		}
	}

	ZoneComponent::destroyObjectFromWorld(sceneObject, sendSelfDestroy);
}
bool DroidObjectImplementation::sendConversationStartTo(SceneObject* player) {
	if (!player->isPlayerCreature() || isDead())
		return false;

	if (player != getLinkedCreature().get())
		return false;

	BaseDroidModuleComponent* m = getModule("personality_chip");
	if (m == NULL) {
		return false;
	}

	DroidPersonalityModuleDataComponent* personality = dynamic_cast<DroidPersonalityModuleDataComponent*>(m);
	if (personality == NULL) {
		return false;
	}

	if (personality->getPersonalityConversationTemplate() == 0) {
		return false;
	}

	//Face player.
	faceObject(player);

	PatrolPoint current(coordinates.getPosition(), getParent().get());

	broadcastNextPositionUpdate(&current);

	CreatureObject* playerCreature = cast<CreatureObject*>( player);
	StartNpcConversation* conv = new StartNpcConversation(playerCreature, getObjectID(), "");
	player->sendMessage(conv);

	SortedVector<ManagedReference<Observer*> > observers = getObservers(ObserverEventType::STARTCONVERSATION);

	for (int i = 0;  i < observers.size(); ++i) {
		if (dynamic_cast<ConversationObserver*>(observers.get(i).get()) != NULL) {
			return true;
		}
	}
	//Create conversation observer.
	ConversationObserver* conversationObserver = ConversationManager::instance()->getConversationObserver(personality->getPersonalityConversationTemplate());

	if (conversationObserver != NULL) {
		//Register observers.
		registerObserver(ObserverEventType::CONVERSE, conversationObserver);
		registerObserver(ObserverEventType::STARTCONVERSATION, conversationObserver);
		registerObserver(ObserverEventType::SELECTCONVERSATION, conversationObserver);
		registerObserver(ObserverEventType::STOPCONVERSATION, conversationObserver);
	} else {
		error("Could not create conversation observer.");
		return false;
	}

	return true;
}
bool ZoneContainerComponent::insertActiveArea(Zone* newZone, ActiveArea* activeArea) {
	if (newZone == NULL)
		return false;

	if (!activeArea->isDeplyoed())
		activeArea->deploy();

	Zone* zone = activeArea->getZone();

	ManagedReference<SceneObject*> thisLocker = activeArea;

	Locker zoneLocker(newZone);

	if (activeArea->isInQuadTree() && newZone != zone) {
		activeArea->error("trying to insert to zone an object that is already in a different quadtree");

		activeArea->destroyObjectFromWorld(true);

		//StackTrace::printStackTrace();
	}

	activeArea->setZone(newZone);

	QuadTree* regionTree = newZone->getRegionTree();

	regionTree->insert(activeArea);

	//regionTree->inRange(activeArea, 512);

	// lets update area to the in range players
	SortedVector<QuadTreeEntry*> objects;
	float range = activeArea->getRadius() + 64;

	newZone->getInRangeObjects(activeArea->getPositionX(), activeArea->getPositionY(), range, &objects, false);

	for (int i = 0; i < objects.size(); ++i) {
		SceneObject* object = cast<SceneObject*>(objects.get(i));

		if (!object->isTangibleObject()) {
			continue;
		}

		TangibleObject* tano = cast<TangibleObject*>(object);
		Vector3 worldPos = object->getWorldPosition();

		if (!tano->hasActiveArea(activeArea) && activeArea->containsPoint(worldPos.getX(), worldPos.getY())) {
			tano->addActiveArea(activeArea);
			activeArea->enqueueEnterEvent(object);
		}
	}

	newZone->addSceneObject(activeArea);

	return true;
}
void SceneObjectImplementation::broadcastDestroyPrivate(SceneObject* object, SceneObject* selfObject) {
	ZoneServer* zoneServer = getZoneServer();

	if (zoneServer != NULL && zoneServer->isServerLoading())
		return;

	if (parent.get() != NULL) {
		ManagedReference<SceneObject*> grandParent = getRootParent().get();

		if (grandParent != NULL) {
			grandParent->broadcastDestroyPrivate(object, selfObject);

			return;
		} else {
			return;
		}
	}

	if (zone == NULL)
		return;

	SortedVector<ManagedReference<QuadTreeEntry*> > closeSceneObjects;
	int maxInRangeObjectCount = 0;

	if (closeobjects == NULL) {
		info("Null closeobjects vector in SceneObjectImplementation::broadcastDestroyPrivate", true);
		zone->getInRangeObjects(getPositionX(), getPositionY(), 256, &closeSceneObjects, true);

		maxInRangeObjectCount = closeSceneObjects.size();
	} else {

		CloseObjectsVector* vec = (CloseObjectsVector*) closeobjects;
		closeSceneObjects.removeAll(vec->size(), 10);

		vec->safeCopyTo(closeSceneObjects);

		maxInRangeObjectCount = closeSceneObjects.size();

	}

	for (int i = 0; i < maxInRangeObjectCount; ++i) {
		SceneObject* scno = static_cast<SceneObject*>(closeSceneObjects.get(i).get());

		ManagedReference<ZoneClientSession*> client = scno->getClient();

		if (scno->isVehicleObject() || client != NULL || scno->isMount()) {
			object->sendDestroyTo(scno);
		}
	}
}
void StructurePermissionList::migrateLists(ZoneServer* zoneServer, uint64 ownerObjectID) {
	Locker locker(&lock);

	ManagedReference<GuildManager*> guildManager = zoneServer->getGuildManager();
	ManagedReference<PlayerManager*> playerManager = zoneServer->getPlayerManager();

	for (int i = 0; i < permissionLists.size(); i++) {
		String listName = permissionLists.elementAt(i).getKey();
		addList(listName);

		SortedVector<String>* list = &permissionLists.get(i);

		for (int j = 0; j < list->size(); j++) {
			String name = list->get(j);

			if (name.beginsWith("guild:")) {
				String abbrev = name.replaceAll("guild:","");

				if (abbrev == "" || !guildManager->guildAbbrevExists(abbrev)) {
					continue;
				}

				ManagedReference<GuildObject*> guild = guildManager->getGuildFromAbbrev(abbrev);

				if (guild == NULL) {
					continue;
				}

				grantPermission(listName, guild->getObjectID());

			} else {
				if (!playerManager->existsName(name)) {
					continue;
				}

				ManagedReference<CreatureObject*> player = playerManager->getPlayer(name);

				if (player == NULL || !player->isPlayerCreature()) {
					continue;
				}

				grantPermission(listName, player->getObjectID());
			}
		}
	}

	ownerID = ownerObjectID;

	permissionLists.removeAll();
}
void ScavengerDroidImplementation::announceToPlayers(const String& message) {
	SortedVector<ManagedReference<QuadTreeEntry*> > closeObjects;
	zone->getInRangeObjects(getPositionX(), getPositionY(), 256, &closeObjects,	true);

	for (int i = 0; i < closeObjects.size(); i++) {
		SceneObject* targetObject = cast<SceneObject*>(closeObjects.get(i).get());
		if (targetObject != NULL && targetObject->isPlayerCreature()) {
			ManagedReference<CreatureObject*> player = cast<CreatureObject*>(targetObject);

			if (player != NULL)
				player->sendSystemMessage("@event_perk:" + message);
		}
	}
}
Exemple #27
0
void ZoneComponent::insertChildObjectsToZone(SceneObject* sceneObject, Zone* zone) {
	SortedVector<ManagedReference<SceneObject*> >* childObjects = sceneObject->getChildObjects();

	//Insert all outdoor child objects to zone
	for (int i = 0; i < childObjects->size(); ++i) {
		ManagedReference<SceneObject*> outdoorChild = childObjects->get(i);

		if (outdoorChild == NULL)
			continue;

		if (outdoorChild->getContainmentType() != 4 && outdoorChild->getParent() == NULL)
			zone->transferObject(outdoorChild, -1, true);
	}
}
void StructurePermissionList::sendTo(CreatureObject* creature, const String& listName) {
	ZoneServer* zoneServer = creature->getZoneServer();

	ReadLocker locker(&lock);

	if (!idPermissionLists.contains(listName)) {
		return;
	}

	PermissionListCreateMessage* listMsg = new PermissionListCreateMessage(listName);

	SortedVector<uint64>* list = &idPermissionLists.get(listName);
	Vector<uint64> invalidIDs;

	for (int i = 0; i < list->size(); ++i) {
		Reference<SceneObject*> object = zoneServer->getObject(list->get(i));

		if (object != NULL && object->isPlayerCreature()) {
			CreatureObject* player = object.castTo<CreatureObject*>();
			String name = player->getFirstName();
			listMsg->addName(name);
		} else if (object != NULL && object->isGuildObject()) {
			GuildObject* guild = object.castTo<GuildObject*>();
			String name = "guild:" + guild->getGuildAbbrev();
			listMsg->addName(name);
		} else {
			invalidIDs.add(list->get(i));
		}
	}

	for (int i = 0; i < invalidIDs.size(); i++) {
		list->drop(invalidIDs.get(i));
	}

	listMsg->generateMessage();
	creature->sendMessage(listMsg);
}
void PlayerCreationManager::addStartingItems(CreatureObject* creature,
		const String& clientTemplate, bool equipmentOnly) {
	SortedVector < String > *items = NULL;

	if (!defaultCharacterEquipment.contains(clientTemplate))
		items = &defaultCharacterEquipment.get(0);
	else
		items = &defaultCharacterEquipment.get(clientTemplate);

	for (int i = 0; i < items->size(); ++i) {
		String itemTemplate = items->get(i);

		//instance()->info("Add Starting Items: " + itemTemplate, true);

		ManagedReference<SceneObject*> item = zoneServer->createObject(
				itemTemplate.hashCode(), 1);

		if (item != NULL) {
			String error;
			if (creature->canAddObject(item, 4, error) == 0) {
				creature->transferObject(item, 4, false);
			} else {
				item->destroyObjectFromDatabase(true);
			}
		}

	}

	// Get inventory.
	if (!equipmentOnly) {
		SceneObject* inventory = creature->getSlottedObject("inventory");
		if (inventory == NULL) {
			return;
		}

		//Add common starting items.
		for (int itemNumber = 0; itemNumber < commonStartingItems.size();
				itemNumber++) {
			ManagedReference<SceneObject*> item = zoneServer->createObject(
					commonStartingItems.get(itemNumber).hashCode(), 1);
			if (item != NULL) {
				if (!inventory->transferObject(item, -1, false)) {
					item->destroyObjectFromDatabase(true);
				}
			}
		}
	}
}
void CampTerminalMenuComponent::assumeCampOwnership(SceneObject* sceneObject,
		CreatureObject* player) const {

	Terminal* terminal = cast<Terminal*>(sceneObject);
	if(terminal == NULL) {
		error("Terminal is null in assumeCampOwnership");
		return;
	}

	if (!player->isInRange(terminal, 16)) {
		return;
	}

	StructureObject* camp = cast<StructureObject*>(terminal->getControlledObject());
	if(camp == NULL) {
		error("Camp is null in assumeCampOwnership");
		return;
	}

	// Find Camp Area
	SortedVector<ManagedReference<ActiveArea* > >* areas = camp->getActiveAreas();
	ManagedReference<ActiveArea*> area = NULL;
	for(int i = 0; i < areas->size(); ++i) {
		area = areas->get(i);

		if(area->isCampArea()) {
			break;
		}

		area = NULL;
	}

	ManagedReference<CampSiteActiveArea*> campArea = cast<CampSiteActiveArea*>(area.get());

	if (campArea != NULL) {
		if(!campArea->isAbandoned())
			return;

		ManagedReference<CreatureObject*> play = player;

		Core::getTaskManager()->executeTask([=] () {
			Locker locker(campArea);
			campArea->assumeOwnership(play);
		}, "AssumeOwnershipLambda");
	}
}