<commit_before>
from pandac.PandaModules import *

from direct.distributed import DistributedNode
from direct.task import Task
from direct.interval.IntervalGlobal import *
from direct.gui import DirectGuiGlobals

if __debug__:
    # For grid drawing
    from direct.directtools.DirectGeometry import *
    from direct.showbase.PythonUtil import randFloat

import CartesianGridBase


class DistributedCartesianGrid(DistributedNode.DistributedNode,
                               CartesianGridBase.CartesianGridBase):

    notify = directNotify.newCategory("DistributedCartesianGrid")

    VisualizeGrid = config.GetBool("visualize-cartesian-grid", 0)

    RuleSeparator = ":"

    def __init__(self, cr):
        DistributedNode.DistributedNode.__init__(self, cr)
        # Let the derived classes instantiate the NodePath
        self.visAvatar = None
        self.visContext = None
        # Do we have grid lines visualized?
        if __debug__:
            self.haveGridLines = 0

    def generate(self):
        DistributedNode.DistributedNode.generate(self)
        
    def disable(self):
        DistributedNode.DistributedNode.disable(self)
        self.stopProcessVisibility()
        
    def delete(self):
        DistributedNode.DistributedNode.delete(self)
        # TODO: when teleporting off an island...
        taskMgr.remove(self.taskName("processVisibility"))

    def isGridParent(self):
        # If this distributed object is a DistributedGrid return 1.  0 by default
        return 1
        
    def setParentingRules(self, style, rule):
        assert self.notify.debug("setParentingRules: style: %s, rule: %s" % (style, rule))
        rules = rule.split(self.RuleSeparator)
        assert(len(rules) == 3)
        self.style = style
        self.startingZone = int(rules[0])
        self.gridSize = int(rules[1])
        self.viewingRadius = int(rules[2])

        # Store the center of the grid
        cx = self.cellWidth * self.gridSize/2.0
        self.centerPos = Vec3(cx, cx, 0)

        if __debug__:
            if self.VisualizeGrid:
                self.visualizeGrid()

    def getCenterPos(self):
        return self.centerPos

    def startProcessVisibility(self, avatar):
        self.stopProcessVisibility()
        self.visAvatar = avatar
        self.visZone = None
        self.visContext = self.cr.addInterest(self.getDoId(), 0, self.uniqueName("visibility"))
        taskMgr.add(self.processVisibility, self.taskName("processVisibility"))

    def stopProcessVisibility(self):
        taskMgr.remove(self.taskName("processVisibility"))
        if self.visContext is not None:
            self.cr.removeInterest(self.visContext)
            self.visContext = None
        self.visAvatar = None
        self.visZone = None

    def processVisibility(self, task):
        pos = self.visAvatar.getPos(self)
        # Check to make sure our x and y are positive
        dx = self.cellWidth * self.gridSize * .5
        x = pos[0] + dx
        y = pos[1] + dx
        col = x // self.cellWidth
        row = y // self.cellWidth
        assert self.notify.debug("processVisibility: %s: avatar pos: %s %s" % (self.doId, x,y))
        if (row < 0) or (col < 0) or (row > self.gridSize) or (col > self.gridSize):
            assert self.notify.debug("processVisibility: %s: not on the grid" % (self.doId))
            # If we are viewingRadius away from this entire grid,
            # remove interest in any current visZone we may have
            if self.visContext:
                self.cr.removeInterest(self.visContext)
                self.visZone = None
                self.visContext = None
            return Task.cont
        # Compute which zone we are in
        zoneId = int(self.startingZone + ((row * self.gridSize) + col))
        assert self.notify.debug("processVisibility: %s: row: %s col: %s zoneId: %s" %
                                 (self.doId, row, col, zoneId))
        if (zoneId == self.visZone):
            assert self.notify.debug("processVisibility: %s: interest did not change" % (self.doId))
            return Task.cont
        else:
            assert self.notify.debug("processVisibility: %s: new interest" % (self.doId))
            self.visZone = zoneId
            if not self.visContext:
                self.visContext = self.cr.addInterest(self.getDoId(), self.visZone,
                                                      self.uniqueName("visibility"))
            else:
                assert self.notify.debug("processVisibility: %s: altering interest to zoneId: %s" %
                                         (self.doId, zoneId))
                self.cr.alterInterest(self.visContext, self.getDoId(), self.visZone)
                # If the visAvatar is parented to this grid, also do a setLocation
                parentId, oldZoneId = self.visAvatar.getLocation()
                assert self.notify.debug("processVisibility: %s: parentId: %s oldZoneId: %s" %
                                         (self.doId, parentId, oldZoneId))
                if parentId == self.doId:
                    assert self.notify.debug("processVisibility: %s: changing location" % (self.doId))
                    self.handleAvatarZoneChange(self.visAvatar, zoneId)
            return Task.cont

    # Take an avatar (or other object) from somewhere in the world and
    # wrtReparent him to the grid.
    def addObjectToGrid(self, av):
        # Get our pos relative to the island grid
        pos = av.getPos(self)
        # Figure out what zone in that island grid
        zoneId = self.getZoneFromXYZ(pos)
        # Do the wrtReparenting to the grid node
        self.handleAvatarZoneChange(av, zoneId)

    def removeObjectFromGrid(self, av):
        # TODO: WHAT LOCATION SHOULD WE SET THIS TO?
        #av.reparentTo(hidden)
        if (av.getParent().compareTo(self) == 0):
            # only detach if object is directly parented
            av.detachNode()
        #av.b_setLocation(0,0)

        
    def handleAvatarZoneChange(self, av, zoneId):<commit_msg>changed getLocation to parentId and zoneId<commit_after>
