added look and travel

This commit is contained in:
Anselme 2016-03-16 17:02:15 +01:00
parent a3b8f75aa7
commit 9c44781fd3
2 changed files with 84 additions and 42 deletions

View File

@ -114,50 +114,91 @@ QString RPGModule::getTerrainName(int type)
}
}
QString RPGModule::lookAt(RPGCharacter *c, QString target)
{
if(target.compare("around") == 0)
return QString("%1 is in %2").arg(c->pseudo).arg(getTerrainName(WORLDMAP[c->x][c->y]));
else if(target.compare("north") == 0 || target.compare("n") == 0)
return QString("%1 looks north and sees %2").arg(c->pseudo).arg(getTerrainName(WORLDMAP[c->x][c->y-1]));
else if(target.compare("south") == 0 || target.compare("s") == 0)
return QString("%1 looks south and sees %2").arg(c->pseudo).arg(getTerrainName(WORLDMAP[c->x][c->y+1]));
else if(target.compare("west") == 0 || target.compare("w") == 0)
return QString("%1 looks west and sees %2").arg(c->pseudo).arg(getTerrainName(WORLDMAP[c->x-1][c->y]));
else if(target.compare("east") == 0 || target.compare("e") == 0)
return QString("%1 looks east and sees %2").arg(c->pseudo).arg(getTerrainName(WORLDMAP[c->x+1][c->y]));
else
return QString("%1 is not a valid target").arg(target);
}
QString RPGModule::playerTravel(RPGCharacter *c, QString dir, bool repeat)
{
char terrainType = WORLDMAP[c->x][c->y];
QString direction;
QString currentArea = getTerrainName(terrainType);
bool blocked = false;
int dist = 0;
if(dir.compare("north") == 0 || dir.compare("n") == 0)
do
{
if(c->y == 0)
blocked = true;
if(dir.compare("north") == 0 || dir.compare("n") == 0)
{
if(c->y == 0 || (repeat && terrainType != WORLDMAP[c->x][c->y-1]))
blocked = true;
else
{
c->y -= 1;
++dist;
}
direction = "north";
}
else if(dir.compare("south") == 0 || dir.compare("s") == 0)
{
if(c->y == 24 || (repeat && terrainType != WORLDMAP[c->x][c->y+1]))
blocked = true;
else
{
c->y += 1;
++dist;
}
direction = "south";
}
else if(dir.compare("west") == 0 || dir.compare("w") == 0)
{
if(c->x == 0 || (repeat && terrainType != WORLDMAP[c->x-1][c->y]))
blocked = true;
else
{
c->x -= 1;
++dist;
}
direction = "west";
}
else if(dir.compare("east") == 0 || dir.compare("e") == 0)
{
if(c->x == 24 || (repeat && terrainType != WORLDMAP[c->x+1][c->y]))
blocked = true;
else
{
c->x += 1;
++dist;
}
direction = "east";
}
else
c->y -= 1;
direction = "north";
return QString("%1 is not a valid direction, use : north, south, west, east").arg(dir);
}
else if(dir.compare("south") == 0 || dir.compare("s") == 0)
{
if(c->y == 24)
blocked = true;
else
c->y += 1;
direction = "south";
}
else if(dir.compare("west") == 0 || dir.compare("w") == 0)
{
if(c->x == 0)
blocked = true;
else
c->x -= 1;
direction = "west";
}
else if(dir.compare("east") == 0 || dir.compare("e") == 0)
{
if(c->x == 24)
blocked = true;
else
c->x += 1;
direction = "east";
}
else
return QString("%1 is not a valid direction, use : north, south, west, east").arg(dir);
while(!blocked && repeat);
if(blocked)
return QString("%1 cannot go %2, a hill is blocking his way").arg(c->pseudo).arg(direction);
{
if(repeat && dist > 0)
{
QString("%1 travels %2 during %3 hours through %4")
.arg(c->pseudo).arg(direction).arg(dist).arg(currentArea);
}
else
return QString("%1 cannot go %2, a hill is blocking his way").arg(c->pseudo).arg(direction);
}
char newTerrainType = WORLDMAP[c->x][c->y];
if(terrainType == newTerrainType)
@ -206,7 +247,7 @@ bool RPGModule::messageHandler(Message msg)
answer = say(QString("%1 joins the RPG as %2").arg(msg.nick).arg(paramList[2]));
}
else
privateSay(QString("ERROR : Wrong password"), msg.nick);
answer = privateSay(QString("ERROR : Wrong password"), msg.nick);
}
else
answer = privateSay(QString("ERROR : Character name \"%1\" does not exists").arg(paramList[2]), msg.nick);
@ -225,16 +266,12 @@ bool RPGModule::messageHandler(Message msg)
{
if(c->password.compare(paramList[3]) == 0)
{
for(const std::pair<User*, RPGCharacter*> &it : charOnline)
{
if(it.second == c)
charOnline.erase(it.first);
}
charOnline.erase(c->user);
charList.erase(paramList[2].toStdString());
answer = say(QString("Character %2 has been deleted").arg(paramList[2]));
}
else
privateSay(QString("ERROR : Wrong password"), msg.nick);
answer = privateSay(QString("ERROR : Wrong password"), msg.nick);
}
else
answer = privateSay(QString("ERROR : Character name \"%1\" does not exists").arg(paramList[2]), msg.nick);
@ -289,7 +326,7 @@ bool RPGModule::messageHandler(Message msg)
QStringList paramList = msg.args.split(' ');
if(charOnline.count(src) > 0 && paramList.size() == 2)
{
answer = say(playerTravel(charOnline[src], paramList[1]));
answer = say(playerTravel(charOnline[src], paramList[1], false));
return true;
}
}
@ -304,8 +341,12 @@ bool RPGModule::messageHandler(Message msg)
}
else if(msg.args.startsWith("look "))
{
answer = say("you look around and you find a walnut.");
return true;
QStringList paramList = msg.args.split(' ');
if(charOnline.count(src) > 0 && paramList.size() == 2)
{
answer = say(lookAt(charOnline[src], paramList[1]));
return true;
}
}
else if(msg.args.compare("!rpg help") == 0)
{

View File

@ -121,6 +121,7 @@ public:
void playerJoin(User* user, RPGCharacter *c);
void playerLeave(User* user);
QString lookAt(RPGCharacter *c, QString target);
QString playerTravel(RPGCharacter *c, QString dir, bool repeat=false);
QString getTerrainName(int type);