if kind is not None and obj.kind != kind:
            return Response(dict(error=_('You cannot turn a regular inventory into a "smart" inventory.')), status=status.HTTP_405_METHOD_NOT_ALLOWED)

        return super(InventoryDetail, self).update(request, *args, **kwargs)

    def destroy(self, request, *args, **kwargs):
        obj = self.get_object()
        if not request.user.can_access(self.model, 'delete', obj):
            raise PermissionDenied()
        self.check_related_active_jobs(obj)  # related jobs mixin
        try:
            obj.schedule_deletion(getattr(request.user, 'id', None))
            return Response(status=status.HTTP_202_ACCEPTED)
        except RuntimeError as e:
            return Response(dict(error=_("{0}".format(e))), status=status.HTTP_400_BAD_REQUEST)

class InventoryActivityStreamList(SubListAPIView):

    model = ActivityStream
    serializer_class = ActivityStreamSerializer
    parent_model = Inventory
    relationship = 'activitystream_set'
    search_fields = ('changes',)

    def get_queryset(self):
        parent = self.get_parent_object()
        self.check_parent_access(parent)
        qs = self.request.user.get_queryset(self.model)
        return qs.filter(Q(inventory=parent) | Q(host__in=parent.hosts.all()) | Q(group__in=parent.groups.all()))

class InventoryInstanceGroupsList(SubListAttachDetachAPIView):

    model = InstanceGroup
    serializer_class = InstanceGroupSerializer
    parent_model = Inventory
    relationship = 'instance_groups'

class InventoryAccessList(ResourceAccessList):

    model = User  # needs to be User for AccessLists's
    parent_model = Inventory

class InventoryObjectRolesList(SubListAPIView):

    model = Role
    serializer_class = RoleSerializer
    parent_model = Inventory
    search_fields = ('role_field', 'content_type__model')

    def get_queryset(self):
        po = self.get_parent_object()
        content_type = ContentType.objects.get_for_model(self.parent_model)
        return Role.objects.filter(content_type=content_type, object_id=po.pk)

class InventoryJobTemplateList(SubListAPIView):

    model = JobTemplate
    serializer_class = JobTemplateSerializer
    parent_model = Inventory
    relationship = 'jobtemplates'

    def get_queryset(self):
        parent = self.get_parent_object()
        self.check_parent_access(parent)
        qs = self.request.user.get_queryset(self.model)
        return qs.filter(inventory=parent)

class InventoryCopy(CopyAPIView):

    model = Inventory
    copy_return_serializer_class = InventorySerializer


try:
            obj.schedule_deletion(getattr(request.user, 'id', None))
            return Response(status=status.HTTP_202_ACCEPTED)
        except RuntimeError as e:
            return Response(dict(error=_("{0}".format(e))), status=status.HTTP_400_BAD_REQUEST)

# All Rights Reserved.

# Python
import logging

# Django
from django.conf import settings
from django.db.models import Q
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _

# Django REST Framework
from rest_framework.exceptions import PermissionDenied
from rest_framework.response import Response
from rest_framework import status

# AWX
from awx.main.models import (
    ActivityStream,
    Inventory,
    JobTemplate,
    Role,
    User,
    InstanceGroup,
    InventoryUpdateEvent,
    InventoryUpdate,
)
from awx.api.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView, SubListAPIView, SubListAttachDetachAPIView, ResourceAccessList, CopyAPIView

from awx.api.serializers import (
    InventorySerializer,
    ActivityStreamSerializer,
    RoleSerializer,
    InstanceGroupSerializer,
    InventoryUpdateEventSerializer,
    JobTemplateSerializer,
)
from awx.api.views.mixin import RelatedJobsPreventDeleteMixin, ControlledByScmMixin

from awx.api.pagination import UnifiedJobEventPagination

logger = logging.getLogger('awx.api.views.organization')

class InventoryUpdateEventsList(SubListAPIView):

    model = InventoryUpdateEvent
    serializer_class = InventoryUpdateEventSerializer
    parent_model = InventoryUpdate
    relationship = 'inventory_update_events'
    name = _('Inventory Update Events List')
    search_fields = ('stdout',)
    pagination_class = UnifiedJobEventPagination

    def get_queryset(self):
        iu = self.get_parent_object()
        self.check_parent_access(iu)
        return iu.get_event_queryset()

    def finalize_response(self, request, response, *args, **kwargs):
        response['X-UI-Max-Events'] = settings.MAX_UI_JOB_EVENTS
        return super(InventoryUpdateEventsList, self).finalize_response(request, response, *args, **kwargs)

class InventoryList(ListCreateAPIView):

    model = Inventory
    serializer_class = InventorySerializer

class InventoryDetail(RelatedJobsPreventDeleteMixin, ControlledByScmMixin, RetrieveUpdateDestroyAPIView):

    model = Inventory
    serializer_class = InventorySerializer

    def update(self, request, *args, **kwargs):
        obj = self.get_object()
        kind = self.request.data.get('kind') or kwargs.get('kind')

        # Do not allow changes to an Inventory kind.
        if kind is not None and obj.kind != kind:
            return Response(dict(error=_('You cannot turn a regular inventory into a "smart" inventory.')), status=status.HTTP_405_METHOD_NOT_ALLOWED)
        return super(InventoryDetail, self).update(request, *args, **kwargs)

    def destroy(self, request, *args, **kwargs):
        obj = self.get_object()
        if not request.user.can_access(self.model, 'delete', obj):
            raise PermissionDenied()
        self.check_related_active_jobs(obj)  # related jobs mixin

class InventoryActivityStreamList(SubListAPIView):

    model = ActivityStream
    serializer_class = ActivityStreamSerializer
    parent_model = Inventory
    relationship = 'activitystream_set'
    search_fields = ('changes',)

    def get_queryset(self):
        parent = self.get_parent_object()
        self.check_parent_access(parent)
        qs = self.request.user.get_queryset(self.model)
        return qs.filter(Q(inventory=parent) | Q(host__in=parent.hosts.all()) | Q(group__in=parent.groups.all()))

class InventoryInstanceGroupsList(SubListAttachDetachAPIView):

    model = InstanceGroup
    serializer_class = InstanceGroupSerializer
    parent_model = Inventory
    relationship = 'instance_groups'

class InventoryAccessList(ResourceAccessList):

    model = User  # needs to be User for AccessLists's
    parent_model = Inventory

class InventoryObjectRolesList(SubListAPIView):

    model = Role
    serializer_class = RoleSerializer
    parent_model = Inventory
    search_fields = ('role_field', 'content_type__model')

    def get_queryset(self):
        po = self.get_parent_object()
        content_type = ContentType.objects.get_for_model(self.parent_model)
        return Role.objects.filter(content_type=content_type, object_id=po.pk)

class InventoryJobTemplateList(SubListAPIView):

    model = JobTemplate
    serializer_class = JobTemplateSerializer
    parent_model = Inventory
    relationship = 'jobtemplates'

    def get_queryset(self):
        parent = self.get_parent_object()
        self.check_parent_access(parent)
        qs = self.request.user.get_queryset(self.model)
        return qs.filter(inventory=parent)

class InventoryCopy(CopyAPIView):

    model = Inventory
    copy_return_serializer_class = InventorySerializer


# All Rights Reserved.

# Python
import logging

# Django
from django.conf import settings
from django.db.models import Q
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _

# Django REST Framework
from rest_framework.exceptions import PermissionDenied
from rest_framework.response import Response
from rest_framework import status

# AWX
from awx.main.models import (
    ActivityStream,
    Inventory,
    JobTemplate,
    Role,
    User,
    InstanceGroup,
    InventoryUpdateEvent,
    InventoryUpdate,
)
from awx.api.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView, SubListAPIView, SubListAttachDetachAPIView, ResourceAccessList, CopyAPIView

from awx.api.serializers import (
    InventorySerializer,
    ActivityStreamSerializer,
    RoleSerializer,
    InstanceGroupSerializer,
    InventoryUpdateEventSerializer,
    JobTemplateSerializer,
)
from awx.api.views.mixin import RelatedJobsPreventDeleteMixin, ControlledByScmMixin

from awx.api.pagination import UnifiedJobEventPagination

logger = logging.getLogger('awx.api.views.organization')

class InventoryUpdateEventsList(SubListAPIView):

    model = InventoryUpdateEvent
    serializer_class = InventoryUpdateEventSerializer
    parent_model = InventoryUpdate
    relationship = 'inventory_update_events'
    name = _('Inventory Update Events List')
    search_fields = ('stdout',)
    pagination_class = UnifiedJobEventPagination

    def get_queryset(self):
        iu = self.get_parent_object()
        self.check_parent_access(iu)
        return iu.get_event_queryset()

    def finalize_response(self, request, response, *args, **kwargs):
        response['X-UI-Max-Events'] = settings.MAX_UI_JOB_EVENTS
        return super(InventoryUpdateEventsList, self).finalize_response(request, response, *args, **kwargs)

class InventoryList(ListCreateAPIView):

    model = Inventory
    serializer_class = InventorySerializer

class InventoryDetail(RelatedJobsPreventDeleteMixin, ControlledBySc

mMixin, RetrieveUpdateDestroyAPIView):

    model = Inventory
    serializer_class = InventorySerializer

    def update(self, request, *args, **kwargs):
        obj = self.get_object()
        kind = self.request.data.get('kind') or kwargs.get('kind')

        # Do not allow changes to an Inventory kind.
        if kind is not None and obj.kind != kind:
            return Response(dict(error=_('You cannot turn a regular inventory into a "smart" inventory.')), status=status.HTTP_405_METHOD_NOT_ALLOWED)
        return super(InventoryDetail, self).update(request, *args, **kwargs)

    def destroy(self, request, *args, **kwargs):
        obj = self.get_object()
        if not request.user.can_access(self.model, 'delete', obj):
            raise PermissionDenied()
        self.check_related_active_jobs(obj)  # related jobs mixin
        try:
            obj.schedule_deletion(getattr(request.user, 'id', None))
            return Response(status=status.HTTP_202_ACCEPTED)
        except RuntimeError as e:
            return Response(dict(error=_("{0}".format(e))), status=status.HTTP_400_BAD_REQUEST)

class InventoryActivityStreamList(SubListAPIView):

    model = ActivityStream
    serializer_class = ActivityStreamSerializer
    parent_model = Inventory
    relationship = 'activitystream_set'
    search_fields = ('changes',)

    def get_queryset(self):
        parent = self.get_parent_object()
        self.check_parent_access(parent)
        qs = self.request.user.get_queryset(self.model)
        return qs.filter(Q(inventory=parent) | Q(host__in=parent.hosts.all()) | Q(group__in=parent.groups.all()))

class InventoryInstanceGroupsList(SubListAttachDetachAPIView):

    model = InstanceGroup
    serializer_class = InstanceGroupSerializer
    parent_model = Inventory
    relationship = 'instance_groups'

class InventoryAccessList(ResourceAccessList):

    model = User  # needs to be User for AccessLists's
    parent_model = Inventory

class InventoryObjectRolesList(SubListAPIView):

    model = Role
    serializer_class = RoleSerializer
    parent_model = Inventory
    search_fields = ('role_field', 'content_type__model')

    def get_queryset(self):
        po = self.get_parent_object()
        content_type = ContentType.objects.get_for_model(self.parent_model)
        return Role.objects.filter(content_type=content_type, object_id=po.pk)

