Re: Pathfinding working... sort of...

Certainly it is confusing.
What I mean is converting as many Python functions and types into C types as possible, and when you compile, Cython generates pure C code that doesn't interact with the much slower CPython API during the actual operation.
My Pathfinder has a pathfinder class like this.
# distutils: language=c++ // I'm using C++ so I can use a vector, which is basically a list who's members must all have the same type. Much faster than a Python list.


from libcpp.vector cimport vector

cdef struct location: // A C struct is sort of like a Python class. They can't have methods, and you can only store C types. This struct, location, just has 2 properties, x and y, which represent the location of a single tile on a map.
unsigned short x, y // Unsigned short, like in BGT, is 2 bytes, and can be from 0 to 65535


cdef struct square: // The pathfinder allocates and stores one of these for every square on the map.
char terrain // A 1 byte number, -128 to 127. A value less than 0 means this square is impassable. 0 means there is no cost, and every value above 0 increases the cost to walk there.
location parent, loc // Parent is the square that the pathfinder was previously on, loc is the coordinates of this tile.
unsigned short factor // Internal number
bint closed // bint is bool, true means this square is closed and should not be looked at anymore.



cdef class pathfinder: # Cdef declares a C class, which can store C datatypes and can take up less memory.
  cdef: # Declare all the properties stored in each pathfinder in a cdef block
  vector[vector[square]] map # This is a list, each item of which is a list of square structs representinng a square on the map. So while pathfinding, you can get a tile by doing self.map[x][y]
  readonly unsigned short max_x, max_y # Readonly means that you can see the values from outside Cython code, but you can't change them.

def __cinit__(self, unsigned short max_x, unsigned short max_y): # This function prepares the internal map, the max_x and max_y parameters are the size of the map.
  cdef vector[square] k
  cdef coord i, j
  cdef square sq
  self.map.reserve(max_x)
  for i in range(max_x):
   k.clear() // Make sure it's empty.
   k.reserve(max_y) // Allocate exactly enough memoy in this list for max_y squares.
   for j in range(max_y):
    sq=square(parent=location(0, 0), closed=0, terrain=0, loc=location(i, j), factor=0) // Create a new square
    k.push_back(sq) // And add it to the vector of squares that we're preparing...
   self.map.push_back(k) # and Now we have a vector of squares that we push onto the map.
  self.max_x=max_x
  self.max_y=max_y


def __dealloc__(self): # __dealloc__ is like __del__, but you should not touch Python objects stored in self. Here, we deallocate the map and free a lot of memory.
  cdef vector[square] i
  for i in self.map:
   i.clear()
  self.map.clear()
  # We iterate through self.map, which of course is a list of vectors, then we clear each vector.
Then, we clear self.map its self.




This should give you an idea of what is required.
You can try reading the Cython tutorials and also the user guide at cython.org.
HTH

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : keithwipf1 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : keithwipf1 via Audiogames-reflector

Reply via email to