// // unload a persistent room from memory. Will not work if PCs are present. PyObject *PyRoom_unloadPersistence(PyObject *pyroom) { ROOM_DATA *room = PyRoom_AsRoom(pyroom); if(room == NULL) { PyErr_Format(PyExc_TypeError, "tried to save nonexistent room."); return NULL; } // it's not pesistent if(!roomIsPersistent(room)) return Py_BuildValue("i", 0); // does it contain a PC? LIST_ITERATOR *ch_i = newListIterator(roomGetCharacters(room)); CHAR_DATA *ch = NULL; bool pc_found = FALSE; ITERATE_LIST(ch, ch_i) { if(!charIsNPC(ch)) { pc_found = TRUE; break; } } deleteListIterator(ch_i); if(pc_found) return Py_BuildValue("i", 0); worldStorePersistentRoom(gameworld, roomGetClass(room), room); extract_room(room); return Py_BuildValue(""); }
int PyRoom_setpersistent(PyObject *self, PyObject *arg) { ROOM_DATA *room = PyRoom_AsRoom(self); if(room == NULL) return -1; else if(arg == Py_True) roomSetPersistent(room, TRUE); else if(arg == Py_False) roomSetPersistent(room, FALSE); else return -1; return 0; }
// // prepare a persistent room to be saved to disc PyObject *PyRoom_dirtyPersistence(PyObject *pyroom) { ROOM_DATA *room = PyRoom_AsRoom(pyroom); if(room == NULL) { PyErr_Format(PyExc_TypeError, "tried to dirty nonexistent room."); return NULL; } // if we're not persistent, ignore if(!roomIsPersistent(room)) return Py_BuildValue("i", 0); else if(!roomIsPersistentDirty(room)) { listPut(p_to_save, room); roomSetPersistentDirty(room); } return Py_BuildValue(""); }
// // interrupt an event involving the given room, object, or mobile PyObject *PyEvent_interrupt_event(PyObject *self, PyObject *args) { PyObject *PyOwner = NULL; // the python representation of our owner // parse our owner if(!PyArg_ParseTuple(args, "O", &PyOwner)) { PyErr_Format(PyExc_TypeError, "A target must be specified for interrupting events."); return NULL; } // check the possibilities if(PyOwner == Py_None) interrupt_events_involving(NULL); else if(PyChar_Check(PyOwner)) interrupt_events_involving(PyChar_AsChar(PyOwner)); else if(PyRoom_Check(PyOwner)) interrupt_events_involving(PyRoom_AsRoom(PyOwner)); else if(PyObj_Check(PyOwner)) interrupt_events_involving(PyObj_AsObj(PyOwner)); // everything ok return Py_BuildValue("i", 1); }
// // no point in reinventing the wheel; start_event and start_update both // need to be handled exactly the same, so here's a function to do just that PyObject *PyEvent_start(PyObject *self, PyObject *args, void *func) { void (* start_func)(void *, int, void *, void *, void *, const char *) = func; PyObject *PyOwner = NULL; // the python representation of our owner PyObject *efunc = NULL; // the event function PyObject *edata = Py_None; // the event data char *arg = NULL; // the arg we will be supplying to the function double delay = 0; // how long the event delay is (in seconds) void *owner = NULL; // actual owner supplied to the event handler char otype[20]; // is the owner a char, room, obj, or None? // try to parse all of our values if(!PyArg_ParseTuple(args, "OdO|Os", &PyOwner, &delay, &efunc, &edata, &arg)){ //PyErr_Format(PyExc_TypeError, //"Invalid arguments provided to event handler"); return NULL; } // make sure our event is a function if(!PyFunction_Check(efunc)) { PyErr_Format(PyExc_TypeError, "The event handler supplied must be a python function"); return NULL; } // figure out what type of data our owner is if(PyOwner == Py_None) { owner = NULL; sprintf(otype, "none"); } else if(PyChar_Check(PyOwner)) { owner = PyChar_AsChar(PyOwner); sprintf(otype, "char"); } else if(PyRoom_Check(PyOwner)) { owner = PyRoom_AsRoom(PyOwner); sprintf(otype, "room"); } else if(PyObj_Check(PyOwner)) { owner = PyObj_AsObj(PyOwner); sprintf(otype, "obj"); } // invalid type else { PyErr_Format(PyExc_TypeError, "An event owner must be a room, char, obj, or none"); return NULL; } // make sure the owner exists if(PyOwner != Py_None && owner == NULL) { PyErr_Format(PyExc_StandardError, "Owner supplied does not exist in game"); return NULL; } // now, queue up the action start_func(owner, (int)(delay SECONDS), PyEvent_on_complete, NULL, Py_BuildValue("sOO", otype, efunc, edata), arg); // everything seems ok. exit normally return Py_BuildValue("i", 1); }
PyObject *py_gen_do_trigs(PyObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] ={ "type","ch","obj","room","exit","cmd","arg","opts",NULL }; char *type = NULL; char *cmd = NULL; char *arg = NULL; PyObject *pych = NULL; PyObject *pyobj = NULL; PyObject *pyroom = NULL; PyObject *pyexit = NULL; PyObject *pyopts = NULL; LIST *opts = NULL; CHAR_DATA *ch = NULL; OBJ_DATA *obj = NULL; ROOM_DATA *room = NULL; EXIT_DATA *exit = NULL; void *me = NULL; int me_type = -1; bool fail = FALSE; // first, parse all of our arguments if(!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOOssO", kwlist, &type, &pych, &pyobj, &pyroom, &pyexit, &cmd, &arg, &pyopts)) { PyErr_Format(PyExc_TypeError, "do_trigs supplied invalid arguments."); return NULL; } // make sure we exist, and are of a valid type if(PyChar_Check(self)) { me = PyChar_AsChar(self); me_type = TRIGVAR_CHAR; } else if(PyObj_Check(self)) { me = PyObj_AsObj(self); me_type = TRIGVAR_OBJ; } else if(PyRoom_Check(self)) { me = PyRoom_AsRoom(self); me_type = TRIGVAR_ROOM; } // did we find a character? if(me == NULL) { PyErr_Format(PyExc_TypeError,"do_trigs owner does not exist."); return NULL; } // make sure ch is of the specified type if(pych!=NULL && (!PyChar_Check(pych) || (ch=PyChar_AsChar(pych)) == NULL)){ PyErr_Format(PyExc_TypeError,"do_trigs expects ch to be character."); return NULL; } // make sure obj is of the specified type if(pyobj!=NULL && (!PyObj_Check(pyobj) || (obj=PyObj_AsObj(pyobj)) == NULL)){ PyErr_Format(PyExc_TypeError,"do_trigs expects obj to be object."); return NULL; } // make sure room is of the specified type if(pyroom!=NULL&&(!PyRoom_Check(pyroom)||(room=PyRoom_AsRoom(pyroom))==NULL)){ PyErr_Format(PyExc_TypeError,"do_trigs expects room to be room."); return NULL; } // make sure exit is of the specified type if(pyexit!=NULL&&(!PyExit_Check(pyexit)||(exit=PyExit_AsExit(pyexit))==NULL)){ PyErr_Format(PyExc_TypeError,"do_trigs expects exit to be an exit."); return NULL; } // parse opts if(pyopts != NULL && !PyDict_Check(pyopts)) { PyErr_Format(PyExc_TypeError,"do_trigs expects opts to be a dict."); return NULL; } else if(pyopts != NULL) { PyObject *pairs = PyDict_Items(pyopts); int i = 0; opts = newList(); // go through each pair and add it to the pyopts list for(; i < PyList_Size(pairs); i++) { PyObject *pair = PyList_GetItem(pairs, i); PyObject *pykey = PyTuple_GetItem(pair, 0); PyObject *pyval = PyTuple_GetItem(pair, 1); OPT_VAR *opt = NULL; char *key = NULL; // make sure the key is a string if(PyString_Check(pykey)) key = PyString_AsString(pykey); else { PyErr_Format(PyExc_TypeError, "do_trigs opt keys must be strings."); fail = TRUE; break; } // check to make sure the val is a valid type if(PyChar_Check(pyval)) { CHAR_DATA *val = PyChar_AsChar(pyval); if(val != NULL) opt = newOptVar(key, val, TRIGVAR_CHAR); else { PyErr_Format(PyExc_TypeError,"%s opt provided nonexistent char.",key); fail = TRUE; break; } } else if(PyObj_Check(pyval)) { OBJ_DATA *val = PyObj_AsObj(pyval); if(val != NULL) opt = newOptVar(key, val, TRIGVAR_OBJ); else { PyErr_Format(PyExc_TypeError,"%s opt provided nonexistent obj.", key); fail = TRUE; break; } } else if(PyRoom_Check(pyval)) { ROOM_DATA *val = PyRoom_AsRoom(pyval); if(val != NULL) opt = newOptVar(key, val, TRIGVAR_ROOM); else { PyErr_Format(PyExc_TypeError,"%s opt provided nonexistent room.",key); fail = TRUE; break; } } else { PyErr_Format(PyExc_TypeError,"%s opt provided invalid value.",key); fail = TRUE; break; } // append the opt to the opt list listPut(opts, opt); } Py_DECREF(pairs); } // did everything succeed? if(fail == FALSE) gen_do_trigs(me,me_type,type,ch,obj,room,exit,cmd,arg,opts); // garbage collection if(opts != NULL) deleteListWith(opts, deleteOptVar); if(fail == TRUE) return NULL; return Py_BuildValue(""); }
PyObject *PyRoom_getpersistent(PyObject *self, void *closure) { ROOM_DATA *room = PyRoom_AsRoom(self); if(room != NULL) return Py_BuildValue("i", roomIsPersistent(room)); else return NULL; }