def setup_preferences(kb, hero_info): hero_uid = uids.hero(hero_info.id) if hero_info.preferences_mob_id is not None: f_mob = fact_mob( mobs_storage.mobs_storage[hero_info.preferences_mob_id]) if f_mob.uid not in kb: kb += f_mob kb += facts.PreferenceMob(object=hero_uid, mob=f_mob.uid) if hero_info.preferences_place_id is not None: f_place = fact_place( places_storage.places[hero_info.preferences_place_id]) if f_place.uid not in kb: kb += f_place kb += facts.PreferenceHometown(object=hero_uid, place=f_place.uid) if hero_info.preferences_friend_id is not None: friend = persons_storage.persons[hero_info.preferences_friend_id] f_place = fact_place(friend.place) f_person = fact_person(friend) if f_place.uid not in kb: kb += f_place if f_person.uid not in kb: kb += f_person kb += facts.PreferenceFriend(object=hero_uid, person=f_person.uid) kb += facts.ExceptBadBranches(object=f_person.uid) if hero_info.preferences_enemy_id: enemy = persons_storage.persons[hero_info.preferences_enemy_id] f_place = fact_place(enemy.place) f_person = fact_person(enemy) if f_place.uid not in kb: kb += f_place if f_person.uid not in kb: kb += f_person kb += facts.PreferenceEnemy(object=hero_uid, person=f_person.uid) kb += facts.ExceptGoodBranches(object=f_person.uid) if hero_info.preferences_equipment_slot: kb += facts.PreferenceEquipmentSlot( object=hero_uid, equipment_slot=hero_info.preferences_equipment_slot.value)
def setUp(self): self.qb = QuestsBase() self.qb += [Simple] self.kb = KnowledgeBase() self.kb += [ facts.Hero(uid='hero'), facts.Place(uid='place_1', terrains=(1, )), facts.Place(uid='place_2', terrains=(0, )), facts.Place(uid='place_3', terrains=(0, )), facts.Place(uid='place_4', terrains=(1, )), facts.Place(uid='place_5', terrains=(2, )), facts.Place(uid='place_6', terrains=(1, )), facts.Place(uid='place_7', terrains=(2, )), facts.Place(uid='place_8', terrains=(2, )), facts.Place(uid='place_9', terrains=(1, )), facts.Place(uid='place_10', terrains=(2, )), facts.Person(uid='person_1', profession=PROFESSION.NONE), facts.Person(uid='person_2', profession=PROFESSION.BLACKSMITH), facts.Person(uid='person_3', profession=PROFESSION.NONE), facts.Person(uid='person_4', profession=PROFESSION.NONE), facts.Person(uid='person_5', profession=PROFESSION.NONE), facts.Person(uid='person_6', profession=PROFESSION.NONE), facts.Person(uid='person_7', profession=PROFESSION.NONE), facts.Person(uid='person_8', profession=PROFESSION.NONE), facts.Person(uid='person_9', profession=PROFESSION.NONE), facts.Person(uid='person_10', profession=PROFESSION.NONE), facts.LocatedIn(object='person_1', place='place_1'), facts.LocatedIn(object='person_2', place='place_2'), facts.LocatedIn(object='person_3', place='place_3'), facts.LocatedIn(object='person_4', place='place_4'), facts.LocatedIn(object='person_5', place='place_5'), facts.LocatedIn(object='person_6', place='place_6'), facts.LocatedIn(object='person_7', place='place_7'), facts.LocatedIn(object='person_8', place='place_8'), facts.LocatedIn(object='person_9', place='place_9'), facts.LocatedIn(object='person_10', place='place_10'), facts.LocatedIn(object='hero', place='place_1'), facts.Mob(uid='mob_1', terrains=(0, )), facts.PreferenceMob(object='hero', mob='mob_1'), facts.PreferenceHometown(object='hero', place='place_2'), facts.PreferenceFriend(object='hero', person='person_4'), facts.PreferenceEnemy(object='hero', person='person_5'), facts.UpgradeEquipmentCost(money=777) ] self.selector = Selector(self.kb, self.qb)
def test_enemy(self): place = self.place_2 person = place.persons[0] self.hero.preferences.set_enemy(person) f_place = logic.fact_place(place) f_person = logic.fact_person(person) logic.setup_preferences(self.knowledge_base, self.get_hero_info()) self.check_facts( places=[f_place], persons=[f_person], enemies=[ facts.PreferenceEnemy(object=self.hero_uid, person=f_person.uid) ], good_branches=[facts.ExceptGoodBranches(object=f_person.uid)])
def test_enemy__second_setup(self): place = self.place_2 person = place.persons[0] self.hero.preferences.set(heroes_relations.PREFERENCE_TYPE.ENEMY, person) f_place = logic.fact_place(place) f_person = logic.fact_person(person) self.knowledge_base += f_place self.knowledge_base += f_person logic.setup_preferences(self.knowledge_base, self.get_hero_info()) self.check_facts( places=[f_place], persons=[f_person], enemies=[ facts.PreferenceEnemy(object=self.hero_uid, person=f_person.uid) ], good_branches=[facts.ExceptGoodBranches(object=f_person.uid)])
def test_preferences_enemy(self): self.kb += facts.PreferenceEnemy(object='hero', person='person_1') self.assertEqual(self.selector.preferences_enemy(), facts.PreferenceEnemy(object='hero', person='person_1'))
def create_quest(): # формируем список заданий для генерации qb = QuestsBase() qb += [ Spying, Hunt, Hometown, SearchSmith, Delivery, Caravan, CollectDebt, HelpFriend, InterfereEnemy, Help ] kb = KnowledgeBase() # описываем мир kb += [ facts.Hero(uid='hero'), # наш герой facts.Place(uid='place_1', terrains=( 1, )), # есть место с идентификатором place_1 и типами ландшафта 1, facts.Place(uid='place_2', terrains=(0, )), facts.Place(uid='place_3', terrains=(0, )), facts.Place(uid='place_4', terrains=(1, )), facts.Place(uid='place_5', terrains=(2, )), facts.Place(uid='place_6', terrains=(1, )), facts.Place(uid='place_7', terrains=(2, )), facts.Place(uid='place_8', terrains=(2, )), facts.Place(uid='place_9', terrains=(1, )), facts.Place(uid='place_10', terrains=(2, )), facts.Person( uid='person_1', profession=PROFESSION.NONE ), # есть персонаж с идентификатором perons_1 и без профессии facts.Person(uid='person_2', profession=PROFESSION.BLACKSMITH), facts.Person(uid='person_3', profession=PROFESSION.ROGUE), facts.Person(uid='person_4', profession=PROFESSION.NONE), facts.Person(uid='person_5', profession=PROFESSION.NONE), facts.Person(uid='person_6', profession=PROFESSION.NONE), facts.Person(uid='person_7', profession=PROFESSION.NONE), facts.Person(uid='person_8', profession=PROFESSION.NONE), facts.Person(uid='person_9', profession=PROFESSION.NONE), facts.Person(uid='person_10', profession=PROFESSION.NONE), facts.LocatedIn( object='person_1', place='place_1'), # персонаж person_1 находится в place_1 facts.LocatedIn(object='person_2', place='place_2'), facts.LocatedIn(object='person_3', place='place_3'), facts.LocatedIn(object='person_4', place='place_4'), facts.LocatedIn(object='person_5', place='place_5'), facts.LocatedIn(object='person_6', place='place_6'), facts.LocatedIn(object='person_7', place='place_7'), facts.LocatedIn(object='person_8', place='place_8'), facts.LocatedIn(object='person_9', place='place_9'), facts.LocatedIn(object='person_10', place='place_10'), facts.LocatedIn(object='hero', place='place_1'), # герой находится в place_1 facts.Mob( uid='mob_1', terrains=(0, ) ), # есть монстр, обитающий на территориях с идентификатором 0 (для задания на охоту) facts.PreferenceMob( object='hero', mob='mob_1'), # герой любит охотиться на монстра mob_1 facts.PreferenceHometown( object='hero', place='place_2'), # герой считате родным место place_2 facts.PreferenceFriend(object='hero', person='person_4'), # герой дружит с person_4 facts.PreferenceEnemy(object='hero', person='person_5'), # герой враждует с person_5 # указываем, что обновление экипировки стоит 777 монет (для задания SearchSmith) # facts.HasMoney(object='hero', money=888), # если этот факт раскоментировать то в этом задании герой купит экипировку, а не пойдёт делать задание кузнеца facts.UpgradeEquipmentCost(money=777), facts.OnlyGoodBranches(object='place_2'), # не вредить месту place_2 facts.OnlyGoodBranches( object='person_4'), # не вредить персонажу person_4 facts.OnlyBadBranches(object='person_5') ] # не помогать персонажу person_5 kb.validate_consistency( WORLD_RESTRICTIONS) # проверяем ограничения на мир, selector = Selector(kb, qb) # создаём квест (получаем список фактов) quests_facts = selector.create_quest_from_place( nesting=0, initiator_position=kb['place_1'], tags=('can_start', )) kb += quests_facts transformators.activate_events( kb ) # активируем события (из нескольких вершин графа оставляем одну, остальные удаляем) transformators.remove_restricted_states( kb ) # удаляем состояния, в которые нельзя переходить (например, которые вредят тому, кому вредить нельщя) transformators.remove_broken_states( kb ) # чистим граф задания от разрушений, вызванных предыдущими действиями transformators.determine_default_choices( kb) # определяем выборы по умолчанию на развилках kb.validate_consistency(WORLD_RESTRICTIONS) # ещё раз проверяем мир kb.validate_consistency( QUEST_RESTRICTIONS ) # проверяем граф задания (вдруг полностью разрушен) return kb
facts.LocatedIn(object='person_1', place='place_1'), facts.LocatedIn(object='person_2', place='place_2'), facts.LocatedIn(object='person_3', place='place_3'), facts.LocatedIn(object='person_4', place='place_4'), facts.LocatedIn(object='person_5', place='place_5'), facts.LocatedIn(object='person_6', place='place_6'), facts.LocatedIn(object='person_7', place='place_7'), facts.LocatedIn(object='person_8', place='place_8'), facts.LocatedIn(object='person_9', place='place_9'), facts.LocatedIn(object='person_10', place='place_10'), facts.LocatedIn(object='hero', place='place_1'), facts.Mob(uid='mob_1', terrains=(0, )), facts.PreferenceMob(object='hero', mob='mob_1'), facts.PreferenceHometown(object='hero', place='place_2'), facts.PreferenceFriend(object='hero', person='person_4'), facts.PreferenceEnemy(object='hero', person='person_5'), facts.UpgradeEquipmentCost(money=777) ] selector = Selector(kb, qb) kb += Quest.construct_from_place( nesting=0, selector=selector, start_place=selector.new_place(candidates=('place_1', ))) try: kb.validate_consistency([ restrictions.SingleStartStateWithNoEnters(), restrictions.FinishStateExists(), restrictions.AllStatesHasJumps(), restrictions.SingleLocationForObject(),