1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-17 01:32:21 +02:00

* minor improvements

This commit is contained in:
mateuszb
2010-02-21 18:07:24 +00:00
parent b2aa0e5930
commit 94b289dab0
8 changed files with 67 additions and 64 deletions

View File

@ -48,26 +48,8 @@ CDefEssential::~CDefEssential()
for(size_t i=0; i < ourImages.size(); ++i) for(size_t i=0; i < ourImages.size(); ++i)
SDL_FreeSurface(ourImages[i].bitmap); SDL_FreeSurface(ourImages[i].bitmap);
} }
// Note: this function is unused
void CDefHandler::openDef(std::string name)
{
int andame;
std::ifstream * is = new std::ifstream();
is -> open(name.c_str(),std::ios::binary);
is->seekg(0,std::ios::end); // na koniec
andame = is->tellg(); // read length
is->seekg(0,std::ios::beg); // wracamy na poczatek
unsigned char * FDef = new unsigned char[andame]; // allocate memory
is->read((char*)FDef, andame); // read map file to buffer
is->close();
delete is;
openFromMemory(FDef, name); void CDefHandler::openFromMemory(unsigned char *table, const std::string & name)
delete [] FDef;
}
void CDefHandler::openFromMemory(unsigned char *table, std::string name)
{ {
BMPPalette palette[256]; BMPPalette palette[256];
SDefEntry &de = * reinterpret_cast<SDefEntry *>(table); SDefEntry &de = * reinterpret_cast<SDefEntry *>(table);
@ -150,8 +132,8 @@ SDL_Surface * CDefHandler::getSprite (int SIndex, const unsigned char * FDef, co
SDL_Surface * ret=NULL; SDL_Surface * ret=NULL;
unsigned int BaseOffset, unsigned int BaseOffset,
SpriteWidth, SpriteHeight, //format sprite'a SpriteWidth, SpriteHeight, //format of sprite
TotalRowLength, // dlugosc przeczytanego segmentu TotalRowLength, // length of read segment
add, FullHeight,FullWidth, add, FullHeight,FullWidth,
RowAdd, //, NextSpriteOffset; //TODO use me RowAdd, //, NextSpriteOffset; //TODO use me
prSize, prSize,
@ -393,7 +375,7 @@ CDefEssential * CDefHandler::essentialize()
return ret; return ret;
} }
CDefHandler * CDefHandler::giveDef(std::string defName) CDefHandler * CDefHandler::giveDef(const std::string & defName)
{ {
unsigned char * data = spriteh->giveFile(defName); unsigned char * data = spriteh->giveFile(defName);
if(!data) if(!data)
@ -404,7 +386,7 @@ CDefHandler * CDefHandler::giveDef(std::string defName)
delete [] data; delete [] data;
return nh; return nh;
} }
CDefEssential * CDefHandler::giveDefEss(std::string defName) CDefEssential * CDefHandler::giveDefEss(const std::string & defName)
{ {
CDefEssential * ret; CDefEssential * ret;
CDefHandler * temp = giveDef(defName); CDefHandler * temp = giveDef(defName);

View File

@ -88,14 +88,13 @@ public:
CDefHandler(); //c-tor CDefHandler(); //c-tor
~CDefHandler(); //d-tor ~CDefHandler(); //d-tor
SDL_Surface * getSprite (int SIndex, const unsigned char * FDef, const BMPPalette * palette) const; //zapisuje klatke o zadanym numerze do "testtt.bmp" SDL_Surface * getSprite (int SIndex, const unsigned char * FDef, const BMPPalette * palette) const; //saves picture with given number to "testtt.bmp"
void openDef(std::string name);
static void expand(unsigned char N,unsigned char & BL, unsigned char & BR); static void expand(unsigned char N,unsigned char & BL, unsigned char & BR);
void openFromMemory(unsigned char * table, std::string name); void openFromMemory(unsigned char * table, const std::string & name);
CDefEssential * essentialize(); CDefEssential * essentialize();
static CDefHandler * giveDef(std::string defName); static CDefHandler * giveDef(const std::string & defName);
static CDefEssential * giveDefEss(std::string defName); static CDefEssential * giveDefEss(const std::string & defName);
}; };
class CDefEssential //DefHandler with images only class CDefEssential //DefHandler with images only

View File

@ -234,7 +234,6 @@ CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
//state = new CLuaObjectScript(); //state = new CLuaObjectScript();
ID = subID = id = -1; ID = subID = id = -1;
defInfo = NULL; defInfo = NULL;
info = NULL;
tempOwner = 254; tempOwner = 254;
blockVisit = false; blockVisit = false;
} }
@ -448,6 +447,13 @@ ui8 CGObjectInstance::getPassableness() const
return 0; return 0;
} }
bool CGObjectInstance::hasShadowAt( int x, int y ) const
{
if( (defInfo->shadowCoverage[y] >> (7-(x) )) & 1 )
return true;
return false;
}
static int lowestSpeed(const CGHeroInstance * chi) static int lowestSpeed(const CGHeroInstance * chi)
{ {
if(!chi->army.slots.size()) if(!chi->army.slots.size())

View File

@ -143,7 +143,6 @@ public:
si32 ID, subID; //normal ID (this one from OH3 maps ;]) - eg. town=98; hero=34 si32 ID, subID; //normal ID (this one from OH3 maps ;]) - eg. town=98; hero=34
si32 id;//number of object in CObjectHandler's vector si32 id;//number of object in CObjectHandler's vector
CGDefInfo * defInfo; CGDefInfo * defInfo;
CSpecObjInfo * info;
ui8 animPhaseShift; ui8 animPhaseShift;
ui8 tempOwner; ui8 tempOwner;
@ -161,6 +160,7 @@ public:
int3 getVisitableOffset() const; //returns (x,y,0) offset to first visitable tile from bottom right obj tile (0,0,0) (h3m pos) int3 getVisitableOffset() const; //returns (x,y,0) offset to first visitable tile from bottom right obj tile (0,0,0) (h3m pos)
bool blockingAt(int x, int y) const; //returns true if object is blocking location (x, y) form left top tile of image (x, y in tiles) bool blockingAt(int x, int y) const; //returns true if object is blocking location (x, y) form left top tile of image (x, y in tiles)
bool coveringAt(int x, int y) const; //returns true if object covers with picture location (x, y) form left top tile of maximal possible image (8 x 6 tiles) (x, y in tiles) bool coveringAt(int x, int y) const; //returns true if object covers with picture location (x, y) form left top tile of maximal possible image (8 x 6 tiles) (x, y in tiles)
bool hasShadowAt(int x, int y) const;//returns true if object covers with shadow location (x, y) form left top tile of maximal possible image (8 x 6 tiles) (x, y in tiles)
std::set<int3> getBlockedPos() const; //returns set of positions blocked by this object std::set<int3> getBlockedPos() const; //returns set of positions blocked by this object
bool operator<(const CGObjectInstance & cmp) const; //screen printing priority comparing bool operator<(const CGObjectInstance & cmp) const; //screen printing priority comparing
CGObjectInstance(); CGObjectInstance();
@ -357,6 +357,7 @@ public:
class DLL_EXPORT CGDwelling : public CArmedInstance class DLL_EXPORT CGDwelling : public CArmedInstance
{ {
public: public:
CSpecObjInfo * info; //h3m info about dewlling
std::vector<std::pair<ui32, std::vector<ui32> > > creatures; //creatures[level] -> <vector of alternative ids (base creature and upgrades, creatures amount> std::vector<std::pair<ui32, std::vector<ui32> > > creatures; //creatures[level] -> <vector of alternative ids (base creature and upgrades, creatures amount>
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)

View File

@ -347,7 +347,6 @@ static CGObjectInstance * createObject(int id, int subid, int3 pos, int owner)
nobj->pos = pos; nobj->pos = pos;
//nobj->state = NULL;//new CLuaObjectScript(); //nobj->state = NULL;//new CLuaObjectScript();
nobj->tempOwner = owner; nobj->tempOwner = owner;
nobj->info = NULL;
nobj->defInfo->id = id; nobj->defInfo->id = id;
nobj->defInfo->subid = subid; nobj->defInfo->subid = subid;
@ -1061,7 +1060,8 @@ std::pair<int,int> CGameState::pickObject (CGObjectInstance *obj)
case 216: //random dwelling case 216: //random dwelling
{ {
int faction = ran()%F_NUMBER; int faction = ran()%F_NUMBER;
CCreGen2ObjInfo* info = static_cast<CCreGen2ObjInfo*>(obj->info); CGDwelling * dwl = static_cast<CGDwelling*>(obj);
CCreGen2ObjInfo* info = static_cast<CCreGen2ObjInfo*>(dwl->info);
if (info->asCastle) if (info->asCastle)
{ {
for(unsigned int i=0;i<map->objects.size();i++) for(unsigned int i=0;i<map->objects.size();i++)
@ -1094,12 +1094,15 @@ std::pair<int,int> CGameState::pickObject (CGObjectInstance *obj)
if(VLC->objh->cregens[i]==cid) if(VLC->objh->cregens[i]==cid)
return std::pair<int,int>(17,i); return std::pair<int,int>(17,i);
tlog3 << "Cannot find a dwelling for creature "<< cid << std::endl; tlog3 << "Cannot find a dwelling for creature "<< cid << std::endl;
return std::pair<int,int>(17,0); return std::pair<int,int>(17,0);
delete dwl->info;
dwl->info = NULL;
} }
case 217: case 217:
{ {
int faction = ran()%F_NUMBER; int faction = ran()%F_NUMBER;
CCreGenObjInfo* info = static_cast<CCreGenObjInfo*>(obj->info); CGDwelling * dwl = static_cast<CGDwelling*>(obj);
CCreGenObjInfo* info = static_cast<CCreGenObjInfo*>(dwl->info);
if (info->asCastle) if (info->asCastle)
{ {
for(unsigned int i=0;i<map->objects.size();i++) for(unsigned int i=0;i<map->objects.size();i++)
@ -1131,11 +1134,14 @@ std::pair<int,int> CGameState::pickObject (CGObjectInstance *obj)
if(VLC->objh->cregens[i]==cid) if(VLC->objh->cregens[i]==cid)
return std::pair<int,int>(17,i); return std::pair<int,int>(17,i);
tlog3 << "Cannot find a dwelling for creature "<<cid <<std::endl; tlog3 << "Cannot find a dwelling for creature "<<cid <<std::endl;
return std::pair<int,int>(17,0); return std::pair<int,int>(17,0);
delete dwl->info;
dwl->info = NULL;
} }
case 218: case 218:
{ {
CCreGen3ObjInfo* info = static_cast<CCreGen3ObjInfo*>(obj->info); CGDwelling * dwl = static_cast<CGDwelling*>(obj);
CCreGen3ObjInfo* info = static_cast<CCreGen3ObjInfo*>(dwl->info);
int level = ((info->maxLevel-info->minLevel) ? (ran()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel)); int level = ((info->maxLevel-info->minLevel) ? (ran()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel));
int cid = VLC->townh->towns[obj->subID].basicCreatures[level]; int cid = VLC->townh->towns[obj->subID].basicCreatures[level];
for(unsigned int i=0;i<VLC->objh->cregens.size();i++) for(unsigned int i=0;i<VLC->objh->cregens.size();i++)
@ -1143,6 +1149,8 @@ std::pair<int,int> CGameState::pickObject (CGObjectInstance *obj)
return std::pair<int,int>(17,i); return std::pair<int,int>(17,i);
tlog3 << "Cannot find a dwelling for creature "<<cid <<std::endl; tlog3 << "Cannot find a dwelling for creature "<<cid <<std::endl;
return std::pair<int,int>(17,0); return std::pair<int,int>(17,0);
delete dwl->info;
dwl->info = NULL;
} }
} }
return std::pair<int,int>(-1,-1); return std::pair<int,int>(-1,-1);

View File

@ -1756,7 +1756,7 @@ void Mapa::readObjects( const unsigned char * bufor, int &i)
spec->asCastle = true; spec->asCastle = true;
} }
nobj->setOwner(spec->player); nobj->setOwner(spec->player);
nobj->info = spec; static_cast<CGDwelling*>(nobj)->info = spec;
break; break;
} }
case 216: case 216:
@ -1778,7 +1778,7 @@ void Mapa::readObjects( const unsigned char * bufor, int &i)
spec->minLevel = bufor[i]; ++i; spec->minLevel = bufor[i]; ++i;
spec->maxLevel = bufor[i]; ++i; spec->maxLevel = bufor[i]; ++i;
nobj->setOwner(spec->player); nobj->setOwner(spec->player);
nobj->info = spec; static_cast<CGDwelling*>(nobj)->info = spec;
break; break;
} }
case 218: case 218:
@ -1794,7 +1794,7 @@ void Mapa::readObjects( const unsigned char * bufor, int &i)
if(spec->minLevel<1) if(spec->minLevel<1)
spec->minLevel = 1; spec->minLevel = 1;
nobj->setOwner(spec->player); nobj->setOwner(spec->player);
nobj->info = spec; static_cast<CGDwelling*>(nobj)->info = spec;
break; break;
} }
case 215: case 215:

View File

@ -438,7 +438,7 @@ void CMapHandler::init()
for (int i = 0; i < 8 ; i++) for (int i = 0; i < 8 ; i++)
{ {
TerrainTile2 &t = ttiles[24+i][0][0]; TerrainTile2 &t = ttiles[24+i][0][0];
tlog0 << t.objects.front().first->defInfo->name << ' '; //tlog0 << t.objects.front().first->defInfo->name << ' ';
} }
} }
@ -665,7 +665,10 @@ void CMapHandler::terrainRect(int3 top_tile, unsigned char anim, const std::vect
if(color < 8 || color==255) if(color < 8 || color==255)
CSDL_Ext::setPlayerColor(bitmap, color); CSDL_Ext::setPlayerColor(bitmap, color);
CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr2); if( obj->hasShadowAt(obj->pos.x - (top_tile.x + bx), top_tile.y + by - obj->pos.y + 5) )
CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr2);
else
SDL_BlitSurface(bitmap,&pp,extSurf,&sr2);
} }
} }
//objects blitted //objects blitted
@ -1150,51 +1153,55 @@ unsigned char CMapHandler::getHeroFrameNum(unsigned char dir, bool isMoving) con
{ {
if(isMoving) if(isMoving)
{ {
switch(dir) static const unsigned char frame [] = {-1, 10, 5, 6, 7, 8, 9, 12, 11};
return frame[dir];
/*switch(dir)
{ {
case 1: case 1:
return 10; return 10;
case 2: case 2:
return 5; return 5;
case 3: case 3:
return 6; return 6;
case 4: case 4:
return 7; return 7;
case 5: case 5:
return 8; return 8;
case 6: case 6:
return 9; return 9;
case 7: case 7:
return 12; return 12;
case 8: case 8:
return 11; return 11;
default: default:
throw std::string("Something very wrong1."); throw std::string("Something very wrong1.");
} }*/
} }
else //if(isMoving) else //if(isMoving)
{ {
switch(dir) static const unsigned char frame [] = {-1, 13, 0, 1, 2, 3, 4, 15, 14};
return frame[dir];
/*switch(dir)
{ {
case 1: case 1:
return 13; return 13;
case 2: case 2:
return 0; return 0;
case 3: case 3:
return 1; return 1;
case 4: case 4:
return 2; return 2;
case 5: case 5:
return 3; return 3;
case 6: case 6:
return 4; return 4;
case 7: case 7:
return 15; return 15;
case 8: case 8:
return 14; return 14;
default: default:
throw std::string("Something very wrong2."); throw std::string("Something very wrong2.");
} }*/
} }
} }

View File

@ -134,7 +134,7 @@ public:
void updateWater(); void updateWater();
unsigned char getHeroFrameNum(unsigned char dir, bool isMoving) const; //terrainRect helper function unsigned char getHeroFrameNum(unsigned char dir, bool isMoving) const; //terrainRect helper function
void validateRectTerr(SDL_Rect * val, const SDL_Rect * ext); //terrainRect helper void validateRectTerr(SDL_Rect * val, const SDL_Rect * ext); //terrainRect helper
static unsigned char getDir(const int3 & a, const int3 & b); //returns direction number in range 0 - 7 (0 is left top, clockwise) [direction: form a to b] static unsigned char getDir(const int3 & a, const int3 & b); //returns direction number in range 0 - 7 (0 is left top, clockwise) [direction: form a to b]
}; };