<commit_before>"""Level.py: contains the Level class"""

from direct.directnotify import DirectNotifyGlobal
import string
import LevelConstants
from direct.showbase.PythonUtil import lineInfo, uniqueElements
import types

"""
Any data that can be edited by a level editor must be represented as
an attribute of an entity owned by the level, in order to keep the
level-editing interface simple and constant (there are at least three
places where the entire editing interface must be duplicated).

To support this, we have entities such as 'levelMgr' and 'zoneEntity' that
contain crucial level information, much of which is needed when setting
up the level object, and is needed before other entity types can be
effectively created. (If you try to create a distributed entity, but
you don't yet have the information for the zone that it's in, because
you haven't created the zone's ZoneEntity, you're hurting.)
"""

"""
ZONE TERMINOLOGY
zoneNum / zoneEntId: the number that a modeler chooses for a zone, and also
                     the entity ID of the ZoneEntity that represents a zone
zoneId : the network ID of a zone
"""

class Level:
    """Level: representation of a game level, keeps track of all of the
    entities and their interrelations, and creates and destroys entities"""
    notify = DirectNotifyGlobal.directNotify.newCategory('Level')

    def __init__(self):
        self.levelSpec = None
        self.initialized = 0

    def initializeLevel(self, levelId, levelSpec, scenarioIndex):
        """subclass should call this as soon as it has located
        its spec data. levelId should be a unique integer (a doId works
        just fine) that differentiates this level from all other levels
        that may exist concurrently."""
        self.levelId = levelId
        self.levelSpec = levelSpec
        self.scenarioIndex = scenarioIndex

        self.levelSpec.setScenario(self.scenarioIndex)
        if __dev__:
            self.levelSpec.setLevel(self)

        # create some handy tables

        # entranceId to entrance entity
        self.entranceId2entity = {}

        # dict of entId -> list of callbacks to be called upon creation
        self.entId2createCallbacks = {}

        # this list contains the entIds of entities that we have actually
        # created, in order of creation
        self.createdEntIds = []

        # get an entity creator object
        self.entityCreator = self.createEntityCreator()

        # entity type -> list of entIds
        self.entType2ids = self.levelSpec.getEntType2ids(
            self.levelSpec.getAllEntIds())
        # create empty list for any entity types that are not represented
        # in the spec
        for entType in self.entityCreator.getEntityTypes():
            self.entType2ids.setdefault(entType, [])

        # create all the entities
        # TODO: maybe we should leave this to a subclass or the level user
        self.createAllEntities(priorityTypes=['levelMgr','zone','propSpinner'])

        # check on the singleton entities
        # we make our own references to them rather than expect them to
        # create the references so that the editor can create dummy
        # do-nothing entities

        # there should be one and only one levelMgr
        assert len(self.entType2ids['levelMgr']) == 1
        assert self.entType2ids['levelMgr'][0] == LevelConstants.LevelMgrEntId
        self.levelMgrEntity = self.getEntity(LevelConstants.LevelMgrEntId)

        # there should be one and only one editMgr
        assert len(self.entType2ids['editMgr']) == 1
        assert self.entType2ids['editMgr'][0] == LevelConstants.EditMgrEntId
        if __debug__:
            self.editMgrEntity = self.getEntity(LevelConstants.EditMgrEntId)

        # there should be one and only one UberZone
        assert LevelConstants.UberZoneEntId in self.entType2ids['zone']
        self.uberZoneEntity = self.getEntity(LevelConstants.UberZoneEntId)

        self.initialized = 1

    def isInitialized(self):
        return self.initialized

    def destroyLevel(self):
        self.destroyAllEntities()
        self.initialized = 0
        del self.createdEntIds
        if hasattr(self, 'entities'):
            del self.entities
        if hasattr(self, 'levelSpec'):
            del self.levelSpec

    def createEntityCreator(self):
        Level.notify.error(
            'concrete Level class must override %s' % lineInfo()[2])

    def createAllEntities(self, priorityTypes=[]):
        """creates all entities in the spec. priorityTypes is an
        optional ordered list of entity types to create first."""
        # this will be filled in as the entities are created and report in
        # this includes distributed objects on the client
        self.entities = {}

        # get list of all entity types we need to create
        entTypes = self.entityCreator.getEntityTypes()

        self.onLevelPreCreate()

        # first create the types in the priority list
        for type in priorityTypes:
            assert type in entTypes
            self.createAllEntitiesOfType(type)
            entTypes.remove(type)<commit_msg>factoryEditor->levelEditor, removed global event names in editor<commit_after>"""Level.py: contains the Level class"""