class InventoryJobTemplateList(SubListAPIView):

    model = JobTemplate
    serializer_class = JobTemplateSerializer
    parent_model = Inventory
    relationship = 'jobtemplates'

    def get_queryset(self):
        parent = self.get_parent_object()
        self.check_parent_access(parent)
        qs = self.request.user.get_queryset(self.model)
        return qs.filter(inventory=parent)

class InventoryCopy(CopyAPIView):

    model = Inventory
    copy_return_serializer_class = InventorySerializer


def t510020_1():
    """ State 0,1 """
    assert GetCurrentStateElapsedTime() > 1
    """ State 2 """
    while True:
        call = t510020_x10()
        assert IsClientPlayer() == 1
        """ State 3 """
        call = t510020_x11()
        assert not IsClientPlayer()

def t510020_x0(action2=12000026):
    """ State 0,1 """
    OpenGenericDialog(8, action2, 3, 4, 2)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    if GetGenericDialogButtonResult() == 1:
        """ State 3 """
        return 0
    else:
        """ State 4 """
        return 1

def t510020_x1(z8=6018, flag4=6001, flag5=6000, flag6=6000, flag7=6000, flag8=6000):
    """ State 0,1 """
    while True:
        assert (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
                and not IsPlayerDead() and not IsCharacterDisabled())
        """ State 3 """
        assert (GetEventStatus(flag4) == 1 or GetEventStatus(flag5) == 1 or GetEventStatus(flag6) ==
                1 or GetEventStatus(flag7) == 1 or GetEventStatus(flag8) == 1)
        """ State 2 """
        if (not (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
            and not IsPlayerDead() and not IsCharacterDisabled())):
            pass
        elif (not GetEventStatus(flag4) and not GetEventStatus(flag5) and not GetEventStatus(flag6) and
              not GetEventStatus(flag7) and not GetEventStatus(flag8)):
            pass
        elif CheckActionButtonArea(z8):
            break
    """ State 4 """
    return 0

def t510020_x2():
    """ State 0,1 """
    if not CheckSpecificPersonTalkHasEnded(0):
        """ State 7 """
        ClearTalkProgressData()
        StopEventAnimWithoutForcingConversationEnd(0)
        """ State 6 """
        ReportConversationEndToHavokBehavior()
    else:
        pass
    """ State 2 """
    if CheckSpecificPersonGenericDialogIsOpen(0) == 1:
        """ State 3 """
        ForceCloseGenericDialog()
    else:
        pass
    """ State 4 """
    if CheckSpecificPersonMenuIsOpen(-1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0):
        """ State 5 """
        ForceCloseMenu()
    else:
        pass
    """ State 8 """
    return 0

def t510020_x3(lot2=_):
    """ State 0,1 """
    GetItemFromItemLot(lot2)
    assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 2 """
    return 0

def t510020_x4(z9=0, z10=9000, flag3=6001):
    """ State 0,1 """
    if GetEventStatus(flag3) == 1:
        """ State 2 """
        pass
    else:
        """ State 3,4 """
        AcquireGesture(z9)
        OpenItemAcquisitionMenu(3, z10, 1)
        SetEventState(flag3, 1)
        assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 5 """
    return 0

def t510020_x5(action5=_):
    """ State 0,1 """
    OpenGenericDialog(7, action5, 1, 0, 1)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    return 0

def t510020_x6(goods1=379, z3=12000007):
    """ State 0,2 """
    ClearQuantityValueOfChooseQuantityDialog()
    """ State 1 """
    OpenChooseQuantityDialog(goods1, z3)
    if GetValueFromNumberSelectDialog() >= 0:
        """ State 3,5 """
        return 0
    elif not (CheckSpecificPersonMenuIsOpen(13, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0)):
        """ State 4,6 """
        return 1

def t510020_x7():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x8():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x9(z6=27, lot2=4288, lot3=4287):
    """ State 0,1 """
    if ComparePlayerStatus(z6, 4, 2) == 1:
        """ State 4,6 """
        assert t510020_x3(lot2=lot3)
    elif ComparePlayerStatus(z6, 0, 1) == 1:
        """ State 3,5 """
        assert t510020_x3(lot2=lot2)
    else:
        """ State 2 """
        pass
    """ State 7 """
    return 0

def t510020_x10():
    """ State 0,1 """
    while True:
        call = t510020_x12()
        assert not GetEventStatus(6001)
        """ State 2 """
        call = t510020_x13()
        assert GetEventStatus(6001) == 1

def t510020_x11():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x12():
    """ State 0,1 """
    # goods:379:, action:12000026:Join Covenant?, action:13000046:You have obtained proof of the covenant, action:13000037:, action:13000017:, action:13000007:
    t510020_x14(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, z7=6830, flag1=75100951, flag2=75100952, z8=6018, flag3=6001, z9=0,
                z10=9000, flag4=6001, z11=75100956, z12=75100957)


def t510020_1():
    """ State 0,1 """
    assert GetCurrentStateElapsedTime() > 1
    """ State 2 """
    while True:
        call = t510020_x10()
        assert IsClientPlayer() == 1
        """ State 3 """
        call = t510020_x11()
        assert not IsClientPlayer()

def t510020_x0(action2=12000026):
    """ State 0,1 """
    OpenGenericDialog(8, action2, 3, 4, 2)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    if GetGenericDialogButtonResult() == 1:
        """ State 3 """
        return 0
    else:
        """ State 4 """
        return 1

def t510020_x1(z8=6018, flag4=6001, flag5=6000, flag6=6000, flag7=6000, flag8=6000):
    """ State 0,1 """
    while True:
        assert (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
                and not IsPlayerDead() and not IsCharacterDisabled())
        """ State 3 """
        assert (GetEventStatus(flag4) == 1 or GetEventStatus(flag5) == 1 or GetEventStatus(flag6) ==
                1 or GetEventStatus(flag7) == 1 or GetEventStatus(flag8) == 1)
        """ State 2 """
        if (not (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
            and not IsPlayerDead() and not IsCharacterDisabled())):
            pass
        elif (not GetEventStatus(flag4) and not GetEventStatus(flag5) and not GetEventStatus(flag6) and
              not GetEventStatus(flag7) and not GetEventStatus(flag8)):
            pass
        elif CheckActionButtonArea(z8):
            break
    """ State 4 """
    return 0

def t510020_x2():
    """ State 0,1 """
    if not CheckSpecificPersonTalkHasEnded(0):
        """ State 7 """
        ClearTalkProgressData()
        StopEventAnimWithoutForcingConversationEnd(0)
        """ State 6 """
        ReportConversationEndToHavokBehavior()
    else:
        pass
    """ State 2 """
    if CheckSpecificPersonGenericDialogIsOpen(0) == 1:
        """ State 3 """
        ForceCloseGenericDialog()
    else:
        pass
    """ State 4 """
    if CheckSpecificPersonMenuIsOpen(-1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0):
        """ State 5 """
        ForceCloseMenu()
    else:
        pass
    """ State 8 """
    return 0

def t510020_x3(lot2=_):
    """ State 0,1 """
    GetItemFromItemLot(lot2)
    assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 2 """
    return 0

def t510020_x4(z9=0, z10=9000, flag3=6001):
    """ State 0,1 """
    if GetEventStatus(flag3) == 1:
        """ State 2 """
        pass
    else:
        """ State 3,4 """
        AcquireGesture(z9)
        OpenItemAcquisitionMenu(3, z10, 1)
        SetEventState(flag3, 1)
        assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 5 """
    return 0

def t510020_x5(action5=_):
    """ State 0,1 """
    OpenGenericDialog(7, action5, 1, 0, 1)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    return 0

def t510020_x6(goods1=379, z3=12000007):
    """ State 0,2 """
    ClearQuantityValueOfChooseQuantityDialog()
    """ State 1 """
    OpenChooseQuantityDialog(goods1, z3)
    if GetValueFromNumberSelectDialog() >= 0:
        """ State 3,5 """
        return 0
    elif not (CheckSpecificPersonMenuIsOpen(13, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0)):
        """ State 4,6 """
        return 1

def t510020_x7():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x8():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x9(z6=27, lot2=4288, lot3=4287):
    """ State 0,1 """
    if ComparePlayerStatus(z6, 4, 2) == 1:
        """ State 4,6 """
        assert t510020_x3(lot2=lot3)
    elif ComparePlayerStatus(z6, 0, 1) == 1:
        """ State 3,5 """
        assert t510020_x3(lot2=lot2)
    else:
        """ State 2 """
        pass
    """ State 7 """
    return 0

def t510020_x10():
    """ State 0,1 """
    while True:
        call = t510020_x12()
        assert not GetEventStatus(6001)
        """ State 2 """
        call = t510020_x13()
        assert GetEventStatus(6001) == 1

def t510020_x11():


""" State 0,1 """
    if GetEventStatus(flag3) == 1:
        """ State 2 """
        pass
    else:
        """ State 3,4 """
        AcquireGesture(z9)
        OpenItemAcquisitionMenu(3, z10, 1)
        SetEventState(flag3, 1)
        assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1

def t510020_1():
    """ State 0,1 """
    assert GetCurrentStateElapsedTime() > 1
    """ State 2 """
    while True:
        call = t510020_x10()
        assert IsClientPlayer() == 1
        """ State 3 """
        call = t510020_x11()
        assert not IsClientPlayer()

def t510020_x0(action2=12000026):
    """ State 0,1 """
    OpenGenericDialog(8, action2, 3, 4, 2)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    if GetGenericDialogButtonResult() == 1:
        """ State 3 """
        return 0
    else:
        """ State 4 """
        return 1

def t510020_x1(z8=6018, flag4=6001, flag5=6000, flag6=6000, flag7=6000, flag8=6000):
    """ State 0,1 """
    while True:
        assert (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
                and not IsPlayerDead() and not IsCharacterDisabled())
        """ State 3 """
        assert (GetEventStatus(flag4) == 1 or GetEventStatus(flag5) == 1 or GetEventStatus(flag6) ==
                1 or GetEventStatus(flag7) == 1 or GetEventStatus(flag8) == 1)
        """ State 2 """
        if (not (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
            and not IsPlayerDead() and not IsCharacterDisabled())):
            pass
        elif (not GetEventStatus(flag4) and not GetEventStatus(flag5) and not GetEventStatus(flag6) and
              not GetEventStatus(flag7) and not GetEventStatus(flag8)):
            pass
        elif CheckActionButtonArea(z8):
            break
    """ State 4 """
    return 0

def t510020_x2():
    """ State 0,1 """
    if not CheckSpecificPersonTalkHasEnded(0):
        """ State 7 """
        ClearTalkProgressData()
        StopEventAnimWithoutForcingConversationEnd(0)
        """ State 6 """
        ReportConversationEndToHavokBehavior()
    else:
        pass
    """ State 2 """
    if CheckSpecificPersonGenericDialogIsOpen(0) == 1:
        """ State 3 """
        ForceCloseGenericDialog()
    else:
        pass
    """ State 4 """
    if CheckSpecificPersonMenuIsOpen(-1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0):
        """ State 5 """
        ForceCloseMenu()
    else:
        pass
    """ State 8 """
    return 0

def t510020_x3(lot2=_):
    """ State 0,1 """
    GetItemFromItemLot(lot2)
    assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 2 """
    return 0

def t510020_x4(z9=0, z10=9000, flag3=6001):

    """ State 5 """
    return 0

def t510020_x5(action5=_):
    """ State 0,1 """
    OpenGenericDialog(7, action5, 1, 0, 1)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    return 0

def t510020_x6(goods1=379, z3=12000007):
    """ State 0,2 """
    ClearQuantityValueOfChooseQuantityDialog()
    """ State 1 """
    OpenChooseQuantityDialog(goods1, z3)
    if GetValueFromNumberSelectDialog() >= 0:
        """ State 3,5 """
        return 0
    elif not (CheckSpecificPersonMenuIsOpen(13, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0)):
        """ State 4,6 """
        return 1

def t510020_x7():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x8():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x9(z6=27, lot2=4288, lot3=4287):
    """ State 0,1 """
    if ComparePlayerStatus(z6, 4, 2) == 1:
        """ State 4,6 """
        assert t510020_x3(lot2=lot3)
    elif ComparePlayerStatus(z6, 0, 1) == 1:
        """ State 3,5 """
        assert t510020_x3(lot2=lot2)
    else:
        """ State 2 """
        pass
    """ State 7 """
    return 0

def t510020_x10():
    """ State 0,1 """
    while True:
        call = t510020_x12()
        assert not GetEventStatus(6001)
        """ State 2 """
        call = t510020_x13()
        assert GetEventStatus(6001) == 1

def t510020_x11():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x12():
    """ State 0,1 """
    # goods:379:, action:12000026:Join Covenant?, action:13000046:You have obtained proof of the covenant, action:13000037:, action:13000017:, action:13000007:
    t510020_x14(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, z7=6830, flag1=75100951, flag2=75100952, z8=6018, flag3=6001, z9=0,
                z10=9000, flag4=6001, z11=75100956, z12=75100957)

def t510020_x13():
    """ State 0 """

def t510020_x14(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, z7=6830, flag1=75100951, flag2=75100952, z8=6018, flag3=6001, z9=0,
                z10=9000, flag4=6001, z11=75100956, z12=75100957):
    """ State 0 """
    ClearPlayerDamageInfo()
    """ State 2 """
    while True:
        assert t510020_x1(z8=z8, flag4=flag4, flag5=6000, flag6=6000, flag7=6000, flag8=6000)
        """ State 3 """
        ClearPlayerDamageInfo()
        call = t510020_x15(lot1=lot1, goods1=goods1, lot2=lot2, lot3=lot3, z1=z1, action1=action1, action2=action2,
                           action3=action3, z2=z2, action4=action4, z3=z3, action5=action5, action6=action6,
                           z4=z4, z5=z5, z6=z6, flag1=flag1, flag2=flag2, flag3=flag3, z9=z9, z10=z10,
                           z11=z11, z12=z12)
        def ExitPause():
            TurnCharacterToFaceEntity(69002, 10000, -1)
        if call.Done():
            pass
        elif IsPlayerDead() == 1:
            break
        elif GetDistanceToPlayer() > 12:
            """ State 6 """
            assert t510020_x8()
        elif HasPlayerBeenAttacked() == 1:
            """ State 4 """
            assert t510020_x2()
            """ State 1 """
            TurnCharacterToFaceEntity(69002, 10000, -1)
            ClearPlayerDamageInfo()
    """ State 5 """
    t510020_x7()

def t510020_x15(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, flag1=75100951, flag2=75100952, flag3=6001, z9=0, z10=9000, z11=75100956,
                z12=75100957):
    """ State 0,1 """
    TurnCharacterToFaceEntity(69000, 10000, -1)
    SetTalkTime(1)
    assert GetCurrentStateElapsedTime() > 1
    """ State 14 """
    assert t510020_x9(z6=z6, lot2=lot2, lot3=lot3)
    """ State 4 """
    c1110()
    """ State 2 """
    while True:
        ClearTalkListData()
        """ State 3 """
        AddTalkListData(2, action1, -1)
        AddTalkListData(99, 15000005, -1)
        """ State 5 """
        ShowShopMessage(1)
        assert not (CheckSpecificPersonMenuIsOpen(1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0))
        """ State 11 """
        if GetTalkListEntryResult() == 2:
            """ State 10,18 """
            assert (t510020_x16(goods1=goods1, z4=z4, z5=z5, z6=z6, z2=z2, action4=action4, z3=z3, action5=action5,
                    action6=action6, lot2=lot2, lot3=lot3, flag2=flag2, z11=z11, z12=z12))
        else:
            """ State 12,19 """
            return 0

def t510020_x16(goods1=379, z4=99, z5=20, z6=27, z2=13000027, action4=13000037, z3=12000007, action5=13000017,
                action6=13000007, lot2=4288, lot3=4287, flag2=75100952, z11=75100956, z12=75100957):
    """ State 0,1 """
    if ComparePlayerInventoryNumber(3, goods1, 2, 0, 0) == 1:
        """ State 2 """
        SetWorkValue(0, GetPlayerStatus(z6))
        """ State 15 """
        call = t510020_x6(goods1=goods1, z3=z3)
        if call.Get() == 0:
            """ State 3 """
            SetEventState(flag2, 1)
            assert GetCurrentStateElapsedTime() > 2
            """ State 4,6 """
            PlayerEquipmentQuantityChange(3, goods1, -1 * GetValueFromNumberSelectDialog())
            """ State 5 """
            ChangePlayerStats(z5, 0, GetValueFromNumberSelectDialog() * 1)
            """ State 7 """
            SetEventState(z11, 1)
            """ State 9 """
            if GetWorkValue(0) > 2:
                """ State 10 """
                SetEventState(z12, 1)
                """ State 20 """
                assert t510020_x3(lot2=lot3)
                """ State 19 """
                Label('L0')
                assert t510020_x5(action5=action6)
            else:
                """ State 8 """
                if ComparePlayerStatus(z6, 2, GetWorkValue(0)):
                    """ State 11 """
                    if ComparePlayerStatus(z6, 3, 1) == 1:
                        """ State 16 """
                        assert t510020_x3(lot2=lot2)
                    else:
                        """ State 12 """
                        SetEventState(z12, 1)
                        """ State 18 """
                        assert t510020_x3(lot2=lot3)
                    """ State 17 """
                    assert t510020_x5(action5=action5)
                else:
                    Goto('L0')
            """ State 13 """
            assert not GetEventStatus(flag2)
        elif call.Get() == 1:
            """ State 14 """
            pass
    else:
        """ State 21 """
        assert t510020_x5(action5=action4)
    """ State 22 """
    return 0


def t510020_1():
    """ State 0,1 """
    assert GetCurrentStateElapsedTime() > 1
    """ State 2 """
    while True:
        call = t510020_x10()
        assert IsClientPlayer() == 1
        """ State 3 """
        call = t510020_x11()
        assert not IsClientPlayer()

def t510020_x0(action2=12000026):
    """ State 0,1 """
    OpenGenericDialog(8, action2, 3, 4, 2)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    if GetGenericDialogButtonResult() == 1:
        """ State 3 """
        return 0
    else:
        """ State 4 """
        return 1

def t510020_x1(z8=6018, flag4=6001, flag5=6000, flag6=6000, flag7=6000, flag8=6000):
    """ State 0,1 """
    while True:
        assert (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
                and not IsPlayerDead() and not IsCharacterDisabled())
        """ State 3 """
        assert (GetEventStatus(flag4) == 1 or GetEventStatus(flag5) == 1 or GetEventStatus(flag6) ==
                1 or GetEventStatus(flag7) == 1 or GetEventStatus(flag8) == 1)
        """ State 2 """
        if (not (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
            and not IsPlayerDead() and not IsCharacterDisabled())):
            pass
        elif (not GetEventStatus(flag4) and not GetEventStatus(flag5) and not GetEventStatus(flag6) and
              not GetEventStatus(flag7) and not GetEventStatus(flag8)):
            pass
        elif CheckActionButtonArea(z8):
            break
    """ State 4 """
    return 0

def t510020_x2():
    """ State 0,1 """
    if not CheckSpecificPersonTalkHasEnded(0):
        """ State 7 """
        ClearTalkProgressData()
        StopEventAnimWithoutForcingConversationEnd(0)
        """ State 6 """
        ReportConversationEndToHavokBehavior()
    else:
        pass
    """ State 2 """
    if CheckSpecificPersonGenericDialogIsOpen(0) == 1:
        """ State 3 """
        ForceCloseGenericDialog()
    else:
        pass
    """ State 4 """
    if CheckSpecificPersonMenuIsOpen(-1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0):
        """ State 5 """
        ForceCloseMenu()
    else:
        pass
    """ State 8 """
    return 0

def t510020_x3(lot2=_):
    """ State 0,1 """
    GetItemFromItemLot(lot2)
    assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 2 """
    return 0

def t510020_x4(z9=0, z10=9000, flag3=6001):
    """ State 0,1 """
    if GetEventStatus(flag3) == 1:
        """ State 2 """
        pass
    else:
        """ State 3,4 """
        AcquireGesture(z9)
        OpenItemAcquisitionMenu(3, z10, 1)
        SetEventState(flag3, 1)
        assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1

    return 0

def t510020_x5(action5=_):
    """ State 0,1 """
    OpenGenericDialog(7, action5, 1, 0, 1)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    return 0

def t510020_x6(goods1=379, z3=12000007):
    """ State 0,2 """
    ClearQuantityValueOfChooseQuantityDialog()
    """ State 1 """
    OpenChooseQuantityDialog(goods1, z3)
    if GetValueFromNumberSelectDialog() >= 0:
        """ State 3,5 """
        return 0
    elif not (CheckSpecificPersonMenuIsOpen(13, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0)):
        """ State 4,6 """
        return 1

def t510020_x7():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x8():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x9(z6=27, lot2=4288, lot3=4287):
    """ State 0,1 """
    if ComparePlayerStatus(z6, 4, 2) == 1:
        """ State 4,6 """
        assert t510020_x3(lot2=lot3)
    elif ComparePlayerStatus(z6, 0, 1) == 1:
        """ State 3,5 """
        assert t510020_x3(lot2=lot2)
    else:
        """ State 2 """
        pass
    """ State 7 """
    return 0

def t510020_x10():
    """ State 0,1 """
    while True:
        call = t510020_x12()
        assert not GetEventStatus(6001)
        """ State 2 """
        call = t510020_x13()
        assert GetEventStatus(6001) == 1

def t510020_x11():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x12():
    """ State 0,1 """
    # goods:379:, action:12000026:Join Covenant?, action:13000046:You have obtained proof of the covenant, action:13000037:, action:13000017:, action:13000007:
    t510020_x14(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, z7=6830, flag1=75100951, flag2=75100952, z8=6018, flag3=6001, z9=0,
                z10=9000, flag4=6001, z11=75100956, z12=75100957)

def t510020_x13():
    """ State 0 """

