liblashgame

Pathfinder and path decision making library for 2D tile game
git clone git://holbrook.no/liblashgame.git
Info | Log | Files | Refs

lash_game_standard_wrapper.c (6712B)


      1 #include "Python.h"
      2 #include "liblashgame/lash_game_standard.h"
      3 #include <stdio.h>
      4 
      5 /**
      6  * \version 0.3 Added cartesianToLinear, collisionCircleLine, compareAngleSpan
      7  * \version 0.2 Added getCartesianMagnitude
      8  */
      9 
     10 
     11 static PyObject * compareAngleSpan_func(PyObject *self, PyObject *args) {
     12 	
     13 	enum lash_anglespan_compare result;
     14 	char *resultstring;
     15 	float oldradmin;
     16 	float oldradmax;
     17 	float newradmin;
     18 	float newradmax;
     19 	
     20 	if (!PyArg_ParseTuple(args, "ffff", &oldradmin, &oldradmax, &newradmin, &newradmax)) {
     21 		PyErr_SetString(PyExc_RuntimeError, "Invalid input");
     22 		return NULL;
     23 	}
     24 		
     25 	result = lash_compareAngleSpan(oldradmin, oldradmax, newradmin, newradmax);
     26 	
     27 	switch(result) {
     28 		case LASH_ANGLESPAN_NONE:
     29 			resultstring = "none";
     30 			break;
     31 		case LASH_ANGLESPAN_MAX:
     32 			resultstring = "max";
     33 			break;
     34 		case LASH_ANGLESPAN_MIN:
     35 			resultstring = "min";
     36 			break;
     37 		case LASH_ANGLESPAN_BOTH:
     38 			resultstring = "minmax";
     39 			break;
     40 		case LASH_ANGLESPAN_SPLIT:
     41 			resultstring = "split";
     42 			break;
     43 		default:
     44 			resultstring = "error";
     45 	}
     46 	
     47 	return Py_BuildValue("s", resultstring);
     48 	
     49 	
     50 }
     51 
     52 static PyObject * collisionCircleLine_func(PyObject *self, PyObject *args) {
     53 	
     54 	lash_game_line_t line;
     55 	float circle_radius;
     56 	lash_game_coords_float_t circle_coords;
     57 	lash_game_coords_float_t intersect_a;
     58 	lash_game_coords_float_t intersect_b;
     59 	char numintersections;
     60 	
     61 	if (!PyArg_ParseTuple(args, "ddfdd", &circle_coords.x, &circle_coords.y, &circle_radius, &line.m, &line.c)) {
     62 		PyErr_SetString(PyExc_RuntimeError, "Invalid input");
     63 		return NULL;
     64 	}
     65 		
     66 	numintersections = lash_collisionCircleLine(circle_coords, circle_radius, line, &intersect_a, &intersect_b);
     67 	
     68 	if (numintersections == 2) {
     69 		return Py_BuildValue("ffff", intersect_a.x, intersect_a.y, intersect_b.x, intersect_b.y);	
     70 	} else if (numintersections == 1) {
     71 		return Py_BuildValue("ffff", intersect_a.x, intersect_a.y);	
     72 	}
     73 	
     74 	return Py_None;
     75 	
     76 	
     77 }
     78 
     79 static PyObject * cartesianToLinear_func(PyObject *self, PyObject *args) {
     80 	
     81 	lash_game_line_t line;
     82 	lash_game_coords_float_t source;
     83 	lash_game_coords_float_t target;
     84 	
     85 	if (!PyArg_ParseTuple(args, "dddd", &source.x, &source.y, &target.x, &target.y)) {
     86 		PyErr_SetString(PyExc_RuntimeError, "Invalid input");
     87 		return NULL;
     88 	}
     89 		
     90 	lash_cartesianToLinear(source, target, &line);
     91 	
     92 	return Py_BuildValue("ff", line.m, line.c);
     93 }
     94 
     95 static PyObject * cartesianMagnitude_func(PyObject *self, PyObject *args) {
     96 	
     97 	int targetx;
     98 	int targety;
     99 	int sourcex;
    100 	int sourcey;
    101 	
    102 	if (!PyArg_ParseTuple(args, "iiii", &targetx, &targety, &sourcex, &sourcey)) {
    103 		PyErr_SetString(PyExc_RuntimeError, "Invalid input");
    104 		return NULL;
    105 	}
    106 	
    107 	return Py_BuildValue("f", lash_cartesianMagnitude(targetx,  targety, sourcex, sourcey));
    108 }
    109 
    110 static PyObject * getContainedAngle_func(PyObject *self, PyObject *args) {
    111 	double x;
    112 	double y;
    113 	double m;
    114 	double w;
    115 	double h;
    116 	int bt, br, bb, bl;
    117 	
    118 	
    119 	double max_r;
    120 	double min_r;
    121 	double max_x;
    122 	double max_y;
    123 	double min_x;
    124 	double min_y;
    125 	
    126 	if (!PyArg_ParseTuple(args, "dddddIIII", &x, &y, &m, &w, &h, &bt, &br, &bb, &bl)) {
    127 		PyErr_SetString(PyExc_RuntimeError, "Invalid input");
    128 		return NULL;
    129 	}
    130 		
    131  
    132 	lash_getContainedAngle(x, y, m, w, h, bt, br, bb, bl, &min_r, &max_r, 1, &min_x, &min_y, &max_x, &max_y);
    133 	
    134 	return Py_BuildValue("dddddd", min_r, max_r, min_x, min_y, max_x, max_y);
    135 
    136 	  
    137 } 
    138 
    139 static PyObject * indexToCartesian_func(PyObject *self, PyObject *args) {
    140 	unsigned int w;
    141 	unsigned int h;
    142 	lash_game_map_index_t idx;
    143 	unsigned int unitsize;
    144 	lash_game_coords_t vtx;
    145 	vtx.x = 0;
    146 	vtx.y = 0; 
    147 	
    148 	if (!PyArg_ParseTuple(args, "IIkI", &w, &h, &idx, &unitsize)) {
    149 		PyErr_SetString(PyExc_RuntimeError, "Invalid input");
    150 		return NULL;
    151 	}
    152 	
    153 	lash_indexToCartesian(&vtx, &w, &h, &unitsize, &idx);
    154 	
    155 	return Py_BuildValue("II", vtx.x, vtx.y);
    156 	
    157 }
    158 
    159 static PyObject * cartesianToIndex_func(PyObject *self, PyObject *args) {
    160 	unsigned int w;
    161 	unsigned int h;
    162 	lash_game_map_index_t idx;
    163 	unsigned int unitsize;
    164 	lash_game_coords_t vtx;
    165 	
    166 	if (!PyArg_ParseTuple(args, "IIIII", &w, &h, &vtx.x, &vtx.y, &unitsize)) {
    167 		PyErr_SetString(PyExc_RuntimeError, "Invalid input");
    168 		return NULL;
    169 	} 
    170 	
    171 	lash_cartesianToIndex(&idx, &w, &h, &unitsize, &vtx);
    172 	
    173 	return Py_BuildValue("I", (unsigned int)idx);
    174 	
    175 }
    176 
    177 static PyObject * cartesianToPolar_func(PyObject *self, PyObject *args) {
    178 	
    179 	int sourcex;
    180 	int sourcey;
    181 	int targetx;
    182 	int targety;
    183 	float radians;
    184 	float radius;
    185 	int invx;
    186 	int invy;
    187 	
    188 	if (!PyArg_ParseTuple(args, "iiiiii", &sourcex, &sourcey, &targetx, &targety, &invx, &invy)) {
    189 		PyErr_SetString(PyExc_RuntimeError, "Invalid input");
    190 		return NULL;
    191 	}	
    192 	
    193 	lash_cartesianToPolar(sourcex, sourcey, targetx, targety, &radians, &radius, invx, invy);
    194 	
    195 	return Py_BuildValue("ff", radians, radius);
    196 }
    197 
    198 static PyObject * normalizeRadians_func(PyObject *self, PyObject *args) {
    199 	
    200 	float rin;
    201 	
    202 	if (!PyArg_ParseTuple(args, "f", &rin)) {
    203 		PyErr_SetString(PyExc_RuntimeError, "Invalid input");
    204 		return NULL;
    205 	}	
    206 	
    207 	return Py_BuildValue("f", lash_normalizeRadians(rin));
    208 }
    209 
    210 static PyMethodDef PylashgameMethods[] = {
    211 	{"cartesianMagnitude", (PyCFunction)cartesianMagnitude_func, METH_VARARGS, "Distance between two (2d) points"},
    212 	{"compareAngleSpan", (PyCFunction)compareAngleSpan_func, METH_VARARGS, "Determine action to be taken to deduct one angle span from the other"},
    213 	{"indexToCartesian", (PyCFunction)indexToCartesian_func, METH_VARARGS, "Convert from index in tiled rectangle to cartesian coordinate"},
    214 	{"cartesianToIndex", (PyCFunction)cartesianToIndex_func, METH_VARARGS, "Convert from cartesian coordinate to index in tiled rectangle"},
    215 	{"getContainedAngle", (PyCFunction)getContainedAngle_func, METH_VARARGS, "Get the minimum and maximum radians from a position in a plane still contained with given radius"},
    216 	{"cartesianToPolar", (PyCFunction)cartesianToPolar_func, METH_VARARGS, "Converts cartesian coords to polar coords"},
    217 	{"cartesianToLinear", (PyCFunction)cartesianToLinear_func, METH_VARARGS, "Converts cartesian coords to magnitude and y intersect"},
    218 	{"normalizeRadians", (PyCFunction)normalizeRadians_func, METH_VARARGS, "Converts negative and positive radians to 2pi > radians > 0"},
    219 	{"collisionCircleLine", (PyCFunction)collisionCircleLine_func, METH_VARARGS, "Intersection coords between circle and line"},
    220 	{NULL, NULL, 0, NULL}
    221 };
    222 
    223 static struct PyModuleDef lashgamemodule = {
    224    PyModuleDef_HEAD_INIT,
    225    "lashgame",   /* name of module */
    226    NULL, /* module documentation, may be NULL */
    227    -1,       /* size of per-interpreter state of the module,
    228                 or -1 if the module keeps state in global variables. */
    229    PylashgameMethods
    230 };
    231 
    232 PyMODINIT_FUNC PyInit_lashgame(void)
    233 {
    234     return PyModule_Create(&lashgamemodule);
    235 }