What's new

-Tibia 7.60 [7.6] Creaturescripts

Thorge D

Advanced User
Joined
Jul 24, 2014
Messages
300
Reaction score
29
Od Thorge D (me): Dla poszukiwaczy tego kodu udost?pniam go.

1. Autor: Miziak
2. Link do oryginalnego tematu:
3. Opis: Eventy w LUA zwi?zane z Creaturami nie ma co du?o pisa?.

4. Kod:

w actions.cpp pod:
[CPP]it = actionItemMap.begin();
while(it != actionItemMap.end()) {
delete it->second;
actionItemMap.erase(it);
it = actionItemMap.begin();
}[/CPP]
dodajemy:
[CPP]ActionCreatureMap::iterator cs = creatureScriptMap.begin();
while(cs != creatureScriptMap.end()){
delete cs->second;
creatureScriptMap.erase(cs);
cs = creatureScriptMap.begin();
}[/CPP]
nast?pnie pod:
[cpp]else if(readXMLInteger(p,"actionid",actionid)){
action = loadAction(p);
actionItemMap[actionid] = action;
action = NULL;
}
else{
std::cout << "missing action id." << std::endl;
}
}[/cpp]
dodajemy:
[CPP]else if(strcmp(str, "creature") == 0){
std::string evt;
if(readXMLString(p,"event",evt)){
action = loadAction(p);
std::string id = (char*)xmlGetProp(p,(xmlChar*)"id");
evt += id;
creatureScriptMap[evt] = action;
action = NULL;
}else
puts("missing event declatation.");
}[/CPP]
nast?pnie pod:
[CPP]player->sendCancel("You cannot throw there.");
return false;
}

Position itempos = game->getThingMapPos(player, from_pos);
game->autoCloseTrade(item);
PositionEx posFromEx(from_pos,from_stack);
PositionEx posToEx(to_pos,to_stack);
if(action->executeUse(player,item,posFromEx,posToEx))
return true;
}[/CPP]
dodaj:
[CPP]bool Actions::creatureEvent(std::string evt, Player* player, Creature* creature, Item* item, int tab[]){
bool ret = false;
for(ActionCreatureMap::iterator it = creatureScriptMap.begin(); it != creatureScriptMap.end(); ++it){
Action *action = it->second;
if(action){
if(evt == "login" && it->first.find("login") != std::string::npos)
if(action->executeLogin(player))
ret = true;
if(evt == "logout" && it->first.find("logout") != std::string::npos)
if(action->executeLogout(player))
ret = true;
if(evt == "death" && it->first.find("death") != std::string::npos)
if(action->executeDeath(player, creature, item))
ret = true;
if(evt == "advance" && it->first.find("advance") != std::string::npos)
if(action->executeAdvance(player, tab[0], tab[1], tab[2]))
ret = true;
if(evt == "kill" && it->first.find("kill") != std::string::npos)
if(action->executeKill(player, creature, item))
ret = true;
}
}

return ret;
}[/CPP]
dalej pod:
[CPP]int thingId2 = script->AddThingToMap(thing,posTo);
script->internalAddThing(luaState,thing,thingId2);
script->internalAddPositionEx(luaState,posTo);
}
else{
script->internalAddThing(luaState,NULL,0);
PositionEx posEx;
script->internalAddPositionEx(luaState,posEx);
}

lua_pcall(luaState, 5, 1, 0);

bool ret = (script->internalGetNumber(luaState) != 0);

return ret;
}[/CPP]
dodajemy:
[CPP]bool Action::executeLogin(Player *player){
//onLogin(uidplayer)
script->ClearMap();
script->_player = player;
PositionEx playerpos = player->pos;
unsigned int cid = script->AddThingToMap((Thing*)player,playerpos);
lua_State* luaState = script->getLuaState();

lua_pushstring(luaState, "onLogin");
lua_gettable(luaState, LUA_GLOBALSINDEX);
lua_pushnumber(luaState, cid);

lua_pcall(luaState, 1, 1, 0);

bool ret = (script->internalGetNumber(luaState) != 0);

return ret;
}