def t510020_x14(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, z7=6830, flag1=75100951, flag2=75100952, z8=6018, flag3=6001, z9=0,
                z10=9000, flag4=6001, z11=75100956, z12=75100957):
    """ State 0 """
    ClearPlayerDamageInfo()
    """ State 2 """
    while True:
        assert t510020_x1(z8=z8, flag4=flag4, flag5=6000, flag6=6000, flag7=6000, flag8=6000)
        """ State 3 """
        ClearPlayerDamageInfo()
        call = t510020_x15(lot1=lot1, goods1=goods1, lot2=lot2, lot3=lot3, z1=z1, action1=action1, action2=action2,
                           action3=action3, z2=z2, action4=action4, z3=z3, action5=action5, action6=action6,
                           z4=z4, z5=z5, z6=z6, flag1=flag1, flag2=flag2, flag3=flag3, z9=z9, z10=z10,
                           z11=z11, z12=z12)
        def ExitPause():
            TurnCharacterToFaceEntity(69002, 10000, -1)
        if call.Done():
            pass
        elif IsPlayerDead() == 1:
            break
        elif GetDistanceToPlayer() > 12:
            """ State 6 """
            assert t510020_x8()
        elif HasPlayerBeenAttacked() == 1:
            """ State 4 """
            assert t510020_x2()
            """ State 1 """
            TurnCharacterToFaceEntity(69002, 10000, -1)
            ClearPlayerDamageInfo()
    """ State 5 """
    t510020_x7()

def t510020_x15(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, flag1=75100951, flag2=75100952, flag3=6001, z9=0, z10=9000, z11=75100956,
                z12=75100957):
    """ State 0,1 """
    TurnCharacterToFaceEntity(69000, 10000, -1)
    SetTalkTime(1)
    assert GetCurrentStateElapsedTime() > 1
    """ State 14 """
    assert t510020_x9(z6=z6, lot2=lot2, lot3=lot3)
    """ State 4 """
    c1110()
    """ State 2 """
    while True:
        ClearTalkListData()
        """ State 3 """
        AddTalkListData(2, action1, -1)
        AddTalkListData(99, 15000005, -1)
        """ State 5 """
        ShowShopMessage(1)
        assert not (CheckSpecificPersonMenuIsOpen(1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0))
        """ State 11 """
        if GetTalkListEntryResult() == 2:
            """ State 10,18 """
            assert (t510020_x16(goods1=goods1, z4=z4, z5=z5, z6=z6, z2=z2, action4=action4, z3=z3, action5=action5,
                    action6=action6, lot2=lot2, lot3=lot3, flag2=flag2, z11=z11, z12=z12))
        else:
            """ State 12,19 """
            return 0

def t510020_x16(goods1=379, z4=99, z5=20, z6=27, z2=13000027, action4=13000037, z3=12000007, action5=13000017,
                action6=13000007, lot2=4288, lot3=4287, flag2=75100952, z11=75100956, z12=75100957):
    """ State 0,1 """
    if ComparePlayerInventoryNumber(3, goods1, 2, 0, 0) == 1:
        """ State 2 """
        SetWorkValue(0, GetPlayerStatus(z6))
        """ State 15 """
        call = t510020_x6(goods1=goods1, z3=z3)
        if call.Get() == 0:
            """ State 3 """
            SetEventState(flag2, 1)
            assert GetCurrentStateElapsedTime() > 2
            """ State 4,6 """
            PlayerEquipmentQuantityChange(3, goods1, -1 * GetValueFromNumberSelectDialog())
            """ State 5 """
            ChangePlayerStats(z5, 0, GetValueFromNumberSelectDialog() * 1)
            """ State 7 """
            SetEventState(z11, 1)
            """ State 9 """
            if GetWorkValue(0) > 2:
                """ State 10 """
                SetEventState(z12, 1)
                """ State 20 """
                assert t510020_x3(lot2=lot3)
                """ State 19 """
                Label('L0')
                assert t510020_x5(action5=action6)
            else:
                """ State 8 """
                if ComparePlayerStatus(z6, 2, GetWorkValue(0)):
                    """ State 11 """
                    if ComparePlayerStatus(z6, 3, 1) == 1:
                        """ State 16 """
                        assert t510020_x3(lot2=lot2)
                    else:
                        """ State 12 """
                        SetEventState(z12, 1)
                        """ State 18 """
                        assert t510020_x3(lot2=lot3)
                    """ State 17 """
                    assert t510020_x5(action5=action5)
                else:
                    Goto('L0')
            """ State 13 """
            assert not GetEventStatus(flag2)
        elif call.Get() == 1:
            """ State 14 """
            pass
    else:
        """ State 21 """
        assert t510020_x5(action5=action4)
    """ State 22 """
    return 0


def t510020_1():
    """ State 0,1 """
    assert GetCurrentStateElapsedTime() > 1
    """ State 2 """
    while True:
        call = t510020_x10()
        assert IsClientPlayer() == 1
        """ State 3 """
        call = t510020_x11()
        assert not IsClientPlayer()

def t510020_x0(action2=12000026):
    """ State 0,1 """
    OpenGenericDialog(8, action2, 3, 4, 2)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    if GetGenericDialogButtonResult() == 1:
        """ State 3 """
        return 0
    else:
        """ State 4 """
        return 1

def t510020_x1(z8=6018, flag4=6001, flag5=6000, flag6=6000, flag7=6000, flag8=6000):
    """ State 0,1 """
    while True:
        assert (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
                and not IsPlayerDead() and not IsCharacterDisabled())
        """ State 3 """
        assert (GetEventStatus(flag4) == 1 or GetEventStatus(flag5) == 1 or GetEventStatus(flag6) ==
                1 or GetEventStatus(flag7) == 1 or GetEventStatus(flag8) == 1)
        """ State 2 """
        if (not (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
            and not IsPlayerDead() and not IsCharacterDisabled())):
            pass
        elif (not GetEventStatus(flag4) and not GetEventStatus(flag5) and not GetEventStatus(flag6) and
              not GetEventStatus(flag7) and not GetEventStatus(flag8)):
            pass
        elif CheckActionButtonArea(z8):
            break
    """ State 4 """
    return 0

def t510020_x2():
    """ State 0,1 """
    if not CheckSpecificPersonTalkHasEnded(0):
        """ State 7 """
        ClearTalkProgressData()
        StopEventAnimWithoutForcingConversationEnd(0)
        """ State 6 """
        ReportConversationEndToHavokBehavior()
    else:
        pass
    """ State 2 """
    if CheckSpecificPersonGenericDialogIsOpen(0) == 1:
        """ State 3 """
        ForceCloseGenericDialog()
    else:
        pass
    """ State 4 """
    if CheckSpecificPersonMenuIsOpen(-1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0):
        """ State 5 """
        ForceCloseMenu()
    else:
        pass
    """ State 8 """
    return 0

def t510020_x3(lot2=_):
    """ State 0,1 """
    GetItemFromItemLot(lot2)
    assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 2 """
    return 0

def t510020_x4(z9=0, z10=9000, flag3=6001):
    """ State 0,1 """
    if GetEventStatus(flag3) == 1:
        """ State 2 """
        pass
    else:
        """ State 3,4 """
        AcquireGesture(z9)
        OpenItemAcquisitionMenu(3, z10, 1)
        SetEventState(flag3, 1)
        assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 5 """

def t510020_x5(action5=_):
    """ State 0,1 """
    OpenGenericDialog(7, action5, 1, 0, 1)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    return 0

def t510020_x6(goods1=379, z3=12000007):
    """ State 0,2 """
    ClearQuantityValueOfChooseQuantityDialog()
    """ State 1 """
    OpenChooseQuantityDialog(goods1, z3)
    if GetValueFromNumberSelectDialog() >= 0:
        """ State 3,5 """
        return 0
    elif not (CheckSpecificPersonMenuIsOpen(13, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0)):
        """ State 4,6 """
        return 1

def t510020_x7():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x8():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x9(z6=27, lot2=4288, lot3=4287):
    """ State 0,1 """
    if ComparePlayerStatus(z6, 4, 2) == 1:
        """ State 4,6 """
        assert t510020_x3(lot2=lot3)
    elif ComparePlayerStatus(z6, 0, 1) == 1:
        """ State 3,5 """
        assert t510020_x3(lot2=lot2)
    else:
        """ State 2 """
        pass
    """ State 7 """
    return 0

def t510020_x10():
    """ State 0,1 """
    while True:
        call = t510020_x12()
        assert not GetEventStatus(6001)
        """ State 2 """
        call = t510020_x13()
        assert GetEventStatus(6001) == 1

def t510020_x11():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x12():
    """ State 0,1 """
    # goods:379:, action:12000026:Join Covenant?, action:13000046:You have obtained proof of the covenant, action:13000037:, action:13000017:, action:13000007:
    t510020_x14(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, z7=6830, flag1=75100951, flag2=75100952, z8=6018, flag3=6001, z9=0,
                z10=9000, flag4=6001, z11=75100956, z12=75100957)

def t510020_x13():
    """ State 0 """

def t510020_x14(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, z7=6830, flag1=75100951, flag2=75100952, z8=6018, flag3=6001, z9=0,
                z10=9000, flag4=6001, z11=75100956, z12=75100957):
    """ State 0 """
    ClearPlayerDamageInfo()
    """ State 2 """
    while True:
        assert t510020_x1(z8=z8, flag4=flag4, flag5=6000, flag6=6000, flag7=6000, flag8=6000)
        """ State 3 """
        ClearPlayerDamageInfo()
        call = t510020_x15(lot1=lot1, goods1=goods1, lot2=lot2, lot3=lot3, z1=z1, action1=action1, action2=action2,
                           action3=action3, z2=z2, action4=action4, z3=z3, action5=action5, action6=action6,
                           z4=z4, z5=z5, z6=z6, flag1=flag1, flag2=flag2, flag3=flag3, z9=z9, z10=z10,
                           z11=z11, z12=z12)
        def ExitPause():
            TurnCharacterToFaceEntity(69002, 10000, -1)
        if call.Done():
            pass
        elif IsPlayerDead() == 1:
            break
        elif GetDistanceToPlayer() > 12:
            """ State 6 """
            assert t510020_x8()
        elif HasPlayerBeenAttacked() == 1:
            """ State 4 """
            assert t510020_x2()
            """ State 1 """
            TurnCharacterToFaceEntity(69002, 10000, -1)
            ClearPlayerDamageInfo()
    """ State 5 """
    t510020_x7()

def t510020_x15(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, flag1=75100951, flag2=75100952, flag3=6001, z9=0, z10=9000, z11=75100956,
                z12=75100957):
    """ State 0,1 """
    TurnCharacterToFaceEntity(69000, 10000, -1)
    SetTalkTime(1)
    assert GetCurrentStateElapsedTime() > 1
    """ State 14 """
    assert t510020_x9(z6=z6, lot2=lot2, lot3=lot3)
    """ State 4 """
    c1110()
    """ State 2 """
    while True:
        ClearTalkListData()
        """ State 3 """
        AddTalkListData(2, action1, -1)
        AddTalkListData(99, 15000005, -1)
        """ State 5 """
        ShowShopMessage(1)
        assert not (CheckSpecificPersonMenuIsOpen(1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0))
        """ State 11 """
        if GetTalkListEntryResult() == 2:
            """ State 10,18 """
            assert (t510020_x16(goods1=goods1, z4=z4, z5=z5, z6=z6, z2=z2, action4=action4, z3=z3, action5=action5,
                    action6=action6, lot2=lot2, lot3=lot3, flag2=flag2, z11=z11, z12=z12))
        else:
            """ State 12,19 """
            return 0

