/* File: module_pyNetrek_server_tools_setship.c Version: 0.1 Date: 2007/03/04 19:52:26 EST William U. Clark, Jr. */ /* gcc -pthread -shared -I../include module_setship.c -I/usr/include/python2.4 ../ntserv/libnetrek.a /usr/lib/libpython2.4.so.1.0 -o setship.so gcc -I/usr/include/python2.4 -I../netrek-server/Vanilla/include -c module_pyNetrek_server_tools_setship.c -o setship.o gcc -pthread -shared setship.o -o setship.so -L/usr/lib/python2.4 -lpython2.4 -L../netrek-server/Vanilla/ntserv -lnetrek */ #include "Python.h" #include #include #include #include #include #include "defs.h" #include "struct.h" #include "data.h" #include "proto.h" #include "util.h" static void refit(struct player *me, int type) { getship(&(me->p_ship), type); /* enable docking */ if (type == STARBASE) me->p_flags |= PFDOCKOK; /* restrict speed to new class */ if (me->p_desspeed > me->p_ship.s_maxspeed) me->p_desspeed = me->p_ship.s_maxspeed; /* bump all docked ships */ bay_release_all(me); me->p_flags |= PFDOCKOK; } struct torp *t_find(struct player *me, int status) { struct torp *k = NULL; for (k = firstTorpOf(me); k <= lastTorpOf(me); k++) if (k->t_status == status) break; return k; } int t_attribute = TOWNERSAFE | TDETTEAMSAFE; int t_torpspeed = -1; static char setship_doc[] = "This module provides a number of functions for \ setting and getting the position, direction, speed \ of ships. In addition it provides a functions for \ firing torpedoes and getting their location."; static PyObject* setship_setPos(PyObject *self, PyObject *args) { PyObject *n, *x, *y; if(!PyArg_ParseTuple(args, "ill", &n, &x, &y)){ return NULL; } p_x_y_set(&players[(int)n], (long)x, (long)y); return Py_BuildValue("s", "Position Set"); } static char setship_setPos_doc[] = "setPos(n, x, y)\n\ \n\ Move ship n to new coordinates."; static PyObject* setship_setDir(PyObject *self, PyObject *args) { struct player *me; PyObject *n, *d; if(!PyArg_ParseTuple(args, "il", &n, &d)){ return NULL; } me = &players[(int)n]; players[(int)n].p_dir = d; me->p_desdir = d; bay_release(me); me->p_flags &= ~(PFBOMB | PFORBIT | PFBEAMUP | PFBEAMDOWN); return Py_BuildValue("s", "Dir set"); } static char setship_setDir_doc[] = "setDir(shipNum, dir)\n\ \n\ Set the direction of ship #shipNum to dir."; static PyObject* setship_getDir(PyObject *self, PyObject *args) { struct player *me; PyObject *n; if(!PyArg_ParseTuple(args, "i", &n)){ return NULL; } me = &players[(int)n]; return Py_BuildValue("i", players[(int)n].p_dir); } static char setship_getDir_doc[] = "getDir(shipNum)\n\ \n\ Get the current direction of ship #shipNum."; static PyObject* setship_setSpd(PyObject *self, PyObject *args) { struct player *me; PyObject *n, *speed; if(!PyArg_ParseTuple(args, "il", &n, &speed)){ return NULL; } me = &players[(int)n]; me->p_desspeed = speed; me->p_flags &= ~(PFREPAIR | PFBOMB | PFORBIT | PFBEAMUP | PFBEAMDOWN); me->p_flags &= ~(PFPLOCK | PFPLLOCK); return Py_BuildValue("sisi", "Setting speed for ship ", n, " to warp ", speed); } static char setship_setSpd_doc[] = "setDir(shipNum, speed)\n\ \n\ Set the ship #shipNum to speed spd."; static PyObject* setship_getSpd(PyObject *self, PyObject *args) { struct player *me; PyObject *n; if(!PyArg_ParseTuple(args, "i", &n)){ return NULL; } me = &players[(int)n]; return Py_BuildValue("i", me->p_speed); } static char setship_getSpd_doc[] = "getSpd(shipNum)\n\ \n\ Get the current speed for ship #shipNum."; static PyObject* setship_setType(PyObject *self, PyObject *args) { struct player *me; PyObject *player, *type; if(!PyArg_ParseTuple(args, "is", &player, &type)){ return NULL; } me = &players[(int)player]; if(strcmp(type,"AS")==0){ refit(&players[(int)player], ASSAULT); } else if(strcmp(type,"BB")==0){ refit(&players[(int)player], BATTLESHIP); } else if(strcmp(type,"CA")==0){ refit(&players[(int)player], CRUISER); } else if(strcmp(type,"DD")==0){ refit(&players[(int)player], DESTROYER); } else if(strcmp(type,"SC")==0){ refit(&players[(int)player], SCOUT); } else if(strcmp(type,"SB")==0){ refit(&players[(int)player], STARBASE); } else if(strcmp(type,"GA")==0){ refit(&players[(int)player], SGALAXY); } else if(strcmp(type,"AT")==0){ refit(&players[(int)player], ATT); } else if(strcmp(type,"SSC")==0){ getship(&players[(int)player].p_ship, SCOUT); players[(int)player].p_ship.s_torpdamage = 1; players[(int)player].p_ship.s_torpfuse = 8; players[(int)player].p_ship.s_phaserdamage = 1; players[(int)player].p_ship.s_plasmadamage = 1; players[(int)player].p_ship.s_plasmaspeed = 1; players[(int)player].p_ship.s_plasmaturns = 1; players[(int)player].p_ship.s_maxshield = 750; } else if(strcmp(type,"FSB")==0){ getship(&players[(int)player].p_ship, STARBASE); /*players[(int)player].p_ship.s_torpdamage = 1; players[(int)player].p_ship.s_torpfuse = 8; players[(int)player].p_ship.s_phaserdamage = 1; players[(int)player].p_ship.s_plasmadamage = 1; players[(int)player].p_ship.s_plasmaspeed = 1; players[(int)player].p_ship.s_plasmaturns = 1; players[(int)player].p_ship.s_maxshield = 750;*/ players[(int)player].p_ship.s_maxspeed = 750; } players[(int)player].p_damage = 0; players[(int)player].p_shield = players[(int)player].p_ship.s_maxshield; players[(int)player].p_wtemp = 0; players[(int)player].p_etemp = 0; players[(int)player].p_fuel = players[(int)player].p_ship.s_maxfuel; if (strcmp(type,"SB")==0) players[(int)player].p_flags |= PFDOCKOK; return Py_BuildValue("ss", "Setting ship type to", type); } static char setship_setType_doc[] = "setType(shipNum, type)\n\ \n\ Set the ship #shipNum to speed spd."; static PyObject* setship_getType(PyObject *self, PyObject *args) { struct player *me; PyObject *n; if(!PyArg_ParseTuple(args, "i", &n)){ return NULL; } me = &players[(int)n]; return Py_BuildValue("i", me->p_speed); } static char setship_getType_doc[] = "getSpd(shipNum)\n\ \n\ Get the current speed for ship #shipNum."; static PyObject* setship_fireTestTorp(PyObject *self, PyObject *args) { struct player *me; struct ship *myship; struct torp *k; PyObject *n, *dir; if(!PyArg_ParseTuple(args, "il", &n, &dir)){ return NULL; } me = &players[(int)n]; myship = &me->p_ship; k = t_find(me, TFREE); me->p_ntorp++; k->t_status = TMOVE; k->t_type = TPLASMA; k->t_attribute = t_attribute; k->t_owner = me->p_no; t_x_y_set(k, me->p_x, me->p_y); k->t_turns = myship->s_torpturns; k->t_damage = 0; k->t_gspeed = (t_torpspeed == -1 ? myship->s_torpspeed : t_torpspeed) * WARP1; k->t_fuse = 500; k->t_dir = dir; k->t_war = me->p_war; k->t_team = me->p_team; k->t_whodet = NODET; return Py_BuildValue("s", "Speed Set"); } static char setship_fireTestTorp_doc[] = "setDir(shipNum, dir)\n\ \n\ Set the ship direction to dir."; static PyObject* setship_getPos(PyObject *self, PyObject *args) { struct player *me; PyObject *n; if(!PyArg_ParseTuple(args, "i", &n)){ return NULL; } me = &players[(int)n]; return Py_BuildValue("{s:i,s:i,s:i,s:i}", "p_x",me->p_x,"p_y",me->p_y, "p_x_internal", me->p_x_internal, "p_y_internal", me->p_y_internal ); } static char setship_getPos_doc[] = "setDir(shipNum, dir)\n\ \n\ Set the ship direction to dir."; static PyObject* setship_sleep(PyObject *self, PyObject *args) { PyObject *t; if(!PyArg_ParseTuple(args, "i", &t)){ return NULL; } sleep(t); return Py_BuildValue("sis", "Sleeping for ",t," seconds." ); } static char setship_sleep_doc[] = "setDir(shipNum, dir)\n\ \n\ Set the ship direction to dir."; static PyObject* setship_waitForStop(PyObject *self, PyObject *args){ struct player *me; PyObject *n; if(!PyArg_ParseTuple(args, "i", &n)){ return NULL; } me = &players[(int)n]; while (me->p_speed) usleep(20000); return Py_BuildValue("s", "Waited For Stop."); } static char setship_waitForStop_doc[] = "waitForStop(shipNum)\n\ \n\ Have program wait until ship #shipNum stops."; static PyObject* setship_setPlanetLock(PyObject *self, PyObject *args){ struct player *me; struct planet *pl; PyObject *n, *p; if(!PyArg_ParseTuple(args, "is", &n, &p)){ return NULL; } /* lock on, from lock_planet() in interface.c */ pl = planet_find(p); me = &players[(int)n]; me->p_flags |= PFPLLOCK; me->p_flags &= ~(PFPLOCK|PFORBIT|PFBEAMUP|PFBEAMDOWN|PFBOMB); me->p_planet = pl->pl_no; return Py_BuildValue("s", "Locking ship ", n, " onto planet ", p); } static char setship_setPlanetLock_doc[] = "setPlanetLock(shipNum, planetName)\n\ \n\ Lock ship #shipNum onto planet #planetName."; static PyMethodDef setship_methods[] = { {"setPos", setship_setPos, METH_VARARGS, setship_setPos_doc}, {"setDir", setship_setDir, METH_VARARGS, setship_setDir_doc}, {"getDir", setship_getDir, METH_VARARGS, setship_getDir_doc}, {"setSpd", setship_setSpd, METH_VARARGS, setship_setSpd_doc}, {"getSpd", setship_getSpd, METH_VARARGS, setship_getSpd_doc}, {"fireTestTorp", setship_fireTestTorp, METH_VARARGS, setship_fireTestTorp_doc}, {"getPos", setship_getPos, METH_VARARGS, setship_getPos_doc}, {"sleep", setship_sleep, METH_VARARGS, setship_sleep_doc}, {"waitForStop", setship_waitForStop, METH_VARARGS, setship_waitForStop_doc}, {"setPlanetLock", setship_setPlanetLock, METH_VARARGS, setship_setPlanetLock_doc}, {"setType", setship_setType, METH_VARARGS, setship_setType_doc}, {NULL, NULL} }; PyMODINIT_FUNC initsetship(void) { Py_InitModule3("setship", setship_methods, setship_doc); openmem(0); }