• Vote in May Mapping Competition to select a winner!
  • 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

Premium User
Premium User
Joined
Mar 23, 2008
Messages
101
Best answers
0
Reaction score
16
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:
OP
F

fish04k

Premium User
Premium User
Joined
Mar 23, 2008
Messages
101
Best answers
0
Reaction score
16
Been around for a long time just never felt knowledgeable enough to contribute. The positive words make me want to share more! Thank you!
 

~X~

LoA Founder
Premium User
Joined
Oct 30, 2007
Messages
363
Best answers
0
Reaction score
58
Location
USA
I done this step by step an nothing..? Maybe I done something incorrect..?
 
OP
F

fish04k

Premium User
Premium User
Joined
Mar 23, 2008
Messages
101
Best answers
0
Reaction score
16
I done this step by step an nothing..? Maybe I done something incorrect..?
Did you put the flags in the specific monsters xml file? It wont work if you dont add that.

It attacks once you deal dmg to the monster.
 

~X~

LoA Founder
Premium User
Joined
Oct 30, 2007
Messages
363
Best answers
0
Reaction score
58
Location
USA
Actually I did get it correct. Just wasn't watching what i was doing... Was kinda knee deep in code :)
 
Top