def t510020_x16(goods1=379, z4=99, z5=20, z6=27, z2=13000027, action4=13000037, z3=12000007, action5=13000017,
                action6=13000007, lot2=4288, lot3=4287, flag2=75100952, z11=75100956, z12=75100957):
    """ State 0,1 """
    if ComparePlayerInventoryNumber(3, goods1, 2, 0, 0) == 1:
        """ State 2 """
        SetWorkValue(0, GetPlayerStatus(z6))
        """ State 15 """
        call = t510020_x6(goods1=goods1, z3=z3)
        if call.Get() == 0:
            """ State 3 """
            SetEventState(flag2, 1)
            assert GetCurrentStateElapsedTime() > 2
            """ State 4,6 """
            PlayerEquipmentQuantityChange(3, goods1, -1 * GetValueFromNumberSelectDialog())
            """ State 5 """
            ChangePlayerStats(z5, 0, GetValueFromNumberSelectDialog() * 1)
            """ State 7 """
            SetEventState(z11, 1)
            """ State 9 """
            if GetWorkValue(0) > 2:
                """ State 10 """
                SetEventState(z12, 1)
                """ State 20 """
                assert t510020_x3(lot2=lot3)
                """ State 19 """
                Label('L0')
                assert t510020_x5(action5=action6)
            else:
                """ State 8 """
                if ComparePlayerStatus(z6, 2, GetWorkValue(0)):
                    """ State 11 """
                    if ComparePlayerStatus(z6, 3, 1) == 1:
                        """ State 16 """
                        assert t510020_x3(lot2=lot2)
                    else:
                        """ State 12 """
                        SetEventState(z12, 1)
                        """ State 18 """
                        assert t510020_x3(lot2=lot3)
                    """ State 17 """
                    assert t510020_x5(action5=action5)
                else:
                    Goto('L0')
            """ State 13 """
            assert not GetEventStatus(flag2)
        elif call.Get() == 1:
            """ State 14 """
            pass
    else:
        """ State 21 """
        assert t510020_x5(action5=action4)
    """ State 22 """
    return 0


def t510020_1():
    """ State 0,1 """
    assert GetCurrentStateElapsedTime() > 1
    """ State 2 """
    while True:
        call = t510020_x10()
        assert IsClientPlayer() == 1
        """ State 3 """
        call = t510020_x11()
        assert not IsClientPlayer()

def t510020_x0(action2=12000026):
    """ State 0,1 """
    OpenGenericDialog(8, action2, 3, 4, 2)
    assert not Ch

    """ State 2 """
    if GetGenericDialogButtonResult() == 1:
        """ State 3 """
        return 0
    else:
        """ State 4 """
        return 1

def t510020_x1(z8=6018, flag4=6001, flag5=6000, flag6=6000, flag7=6000, flag8=6000):
    """ State 0,1 """
    while True:
        assert (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
                and not IsPlayerDead() and not IsCharacterDisabled())
        """ State 3 """
        assert (GetEventStatus(flag4) == 1 or GetEventStatus(flag5) == 1 or GetEventStatus(flag6) ==
                1 or GetEventStatus(flag7) == 1 or GetEventStatus(flag8) == 1)
        """ State 2 """
        if (not (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
            and not IsPlayerDead() and not IsCharacterDisabled())):
            pass
        elif (not GetEventStatus(flag4) and not GetEventStatus(flag5) and not GetEventStatus(flag6) and
              not GetEventStatus(flag7) and not GetEventStatus(flag8)):
            pass
        elif CheckActionButtonArea(z8):
            break
    """ State 4 """
    return 0

def t510020_x2():
    """ State 0,1 """
    if not CheckSpecificPersonTalkHasEnded(0):
        """ State 7 """
        ClearTalkProgressData()
        StopEventAnimWithoutForcingConversationEnd(0)
        """ State 6 """
        ReportConversationEndToHavokBehavior()
    else:
        pass
    """ State 2 """
    if CheckSpecificPersonGenericDialogIsOpen(0) == 1:
        """ State 3 """
        ForceCloseGenericDialog()
    else:
        pass
    """ State 4 """
    if CheckSpecificPersonMenuIsOpen(-1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0):
        """ State 5 """
        ForceCloseMenu()
    else:
        pass
    """ State 8 """
    return 0

def t510020_x3(lot2=_):
    """ State 0,1 """
    GetItemFromItemLot(lot2)
    assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 2 """
    return 0

def t510020_x4(z9=0, z10=9000, flag3=6001):
    """ State 0,1 """
    if GetEventStatus(flag3) == 1:
        """ State 2 """
        pass
    else:
        """ State 3,4 """
        AcquireGesture(z9)
        OpenItemAcquisitionMenu(3, z10, 1)
        SetEventState(flag3, 1)
        assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 5 """
    return 0

def t510020_x5(action5=_):
    """ State 0,1 """
    OpenGenericDialog(7, action5, 1, 0, 1)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    return 0

def t510020_x6(goods1=379, z3=12000007):
    """ State 0,2 """
    ClearQuantityValueOfChooseQuantityDialog()
    """ State 1 """
    OpenChooseQuantityDialog(goods1, z3)
    if GetValueFromNumberSelectDialog() >= 0:
        """ State 3,5 """
        return 0
    elif not (CheckSpecificPersonMenuIsOpen(13, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0)):
        """ State 4,6 """
        return 1

def t510020_x7():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x8():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x9(z6=27, lot2=4288, lot3=4287):
    """ State 0,1 """
    if ComparePlayerStatus(z6, 4, 2) == 1:
        """ State 4,6 """
        assert t510020_x3(lot2=lot3)
    elif ComparePlayerStatus(z6, 0, 1) == 1:
        """ State 3,5 """
        assert t510020_x3(lot2=lot2)
    else:
        """ State 2 """
        pass
    """ State 7 """
    return 0

def t510020_x10():
    """ State 0,1 """
    while True:
        call = t510020_x12()
        assert not GetEventStatus(6001)
        """ State 2 """
        call = t510020_x13()
        assert GetEventStatus(6001) == 1

def t510020_x11():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x12():
    """ State 0,1 """
    # goods:379:, action:12000026:Join Covenant?, action:13000046:You have obtained proof of the covenant, action:13000037:, action:13000017:, action:13000007:
    t510020_x14(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, z7=6830, flag1=75100951, flag2=75100952, z8=6018, flag3=6001, z9=0,
                z10=9000, flag4=6001, z11=75100956, z12=75100957)

def t510020_x13():
    """ State 0 """

def t510020_x14(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, z7=6830, flag1=75100951, flag2=75100952, z8=6018, flag3=6001, z9=0,
                z10=9000, flag4=6001, z11=75100956, z12=75100957):
    """ State 0 """
    ClearPlayerDamageInfo()
    """ State 2 """
    while True:
        assert t510020_x1(z8=z8, flag4=flag4, flag5=6000, flag6=6000, flag7=6000, flag8=6000)
        """ State 3 """
        ClearPlayerDamageInfo()
        call = t510020_x15(lot1=lot1, goods1=goods1, lot2=lot2, lot3=lot3, z1=z1, action1=action1, action2=action2,
                           action3=action3, z2=z2, action4=action4, z3=z3, action5=action5, action6=action6,
                           z4=z4, z5=z5, z6=z6, flag1=flag1, flag2=flag2, flag3=flag3, z9=z9, z10=z10,
                           z11=z11, z12=z12)
        def ExitPause():
            TurnCharacterToFaceEntity(69002, 10000, -1)
        if call.Done():
            pass
        elif IsPlayerDead() == 1:
            break
        elif GetDistanceToPlayer() > 12:
            """ State 6 """
            assert t510020_x8()
        elif HasPlayerBeenAttacked() == 1:
            """ State 4 """
            assert t510020_x2()
            """ State 1 """
            TurnCharacterToFaceEntity(69002, 10000, -1)
            ClearPlayerDamageInfo()
    """ State 5 """
    t510020_x7()

def t510020_x15(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, flag1=75100951, flag2=75100952, flag3=6001, z9=0, z10=9000, z11=75100956,
                z12=75100957):
    """ State 0,1 """
    TurnCharacterToFaceEntity(69000, 10000, -1)
    SetTalkTime(1)
    assert GetCurrentStateElapsedTime() > 1
    """ State 14 """
    assert t510020_x9(z6=z6, lot2=lot2, lot3=lot3)
    """ State 4 """
    c1110()
    """ State 2 """
    while True:
        ClearTalkListData()
        """ State 3 """
        AddTalkListData(2, action1, -1)
        AddTalkListData(99, 15000005, -1)
        """ State 5 """
        ShowShopMessage(1)
        assert not (CheckSpecificPersonMenuIsOpen(1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0))
        """ State 11 """
        if GetTalkListEntryResult() == 2:
            """ State 10,18 """
            assert (t510020_x16(goods1=goods1, z4=z4, z5=z5, z6=z6, z2=z2, action4=action4, z3=z3, action5=action5,
                    action6=action6, lot2=lot2, lot3=lot3, flag2=flag2, z11=z11, z12=z12))
        else:
            """ State 12,19 """
            return 0

def t510020_x16(goods1=379, z4=99, z5=20, z6=27, z2=13000027, action4=13000037, z3=12000007, action5=13000017,
                action6=13000007, lot2=4288, lot3=4287, flag2=75100952, z11=75100956, z12=75100957):
    """ State 0,1 """
    if ComparePlayerInventoryNumber(3, goods1, 2, 0, 0) == 1:
        """ State 2 """
        SetWorkValue(0, GetPlayerStatus(z6))
        """ State 15 """
        call = t510020_x6(goods1=goods1, z3=z3)
        if call.Get() == 0:
            """ State 3 """
            SetEventState(flag2, 1)
            assert GetCurrentStateElapsedTime() > 2
            """ State 4,6 """
            PlayerEquipmentQuantityChange(3, goods1, -1 * GetValueFromNumberSelectDialog())
            """ State 5 """
            ChangePlayerStats(z5, 0, GetValueFromNumberSelectDialog() * 1)
            """ State 7 """
            SetEventState(z11, 1)
            """ State 9 """
            if GetWorkValue(0) > 2:
                """ State 10 """
                SetEventState(z12, 1)
                """ State 20 """
                assert t510020_x3(lot2=lot3)
                """ State 19 """
                Label('L0')
                assert t510020_x5(action5=action6)
            else:
                """ State 8 """
                if ComparePlayerStatus(z6, 2, GetWorkValue(0)):
                    """ State 11 """
                    if ComparePlayerStatus(z6, 3, 1) == 1:
                        """ State 16 """
                        assert t510020_x3(lot2=lot2)
                    else:
                        """ State 12 """
                        SetEventState(z12, 1)
                        """ State 18 """
                        assert t510020_x3(lot2=lot3)
                    """ State 17 """
                    assert t510020_x5(action5=action5)
                else:
                    Goto('L0')
            """ State 13 """
            assert not GetEventStatus(flag2)
        elif call.Get() == 1:
            """ State 14 """
            pass
    else:
        """ State 21 """
        assert t510020_x5(action5=action4)
    """ State 22 """
    return 0


    while True:
        call = t510020_x12()
        assert not GetEventStatus(6001)
        """ State 2 """
        call = t510020_x13()
        assert GetEventStatus(6001) == 1