from pandac.PandaModules import *

from direct.distributed import DistributedNode
from direct.task import Task
from direct.interval.IntervalGlobal import *
from direct.gui import DirectGuiGlobals

if __debug__:
    # For grid drawing
    from direct.directtools.DirectGeometry import *
    from direct.showbase.PythonUtil import randFloat

import CartesianGridBase


class DistributedCartesianGrid(DistributedNode.DistributedNode,
                               CartesianGridBase.CartesianGridBase):

    notify = directNotify.newCategory("DistributedCartesianGrid")

    VisualizeGrid = config.GetBool("visualize-cartesian-grid", 0)

    RuleSeparator = ":"

    def __init__(self, cr):
        DistributedNode.DistributedNode.__init__(self, cr)
        # Let the derived classes instantiate the NodePath
        self.visAvatar = None
        self.visContext = None
        # Do we have grid lines visualized?
        if __debug__:
            self.haveGridLines = 0

    def generate(self):
        DistributedNode.DistributedNode.generate(self)
        
    def disable(self):
        DistributedNode.DistributedNode.disable(self)
        self.stopProcessVisibility()
        
    def delete(self):
        DistributedNode.DistributedNode.delete(self)
        # TODO: when teleporting off an island...
        taskMgr.remove(self.taskName("processVisibility"))

    def isGridParent(self):
        # If this distributed object is a DistributedGrid return 1.  0 by default
        return 1
        
    def setParentingRules(self, style, rule):
        assert self.notify.debug("setParentingRules: style: %s, rule: %s" % (style, rule))
        rules = rule.split(self.RuleSeparator)
        assert(len(rules) == 3)
        self.style = style
        self.startingZone = int(rules[0])
        self.gridSize = int(rules[1])
        self.viewingRadius = int(rules[2])

        # Store the center of the grid
        cx = self.cellWidth * self.gridSize/2.0
        self.centerPos = Vec3(cx, cx, 0)

        if __debug__:
            if self.VisualizeGrid:
                self.visualizeGrid()

    def getCenterPos(self):
        return self.centerPos

    def startProcessVisibility(self, avatar):
        self.stopProcessVisibility()
        self.visAvatar = avatar
        self.visZone = None
        self.visContext = self.cr.addInterest(self.getDoId(), 0, self.uniqueName("visibility"))
        taskMgr.add(self.processVisibility, self.taskName("processVisibility"))

    def stopProcessVisibility(self):
        taskMgr.remove(self.taskName("processVisibility"))
        if self.visContext is not None:
            self.cr.removeInterest(self.visContext)
            self.visContext = None
        self.visAvatar = None
        self.visZone = None

    def processVisibility(self, task):
        pos = self.visAvatar.getPos(self)
        # Check to make sure our x and y are positive
        dx = self.cellWidth * self.gridSize * .5
        x = pos[0] + dx
        y = pos[1] + dx
        col = x // self.cellWidth
        row = y // self.cellWidth
        assert self.notify.debug("processVisibility: %s: avatar pos: %s %s" % (self.doId, x,y))
        if (row < 0) or (col < 0) or (row > self.gridSize) or (col > self.gridSize):
            assert self.notify.debug("processVisibility: %s: not on the grid" % (self.doId))
            # If we are viewingRadius away from this entire grid,
            # remove interest in any current visZone we may have
            if self.visContext:
                self.cr.removeInterest(self.visContext)
                self.visZone = None
                self.visContext = None
            return Task.cont
        # Compute which zone we are in
        zoneId = int(self.startingZone + ((row * self.gridSize) + col))
        assert self.notify.debug("processVisibility: %s: row: %s col: %s zoneId: %s" %
                                 (self.doId, row, col, zoneId))
        if (zoneId == self.visZone):
            assert self.notify.debug("processVisibility: %s: interest did not change" % (self.doId))
            return Task.cont
        else:
            assert self.notify.debug("processVisibility: %s: new interest" % (self.doId))
            self.visZone = zoneId
            if not self.visContext:
                self.visContext = self.cr.addInterest(self.getDoId(), self.visZone,
                                                      self.uniqueName("visibility"))
            else:
                assert self.notify.debug("processVisibility: %s: altering interest to zoneId: %s" %
                                         (self.doId, zoneId))
                self.cr.alterInterest(self.visContext, self.getDoId(), self.visZone)
                # If the visAvatar is parented to this grid, also do a setLocation
                parentId = self.visAvatar.parentId
                oldZoneId = self.visAvatar.zoneId
                assert self.notify.debug(
                    "processVisibility: %s: parentId: %s oldZoneId: %s" %
                    (self.doId, parentId, oldZoneId))
                if parentId == self.doId:
                    assert self.notify.debug("processVisibility: %s: changing location" % (self.doId))
                    self.handleAvatarZoneChange(self.visAvatar, zoneId)
            return Task.cont

    # Take an avatar (or other object) from somewhere in the world and
    # wrtReparent him to the grid.
    def addObjectToGrid(self, av):
        # Get our pos relative to the island grid
        pos = av.getPos(self)
        # Figure out what zone in that island grid
        zoneId = self.getZoneFromXYZ(pos)
        # Do the wrtReparenting to the grid node
        self.handleAvatarZoneChange(av, zoneId)

    def removeObjectFromGrid(self, av):
        # TODO: WHAT LOCATION SHOULD WE SET THIS TO?
        #av.reparentTo(hidden)
        if (av.getParent().compareTo(self) == 0):
            # only detach if object is directly parented
            av.detachNode()
        #av.b_setLocation(0,0)

        
    def handleAvatarZoneChange(self, av, zoneId):
{'fraction_non_alphanumeric': 0.09235328010535847, 'fraction_numerical': 0.0036216972590336653, 'mean_word_length': 2.1476683937823835, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6954354', 'n_tokens_mistral': 3433, 'n_tokens_neox': 3301, 'n_words': 896}