• 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!

[TFS] Lua funcion: cleanMap()!

slaw

Software Developer
Joined
Aug 27, 2007
Messages
3,667
Solutions
125
Reaction score
1,114
Location
Germany
GitHub
slawkens
This code will add to your server new lua function which will clean your map.

Tested only on TFS.

luascript.cpp
after
Code:
	//savePlayers()
	lua_register(m_luaState, "savePlayers", LuaScriptInterface::luaSavePlayers);
add
Code:
	//cleanmap()
	lua_register(m_luaState, "cleanMap", LuaScriptInterface::luaCleanMap);

after
Code:
int32_t LuaScriptInterface::luaSavePlayers(lua_State* L)
{
	g_game.saveData(false);
	return 1;
}
add
Code:
int32_t LuaScriptInterface::luaCleanMap(lua_State* L)
{
	g_game.getMap()->clean();
	return 1;
}

luascript.h
after
Code:
	static int32_t luaSavePlayers(lua_State* L);
add
Code:
	static int32_t luaCleanMap(lua_State* L);

Now you can use in all .lua scripts funcion: cleanMap() - it work same as /clean, it will show in console how much items was cleaned.

Later i will give an example for automatic map clean after every xx minutes.
 
Yea, thats true :p

But it much gives. Becouse i have made for it talkaction that will make auto clean with warning messages.
 
Map.cpp

After:
Code:
Floor* QTreeLeafNode::createFloor(uint32_t z)

Add:
Code:
uint32_t Map::clean()
{
	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(!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;
	return count;
}

Map.h

After:
Code:
		Tile* getTile(const Position& pos);

Add:
Code:
		uint32_t clean();
 

Similar threads

Back
Top