def t510020_1():
    """ State 0,1 """
    assert GetCurrentStateElapsedTime() > 1
    """ State 2 """
    while True:
        call = t510020_x10()
        assert IsClientPlayer() == 1
        """ State 3 """
        call = t510020_x11()
        assert not IsClientPlayer()

def t510020_x0(action2=12000026):
    """ State 0,1 """
    OpenGenericDialog(8, action2, 3, 4, 2)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    if GetGenericDialogButtonResult() == 1:
        """ State 3 """
        return 0
    else:
        """ State 4 """
        return 1

def t510020_x1(z8=6018, flag4=6001, flag5=6000, flag6=6000, flag7=6000, flag8=6000):
    """ State 0,1 """
    while True:
        assert (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
                and not IsPlayerDead() and not IsCharacterDisabled())
        """ State 3 """
        assert (GetEventStatus(flag4) == 1 or GetEventStatus(flag5) == 1 or GetEventStatus(flag6) ==
                1 or GetEventStatus(flag7) == 1 or GetEventStatus(flag8) == 1)
        """ State 2 """
        if (not (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
            and not IsPlayerDead() and not IsCharacterDisabled())):
            pass
        elif (not GetEventStatus(flag4) and not GetEventStatus(flag5) and not GetEventStatus(flag6) and
              not GetEventStatus(flag7) and not GetEventStatus(flag8)):
            pass
        elif CheckActionButtonArea(z8):
            break
    """ State 4 """
    return 0

def t510020_x2():
    """ State 0,1 """
    if not CheckSpecificPersonTalkHasEnded(0):
        """ State 7 """
        ClearTalkProgressData()
        StopEventAnimWithoutForcingConversationEnd(0)
        """ State 6 """
        ReportConversationEndToHavokBehavior()
    else:
        pass
    """ State 2 """
    if CheckSpecificPersonGenericDialogIsOpen(0) == 1:
        """ State 3 """
        ForceCloseGenericDialog()
    else:
        pass
    """ State 4 """
    if CheckSpecificPersonMenuIsOpen(-1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0):
        """ State 5 """
        ForceCloseMenu()
    else:
        pass
    """ State 8 """
    return 0

def t510020_x3(lot2=_):
    """ State 0,1 """
    GetItemFromItemLot(lot2)
    assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 2 """
    return 0

def t510020_x4(z9=0, z10=9000, flag3=6001):
    """ State 0,1 """
    if GetEventStatus(flag3) == 1:
        """ State 2 """
        pass
    else:
        """ State 3,4 """
        AcquireGesture(z9)
        OpenItemAcquisitionMenu(3, z10, 1)
        SetEventState(flag3, 1)
        assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 5 """
    return 0

def t510020_x5(action5=_):
    """ State 0,1 """
    OpenGenericDialog(7, action5, 1, 0, 1)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    return 0

def t510020_x6(goods1=379, z3=12000007):
    """ State 0,2 """
    ClearQuantityValueOfChooseQuantityDialog()
    """ State 1 """
    OpenChooseQuantityDialog(goods1, z3)
    if GetValueFromNumberSelectDialog() >= 0:
        """ State 3,5 """
        return 0
    elif not (CheckSpecificPersonMenuIsOpen(13, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0)):
        """ State 4,6 """
        return 1

def t510020_x7():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x8():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x9(z6=27, lot2=4288, lot3=4287):
    """ State 0,1 """
    if ComparePlayerStatus(z6, 4, 2) == 1:
        """ State 4,6 """
        assert t510020_x3(lot2=lot3)
    elif ComparePlayerStatus(z6, 0, 1) == 1:
        """ State 3,5 """
        assert t510020_x3(lot2=lot2)
    else:
        """ State 2 """
        pass
    """ State 7 """
    return 0

def t510020_x10():
    """ State 0,1 """


if GetGenericDialogButtonResult() == 1:
        """ State 3 """
        return 0
    else:
        """ State 4 """
        return 1

def t510020_1():
    """ State 0,1 """
    assert GetCurrentStateElapsedTime() > 1
    """ State 2 """
    while True:
        call = t510020_x10()
        assert IsClientPlayer() == 1
        """ State 3 """
        call = t510020_x11()
        assert not IsClientPlayer()

def t510020_x0(action2=12000026):
    """ State 0,1 """
    OpenGenericDialog(8, action2, 3, 4, 2)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """

def t510020_x1(z8=6018, flag4=6001, flag5=6000, flag6=6000, flag7=6000, flag8=6000):
    """ State 0,1 """
    while True:
        assert (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
                and not IsPlayerDead() and not IsCharacterDisabled())
        """ State 3 """
        assert (GetEventStatus(flag4) == 1 or GetEventStatus(flag5) == 1 or GetEventStatus(flag6) ==
                1 or GetEventStatus(flag7) == 1 or GetEventStatus(flag8) == 1)
        """ State 2 """
        if (not (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
            and not IsPlayerDead() and not IsCharacterDisabled())):
            pass
        elif (not GetEventStatus(flag4) and not GetEventStatus(flag5) and not GetEventStatus(flag6) and
              not GetEventStatus(flag7) and not GetEventStatus(flag8)):
            pass
        elif CheckActionButtonArea(z8):
            break
    """ State 4 """
    return 0

def t510020_x2():
    """ State 0,1 """
    if not CheckSpecificPersonTalkHasEnded(0):
        """ State 7 """
        ClearTalkProgressData()
        StopEventAnimWithoutForcingConversationEnd(0)
        """ State 6 """
        ReportConversationEndToHavokBehavior()
    else:
        pass
    """ State 2 """
    if CheckSpecificPersonGenericDialogIsOpen(0) == 1:
        """ State 3 """
        ForceCloseGenericDialog()
    else:
        pass
    """ State 4 """
    if CheckSpecificPersonMenuIsOpen(-1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0):
        """ State 5 """
        ForceCloseMenu()
    else:
        pass
    """ State 8 """
    return 0

def t510020_x3(lot2=_):
    """ State 0,1 """
    GetItemFromItemLot(lot2)
    assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 2 """
    return 0

def t510020_x4(z9=0, z10=9000, flag3=6001):
    """ State 0,1 """
    if GetEventStatus(flag3) == 1:
        """ State 2 """
        pass
    else:
        """ State 3,4 """
        AcquireGesture(z9)
        OpenItemAcquisitionMenu(3, z10, 1)
        SetEventState(flag3, 1)
        assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 5 """
    return 0

def t510020_x5(action5=_):
    """ State 0,1 """
    OpenGenericDialog(7, action5, 1, 0, 1)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    return 0

def t510020_x6(goods1=379, z3=12000007):
    """ State 0,2 """
    ClearQuantityValueOfChooseQuantityDialog()
    """ State 1 """
    OpenChooseQuantityDialog(goods1, z3)
    if GetValueFromNumberSelectDialog() >= 0:
        """ State 3,5 """
        return 0
    elif not (CheckSpecificPersonMenuIsOpen(13, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0)):
        """ State 4,6 """
        return 1

def t510020_x7():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x8():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x9(z6=27, lot2=4288, lot3=4287):
    """ State 0,1 """
    if ComparePlayerStatus(z6, 4, 2) == 1:
        """ State 4,6 """
        assert t510020_x3(lot2=lot3)
    elif ComparePlayerStatus(z6, 0, 1) == 1:
        """ State 3,5 """
        assert t510020_x3(lot2=lot2)
    else:
        """ State 2 """
        pass
    """ State 7 """
    return 0

def t510020_x10():
    """ State 0,1 """
    while True:
        call = t510020_x12()
        assert not GetEventStatus(6001)
        """ State 2 """
        call = t510020_x13()
        assert GetEventStatus(6001) == 1

def t510020_x11():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x12():
    """ State 0,1 """
    # goods:379:, action:12000026:Join Covenant?, action:13000046:You have obtained proof of the covenant, action:13000037:, action:13000017:, action:13000007:
    t510020_x14(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, z7=6830, flag1=75100951, flag2=75100952, z8=6018, flag3=6001, z9=0,
                z10=9000, flag4=6001, z11=75100956, z12=75100957)

def t510020_x13():
    """ State 0 """

def t510020_x14(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, z7=6830, flag1=75100951, flag2=75100952, z8=6018, flag3=6001, z9=0,
                z10=9000, flag4=6001, z11=75100956, z12=75100957):
    """ State 0 """
    ClearPlayerDamageInfo()
    """ State 2 """
    while True:
        assert t510020_x1(z8=z8, flag4=flag4, flag5=6000, flag6=6000, flag7=6000, flag8=6000)
        """ State 3 """
        ClearPlayerDamageInfo()
        call = t510020_x15(lot1=lot1, goods1=goods1, lot2=lot2, lot3=lot3, z1=z1, action1=action1, action2=action2,
                           action3=action3, z2=z2, action4=action4, z3=z3, action5=action5, action6=action6,
                           z4=z4, z5=z5, z6=z6, flag1=flag1, flag2=flag2, flag3=flag3, z9=z9, z10=z10,
                           z11=z11, z12=z12)
        def ExitPause():
            TurnCharacterToFaceEntity(69002, 10000, -1)
        if call.Done():
            pass
        elif IsPlayerDead() == 1:
            break
        elif GetDistanceToPlayer() > 12:
            """ State 6 """
            assert t510020_x8()
        elif HasPlayerBeenAttacked() == 1:
            """ State 4 """
            assert t510020_x2()
            """ State 1 """
            TurnCharacterToFaceEntity(69002, 10000, -1)
            ClearPlayerDamageInfo()
    """ State 5 """
    t510020_x7()

def t510020_x15(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, flag1=75100951, flag2=75100952, flag3=6001, z9=0, z10=9000, z11=75100956,
                z12=75100957):
    """ State 0,1 """
    TurnCharacterToFaceEntity(69000, 10000, -1)
    SetTalkTime(1)
    assert GetCurrentStateElapsedTime() > 1
    """ State 14 """
    assert t510020_x9(z6=z6, lot2=lot2, lot3=lot3)
    """ State 4 """
    c1110()
    """ State 2 """
    while True:
        ClearTalkListData()
        """ State 3 """
        AddTalkListData(2, action1, -1)
        AddTalkListData(99, 15000005, -1)
        """ State 5 """
        ShowShopMessage(1)
        assert not (CheckSpecificPersonMenuIsOpen(1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0))
        """ State 11 """
        if GetTalkListEntryResult() == 2:
            """ State 10,18 """
            assert (t510020_x16(goods1=goods1, z4=z4, z5=z5, z6=z6, z2=z2, action4=action4, z3=z3, action5=action5,
                    action6=action6, lot2=lot2, lot3=lot3, flag2=flag2, z11=z11, z12=z12))
        else:
            """ State 12,19 """
            return 0

def t510020_x16(goods1=379, z4=99, z5=20, z6=27, z2=13000027, action4=13000037, z3=12000007, action5=13000017,
                action6=13000007, lot2=4288, lot3=4287, flag2=75100952, z11=75100956, z12=75100957):
    """ State 0,1 """
    if ComparePlayerInventoryNumber(3, goods1, 2, 0, 0) == 1:
        """ State 2 """
        SetWorkValue(0, GetPlayerStatus(z6))
        """ State 15 """
        call = t510020_x6(goods1=goods1, z3=z3)
        if call.Get() == 0:
            """ State 3 """
            SetEventState(flag2, 1)
            assert GetCurrentStateElapsedTime() > 2
            """ State 4,6 """
            PlayerEquipmentQuantityChange(3, goods1, -1 * GetValueFromNumberSelectDialog())
            """ State 5 """
            ChangePlayerStats(z5, 0, GetValueFromNumberSelectDialog() * 1)
            """ State 7 """
            SetEventState(z11, 1)
            """ State 9 """
            if GetWorkValue(0) > 2:
                """ State 10 """
                SetEventState(z12, 1)
                """ State 20 """
                assert t510020_x3(lot2=lot3)
                """ State 19 """
                Label('L0')
                assert t510020_x5(action5=action6)
            else:
                """ State 8 """
                if ComparePlayerStatus(z6, 2, GetWorkValue(0)):
                    """ State 11 """
                    if ComparePlayerStatus(z6, 3, 1) == 1:
                        """ State 16 """
                        assert t510020_x3(lot2=lot2)
                    else:
                        """ State 12 """
                        SetEventState(z12, 1)
                        """ State 18 """
                        assert t510020_x3(lot2=lot3)
                    """ State 17 """
                    assert t510020_x5(action5=action5)
                else:
                    Goto('L0')
            """ State 13 """
            assert not GetEventStatus(flag2)
        elif call.Get() == 1:
            """ State 14 """
            pass
    else:
        """ State 21 """
        assert t510020_x5(action5=action4)
    """ State 22 """
    return 0


while True:
        call = t510020_x12()
        assert not GetEventStatus(6001)
        """ State 2 """
        call = t510020_x13()
        assert GetEventStatus(6001) == 1


def t510020_1():
    """ State 0,1 """
    assert GetCurrentStateElapsedTime() > 1
    """ State 2 """
    while True:
        call = t510020_x10()
        assert IsClientPlayer() == 1
        """ State 3 """
        call = t510020_x11()
        assert not IsClientPlayer()

def t510020_x0(action2=12000026):
    """ State 0,1 """
    OpenGenericDialog(8, action2, 3, 4, 2)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    if GetGenericDialogButtonResult() == 1:
        """ State 3 """
        return 0
    else:
        """ State 4 """
        return 1

def t510020_x1(z8=6018, flag4=6001, flag5=6000, flag6=6000, flag7=6000, flag8=6000):
    """ State 0,1 """
    while True:
        assert (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
                and not IsPlayerDead() and not IsCharacterDisabled())
        """ State 3 """
        assert (GetEventStatus(flag4) == 1 or GetEventStatus(flag5) == 1 or GetEventStatus(flag6) ==
                1 or GetEventStatus(flag7) == 1 or GetEventStatus(flag8) == 1)
        """ State 2 """
        if (not (not GetOneLineHelpStatus() and not IsTalkingToSomeoneElse() and not IsClientPlayer()
            and not IsPlayerDead() and not IsCharacterDisabled())):
            pass
        elif (not GetEventStatus(flag4) and not GetEventStatus(flag5) and not GetEventStatus(flag6) and
              not GetEventStatus(flag7) and not GetEventStatus(flag8)):
            pass
        elif CheckActionButtonArea(z8):
            break
    """ State 4 """
    return 0

def t510020_x2():
    """ State 0,1 """
    if not CheckSpecificPersonTalkHasEnded(0):
        """ State 7 """
        ClearTalkProgressData()
        StopEventAnimWithoutForcingConversationEnd(0)
        """ State 6 """
        ReportConversationEndToHavokBehavior()
    else:
        pass
    """ State 2 """
    if CheckSpecificPersonGenericDialogIsOpen(0) == 1:
        """ State 3 """
        ForceCloseGenericDialog()
    else:
        pass
    """ State 4 """
    if CheckSpecificPersonMenuIsOpen(-1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0):
        """ State 5 """
        ForceCloseMenu()
    else:
        pass
    """ State 8 """
    return 0

def t510020_x3(lot2=_):
    """ State 0,1 """
    GetItemFromItemLot(lot2)
    assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 2 """
    return 0

