• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Auto map clean source help

Kiman

Grimhaven Staff
Premium User
Joined
Aug 18, 2009
Messages
560
Reaction score
23
Location
Sweden
I want to make the map clean on my server to automatcally do it every 4 hours. And to my understanding, I have to do it through source. But I dont know how, Would also like to put a warning before and after the map clean like:
Before: Map clean in 30 seconds,please pickup your items.
After: Map clean completed, next one in 4 hours.

I use TFS 0.2.5

Code:
uint32_t Map::clean()
{
	g_game.setGameState(GAME_STATE_MAINTAIN);

	g_game.setStateTime(0);
	uint64_t start = OTSYS_TIME();
	uint64_t count = 0;
	Tile* tile = NULL;
	Item *item = NULL;

	QTreeLeafNode* startLeaf;
	QTreeLeafNode* leafE;
	QTreeLeafNode* leafS;
	Floor* floor;

	startLeaf = getLeaf(0, 0);
	leafS = startLeaf;

	for(int32_t ny = 0; ny <= 0xFFFF; ny += FLOOR_SIZE)
	{
		leafE = leafS;
		for(int32_t nx = 0; nx <= 0xFFFF; nx += FLOOR_SIZE)
		{
			if(leafE)
			{
				for(int32_t nz = 0; nz < MAP_MAX_LAYERS; ++nz)
				{
					if(leafE && (floor = leafE->getFloor(nz)))
					{
						for(int32_t ly = 0; ly < FLOOR_SIZE; ++ly)
						{
							for(int32_t lx = 0; lx < FLOOR_SIZE; ++lx)
							{
								if(floor && (tile = floor->tiles[(nx + lx) & FLOOR_MASK][(ny + ly) & FLOOR_MASK]))
								{
                                    if(g_config.getBoolean(ConfigManager::CLEAN_PROTECTION_ZONE))
                                    {
			                            if(!tile->hasFlag(TILESTATE_HOUSE))
			                            {
										    for(uint32_t i = 0; i < tile->getThingCount(); ++i)
										    {
											    item = tile->__getThing(i)->getItem();
											    if(item && !item->isLoadedFromMap() && !item->isNotMoveable())
											    {
												    g_game.internalRemoveItem(item);
												    i--;
												    count++;
                                                }
											}
										}
									}
									else if(!g_config.getBoolean(ConfigManager::CLEAN_PROTECTION_ZONE))
									{
                                        if(!tile->hasFlag(TILESTATE_PROTECTIONZONE))
			                            {  
                                            for(uint32_t i = 0; i < tile->getThingCount(); ++i)
	                                        {
                                                item = tile->__getThing(i)->getItem();
                                                if(item && !item->isLoadedFromMap() && !item->isNotMoveable())
                                                {
                                                    g_game.internalRemoveItem(item);
                                                    i--;
                                                    count++;
                                                }
                                            }
					                    }
								    }
                                }
							}
						}
					}
				}
				leafE = leafE->stepEast();
			}
			else
				leafE = getLeaf(nx + FLOOR_SIZE, ny);
		}
		if(leafS)
			leafS = leafS->stepSouth();
		else
			leafS = getLeaf(0, ny + FLOOR_SIZE);
	}

	std::cout << "> Cleaning time: " << (OTSYS_TIME() - start) / (1000.) << " seconds, collected " << count << " item" << (count != 1 ? "s" : "") << "." << std::endl;
	g_game.setStateTime(OTSYS_TIME() + STATE_TIME);
	g_game.setGameState(GAME_STATE_NORMAL);
	return count;
}
 
Last edited:
Code:
[url]http://otland.net/f35/forgotten-server-lua-funcion-cleanmap-2725/[/url]

and do a npc:
PHP:
local hours = {1, 5, 9, 13, 17, 21} --save in 1,5,9 am and 1,5,9 pm
local message = "Map clean in 30 seconds,please pickup your items."
local timetoclean = 30
local typ = MESSAGE_STATUS_WARNING -- type of message
local lastSaid = 0
local oneHour = 60*60
function onThingMove(creature, thing, oldpos, oldstackpos)
end
function onCreatureAppear(creature)
end
function onCreatureDisappear(cid, pos)
end
function onCreatureTurn(creature)
end
function onCreatureSay(cid, type, msg)
end
function onCreatureChangeOutfit(creature)
end
function onThink()
local aktualnaGodzina = os.date("%H", os.time())

if(lastSaid+oneHour < os.time()) then
for i=1, #hours do
if(aktualnaGodzina+1 == hours[i]) then
local x = {}
broadcastMessage(message, typ)
addEvent(say,timetoclean*1000,x)
lastSaid = os.time()
end
end
end
end
function say(x)
cleanMap()
end
 
Code:
[url]http://otland.net/f35/forgotten-server-lua-funcion-cleanmap-2725/[/url]

and do a npc:
PHP:
local hours = {1, 5, 9, 13, 17, 21} --save in 1,5,9 am and 1,5,9 pm
local message = "Map clean in 30 seconds,please pickup your items."
local timetoclean = 30
local typ = MESSAGE_STATUS_WARNING -- type of message
local lastSaid = 0
local oneHour = 60*60
function onThingMove(creature, thing, oldpos, oldstackpos)
end
function onCreatureAppear(creature)
end
function onCreatureDisappear(cid, pos)
end
function onCreatureTurn(creature)
end
function onCreatureSay(cid, type, msg)
end
function onCreatureChangeOutfit(creature)
end
function onThink()
local aktualnaGodzina = os.date("%H", os.time())

