From d8103fd242c977a128038fbdd39e2df8947f0952 Mon Sep 17 00:00:00 2001 From: mateuszb Date: Tue, 20 Nov 2007 17:52:22 +0000 Subject: [PATCH] * part of new object randomizing; make it work (I mean moving the last hero on your Arrogance2 - it doesn't wotk and I don't know why) --- CMT.cpp | 5 +- SDL_Extensions.cpp | 87 ++++++-------- hch/CAmbarCendamo.cpp | 3 +- hch/CDefObjInfoHandler.cpp | 2 + mapHandler.cpp | 226 +++++++++++++++++++++++++++++++++++-- mapHandler.h | 1 + 6 files changed, 263 insertions(+), 61 deletions(-) diff --git a/CMT.cpp b/CMT.cpp index 64f758e5b..c1194baa7 100644 --- a/CMT.cpp +++ b/CMT.cpp @@ -713,7 +713,10 @@ int _tmain(int argc, _TCHAR* argv[]) initGameState(cgi); THC std::cout<<"Initializing GameState: "<playerint.push_back(NULL); + }*/ for (int i=0; iscenarioOps.playerInfos.size();i++) //initializing interfaces { diff --git a/SDL_Extensions.cpp b/SDL_Extensions.cpp index 09d0db640..19af36fc1 100644 --- a/SDL_Extensions.cpp +++ b/SDL_Extensions.cpp @@ -143,37 +143,31 @@ void CSDL_Ext::printAt(std::string text, int x, int y, TTF_Font * font, SDL_Colo } void CSDL_Ext::SDL_PutPixel(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B, int myC, Uint8 A) { - Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel-myC; - if(SDL_BYTEORDER == SDL_BIG_ENDIAN) - { - p[0] = R; - p[1] = G; - p[2] = B; - } - else - { - p[0] = B; - p[1] = G; - p[2] = R; - } - SDL_UpdateRect(ekran, x, y, 1, 1); + Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel-myC; +#if (SDL_BYTEORDER == SDL_BIG_ENDIAN) + p[0] = R; + p[1] = G; + p[2] = B; +#else + p[0] = B; + p[1] = G; + p[2] = R; +#endif + SDL_UpdateRect(ekran, x, y, 1, 1); } void CSDL_Ext::SDL_PutPixelWithoutRefresh(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B, int myC, Uint8 A) { Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel-myC; - if(SDL_BYTEORDER == SDL_BIG_ENDIAN) - { - p[0] = B; - p[1] = G; - p[2] = R; - } - else - { - p[0] = R; - p[1] = G; - p[2] = B; - } +#if (SDL_BYTEORDER == SDL_BIG_ENDIAN) + p[0] = B; + p[1] = G; + p[2] = R; +#else + p[0] = R; + p[1] = G; + p[2] = B; +#endif } ///**************/ @@ -192,14 +186,11 @@ SDL_Surface * CSDL_Ext::rotate01(SDL_Surface * toRot, int myC) { { Uint8 *p = (Uint8 *)toRot->pixels + j * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel; - if(SDL_BYTEORDER == SDL_BIG_ENDIAN) - { +#if(SDL_BYTEORDER == SDL_BIG_ENDIAN) CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], myC); - } - else - { +#else CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], myC); - } +#endif } } } @@ -234,14 +225,11 @@ SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot) { Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j -1) * toRot->pitch + i * toRot->format->BytesPerPixel-2; int k=2; - if(SDL_BYTEORDER == SDL_BIG_ENDIAN) - { +#if(SDL_BYTEORDER == SDL_BIG_ENDIAN) CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], k); - } - else - { +#else CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], k); - } +#endif } } } @@ -277,14 +265,11 @@ SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot) { { Uint8 *p = (Uint8 *)toRot->pixels + i * toRot->pitch + j * toRot->format->BytesPerPixel; - if(SDL_BYTEORDER == SDL_BIG_ENDIAN) - { +#if(SDL_BYTEORDER == SDL_BIG_ENDIAN) SDL_PutPixel(ret, i, j, p[0], p[1], p[2]); - } - else - { +#else SDL_PutPixel(ret, i, j, p[2], p[1], p[0]); - } +#endif } } } @@ -308,14 +293,11 @@ SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot) { { Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel+2; - if(SDL_BYTEORDER == SDL_BIG_ENDIAN) - { +#if(SDL_BYTEORDER == SDL_BIG_ENDIAN) SDL_PutPixel(ret, i, j, p[0], p[1], p[2], 2); - } - else - { +#else SDL_PutPixel(ret, i, j, p[2], p[1], p[0], 2); - } +#endif } } } @@ -392,10 +374,11 @@ Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, int x, int y, bool colorByte return *(Uint16 *)p; case 3: - if(SDL_BYTEORDER == SDL_BIG_ENDIAN) +#if (SDL_BYTEORDER == SDL_BIG_ENDIAN) return p[0] << 16 | p[1] << 8 | p[2]; - else +#else return p[0] | p[1] << 8 | p[2] << 16; +#endif case 4: return *(Uint32 *)p; diff --git a/hch/CAmbarCendamo.cpp b/hch/CAmbarCendamo.cpp index ab1941f2d..12fa2cb1f 100644 --- a/hch/CAmbarCendamo.cpp +++ b/hch/CAmbarCendamo.cpp @@ -930,6 +930,7 @@ void CAmbarCendamo::deh3m() spec->myInstance = nhi; //nobj->isHero = true; (static_cast(nobj))->moveDir = 4; + nhi->isStanding = true; nhi->exp = spec->experience; nhi->level = CGI->heroh->level(nhi->exp); nhi->primSkills.resize(PRIMARY_SKILLS); @@ -1944,7 +1945,7 @@ void CAmbarCendamo::deh3m() }//*/ //end of loading objects; commented to make application work until it will be finished ////objects loaded - processMap(defsToUnpack); + //processMap(defsToUnpack); std::vector dhandlers = CGameInfo::mainObj->spriteh->extractManyFiles(defsToUnpack); for (int i=0;ihandler=dhandlers[i]; diff --git a/hch/CDefObjInfoHandler.cpp b/hch/CDefObjInfoHandler.cpp index 9a95e0c53..3b447ae4f 100644 --- a/hch/CDefObjInfoHandler.cpp +++ b/hch/CDefObjInfoHandler.cpp @@ -19,6 +19,8 @@ void CDefObjInfoHandler::load() DefObjInfo nobj; std::string dump; inp>>nobj.defName; + + std::transform(nobj.defName.begin(), nobj.defName.end(), nobj.defName.begin(), (int(*)(int))toupper); for(int o=0; o<6; ++o) { diff --git a/mapHandler.cpp b/mapHandler.cpp index 0201704c4..85c2e88db 100644 --- a/mapHandler.cpp +++ b/mapHandler.cpp @@ -10,6 +10,7 @@ #include #include "CGameState.h" #include "CLua.h" +#include "hch\CCastleHandler.h" #include "hch/CHeroHandler.h" extern SDL_Surface * ekran; @@ -24,6 +25,54 @@ public: void CMapHandler::init() { + ///////////////randomizing objects on map/////////////////////////// + + for(int no=0; noobjh->objInstances.size(); ++no) + { + std::string nname = getRandomizedDefName(CGI->objh->objInstances[no]->defInfo, CGI->objh->objInstances[no]); + if(nname.size()>0) //change def + { + int f=-1; + for(f=0; fdobjinfo->objs.size(); ++f) + { + if(CGI->dobjinfo->objs[f].defName==nname) + { + break; + } + } + CGI->objh->objInstances[no]->defInfo->name = nname; + if(loadedDefs.find(nname)!=loadedDefs.end()) + { + CGI->objh->objInstances[no]->defInfo->handler = loadedDefs.find(nname)->second; + } + else + { + CGI->objh->objInstances[no]->defInfo->handler = CGI->spriteh->giveDef(nname); + for(int dd=0; ddobjh->objInstances[no]->defInfo->handler->ourImages.size(); ++dd) + { + CSDL_Ext::fullAlphaTransform(CGI->objh->objInstances[no]->defInfo->handler->ourImages[dd].bitmap); + } + loadedDefs.insert(std::pair(nname, CGI->objh->objInstances[no]->defInfo->handler)); + } + CGI->objh->objInstances[no]->defObjInfoNumber = f; + if(f!=-1) + { + CGI->objh->objInstances[no]->defInfo->isOnDefList = true; + CGI->objh->objInstances[no]->ID = CGI->dobjinfo->objs[f].type; + CGI->objh->objInstances[no]->subID = CGI->dobjinfo->objs[f].subtype; + CGI->objh->objInstances[no]->defInfo->id = CGI->dobjinfo->objs[f].type; + CGI->objh->objInstances[no]->defInfo->subid = CGI->dobjinfo->objs[f].subtype; + CGI->objh->objInstances[no]->defInfo->printPriority = CGI->dobjinfo->objs[f].priority; + } + else + { + CGI->objh->objInstances[no]->defInfo->isOnDefList = false; + } + } + } + + ///////////////objects randomized/////////////////////////////////// + for(int h=0; hmap.defy.size(); ++h) //initializing loaded def handler's info { std::string hlp = reader->map.defy[h]->name; @@ -425,7 +474,7 @@ void CMapHandler::init() cr.y = fy*32; std::pair>>> toAdd = std::make_pair(CGI->objh->objInstances[f], std::make_pair(cr, std::vector>())); ///initializing places that will be coloured by blitting (flag colour / player colour positions) - if(toAdd.first->defObjInfoNumber>=0 && CGI->dobjinfo->objs[toAdd.first->defObjInfoNumber].isVisitable()) + if(toAdd.first->defInfo->isVisitable()) { toAdd.second.second.resize(toAdd.first->defInfo->handler->ourImages.size()); for(int no = 0; nodefInfo->handler->ourImages.size(); ++no) @@ -467,8 +516,6 @@ void CMapHandler::init() } // for(int f=0; fobjh->objInstances.size(); ++f) for(int f=0; fobjh->objInstances.size(); ++f) //calculationg blocked / visitable positions { - if(CGI->objh->objInstances[f]->defObjInfoNumber == -1) - continue; CDefHandler * curd = CGI->objh->objInstances[f]->defInfo->handler; for(int fx=0; fx<8; ++fx) { @@ -480,9 +527,9 @@ void CMapHandler::init() if(xVal>=0 && xVal=0 && yValdobjinfo->objs[CGI->objh->objInstances[f]->defObjInfoNumber].visitMap[fy] >> (7 - fx)) & 1)) + if(((CGI->objh->objInstances[f]->defInfo->visitMap[fy] >> (7 - fx)) & 1)) curt.visitable = true; - if(!((CGI->dobjinfo->objs[CGI->objh->objInstances[f]->defObjInfoNumber].blockMap[fy] >> (7 - fx)) & 1)) + if(!((CGI->objh->objInstances[f]->defInfo->blockMap[fy] >> (7 - fx)) & 1)) curt.blocked = true; } } @@ -1493,7 +1540,7 @@ bool CMapHandler::printObject(CGObjectInstance *obj) cr.y = fy*32; std::pair>>> toAdd = std::make_pair(obj, std::make_pair(cr, std::vector>())); ///initializing places that will be coloured by blitting (flag colour / player colour positions) - if(toAdd.first->defObjInfoNumber>=0 && CGI->dobjinfo->objs[toAdd.first->defObjInfoNumber].isVisitable()) + if(CGI->dobjinfo->objs[toAdd.first->defObjInfoNumber].isVisitable()) { toAdd.second.second.resize(toAdd.first->defInfo->handler->ourImages.size()); for(int no = 0; nodefInfo->handler->ourImages.size(); ++no) @@ -1555,4 +1602,169 @@ bool CMapHandler::hideObject(CGObjectInstance *obj) } // for(int fy=0; fyourImages[0].bitmap->h/32; ++fy) } //for(int fx=0; fxourImages[0].bitmap->w/32; ++fx) return true; -} \ No newline at end of file +} + +std::string CMapHandler::getRandomizedDefName(CGDefInfo *di, CGObjectInstance * obj) +{ + if(di->id==76) //random resource + { + std::vector resDefNames; + resDefNames.push_back("AVTRNDM0.DEF"); + resDefNames.push_back("AVTWOOD0.DEF"); + resDefNames.push_back("AVTMERC0.DEF"); + resDefNames.push_back("AVTORE0.DEF"); + resDefNames.push_back("AVTSULF0.DEF"); + resDefNames.push_back("AVTCRYS0.DEF"); + resDefNames.push_back("AVTGEMS0.DEF"); + resDefNames.push_back("AVTGOLD0.DEF"); + resDefNames.push_back("ZMITHR.DEF"); + return resDefNames[rand()%resDefNames.size()]; + } + else if(di->id==72 || di->id==73 || di->id==74 || di->id==75 || di->id==162 || di->id==163 || di->id==164 || di->id==71) //random monster + { + std::vector creDefNames; + for(int dd=0; dd<140; ++dd) //we do not use here WoG units + { + creDefNames.push_back(CGI->dobjinfo->objs[dd+1184].defName); + } + + switch(di->id) + { + case 72: //level 1 + return creDefNames[14*(rand()%9)+rand()%2]; + case 73: //level 2 + return creDefNames[14*(rand()%9)+rand()%2+2]; + case 74: //level 3 + return creDefNames[14*(rand()%9)+rand()%2+4]; + case 75: //level 4 + return creDefNames[14*(rand()%9)+rand()%2+6]; + case 162: //level 5 + return creDefNames[14*(rand()%9)+rand()%2+8]; + case 163: //level 6 + return creDefNames[14*(rand()%9)+rand()%2+10]; + case 164: //level 7 + return creDefNames[14*(rand()%9)+rand()%2+12]; + case 71: // any level + return creDefNames[rand()%126]; + } + } + else if(di->id==65) //random artifact (any class) + { + std::vector artDefNames; + for(int bb=0; bb<162; ++bb) + { + if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass!=EartClass::SartClass) + artDefNames.push_back(CGI->dobjinfo->objs[bb+213].defName); + } + return artDefNames[rand()%artDefNames.size()]; + } + else if(di->id==66) //random artifact (treasure) + { + std::vector art1DefNames; + for(int bb=0; bb<162; ++bb) + { + if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::TartClass) + art1DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName); + } + return art1DefNames[rand()%art1DefNames.size()]; + } + else if(di->id==67) //random artifact (minor) + { + std::vector art2DefNames; + for(int bb=0; bb<162; ++bb) + { + if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::NartClass) + art2DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName); + } + return art2DefNames[rand()%art2DefNames.size()]; + } + else if(di->id==68) //random artifact (major) + { + std::vector art3DefNames; + for(int bb=0; bb<162; ++bb) + { + if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::JartClass) + art3DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName); + } + return art3DefNames[rand()%art3DefNames.size()]; + } + else if(di->id==69) //random artifact (relic) + { + std::vector art4DefNames; + for(int bb=0; bb<162; ++bb) + { + if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::RartClass) + art4DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName); + } + return art4DefNames[rand()%art4DefNames.size()]; + } + else if(di->id==77) //random town + { + if(!obj) + return std::string(); //obj is necessary! + std::vector town0DefNames; //without fort + town0DefNames.push_back("AVCCAST0.DEF"); + town0DefNames.push_back("AVCRAMP0.DEF"); + town0DefNames.push_back("AVCTOWR0.DEF"); + town0DefNames.push_back("AVCINFT0.DEF"); + town0DefNames.push_back("AVCNECR0.DEF"); + town0DefNames.push_back("AVCDUNG0.DEF"); + town0DefNames.push_back("AVCSTRO0.DEF"); + town0DefNames.push_back("AVCFTRT0.DEF"); + town0DefNames.push_back("AVCHFOR0.DEF"); + + std::vector town1DefNames; //with fort + for(int dd=0; dddobjinfo->objs[dd+385].defName); + } + + std::vector town2DefNames; //with capitol + for(int dd=0; dddobjinfo->objs[dd+385].defName); + } + for(int b=0; btempOwner==0xff) //no preselected preferentions + { + if(((CCastleObjInfo*)obj->info)->hasFort) + return town1DefNames[rand()%town1DefNames.size()]; + else + return town0DefNames[rand()%town0DefNames.size()]; + } + else + { + + if(CGI->scenarioOps.getIthPlayersSettings(((CCastleObjInfo*)obj->info)->player).castle>-1) //castle specified in start options + { + int defnr = CGI->scenarioOps.getIthPlayersSettings(((CCastleObjInfo*)obj->info)->player).castle; + if(((CCastleObjInfo*)obj->info)->hasFort) + return town1DefNames[defnr]; + else + return town0DefNames[defnr]; + } + else //no castle specified + { + int defnr = rand()%F_NUMBER; + if(((CCastleObjInfo*)obj->info)->hasFort) + return town1DefNames[defnr]; + else + return town0DefNames[defnr]; + } + } + } + + return std::string(); +} diff --git a/mapHandler.h b/mapHandler.h index bbaf63dbc..5159936a4 100644 --- a/mapHandler.h +++ b/mapHandler.h @@ -90,6 +90,7 @@ public: SDL_Surface * terrainRect(int x, int y, int dx, int dy, int level=0, unsigned char anim=0, PseudoV< PseudoV< PseudoV > > & visibilityMap = CGI->mh->visibility); SDL_Surface * terrBitmap(int x, int y); SDL_Surface * undTerrBitmap(int x, int y); + std::string getRandomizedDefName(CGDefInfo* di, CGObjectInstance * obj = NULL); //objinstance needed only for heroes and towns };