def t510020_x4(z9=0, z10=9000, flag3=6001):
    """ State 0,1 """
    if GetEventStatus(flag3) == 1:
        """ State 2 """
        pass
    else:
        """ State 3,4 """
        AcquireGesture(z9)
        OpenItemAcquisitionMenu(3, z10, 1)
        SetEventState(flag3, 1)
        assert not IsMenuOpen(63) and GetCurrentStateElapsedFrames() > 1
    """ State 5 """
    return 0

def t510020_x5(action5=_):
    """ State 0,1 """
    OpenGenericDialog(7, action5, 1, 0, 1)
    assert not CheckSpecificPersonGenericDialogIsOpen(0)
    """ State 2 """
    return 0

def t510020_x6(goods1=379, z3=12000007):
    """ State 0,2 """
    ClearQuantityValueOfChooseQuantityDialog()
    """ State 1 """
    OpenChooseQuantityDialog(goods1, z3)
    if GetValueFromNumberSelectDialog() >= 0:
        """ State 3,5 """
        return 0
    elif not (CheckSpecificPersonMenuIsOpen(13, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0)):
        """ State 4,6 """
        return 1

def t510020_x7():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x8():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x9(z6=27, lot2=4288, lot3=4287):
    """ State 0,1 """
    if ComparePlayerStatus(z6, 4, 2) == 1:
        """ State 4,6 """
        assert t510020_x3(lot2=lot3)
    elif ComparePlayerStatus(z6, 0, 1) == 1:
        """ State 3,5 """
        assert t510020_x3(lot2=lot2)
    else:
        """ State 2 """
        pass
    """ State 7 """
    return 0

def t510020_x10():
    """ State 0,1 """
    while True:
        call = t510020_x12()
        assert not GetEventStatus(6001)
        """ State 2 """
        call = t510020_x13()
        assert GetEventStatus(6001) == 1

def t510020_x11():
    """ State 0,1 """
    assert t510020_x2()
    """ State 2 """
    return 0

def t510020_x12():
    """ State 0,1 """
    # goods:379:, action:12000026:Join Covenant?, action:13000046:You have obtained proof of the covenant, action:13000037:, action:13000017:, action:13000007:
    t510020_x14(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, z7=6830, flag1=75100951, flag2=75100952, z8=6018, flag3=6001, z9=0,
                z10=9000, flag4=6001, z11=75100956, z12=75100957)

def t510020_x13():
    """ State 0 """

def t510020_x14(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, z7=6830, flag1=75100951, flag2=75100952, z8=6018, flag3=6001, z9=0,
                z10=9000, flag4=6001, z11=75100956, z12=75100957):
    """ State 0 """
    ClearPlayerDamageInfo()
    """ State 2 """
    while True:
        assert t510020_x1(z8=z8, flag4=flag4, flag5=6000, flag6=6000, flag7=6000, flag8=6000)
        """ State 3 """
        ClearPlayerDamageInfo()
        call = t510020_x15(lot1=lot1, goods1=goods1, lot2=lot2, lot3=lot3, z1=z1, action1=action1, action2=action2,
                           action3=action3, z2=z2, action4=action4, z3=z3, action5=action5, action6=action6,
                           z4=z4, z5=z5, z6=z6, flag1=flag1, flag2=flag2, flag3=flag3, z9=z9, z10=z10,
                           z11=z11, z12=z12)
        def ExitPause():
            TurnCharacterToFaceEntity(69002, 10000, -1)
        if call.Done():
            pass
        elif IsPlayerDead() == 1:
            break
        elif GetDistanceToPlayer() > 12:
            """ State 6 """
            assert t510020_x8()
        elif HasPlayerBeenAttacked() == 1:
            """ State 4 """
            assert t510020_x2()
            """ State 1 """
            TurnCharacterToFaceEntity(69002, 10000, -1)
            ClearPlayerDamageInfo()
    """ State 5 """
    t510020_x7()

def t510020_x15(lot1=4280, goods1=379, lot2=4288, lot3=4287, z1=15000406, action1=15000417, action2=12000026,
                action3=13000046, z2=13000027, action4=13000037, z3=12000007, action5=13000017, action6=13000007,
                z4=99, z5=20, z6=27, flag1=75100951, flag2=75100952, flag3=6001, z9=0, z10=9000, z11=75100956,
                z12=75100957):
    """ State 0,1 """
    TurnCharacterToFaceEntity(69000, 10000, -1)
    SetTalkTime(1)
    assert GetCurrentStateElapsedTime() > 1
    """ State 14 """
    assert t510020_x9(z6=z6, lot2=lot2, lot3=lot3)
    """ State 4 """
    c1110()
    """ State 2 """
    while True:
        ClearTalkListData()
        """ State 3 """
        AddTalkListData(2, action1, -1)
        AddTalkListData(99, 15000005, -1)
        """ State 5 """
        ShowShopMessage(1)
        assert not (CheckSpecificPersonMenuIsOpen(1, 0) == 1 and not CheckSpecificPersonGenericDialogIsOpen(0))
        """ State 11 """
        if GetTalkListEntryResult() == 2:
            """ State 10,18 """
            assert (t510020_x16(goods1=goods1, z4=z4, z5=z5, z6=z6, z2=z2, action4=action4, z3=z3, action5=action5,
                    action6=action6, lot2=lot2, lot3=lot3, flag2=flag2, z11=z11, z12=z12))
        else:
            """ State 12,19 """
            return 0

def t510020_x16(goods1=379, z4=99, z5=20, z6=27, z2=13000027, action4=13000037, z3=12000007, action5=13000017,
                action6=13000007, lot2=4288, lot3=4287, flag2=75100952, z11=75100956, z12=75100957):
    """ State 0,1 """
    if ComparePlayerInventoryNumber(3, goods1, 2, 0, 0) == 1:
        """ State 2 """
        SetWorkValue(0, GetPlayerStatus(z6))
        """ State 15 """
        call = t510020_x6(goods1=goods1, z3=z3)
        if call.Get() == 0:
            """ State 3 """
            SetEventState(flag2, 1)
            assert GetCurrentStateElapsedTime() > 2
            """ State 4,6 """
            PlayerEquipmentQuantityChange(3, goods1, -1 * GetValueFromNumberSelectDialog())
            """ State 5 """
            ChangePlayerStats(z5, 0, GetValueFromNumberSelectDialog() * 1)
            """ State 7 """
            SetEventState(z11, 1)
            """ State 9 """
            if GetWorkValue(0) > 2:
                """ State 10 """
                SetEventState(z12, 1)
                """ State 20 """
                assert t510020_x3(lot2

=lot3)
                """ State 19 """
                Label('L0')
                assert t510020_x5(action5=action6)
            else:
                """ State 8 """
                if ComparePlayerStatus(z6, 2, GetWorkValue(0)):
                    """ State 11 """
                    if ComparePlayerStatus(z6, 3, 1) == 1:
                        """ State 16 """
                        assert t510020_x3(lot2=lot2)
                    else:
                        """ State 12 """
                        SetEventState(z12, 1)
                        """ State 18 """
                        assert t510020_x3(lot2=lot3)
                    """ State 17 """
                    assert t510020_x5(action5=action5)
                else:
                    Goto('L0')
            """ State 13 """
            assert not GetEventStatus(flag2)
        elif call.Get() == 1:
            """ State 14 """
            pass
    else:
        """ State 21 """
        assert t510020_x5(action5=action4)
    """ State 22 """
    return 0


