def Tectonics(self, startY, startX, level, lenY, lenX, falloff, vertY,
                  vertX, m, worldY, worldX, terraKey):
        x, y = divmod(lenX, self.size[0]), divmod(lenY, self.size[1])

        indexes = [
            str((h, w)) for h in range(y[0] + 1) for w in range(x[0] + 1)
        ]
        startY = min(startY, y[0])
        startX = min(startX, x[0])
        radius = max(lenX, lenY)

        if not os.path.exists(gamepath + "\\dsm\\cache\\landmass\\" +
                              terraKey):
            os.mkdir(gamepath + "\\dsm\\cache\\landmass\\" + terraKey)

        file = gamepath + "\\dsm\\cache\\landmass\\%s\\exclude.txt" % (
            terraKey)
        if os.path.exists(file):
            exclude = [line.rstrip('\n') for line in open(file)]
            exclude = str(exclude)

        else:
            open(file, "w+")
            exclude = "[]"

        for chunk in chunkIt(indexes, 12):
            l = [
                chunk, startY, startX, y[0], x[0], y[1], x[1], level, radius,
                lenY, lenX, falloff, vertY, vertX, worldY, worldX, m, terraKey,
                exclude
            ]

            core.queues["terraform"].append(l)
    def onHeightModEnd(self):

        if self.ripMesh:
            m = self.meshes[0]
            cellDict = core.world["cellDict"]
            wx = core.wOrigin[0] + self.cell[0]
            wy = core.wOrigin[1] + self.cell[1]

            cellIndex = (int(self.worldPosition.x), int(self.worldPosition.y))
            if not cellDict[(wx, wy)]["natr"]:
                d = dict(defStatic)
                d["worldKey"] = (wx, wy)

                spawns = np.full(core.grid["verts"].shape, False)
                r, rf, rs = self.genArrs

                spawns = colorMod_ufunc(core.grid["verts"], r, rf, rs, m,
                                        str(cellIndex), spawns, d)

            else:
                spawns = np.asarray(list(cellDict[(wx, wy)]["natr"].values()))

            if len(spawns[spawns != False]):
                spawns = chunkIt(spawns[spawns != False], 24)
                for spawn in spawns:
                    core.queues["addObject_mainScene"].append(spawn)
    def endObject(self):
        if self.children:
            chunks = chunkIt(self.children, 18)
            for chunk in chunks:
                core.queues["endObject_mainScene"].append(chunk)

            core.queues["endObject_mainScene"].append(self)

        elif self not in core.queues["endObject_mainScene"]:
            core.reserveID(self)
            KX_GameObject.endObject(self)
