Beispiel #1
0
def get_knowledge_base(hero_info, without_restrictions=False):  # pylint: disable=R0912

    kb = KnowledgeBase()

    hero_uid = uids.hero(hero_info.id)

    kb += facts.Hero(uid=hero_uid, externals={'id': hero_info.id})

    setup_places(kb, hero_info)
    setup_persons(kb, hero_info)
    setup_preferences(kb, hero_info)
    setup_social_connections(kb)

    if not without_restrictions:

        for person in persons_storage.persons.all():
            if person.place.id == hero_info.position_place_id and person.id in hero_info.interfered_persons:
                kb += facts.NotFirstInitiator(person=uids.person(person.id))

    kb.validate_consistency(WORLD_RESTRICTIONS)

    kb += [
        facts.UpgradeEquipmentCost(
            money=QuestPrototype.upgrade_equipment_cost(hero_info))
    ]

    return kb
Beispiel #2
0
    def setUp(self):
        self.kb = KnowledgeBase()

        self.fact = Fact(uid='fact')
        self.fact_2 = Fact(uid='fact_2', description='cba')

        self.kb += [self.fact, self.fact_2]
    def test_linked_choices__linked_option_with_processed_choice(self):
        is_raised = False

        for i in xrange(100):
            kb = KnowledgeBase()
            start = facts.Start(uid='start', type='test', nesting=0)
            choice_1 = facts.Choice(uid='choice_1')
            choice_2 = facts.Choice(uid='choice_2')
            finish_1 = facts.Finish(uid='finish_1',
                                    results={},
                                    nesting=0,
                                    start='start')
            finish_2 = facts.Finish(uid='finish_2',
                                    results={},
                                    nesting=0,
                                    start='start')

            option_1 = facts.Option(state_from=choice_1.uid,
                                    state_to=finish_1.uid,
                                    type='opt_1',
                                    markers=())
            option_2 = facts.Option(state_from=choice_1.uid,
                                    state_to=choice_2.uid,
                                    type='opt_2',
                                    markers=())

            option_2_1 = facts.Option(state_from=choice_2.uid,
                                      state_to=finish_1.uid,
                                      type='opt_2_1',
                                      markers=())
            option_2_2 = facts.Option(state_from=choice_2.uid,
                                      state_to=finish_2.uid,
                                      type='opt_2_2',
                                      markers=())

            facts_list = [
                start,
                facts.Jump(state_from=start.uid,
                           state_to=choice_1.uid), choice_1, choice_2,
                finish_1, finish_2, option_1, option_2, option_2_1, option_2_2,
                facts.OptionsLink(options=(option_2.uid, option_2_2.uid))
            ]

            kb += facts_list

            try:
                transformators.determine_default_choices(kb)
            except exceptions.LinkedOptionWithProcessedChoiceError:
                is_raised = True
                self.assertEqual(len(list(kb.filter(facts.ChoicePath))), 1)
                self.assertEqual(
                    [path.choice for path in kb.filter(facts.ChoicePath)],
                    [choice_2.uid])
                self.assertEqual(
                    [path.option for path in kb.filter(facts.ChoicePath)],
                    [option_2_1.uid])
                break

        self.assertFalse(is_raised)
