Jump to content

Game Source Code - where to call void methods to spawn mobs, move mobs and others - in game source side -?


Recommended Posts

  • Premium
Posted (edited)

Hello!

Lets say I have the follow scenario:
At server startup:
I want to spawn two mobs, as an example function(correctness does not matter)

void SpawnMobs(DWORD mob_vnum, int count, int map_index, int x, int y)
{
    for (int i = 0; i < count; ++i)
    {
        LPCHARACTER mob = CHARACTER_MANAGER::instance().SpawnMob(mob_vnum, map_index, x, y, 0, false, -1);
        if (!mob)
            sys_err("Failed to spawn mob VNUM %d on map %d at (%d, %d)", mob_vnum, map_index, x, y);
    }
}

 

And at a given date-time, to make the mobs move to a specific location(Again function correctness is probably wrong, but this is just a POCE)

void MoveMobTo(DWORD mob_vid, int target_x, int target_y)
{
    using namespace std::chrono;

    //April 18, 2024, at 18:00
    std::tm scheduled_time = {};
    scheduled_time.tm_year = 2024
    scheduled_time.tm_mon = 4 - 1;        
    scheduled_time.tm_mday = 18;       
    scheduled_time.tm_hour = 18;          
    scheduled_time.tm_min = 0;          
    scheduled_time.tm_sec = 0;           

    auto scheduled_time_t = std::mktime(&scheduled_time);
    system_clock::time_point scheduled_tp = system_clock::from_time_t(scheduled_time_t);

    // Get current time
    system_clock::time_point now = system_clock::now();

    // Check if the current time matches the scheduled time
    if (now == scheduled_tp)
    {
        LPCHARACTER mob = CHARACTER_MANAGER::instance().Find(mob_vid);
        if (mob)
        {
            mob->Goto(target_x, target_y);
            sys_log(0, "Mob with VID %d moved to (%d, %d) as scheduled", mob_vid, target_x, target_y);
        }
        else
        {
            sys_err("Failed to find mob with VID %d to move", mob_vid);
        }
    }
    else
    {
        sys_log(0, "MoveMobTo called, but it is not the scheduled time yet.");
    }
}

I have two main questions
1. !Where should i be calling these functions within the source game, in main seems like it might not be a good idea
2. Checking time constantly until desired time is true, seems quite tricky, a while true surely is not good, if u have any basic suggestions it would be more then welcomed 😄

Basically my goal is to have mobs spawn, if not already spawned, move them at a specific hour to a exact spot on the map, make em fight each other, while fighting pc can't attack, after pc can attack the surviver, but not instantly, spawn another boss, keep it there for a specific time, if not dead, despawn it

Realistically, if i'd know where to call my functions with the instructions and how to not use while loops to check for specific  states, it would be enough 🙂

Thank you in advance!


 

Edited by astroNOT
Link to comment
Share on other sites

  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

  • Premium

Can confirm that adding any instruction prior to while idle, seems to not give enough time for the maps & mobs to load

	try
	{
		// Hardcoded values
		const DWORD dwVnum = 101;
		const int count = 10;
		const bool isAggressive = false;
		const int iMapIndex = 352;
		const int iMapX = 360;
		const int iMapY = 360;

		// Assume SECTREE_MANAGER and CHARACTER_MANAGER are properly initialized and available
		PIXEL_POSITION pos;
		if (!SECTREE_MANAGER::instance().GetMapBasePositionByMapIndex(iMapIndex, pos))
		{
			sys_log(0, "PPPPPPQQQ Error: Cannot find base position in this map %d", iMapIndex);
		}

		const CMob *pMonster = CMobManager::instance().Get(dwVnum);
		if (pMonster == NULL)
		{
			sys_log(0, "PPPPPPQQQ Error: No mob data for VNUM %d", dwVnum);
		}

		size_t SpawnCount = 0;
		for (size_t i = 0; i < count; ++i)
		{
			LPCHARACTER pSpawnMonster = CHARACTER_MANAGER::instance().SpawnMobRange(
				dwVnum,
				iMapIndex,
				pos.x - number(5, 5) + (iMapX * 100),
				pos.y - number(5, 5) + (iMapY * 100),
				pos.x + number(5, 5) + (iMapX * 100),
				pos.y + number(5, 5) + (iMapY * 100),
				false,
				pMonster->m_table.bType == CHAR_TYPE_MONSTER,
				isAggressive);
			if (pSpawnMonster != NULL)
			{

				SpawnCount++;
			}
		}

		sys_log(0, "Spawned %u monsters successfully.", SpawnCount);
	}
	catch (const std::exception &e)
	{
		sys_log(0, "An exception occurred: %s", e.what());
	}

	while (idle())
		;

What it logs is:


./srv1/chan/ch1/core1/syslog:Apr 17 19:23:11 :: PPPPPPQQQ Error: Cannot find base position in this map 352
./srv1/chan/ch1/core1/syslog:Apr 17 19:23:11 :: PPPPPPQQQ Error: No mob data for VNUM 101

Trial and error continues 😄

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.