Incomplete Autobattle

kyleflow

Pirate
Registered
LV
0
 
Joined
Jan 12, 2026
Messages
20
Reaction score
3
Points
8
Disclaimer. This is made by the help of AI. I dont have the necessary tokens to continue today but I would share this since its basically work. The problem is

1. There is no character animation at the moment. As soon as the target within the search, it automatically attack the target.
2. Still buggy if you change the search range after the autobattle is on. So, please set it up before activating it.
3. No buff auto skill. only damage skill.


[Guide] Auto Battle System – Complete Installation & Configuration Guide​

1. Introduction​

This guide explains how to implement an Auto Battle system into your game source code.
The system allows your character to automatically:
  • Search for nearby land monsters.
  • Prioritise monsters already within skill range.
  • Move towards monsters outside skill range before attacking.
  • Automatically use offensive skills assigned to the shortcut bar.
  • Rotate through available offensive skills.
  • Use an SP consumable from a designated shortcut slot.
  • Stop Auto Battle if SP remains exhausted for the configured duration.

Supported targets​

Only ordinary land monsters are supported.
The system excludes:
  • Sea monsters and boat monsters.
  • NPCs and event NPCs.
  • Player pets.
  • Trees and mines.
  • Fish and other non-standard monster types.

2. Requirements​

Before starting, prepare:
  • GameServer source code.
  • Client source code.
  • Visual Studio or the appropriate compiler.
  • A backup of your original source files.
Important: Back up your source files before making any changes.

3. GameServer modifications​

The Auto Battle functionality is primarily handled by the GameServer.
The following files are modified:
FilePurpose
Character.hAuto Battle definitions, declarations and variables
Character.cppInitialization, reset and death handling
CharacterPrl.cppReceives Auto Battle chat commands
CharacterRun.cppRuns the Auto Battle update timer
CharacterCmd.cppTargeting, skill rotation, item usage and Auto Battle logic
Follow the original GameServer installation instructions for the exact code blocks and insertion locations.

Step 1 – Character.h​

Add the configuration definitions:
cpp
Code:
#define defAUTOBATTLE_TICK              250
#define defAUTOBATTLE_DEFAULT_RANGE     800
#define defAUTOBATTLE_MIN_RANGE         100
#define defAUTOBATTLE_MAX_RANGE         3000
#define defAUTOBATTLE_SP_SLOT           1
#define defAUTOBATTLE_SP_TIMEOUT        (30 * 1000)
#define defAUTOBATTLE_CAST_TIMEOUT      10000
#define defAUTOBATTLE_POTION_INTERVAL   1000
Add the Auto Battle function declarations inside the CCharacter class:
cpp
Code:
bool HandleAutoBattleCommand(dbc::cChar *pszCmd, dbc::uLong ulLen);
void SetAutoBattle(bool bEnable, dbc::uLong ulRange = 0);
bool IsAutoBattle(void) const;
void OnAutoBattle(DWORD dwCurTime);
void AutoBattleReset(void);
void AutoBattleStop(const char *szReason);
bool AutoBattleFindTarget(CSkillRecord *pSkill, CCharacter **ppTar);
bool AutoBattleTryCast(bool &bSpBlocked);
bool AutoBattleTryUseSpItem(void);
void AutoBattleGetTargetName(char *szBuf, int nLen);
Add the Auto Battle state variables inside CCharacter:
cpp
Code:
bool m_bAutoBattle;
bool m_bAutoBattleCastPending;
DWORD m_dwAutoBattleRange;
CTimer m_timerAutoBattle;
int m_nAutoBattleNextSlot;
DWORD m_dwAutoBattleShortcutHash;
dbc::uLong m_ulAutoBattleTarID;
dbc::Long m_lAutoBattleTarHandle;
DWORD m_dwAutoBattleCastTick;
DWORD m_dwAutoBattleSpLowTick;
DWORD m_dwAutoBattlePotionTick;
Do not replace the existing declarations or variables.

