static inline void SetupMissionFrame(lua_State* _State, struct MissionFrame* _Frame) { LuaCtor(_State, _Frame, LOBJ_MISSIONFRAME); lua_pushstring(_State, "From"); (_Frame->From != NULL) ? LuaCtor(_State, _Frame->From, LOBJ_BIGGUY) : lua_pushnil(_State); lua_rawset(_State, -3); lua_pushstring(_State, "Owner"); LuaCtor(_State, _Frame->Owner, LOBJ_BIGGUY); lua_rawset(_State, -3); }
int LuaCreateAnimation(lua_State* _State) { struct Sprite* _Sprite = NULL; const char* _File = luaL_checkstring(_State, 1);\ struct Resource* _Resource = ResourceGet(_File); SDL_Point _Pos = {0, 0}; _Sprite = CreateSprite(ResourceGetData(_Resource), 0, &_Pos); LuaCtor(_State, _Sprite, LOBJ_SPRITE); return 1; }
int LuaCreateSprite(lua_State* _State) { struct Sprite* _Sprite = NULL; const char* _File = luaL_checkstring(_State, 1); struct Resource* _Resource = ResourceGet(_File); SDL_Point _Pos = {0, 0}; if(_Resource == NULL) { lua_pushnil(_State); return 1; } _Sprite = CreateSprite(_Resource, 0, &_Pos); LuaCtor(_State, _Sprite, LOBJ_SPRITE); return 1; }
int LuaBhvCreateComposite(lua_State* _State, BhvCallback _Callback) { struct Behavior* _Bhv = NULL; int _Len = 0; int _ChildSz = 0; struct Behavior* _Child = NULL; luaL_checktype(_State, 1, LUA_TTABLE); _Len = lua_rawlen(_State, 1); _Bhv = CreateBehavior(NULL, NULL, _Len, _Callback); lua_pushnil(_State); while(lua_next(_State, -2) != 0) { if((_Child = LuaCheckClass(_State, -1, LOBJ_BEHAVIOR)) == NULL) { lua_pop(_State, 1); continue; } _Bhv->Children[_ChildSz++] = _Child; lua_pop(_State, 1); } LuaCtor(_State, _Bhv, LOBJ_BEHAVIOR); return 1; }
struct BehaviorNode* LuaBhvCreateNode(lua_State* _State) { struct LuaBhvAction _Temp; struct LuaBhvAction* _Action = NULL; struct BehaviorNode* _Behavior = NULL; int _Args = lua_gettop(_State) - 1; _Temp.Name = luaL_checkstring(_State, 1); if((_Action = bsearch(&_Temp, g_BhvActions, g_BhvActionsSz, sizeof(struct LuaBhvAction), LuaBaCmp)) == NULL) { lua_pushfstring(_State, ERRMSG_BHV, _Temp.Name); luaL_argerror(_State, 1, lua_tostring(_State, -1)); return NULL; } if(_Args != _Action->Arguments) { luaL_error(_State, "Behavior %s requires %d arguments but provided with %d.", _Temp.Name, _Action->Arguments, _Args); return NULL; } _Behavior = CreateBehaviorNode(_Action->Action, _Args); for(int i = 0; i < _Args; ++i) LuaToPrimitive(_State, 2 + i, &_Behavior->Arguments[i]); LuaCtor(_State, _Behavior, LOBJ_BHVNODE); return _Behavior; }
void MissionCall(lua_State* _State, const struct Mission* _Mission, struct BigGuy* _Owner, struct BigGuy* _From) { struct MissionFrame* _Frame= NULL; const char* _NewDesc = NULL; if(_Mission == NULL) return; _Frame = CreateMissionFrame(_From, _Owner, _Mission); _NewDesc = _Mission->Description; if(g_GameWorld.Player == _Owner) { if(_Mission->TextFormatSz > 0) { //Breaks when MissionFormatText is run on a coroutine other than the main coroutine. //because the rule used points to the main coroutine's lua_State. _NewDesc = MissionFormatText(_Mission->Description, _Mission->TextFormat, _Mission->TextFormatSz, _Frame); /* if(_SizeOf == 0) { Log(ELOG_WARNING, "Mission %s failed: MissionFormatText failed to format text.", _Mission->Name); return; } */ } if(_Mission->OnTrigger != 0) { CallMissionAction(_State, _Mission->OnTrigger, _Frame); } if((_Mission->Flags & MISSION_FNOMENU) == MISSION_FNOMENU) return; lua_settop(_State, 0); lua_pushstring(_State, "MissionMenu"); lua_createtable(_State, 0, 3); lua_pushstring(_State, "Mission"); LuaConstCtor(_State, _Mission, LOBJ_MISSION); lua_rawset(_State, -3); lua_pushstring(_State, "BigGuy"); LuaCtor(_State, _Owner, LOBJ_BIGGUY); lua_rawset(_State, -3); lua_pushstring(_State, "Data"); lua_pushlightuserdata(_State, _Frame); lua_rawset(_State, -3); lua_pushstring(_State, "Description"); lua_pushstring(_State, _NewDesc); lua_rawset(_State, -3); lua_pushinteger(_State, 512); lua_pushinteger(_State, 512); LuaCreateWindow(_State); if(CoRunning() != 0) { g_PlayerMisCall = CoRunning(); CoYield(); } } else { int _BestIndex = -1; //float _BestUtility = -1.0; //float _Utility = 0.0; if(_Mission->OnTrigger != 0) { CallMissionCond(_State, _Mission->OnTrigger, _Frame); } if((_Mission->Flags & MISSION_FNOMENU) == MISSION_FNOMENU) return; /*for(int i = 0; i < _Mission->OptionCt; ++i) { if(RuleEval(_Mission->Options[i].Condition) == 0) continue; _Utility = RuleEval(_Mission->Options[i].Utility); if(_Utility > _BestUtility) { _BestIndex = i; _BestUtility = _Utility; } }*/ _BestIndex = 0; _Frame->IsOptSel = 1; CallMissionAction(_State, _Mission->Options[_BestIndex].Action, _Frame); MissionFrameClear(_Frame); } }