• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

Feature Quest Log lua handler.

tarjei

Necronian Engineer
Joined
May 25, 2008
Messages
505
Reaction score
126
Location
Poland
Hello, I wrote recently nice lib, that handles questlog in lua. It doesn't have any kickedass abilities in this version but it wasnt my point to give that for you. I hope it will be a bit educational, thats why I wont lead you step by step how to implement it.

C++ code:

Code:
	//createQuest(name, storage);
	lua_register(m_luaState, "createQuest", LuaScriptInterface::luaCreateQuest);

	//addMission(questname, missionname,missionState, startstorage, endstoragevalue);
	lua_register(m_luaState, "addMission", LuaScriptInterface::luaAddMission);

//AddMissionState(questname, missionname, id, descr);
	lua_register(m_luaState, "addMissionState", LuaScriptInterface::luaAddMissionState);
Code:
int32_t LuaScriptInterface::luaCreateQuest(lua_State* L){ 
    //CreateQuest(name, storage);
    int32_t id = popNumber(L);
    int32_t storageId = popNumber(L);
    std::string name = popString(L);
    
    Quest* quest = new Quest(name, Quests::getInstance()->getLastQuestId() + id, storageId, 1);
    LuaQuestMap::iterator it = Quests::getInstance()->luaQuests.find(name);
    if( it != Quests::getInstance()->luaQuests.end())
     std::cout<<" luaCreateQuest: warning quest with this name already exists"<<std::endl;
     
    Quests::getInstance()->luaQuests[name] = quest;
    
	return 1;               
}  
int32_t LuaScriptInterface::luaAddMission(lua_State* L){ 
    //AddMission(questname, missionname, startstorage, endstoragevalue);
    int32_t endValue = popNumber(L);
    int32_t startValue = popNumber(L);
    int32_t storageId = popNumber(L);

    std::string missionName = popString(L);
    std::string questName = popString(L);
    
    LuaQuestMap::iterator it = Quests::getInstance()->luaQuests.find(questName);
    if( it == Quests::getInstance()->luaQuests.end())
    std::cout<<" luaCreateQuest: warning quest with this name DOESNT exists"<<std::endl;
    Quest* quest = Quests::getInstance()->luaQuests[questName];
    Mission* mission = new Mission(missionName, "", storageId, startValue, endValue);
    quest->newMission(mission);
	return 1;               
}    
int32_t LuaScriptInterface::luaAddMissionState(lua_State* L){ 
    //AddMissionState(questname, missionname, id, descr);
    
    std::string missionState = popString(L);
    int32_t missionId = popNumber(L);
    std::string missionName = popString(L);
    std::string questName = popString(L);
    
    LuaQuestMap::iterator it = Quests::getInstance()->luaQuests.find(questName);
    if( it == Quests::getInstance()->luaQuests.end())
    std::cout<<" luaAddMissionState: warning quest with this name DOESNT exists"<<std::endl;
    Quest* quest = Quests::getInstance()->luaQuests[questName];
    if(!quest){
    std::cout<<" luaAddMissionState: warning quest with this name DOESNT exists!!! FATAL"<<std::endl;
     return 1;
     }
    Mission* mission = quest->getMissionByName(missionName);
    if(!mission)
    {
     std::cout<<" luaAddMissionState: warning mission with this name DOESNT exists!!! FATAL"<<std::endl;
       return 1;       
    }
     std::cout<<" luaAddMissionState: new state added."<<std::endl;
   
    mission->newState(missionId, missionState);
	return 1;               
}

Code:
uint16_t Quests::getQuestCount(Player* player)
{
	uint16_t count = 0;
	for(QuestList::iterator it = quests.begin(); it != quests.end(); it++)
	{
		if((*it)->isStarted(player))
			count++;
	}
	for(LuaQuestMap::iterator it = luaQuests.begin(); it != luaQuests.end(); it++)
	{
		if(it->second->isStarted(player))
			count++;
	} 
	return count;
}

Quest* Quests::getQuestById(uint16_t id) const
{
	for(QuestList::const_iterator it = quests.begin(); it != quests.end(); it++)
	{
		if((*it)->getId() == id)
			return (*it);
	}
	for(LuaQuestMap::const_iterator it = luaQuests.begin(); it != luaQuests.end(); it++)
	{
		if(it->second->getId() == id)
			return it->second;
	}
	return NULL;
}

