1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00
* added files for Lua handler and moved Lua test code
This commit is contained in:
Michał W. Urbańczyk 2007-10-14 12:11:18 +00:00
parent 70ce922fe6
commit f1d5e686ce
10 changed files with 116 additions and 65 deletions

View File

@ -514,7 +514,7 @@ void CTownList::draw()
continue;
}
blitAtWR(CGI->townh->getPic(items[i]->type),posporx,pospory+i*32);
blitAtWR(CGI->townh->getPic(items[i]->town->typeID),posporx,pospory+i*32);
if ((selected == iT) && (LOCPLINT->adventureInt->selection.type == TOWNI_TYPE))
{
@ -961,9 +961,12 @@ void CTerrainRect::show()
pn = 20;
}
}
}
if ( ((currentPath->nodes[i].dist)-(*(currentPath->nodes.end()-1)).dist) > ((const CHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->movement)
pn+=25;
if (pn>=0)
{
{
int x = 32*(currentPath->nodes[i].coord.x-LOCPLINT->adventureInt->position.x)+7,
y = 32*(currentPath->nodes[i].coord.y-LOCPLINT->adventureInt->position.y)+6;
if (x<0 || y<0 || x>pos.w || y>pos.h)

View File

@ -102,8 +102,9 @@ bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
{
hero->pos = endpos;
}*/
//if(CGI->heroh->heroInstances[ID]->movement>=CGI->mh->getCost(stpos, endpos, CGI->heroh->heroInstances[ID]))
if((hero->movement>=CGI->mh->getCost(stpos, endpos, hero)) || player==-1)
{ //performing move
hero->movement-=CGI->mh->getCost(stpos, endpos, hero);
int nn=0; //number of interfece of currently browsed player
for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
{
@ -115,8 +116,8 @@ bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
break; //for testing only
}
}
//else
//return true; //move ended - no more movement points
else
return true; //move ended - no more movement points
hero->pos = curd.dst;
hero->ourObject->pos = curd.dst;
}

View File

@ -768,6 +768,7 @@ void CPlayerInterface::heroMoved(const HeroMoveDetails & details)
//ho->moveDir = 0; //move ended
ho->isStanding = true;
//move finished
adventureInt->heroList.draw();
}
void CPlayerInterface::heroKilled(const CHeroInstance * hero)
{

56
CLuaHandler.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "stdafx.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include <luabind/luabind.hpp>
#include <luabind/function.hpp>
#include <luabind/class.hpp>
#include "CLuaHandler.h"
void piszpowitanie2(std::string i) //simple global function for testing
{
std::cout<<"powitanie2zc++. Liczba dnia to " << i;
}
CLuaHandler::CLuaHandler()
{
}
CLuaHandler::~CLuaHandler()
{
}
void CLuaHandler::test()
{
int iErr = 0;
lua_State *lua = lua_open (); // Open Lua
LUA_OPEN_LIB(lua, luaopen_base);
LUA_OPEN_LIB(lua, luaopen_io);
if ((iErr = luaL_loadfile (lua, "test.lua")) == 0)
{
// Call main...
if ((iErr = lua_pcall (lua, 0, LUA_MULTRET, 0)) == 0)
{
luabind::open(lua);
luabind::module(lua)
[
luabind::def("powitanie",&piszpowitanie2)
];
//int ret = luabind::call_function<int>(lua, "helloWorld2");
lua_pushstring (lua, "helloWorld2");
lua_gettable (lua, LUA_GLOBALSINDEX);
lua_pcall (lua, 0, 0, 0);
// Push the function name onto the stack
lua_pushstring (lua, "helloWorld");
lua_gettable (lua, LUA_GLOBALSINDEX);
lua_pcall (lua, 0, 0, 0);
}
}
lua_close (lua);
}

19
CLuaHandler.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef CLUAHANDLER_H
#define CLUAHANDLER_H
#include "global.h"
#if (LUA_VERSION_NUM < 500)
# define LUA_OPEN_LIB(L, lib) lib(L)
#else
# define LUA_OPEN_LIB(L, lib) \
lua_pushcfunction((L), lib); \
lua_pcall((L), 0, 0, 0);
#endif
class CLuaHandler
{
public:
CLuaHandler();
~CLuaHandler();
void test();
};
#endif //CLUAHANDLER_H

61
CMT.cpp
View File

@ -43,7 +43,7 @@
#include "CPathfinder.h"
#include "CGameState.h"
#include "CCallback.h"
#include "CLuaHandler.h"
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
@ -58,25 +58,6 @@ const char * NAME = "VCMI 0.3 \"Tol Galen\"";
SDL_Surface * ekran, * screen, * screen2;
TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM;
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include <luabind/luabind.hpp>
#include <luabind/function.hpp>
#include <luabind/class.hpp>
#if (LUA_VERSION_NUM < 500)
# define LUA_OPEN_LIB(L, lib) lib(L)
#else
# define LUA_OPEN_LIB(L, lib) \
lua_pushcfunction((L), lib); \
lua_pcall((L), 0, 0, 0);
#endif
void piszpowitanie2(std::string i)
{
std::cout<<"powitanie2zc++. Liczba dnia to " << i;
}
void initGameState(CGameInfo * cgi)
{
cgi->state->day=1;
@ -170,7 +151,8 @@ void initGameState(CGameInfo * cgi)
{
CTownInstance * vti = new CTownInstance();
(*vti)=*(cgi->townh->townInstances[i]);
if (vti->name.length()==0) // if town hasn't name we draw it
vti->name=vti->town->names[rand()%vti->town->names.size()];
cgi->state->players[vti->owner].towns.push_back(vti);
}
@ -178,41 +160,8 @@ void initGameState(CGameInfo * cgi)
int _tmain(int argc, _TCHAR* argv[])
{
int iErr = 0;
lua_State *lua = lua_open (); // Open Lua
LUA_OPEN_LIB(lua, luaopen_base);
LUA_OPEN_LIB(lua, luaopen_io);
if ((iErr = luaL_loadfile (lua, "test.lua")) == 0)
{
// Call main...
if ((iErr = lua_pcall (lua, 0, LUA_MULTRET, 0)) == 0)
{
luabind::open(lua);
luabind::module(lua)
[
luabind::def("powitanie",&piszpowitanie2)
];
//int ret = luabind::call_function<int>(lua, "helloWorld2");
lua_pushstring (lua, "helloWorld2");
lua_gettable (lua, LUA_GLOBALSINDEX);
lua_pcall (lua, 0, 0, 0);
// Push the function name onto the stack
lua_pushstring (lua, "helloWorld");
lua_gettable (lua, LUA_GLOBALSINDEX);
lua_pcall (lua, 0, 0, 0);
}
}
lua_close (lua);
CLuaHandler luatest;
luatest.test();
//CBIKHandler cb;
//cb.open("CSECRET.BIK");

View File

@ -1508,14 +1508,14 @@ void CAmbarCendamo::deh3m()
nobj->owner = spec->player;
nobj->info = spec;
//////////// rewriting info to CTownInstance class /////////////////////
CTownInstance * nt = new CTownInstance;
CTownInstance * nt = new CTownInstance();
nt->owner = spec->player;
nt->type = CTownHandler::getTypeByDefName(map.defy[nobj->defNumber].name);
nt->town = &CGI->townh->towns[CTownHandler::getTypeByDefName(map.defy[nobj->defNumber].name)];
nt->builded = 0;
nt->destroyed = 0;
nt->name = spec->name;
nt->garrison = spec->garrison;
nt->garrisonHero = spec->garnisonHero;
nt->garrisonHero = NULL;// spec->garnisonHero is not readed - TODO: readit
nt->pos = int3(spec->x, spec->y, spec->z);
nt->possibleSpells = spec->possibleSpells;
nt->obligatorySpells = spec->obligatorySpells;
@ -2844,6 +2844,14 @@ void CAmbarCendamo::processMap(std::vector<std::string> & defsToUnpack)
{
town1DefNumbers[nxt.bytes[20]] = map.defy.size()-1;
}
for (int ij=0;ij<CGI->townh->townInstances.size();ij++) // wyharatac gdy bedzie dziedziczenie
{
if (CGI->townh->townInstances[ij]->pos==CGI->objh->objInstances[j]->pos)
{
CGI->townh->townInstances[ij]->town = &CGI->townh->towns[nxt.bytes[20]];
break;
}
}
}
//((CCastleObjInfo*)CGI->objh->objInstances[j].info)
break;

View File

@ -17,6 +17,7 @@ void CTownHandler::loadNames()
std::istringstream ins, names;
ins.str(CGI->bitmaph->getTextFile("TOWNTYPE.TXT"));
names.str(CGI->bitmaph->getTextFile("TOWNNAME.TXT"));
int si=0;
while (!ins.eof())
{
CTown town;
@ -27,6 +28,7 @@ void CTownHandler::loadNames()
names.getline(bufname,50);
town.names.push_back(std::string(bufname));
}
town.typeID=si++;
town.bonus=towns.size();
if (town.bonus==8) town.bonus=3;
if (town.name.length())
@ -60,3 +62,13 @@ int CTownHandler::getTypeByDefName(std::string name)
//TODO
return 0;
}
CTownInstance::CTownInstance()
:pos(-1,-1,-1)
{
builded=-1;
destroyed=-1;
garrisonHero=NULL;
owner=-1;
town=NULL;
}

View File

@ -19,6 +19,7 @@ public:
std::string name; //name of type
std::vector<std::string> names; //names of the town instances
int bonus; //pic number
int typeID;
};
class CTownHandler
@ -39,7 +40,6 @@ public:
class CTownInstance
{
public:
int type; //type of town
int owner; //ID of owner
int3 pos; //position
CTown * town;
@ -56,6 +56,8 @@ public:
CHero * garrisonHero;
std::vector<CSpell *> possibleSpells, obligatorySpells, availableSpells;
CTownInstance();
};
#endif //CTOWNHANDLER_H

Binary file not shown.