if(lastSaid+oneHour < os.time()) then
for i=1, #hours do
if(aktualnaGodzina+1 == hours[i]) then
local x = {}
broadcastMessage(message, typ)
addEvent(say,timetoclean*1000,x)
lastSaid = os.time()
end
end
end
end
function say(x)
cleanMap()
end

Will try, thank you.
 
[29/10/2009 04:29:08] > Broadcasted message: "Map clean in 30 seconds,please pickup your items.".

[29/10/2009 04:29:38] Lua Script Error: [Npc interface]
[29/10/2009 04:29:38] in a timer event called from:
[29/10/2009 04:29:38] data/npc/scripts/cleaner.lua:onThink

[29/10/2009 04:29:38] data/npc/scripts/cleaner.lua:34: attempt to call global 'cleanMap' (a nil value)
[29/10/2009 04:29:38] stack traceback:
[29/10/2009 04:29:38] data/npc/scripts/cleaner.lua:34: in function <data/npc/scripts/cleaner.lua:33>
 
[29/10/2009 04:29:08] > Broadcasted message: "Map clean in 30 seconds,please pickup your items.".

[29/10/2009 04:29:38] Lua Script Error: [Npc interface]
[29/10/2009 04:29:38] in a timer event called from:
[29/10/2009 04:29:38] data/npc/scripts/cleaner.lua:onThink

[29/10/2009 04:29:38] data/npc/scripts/cleaner.lua:34: attempt to call global 'cleanMap' (a nil value)
[29/10/2009 04:29:38] stack traceback:
[29/10/2009 04:29:38] data/npc/scripts/cleaner.lua:34: in function <data/npc/scripts/cleaner.lua:33>

Have you added the Lua Function in sources ?
 
this is my script

configmanager.cpp
Code:
	m_confInteger[MAX_PLAYERS] = getGlobalNumber(L, "MaxPlayers");
#if defined HHB_AUTO_CLEAN
    m_confInteger[AUTOCLEAN_MINUTES] = getGlobalNumber(L, "AutocleanMinutes", 0);
#endif//HHB_AUTO_CLEAN
configmanager.h
Code:
		MAX_PLAYERS,
#if defined HHB_AUTO_CLEAN
        AUTOCLEAN_MINUTES,
#endif//HHB_AUTO_CLEAN
game.cpp
Code:
void Game::setWorldType(WorldType_t type)
{
	worldType = type;
}

#if defined HHB_AUTO_CLEAN
void Game::AutoClean()
{
    uint32_t cleanminutes=g_config.getNumber(ConfigManager::AUTOCLEAN_MINUTES);
/*
if (cleanminutes <= 0)
    {
     std::cout << "Autoclean disabled!" << std::endl;
     return;
    }
    std::cout << "Autoclean Initialized. ran every " << cleanminutes << " minutes!" << std::endl;
*/
    if(cleanminutes > 1)
    Scheduler::getScheduler().addEvent(createSchedulerTask(((cleanminutes-1)*60)*1000+1,
		boost::bind(&Game::anonymousBroadcastMessage, this, MSG_STATUS_WARNING, "Auto Clean in  1 minute - get your items off the floor now!")));
    Scheduler::getScheduler().addEvent(createSchedulerTask(((cleanminutes*60)-30)*1000+1,
		boost::bind(&Game::anonymousBroadcastMessage, this, MSG_STATUS_WARNING, "Auto Clean in  30 seconds - get your items off the floor now!!")));
    Scheduler::getScheduler().addEvent(createSchedulerTask(((cleanminutes*60)-10)*1000+1,
		boost::bind(&Game::anonymousBroadcastMessage, this, MSG_STATUS_WARNING, "Auto Clean in  10 seconds - get your items off the floor now!!!")));
    Scheduler::getScheduler().addEvent(createSchedulerTask(((cleanminutes)*60)*1000+1,
		boost::bind(&Game::AutoClean, this)));
}
#endif//HHB_AUTO_CLEAN
game.h
Code:
	int loadMap(std::string filename, std::string filekind);

	/**
	  * Get the map size - info purpose only
	  * \param width width of the map
	  * \param height height of the map
	  */
#if defined HHB_AUTO_CLEAN
void Game::AutoClean();
#endif//HHB_AUTO_CLEAN

otserv.cpp
Code:
	Server server(INADDR_ANY, port);
#if defined HHB_AUTO_CLEAN
if (g_config.getNumber(ConfigManager::AUTOCLEAN_MINUTES) <= 0)
std::cout << "Autoclean disabled!" << std::endl;
else
{
g_game.AutoClean();
std::cout << "Autoclean initialized every " << (double)g_config.getNumber(ConfigManager::AUTOCLEAN_MINUTES)/60 << " hours." << std::endl;
}
#endif//HHB_AUTO_CLEAN


config.lua
Code:
AutocleanMinutes=X
(0/false for disabled)
 
Last edited:
Back
Top