2008-07-30 20:51:19 +03:00
# include "CScriptCallback.h"
# include "../lib/Connection.h"
# include "CVCMIServer.h"
# include "CGameHandler.h"
# include "../CGameState.h"
# include "../map.h"
2008-09-29 00:01:49 +03:00
# include "../hch/CArtHandler.h"
2008-07-30 20:51:19 +03:00
# include "../hch/CObjectHandler.h"
# include "../hch/CTownHandler.h"
# include "../hch/CHeroHandler.h"
# include "../lib/NetPacks.h"
2008-08-04 18:56:36 +03:00
# include "../lib/VCMI_Lib.h"
# include <boost/bind.hpp>
2008-07-30 20:51:19 +03:00
# include <boost/foreach.hpp>
# include <boost/thread.hpp>
2008-08-13 03:44:31 +03:00
2008-07-30 20:51:19 +03:00
CScriptCallback : : CScriptCallback ( void )
{
}
CScriptCallback : : ~ CScriptCallback ( void )
{
}
void CScriptCallback : : setBlockVis ( int objid , bool bv )
{
SetObjectProperty sop ( objid , 2 , bv ) ;
gh - > sendAndApply ( & sop ) ;
}
2008-08-13 07:41:11 +03:00
void CScriptCallback : : removeObject ( int objid )
{
RemoveObject ro ;
ro . id = objid ;
gh - > sendAndApply ( & ro ) ;
}
2008-09-20 21:30:37 +03:00
void CScriptCallback : : setAmount ( int objid , ui32 val )
{
SetObjectProperty sop ( objid , 3 , val ) ;
gh - > sendAndApply ( & sop ) ;
}
2008-09-23 13:58:54 +03:00
void CScriptCallback : : moveHero ( int hid , int3 pos , bool instant )
{
if ( ! instant )
{
tlog1 < < " Not supported call to CScriptCallback::moveHero \n " ;
return ;
}
CGHeroInstance * h = const_cast < CGHeroInstance * > ( getHero ( hid ) ) ;
//check if destination tile is free
BOOST_FOREACH ( CGObjectInstance * obj , gh - > gs - > map - > terrain [ pos . x - 1 ] [ pos . y ] [ pos . z ] . blockingObjects )
{
if ( obj - > ID = = 34 )
{
if ( obj - > tempOwner = = h - > tempOwner )
return ; //TODO: exchange
//TODO: check for ally
CGHeroInstance * dh = static_cast < CGHeroInstance * > ( obj ) ;
startBattle ( & h - > army , & dh - > army , pos , h , dh , 0 ) ;
return ;
}
}
TryMoveHero tmh ;
tmh . start = h - > pos ;
tmh . end = pos ;
tmh . id = hid ;
tmh . movePoints = h - > movement ;
tmh . result = instant + 1 ;
tmh . fowRevealed = gh - > gs - > tilesToReveal ( CGHeroInstance : : convertPosition ( pos , false ) , h - > getSightDistance ( ) , h - > tempOwner ) ;
gh - > sendAndApply ( & tmh ) ;
}
2008-07-30 20:51:19 +03:00
void CScriptCallback : : setOwner ( int objid , ui8 owner )
{
SetObjectProperty sop ( objid , 1 , owner ) ;
gh - > sendAndApply ( & sop ) ;
}
const CGObjectInstance * CScriptCallback : : getObj ( int objid )
{
return gh - > gs - > map - > objects [ objid ] ;
}
const CGHeroInstance * CScriptCallback : : getHero ( int objid )
{
return static_cast < const CGHeroInstance * > ( gh - > gs - > map - > objects [ objid ] ) ;
}
const CGTownInstance * CScriptCallback : : getTown ( int objid )
{
return static_cast < const CGTownInstance * > ( gh - > gs - > map - > objects [ objid ] ) ;
}
void CScriptCallback : : setHoverName ( int objid , MetaString * name )
{
SetHoverName shn ( objid , * name ) ;
gh - > sendAndApply ( & shn ) ;
}
int3 CScriptCallback : : getPos ( CGObjectInstance * ob )
{
return ob - > pos ;
}
2008-08-04 18:56:36 +03:00
void CScriptCallback : : changePrimSkill ( int ID , int which , int val , bool abs )
{
gh - > changePrimSkill ( ID , which , val , abs ) ;
2008-07-30 20:51:19 +03:00
}
2008-08-26 00:14:00 +03:00
int CScriptCallback : : getOwner ( int heroID )
2008-07-30 20:51:19 +03:00
{
2008-07-31 13:35:22 +03:00
return gh - > gs - > map - > objects [ heroID ] - > tempOwner ;
2008-07-30 20:51:19 +03:00
}
2008-08-22 15:21:09 +03:00
int CScriptCallback : : getResource ( int player , int which )
{
return gh - > gs - > players [ player ] . resources [ which ] ;
}
2008-07-31 00:27:15 +03:00
void CScriptCallback : : showInfoDialog ( InfoWindow * iw )
2008-07-30 20:51:19 +03:00
{
2008-07-31 00:27:15 +03:00
gh - > sendToAllClients ( iw ) ;
2008-07-30 20:51:19 +03:00
}
2008-08-22 15:21:09 +03:00
void CScriptCallback : : showYesNoDialog ( YesNoDialog * iw , const CFunctionList < void ( ui32 ) > & callback )
{
gh - > ask ( iw , iw - > player , callback ) ;
}
void CScriptCallback : : showSelectionDialog ( SelectionDialog * iw , const CFunctionList < void ( ui32 ) > & callback )
2008-07-30 20:51:19 +03:00
{
2008-08-13 03:44:31 +03:00
gh - > ask ( iw , iw - > player , callback ) ;
2008-07-30 20:51:19 +03:00
}
2008-08-13 03:44:31 +03:00
2008-07-30 20:51:19 +03:00
int CScriptCallback : : getSelectedHero ( )
{
2008-08-04 18:56:36 +03:00
//int ret;
2008-08-28 20:36:34 +03:00
//if (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE)
// ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection))->subID;
2008-07-30 20:51:19 +03:00
//else
// ret = -1;;
2008-08-01 14:21:15 +03:00
return - 1 ;
2008-07-30 20:51:19 +03:00
}
int CScriptCallback : : getDate ( int mode )
{
2008-07-31 13:35:22 +03:00
return gh - > gs - > getDate ( mode ) ;
2008-07-30 20:51:19 +03:00
}
void CScriptCallback : : giveResource ( int player , int which , int val )
{
2008-07-31 00:27:15 +03:00
SetResource sr ;
sr . player = player ;
sr . resid = which ;
sr . val = ( gh - > gs - > players [ player ] . resources [ which ] + val ) ;
gh - > sendAndApply ( & sr ) ;
2008-07-30 20:51:19 +03:00
}
2008-08-04 18:56:36 +03:00
void CScriptCallback : : showCompInfo ( ShowInInfobox * comp )
2008-07-30 20:51:19 +03:00
{
2008-08-04 18:56:36 +03:00
gh - > sendToAllClients ( comp ) ;
2008-07-30 20:51:19 +03:00
}
void CScriptCallback : : heroVisitCastle ( int obj , int heroID )
{
2008-08-13 12:28:06 +03:00
HeroVisitCastle vc ;
vc . hid = heroID ;
vc . tid = obj ;
vc . flags | = 1 ;
gh - > sendAndApply ( & vc ) ;
2008-09-12 11:51:46 +03:00
gh - > giveSpells ( getTown ( obj ) , getHero ( heroID ) ) ;
2008-07-30 20:51:19 +03:00
}
void CScriptCallback : : stopHeroVisitCastle ( int obj , int heroID )
{
2008-08-13 12:28:06 +03:00
HeroVisitCastle vc ;
vc . hid = heroID ;
vc . tid = obj ;
gh - > sendAndApply ( & vc ) ;
2008-07-30 20:51:19 +03:00
}
void CScriptCallback : : giveHeroArtifact ( int artid , int hid , int position ) //pos==-1 - first free slot in backpack
{
2008-08-25 13:25:16 +03:00
const CGHeroInstance * h = getHero ( hid ) ;
SetHeroArtifacts sha ;
sha . hid = hid ;
sha . artifacts = h - > artifacts ;
sha . artifWorn = h - > artifWorn ;
2008-07-30 20:51:19 +03:00
if ( position < 0 )
2008-09-29 00:01:49 +03:00
{
if ( position = = - 2 )
{
int i ;
for ( i = 0 ; i < VLC - > arth - > artifacts [ artid ] . possibleSlots . size ( ) ; i + + ) //try to put artifact into first avaialble slot
{
if ( ! vstd : : contains ( sha . artifWorn , VLC - > arth - > artifacts [ artid ] . possibleSlots [ i ] ) )
{
sha . artifWorn [ VLC - > arth - > artifacts [ artid ] . possibleSlots [ i ] ] = artid ;
break ;
}
}
if ( i = = VLC - > arth - > artifacts [ artid ] . possibleSlots . size ( ) ) //if haven't find proper slot, use backpack
sha . artifacts . push_back ( artid ) ;
}
else //should be -1 => putartifact into backpack
{
sha . artifacts . push_back ( artid ) ;
}
}
2008-07-30 20:51:19 +03:00
else
2008-09-29 00:01:49 +03:00
{
2008-08-25 13:25:16 +03:00
if ( ! vstd : : contains ( sha . artifWorn , ui16 ( position ) ) )
sha . artifWorn [ position ] = artid ;
else
sha . artifacts . push_back ( artid ) ;
2008-09-29 00:01:49 +03:00
}
2008-08-25 13:25:16 +03:00
gh - > sendAndApply ( & sha ) ;
2008-07-30 20:51:19 +03:00
}
2008-09-12 11:51:46 +03:00
void CScriptCallback : : startBattle ( const CCreatureSet * army1 , const CCreatureSet * army2 , int3 tile , const CGHeroInstance * hero1 , const CGHeroInstance * hero2 , boost : : function < void ( BattleResult * ) > cb ) //use hero=NULL for no hero
2008-07-30 20:51:19 +03:00
{
2008-09-12 11:51:46 +03:00
boost : : thread ( boost : : bind ( & CGameHandler : : startBattle , gh , * ( CCreatureSet * ) army1 , * ( CCreatureSet * ) army2 , tile , ( CGHeroInstance * ) hero1 , ( CGHeroInstance * ) hero2 , cb ) ) ;
2008-07-30 20:51:19 +03:00
}
2008-09-12 11:51:46 +03:00
void CScriptCallback : : startBattle ( int heroID , CCreatureSet army , int3 tile , boost : : function < void ( BattleResult * ) > cb ) //for hero<=>neutral army
2008-07-30 20:51:19 +03:00
{
2008-08-04 18:56:36 +03:00
CGHeroInstance * h = const_cast < CGHeroInstance * > ( getHero ( heroID ) ) ;
2008-09-12 11:51:46 +03:00
startBattle ( & h - > army , & army , tile , h , NULL , cb ) ;
2008-07-30 20:51:19 +03:00
//gh->gs->battle(&h->army,army,tile,h,NULL);
}
2008-09-12 11:51:46 +03:00
void CScriptCallback : : changeSpells ( int hid , bool give , const std : : set < ui32 > & spells )
{
ChangeSpells cs ;
cs . hid = hid ;
cs . spells = spells ;
cs . learn = give ;
gh - > sendAndApply ( & cs ) ;
}
2008-07-30 20:51:19 +03:00
void CLuaCallback : : registerFuncs ( lua_State * L )
{
// lua_newtable(L);
//
/ / # define REGISTER_C_FUNC ( x ) \
/ / lua_pushstring ( L , # x ) ; \
/ / lua_pushcfunction ( L , x ) ; \
// lua_rawset(L, -3)
//
// REGISTER_C_FUNC(getPos);
// REGISTER_C_FUNC(changePrimSkill);
// REGISTER_C_FUNC(getGnrlText);
// REGISTER_C_FUNC(getSelectedHero);
//
// lua_setglobal(L, "vcmi");
// #undef REGISTER_C_FUNC
}
int CLuaCallback : : getPos ( lua_State * L ) //(CGObjectInstance * object);
{
//const int args = lua_gettop(L); // number of arguments
//if ((args < 1) || !lua_isnumber(L, 1) )
// luaL_error(L,
// "Incorrect arguments to getPos([Object address])");
//CGObjectInstance * object = (CGObjectInstance *)(lua_tointeger(L, 1));
//lua_pushinteger(L,object->pos.x);
//lua_pushinteger(L,object->pos.y);
//lua_pushinteger(L,object->pos.z);
return 3 ;
}
int CLuaCallback : : changePrimSkill ( lua_State * L ) //(int ID, int which, int val);
{
//const int args = lua_gettop(L); // number of arguments
//if ((args < 1) || !lua_isnumber(L, 1) ||
// ((args >= 2) && !lua_isnumber(L, 2)) ||
// ((args >= 3) && !lua_isnumber(L, 3)) )
//{
// luaL_error(L,
// "Incorrect arguments to changePrimSkill([Hero ID], [Which Primary skill], [Change by])");
//}
//int ID = lua_tointeger(L, 1),
// which = lua_tointeger(L, 2),
// val = lua_tointeger(L, 3);
//CScriptCallback::changePrimSkill(ID,which,val);
return 0 ;
}
int CLuaCallback : : getGnrlText ( lua_State * L ) //(int which),returns string
{
//const int args = lua_gettop(L); // number of arguments
//if ((args < 1) || !lua_isnumber(L, 1) )
// luaL_error(L,
// "Incorrect arguments to getGnrlText([Text ID])");
//int which = lua_tointeger(L,1);
//lua_pushstring(L,CGI->generaltexth->allTexts[which].c_str());
return 1 ;
}
int CLuaCallback : : getSelectedHero ( lua_State * L ) //(),returns int (ID of hero, -1 if no hero is seleceted)
{
//int ret;
2008-08-28 20:36:34 +03:00
//if (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE)
// ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection))->subID;
2008-07-30 20:51:19 +03:00
//else
// ret = -1;
//lua_pushinteger(L,ret);
return 1 ;
}