Beispiel #4
0
    def setUp(self):
        self.kb = KnowledgeBase()

        # world
        self.kb += [
            Place(uid='place_from'),
            Person(uid='person_from'),
            Place(uid='place_thought'),
            Place(uid='place_to'),
            Person(uid='person_to'),
            LocatedIn(object='person_from', place='place_from'),
            LocatedIn(object='person_to', place='place_to')
        ]

        # quest
        self.kb += [
            Start(uid='start',
                  nesting=0,
                  type='simple_test',
                  require=(requirements.LocatedIn(object='person_from',
                                                  place='place_from'),
                           requirements.LocatedIn(object='person_to',
                                                  place='place_to'),
                           requirements.LocatedIn(object='hero',
                                                  place='place_from'))),
            State(uid='st_throught_place',
                  require=(requirements.LocatedIn(object='hero',
                                                  place='place_thought'), )),
            Finish(uid='st_finish',
                   start='start',
                   results={},
                   nesting=0,
                   require=(requirements.LocatedIn(object='hero',
                                                   place='place_to'), )),
            Jump(state_from='start', state_to='st_throught_place'),
            Jump(state_from='st_throught_place', state_to='st_finish')
        ]

        self.kb += [Hero(uid='hero')]

        self.kb.validate_consistency([
            restrictions.SingleStartStateWithNoEnters(),
            restrictions.FinishStateExists(),
            restrictions.AllStatesHasJumps(),
            restrictions.SingleLocationForObject(),
            restrictions.ReferencesIntegrity(),
            restrictions.ConnectedStateJumpGraph(),
            restrictions.NoCirclesInStateJumpGraph(),
            restrictions.MultipleJumpsFromNormalState(),
            restrictions.ChoicesConsistency()
        ])

        self.machine = machine.Machine(knowledge_base=self.kb,
                                       interpreter=FakeInterpreter())
Beispiel #5
0
    def setUp(self):
        self.kb = KnowledgeBase()

        self.hero = facts.Hero(uid='hero')

        self.start = facts.Start(uid='start', type='test', nesting=0)
        self.state_1 = facts.State(uid='state_1')
        self.state_2 = facts.State(uid='state_2')
        self.finish_1 = facts.Finish(start='start', uid='finish_1', results={}, nesting=0)

        self.kb += [ self.start, self.state_1, self.state_2, self.finish_1, self.hero]

        self.machine = Machine(knowledge_base=self.kb, interpreter=FakeInterpreter())
Beispiel #6
0
    def setUp(self):
        super(LogicTestsBase, self).setUp()

        self.place_1, self.place_2, self.place_3 = create_test_map()

        account = self.accounts_factory.create_account(is_fast=True)

        self.storage = LogicStorage()
        self.storage.load_account_data(account)
        self.hero = self.storage.accounts_to_heroes[account.id]

        self.hero_uid = uids.hero(self.hero.id)

        self.knowledge_base = KnowledgeBase()
Beispiel #7
0
    def setUp(self):
        super(LogicTestsBase, self).setUp()

        self.place_1, self.place_2, self.place_3 = create_test_map()

        result, account_id, bundle_id = register_user('test_user')

        self.storage = LogicStorage()
        self.storage.load_account_data(AccountPrototype.get_by_id(account_id))
        self.hero = self.storage.accounts_to_heroes[account_id]

        self.hero_uid = uids.hero(self.hero.id)

        self.knowledge_base = KnowledgeBase()
Beispiel #8
0
    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)
Beispiel #9
0
    def setUp(self):
        self.kb = KnowledgeBase()

        self.kb += [
            facts.Hero(uid='hero'),
            facts.Hero(uid='hero_2'),
            facts.Place(uid='place_1', terrains=(1, ), type=11),
            facts.Place(uid='place_2', terrains=(0, ), type=22),
            facts.Place(uid='place_3', terrains=(0, ), type=33),
            facts.Person(uid='person_1', profession=0),
            facts.Person(uid='person_2', profession=1),
            facts.Person(uid='person_3', profession=2),
            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.Mob(uid='mob_1', terrains=(0, ))
        ]

        self.selector = selectors.Selector(self.kb, None)
Beispiel #10
0
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
Beispiel #11
0
from questgen.quests.help import Help
from questgen.quests.pilgrimage import Pilgrimage

QUESTS = [
    Spying, Hunt, Hometown, SearchSmith, Delivery, Caravan, CollectDebt,
    HelpFriend, InterfereEnemy, Help, Simple, Simplest, Complex, Pilgrimage
]

qb = QuestsBase()
qb += [Simple]

for Quest in QUESTS:

    print('process quest: %s' % Quest.TYPE)

    kb = KnowledgeBase()

    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, ),
                    type=relations.PLACE_TYPE.HOLY_CITY),
 def setUp(self):
     self.kb = KnowledgeBase()