def onTreeAdd(cont):
    own = cont.owner

    if own.parent and "init" not in own:

        cell = own.parent.cell
        cellDict = core.world["cellDict"]
        cellIndex = (core.wOrigin[0] + cell[0], core.wOrigin[1] + cell[1])

        if cellIndex in cellDict:
            own["init"] = True
            cancel = False
            try:
                cancel = cellDict[cellIndex]["natr"][tuple(own.localPosition)]

            except:
                vecTo = own.worldPosition.copy() + Vector([0, 0, 4])
                vecFrom = own.worldPosition.copy() - Vector([0, 0, -1])
                ray = own.rayCast(vecTo,
                                  vecFrom,
                                  prop="natr",
                                  xray=True,
                                  mask=1)
                if ray[0]:
                    own.endObject()
                    cancel = True

                if not cancel:
                    dd = {
                        "generic": True,
                        "worldKey": own.worldKey,
                        "team": own.team,
                        "kls": "statics"
                    }

                    own_d = ({
                        "obj": own.name,
                        "pos": own.localPosition.copy(),
                        "rot": own.localOrientation.copy(),
                        "scale": own.localScale.copy(),
                        "d": dd,
                        "kls": "statics"
                    })

                    cellDict[cellIndex]["natr"][tuple(
                        own.localPosition)] = own_d

                    if not hasattr(own, "noReproduction"):
                        rr, rf, rs = cellDict[cellIndex]["init"]
                        spawns = np.full((12 * 12), False, dtype=object)
                        q = -1 if "grass" in own else -2
                        i = 0

                        for x in range(q, -q + 1, 2):
                            if x == 0: x += 1
                            for y in range(q, -q + 1, 2):
                                if y == 0: y += 1
                                r = rr[y, x]
                                f = rs[y, x]
                                dist = f * r

                                vecTo = (own.worldPosition.copy() +
                                         Vector([x * dist, y * dist, -1]))
                                vecFrom = (own.worldPosition.copy() +
                                           Vector([x * dist, y * dist, 5]))

                                ray = own.rayCast(vecTo,
                                                  vecFrom,
                                                  prop="ground",
                                                  xray=True,
                                                  mask=1)

                                if ray[1]:
                                    pos = ray[1] - own.parent.worldPosition

                                    rot = own.worldOrientation.copy()
                                    own.applyRotation([0, 0, x * dist])
                                    scale = own.worldScale.copy() * 0.9
                                    d = {
                                        "cell": cell,
                                        "worldKey": own.worldKey,
                                        "generic": True,
                                        "team": 1,
                                        "noReproduction": True,
                                        "kls": "statics"
                                    }

                                    spawns[i] = ({
                                        "obj": own.name,
                                        "pos": pos,
                                        "rot": rot,
                                        "scale": scale,
                                        "d": d,
                                        "kls": "statics"
                                    })

                                    i += 1

                        if len(spawns[spawns != False]):
                            spawns = chunkIt(spawns[spawns != False], 24)
                            for spawn in spawns:
                                core.queues["addObject_mainScene"].append(
                                    spawn)

        own.state = 2
    def __init__(self, old_owner, pos, **kwargs):
        for key, value in kwargs.items():
            self.__dict__[key] = value

        if core.terra["reserved"]:
            self.id = core.terra["reserved"].pop(0)
            core.registerReserved(self)
        else:
            self.id = core.genID(self, "terra")

        cellDict = core.world["cellDict"]
        core.grid["cells"][(self.cell[0], self.cell[1])] = self.id

        cellIndex = (core.wOrigin[0] + self.cell[0],
                     core.wOrigin[1] + self.cell[1])
        self.regions = (core.world["cellGen"][cellIndex]
                        if cellIndex in core.world["cellGen"] else [])

        if cellIndex not in cellDict:
            cellDict[cellIndex] = {
                "heightMap": [],
                "objects": [],
                "natr": {},
                "init": False
            }

        for key, value in cellDict[cellIndex].items():
            self.__dict__[key] = value

        if self.objects:
            for spawn in chunkIt(self.objects, 12):

                if not cellDict[cellIndex]["init"]:
                    for obj in spawn:
                        obj["d"]["worldKey"] = cellIndex

                core.queues["addObject_mainScene"].append(spawn)

        cellDict[cellIndex]["objects"] = []

        if core.genVerts:
            self.gridSetup()
            core.genVerts = False

        if not cellDict[cellIndex]["init"]:
            prng = self.getRandomState(cellIndex)
            r = prng.rand(self.size[0], self.size[1])
            rf = prng.randint(1, 275, r.shape)
            rs = prng.randint(9, 21, r.shape)

            cellDict[cellIndex]["init"] = [r, rf, rs]

        self.genArrs = cellDict[cellIndex]["init"]
        r = self.genArrs[0]

        self.worldPosition.xy = (self.cell[0] * 200, self.cell[1] * 200)

        wx = core.wOrigin[0] + self.cell[0]
        wy = core.wOrigin[1] + self.cell[1]
        ripMesh = "%s_%dx%d" % (core.worldName, wx, wy)
        self.ripMesh = ripMesh if ripMesh in self.scene.objectsInactive else ""
        core.grid["meshes"][self.id] = self.meshes[0]

        if not self.ripMesh:
            m = self.meshes[0]
            self.stdSetup(m, r, cellIndex)

        else:
            self.onHeightModEnd()