uint16_t Quests::getLastQuestId(){return m_lastId;}

Code:
Mission* Quest::getMissionByName(std::string name){
     for(MissionList::iterator it = missions.begin(); it != missions.end(); ++it)
     {
          if((*it)->getRealName() == name)
            return (*it);                      
     }  
     return NULL;  
}
		std::string getRealName() {return  name;}

Code:
void ProtocolGame::sendQuests()
{ 
	NetworkMessage_ptr msg = getOutputBuffer();
	if(msg)
	{
		TRACK_MESSAGE(msg);
		msg->AddByte(0xF0);

		msg->AddU16(Quests::getInstance()->getQuestCount(player));
		for(QuestList::const_iterator it = Quests::getInstance()->getFirstQuest(); it != Quests::getInstance()->getLastQuest(); ++it)
		{
			if(!(*it)->isStarted(player))
				continue;

			msg->AddU16((*it)->getId());
			msg->AddString((*it)->getName());
			msg->AddByte((*it)->isCompleted(player));
		}
		for(LuaQuestMap::const_iterator it = Quests::getInstance()->luaQuests.begin(); it != Quests::getInstance()->luaQuests.end(); ++it)
		{
			if(!it->second->isStarted(player))
				continue;

			msg->AddU16(it->second->getId());
			msg->AddString(it->second->getName());
			msg->AddByte(it->second->isCompleted(player));
		}
	}
}


Lua lib:
Code:
local Quest = {}
local Mission = {}

function Mission:new(questname, name)
	local obj = {}
	obj.questname = questname
	obj.missionname = name
	self.__index = self
	setmetatable(obj,self)
	
	return obj
end
function Mission:addState(id,description)
	addMissionState(self.questname, self.missionname, id,description);
end

function Quest:new(name, storage, id)
	local obj = {}
	obj.questname = name
	self.__index = self
	setmetatable(obj,self)
	
	createQuest(name, storage, id)
	return obj
end
function Quest:addMission(name, storage, startvalue, endvalue)
	local mission = Mission:new(self.questname, name)
	
	addMission(self.questname,name, storage, startvalue,endvalue);
	return mission
end

Sample:

Code:
local SampleQuest = Quest:new("Sample",5003, 1)
local mission1 = SampleQuest:addMission("DoSomething",5003,0,3)
mission1:addState(0,"State 0.")
mission1:addState(1,"State 1.")
mission1:addState(2,"State 2.")
mission1:addState(3,"State 4.")
local mission2 = SampleQuest:addMission("Other Mission",5003,0,3)
mission2:addState(0,"State 0.")
mission2:addState(1,"State 1.")
mission2:addState(2,"State 2.")
mission2:addState(3,"State 4.")

local SampleQuest2 = Quest:new("Sample 2",5004, 2) -->third arg is quest id
local m1 = SampleQuest2:addMission("Make me feel better",5004,0,3)
m1:addState(0,"State 0.")
m1:addState(1,"State 1.")
m1:addState(2,"State 2.")
m1:addState(3,"State 4.")
local m2 = SampleQuest:addMission("Task 2",5004,0,3)
m2:addState(0,"State 0.")
m2:addState(1,"State 1.")
m2:addState(2,"State 2.")
m2:addState(3,"State 4.")
Statistic.png



I hope you can handle the header files, I was too lazy to put it, and didnt want to give freeby for noobs that need 100% directions how to do stuff. Forget it, no more complete solutions. Have fun with that.
Regards.
 

Attachments

Last edited:
perfect

- - - Updated - - -

really awesome, thanks for sharing ^^
 
nice... but xml way is better.. lua will be if you will put some more things
 
I made it for my npc lua library, inside its even easier to handle questlog xD.
 
It is good, but no need to create new QuestList for lua quests, just you could have added them to the normal list, you would have only changed luascript.cpp rather than all that changes done.
 
Its preety much self explaining when you look at class names...
Luascripts, quests, protocolgame (cpp and h files)
 
Back
Top