📄 mymaps.h
字号:
//************************************************************
//地图类
//作者:曾铮
//时间:2004年6月
//说明:游戏中的地图类。具体见程序注释。
//***********************************************************
#ifndef MyMaps_h_
#define MyMaps_h_
#define NOTHING -1
//path数组中的常量
#define PATH0 0 //path中标记路0~4。
#define PATH1 1
#define PATH2 2
#define PATH3 3
#define PATH4 4
#define GRASS1 5 //path中标记为草地,不可达。
#define GRASS2 6 //path中标记为烂草地,不可达。
#define TREE1 7 //树,不可达。
#define TREE2 8 //大树枝,不可达。
#define FLOWER 9 //花,不可达。
#define WATER1 18 //水,不可达。
#define WATER2 19 //水,不可达。
#define SNOW 20 //表示为雪地,不可达。
#define DESERT 21 //表示为沙漠地,不可达
#define BANK 22 //path中标记银行。
#define SHOP 23 //表示为商场
#define BUILDFACTORY0 24 //表示为工厂用地24~28
#define BUILDFACTORY1 25
#define BUILDFACTORY2 26
#define BUILDFACTORY3 27
#define BUILDFACTORY4 28
#define BUILDFACTORY5 29//暂时
#define BUIGROUND 30 //path中标记是否为建筑用地。
#define EVENT1 31 //path中标记事件地段30~32。
#define EVENT2 32
#define EVENT3 33
#define LUCKY1 34 //在此地可拾到卡片
#define LUCKY2 35
#define LANDFORFAC 49//建厂指示地
//path中尚未确定的:
//building数组中的常量。
#define FACTORY 88 //标志工厂所在地
#define BUILDING 89 //building数组中标记建筑。
//#include "CZDemo.h"
#include "Myclasses\MyBuilding.h"
#include "Myclasses\Myllist.h"
class MyMaps
{
public:
int **path;//0:可以通过,1:有障碍,不能走通;其它的待定。
int **building;//建筑数据矩阵。
int **block;//地图障碍矩阵。
linklist<MyBuilding> pbuilding;//建筑链。
linklist<MyFactory> pfactory;//工厂链。
int indexb;//建筑链单元索引。
int indexf;//工厂链单元索引。
public:
MyMaps();
bool GetAccess(const int& x,const int& y);//是否可以到达。
int GetEvent(const int& x,const int& y);//是否触发事件。
int GetBuildKind(const int& x,const int& y);//取得待建建筑种类。
void SaveBuilding(int index=1);//存建筑链。
void SaveFactory(int index=1);//存工厂链。
void LoadBuilding(int index=1);//取建筑链。
void LoadFactory(int index=1);//取工厂链。
void LoadMap(int index=1);//读取地图。
void LoadBlock(int index=1);//读取障碍。
bool IsSpace(int obj);//obj周围是否有空(建房)
bool IsSpacefa(int obj);//建厂求空地
bool IsLeveled(int obj,int owner);//obj是否为升级的数据
bool CanBuild(const int& x,const int& y,const int& kind);//是否可以建造建筑。
bool CanLevelup(int x,int y,int kind);//是否能升级
bool Levelup(int x,int y,int playerid);//房屋升级
bool Build(const int& x,const int& y,const int& playerID,const int& kind);//建造房子。
void AddBuilding(const int& x,const int& y,const int& playerID);//加入建筑链表。
void AddFactory(const int& x,const int& y,const int& playerID,const int& fackind);//加入工厂链表。
int SearchOwnerB(const int& x,const int& y);//搜索建筑主人,有则返回主人号;无则返回零。
int SearchOwnerB(const int& x,const int& y,int& index,int& kind);//搜索建筑主人,带回类型及建筑链中的位置。
int SearchOwnerF(const int& x,const int& y);//搜索建筑主人,有则返回主人号;无则返回零。
int SearchOwnerF(const int& x,const int& y,int& index,int& kind);//搜索建筑主人,带回类型及建筑链中的位置。
bool ChangeOwnerB(int x,int y,int old_id,int new_id);//改变x,y处建筑的主人
bool ChangeOwnerF(int x,int y,int old_id,int new_id);//改变x,y处建筑的主人
bool SearchKind(int index,int pkind);//判断工厂链中的index项是不是kind类型的。
int GetLenofFac();//取得工厂链的长度
int GetLenofHou();//取得房屋链的长度
void GetProfitofB(int playerid,long& tempmoney);//对playerid玩家当周利润存在tempmoney中
void GetProfitofF(int playerid,long& tempmoney);
MyBuilding GetBuilding(int playerid);//取得该玩家一个房屋副本
MyFactory GetFactory(int playerid);//同上
~MyMaps();
};
MyMaps::MyMaps()
{
this->indexb=1;
this->indexf=1;
this->path= new int*[64];
this->building= new int*[64];
this->block= new int*[64];
for(int i=0;i<64;i++)
{
this->path[i]=new int[48];
this->building[i]= new int[48];
this->block[i]= new int[48];
}
}
bool MyMaps::IsSpace(int obj)
{
if(obj==5 || obj==20 || obj==21)
return true;
else
return false;
}
bool MyMaps::IsSpacefa(int obj)
{
if(obj==LANDFORFAC)
return true;
else
return false;
}
bool MyMaps::IsLeveled(int obj,int owner)
{
switch (owner)
{
case 0:
{
if(obj==10)
return true;
}
break;
case 1:
{
if(obj==11)
return true;
}
break;
case 2:
{
if(obj==12)
return true;
}
break;
case 3:
{
if(obj==13)
return true;
}
break;
}
return false;
}
int MyMaps::GetEvent(const int& x,const int& y)
{
switch (this->path[x][y])
{
case EVENT1:
case EVENT2:
case EVENT3:
return EVENT1;
break;
case BUIGROUND:
return BUIGROUND;
break;
case BANK:
return BANK;
break;
case BUILDFACTORY0:
case BUILDFACTORY1:
case BUILDFACTORY2:
case BUILDFACTORY3:
case BUILDFACTORY4:
case BUILDFACTORY5:
return BUILDFACTORY0;
break;
case LUCKY1:
return LUCKY1;
break;
case LUCKY2:
return LUCKY2;
break;
case SHOP:
return SHOP;
break;
default:
return NOTHING;
break;
}
}
bool MyMaps::GetAccess(const int& x,const int& y)
{
int kind=this->path[x][y];
if(kind!=PATH0 && kind!=PATH1 && kind!=PATH2 && kind!=PATH3 && kind!=PATH4 && \
kind!=EVENT1 && kind!=EVENT2 && kind!=EVENT3 && kind!=BUIGROUND && kind!=BANK \
&& kind!=BUILDFACTORY0 && kind!=BUILDFACTORY1 && kind!=BUILDFACTORY2 \
&& kind!=BUILDFACTORY3 && kind!=BUILDFACTORY4 && kind!=BUILDFACTORY5 && kind!=SHOP && \
kind!=LUCKY1 && kind!=LUCKY2 )
return false;
return true;
}
void MyMaps::SaveBuilding(int index)
{
ofstream savebu;
if(this->indexb<=1)
return;
if(index==1)
savebu.open("gamesave/busave1.sav");
else if(index==2)
savebu.open("gamesave/busave2.sav");
else
savebu.open("gamesave/busave3.sav");
savebu<<this->pbuilding.listlength()<<" ";
for(int i=1;i<this->indexb;i++)
{
MyBuilding build;
pbuilding.getelem(i,build);
savebu<<build.x<<" "<<build.y<<" ";//在网格中的索引。
savebu<<build.owner<<" ";//所属玩家。
savebu<<build.kind<<" ";//房屋类型
savebu<<build.leveled<<" ";//房屋升级否
}
savebu.close();
return;
}
void MyMaps::SaveFactory(int index)
{
ofstream savefa;
if(this->indexf<=1)
return;
if(index==1)
savefa.open("gamesave/fasave1.sav");
else if(index==2)
savefa.open("gamesave/fasave2.sav");
else
savefa.open("gamesave/fasave3.sav");
savefa<<this->pfactory.listlength()<<" ";
for(int i=1;i<this->indexf;i++)
{
MyFactory factory;
pfactory.getelem(i,factory);
savefa<<factory.x<<" "<<factory.y<<" ";//在网格中的索引。
savefa<<factory.owner<<" ";//所属玩家。
savefa<<factory.kind<<" ";//房屋类型
}
savefa.close();
return;
}
void MyMaps::LoadBuilding(int index)
{
ifstream loadbu;
if(index==1)
loadbu.open("gamesave/busave1.sav");
else if(index==2)
loadbu.open("gamesave/busave2.sav");
else
loadbu.open("gamesave/busave3.sav");
int blength;
loadbu>>blength;//读取房子个数
int bx,by;//房子的x,y坐标
int pid,pkind;//房子的主人ID,及类型
int leveled;//是否升级
int tempx=0,tempy=0;
int colour=0;//判断种类
for(int n=1;n<=blength;n++)//还原房屋数据
{
loadbu>>bx>>by>>pid>>pkind>>leveled;
if(leveled==0)//未升级的房子
{
switch (pid)//根据不同的角色来建不同颜色的房子
{
case 0:
colour=10;
break;
case 1:
colour=11;
break;
case 2:
colour=12;
break;
case 3:
colour=13;
break;
default:
colour=10;
break;
}
}
else if(colour!=0)//升级了的房子
{
switch (pid)//根据不同的角色来建不同颜色的房子
{
case 0:
colour=14;
break;
case 1:
colour=15;
break;
case 2:
colour=16;
break;
case 3:
colour=17;
break;
default:
colour=14;
break;
}
}
if( this->IsSpace(this->building[bx-1][by]) )//路的左边有空。
tempx=bx-1,tempy=by;
else if( this->IsSpace(this->building[bx+1][by]) )//路的右边有空。
tempx=bx+1,tempy=by;
else if( this->IsSpace(this->building[bx][by-1]) )//路的上边有空。
tempx=bx,tempy=by-1;
else if( this->IsSpace (this->building[bx][by+1]) )//路的下边有空。
tempx=bx,tempy=by+1;
else
continue;//数据有错
this->building[tempx][tempy]=colour;//修筑。
this->AddBuilding(bx,by,pid); //加入建筑链。
}
loadbu.close();
return;
}
void MyMaps::LoadFactory(int index)
{
ifstream loadfa;
if(index==1)
loadfa.open("gamesave/fasave1.sav");
else if(index==2)
loadfa.open("gamesave/fasave2.sav");
else
loadfa.open("gamesave/fasave3.sav");
int flength;
loadfa>>flength;//读取房子个数
int bx,by;//房子的x,y坐标
int pid,pkind;//房子的主人ID,及类型
int tempx=0,tempy=0;
for(int n=1;n<=flength;n++)//还原房屋数据
{
loadfa>>bx>>by>>pid>>pkind;
if( this->IsSpacefa(this->building[bx-1][by]) )//在左
{
tempx=bx-3,tempy=by-1;
this->building[tempx+1][tempy]=100;//提供占位符,供作图时使用。下同
this->building[tempx+2][tempy]=100;
this->building[tempx][tempy+1]=100;
this->building[tempx+1][tempy+1]=100;
this->building[tempx+2][tempy+1]=100;
this->building[tempx][tempy+2]=100;
this->building[tempx+1][tempy+2]=100;
this->building[tempx+2][tempy+2]=100;
}
else if(this->IsSpacefa(this->building[bx+1][by]) )//在右
{
tempx=bx+1,tempy=by-1;
this->building[tempx+1][tempy]=100;//提供占位符,供作图时使用。下同
this->building[tempx+2][tempy]=100;
this->building[tempx][tempy+1]=100;
this->building[tempx+1][tempy+1]=100;
this->building[tempx+2][tempy+1]=100;
this->building[tempx][tempy+2]=100;
this->building[tempx+1][tempy+2]=100;
this->building[tempx+2][tempy+2]=100;
}
else if(IsSpacefa(this->building[bx][by-1]) )//在上
{
tempx=bx-1,tempy=by-3;
this->building[tempx+1][tempy]=100;//提供占位符,供作图时使用。下同
this->building[tempx+2][tempy]=100;
this->building[tempx][tempy+1]=100;
this->building[tempx+1][tempy+1]=100;
this->building[tempx+2][tempy+1]=100;
this->building[tempx][tempy+2]=100;
this->building[tempx+1][tempy+2]=100;
this->building[tempx+2][tempy+2]=100;
}
else if(this->IsSpacefa(this->building[bx][by+1]) )//在下
{
tempx=bx-1,tempy=by+1;
this->building[tempx+1][tempy]=100;//提供占位符,供作图时使用。下同
this->building[tempx+2][tempy]=100;
this->building[tempx][tempy+1]=100;
this->building[tempx+1][tempy+1]=100;
this->building[tempx+2][tempy+1]=100;
this->building[tempx][tempy+2]=100;
this->building[tempx+1][tempy+2]=100;
this->building[tempx+2][tempy+2]=100;
}
else
continue;
this->building[tempx][tempy]=pkind;//修筑。在修筑建筑地方的左上角设置标识值。
this->AddFactory(bx,by,pid,this->building[tempx][tempy]); //加入建筑链。
}
loadfa.close();
return;
}
void MyMaps::LoadMap(int index)
{
ifstream fin;
if(index==1)
fin.open("gamedata/map1.map");
else if(index==2)
fin.open("gamedata/map2.map");
else
fin.open("gamedata/map3.map");
for(int i=0;i<48;i++)
for(int j=0;j<64;j++)
fin>>this->path[j][i];
fin.close();
return;
}
int MyMaps::GetBuildKind(const int& x,const int& y)
{
return this->building[x][y];
}
void MyMaps::LoadBlock(int index)
{
ifstream fin;
if(index==1)
fin.open("gamedata/map1.blo");
else if(index==2)
fin.open("gamedata/map2.blo");
else
fin.open("gamedata/map3.blo");
for(int i=0;i<48;i++)
for(int j=0;j<64;j++)
fin>>this->block[j][i];
fin.close();
return;
}
bool MyMaps::CanBuild(const int& x,const int& y,const int& kind)
{
if(kind==BUIGROUND)
{
if(this->IsSpace(this->building[x-1][y])) //路的左边有空。
return true;
else if(this->IsSpace(this->building[x+1][y]) )//路的右边有空。
return true;
else if( this->IsSpace(this->building[x][y-1]) )//路的上边有空。
return true;
else if( this->IsSpace(this->building[x][y+1]))//路的下边有空。
return true;
else
return false;
}
else if(kind==BUILDFACTORY0 || kind==BUILDFACTORY1 || \
kind==BUILDFACTORY2 || kind==BUILDFACTORY3 || kind==BUILDFACTORY4 || \
kind==BUILDFACTORY5)
{
if( this->IsSpacefa(this->building[x-1][y]))//路的左边有空。
return true;
else if( this->IsSpacefa(this->building[x+1][y]) )//路的右边有空。
return true;
else if( this->IsSpacefa(this->building[x][y-1]) )//路的上边有空。
return true;
else if( this->IsSpacefa(this->building[x][y+1]) )//路的下边有空。
return true;
else
return false;
}
return false;
}
bool MyMaps::CanLevelup(int x,int y,const int kind)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -