/** * @brief Gets pilot at index or raises error if there is no pilot at index. * * @param L Lua state to get pilot from. * @param ind Index position to find pilot. * @return Pilot found at the index in the state. */ LuaPilot* luaL_checkpilot( lua_State *L, int ind ) { if (lua_ispilot(L,ind)) return lua_topilot(L,ind); luaL_typerror(L, ind, PILOT_METATABLE); return NULL; }
/** * @brief Hooks the function to a specific pilot. * * You can hook to different actions. Curently hook system only supports:<br /> * <ul> * <li> "death" : triggered when pilot dies (before marked as dead). <br /> * <li> "exploded" : triggered when pilot has died and the final explosion has begun. <br /> * <li> "board" : triggered when pilot is boarded.<br /> * <li> "disable" : triggered when pilot is disabled (with disable set).<br /> * <li> "jump" : triggered when pilot jumps to hyperspace (before he actually jumps out).<br /> * <li> "hail" : triggered when pilot is hailed.<br /> * <li> "land" : triggered when pilot is landing (right when starting land descent).<br /> * <li> "attacked" : triggered when the pilot is attacked. <br /> * <li> "idle" : triggered when the pilot becomes idle in manual control.<br /> * </ul> * <br /> * If you pass nil as pilot, it will set it as a global hook that will jump for all pilots.<br /> * <br /> * DO NOT DO UNSAFE THINGS IN PILOT HOOKS. THIS MEANS STUFF LIKE player.teleport(). IF YOU HAVE DOUBTS USE A "safe" HOOK.<br /> * <br /> * These hooks all pass the pilot triggering the hook as a parameter, so they should have the structure of:<br /> * <br /> * function my_hook( pilot, arg )<br /> * end<br /> * <br /> * The combat hooks also pass the pilot acting on it, so for example the pilot * that disabled, attacked or killed the selected pilot. They have the * following format:<br /> * <br /> * function combat_hook( pilot, attacker, arg )<br /> * end<br /> * <br /> * Please note that in the case of disable or death hook the attacker may be nil * indicating that it was killed by other means like for example the shockwave * of a dying ship or nebula volatility. * * @luaparam pilot Pilot identifier to hook (or nil for all). * @luaparam type One of the supported hook types. * @luaparam funcname Name of function to run when hook is triggered. * @luaparam arg Argument to pass to hook. * @luareturn Hook identifier. * @luafunc pilot( pilot, type, funcname, arg ) */ static int hook_pilot( lua_State *L ) { unsigned int h; LuaPilot *p; int type; const char *hook_type; char buf[ PATH_MAX ]; /* Parameters. */ if (lua_ispilot(L,1)) p = luaL_checkpilot(L,1); else if (lua_isnil(L,1)) p = NULL; else { NLUA_ERROR(L, "Invalid parameter #1 for hook.pilot, expecting pilot or nil."); return 0; } hook_type = luaL_checkstring(L,2); /* Check to see if hook_type is valid */ if (strcmp(hook_type,"death")==0) type = PILOT_HOOK_DEATH; else if (strcmp(hook_type,"exploded")==0) type = PILOT_HOOK_EXPLODED; else if (strcmp(hook_type,"board")==0) type = PILOT_HOOK_BOARD; else if (strcmp(hook_type,"disable")==0) type = PILOT_HOOK_DISABLE; else if (strcmp(hook_type,"jump")==0) type = PILOT_HOOK_JUMP; else if (strcmp(hook_type,"hail")==0) type = PILOT_HOOK_HAIL; else if (strcmp(hook_type,"land")==0) type = PILOT_HOOK_LAND; else if (strcmp(hook_type,"attacked")==0) type = PILOT_HOOK_ATTACKED; else if (strcmp(hook_type,"idle")==0) type = PILOT_HOOK_IDLE; else { /* hook_type not valid */ NLUA_ERROR(L, "Invalid pilot hook type: '%s'", hook_type); return 0; } /* actually add the hook */ snprintf( buf, sizeof(buf), "p_%s", hook_type ); h = hook_generic( L, buf, 0., 3, 0 ); if (p==NULL) pilots_addGlobalHook( type, h ); else pilot_addHook( pilot_get(p->pilot), type, h ); lua_pushnumber( L, h ); return 1; }