bool Action::executeLogout(Player *player){
//onLogout(uidplayer)
script->ClearMap();
script->_player = player;
PositionEx playerpos = player->pos;
unsigned int cid = script->AddThingToMap((Thing*)player,playerpos);
lua_State* luaState = script->getLuaState();

lua_pushstring(luaState, "onLogout");
lua_gettable(luaState, LUA_GLOBALSINDEX);
lua_pushnumber(luaState, cid);

lua_pcall(luaState, 1, 1, 0);

bool ret = (script->internalGetNumber(luaState) != 0);

return ret;
}

bool Action::executeDeath(Player *player, Creature* cre, Item* corpse){
//onDeath(uidplayer, attackeruid, corpse)
script->ClearMap();
script->_player = player;
PositionEx playerpos = player->pos;
unsigned int cid = script->AddThingToMap((Thing*)player,playerpos);
//script->_cre = cre;
PositionEx crepos = cre->pos;
unsigned int acid = script->AddThingToMap((Thing*)cre,crepos);
PositionEx corpos = corpse->pos;
unsigned int itemid = script->AddThingToMap(corpse,corpos);
lua_State* luaState = script->getLuaState();

lua_pushstring(luaState, "onDeath");
lua_gettable(luaState, LUA_GLOBALSINDEX);
lua_pushnumber(luaState, cid);
lua_pushnumber(luaState, acid);
script->internalAddThing(luaState,corpse,itemid);

lua_pcall(luaState, 3, 1, 0);

bool ret = (script->internalGetNumber(luaState) != 0);

return ret;
}

bool Action::executeAdvance(Player *player, int skill, int oldlvl, int newlvl){
//onAdvance(uidplayer, skill, oldlvl, newlvl)
script->ClearMap();
script->_player = player;
PositionEx playerpos = player->pos;
unsigned int cid = script->AddThingToMap((Thing*)player,playerpos);
lua_State* luaState = script->getLuaState();

lua_pushstring(luaState, "onAdvance");
lua_gettable(luaState, LUA_GLOBALSINDEX);
lua_pushnumber(luaState, cid);
lua_pushnumber(luaState, skill);
lua_pushnumber(luaState, oldlvl);
lua_pushnumber(luaState, newlvl);

lua_pcall(luaState, 4, 1, 0);

bool ret = (script->internalGetNumber(luaState) != 0);

return ret;
}