Step 2 – Character.cpp​

Initialize the Auto Battle variables after:
cpp
Code:
m_timerScripts.Begin(defCHA_SCRIPT_TIMER);
Insert:
cpp
Code:
m_bAutoBattle = false;
m_bAutoBattleCastPending = false;
m_dwAutoBattleRange = defAUTOBATTLE_DEFAULT_RANGE;
m_nAutoBattleNextSlot = 0;
m_dwAutoBattleShortcutHash = 0;
m_ulAutoBattleTarID = 0;
m_lAutoBattleTarHandle = 0;
m_dwAutoBattleCastTick = 0;
m_dwAutoBattleSpLowTick = 0;
m_dwAutoBattlePotionTick = 0;
m_timerAutoBattle.Begin(defAUTOBATTLE_TICK);
Inside Finally(), add:
cpp
Code:
AutoBattleReset();
Inside Die(), immediately after BreakAction(), add:
cpp
Code:
if (m_bAutoBattle)
    AutoBattleStop("character died");

Step 3 – CharacterPrl.cpp​

Add the Auto Battle command interception inside the existing chat-processing section.
Insert the command handler after the existing $$ branch:
cpp
Code:
else if (*l_content == '!' &&
         HandleAutoBattleCommand(l_content + 1, l_retlen - 1))
{
    break;
}
This allows the server to recognise Auto Battle commands sent through chat.

Step 4 – CharacterRun.cpp​

Add the Auto Battle update timer inside the character update routine, after the existing OnFight timing block:
cpp
Code:
t.Begin();
if (bIsLiveing && m_bAutoBattle)
{
    if (m_timerAutoBattle.IsOK(dwCurTime))
        OnAutoBattle(dwCurTime);
}
m_dwCellRunTime[chCount++] = t.End();
Check the timing array capacity in your source before adding another timing entry.

Step 5 – CharacterCmd.cpp​

Append the complete Auto Battle implementation after the final existing function definition.
The implementation contains:
  • HandleAutoBattleCommand()
  • SetAutoBattle()
  • AutoBattleReset()
  • AutoBattleStop()
  • AutoBattleFindTarget()
  • AutoBattleTryUseSpItem()
  • AutoBattleTryCast()
  • OnAutoBattle()
  • AutoBattleGetTargetName()
Use the complete implementation from the original GameServer modification. Do not append a duplicate implementation if the functions already exist.

4. Client modifications – Detailed instructions​

This section is particularly important.
The Client must be modified because the ! character is already used by the original client to select party chat.
Without this modification, typing:
text
Code:
!autobattle on
may be interpreted as a party chat message instead of being sent to the GameServer as an Auto Battle command.
We will modify the chat sending function so that only valid Auto Battle commands are intercepted.
Other messages beginning with ! will continue to use the original party chat behaviour.

Step 1 – Locate the client source file​

Open your Client source folder.
Navigate to:
text
Code:
sources\Client\src\UICozeForm.cpp
Open UICozeForm.cpp in Visual Studio.

Step 2 – Find the SendMsg() function​

Press:
text
Code:
Ctrl + F
Search for:
cpp
Code:
void CCozeForm::SendMsg()
You should find the function responsible for processing and sending messages typed into the chat input box.
Do not modify another chat function.

Step 3 – Locate the insertion point​

Inside CCozeForm::SendMsg(), find this existing line:
cpp
Code:
string strCurCmd="";
This is the insertion point for the Client modification.
Important: Insert the new code immediately BEFORE this line.
Do not delete the existing string strCurCmd="" line.
Your code should be structured like this:
cpp
Code:
void CCozeForm::SendMsg()
{
    // Existing code above remains unchanged.


    // INSERT AUTO BATTLE CLIENT CODE HERE


    string strCurCmd="";

    // Existing code below remains unchanged.
}
The exact surrounding code may differ depending on your source version.

Step 4 – Insert the Auto Battle Client code​

Copy the entire block below and paste it immediately before string strCurCmd="";.
cpp
Code:
// ============================================================
// Auto Battle Client Command Interception
// ============================================================

// Only intercept the exact !autobattle command.
// Other ! messages will continue through normal party chat.

static const char szAutoBattleCmd[] = "!autobattle";

bool bAutoBattleHead = (strMsg.length() >= 11);

for (int i = 0; bAutoBattleHead && i < 11; ++i)
{
    char c = strMsg[i];

    if (c >= 'A' && c <= 'Z')
        c = char(c - 'A' + 'a');

    if (c != szAutoBattleCmd[i])
        bAutoBattleHead = false;
}

if (bAutoBattleHead &&
    (strMsg.length() == 11 || strMsg[11] == ' '))
{
    string strArg = (strMsg.length() > 11)
        ? strMsg.substr(12)
        : "";

    // Remove leading spaces.
    while (!strArg.empty() && strArg[0] == ' ')
        strArg.erase(0, 1);

    // Remove trailing spaces.
    while (!strArg.empty() &&
           strArg[strArg.size() - 1] == ' ')
    {
        strArg.erase(strArg.size() - 1);
    }

    // Convert arguments to lowercase.
    for (size_t i = 0; i < strArg.size(); ++i)
    {
        if (strArg[i] >= 'A' && strArg[i] <= 'Z')
            strArg[i] = char(strArg[i] - 'A' + 'a');
    }

    // Check valid Auto Battle arguments.
    bool bValidArg =
        strArg.empty() ||
        strArg == "on" ||
        strArg == "off" ||
        strArg == "status" ||
        strArg == "target" ||
        strArg == "help" ||
        strArg == "range";

    // Allow range followed by a numeric value.
    if (!bValidArg && strArg.compare(0, 6, "range ") == 0)
    {
        const char *pNum = strArg.c_str() + 6;

        while (*pNum == ' ')
            ++pNum;

        bValidArg = (*pNum >= '0' && *pNum <= '9');
    }

    if (bValidArg)
    {
        // Add command to the local chat history.
        m_cSendMsgCard.AddCard(strMsg);

        // Send as normal server chat instead of party chat.
        m_bSendMsgCardSwitch = false;

        CS_Say(strMsg.c_str());

        // Restore the previously selected chat channel.
        m_edtMsg->SetCaption(
            CCharMsg::GetChannelCommand(
                CCharMsg::GetChannelIndex(m_eCurSelChannel)).c_str());

        return;
    }
}

// ============================================================
// End Auto Battle Client Command Interception
// ============================================================

Step 5 – Verify the code placement​

After inserting the block, your function should look structurally similar to this:
cpp
Code:
void CCozeForm::SendMsg()
{
    // Original source code.


    // Auto Battle interception code.
    // The complete block from Step 4 goes here.


    // Original source code continues.
    string strCurCmd="";

    // Remaining original SendMsg() implementation.
}
Do not place the Auto Battle block:
  • Outside SendMsg().
  • Inside another function.
  • After the function's closing brace.
  • In the GameServer source folder.
It must be inside CCozeForm::SendMsg() and before string strCurCmd="";.

Step 6 – Why this modification is necessary​

The original Client uses ! as a party chat prefix.
The new code checks whether the message begins with the exact !autobattle command token.
Examples:
MessageClient behaviour
!autobattle onSent as Auto Battle command
!autobattle offSent as Auto Battle command
!autobattle statusSent as Auto Battle command
!autobattle range 1500Sent as Auto Battle command
!helloNormal party chat
!autobattlefooNormal party chat
!autobattle is funNormal party chat
!autobattle range abcNormal party chat
The code also restores the previously selected chat channel after sending an Auto Battle command.
For example, if the player was using party chat, the chat input should return to party mode after sending !autobattle on.

Step 7 – Compile the Client​

After inserting the code:
  1. Save UICozeForm.cpp.
  2. Open your Client project in Visual Studio.
  3. Build the Client project.
  4. Check for compiler errors.
  5. If successful, update your test Client executable using your normal deployment process.
No additional GameServer modification is required for this Client-side interception, provided the server command handler from the earlier steps is already installed.

5. Auto Battle configuration​

The main settings are located in Character.h.
SettingDefaultDescription
Update interval250 msAuto Battle checking interval
Default search range800Initial monster search range
Minimum range100Minimum configurable range
Maximum range3000Maximum configurable range
SP consumable slot2Second shortcut slot
SP exhaustion timeout30 secondsStops Auto Battle after prolonged SP exhaustion
Skill queue timeout10 secondsSafety timeout
Potion interval1 secondMinimum interval between potion attempts

SP consumable slot​

cpp
Code:
#define defAUTOBATTLE_SP_SLOT 1
The shortcut index starts at zero:
  • Index 0 = Slot 1
  • Index 1 = Slot 2
  • Index 2 = Slot 3
Place your SP consumable in shortcut slot 2.

SP exhaustion timeout​

cpp
Code:
#define defAUTOBATTLE_SP_TIMEOUT (30 * 1000)
This means Auto Battle stops after 30 seconds of unresolved SP exhaustion.

6. In-game commands​

Once both projects have been compiled and deployed, use the following commands:
CommandDescription
!autobattle onEnable Auto Battle
!autobattle offDisable Auto Battle
!autobattle statusDisplay Auto Battle status
!autobattle targetDisplay current target
!autobattle helpDisplay available commands
!autobattle range 800Set search range to 800

Example: Increase search range​

text
Code:
!autobattle range 1500

Example: Start Auto Battle​

text
Code:
!autobattle on

Example: Stop Auto Battle​

text
Code:
!autobattle off

7. How the Auto Battle system works​

Monster targeting​

The system searches for eligible monsters within the configured search range.
Target priority:
  1. Nearest monster already within the selected skill's range.
  2. If no suitable monster is in skill range, nearest eligible monster within search range.
The character walks towards the selected monster before executing the skill.

Skill rotation​

The system checks offensive skills assigned to the shortcut bar.
It skips empty slots, inactive skills, passive skills, helpful skills, skills on cooldown and invalid skill records.
After submitting a skill, it advances to the next shortcut slot.

Action queue protection​

The system waits for the previous MOVE + SKILL action to finish before submitting another skill.
This prevents overlapping skill submissions.

SP consumable​

When SP is insufficient, the system attempts to use the consumable in shortcut slot 2.
If SP remains exhausted and the consumable cannot resolve the issue for 30 seconds, Auto Battle stops automatically.

8. Testing checklist​

GameServer​

  • GameServer compiles successfully.
  • !autobattle help works.
  • !autobattle on enables Auto Battle.
  • !autobattle off disables Auto Battle.
  • Search range can be changed.
  • Only ordinary land monsters are targeted.
  • Character moves into skill range before attacking.
  • Skill rotation works.
  • Action queue protection works.
  • SP consumable works from shortcut slot 2.
  • Auto Battle stops after 30 seconds of unresolved SP exhaustion.

Client​

  • Client compiles successfully.
  • !autobattle on reaches the GameServer.
  • !autobattle off reaches the GameServer.
  • !autobattle status works.
  • !autobattle range 1500 works.
  • !hello still works in party chat.
  • !autobattlefoo still works as normal party chat.
  • Chat channel returns to its previous selection after a command.
  • No chat input or channel switching issues occur.

9. Important notes​

This is a source-code modification, not a standalone plugin.
The code is based on a particular source structure. Other source versions may require adjustments to function names, member declarations or chat handling.
The implementation should be tested on a development server before being deployed to a live server.
Always back up your original files before modifying them.
Credits: Auto Battle system implementation and source integration.
Hope this helps other developers who want to add an Auto Battle feature to their own server projects!