from direct.directnotify import DirectNotifyGlobal
import string
import LevelConstants
from direct.showbase.PythonUtil import lineInfo, uniqueElements
import types

"""
Any data that can be edited by a level editor must be represented as
an attribute of an entity owned by the level, in order to keep the
level-editing interface simple and constant (there are at least three
places where the entire editing interface must be duplicated).

To support this, we have entities such as 'levelMgr' and 'zoneEntity' that
contain crucial level information, much of which is needed when setting
up the level object, and is needed before other entity types can be
effectively created. (If you try to create a distributed entity, but
you don't yet have the information for the zone that it's in, because
you haven't created the zone's ZoneEntity, you're hurting.)
"""

"""
ZONE TERMINOLOGY
zoneNum / zoneEntId: the number that a modeler chooses for a zone, and also
                     the entity ID of the ZoneEntity that represents a zone
zoneId : the network ID of a zone
"""

class Level:
    """Level: representation of a game level, keeps track of all of the
    entities and their interrelations, and creates and destroys entities"""
    notify = DirectNotifyGlobal.directNotify.newCategory('Level')

    def __init__(self):
        self.levelSpec = None
        self.initialized = 0

    def initializeLevel(self, levelId, levelSpec, scenarioIndex):
        """subclass should call this as soon as it has located
        its spec data. levelId should be a unique integer (a doId works
        just fine) that differentiates this level from all other levels
        that may exist concurrently."""
        self.levelId = levelId
        self.levelSpec = levelSpec
        self.scenarioIndex = scenarioIndex

        self.levelSpec.setScenario(self.scenarioIndex)
        if __dev__:
            self.levelSpec.setLevel(self)

        # create some handy tables

        # entranceId to entrance entity
        self.entranceId2entity = {}

        # dict of entId -> list of callbacks to be called upon creation
        self.entId2createCallbacks = {}

        # this list contains the entIds of entities that we have actually
        # created, in order of creation
        self.createdEntIds = []

        # get an entity creator object
        self.entityCreator = self.createEntityCreator()

        # entity type -> list of entIds
        self.entType2ids = self.levelSpec.getEntType2ids(
            self.levelSpec.getAllEntIds())
        # create empty list for any entity types that are not represented
        # in the spec
        for entType in self.entityCreator.getEntityTypes():
            self.entType2ids.setdefault(entType, [])

        # create all the entities
        # TODO: maybe we should leave this to a subclass or the level user
        self.createAllEntities(priorityTypes=['levelMgr','zone','propSpinner'])

        # check on the singleton entities
        # we make our own references to them rather than expect them to
        # create the references so that the editor can create dummy
        # do-nothing entities

        # there should be one and only one levelMgr
        assert len(self.entType2ids['levelMgr']) == 1
        assert self.entType2ids['levelMgr'][0] == LevelConstants.LevelMgrEntId
        self.levelMgrEntity = self.getEntity(LevelConstants.LevelMgrEntId)

        # there should be one and only one editMgr
        assert len(self.entType2ids['editMgr']) == 1
        assert self.entType2ids['editMgr'][0] == LevelConstants.EditMgrEntId
        if __debug__:
            self.editMgrEntity = self.getEntity(LevelConstants.EditMgrEntId)

        # there should be one and only one UberZone
        assert LevelConstants.UberZoneEntId in self.entType2ids['zone']
        self.uberZoneEntity = self.getEntity(LevelConstants.UberZoneEntId)

        self.initialized = 1

    def isInitialized(self):
        return self.initialized

    def getLevelId(self):
        return self.levelId

    def destroyLevel(self):
        self.destroyAllEntities()
        self.initialized = 0
        del self.createdEntIds
        if hasattr(self, 'entities'):
            del self.entities
        if hasattr(self, 'levelSpec'):
            del self.levelSpec

    def createEntityCreator(self):
        Level.notify.error(
            'concrete Level class must override %s' % lineInfo()[2])

    def createAllEntities(self, priorityTypes=[]):
        """creates all entities in the spec. priorityTypes is an
        optional ordered list of entity types to create first."""
        # this will be filled in as the entities are created and report in
        # this includes distributed objects on the client
        self.entities = {}

        # get list of all entity types we need to create
        entTypes = self.entityCreator.getEntityTypes()

        self.onLevelPreCreate()

        # first create the types in the priority list
        for type in priorityTypes:
            assert type in entTypes
            self.createAllEntitiesOfType(type)
            entTypes.remove(type)
{'fraction_non_alphanumeric': 0.06502890173410404, 'fraction_numerical': 0.003468208092485549, 'mean_word_length': 2.7970007315288954, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7216044', 'n_tokens_mistral': 2653, 'n_tokens_neox': 2610, 'n_words': 1053}