#include "Quest.h" #include "Game.h" QuestRequirementStorage::QuestRequirementStorage(Framework::Text requirementId) : ReferenceCounter(), requirementId(requirementId), fulfilled(0), data(new Framework::JSON::JSONObject()) {} QuestRequirementStorage::~QuestRequirementStorage() { data->release(); } void QuestRequirementStorage::putValue( Framework::Text key, Framework::JSON::JSONValue* value) { data->removeValue(key); data->addValue(key, value); } Framework::JSON::JSONValue* QuestRequirementStorage::getValue( Framework::Text key) const { return data->getValue(key); } Framework::JSON::JSONValue* QuestRequirementStorage::zValue( Framework::Text key) const { return data->zValue(key); } bool QuestRequirementStorage::containsKey(Framework::Text key) const { return data->hasValue(key); } void QuestRequirementStorage::setFullfilled(bool fullfilled) { this->fulfilled = fullfilled; } bool QuestRequirementStorage::isFullfilled() const { return fulfilled; } const Framework::Text& QuestRequirementStorage::getRequirementId() const { return requirementId; } QuestRequirementStorageType::QuestRequirementStorageType() : TypeFactory() {} QuestRequirementStorage* QuestRequirementStorageType::fromJson( Framework::JSON::JSONValue* zJson) const { QuestRequirementStorage* result = new QuestRequirementStorage( zJson->asObject()->zValue("requirementId")->asString()->getString()); result->setFullfilled( zJson->asObject()->zValue("fulfilled")->asBool()->getBool()); result->data->release(); result->data = zJson->asObject()->getValue("data")->asObject(); return result; } Framework::JSON::JSONValue* QuestRequirementStorageType::toJson( QuestRequirementStorage* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); result->addValue("requirementId", new Framework::JSON::JSONString(zObject->getRequirementId())); result->addValue( "fulfilled", new Framework::JSON::JSONBool(zObject->isFullfilled())); Framework::Text dataString = zObject->data->toString(); result->addValue("data", Framework::JSON::Parser::getValue(dataString)); return result; } Framework::JSON::Validator::JSONValidator* QuestRequirementStorageType::getValidator() const { return Framework::JSON::Validator::JSONValidator::buildForObject() ->withRequiredString("requirementId") ->finishString() ->withRequiredBool("fulfilled") ->withDefault(false) ->finishBool() ->withRequiredObject("data") ->finishObject() ->finishObject(); } QuestStorage::QuestStorage(Framework::Text questId) : questId(questId), finished(0), rewarded(0) {} void QuestStorage::setQuestFinished(bool finished) { this->finished = finished; } void QuestStorage::setQuestRewarded(bool rewarded) { this->rewarded = rewarded; } bool QuestStorage::isQuestFinished() const { return finished; } bool QuestStorage::isQuestRewarded() const { return rewarded; } QuestRequirementStorage* QuestStorage::zStorage(Framework::Text requirementId) { for (QuestRequirementStorage* storage : requirements) { if (storage->getRequirementId().istGleich(requirementId)) { return storage; } } QuestRequirementStorage* result = new QuestRequirementStorage(requirementId); requirements.add(result); return result; } const Framework::Text& QuestStorage::getQuestId() const { return questId; } bool QuestStorage::isVisible() const { return visible; } void QuestStorage::setVisible(bool visible) { this->visible = visible; } QuestStorageType::QuestStorageType() : TypeFactory() {} QuestStorage* QuestStorageType::fromJson( Framework::JSON::JSONValue* zJson) const { QuestStorage* result = new QuestStorage( zJson->asObject()->zValue("questId")->asString()->getString()); result->finished = zJson->asObject()->zValue("finished")->asBool()->getBool(); result->rewarded = zJson->asObject()->zValue("rewarded")->asBool()->getBool(); Framework::JSON::JSONArray* rewardsArray = zJson->asObject()->zValue("requirements")->asArray(); for (int i = 0; i < rewardsArray->getLength(); i++) { result->requirements.add( Game::INSTANCE->zTypeRegistry()->fromJson( rewardsArray->zValue(i)->asObject())); } return result; } Framework::JSON::JSONValue* QuestStorageType::toJson( QuestStorage* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); result->addValue( "questId", new Framework::JSON::JSONString(zObject->questId)); result->addValue( "finished", new Framework::JSON::JSONBool(zObject->finished)); result->addValue( "rewarded", new Framework::JSON::JSONBool(zObject->rewarded)); Framework::JSON::JSONArray* rewardsArray = new Framework::JSON::JSONArray(); for (QuestRequirementStorage* storage : zObject->requirements) { rewardsArray->addValue( Game::INSTANCE->zTypeRegistry()->toJson(storage)); } result->addValue("requirements", rewardsArray); return result; } Framework::JSON::Validator::JSONValidator* QuestStorageType::getValidator() const { return Framework::JSON::Validator::JSONValidator::buildForObject() ->withRequiredString("questId") ->finishString() ->withRequiredBool("finished") ->withDefault(false) ->finishBool() ->withRequiredBool("rewarded") ->withDefault(false) ->finishBool() ->withRequiredArray("requirements") ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry() ->getValidator()) ->finishArray() ->finishObject(); } QuestParty::QuestParty() : ReferenceCounter() {} void QuestParty::addMember(int memberEntityId) { memberEntityIds.add(memberEntityId); } void QuestParty::removeMember(int memberEntityId) { memberEntityIds.removeValue(memberEntityId); } bool QuestParty::isMember(int memberEntityId) const { return memberEntityIds.hat(memberEntityId); } bool QuestParty::isEmpty() const { return memberEntityIds.getEintragAnzahl() == 0; } QuestStorage* QuestParty::zQuestStorage(Framework::Text questId) { for (QuestStorage* storage : questStorage) { if (storage->getQuestId().istGleich(questId)) { return storage; } } QuestStorage* result = new QuestStorage(questId); questStorage.add(result); return result; } QuestParty* QuestParty::clone() const { QuestParty* result = new QuestParty(); for (QuestStorage* storage : questStorage) { Framework::JSON::JSONValue* value = Game::INSTANCE->zTypeRegistry()->toJson(storage); result->questStorage.add( Game::INSTANCE->zTypeRegistry()->fromJson(value)); value->release(); } return result; } QuestPartyType::QuestPartyType() : TypeFactory() {} QuestParty* QuestPartyType::fromJson(Framework::JSON::JSONValue* zJson) const { QuestParty* result = new QuestParty(); Framework::JSON::JSONArray* membersArray = zJson->asObject()->zValue("members")->asArray(); for (int i = 0; i < membersArray->getLength(); i++) { result->memberEntityIds.add( (int)membersArray->zValue(i)->asNumber()->getNumber()); } Framework::JSON::JSONArray* storagesArray = zJson->asObject()->zValue("quests")->asArray(); for (int i = 0; i < storagesArray->getLength(); i++) { result->questStorage.add( Game::INSTANCE->zTypeRegistry()->fromJson( storagesArray->zValue(i)->asObject())); } return result; } Framework::JSON::JSONValue* QuestPartyType::toJson(QuestParty* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); Framework::JSON::JSONArray* membersArray = new Framework::JSON::JSONArray(); for (int memberEntityId : zObject->memberEntityIds) { membersArray->addValue(new Framework::JSON::JSONNumber(memberEntityId)); } result->addValue("members", membersArray); Framework::JSON::JSONArray* storagesArray = new Framework::JSON::JSONArray(); for (QuestStorage* storage : zObject->questStorage) { storagesArray->addValue( Game::INSTANCE->zTypeRegistry()->toJson(storage)); } result->addValue("quests", storagesArray); return result; } Framework::JSON::Validator::JSONValidator* QuestPartyType::getValidator() const { return Framework::JSON::Validator::JSONValidator::buildForObject() ->withRequiredArray("members") ->addAcceptedNumberInArray() ->finishNumber() ->removeInvalidEntries() ->finishArray() ->withRequiredArray("quests") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->removeInvalidEntries() ->finishArray() ->finishObject(); } Quest::Quest(Framework::Text questId) : ReferenceCounter(), questId(questId), mainQuest(0) {} void Quest::processEvent(QuestEvent* zEvent, QuestStorage* zStorage) { bool finished = 1; for (QuestRequirement* requirement : requirements) { QuestRequirementStorage* zRequirementStorage = zStorage->zStorage(requirement->getRequirementId()); if (!zRequirementStorage->isFullfilled()) { requirement->processEvent(zEvent, zRequirementStorage); finished &= zRequirementStorage->isFullfilled(); } } if (finished) { zStorage->setQuestFinished(1); } } bool Quest::isVisible(QuestParty* zParty, QuestManager* zManager) { if (zParty->zQuestStorage(questId)->isVisible()) { return 1; } bool visible = 0; for (Framework::Text* requiredQuestId : requiredQuestsIds) { visible |= zParty->zQuestStorage(*requiredQuestId)->isQuestFinished(); } if (visible) { zParty->zQuestStorage(questId)->setVisible(visible); } for (Framework::Text* requiredQuestId : requiredQuestsIds) { zManager->setQuestVisible(true, *requiredQuestId, zParty); } return visible; } bool Quest::isActive(QuestParty* zParty) { bool active = 1; for (Framework::Text* requiredQuestId : requiredQuestsIds) { active &= zParty->zQuestStorage(*requiredQuestId)->isQuestFinished(); } return active; } void Quest::giveReward( int choice, Entity* zTargetEntity, QuestStorage* zStorage) { if (!zStorage->isQuestFinished() || zStorage->isQuestRewarded()) { return; } zStorage->setQuestRewarded(1); rewards.z(choice)->giveReward(zTargetEntity); } const Framework::Text& Quest::getQuestId() const { return questId; } Framework::Text Quest::getQuestViewUIML(QuestStorage* zStorage) { Framework::Text result = "isQuestFinished() << "\" rewarded=\"" << zStorage->isQuestRewarded() << "\">"; for (QuestRequirement* requirement : requirements) { result += requirement->getRequirementUIML( zStorage->zStorage(requirement->getRequirementId())); } for (QuestReward* reward : rewards) { result += reward->getRewardUIML(); } result += ""; return result; } void Quest::setVisible(bool visible, QuestParty* zParty, QuestManager* zManager) { if (zParty->zQuestStorage(questId)->isVisible() == visible) { return; } zParty->zQuestStorage(questId)->setVisible(visible); if (visible) { for (Framework::Text* requiredQuestId : requiredQuestsIds) { zManager->setQuestVisible(visible, *requiredQuestId, zParty); } } } QuestType::QuestType() : TypeFactory() {} Quest* QuestType::fromJson(Framework::JSON::JSONValue* zJson) const { Quest* result = new Quest( zJson->asObject()->zValue("questId")->asString()->getString()); result->questName = zJson->asObject()->zValue("questName")->asString()->getString(); result->description = zJson->asObject()->zValue("description")->asString()->getString(); Framework::JSON::JSONArray* requiredQuestGroups = zJson->asObject()->zValue("requiredQuestIds")->asArray(); for (int i = 0; i < requiredQuestGroups->getLength(); i++) { Framework::JSON::JSONArray* requiredQuestIdsArray = requiredQuestGroups->zValue(i)->asArray(); for (int j = 0; j < requiredQuestIdsArray->getLength(); j++) { result->requiredQuestsIds.add(new Framework::Text( requiredQuestIdsArray->zValue(i)->asString()->getString())); result->requiredQuestsGroups.add(i); } } Framework::JSON::JSONArray* requirementsArray = zJson->asObject()->zValue("requirements")->asArray(); for (int i = 0; i < requirementsArray->getLength(); i++) { result->requirements.add( Game::INSTANCE->zTypeRegistry()->fromJson( requirementsArray->zValue(i))); } Framework::JSON::JSONArray* rewardsArray = zJson->asObject()->zValue("rewards")->asArray(); for (int i = 0; i < rewardsArray->getLength(); i++) { result->rewards.add( Game::INSTANCE->zTypeRegistry()->fromJson( rewardsArray->zValue(i))); } result->imagePath = zJson->asObject()->zValue("imagePath")->asString()->getString(); return result; } Framework::JSON::JSONValue* QuestType::toJson(Quest* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); result->addValue( "questId", new Framework::JSON::JSONString(zObject->questId)); result->addValue( "questName", new Framework::JSON::JSONString(zObject->questName)); result->addValue( "description", new Framework::JSON::JSONString(zObject->description)); Framework::JSON::JSONArray* requiredQuestGroupArray = new Framework::JSON::JSONArray(); Framework::JSON::JSONArray* requiredQuestIdsArray = new Framework::JSON::JSONArray(); int index = 0; int lastGroup = 0; for (Framework::Text* requiredQuestId : zObject->requiredQuestsIds) { int group = zObject->requiredQuestsGroups.get(index); if (lastGroup != group) { if (requiredQuestIdsArray->getLength()) { requiredQuestGroupArray->addValue(requiredQuestIdsArray); requiredQuestIdsArray = new Framework::JSON::JSONArray(); } lastGroup = group; } requiredQuestIdsArray->addValue( new Framework::JSON::JSONString(*requiredQuestId)); index++; } if (requiredQuestIdsArray->getLength()) { requiredQuestGroupArray->addValue(requiredQuestIdsArray); requiredQuestIdsArray = new Framework::JSON::JSONArray(); } result->addValue("requiredQuestIds", requiredQuestGroupArray); Framework::JSON::JSONArray* requirementsArray = new Framework::JSON::JSONArray(); for (QuestRequirement* requirement : zObject->requirements) { requirementsArray->addValue( Game::INSTANCE->zTypeRegistry()->toJson(requirement)); } result->addValue("requirements", requirementsArray); Framework::JSON::JSONArray* rewardsArray = new Framework::JSON::JSONArray(); for (QuestReward* reward : zObject->rewards) { rewardsArray->addValue(Game::INSTANCE->zTypeRegistry()->toJson(reward)); } result->addValue("rewards", rewardsArray); result->addValue( "imagePath", new Framework::JSON::JSONString(zObject->imagePath)); return result; } Framework::JSON::Validator::JSONValidator* QuestType::getValidator() const { return Framework::JSON::Validator::JSONValidator::buildForObject() ->withRequiredString("questId") ->finishString() ->withRequiredString("questName") ->finishString() ->withRequiredString("description") ->finishString() ->withRequiredArray("requiredQuestIds") ->addAcceptedArrayInArray() ->addAcceptedStringInArray() ->finishString() ->finishArray() ->withDefault(new Framework::JSON::JSONArray()) ->finishArray() ->withRequiredArray("requirements") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishArray() ->withRequiredArray("rewards") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishArray() ->withRequiredString("imagePath") ->finishString() ->finishObject(); } QuestCollection::QuestCollection(Framework::Text name) : ReferenceCounter(), name(name) {} void QuestCollection::processEvent( QuestEvent* zEvent, QuestParty* zParty) { for (Quest* quest : quests) { QuestStorage* zStorage = zParty->zQuestStorage(quest->getQuestId()); if (quest->isActive(zParty) && !zStorage->isQuestFinished()) { quest->processEvent(zEvent, zStorage); } } } bool QuestCollection::isVisible(QuestParty* zParty, QuestManager* zManager) { bool visible = 1; for (Quest* quest : quests) { visible &= quest->isVisible(zParty, zManager); } return visible; } void QuestCollection::giveReward(Framework::Text questId, int choice, Entity* zTargetEntity, QuestParty* zParty) { QuestStorage* zStorage = zParty->zQuestStorage(questId); if (zStorage->isQuestRewarded() || !zStorage->isQuestFinished()) { return; } for (Quest* quest : quests) { if (quest->getQuestId() == questId) { quest->giveReward(choice, zTargetEntity, zStorage); return; } } } void QuestCollection::addQuest(Quest* zQuest) { quests.add(zQuest); } const Framework::Text& QuestCollection::getName() const { return name; } Framework::Text QuestCollection::getQuestViewUIML( QuestParty* zParty, Framework::Text questId) { for (Quest* quest : quests) { if (quest->getQuestId().istGleich(questId)) { QuestStorage* zStorage = zParty->zQuestStorage(quest->getQuestId()); return quest->getQuestViewUIML(zStorage); } } return ""; } void QuestCollection::setQuestVisible(bool visible, Framework::Text questId, QuestParty* zParty, QuestManager* zManager) { for (Quest* quest : quests) { if (quest->getQuestId().istGleich(questId)) { return quest->setVisible(visible, zParty, zManager); } } } QuestCollectionType::QuestCollectionType() : TypeFactory() {} QuestCollection* QuestCollectionType::fromJson( Framework::JSON::JSONValue* zJson) const { QuestCollection* result = new QuestCollection( zJson->asObject()->zValue("name")->asString()->getString()); Framework::JSON::JSONArray* questsArray = zJson->asObject()->zValue("quests")->asArray(); for (int i = 0; i < questsArray->getLength(); i++) { result->quests.add(Game::INSTANCE->zTypeRegistry()->fromJson( questsArray->zValue(i)->asObject())); } return result; } Framework::JSON::JSONValue* QuestCollectionType::toJson( QuestCollection* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); result->addValue("name", new Framework::JSON::JSONString(zObject->name)); Framework::JSON::JSONArray* questsArray = new Framework::JSON::JSONArray(); for (Quest* quest : zObject->quests) { questsArray->addValue(Game::INSTANCE->zTypeRegistry()->toJson(quest)); } result->addValue("quests", questsArray); return result; } Framework::JSON::Validator::JSONValidator* QuestCollectionType::getValidator() const { return Framework::JSON::Validator::JSONValidator::buildForObject() ->withRequiredString("name") ->finishString() ->withRequiredArray("quests") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->removeInvalidEntries() ->finishArray() ->finishObject(); } QuestManager::QuestManager() : ReferenceCounter() {} QuestParty* QuestManager::zParty(int entityId) { for (QuestParty* party : parties) { if (party->isMember(entityId)) { return party; } } QuestParty* result = new QuestParty(); result->addMember(entityId); parties.add(result); return result; } QuestCollection* QuestManager::zCollection(Framework::Text collectionName) { for (QuestCollection* collection : questCollections) { if (collection->getName().istGleich(collectionName)) { return collection; } } return 0; } void QuestManager::loadQuests() { Framework::Text worldDir = Game::INSTANCE->getWorldDirectory(); Framework::Text questDir = worldDir + "/quests"; if (!Framework::DateiExistiert(questDir)) { std::cout << "no quest data found in world save, using default data\n"; questDir = "data/quests"; } Framework::JSON::JSONValue* value = Framework::JSON::loadJSONFromFile(questDir + "/quests.json"); if (!value || value->getType() == Framework::JSON::JSONType::NULL_) { std::cout << "No valid quests.json found in " << questDir << "\n"; if (value) { value->release(); } } else { std::cout << "loading quests ...\n"; Framework::JSON::Validator::JSONValidator* validator = Framework::JSON::Validator::JSONValidator::buildForArray() ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry() ->getValidator()) ->removeInvalidEntries() ->finishArray(); Framework::RCArray validationResult; Framework::JSON::JSONArray* valid = validator->getValidParts(value, &validationResult)->asArray(); for (Framework::JSON::Validator::JSONValidationResult* invalidPart : validationResult) { invalidPart->printInvalidInfo(); } value->release(); for (Framework::JSON::JSONValue* validPart : *valid) { questCollections.add( Game::INSTANCE->zTypeRegistry()->fromJson( validPart->asObject())); } valid->release(); validator->release(); } value = Framework::JSON::loadJSONFromFile(questDir + "/parties.json"); if (!value || value->getType() == Framework::JSON::JSONType::NULL_) { if (value) { value->release(); } } else { std::cout << "loading quest parties ...\n"; Framework::JSON::Validator::JSONValidator* validator = Framework::JSON::Validator::JSONValidator::buildForArray() ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry() ->getValidator()) ->removeInvalidEntries() ->finishArray(); Framework::RCArray validationResult; Framework::JSON::JSONArray* valid = validator->getValidParts(value, &validationResult)->asArray(); for (Framework::JSON::Validator::JSONValidationResult* invalidPart : validationResult) { invalidPart->printInvalidInfo(); } value->release(); for (Framework::JSON::JSONValue* validPart : *valid) { parties.add(Game::INSTANCE->zTypeRegistry()->fromJson( validPart->asObject())); } valid->release(); validator->release(); } std::cout << "Loaded " << questCollections.getEintragAnzahl() << " Quest Collections and " << parties.getEintragAnzahl() << " Quest Parties\n"; } void QuestManager::saveQuests() { Framework::Text worldDir = Game::INSTANCE->getWorldDirectory(); Framework::Text questDir = worldDir + "/quests"; if (!Framework::DateiExistiert(questDir)) { Framework::DateiPfadErstellen(questDir + "/"); } Framework::JSON::JSONArray* questCollectionsArray = new Framework::JSON::JSONArray(); for (QuestCollection* questCollection : questCollections) { questCollectionsArray->addValue( Game::INSTANCE->zTypeRegistry()->toJson(questCollection)); } Datei datei(questDir + "/quests.json"); if (!datei.existiert()) datei.erstellen(); datei.open(Framework::Datei::Style::schreiben); Framework::Text jsonStr = questCollectionsArray->toString(); datei.schreibe(jsonStr.getText(), jsonStr.getLength()); datei.close(); questCollectionsArray->release(); Framework::JSON::JSONArray* partiesArray = new Framework::JSON::JSONArray(); for (QuestParty* party : parties) { partiesArray->addValue(Game::INSTANCE->zTypeRegistry()->toJson(party)); } datei.setDatei(questDir + "/parties.json"); if (!datei.existiert()) datei.erstellen(); datei.open(Framework::Datei::Style::schreiben); jsonStr = partiesArray->toString(); datei.schreibe(jsonStr.getText(), jsonStr.getLength()); datei.close(); partiesArray->release(); } void QuestManager::processEvent(QuestEvent* event) { QuestParty* party = zParty(event->zActingEntity()->getId()); if (party) { for (QuestCollection* questCollection : questCollections) { questCollection->processEvent(event, party); } } event->release(); } void QuestManager::giveReward( Framework::Text questId, int choice, Entity* zTargetEntity) { QuestParty* party = zParty(zTargetEntity->getId()); if (party) { for (QuestCollection* questCollection : questCollections) { questCollection->giveReward(questId, choice, zTargetEntity, party); } } } Framework::Text QuestManager::getQuestViewUIML( Entity* zTargetEntity, Framework::Text questId) { Framework::Text result = ""; QuestParty* party = zParty(zTargetEntity->getId()); for (QuestCollection* questCollection : questCollections) { result += questCollection->getQuestViewUIML(party, questId); } return result; } void QuestManager::setQuestVisible( bool visible, Framework::Text questId, QuestParty* zParty) { for (QuestCollection* questCollection : questCollections) { questCollection->setQuestVisible(visible, questId, zParty, this); } }