def _get_ser_data(element, ref_state, fallback_ref_state="SGTE91") -> Dict[str, Union[str, float]]:

"""Provides utilities for creating and working with Databases in ESPEI
"""

import logging
from typing import Dict, Union
import sympy
from pycalphad import Database, variables as v
import espei.refdata
from espei.utils import extract_aliases

_log = logging.getLogger(__name__)

"""Return a dictionary of the stable element reference (SER) data.

    If no SER data is found, returns an empty dictionary.
    """
    ser_ref_state = ref_state + "SER"
    # Return an empty dict for backwards compatibility, since the SER data may not exist
    ser_dict = getattr(espei.refdata, ser_ref_state, {})
    fallback_ser_ref_state = fallback_ref_state + "SER"
    fallback_ser_dict = getattr(espei.refdata, fallback_ser_ref_state)
    el_ser_data = ser_dict.get(element)
    if el_ser_data is None and ref_state == fallback_ref_state:
        # No data found, no fallback alternative
        _log.warning("%s has no entry in the %s reference data. Fitting formation energies will not be possible.", element, ser_ref_state)
    elif el_ser_data is None:
        # No data found, try the fallback
        el_ser_data = fallback_ser_dict.get(element)
        if el_ser_data is None:
            # No data found in the fallback
            _log.warning("%s has no entry in the %s reference data nor in the %s fallback reference data. Fitting formation energies will not be possible.", element, ser_ref_state + "SER", fallback_ser_ref_state)
            return {}
        else:
            # Data found in the fallback
            _log.trace("%s has no entry in the %s reference data, but was available in the %s fallback reference data.", element, ser_ref_state + "SER", fallback_ser_ref_state)
    if el_ser_data is not None:
        return el_ser_data
    else:
        return {}

def initialize_database(phase_models, ref_state, dbf=None, fallback_ref_state="SGTE91"):
    """Return a Database boostraped with elements, species, phases and unary lattice stabilities.

    Parameters
    ----------
    phase_models : Dict[str, Any]
        Dictionary of components and phases to fit.
    ref_state : str
        String of the reference data to use, e.g. 'SGTE91' or 'SR2016'
    dbf : Optional[Database]
        Initial pycalphad Database that can have parameters that would not be fit by ESPEI
    fallback_ref_state : str
        String of the reference data to use for SER data, defaults to 'SGTE91'

    Returns
    -------
    Database
        A new pycalphad Database object, or a modified one if it was given.

    """
    if dbf is None:
        dbf = Database()
    lattice_stabilities = getattr(espei.refdata, ref_state)
    ser_stability = getattr(espei.refdata, ref_state + "Stable")
    aliases = extract_aliases(phase_models)
    phases = sorted({ph.upper() for ph in phase_models["phases"].keys()})
    elements = {el.upper() for el in phase_models["components"]}
    dbf.elements.update(elements)
    dbf.species.update({v.Species(el, {el: 1}, 0) for el in elements})

    # Add SER reference data for this element
    for element in dbf.elements:
        if element in dbf.refstates:
            continue  # Do not clobber user reference states
        el_ser_data = _get_ser_data(element, ref_state, fallback_ref_state=fallback_ref_state)
        # Try to look up the alias that we are using in this fitting
        el_ser_data["phase"] = aliases.get(el_ser_data["phase"], el_ser_data["phase"])
        # Don't warn if the element is a species with no atoms because per-atom
        # formation energies are not possible (e.g. VA (VACUUM) or /- (ELECTRON_GAS))
        if el_ser_data["phase"] not in phases and v.Species(element).number_of_atoms != 0:
            # We have the Gibbs energy expression that we need in the reference
            # data, but this phase is not a candidate in the phase models. The
            # phase won't be added to the database, so looking up the phases's
            # energy won't work.
            _log.warning(
                "The reference phase for %s, %s, is not in the supplied phase models "
                "and won't be added to the Database phases. Fitting formation "
                "energies will not be possible.", element, el_ser_data["phase"]
            )
        dbf.refstates[element] = el_ser_data

    # Add the phases
    for phase_name, phase_data in phase_models['phases'].items():
        if phase_name not in dbf.phases.keys():  # Do not clobber user phases
            # TODO: Need to support model hints for: magnetic, order-disorder, etc.
            site_ratios = phase_data['sublattice_site_ratios']
            subl_model = phase_data['sublattice_model']
            # Only generate the sublattice model for active components
            subl_model = [sorted(set(subl).intersection(dbf.elements)) for subl in subl_model]
            if all(len(subl) > 0 for subl in subl_model):
                dbf.add_phase(phase_name, dict(), site_ratios)
                dbf.add_phase_constituents(phase_name, subl_model)

    # Add the GHSER functions to the Database
    for element in dbf.elements:
        # Use setdefault here to not clobber user-provided functions
        if element == "VA":
            dbf.symbols.setdefault("GHSERVA", 0)
        else:
            # note that `c.upper()*2)[:2]` returns "AL" for "Al" and "BB" for "B"
            # Using this ensures that GHSER functions will be unique, e.g.
            # GHSERC would be an abbreviation for GHSERCA.
            sym_name = "GHSER" + (element.upper()*2)[:2]
            dbf.symbols.setdefault(sym_name, ser_stability[element])
    return dbf


"""Return a dictionary of the stable element reference (SER) data.

    If no SER data is found, returns an empty dictionary.
    """
    ser_ref_state = ref_state + "SER"
    # Return an empty dict for backwards compatibility, since the SER data may not exist

"""Provides utilities for creating and working with Databases in ESPEI
"""

import logging
from typing import Dict, Union
import sympy
from pycalphad import Database, variables as v
import espei.refdata
from espei.utils import extract_aliases

_log = logging.getLogger(__name__)

def _get_ser_data(element, ref_state, fallback_ref_state="SGTE91") -> Dict[str, Union[str, float]]:

    ser_dict = getattr(espei.refdata, ser_ref_state, {})
    fallback_ser_ref_state = fallback_ref_state + "SER"
    fallback_ser_dict = getattr(espei.refdata, fallback_ser_ref_state)
    el_ser_data = ser_dict.get(element)
    if el_ser_data is None and ref_state == fallback_ref_state:
        # No data found, no fallback alternative
        _log.warning("%s has no entry in the %s reference data. Fitting formation energies will not be possible.", element, ser_ref_state)
    elif el_ser_data is None:
        # No data found, try the fallback
        el_ser_data = fallback_ser_dict.get(element)
        if el_ser_data is None:
            # No data found in the fallback
            _log.warning("%s has no entry in the %s reference data nor in the %s fallback reference data. Fitting formation energies will not be possible.", element, ser_ref_state + "SER", fallback_ser_ref_state)
            return {}
        else:
            # Data found in the fallback
            _log.trace("%s has no entry in the %s reference data, but was available in the %s fallback reference data.", element, ser_ref_state + "SER", fallback_ser_ref_state)
    if el_ser_data is not None:
        return el_ser_data
    else:
        return {}

def initialize_database(phase_models, ref_state, dbf=None, fallback_ref_state="SGTE91"):
    """Return a Database boostraped with elements, species, phases and unary lattice stabilities.

    Parameters
    ----------
    phase_models : Dict[str, Any]
        Dictionary of components and phases to fit.
    ref_state : str
        String of the reference data to use, e.g. 'SGTE91' or 'SR2016'
    dbf : Optional[Database]
        Initial pycalphad Database that can have parameters that would not be fit by ESPEI
    fallback_ref_state : str
        String of the reference data to use for SER data, defaults to 'SGTE91'

    Returns
    -------
    Database
        A new pycalphad Database object, or a modified one if it was given.

    """
    if dbf is None:
        dbf = Database()
    lattice_stabilities = getattr(espei.refdata, ref_state)
    ser_stability = getattr(espei.refdata, ref_state + "Stable")
    aliases = extract_aliases(phase_models)
    phases = sorted({ph.upper() for ph in phase_models["phases"].keys()})
    elements = {el.upper() for el in phase_models["components"]}
    dbf.elements.update(elements)
    dbf.species.update({v.Species(el, {el: 1}, 0) for el in elements})

    # Add SER reference data for this element
    for element in dbf.elements:
        if element in dbf.refstates:
            continue  # Do not clobber user reference states
        el_ser_data = _get_ser_data(element, ref_state, fallback_ref_state=fallback_ref_state)
        # Try to look up the alias that we are using in this fitting
        el_ser_data["phase"] = aliases.get(el_ser_data["phase"], el_ser_data["phase"])
        # Don't warn if the element is a species with no atoms because per-atom
        # formation energies are not possible (e.g. VA (VACUUM) or /- (ELECTRON_GAS))
        if el_ser_data["phase"] not in phases and v.Species(element).number_of_atoms != 0:
            # We have the Gibbs energy expression that we need in the reference
            # data, but this phase is not a candidate in the phase models. The
            # phase won't be added to the database, so looking up the phases's
            # energy won't work.
            _log.warning(
                "The reference phase for %s, %s, is not in the supplied phase models "
                "and won't be added to the Database phases. Fitting formation "
                "energies will not be possible.", element, el_ser_data["phase"]
            )
        dbf.refstates[element] = el_ser_data

    # Add the phases
    for phase_name, phase_data in phase_models['phases'].items():
        if phase_name not in dbf.phases.keys():  # Do not clobber user phases
            # TODO: Need to support model hints for: magnetic, order-disorder, etc.
            site_ratios = phase_data['sublattice_site_ratios']
            subl_model = phase_data['sublattice_model']
            # Only generate the sublattice model for active components
            subl_model = [sorted(set(subl).intersection(dbf.elements)) for subl in subl_model]
            if all(len(subl) > 0 for subl in subl_model):
                dbf.add_phase(phase_name, dict(), site_ratios)
                dbf.add_phase_constituents(phase_name, subl_model)

    # Add the GHSER functions to the Database
    for element in dbf.elements:
        # Use setdefault here to not clobber user-provided functions
        if element == "VA":
            dbf.symbols.setdefault("GHSERVA", 0)
        else:
            # note that `c.upper()*2)[:2]` returns "AL" for "Al" and "BB" for "B"
            # Using this ensures that GHSER functions will be unique, e.g.
            # GHSERC would be an abbreviation for GHSERCA.
            sym_name = "GHSER" + (element.upper()*2)[:2]
            dbf.symbols.setdefault(sym_name, ser_stability[element])
    return dbf