diff --git a/app/rpgmodule.cpp b/app/rpgmodule.cpp index bbfe8f0..d5e360a 100644 --- a/app/rpgmodule.cpp +++ b/app/rpgmodule.cpp @@ -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 &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) { diff --git a/app/rpgmodule.h b/app/rpgmodule.h index 592805c..b876618 100644 --- a/app/rpgmodule.h +++ b/app/rpgmodule.h @@ -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);