/* ShipWars Plugin Sample Source To compile plugin, type: cc moveobj.c -o moveobj -shared -g --- A bit more advanced use of timming and to demostrate the fundimentals of manipulating objects. Pay close attention to how the SWServ's global objects are accessed and manipulated here in this code. In this example we will move the object three times then exit. */ #include #include "../include/swserv-plugins.h" #define TOTAL_MOVES 3 static int move_code; static time_t last_millitime, next_movement; /* See timming.c for why timmers have to be reset */ void ResetTimmers(void) { next_movement = 0; } /* * SWServ Plugin initialize. */ SWPLUGIN_INIT_FUNCRTN SWPLUGIN_INIT_FUNC(SWPLUGIN_INIT_FUNCINPUT) { /* Upon initialization, reset our globals */ ResetTimmers(); move_code = 0; /* Get current time of day in milliseconds */ last_millitime = SWServCurrentTimeMS(in); return(0); } /* * SWServ Plugin manage. */ SWPLUGIN_MANAGE_FUNCRTN SWPLUGIN_MANAGE_FUNC(SWPLUGIN_MANAGE_FUNCINPUT) { const time_t cur_millitime = SWServCurrentTimeMS(in); const int total = SWServTotalObjects(in); /* Now check if the time `cycled' */ if(cur_millitime < last_millitime) ResetTimmers(); last_millitime = cur_millitime; /* Is it time to move objects? */ if(next_movement <= cur_millitime) { int obj_num; xsw_object_struct *obj; float x_offset = 0.0, y_offset = 0.0; /* Calculate movement offset (in real units) */ switch(move_code) { case 0: x_offset = 0.0; y_offset = 2.5; break; case 1: x_offset = 0.0; y_offset = -4.2; break; case 2: x_offset = 2.8; y_offset = 1.3; break; } /* Move each object */ for(obj_num = 0; obj_num < total; obj_num++) { /* Get pointer to object from array */ obj = SWServObjectGetPointer(in, obj_num); if(obj == NULL) continue; /* Skip this object if its of type garbage or error */ if(obj->type <= SW_OBJ_TYPE_GARBAGE) continue; /* Skip this object if its not a type HOME object */ if(obj->type != SW_OBJ_TYPE_HOME) continue; /* Move this object */ obj->x += x_offset; obj->y += y_offset; /* Update object position to clients */ SWServObjectSyncConnections(in, obj_num, 0); } /* Increment move code */ move_code++; /* Finished? */ if(move_code >= TOTAL_MOVES) return(-1); /* Return -1 to indicate want to exit */ /* Schedual next movement in 2.5 seconds */ next_movement = cur_millitime + 2500l; } return(0); } /* * SWServ Plugin shutdown. */ SWPLUGIN_SHUTDOWN_FUNCRTN SWPLUGIN_SHUTDOWN_FUNC(SWPLUGIN_SHUTDOWN_FUNCINPUT) { return; }