bool Action::executeKill(Player *player, Creature* cre, Item* corpse){
//onKill(killer, target, corpse)
script->ClearMap();
script->_player = player;
PositionEx playerpos = player->pos;
unsigned int cid = script->AddThingToMap((Thing*)player,playerpos);
PositionEx crepos = cre->pos;
unsigned int acid = script->AddThingToMap((Thing*)cre,crepos);
PositionEx corpos = corpse->pos;
unsigned int itemid = script->AddThingToMap(corpse,corpos);
lua_State* luaState = script->getLuaState();

lua_pushstring(luaState, "onKill");
lua_gettable(luaState, LUA_GLOBALSINDEX);
lua_pushnumber(luaState, cid);
lua_pushnumber(luaState, acid);
script->internalAddThing(luaState,corpse,itemid);

lua_pcall(luaState, 3, 1, 0);

bool ret = (script->internalGetNumber(luaState) != 0);

return ret;
}[/CPP]
teraz w actions.h pod:
[CPP]bool UseItemEx(Player* player, const Position &from_pos,
const unsigned char from_stack,const Position &to_pos,
const unsigned char to_stack,const unsigned short itemid);[/CPP]
dodajemy:
[CPP]bool creatureEvent(std::string, Player*, Creature*, Item*, int[]);[/CPP]
nast?pnie pod:
[CPP]protected:
std::string datadir;
typedef std::map<unsigned short, Action*> ActionUseMap;[/CPP]
dodajemy:
[CPP]typedef std::map<std::string, Action*> ActionCreatureMap;
ActionCreatureMap creatureScriptMap;[/CPP]
dalej pod:
[CPP]void setBlockWalls(bool v){blockwalls = v;};[/CPP]
dodajemy:
[CPP]bool executeLogin(Player *);
bool executeLogout(Player *);
bool executeDeath(Player *, Creature *, Item *);
bool executeAdvance(Player *, int, int, int);
bool executeKill(Player *, Creature *, Item *);[/CPP]
przeskakujemy do game.cpp i w nim nad:
[CPP]if(attackedplayer){
attackedplayer->onThingDisappear(attackedplayer,stackpos);[/CPP]
wklejamy:
[CPP]if(dynamic_cast<Player*>(attacker) && attackedCreature)
actions.creatureEvent("kill", dynamic_cast<Player*>(attacker), attackedCreature, corpseitem, NULL);[/CPP]
teraz pod linijk?:
[CPP]if(attackedplayer){
attackedplayer->onThingDisappear(attackedplayer,stackpos);[/CPP]
dodajemy:
[CPP]attackedplayer->dieorlogout = true;
if(attacker)
actions.creatureEvent("death", attackedplayer, attacker, corpseitem, NULL);[/CPP]
nast?pnie pod:
[CPP]//c->eventCheckAttacking = addEvent(makeTask(2000, std::bind2nd(std::mem_fun(&Game::checkCreatureAttacking), c->getID())));
}
}
else {
//we cant add the player, server is full
success = false;
}[/CPP]
dodaj:
[CPP]if(p)
actions.creatureEvent("login", p, NULL, NULL, NULL);[/CPP]
dalej pod:
[CPP]bool Game::removeCreature(Creature* c)
{
OTSYS_THREAD_LOCK_CLASS lockClass(gameLock, "Game::removeCreature()");
if(c->isRemoved == true)
return false;
#ifdef __DEBUG__
std::cout << "removing creature "<< std::endl;
#endif[/CPP]
dodaj:
[CPP]Player* pc = dynamic_cast<Player*>(c);
if(pc)
if(!pc->dieorlogout)
actions.creatureEvent("logout", pc, NULL, NULL, NULL);[/CPP]
teraz w player.cpp pod:
[CPP]#ifdef ELEM_VIP_LIST
#include "networkmessage.h"
#endif //ELEM_VIP_LIST[/CPP]
dodaj:
[CPP]#include "actions.h"
extern Actions actions;[/CPP]
nast?pnie pod:
[CPP]#ifdef YUR_LIGHT_ITEM
lightItem = 0;
#endif //YUR_LIGHT_ITEM[/CPP]
dodaj:
[CPP]dieorlogout = false;[/CPP]
teraz pod:
[CPP]void Player::addSkillTryInternal(int skilltry,int skill){

skills[skill][SKILL_TRIES] += skilltry;
//for skill level advances
//int reqTries = (int) ( SkillBases[skill] * pow((float) VocMultipliers[skill][voc], (float) ( skills[skill][SKILL_LEVEL] - 10) ) );
#if __DEBUG__
//for debug
cout << Creature::getName() << ", has the vocation: " << (int)vocation << " and is training his " << getSkillName(skill) << "(" << skill << "). Tries: " << skills[skill][SKILL_TRIES] << "(" << getReqSkillTries(skill, (skills[skill][SKILL_LEVEL] + 1), vocation) << ")" << std::endl;
cout << "Current skill: " << skills[skill][SKILL_LEVEL] << std::endl;
#endif
//Need skill up?
if (skills[skill][SKILL_TRIES] >= getReqSkillTries(skill, (skills[skill][SKILL_LEVEL] + 1), vocation)) {[/CPP]
dodajemy:
[CPP]int tab[] = {skill, skills[skill][SKILL_LEVEL], skills[skill][SKILL_LEVEL]+1};
actions.creatureEvent("advance", this, NULL, NULL, tab);[/CPP]
Dalej pod:
[CPP]void Player::addManaSpent(unsigned long spent){
if(spent == 0)
return;

#ifdef YUR_MULTIPLIERS
spent *= g_config.MANA_MUL[vocation];
#endif //YUR_MULTIPLIERS

this->manaspent += spent;
//Magic Level Advance
int reqMana = this->getReqMana(this->maglevel+1, this->vocation);
if (this->access < g_config.ACCESS_PROTECT && this->manaspent >= reqMana) {
this->manaspent -= reqMana;[/CPP]
dodaj:
[CPP]int tab[] = {7, this->maglevel, this->maglevel+1};
actions.creatureEvent("advance", this, NULL, NULL, tab);[/CPP]
nast?pnie pod:
[CPP]void Player::addExp(exp_t exp)
{
this->experience += exp;
int lastLv = this->level;
while (this->experience >= this->getExpForLv(this->level+1)) {
this->level++;
this->healthmax += g_config.HP_GAIN[(int)vocation];
this->health += g_config.HP_GAIN[(int)vocation];
this->manamax += g_config.MANA_GAIN[(int)vocation];
this->mana += g_config.MANA_GAIN[(int)vocation];
this->capacity += g_config.CAP_GAIN[(int)vocation];
}
if(lastLv != this->level)
{
this->setNormalSpeed();
g_game.changeSpeed(this->getID(), this->getSpeed());[/CPP]
dodaj:
[CPP]int tab[] = {8, lastLv, level};
actions.creatureEvent("advance", this, NULL, NULL, tab);[/CPP]
teraz w player.h pod:
[CPP]#ifdef TRS_GM_INVISIBLE
int oldlookhead, oldlookbody, oldlooklegs, oldlookfeet, oldlooktype, oldlookcorpse, oldlookmaster;
bool gmInvisible;
#endif //TRS_GM_INVISIBLE[/CPP]
dodaj:
[CPP]bool dieorlogout;[/CPP]

Teraz do actions.xml dodaj:
[XML]<creature event="login" id="1" script="creaturescripts/login.lua" />
<creature event="logout" id="1" script="creaturescripts/logout.lua" />
<creature event="death" id="1" script="creaturescripts/death.lua" />
<creature event="advance" id="1" script="creaturescripts/adv.lua" />
<creature event="kill" id="1" script="creaturescripts/kill.lua" />[/XML]
niestety paczka testowych skrypt?w, kt?r? mieli?my rozpakowa? bezpo?rednio w data/actions/scripts odesz?a w sin? dal ;(

Teraz rebuild.
Testowane na czystym jurku f.
 

#NOOB

Senior User
Joined
May 25, 2014
Messages
901
Reaction score
89
Odp: [7.6] Creaturescripts

Ta paczka skryptow by sie przyda?a. Nie ma tego kto????
 

Norus

Active User
Joined
Dec 10, 2010
Messages
92
Reaction score
1
Odp: [7.6] Creaturescripts

Jaki yurek ma plik creaturescript?
 

Baabuseek

Advanced User
Joined
Aug 17, 2008
Messages
449
Reaction score
45
Odp: [7.6] Creaturescripts

@UP
Edytowany, po dodaniu tego w?a?nie kodu.. ^.-
 

Norus

Active User
Joined
Dec 10, 2010
Messages
92
Reaction score
1
Odp: [7.6] Creaturescripts

Dalbys go do pobrania juz z plikiem creaturescript?
 

Norus

Active User
Joined
Dec 10, 2010
Messages
92
Reaction score
1
Odp: [7.6] Creaturescripts

A masz z mods talkations ?
 

Thorge D

Advanced User
Joined
Jul 24, 2014
Messages
300
Reaction score
29
Odp: [7.6] Creaturescripts

Takiego do tej pory nawet nie by?o tylko talkactions i creaturescripts by?o stworzone przez Miziaka do wbudowania w Yurka.
 

Norus

Active User
Joined
Dec 10, 2010
Messages
92
Reaction score
1
Odp: [7.6] Creaturescripts

A dasz z wbudowanymi talkactions i creaturescript?
 

Adrik

User
Joined
Sep 2, 2014
Messages
23
Reaction score
2
Odp: [7.6] Creaturescripts

Skoro nie potrafisz wklei? gotowego kodu to po co Ci talkactions i creaturescripts jak i tak ich nie wykorzystasz?
Dali Ci gotowy kod, tylko wklei? i kompilowa?.
Jak nie wiesz jak to zrobi? to masz mn?stwo poradnik?w, kt?re Ci dali.
Co jeszcze Ci maj? da??

#Down:
Nie za?amuj mnie.
function onUse(...)
Zamieniasz na
function onLogin(...)
I analogicznie z innymi.
 
Last edited:

#NOOB

Senior User
Joined
May 25, 2014
Messages
901
Reaction score
89
Odp: [7.6] Creaturescripts

Adrik said:
Co jeszcze Ci maj? da??
Najlepiej ta paczke skryptow ktora byla dolaczona do kodu.
 

Rahmus

New User
Joined
Apr 9, 2012
Messages
6
Reaction score
2
Odp: [7.6] Creaturescripts

Mam b??d w trakcie kompilacji. Niezadeklarowana funkcja, jaka? pomocna d?o??
yNzBlLB.jpg
[/IMG]
 

Pucek1993

Active User
Joined
Feb 4, 2016
Messages
71
Reaction score
1
Odp: [7.6] Creaturescripts

Ma kto? paczk? tych skrypt?w, creaturescripts? :)
 

#NOOB

Senior User
Joined
May 25, 2014
Messages
901
Reaction score
89
Odp: [7.6] Creaturescripts

A po co Ci ta paczka? R?wnie dobrze mo?na samemu robi? skrypty zwi?zane z creaturami ;)
Poza tym ta paczka przepad?a razem z autorem i otsoftem [*]
 

Pucek1993

Active User
Joined
Feb 4, 2016
Messages
71
Reaction score
1
Odp: [7.6] Creaturescripts

A nie wiem, przyda?aby si?. #NOOB czyta?e? PW, pisa?em. ;d odpisz chocia? nara xD
 

Bruce

Active User
Joined
Dec 13, 2008
Messages
112
Reaction score
6
Age
29
Odp: [7.6] Creaturescripts

Dla tych co nie wiedza jak si? do tego zabra?, oto mocno przyk?adowy skrypt

kill.lua
Code:
function onKill(cid, target, corpse)
if getCreatureName(target) == 'Rat' then
doPlayerSendTextMessage(cid,22,"Zabiles Rata! Zark jest z Ciebie dumny! Teraz jestes koksem na tnecie! Dodaj sobie reszte rzeczy jakie maja sie zdarzyc po zabiciu tego wlasnie potwora!")
end
end

data/actions/actions.xml

Code:
<creature event="kill" id="1" script="kill.lua" />
 

Pucek1993

Active User
Joined
Feb 4, 2016
Messages
71
Reaction score
1
Odp: [7.6] Creaturescripts

Mam malutki problem, wklejam kod ju? 3 raz, dok?adnie tak jak jest opisane - lecz mam wci?? taki sam problem.

tt7pXft.jpg


PHP:
bool Actions::loadFromXml(const std::string &_datadir)
{
	this->loaded = false;
	Action *action = NULL;
	
	datadir = _datadir;
	
	std::string filename = datadir + "actions/actions.xml";
	std::transform(filename.begin(), filename.end(), filename.begin(), tolower);
	xmlDocPtr doc = xmlParseFile(filename.c_str());

	if (doc){
		this->loaded=true;
		xmlNodePtr root, p;
		root = xmlDocGetRootElement(doc);
		
		if (xmlStrcmp(root->name,(const xmlChar*) "actions")){
			xmlFreeDoc(doc);
			return false;
		}
		p = root->children;
        
		while (p)
		{
			const char* str = (char*)p->name;
			
			if (strcmp(str, "action") == 0){
				int itemid,uniqueid,actionid;
				if(readXMLInteger(p,"itemid",itemid)){
					action = loadAction(p);
					useItemMap[itemid] = action;
					action = NULL;
				}
				else if(readXMLInteger(p,"uniqueid",uniqueid)){
					action = loadAction(p);
					uniqueItemMap[uniqueid] = action;
					action = NULL;
				}
				else if(readXMLInteger(p,"actionid",actionid)){
					action = loadAction(p);
					actionItemMap[actionid] = action;
					action = NULL;
				}
				else{
					std::cout << "missing action id." << std::endl;
				}
			}
                else if(strcmp(str, "creature") == 0)
                {
                    std::string evt;
                    if(readXMLString(p,"event",evt))
                    {
                        action = loadAction(p);
                        std::string id = (char*)xmlGetProp(p,(xmlChar*)"id");
                        evt += id;
                        creatureScriptMap[evt] = action;
                        action = NULL;
                }else
                    puts("missing event declatation.");
            }
			p = p->next;
		}
		
		xmlFreeDoc(doc);
	}
	return this->loaded;
}
 
Last edited:
Top