MainWindow::MainWindow(QWindow *parent) : QQuickView(parent) { setSource(QUrl::fromLocalFile("H:/VK/main.qml").toString()); photoSize = "130"; // !!! SET QNAM AND CACHE manager = new QNetworkAccessManager(this); cache = new QNetworkDiskCache(this); // 500 Mb cache->setMaximumCacheSize(500*1024*1024); cache->setCacheDirectory("cacheDir"); manager->setCache(cache); // SET CACHED NAM TO QMLENGINE TO MyNetworkAccessManagerFactory* namFactory = new MyNetworkAccessManagerFactory(); QQmlEngine* eng = engine(); eng->setNetworkAccessManagerFactory(namFactory); QQmlProperty((QObject*)rootObject(), "color").write("#F5F5F5"); QQmlProperty((QObject*)rootObject(), "height").write(height()); wallObj = rootObject(); //wallObj->setProperty("color", "#F5F5F5"); //wallObj->setProperty("height",height()); connect((QObject*)wallObj, SIGNAL(login()),this,SLOT(login())); connect((QObject*)wallObj, SIGNAL(getGroups()),this, SLOT(getGroups())); connect((QObject*)wallObj, SIGNAL(getWall(QString)),this, SLOT(getWall(QString))); connect((QObject*)wallObj, SIGNAL(morePosts()), this, SLOT(morePosts())); connect((QObject*)wallObj, SIGNAL(setPhotoSize(QString)), this, SLOT(setPhotoSize(QString))); LoadIni("H:/VK/config.ini"); // do we have token if( settings->contains("token")){ // its a unlimit token qDebug() << " HAVE TOKEN "; if(ReadConfig("expires").toInt()==0){ Token = ReadConfig("token").toString(); } // if no doesnt it token expire else if(ReadConfig("expDate").toDate()<=QDate::currentDate()){ Token = ReadConfig("token").toString(); } // if havent token login }else{ login(); qDebug() << " NEED LOGIN "; } }
void Wall::wallCmd(const QString &from, const QStringList& list) { octAssert(list.size() < 2); if(list.size() != 0 && list[0] != "") { QString message(QDate::currentDate().toString("dd/MM/yyyy ") + QTime::currentTime().toString("hh:mm:ss ") + from); for(int i=from.length(); i<=manager()->databasePlugin()->validLoginLength(); i++) message += " "; message += list[0]; addToWall(message); manager()->logPlugin()->write("wall", message); QString txt1(from + " writes to the wall: " + list[0]); manager()->connectionPlugin()->serverBroadcastOthers(from, txt1); QString txt2("You write to the wall: " + list[0]); manager()->connectionPlugin()->serverSend(from, txt2); } else { QString wall_lines = getWall(from); QString txt; if (wall_lines == "") txt = "Wall is empty"; else txt = "Wall :\n" + wall_lines; manager()->connectionPlugin()->serverSend(from, txt); if (wall_lines != "") manager()->connectionPlugin()->serverSend(from, "End of Wall"); } }
// is mapped boolean Map::setSideMapped(byte x, byte y, compassDir side) { if(x >= 0 && x < width && y >= 0 && y < height && getWall(x, y, side) != true) { switch(side) { case NORTH: Array[x][y + 1].setSideMapped(SOUTH); break; case EAST: Array[x + 1][y].setSideMapped(WEST); break; case SOUTH: Array[x][y - 1].setSideMapped(NORTH); break; case WEST: Array[x - 1][y].setSideMapped(EAST); break; } if(getSideMapped(x, y, NORTH) && getSideMapped(x, y, EAST) && getSideMapped(x, y, SOUTH) && getSideMapped(x, y, WEST)) { Array[x][y].setMapped(); } return Array[x][y].setSideMapped(side); } else { return false; } }
void parseFloorPlan(int count, char *line) { int i; int *walls; int row; float x,y; /* make room for the data */ walls = (int *)malloc(sizeof(int) * cells_wide); /* first parse the line and determine where the walls are */ for (i = 0; i < cells_wide; i++) { walls[i] = getWall(&line); /* note that a 0 indicates no wall, else is the wall number */ if ((walls[i] < 0) || (walls[i] > num_tex)) { fprintf(stderr,"ERROR! Invalid wall specified\n"); fflush(stderr); exit(-1); } } row = count/2; y = (((float)scene_height) / (float)2) - row * cell_size; x = -(((float)scene_width) / (float)2); if (count %2) { /* count is odd, so we are specifying vertical walls */ for (i = 0; i < cells_wide; i++) { if (walls[i]) { /* add the wall to the list */ scene.addWall(x, y, x, y - (float)cell_size, wall_height, tex[walls[i] - 1]); } x += (float)cell_size; } } else { /* count is even, so we are specifying horizontal walls */ for (i = 0; i < cells_wide; i++) { if (walls[i]) { scene.addWall(x, y, x + (float)cell_size, y, wall_height, tex[walls[i] - 1]); } x += (float)cell_size; } } free(walls); return; }
void printMaze() { for(int j=0; j<COL; j++) printf(" _"); printf("\n"); for(int i=0; i<ROW; i++) { printf("|"); for(int j=0; j<COL; j++) { if(getWall(i,j,2)==0) printf("_"); else printf(" "); if(getWall(i,j,1)==0) printf("|"); else printf(" "); } printf("\n"); } }
void AI::placeHumans() { int x, y, count; count = getHumansReady(); while (count > 0) { x = rand() % 7 - 3; y = rand() % 7 - 3; if (getWall(x,y) == -1) { count -= 1; Human::spawn(weapons[4], x, y); } } }
//human i runs around randomly and attacks things if they get in the way void AI::scaredHuman(int i) { int attacks = 1; int moves; int tries; int dir; int x, y; int oldX, oldY; int zombieIndex, crateIndex; tries = 0; moves = humans[i].moves(); oldX = humans[i].x(); oldY = humans[i].y(); while ((moves > 0 || attacks > 0) && tries < 12) { dir = rand() % 6; x = nextX(oldX, oldY, dir); y = nextY(oldX, oldY, dir); zombieIndex = getZombie(x, y); crateIndex = getCrate(x, y); if (crateIndex > -1) { humans[i].grab(crates[crateIndex]); } if (zombieIndex == -1 && getWall(x, y) == -1 && moves > 0) { humans[i].move(x, y); oldX = x; oldY = y; moves -= 1; } else if (zombieIndex > -1 && attacks > 0) { humans[i].attack(zombies[zombieIndex]); attacks -= 1; } tries += 1; } }
// has been gone through boolean Map::setGoneThrough(byte x, byte y, compassDir side) { if(x >= 0 && x < width && y >= 0 && y < height && getWall(x, y, side) != true) { switch(side) { case NORTH: Array[x][y + 1].setGoneThrough(SOUTH); break; case EAST: Array[x + 1][y].setGoneThrough(WEST); break; case SOUTH: Array[x][y - 1].setGoneThrough(NORTH); break; case WEST: Array[x - 1][y].setGoneThrough(EAST); break; } setSideMapped(x, y, side); return Array[x][y].setGoneThrough(side); } else { return false; } }
//Zombie i is given orders based soley on its smell and the object // directly in front of them. void AI::blindZombie(int i) { int x, y, wallIndex, humanIndex, zombieIndex; bool allowEat = (int(getZombieCap()) == zombies.size()); if (zombies[i].smell() == 0) { zombies[i].turn((rand()%2) * 2 - 1); } else { x = nextX(zombies[i].x(), zombies[i].y(), zombies[i].facing()); y = nextY(zombies[i].x(), zombies[i].y(), zombies[i].facing()); wallIndex = getWall(x, y); humanIndex = getHuman(x, y); zombieIndex = getZombie(x, y); if (humanIndex > -1) { zombies[i].attack(humans[humanIndex]); } else if (wallIndex > -1) { zombies[i].attack(walls[wallIndex]); } else if (zombieIndex > -1 && allowEat) { zombies[i].eat(zombies[zombieIndex]); eaten += 1; } else { zombies[i].move(); } } }
float View::_computeFocusRatio( Vector3f& eye ) { eye = Vector3f::ZERO; const Observer* observer = getObserver(); const FocusMode mode = observer ? observer->getFocusMode() :FOCUSMODE_FIXED; if( mode == FOCUSMODE_FIXED ) return 1.f; const Channels& channels = getChannels(); if( channels.empty( )) return 1.f; Vector4f view4( Vector3f::FORWARD ); if( mode == FOCUSMODE_RELATIVE_TO_OBSERVER ) { view4 = observer->getHeadMatrix() * view4; eye = observer->getEyePosition( EYE_CYCLOP ); } Vector3f view = view4; view.normalize(); float distance = std::numeric_limits< float >::max(); if( getCurrentType() != Frustum::TYPE_NONE ) // frustum from view { const Wall& wall = getWall(); const Vector3f w = wall.getW(); const float denom = view.dot( w ); if( denom != 0.f ) // view parallel to wall { const float d = (wall.bottomLeft - eye).dot( w ) / denom; if( d > 0.f ) distance = d; } } else { // Find closest segment and its distance from cyclop eye for( ChannelsCIter i = channels.begin(); i != channels.end(); ++i ) { Segment* segment = (*i)->getSegment(); if( segment->getCurrentType() == Frustum::TYPE_NONE ) { segment->notifyFrustumChanged(); if( segment->getCurrentType() == Frustum::TYPE_NONE ) continue; } // http://en.wikipedia.org/wiki/Line-plane_intersection const Wall& wall = segment->getWall(); const Vector3f w = wall.getW(); const float denom = view.dot( w ); if( denom == 0.f ) // view parallel to wall continue; const float d = (wall.bottomLeft - eye).dot( w ) / denom; if( d > distance || d <= 0.f ) // further away or behind continue; distance = d; //LBINFO << "Eye " << eye << " is " << d << " from " << wall // << std::endl; } } float focusDistance = observer->getFocusDistance(); if( mode == FOCUSMODE_RELATIVE_TO_ORIGIN ) { eye = observer->getEyePosition( EYE_CYCLOP ); if( distance != std::numeric_limits< float >::max( )) { distance += eye.z(); focusDistance += eye.z(); if( fabsf( distance ) <= std::numeric_limits< float >::epsilon( )) distance = 2.f * std::numeric_limits< float >::epsilon(); } } if( distance == std::numeric_limits< float >::max( )) return 1.f; return focusDistance / distance; }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { for (int i = 0; i < 6 ;i++) { levels.append(0); } ui->setupUi(this); settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "enkidu", "battalbot"); ui->le_server->setText(settings->value("server", "").toString()); ui->le_channel->setText(settings->value("channel", "").toString()); ui->le_botname->setText(settings->value("botname", "").toString()); QHash<QString, QVariant> tmp; auths = settings->value("auths", tmp).toHash(); onFinish = false; added = 0; QLocale::setDefault(QLocale::English); trayIcon = new QSystemTrayIcon(); QMenu *menu = new QMenu(); QAction *actionQuit = new QAction("Quit", trayIcon); menu->addAction(actionQuit); trayIcon->setContextMenu(menu); trayIcon->setIcon(QIcon(":/icon.png")); trayIcon->show(); this->setWindowIcon(QIcon(":/icon.png")); this->setWindowTitle("eRepublik wall-watching BOT"); connect(ui->pb_connect, SIGNAL(clicked()), this, SLOT(connectToServer())); irc = new Irc(this); reader = new Reader; battle = new Battle; refreshTree(); connect(battle, SIGNAL(getWall()), reader, SLOT(read())); connect(ui->pb_StartCount, SIGNAL(clicked()), this, SLOT(getInfo())); connect(reader, SIGNAL(startCounting(int,QTime)), battle, SLOT(startCounting(int, QTime))); connect(reader, SIGNAL(startCounting(int,QTime)), this, SLOT(setInitialWall(int,QTime))); connect(battle, SIGNAL(setTimeT(QTime)), this, SLOT(setTimeElapsed(QTime))); connect(reader, SIGNAL(wall(int,bool)), battle, SLOT(actWall(int,bool))); connect(reader, SIGNAL(wall(int,bool)), this, SLOT(setWall(int,bool))); connect(battle, SIGNAL(setText(QString)), irc, SLOT(write(QString))); connect(reader, SIGNAL(battleInfo(QString)), irc, SLOT(setTopic(QString))); connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason))); connect(irc, SIGNAL(authRequest(QString,QString,QString)), this, SLOT(auth(QString,QString,QString))); connect(irc, SIGNAL(wallRequest(QString,QString)), this, SLOT(wall(QString,QString))); connect(irc, SIGNAL(playerRequest(QString)), reader, SLOT(getPlayer(QString))); connect(reader, SIGNAL(playerInfo(QString)), irc, SLOT(write(QString))); connect(ui->pb_authAdd, SIGNAL(clicked()), this, SLOT(addAuth())); connect(ui->pb_delAuth, SIGNAL(clicked()), this, SLOT(delAuth())); connect(actionQuit, SIGNAL(triggered()), this, SLOT(appQuit())); curve = new QwtPlotCurve(); curve->setRenderHint(QwtPlotItem::RenderAntialiased); curve->attach(ui->qwtPlot); for (int i = 0; i < 6; i++) { QwtPlotCurve *line = new QwtPlotCurve(); if (i == 0 || i == 5) { line->setPen(QPen(QColor(0,0,255))); } else { line->setPen(QPen(QColor(0,200,0))); } line->attach(ui->qwtPlot); lines.append(line); } }
//Zombie i is given orders based on an A* search to the nearest human // where nearest is defined by the zombieDistance function //Smart zombies will not break down walls, and will do nothing if // no path is available to the nearest human. void AI::smartZombie(int index) { const int FORGET = 5; //forget the last x of the generated path. const int REMEMBER = 5; //go no more than x turns without recalulating path int forgotten = 0; Zombie* me = &zombies[index]; Human* target = NULL; int bestDis = 9999999; int nextDis; int x, y, humanIndex, wallIndex; std::deque<int> path; std::cout << "smart" << std::endl; for (int i = 0; i < humans.size(); i++) { nextDis = zombieDistance(me->x(), me->y(), humans[i].x(), humans[i].y(), me->facing()); if (nextDis < bestDis) { bestDis = nextDis; target = &humans[i]; } } if (target != NULL) { path = findZombiePath(me->x(), me->y(), target->x(), target->y(), me->facing()); std::cout << "(" << me->x() << "," << me->y() << "," << target->x() << "," << target->y() << "," << me->facing() << ") = "; for (int k = 0; k < path.size(); k++) { std::cout << path[k] << " "; } if (path.size() == 0) { std::cout << "No action"; } std::cout << std::endl; if (path.size() > 1) { path.pop_front(); switch (path.front()) { case 0: x = nextX(me->x(), me->y(), me->facing()); y = nextY(me->x(), me->y(), me->facing()); humanIndex = getHuman(x, y); wallIndex = getWall(x, y); if (humanIndex > -1) me->attack(humans[humanIndex]); else if (wallIndex > -1) me->attack(walls[wallIndex]); else me->move(); break; case 1: me->turn(-1); break; case 2: me->turn(1); break; } /* while (path.size() > 0 && forgotten < FORGET) { path.pop_back(); forgotten += 1; } while (path.size() > REMEMBER) { path.pop_back(); } if (path.size() > 0) orders[me->id()] = path; */ } } std::cout << "end" << std::endl; }
//Returns a deque of zombie steps to get you to the target. //facing is the current facing //Uses A* hopefully. std::deque<int> AI::findZombiePath(int startX, int startY, int targetX, int targetY, int facing) { std::cout << "s" << std::endl; const int ZOMBIE_ATTACK = 5 * 5; int wallIndex; bool solved = false; bool addChild; std::deque<int> solution; std::list<ZombieStep*> openList; std::map<int, ZombieStep*> allStates; //(ZombieStep.mapKey(), ZombieStep) std::map<int, ZombieStep*>::iterator iter; ZombieStep* start = new ZombieStep(startX, startY, facing, NULL); ZombieStep* nextChild; ZombieStep* parent; openList.push_front(start); allStates[start->mapKey()] = start; while (openList.size() > 0 && !solved) { solved = (openList.front()->x == targetX) && (openList.front()->y == targetY); if (!solved) { for (int i = 0; i < 3; i++) { nextChild = new ZombieStep(openList.front(), i); wallIndex = getWall(nextChild->x, nextChild->y); if (wallIndex > -1) { nextChild->depth += walls[wallIndex].hp() / ZOMBIE_ATTACK; } std::map<int, ZombieStep*>::iterator iter; int index = -1; iter = allStates.find(nextChild->mapKey()); if (iter != allStates.end()) addChild = false; else addChild = true; if (nextChild->x > getMapSize() || nextChild->x < -getMapSize()) addChild = false; else if (nextChild->y > getMapSize() || nextChild->y < -getMapSize()) addChild = false; if (addChild) { nextChild->remaining = zombieDistance(nextChild->x, nextChild->y, targetX, targetY, nextChild->facing); openList.push_back(nextChild); allStates[nextChild->mapKey()] = nextChild; } else { delete nextChild; //free if not added } } openList.pop_front(); } } solution.clear(); if (solved) { parent = openList.front(); while(parent != NULL) { solution.push_front(parent->action); parent = parent->parent; } } for(iter = allStates.begin(); iter != allStates.end(); ++iter ) { delete (iter->second); } return solution; }