feat(entity): implement path_to() method for entity pathfinding
- Add path_to(target_x, target_y) method to UIEntity class - Uses existing Dijkstra pathfinding implementation from UIGrid - Returns list of (x, y) coordinate tuples for complete path - Supports both positional and keyword argument formats - Proper error handling for out-of-bounds and no-grid scenarios - Comprehensive test suite covering normal and edge cases Part of TCOD integration sprint - gives entities immediate pathfinding capabilities. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
1a3e308c77
commit
7ee0a08662
11 changed files with 1163 additions and 30 deletions
|
|
@ -377,10 +377,68 @@ PyObject* UIEntity::die(PyUIEntityObject* self, PyObject* Py_UNUSED(ignored))
|
|||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyObject* UIEntity::path_to(PyUIEntityObject* self, PyObject* args, PyObject* kwds) {
|
||||
static const char* keywords[] = {"target_x", "target_y", "x", "y", nullptr};
|
||||
int target_x = -1, target_y = -1;
|
||||
|
||||
// Parse arguments - support both target_x/target_y and x/y parameter names
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "ii", const_cast<char**>(keywords),
|
||||
&target_x, &target_y)) {
|
||||
PyErr_Clear();
|
||||
// Try alternative parameter names
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iiii", const_cast<char**>(keywords),
|
||||
&target_x, &target_y, &target_x, &target_y)) {
|
||||
PyErr_SetString(PyExc_TypeError, "path_to() requires target_x and target_y integer arguments");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if entity has a grid
|
||||
if (!self->data || !self->data->grid) {
|
||||
PyErr_SetString(PyExc_ValueError, "Entity must be associated with a grid to compute paths");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Get current position
|
||||
int current_x = static_cast<int>(self->data->position.x);
|
||||
int current_y = static_cast<int>(self->data->position.y);
|
||||
|
||||
// Validate target position
|
||||
auto grid = self->data->grid;
|
||||
if (target_x < 0 || target_x >= grid->grid_x || target_y < 0 || target_y >= grid->grid_y) {
|
||||
PyErr_Format(PyExc_ValueError, "Target position (%d, %d) is out of grid bounds (0-%d, 0-%d)",
|
||||
target_x, target_y, grid->grid_x - 1, grid->grid_y - 1);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Use the grid's Dijkstra implementation
|
||||
grid->computeDijkstra(current_x, current_y);
|
||||
auto path = grid->getDijkstraPath(target_x, target_y);
|
||||
|
||||
// Convert path to Python list of tuples
|
||||
PyObject* path_list = PyList_New(path.size());
|
||||
if (!path_list) return PyErr_NoMemory();
|
||||
|
||||
for (size_t i = 0; i < path.size(); ++i) {
|
||||
PyObject* coord_tuple = PyTuple_New(2);
|
||||
if (!coord_tuple) {
|
||||
Py_DECREF(path_list);
|
||||
return PyErr_NoMemory();
|
||||
}
|
||||
|
||||
PyTuple_SetItem(coord_tuple, 0, PyLong_FromLong(path[i].first));
|
||||
PyTuple_SetItem(coord_tuple, 1, PyLong_FromLong(path[i].second));
|
||||
PyList_SetItem(path_list, i, coord_tuple);
|
||||
}
|
||||
|
||||
return path_list;
|
||||
}
|
||||
|
||||
PyMethodDef UIEntity::methods[] = {
|
||||
{"at", (PyCFunction)UIEntity::at, METH_O},
|
||||
{"index", (PyCFunction)UIEntity::index, METH_NOARGS, "Return the index of this entity in its grid's entity collection"},
|
||||
{"die", (PyCFunction)UIEntity::die, METH_NOARGS, "Remove this entity from its grid"},
|
||||
{"path_to", (PyCFunction)UIEntity::path_to, METH_VARARGS | METH_KEYWORDS, "Find path from entity to target position using Dijkstra pathfinding"},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
|
|
@ -393,6 +451,7 @@ PyMethodDef UIEntity_all_methods[] = {
|
|||
{"at", (PyCFunction)UIEntity::at, METH_O},
|
||||
{"index", (PyCFunction)UIEntity::index, METH_NOARGS, "Return the index of this entity in its grid's entity collection"},
|
||||
{"die", (PyCFunction)UIEntity::die, METH_NOARGS, "Remove this entity from its grid"},
|
||||
{"path_to", (PyCFunction)UIEntity::path_to, METH_VARARGS | METH_KEYWORDS, "Find path from entity to target position using Dijkstra pathfinding"},
|
||||
{NULL} // Sentinel
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue