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

Passive Monsters (Attack when attacked)

fish04k

Member
Joined
Mar 23, 2008
Messages
102
Reaction score
19
TESTED ON A FRESH TFS 1.1
Not sure what it would be compatible with.

Hello there! I just figured this out and thought i'd share it since I could not find it anywhere.

*Disclaimer - I am very new to this. This is my first ever source edit. If there could of been an easier way please feel free to share it with me. Enjoy!

Find in luascript.cpp
C++:
registerMethod("MonsterType", "isHostile", LuaScriptInterface::luaMonsterTypeIsHostile);
Place Below
C++:
registerMethod("MonsterType", "isHostileOnAttack", LuaScriptInterface::luaMonsterTypeIsHostileOnAttack);

Find in luascript.cpp
C++:
int LuaScriptInterface::luaMonsterTypeIsHostile(lua_State* L)
{
    // monsterType:isHostile()
    MonsterType* monsterType = getUserdata<MonsterType>(L, 1);
    if (monsterType) {
        pushBoolean(L, monsterType->isHostile);
    } else {
        lua_pushnil(L);
    }
    return 1;
}
Place Below
C++:
int LuaScriptInterface::luaMonsterTypeIsHostileOnAttack(lua_State* L)
{
    // monsterType:isHostileOnAttack()
    MonsterType* monsterType = getUserdata<MonsterType>(L, 1);
    if (monsterType) {
        pushBoolean(L, monsterType->isHostileOnAttack);
    } else {
        lua_pushnil(L);
    }
    return 1;
}

Find in luascript.h
C++:
static int luaMonsterTypeIsHostile(lua_State* L);
Place Below
C++:
static int luaMonsterTypeIsHostileOnAttack(lua_State* L);

Find in monster.cpp
C++:
    if (isHostile() || isSummon()) {
        if (setAttackedCreature(creature) && !isSummon()) {
            g_dispatcher.addTask(createTask(std::bind(&Game::checkCreatureAttack, &g_game, getID())));
        }
    }
    return setFollowCreature(creature);
}
REPLACE WITH
C++:
    if (isHostileOnAttack() && getHealth() >= getMaxHealth()) {
            return false;
    } else if (isHostile() || isSummon()) {
        if (setAttackedCreature(creature) && !isSummon()) {
            g_dispatcher.addTask(createTask(std::bind(&Game::checkCreatureAttack, &g_game, getID())));
        }
    }
    return setFollowCreature(creature);
}


Find in monster.h
C++:
        bool isHostile() const {
            return mType->isHostile;
        }
Place Below
C++:
        bool isHostileOnAttack() const {
            return mType->isHostileOnAttack;
        }


Find in monsters.cpp
C++:
    isHostile = true;
Place Below
C++:
    isHostileOnAttack = false;

Find
C++:
            } else if (strcasecmp(attrName, "hostile") == 0) {
                mType->isHostile = attr.as_bool();
Place Below
C++:
            } else if (strcasecmp(attrName, "hostileonattack") == 0) {
                mType->isHostileOnAttack = attr.as_bool();

Find in monsters.h
C++:
        bool isHostile;
Place Below
C++:
        bool isHostileOnAttack;

Edit any monster's xml file that you want to only attack when you attack them. Make sure these two flags are in there.
XML:
        <flag hostile="1"/>
        <flag hostileonattack="1"/>
 
Last edited by a moderator:
I done this step by step an nothing..? Maybe I done something incorrect..?
 
Actually I did get it correct. Just wasn't watching what i was doing... Was kinda knee deep in code :)
 
Back
Top