void chordService::detectNodes() { DEBUG_PRINT("chordService is detecting existing nodes."); struct sockaddr_in addr; int addrlen, fd; chordMessager::chordMessageDetectNode detect_msg(serviceChordNode->thisNode, node_t(),serviceChordNode->thisNode); std::string msg_serialized = detect_msg.serialize(); fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { throw ERRORS::chordServiceSocketCreateFail(); } bzero(&addr, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(SERVICE_DETECT_PORT); addrlen = sizeof(addr); addr.sin_addr.s_addr = inet_addr(GROUP_IP); for (int i = 0; i < SERVICE_DETECT_RETRY_TIME; ++i) { DEBUG_PRINT("chordService is sending detect node messages..."); if (serviceChordNode->gotDetectNodeResponse) { //chordNode has received bound back message break; } sendto(fd, msg_serialized.c_str(), msg_serialized.size(), 0, (struct sockaddr*)&addr, addrlen); //sleep for 5 seconds std::this_thread::sleep_for(std::chrono::seconds(5)); } if (!serviceChordNode->gotDetectNodeResponse) { //this is the first node in the network //ask chordnode to do self fingertable update DEBUG_PRINT("this chord node is the first in the network."); node_t dummy; serviceChordNode->join(dummy); } close(fd); }
const Master::node_t& Master::_open_file(const char* file_path, int flag, ssize_t& new_file_no)throw(std::runtime_error, std::invalid_argument, std::bad_alloc) { //open in read file_no_t::iterator it; //file not buffered const file_info *file=NULL; if(_file_no.end() == (it=_file_no.find(file_path))) { new_file_no=_get_file_no(); if(-1 != new_file_no) { //open file if(flag == O_RDONLY || flag&O_RDWR) { size_t file_length=0,block_size=0; node_t nodes=_send_request_to_IOnodes(file_path, new_file_no, flag, file_length, block_size); file_info new_file(file_path, file_length,block_size,nodes, flag); _buffered_files.insert(std::make_pair(new_file_no, new_file)); file=&(_buffered_files.at(new_file_no)); _file_no.insert(std::make_pair(file_path, new_file_no)); for(node_t::const_iterator it=nodes.begin(); nodes.end()!=it; ++it) { //send read request to each IOnode //buffer requset //file_no //file_path //start_point //block_size int socket=_registed_IOnodes.at(it->second).socket; Send(socket, READ_FILE); Send(socket, new_file_no); Send(socket, it->first); Send(socket, block_size); } } else { size_t file_length=0,block_size=0; file_info new_file(file_path, file_length, block_size, node_t(), flag); _buffered_files.insert(std::make_pair(new_file_no, new_file)); file=&(_buffered_files.at(new_file_no)); _file_no.insert(std::make_pair(file_path, new_file_no)); } } else { throw std::bad_alloc(); } } //file buffered else { file=&(_buffered_files.at(it->second)); } return file->p_node; }
hash_set<elem_t>::hash_set(int prime, int capacity) { this->prime = prime; this->capacity = capacity; head = new int[prime]; node = new node_t[capacity]; fill(head head + prime, -1); //将head填满-1 fill(node, node + capacity, node_t()); //将node填满node_t() size = 0; }
/*! * Return true if any changes were made. */ bool link_to(const magic_ref<node_t>& node, const magic_ref<std::function<void(const transaction& trans, const void*)>>& action, const magic_ref<node_t>& target, target_ref_t& target_ref) { bool changed = ensure_bigger_than(target, node->rank); std::vector<node_t::target_t> listeners = node->listeners; target_ref = ++node_t::target_t::next_target_ref; listeners.push_back(node_t::target_t(action, target, target_ref)); node.assign(node_t(node->rank, listeners)); return changed; }
void unlink_to(const magic_ref<node_t>& node, target_ref_t target_ref) { // The node we're referring to could be deleted already. This is because if // we're in a circular reference loop, we can't control what order the cycle // is deleted in. if (node) { std::vector<node_t::target_t> listeners; bool changed = false; for (auto it = node->listeners.begin(); it != node->listeners.end(); ++it) if (it->target_ref == target_ref) changed = true; else listeners.push_back(*it); if (changed) node.assign(node_t(node->rank, listeners)); } }
PUBLIC tree lib_call(def d, tree args) { int n_args = list_len(args); tree call = NULL; bool ok = TRUE; type t = d->t_type; switch (d->d_libid) { case L_CHR: ok = (n_args == 1 && same_type(car(args)->t_type, int_type)); break; case L_ORD: ok = (n_args == 1 && (same_type(car(args)->t_type, char_type) || same_type(car(args)->t_type, bool_type))); break; case L_HALT: case L_FLUSH: ok = (n_args == 0); break; case L_EOF: case L_EOLN: if (n_args == 0) args = list1((tree) _input_); else ok = (n_args = 1 && same_type(car(args)->t_type, text_type)); break; case L_READ: case L_READLN: case L_WRITE: case L_WRITELN: { tree p = args; if (p != nil && same_type(car(p)->t_type, text_type)) p = cdr(p); for (; p != nil; p = cdr(p)) { type at = car(p)->t_type; if (! (same_type(at, int_type) || same_type(at, char_type) || same_type(at, string_type))) { ok = FALSE; break; } } return node_t(LIBCALL, (tree) d, args, void_type); } case L_ARGC: ok = (n_args == 0); break; case L_ARGV: ok = (n_args == 2 && same_type(car(args)->t_type, int_type) && is_string_type(cadr(args)->t_type)); break; case L_OPENIN: ok = (n_args == 2 && same_type(car(args)->t_type, text_type) && is_string_type(cadr(args)->t_type)); break; case L_CLOSEIN: ok = (n_args == 1 && same_type(car(args)->t_type, text_type)); break; default: ok = FALSE; t = err_type; } if (! ok) { error("I choked on a library call"); return (tree) dummy_def; } if (call == NULL) return node_t(CALL, (tree) d, args, t); return call; }
void FloorMapLayer::onTouchEnded(Touch *touch, Event *e) { // bugfix: 如果对话框已弹出,就不再执行以下代码,这种情况是在对话框弹出前,touchbegan就已经触发的情况下发生 // 重现方法: 控制勇士经过怪物,然后鼠标按下等待勇士到达怪物处,弹出提示对话框后,松开鼠标继续弹出 if (ModalDialogManager::GetInstance()->isShown()) { return; } // 正在战斗中则不允许重新走动,以免计算混乱 if (_warrior->is_fighting()) { return; } // 获取起始点 auto end_vec2 = _tiled_map->convertTouchToNodeSpace(touch) / 75.0f; auto end_pt = node_t(end_vec2.x, end_vec2.y); if (!_paths.empty() && _paths.back() == end_pt) { return; } auto start_vec2 = _tiled_map->convertToNodeSpace(_warrior->getPosition()) / 75.0f; auto start_pt = node_t(start_vec2.x, start_vec2.y); if (start_pt == end_pt) { return; } // 获取阻碍点 auto wall_layer = _tiled_map->getLayer("wall"); auto wall_layer_size = wall_layer->getLayerSize(); auto pos = touch->getLocation(); auto tiles = wall_layer->getTiles(); std::vector<node_t> blocks; for (uint32_t i = 0; i < (uint32_t)(wall_layer_size.height); ++i) { for (uint32_t j = 0; j < (uint32_t)(wall_layer_size.width); ++j) { if (tiles[i * (uint32_t)(wall_layer_size.width) + j] != 0) { blocks.push_back(node_t(j, (uint32_t)(wall_layer_size.height) - 1 - i)); } } } if (std::find(blocks.begin(), blocks.end(), end_pt) != blocks.end() || end_pt.x >= 10 || end_pt.y >= 12) { return; } // npc列表 npc_t stair_up, stair_down; auto npc_layer = _tiled_map->getLayer("npc"); auto npc_layer_size = npc_layer->getLayerSize(); auto npc_tiles = npc_layer->getTiles(); _npcs.clear(); for (uint32_t i = 0; i < (uint32_t)(npc_layer_size.height); ++i) { for (uint32_t j = 0; j < (uint32_t)(npc_layer_size.width); ++j) { int32_t gid = (int32_t)(npc_tiles[i * (uint32_t)(npc_layer_size.width) + j]); if (gid != 0) { _npcs.push_back(npc_t(j, npc_layer_size.height - 1 - i, gid)); if (get_tile_prop(gid, "style").asInt() == 6) { if (get_tile_prop(gid, "type").asInt() == 1) stair_up = _npcs.back(); else stair_down = _npcs.back(); } } } } // A星路径 AStar astar(10, 12); astar.set_start_and_end(start_pt, end_pt); astar.set_blocks(blocks); _paths = astar.get_path(); auto iter = std::find_if(_paths.begin(), _paths.end(), [&](node_t node) { // 找到除了首节点的第一个楼梯节点 return (node.x != start_pt.x || node.y != start_pt.y) && (node.x == stair_up.x && node.y == stair_up.y || node.x == stair_down.x && node.y == stair_down.y); }); if (iter != _paths.end()) _paths.erase(++iter, _paths.end()); // 箭头 _arrow_node->setVisible(true); _arrow_node->setPosition(Vec2((end_pt.x + 0.5f) * 75.0f - 50.0f, (end_pt.y + 0.5f) * 75.0f - 50.0f)); // 路线 uint32_t index = 0; _road_node->removeAllChildren(); for (auto node : _paths) { if (index == _paths.size() - 1) break; auto pos = Vec2((node.x + 0.5f) * 75.0f - 50.0f, (node.y + 0.5f) * 75.0f - 50.0f); auto res = "Images/diban" + String::createWithFormat("%d", index % 22)->_string + ".png"; auto sprite = Sprite::create(res); sprite->setPosition(pos); _road_node->addChild(sprite); ++index; } // 勇士 _warrior->stopAllActions(); step(); }
path_t game_t::_compute_hero_path(int xi, int yi, int xf, int yf) { //we can reimplement this more efficiently later if need be std::list<node_t> path; std::set<node_t> open; std::set<node_t> closed; node_t start(xi, yi); start.hcost = _mh_dist(xi, yi, xf, yf); node_t end(xf, yf); //return empty path if destination unreachable if(!_map.is_visitable(end.x(), end.y()) && !_map.is_walkable(end.x(), end.y())) return path; open.insert(node_t(xi, yi)); while(!open.empty()) { node_t cur = *open.begin(); //find lowest fcost node on the open list for(std::set<node_t>::iterator it = open.begin(); it != open.end(); it++) { if((*it).fcost() < cur.fcost()) cur = *it; } //move it to the closed list open.erase(cur); closed.insert(cur); node_t adj[8]; adj[0]._x = cur.x() - 1; adj[0]._y = cur.y(); //to the left; adj[1]._x = cur.x() + 1; adj[1]._y = cur.y(); //to the right; adj[2]._x = cur.x(); adj[2]._y = cur.y() + 1; //top; adj[3]._x = cur.x(); adj[3]._y = cur.y() - 1; //below; adj[4]._x = cur.x() - 1; adj[4]._y = cur.y() + 1; //to the top left; adj[5]._x = cur.x() + 1; adj[5]._y = cur.y() + 1; //to the top right; adj[6]._x = cur.x() - 1; adj[6]._y = cur.y() - 1; //bottom left; adj[7]._x = cur.x() + 1; adj[7]._y = cur.y() - 1; //bottom right; for(int i = 0; i < 8; i++) { if(adj[i].x() < 0 || adj[i].x() > _map.width || adj[i].y() < 0 || adj[i].y() > _map.height) continue; if(closed.count(adj[i])) continue; if(adj[i] != end) { if(!_map.is_walkable(adj[i].x(), adj[i].y())) continue; } else {//problem: some objects are 'visitable' only from a certain direction (usually bottom) if(!_map.is_visitable(adj[i].x(), adj[i].y())) continue; } adj[i]._parent_x = cur.x(); adj[i]._parent_y = cur.y(); if(open.count(adj[i])) { set<node_t>::iterator it = open.find(adj[i]); node_t nt = *it; if(it->gcost > cur.gcost + (i < 4 ? 10 : 14)) { nt.gcost = cur.gcost + (i < 4 ? 10 : 14); nt._parent_x = cur.x(); nt._parent_y = cur.y(); } open.insert(it, nt); open.erase(it); } else { adj[i].gcost = cur.gcost + (i < 4 ? 10 : 14); adj[i].hcost = _mh_dist(adj[i].x(), adj[i].y(), end.x(), end.y()); open.insert(adj[i]); } if(adj[i] == end) { node_t nd = adj[i]; while(!(nd == start)) { path.push_front(nd); if(closed.count(nd.parent())) nd = (*closed.find(nd.parent())); else if(open.count(nd.parent())) nd = (*open.find(nd.parent())); else return path; //we have a problem } return path; } } } return path; }