Point<double> getSpawn(const char team){ switch(team){ case 'b':{ //Pd spawn1 = game.world->terrain->ToPointD(GridPoint(5, 24)); Pd spawn2 = game.world->terrain->ToPointD(GridPoint(5, 25)); int randval = rand() % 2; if(randval == 0){ return spawn2; }else{ return spawn2; } } break; case 'a':{ //Pd spawn1 = game.world->terrain->ToPointD(GridPoint(45,25)); Pd spawn2 = game.world->terrain->ToPointD(GridPoint(45,26)); int randval = rand() % 2; if(randval == 0){ return spawn2; }else{ return spawn2; } } break; default:{ return Pd(); } } }
void Weapon(WeaponType weapon) { WeaponType prevWeapon = game.player->weapon; Terrain *terrain = TO(Terrain, game.world->terrain); if (prevWeapon == weapon) return; game.player->weapon = weapon; if (weapon == weapWrench) { // Goto build mode terrain->showGrid = true; #ifndef AUTOBUILD game.world->hud->buildselector->show = true; #endif //game.controller->setView(true); } else if (prevWeapon == weapWrench) { // Leave build mode terrain->showGrid = false; #ifndef AUTOBUILD game.world->hud->buildselector->show = false; #endif game.world->terrain->setSelected(GridPoint(-1, -1)); //game.controller->restoreView(); } }
void Group::carryOverFormationStatistics() { width = formation->getWidth(); height = formation->getHeight(); base = vector<vector<Unit*> >(width, vector<Unit*>(height)); grid = vector<vector<Unit*> >(width, vector<Unit*>(height)); for (int x = 0; x < width; ++x) for (int y = 0; y < height; ++y) { if (formation->getCharacterAt(x, y) != NULL) { Unit* unit = new Unit(formation->getCharacterAt(x, y), gid, x, y); unit->applyCharacterSkillSets(); base[x][y] = unit; grid[x][y] = base[x][y]; if (formation->getLeaderPosition() == GridPoint(x, y)) { leader = unit; unit->setLeader(true); } addUnitToGroup(unit); } else { base[x][y] = NULL; grid[x][y] = NULL; } } }
Grid(int x, int y, int z, float spacing, float pos_x, float pos_y, float pos_z) : dimx(x), dimy(y), dimz(z), h(spacing), x_off(pos_x), y_off(pos_y), z_off(pos_z) { for (int i = 0; i < dimx * dimy * dimz; i++) { gridPoints.push_back(GridPoint()); } float above = spacing * ((float)y) - (float(GRID_COLLISION_PLANE_OFFSET)) * spacing; float right = spacing * ((float)x) - (float(GRID_COLLISION_PLANE_OFFSET)) * spacing; float before = spacing * ((float)z) - (float(GRID_COLLISION_PLANE_OFFSET)) * spacing; float behind = (float(GRID_COLLISION_PLANE_OFFSET)) * spacing; float left = (float(GRID_COLLISION_PLANE_OFFSET)) * spacing; float below = (float(GRID_COLLISION_PLANE_OFFSET)) * spacing; gridBorders.push_back( Vector4f(pos_x + left, pos_y + below, pos_z + behind, 1.0f)); gridBorders.push_back( Vector4f(pos_x + left, pos_y + above, pos_z + behind, 1.0f)); gridBorders.push_back( Vector4f(pos_x + right, pos_y + above, pos_z + behind, 1.0f)); gridBorders.push_back( Vector4f(pos_x + right, pos_y + below, pos_z + behind, 1.0f)); gridBorders.push_back( Vector4f(pos_x + left, pos_y + below, pos_z + before, 1.0f)); gridBorders.push_back( Vector4f(pos_x + left, pos_y + above, pos_z + before, 1.0f)); gridBorders.push_back( Vector4f(pos_x + right, pos_y + above, pos_z + before, 1.0f)); gridBorders.push_back( Vector4f(pos_x + right, pos_y + below, pos_z + before, 1.0f)); // gridBorders.at(0).print(); iGridBorders.push_back(0); iGridBorders.push_back(1); iGridBorders.push_back(5); iGridBorders.push_back(4); iGridBorders.push_back(0); iGridBorders.push_back(3); iGridBorders.push_back(7); iGridBorders.push_back(4); iGridBorders.push_back(5); iGridBorders.push_back(6); iGridBorders.push_back(7); iGridBorders.push_back(3); iGridBorders.push_back(2); iGridBorders.push_back(6); iGridBorders.push_back(5); iGridBorders.push_back(1); iGridBorders.push_back(2); iGridBorders.push_back(3); iGridBorders.push_back(0); }
void ImplicitSampler::update_samples(GridPoint _newPt, UniformGrid* _grid) { GridPoint pt = sampled_pts[_newPt.ind]; if(!_grid->remove_point(pt.pt)) cerr << "error: should have removed point!" << endl; _grid->add_point(GridPoint(_newPt.pt, _newPt.ind)); sampled_pts[_newPt.ind] = _newPt; }
void CUnit::Move(const CPoint& newMapPoint){ CPoint newGridPoint(CConvert::GetGridPointByMapPoint(newMapPoint)); CGameMap* map = CGameMap::GetMap(); this->ClearVisionCount(); if(GridPoint()!=newGridPoint){//移動到新的地方 if(map->GetMapTile(newGridPoint)->Sprites().empty()){//可以移動 this->SetMapPointAndMapTile(newMapPoint); } }else{ this->SetMapPoint(newMapPoint); } this->SetVisionCount(); }
void Test(string str) { if(str == "a"){ game.world->children.insert(Droppable(Pd(0.0, 0.0, 0.0), 30)); }else if(str == "b"){ game.world->temporary.push_back(Droppable(Pd(0.0, 0.0, 0.0), 30)); }else{ Player::Id pid2 = game.topId++; ObjectHandle player2 = Player(pid2, 'a', "Carl"); game.root->children.insert(player2); game.players[pid2] = player2; if(game.teams.count('a')) game.teams['a'].resources = 1000; game.world->terrain->placeStructure(GridPoint(22,24), DefenseTower(pid2)); } }
void Build() { Camera &cam = game.controller->camera; GridPoint clicked = game.world->terrain->getGridCoordinates(cam.origin, cam.objective); if(clicked.isValid()) { #ifdef AUTOBUILD int structure = game.world->terrain->canPlaceStructure(clicked); ObjectHandle tower; switch(structure){ case 1: case 11: tower = Objects::DefenseTower(game.player->id); break; case 2: case 12: tower = Objects::ResourceMine(game.player->id); break; case 3: case 13: tower = Objects::RichResourceMine(game.player->id); break; default: tower = ObjectHandle(); } Resource cost = 0; if(tower){ Building *b = TO(Building, tower); if(b) cost = b->cost; } map<unsigned char,Team>::iterator it = Game::game.teams.find(Game::game.player->team); if(it != Game::game.teams.end()){ if(it->second.resources >= cost){ game.world->terrain->setSelected(GridPoint(-1, -1)); if (!game.world->terrain->placeStructure(clicked, tower)){ Echo("There's already a tower there"); }else{ NetCode::Build(clicked,TO(Structure,tower)); it->second.resources -= cost; } }else{ Echo("You don't have enough money"); } } #endif } else Echo("Invalid place to build"); }
GridPoint ImplicitSampler::projection(Vector3 _pt, GridPoint _samplerPt) { int projection_iterations = 10; double lambda = 0.75; double prev_dist = 1e10; Vector3 pt = _pt; for(int j = 0; j < projection_iterations; j++) { double pt_function = surface->function(pt); Vector3 pt_gradient = surface->gradient(pt); Vector3 pt_normal = pt_gradient; pt_normal.normalize(); pt_normal.scale(-(pt_function*lambda)/pt_gradient.length()); pt = pt + pt_normal; double next_dist = pt_function / pt_gradient.length(); double next_abs = fabs(next_dist); if(next_abs < 1e-3) break; if(next_abs > prev_dist) cerr << "error in projection: " << next_abs << " : " << prev_dist << endl; prev_dist = next_abs; } return GridPoint(pt, _samplerPt.ind); }
void Initialize(int argc, char *argv[]) { // Process command line arguments for (int i = 0; i < argc - 1; ++i) { if (!strcmp(argv[i], "-x") || !strcmp(argv[i], "--map-width")) gameWidth = atof(argv[++i]); else if (!strcmp(argv[i], "-y") || !strcmp(argv[i], "--map-height")) gameHeight = atof(argv[++i]); else if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--path")) path = argv[++i]; else if (!strcmp(argv[i], "-w") || !strcmp(argv[i], "--screen-width")) windowWidth = atoi(argv[++i]); else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--screen-height")) windowHeight = atoi(argv[++i]); } // Process config file if (!file_exists(CONFIG_FILE)) { ConfigFile newconfig; newconfig.add("playername", "Unnamed"); newconfig.add("team", 'a'); newconfig.save(CONFIG_FILE); } try { config = new ConfigFile(CONFIG_FILE); } catch (ConfigFile::file_not_found e) { delete config; config = NULL; puts("Unable to create config file: " CONFIG_FILE "!"); exit(EXIT_FAILURE); } fullscreen = (config->read("fullscreen", '0')) == '1'? true : false; /* deprecated: for (int i = 0; i < argc; ++i) { if (!strcmp(argv[i], "--fullscreen")) fullscreen = true; }*/ // Global subsystem initializations srand(time(NULL)); // Create window and set up viewports game.window = new Video::Window(windowWidth, windowHeight, GAME_NAME, fullscreen); Video::Viewport *view = new Video::Viewport(1,1); game.window->viewports.push_back(view); // Show loading screen // Problem: we can't use the hud prior to asset loading // Load game assets Assets::Initialize(argc, argv); config->readInto(NetCode::MessageOfTheDay, "motd", string("No message of the day ;(")); // Set up game world game.root = World(gameWidth, gameHeight); game.world = TO(World,game.root); game.topId = 1; game.teams.insert(make_pair('a', Team('a'))); game.teams.insert(make_pair('b', Team('b'))); string name; unsigned char team; config->readInto(name, "playername", string("Unnamed")); team = config->read("team", 'a'); Player::Id pid = game.topId++; ObjectHandle player = Player(pid, team, name); game.player = TO(Player,player); game.player->weapon = weapLaser; game.player->origin = getSpawn(team); game.root->children.insert(player); game.players[pid] = player; game.world->terrain->placeStructure(GridPoint(2,2), Mine()); game.world->terrain->placeStructure(GridPoint(2,48), Mine()); game.world->terrain->placeStructure(GridPoint(48,2), Mine()); game.world->terrain->placeStructure(GridPoint(48,48), Mine()); game.world->terrain->placeStructure(GridPoint(25,25), RichMine()); game.world->terrain->placeStructure(GridPoint(15,25), Mine()); game.world->terrain->placeStructure(GridPoint(35,25), Mine()); game.world->terrain->placeStructure(GridPoint(25,5), RichMine()); game.world->terrain->placeStructure(GridPoint(25,45), RichMine()); game.world->terrain->placeStructure(GridPoint(6,9), Mine()); game.world->terrain->placeStructure(GridPoint(6,41), Mine()); game.world->terrain->placeStructure(GridPoint(44,9), Mine()); game.world->terrain->placeStructure(GridPoint(44,41), Mine()); game.world->terrain->placeStructure(GridPoint(10,17), Mine()); game.world->terrain->placeStructure(GridPoint(10,33), Mine()); game.world->terrain->placeStructure(GridPoint(40,17), Mine()); game.world->terrain->placeStructure(GridPoint(40,33), Mine()); ObjectHandle RedBot = player; ObjectHandle BlueBot = player; if (team == 'b') { RedBot = Player(INT_MAX - 'a', 'a', "RedBot", Pd(-1000,-1000,-1000)); //game.world->children.insert(RedBot); game.players[TO(Player, RedBot)->id] = RedBot; } else if (team == 'a') { BlueBot = Player(INT_MAX - 'b', 'b', "BlueBot", Pd(-1000,-1000,-1000)); //game.world->children.insert(BlueBot); game.players[TO(Player, BlueBot)->id] = BlueBot; } game.world->terrain->placeStructure(GridPoint(3,25), HeadQuarters(TO(Player, BlueBot)->id)); game.world->terrain->placeStructure(GridPoint(48,26), HeadQuarters(TO(Player, RedBot)->id)); game.world->terrain->placeStructure(GridPoint(4,26), DefenseTower(TO(Player, BlueBot)->id)); game.world->terrain->placeStructure(GridPoint(4,23), DefenseTower(TO(Player, BlueBot)->id)); game.world->terrain->placeStructure(GridPoint(46,27), DefenseTower(TO(Player, RedBot)->id)); game.world->terrain->placeStructure(GridPoint(46,24), DefenseTower(TO(Player, RedBot)->id)); for(int i = 0; i <= 50; i++) { game.world->terrain->placeStructure(GridPoint(0,i), Wall()); game.world->terrain->placeStructure(GridPoint(50,i), Wall()); game.world->terrain->placeStructure(GridPoint(i,0), Wall()); game.world->terrain->placeStructure(GridPoint(i,50), Wall()); } for(int i = 8; i <= 42; i++) { if (i < 23 || i > 27) { game.world->terrain->placeStructure(GridPoint(25,i), Wall()); game.world->terrain->placeStructure(GridPoint(i,5), Wall()); game.world->terrain->placeStructure(GridPoint(i,45), Wall()); } if (i < 22 || i > 28) { game.world->terrain->placeStructure(GridPoint(i,15), Wall()); game.world->terrain->placeStructure(GridPoint(i,35), Wall()); } if (i < 13 || i > 37) { game.world->terrain->placeStructure(GridPoint(i,25), Wall()); } } for (int i = 5; i <=11; i++) { game.world->terrain->placeStructure(GridPoint(4,i), Wall()); game.world->terrain->placeStructure(GridPoint(46,i), Wall()); game.world->terrain->placeStructure(GridPoint(i,11), Wall()); game.world->terrain->placeStructure(GridPoint(i,39), Wall()); } for (int i = 39; i <=45; i++) { game.world->terrain->placeStructure(GridPoint(4,i), Wall()); game.world->terrain->placeStructure(GridPoint(46,i), Wall()); game.world->terrain->placeStructure(GridPoint(i,11), Wall()); game.world->terrain->placeStructure(GridPoint(i,39), Wall()); } for (int i = 15; i <= 35; i++) { game.world->terrain->placeStructure(GridPoint(8,i), Wall()); game.world->terrain->placeStructure(GridPoint(42,i), Wall()); } for (int i = 3; i <= 47; i++) { if (i < 12 || i > 38 || (i > 19 && i < 31)) { game.world->terrain->placeStructure(GridPoint(18,i), Wall()); game.world->terrain->placeStructure(GridPoint(32,i), Wall()); } } for (int i = 13; i <= 37; i++) { if (i < 18 || i > 32) { game.world->terrain->placeStructure(GridPoint(i,20), Wall()); game.world->terrain->placeStructure(GridPoint(i,30), Wall()); } } game.world->terrain->placeStructure(GridPoint(11,1), Wall()); game.world->terrain->placeStructure(GridPoint(11,2), Wall()); game.world->terrain->placeStructure(GridPoint(11,49), Wall()); game.world->terrain->placeStructure(GridPoint(11,48), Wall()); game.world->terrain->placeStructure(GridPoint(39,1), Wall()); game.world->terrain->placeStructure(GridPoint(39,2), Wall()); game.world->terrain->placeStructure(GridPoint(39,49), Wall()); game.world->terrain->placeStructure(GridPoint(39,48), Wall()); // Set up user interface view->world = game.root; game.controller = new Controller(view->camera, player); game.input = new Input(*game.window); game.input->onKeyUp = KeyUp; game.input->onKeyDown = KeyDown; //input->onMouseMove = MouseMove; Echo("Everything loaded!"); Echo("Welcome to the game"); Echo(NetCode::MessageOfTheDay); }
/** * 持续移动 */ void RpgPlayer::doContinueMove(int nowDir) { continueMoveDir = nowDir; nowDestination = GridPoint(-1,-1); }
GridPoint operator*(const double lambda, const GridPoint& point) { return GridPoint(lambda * point.x, lambda * point.y); }
GridPoint GridPoint::operator*(const double lambda) const { return GridPoint(lambda * x, lambda * y); }
GridPoint GridPoint::operator+(const GridPoint& other) const { return GridPoint(x + other.x, y + other.y); }
GridPoint Grid::center() const { return GridPoint(width / 2, height / 2); }
/* * @descr effect of pressing mousse * @param QMouseEvent * @return nothing */ void GameScreen::mousePressEvent(QMouseEvent *e) { QGraphicsView::mousePressEvent(e); GridPoint(e); click(); }
void ImplicitSampler::init() { surface_area = 0; pi_over_2 = 0.5*acos(-1); vector<TriIndex> all_tris; BasicTriMesh* mc_mesh = surface->isosurface(64,256,0.01); ll_bound = Vector3(1e10,1e10,1e10); ur_bound = Vector3(-1e10,-1e10,-1e10); for(int t = 0; t < mc_mesh->n_faces(); t++) { BasicTriMesh::FaceVertexIter fv_it = mc_mesh->fv_iter(OpenMesh::FaceHandle(t)); BasicTriMesh::VertexHandle v0 = fv_it.handle(); BasicTriMesh::VertexHandle v1 = (++fv_it).handle(); BasicTriMesh::VertexHandle v2 = (++fv_it).handle(); BasicTriMesh::Point p0 = mc_mesh->point(v0), p1 = mc_mesh->point(v1), p2 = mc_mesh->point(v2); BasicTriMesh::Point raw_normal = (p1-p0) % (p2-p0); double tri_area = 0.5*raw_normal.length(); Vector3 vert0(p0[0],p0[1],p0[2]); Vector3 vert1(p1[0],p1[1],p1[2]); Vector3 vert2(p2[0],p2[1],p2[2]); sampler_tris.push_back(SamplerTri(vert0,vert1,vert2)); ll_bound.expand_min_bound(vert0); ur_bound.expand_max_bound(vert0); ll_bound.expand_min_bound(vert1); ur_bound.expand_max_bound(vert1); ll_bound.expand_min_bound(vert2); ur_bound.expand_max_bound(vert2); if(p0 == p1 || p0 == p2 || p1 == p2 || tri_area < 1e-9) continue; all_tris.push_back(TriIndex(tri_area, t)); surface_area += tri_area; } std::sort(all_tris.begin(),all_tris.end()); global_radius = 2.5*sqrt(surface_area / ((double)num_points)); vector<TriIndex> cum_tris; for(unsigned t = 0; t < all_tris.size(); t++) { TriIndex next_tri = all_tris[t]; if(t == 0) cum_tris.push_back(next_tri); else { TriIndex prev_tri = cum_tris[t-1]; double cum_area = next_tri.area + prev_tri.area; cum_tris.push_back(TriIndex(cum_area, next_tri.tri)); } } time_t cur_time; time(&cur_time); srand(cur_time); for(int i = 0; i < 20; i++) rand(); sampled_pts = new GridPoint[num_points]; double total_area = cum_tris[cum_tris.size()-1].area; for(int s = 0; s < num_points; s++) { double rand_unit = (double)rand() / (double)RAND_MAX; double rand_area = rand_unit*total_area; int rand_ind = this->tri_search(&cum_tris, rand_area, 0, cum_tris.size()-1); TriIndex rand_tri_index = cum_tris[rand_ind]; SamplerTri rand_tri = sampler_tris[rand_tri_index.tri]; double r1 = (double)rand() / (double)RAND_MAX; double r2 = (double)rand() / (double)RAND_MAX; double r1_sqrt = sqrt(r1); Vector3 rand_pt = rand_tri.v1*(1.0-r1_sqrt) + rand_tri.v2*(r1_sqrt*(1.0-r2)) + rand_tri.v3*(r1_sqrt*r2); sampled_pts[s] = GridPoint(rand_pt, s); } grid_res_x = (ur_bound.x-ll_bound.x)/global_radius; grid_res_y = (ur_bound.y-ll_bound.y)/global_radius; grid_res_z = (ur_bound.z-ll_bound.z)/global_radius; grid_res_x = grid_res_x > 300 ? 300 : grid_res_x; grid_res_y = grid_res_y > 300 ? 300 : grid_res_y; grid_res_z = grid_res_z > 300 ? 300 : grid_res_z; cout << "grid res: " << grid_res_x << " : " << grid_res_y << " : " << grid_res_z << endl; delete mc_mesh; }