CartesianGridBase.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Utility functions that are useful to both AI and client CartesianGrid code
  2. class CartesianGridBase:
  3. def isValidZone(self, zoneId):
  4. if self.style == "Cartesian":
  5. if ((zoneId < self.startingZone) or
  6. (zoneId > self.startingZone + self.gridSize * self.gridSize - 1)):
  7. return 0
  8. return 1
  9. elif self.style == "CartesianStated":
  10. return 1
  11. else:
  12. return 0
  13. def getZoneFromXYZ(self, pos):
  14. # NOTE: pos should be relative to our own grid origin
  15. # Convert a 3d position to a zone
  16. dx = self.cellWidth * self.gridSize * .5
  17. x = pos[0] + dx
  18. y = pos[1] + dx
  19. col = x // self.cellWidth
  20. row = y // self.cellWidth
  21. # Compute which zone we are in
  22. zoneId = int(self.startingZone + ((row * self.gridSize) + col))
  23. return zoneId
  24. def getGridSizeFromSphereRadius(self, sphereRadius, cellWidth, gridRadius):
  25. # NOTE: This ensures that the grid is at least a "gridRadius" number of cells
  26. # larger than the trigger sphere that loads the grid. This gives us some
  27. # room to start setting interest to the grid before we expect to see any objects
  28. # on it.
  29. sphereRadius = max(sphereRadius, gridRadius*cellWidth)
  30. return 2 * (sphereRadius // cellWidth)
  31. def getZoneCellOrigin(self, zoneId):
  32. dx = self.cellWidth * self.gridSize * .5
  33. zone = zoneId - self.startingZone
  34. row = zone // self.gridSize
  35. col = zone % self.gridSize
  36. x = col * self.cellWidth - dx
  37. y = row * self.cellWidth - dx
  38. return (x,y,0)