void Functions::getObjectType(Aurora::NWScript::FunctionContext &ctx) {
	ctx.getReturn() = (int32) kObjectTypeInvalid;

	DragonAge::Object *object = DragonAge::ObjectContainer::toObject(getParamObject(ctx, 0));
	if (!object || ((uint32)object->getType() >= kObjectTypeMAX))
		return;

	ctx.getReturn() = (int32) object->getType();
}
void Functions::getNearestObject(Aurora::NWScript::FunctionContext &ctx) {
	Campaign *campaign = _game->getCampaigns().getCurrentCampaign();
	if (!campaign)
		return;

	DragonAge::Object *target = DragonAge::ObjectContainer::toObject(getParamObject(ctx, 0));
	if (!target)
		return;

	// Bitfield of type(s) to check for
	const uint32 type = ctx.getParams()[1].getInt();
	// We want the nth nearest object
	size_t count = MAX<int32>(ctx.getParams()[2].getInt(), 0);

	if (count == 0)
		return;

	Aurora::NWScript::Variable::Array &result = ctx.getReturn().getArray();

	// TODO: nCheckLiving
	// TODO: nCheckPerceived

	const bool includeSelf = ctx.getParams()[5].getInt() != 0;
	if (includeSelf) {
		result.push_back(boost::make_shared<Aurora::NWScript::Variable>(target));
		count--;
	}

	if (count == 0)
		return;

	Aurora::NWScript::ObjectSearch *search = campaign->findObjects();
	Aurora::NWScript::Object       *object = 0;

	std::list<Object *> objects;
	while ((object = search->next())) {
		// Needs to be a valid object and not the target
		DragonAge::Object *daObject = DragonAge::ObjectContainer::toObject(object);
		if (!daObject || (daObject == target))
			continue;

		// Ignore invalid object types
		const uint32 objectType = (uint32) daObject->getType();
		if (objectType >= kObjectTypeMAX)
			continue;

		if (type & objectType)
			objects.push_back(daObject);
	}

	delete search;

	objects.sort(ObjectDistanceSort(*target));

	for (std::list<Object *>::iterator it = objects.begin(); it != objects.end() && count > 0; ++it, count--)
		result.push_back(boost::make_shared<Aurora::NWScript::Variable>(*it));
}