void CPropTalking::SayPhrase(CStrID PhraseID) { PParams P = n_new(CParams); //!!! Try to find the phrase by it's ID before saying it. P->Set(CStrID("Text"), nString(PhraseID.CStr())); P->Set(CStrID("EntityID"), GetEntity()->GetUniqueID()); EventMgr->FireEvent(CStrID("ShowPhrase"), P); P = n_new(CParams); P->Set(CStrID("EntityID"), GetEntity()->GetUniqueID()); //!!! TODO: Calculate time EventMgr->FireEvent(CStrID("HidePhrase"), P, 0, 5.f); }
API void Categories_BeginCreate(const char* Name, const char* CppClass, const char* TplTable, const char* InstTable) { NewCat = n_new(Data::CParams); NewCatName = CStrID(Name); NewCat->Set(CStrID("CppClass"), nString(CppClass)); nString TplTableStr = TplTable; if (TplTableStr != nString("Tpl") + Name) NewCat->Set(CStrID("TplTableName"), TplTableStr); nString InstTableStr = InstTable; if (InstTableStr != nString("Inst") + Name) NewCat->Set(CStrID("InstTableName"), InstTableStr); NewCatProps = n_new(Data::CDataArray); NewCat->Set(CStrID("Props"), NewCatProps); }
// "Travel" command handler bool CPropTransitionZone::OnTravel(const Events::CEventBase& Event) { Data::PParams P = ((const Events::CEvent&)Event).Params; Game::CEntity* pActorEnt = EntityMgr->GetEntityByID(P->Get<CStrID>(CStrID("Actor"))); n_assert(pActorEnt); const nString& LevelID = GetEntity()->Get<nString>(Attr::TargetLevelID); const nString& DestPt = GetEntity()->Get<nString>(Attr::DestPoint); //???set pause? if (DestPt.IsValid()) { matrix44 Tfm; if (EntityFct->GetEntityAttribute<matrix44>(DestPt, Attr::Transform, Tfm)) pActorEnt->Set<matrix44>(Attr::Transform, Rotate180 * Tfm); //???or fire SetTransform? else n_printf("Travel, Warning: destination point '%s' not found\n", DestPt.Get()); } pActorEnt->Set<nString>(Attr::LevelID, LevelID); LoaderSrv->CommitChangesToDB(); P = n_new(Data::CParams(1)); P->Set(CStrID("LevelID"), LevelID); EventMgr->FireEvent(CStrID("RequestLevel"), P); OK; }
bool CActionWander::SelectAction(CActor* pActor) { if (CurrAction.isvalid()) CurrAction->Deactivate(pActor); //!!!can tune all these probabilities and timings! float Rnd = n_rand(); if (Rnd < 0.6f) { // Can also start from current direction & limit angle vector2 Dest(0.f, -1.f); Dest.rotate(n_rand(-PI, PI)); if (Rnd < 0.3f) { // NavSystem automatically clamps a destination to the navmesh Dest *= (n_rand() * 11.5f + 3.5f); pActor->GetNavSystem().SetDestPoint(vector3(Dest.x + InitialPos.x, pActor->Position.y, Dest.y + InitialPos.y)); CurrAction = n_new(CActionGoto); //vector3 Loc; //if (pActor->GetNavSystem().GetRandomValidLocation(15.f, Loc)) //{ // pActor->GetNavSystem().SetDestPoint(Loc); // CurrAction = n_new(CActionGoto); //} } else { pActor->GetMotorSystem().SetFaceDirection(vector3(Dest.x, 0.f, Dest.y)); CurrAction = n_new(CActionFace); } } else { CurrAction = NULL; NextActSelectionTime = (float)GameSrv->GetTime() + n_rand() * 5.f + 5.f; OK; } return CurrAction->Activate(pActor); }
static QuviError _http_metainfo(_quvi_http_metainfo_t qmi) { _quvi_net_t n; QuviError rc; _quvi_t q; q = qmi->handle.quvi; n = n_new(q, qmi->url.input->str); if (q->cb.http_metainfo != NULL) rc = q->cb.http_metainfo(n); else rc = c_http_metainfo(q, n); /* Query using cURL (default). */ if (rc == QUVI_OK) { rc = l_exec_util_to_file_ext(q, n->http_metainfo.content_type->str, qmi->file_ext); if (rc == QUVI_OK) { g_string_assign(qmi->content_type, n->http_metainfo.content_type->str); qmi->length_bytes = n->http_metainfo.content_length; } if (q->cb.status != NULL) { const glong p = q_makelong(QUVI_CALLBACK_STATUS_HTTP_QUERY_METAINFO, QUVI_CALLBACK_STATUS_DONE); if (q->cb.status(p, 0, q->cb.userdata.status) != QUVI_OK) rc = QUVI_ERROR_CALLBACK_ABORTED; } } else { /* Save returned error message. */ if (n->status.errmsg->len >0) g_string_assign(q->status.errmsg, n->status.errmsg->str); else { g_string_assign(q->status.errmsg, "unknown error: http_metainfo: callback returned " "an empty errmsg"); } } q->status.resp_code = n->status.resp_code; n_free(n); return (rc); }
bool CDataServer::MountNPK(const nString& NPKPath, const nString& Root) { PFileSystem NewFS = n_new(CFileSystemNPK); //!!!mangle NPKPath and check if this NPK is already mounted! nString RealRoot; if (Root.IsValid()) RealRoot = Root; else RealRoot = NPKPath.ExtractDirName(); if (!NewFS->Mount(NPKPath, RealRoot)) FAIL; FS.Append(NewFS); OK; }
CDataServer::CDataServer(): HRDCache(PParams()), Assigns(nString()) { n_assert(!Singleton); Singleton = this; DefaultFS = n_new(CFileSystemWin32); nString SysFolder; if (DefaultFS->GetSystemFolderPath(SF_HOME, SysFolder)) SetAssign("home", SysFolder); if (DefaultFS->GetSystemFolderPath(SF_BIN, SysFolder)) SetAssign("bin", SysFolder); if (DefaultFS->GetSystemFolderPath(SF_USER, SysFolder)) SetAssign("user", SysFolder); if (DefaultFS->GetSystemFolderPath(SF_TEMP, SysFolder)) SetAssign("temp", SysFolder); if (DefaultFS->GetSystemFolderPath(SF_APP_DATA, SysFolder)) SetAssign("appdata", SysFolder); if (DefaultFS->GetSystemFolderPath(SF_PROGRAMS, SysFolder)) SetAssign("programs", SysFolder); }
bool CDataServer::LoadDataSchemes(const nString& FileName) { PParams SchemeDescs = LoadHRD(FileName, false); for (int i = 0; i < SchemeDescs->GetCount(); ++i) { const CParam& Prm = SchemeDescs->Get(i); if (!Prm.IsA<PParams>()) FAIL; int Idx = DataSchemes.FindIndex(Prm.GetName()); if (Idx != INVALID_INDEX) DataSchemes.EraseAt(Idx); PDataScheme Scheme = n_new(CDataScheme); if (!Scheme->Init(*Prm.GetValue<PParams>())) FAIL; DataSchemes.Add(Prm.GetName(), Scheme); } OK; }
PParams CDataServer::ReloadHRD(const nString& FileName, bool Cache) { char* Buffer; int BytesRead = LoadFileToBuffer(FileName, Buffer); PParams Params; if (!pHRDParser.isvalid()) pHRDParser = n_new(CHRDParser); if (pHRDParser->ParseBuffer(Buffer, BytesRead, Params)) { n_printf("FileIO: HRD \"%s\" successfully loaded from HDD\n", FileName.Get()); if (Cache) HRDCache.Add(FileName.Get(), Params); //!!!???mangle/unmangle path to avoid duplicates? } else n_printf("FileIO: HRD parsing of \"%s\" failed\n", FileName.Get()); n_delete_array(Buffer); return Params; }
//!!!need desc cache! (independent from HRD cache) bool CDataServer::LoadDesc(PParams& Out, const nString& FileName, bool Cache) { PParams Main = LoadPRM(FileName, Cache); if (!Main.isvalid()) FAIL; nString BaseName; if (Main->Get(BaseName, CStrID("_Base_"))) { BaseName = "actors:" + BaseName + ".prm"; n_assert(BaseName != FileName); if (!LoadDesc(Out, BaseName, Cache)) FAIL; Out->Merge(*Main, Merge_AddNew | Merge_Replace | Merge_Deep); //!!!can specify merge flags in Desc! } else Out = n_new(CParams(*Main)); OK; }
void CCharEntity::Activate() { n_assert(!pNCharacter); n_assert(!pAnimEventHandler); CShapeEntity::Activate(); pAnimEventHandler = n_new(CCharAnimEHandler); n_assert(pAnimEventHandler); //pAnimEventHandler->AddRef(); pAnimEventHandler->Entity = this; // lookup character pointer nVariable* pVar = RenderCtx.FindLocalVar(HCharacter); if (pVar) { pNCharacter = (nCharacter2*)pVar->GetObj(); n_assert(pNCharacter); pNCharacter->SetAnimEventHandler(pAnimEventHandler); } // lookup character set HCharacterSet = pNCharacter->GetSkinAnimator()->GetCharacterSetIndexHandle(); const nVariable& CharacterSetVar = RenderCtx.GetLocalVar(HCharacterSet); pCharacterSet = (nCharacter2Set*)CharacterSetVar.GetObj(); //nClass* nCharacter3SkinAnimatorClass = nKernelServer::Instance()->FindClass("ncharacter3skinanimator"); //n_assert(nCharacter3SkinAnimatorClass); //if (pNCharacter->GetSkinAnimator()->IsA(nCharacter3SkinAnimatorClass)) //{ // nClass* nCharacter3NodeClass = nKernelServer::Instance()->FindClass("ncharacter3node"); // n_assert(nCharacter3NodeClass); // nTransformNode* pCharParentNode = GetResource().GetNode(); // n_assert(pCharParentNode); // nCharacter3Node* pCharNode = (nCharacter3Node*)FindFirstInstance(pCharParentNode, nCharacter3NodeClass); // n_assert(pCharNode); // pCharacter3Node = pCharNode; // IsChar3Mode = true; //} }
PXMLDocument CDataServer::LoadXML(const nString& FileName) //, bool Cache) { char* Buffer; int BytesRead = DataSrv->LoadFileToBuffer(FileName, Buffer); PXMLDocument XML = n_new(CXMLDocument); if (XML->Parse(Buffer, BytesRead) == tinyxml2::XML_SUCCESS) { n_printf("FileIO: XML \"%s\" successfully loaded from HDD\n", FileName.Get()); //if (Cache) XMLCache.Add(FileName.Get(), XML); //!!!???mangle/unmangle path to avoid duplicates? } else { n_printf("FileIO: XML parsing of \"%s\" failed: %s. %s.\n", FileName.Get(), XML->GetErrorStr1(), XML->GetErrorStr2()); XML = NULL; } n_delete_array(Buffer); return XML; }
bool CIngameMenuPanel::OnInventoryBtnClick(const CEGUI::EventArgs& e) { if (!pView) OK; Game::CEntity* pEnt = NULL; const CArray<CStrID>& Sel = pView->GetSelection(); for (UPTR i = 0; i < Sel.GetCount(); ++i) { CStrID EntityID = Sel[i]; pEnt = GameSrv->GetEntityMgr()->GetEntity(EntityID); if (pEnt && pEnt->HasProperty<Prop::CPropInventory>()) { Data::PParams P = n_new(Data::CParams(1)); P->Set(CStrID("EntityID"), EntityID); EventSrv->FireEvent(CStrID("ShowInventory"), P); OK; } } OK; }
/* * NAME: split() * DESCRIPTION: divide a node into two and insert a record */ static int split(node *left, byte *record, unsigned int *reclen) { btree *bt = left->bt; node n, *right = &n, *side = 0; int mark, i; /* create a second node by cloning the first */ *right = *left; if (n_new(right) == -1) goto fail; left->nd.ndFLink = right->nnum; right->nd.ndBLink = left->nnum; /* divide all records evenly between the two nodes */ mark = (NODEUSED(*left) + 2 * left->nd.ndNRecs + *reclen + 2) >> 1; if (left->rnum == -1) { side = left; mark -= *reclen + 2; } for (i = 0; i < left->nd.ndNRecs; ++i) { node *np; byte *rec; np = (mark > 0) ? right : left; rec = HFS_NODEREC(*np, i); mark -= HFS_RECLEN(*np, i) + 2; HFS_SETKEYLEN(rec, 0); if (left->rnum == i) { side = (mark > 0) ? left : right; mark -= *reclen + 2; } } compact(left); compact(right); /* insert the new record and store the modified nodes */ ASSERT(side); n_search(side, record); n_insertx(side, record, *reclen); if (bt_putnode(left) == -1 || bt_putnode(right) == -1) goto fail; /* create an index record in the parent for the new node */ n_index(right, record, reclen); /* update link pointers */ if (bt->hdr.bthLNode == left->nnum) { bt->hdr.bthLNode = right->nnum; bt->flags |= HFS_BT_UPDATE_HDR; } if (right->nd.ndFLink > 0) { node sib; if (bt_getnode(&sib, right->bt, right->nd.ndFLink) == -1) goto fail; sib.nd.ndBLink = right->nnum; if (bt_putnode(&sib) == -1) goto fail; } return 0; fail: return -1; }
/* * NAME: node->split() * DESCRIPTION: divide a node into two and insert a record */ int n_split(node *left, unsigned char *record, int *reclen) { node right; int nrecs, i; unsigned char *rec; right = *left; right.nd.ndBLink = left->nnum; if (n_new(&right) < 0) return -1; left->nd.ndFLink = right.nnum; nrecs = left->nd.ndNRecs; for (i = 0; i < nrecs; ++i) { if (i < nrecs / 2) rec = HFS_NODEREC(right, i); else rec = HFS_NODEREC(*left, i); HFS_RECKEYLEN(rec) = 0; } n_compact(left); n_compact(&right); n_search(&right, record); if (right.rnum >= 0) n_insertx(&right, record, *reclen); else { n_search(left, record); n_insertx(left, record, *reclen); } /* store the new/modified nodes */ if (bt_putnode(left) < 0 || bt_putnode(&right) < 0) return -1; /* create an index record for the new node in the parent */ n_index(right.bt, HFS_NODEREC(right, 0), right.nnum, record, reclen); /* update link pointers */ if (left->bt->hdr.bthLNode == left->nnum) { left->bt->hdr.bthLNode = right.nnum; left->bt->flags |= HFS_UPDATE_BTHDR; } if (right.nd.ndFLink) { node n; n.bt = right.bt; n.nnum = right.nd.ndFLink; if (bt_getnode(&n) < 0) return -1; n.nd.ndBLink = right.nnum; if (bt_putnode(&n) < 0) return -1; } return 0; }
PAction CTaskGoto::BuildPlan() { return n_new(CActionGoto); }
bool AStarPathPlanner::plan(const QPoint &source, const QPoint &terminal, const GridMap &grid_map, const int rows, const int cols, QGraphicsScene *scene, PathI &path) { #if DEBUG qDebug() << rows << " row(s), " << cols << " col(s)"; qDebug() << source; qDebug() << terminal; for (int j = 0; j < cols; ++j) { for (int i = 0; i < rows; ++i) std::cout << (grid_map[i][j] ? 1 : 0) << " "; std::cout << std::endl; } #endif int xs = source.x(); int ys = source.y(); int xt = terminal.x(); int yt = terminal.y(); std::priority_queue<CostNode> cost_nodes; std::vector<std::vector<bool> > visited(rows, std::vector<bool>(cols, false)); CostNode ns(xs, ys, 0, distance(source, terminal)); ns.path.push_back(source); cost_nodes.push(ns); visited[ys][xs] = true; while (true) { const CostNode n_best = cost_nodes.top(); cost_nodes.pop(); int x = n_best.x; int y = n_best.y; if (x == xt && y == yt) { path = n_best.path; break; } // 1 2 3 // 8 4 // 7 6 5 static const double gs[] = {1.414, 1.000, 1.414, 1.000, 1.414, 1.000, 1.414, 1.000}; static const int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1 }; static const int dy[] = {-1 -1, -1, 0, 1, 1, 1, 0 }; for (int i = 0; i < 8; ++i) { int x_new = x + dx[i]; int y_new = y + dy[i]; if ( x_new >= 0 && y_new >= 0 && x_new < cols && y_new < rows && !visited[y_new][x_new] && !grid_map[y_new][x_new]) { CostNode n_new(x_new, y_new, n_best.g + gs[i], distance(QPoint(x_new, y_new), terminal)); n_new.path = n_best.path; n_new.path.push_back(QPoint(x_new, y_new)); cost_nodes.push(n_new); visited[y_new][x_new] = true; } } } return true; }
PAction CActionTplWander::CreateInstance(const CWorldState& Context) const { return n_new(CActionWander); }
/* * NAME: node->split() * DESCRIPTION: divide a node into two and insert a record */ int n_split(node *left, unsigned char *record, int *reclen) { node right; int nrecs, i, mid; unsigned char *rec; right = *left; right.nd.ndBLink = left->nnum; if (n_new(&right) < 0) return -1; left->nd.ndFLink = right.nnum; nrecs = left->nd.ndNRecs; /* * Ensure node split leaves enough room for new record. * The size calculations used are based on the NODESPACE() macro, but * I don't know what the extra 2's and 1's are needed for. * John Witford <*****@*****.**> */ n_search(&right, record); mid = nrecs/2; for(;;) { if (right.rnum < mid) { if ( mid > 0 && (int)left->roff[mid] + *reclen + 2 > HFS_BLOCKSZ - 2 * (mid + 1)) { --mid; if (mid > 0) continue; } } else { if ( mid < nrecs && (int)right.roff[nrecs] - (int)right.roff[mid] + (int)left->roff[0] + *reclen + 2 > HFS_BLOCKSZ - 2 * (mid + 1)) { ++mid; if (mid < nrecs) continue; } } break; } for (i = 0; i < nrecs; ++i) { if (i < mid) rec = HFS_NODEREC(right, i); else rec = HFS_NODEREC(*left, i); HFS_RECKEYLEN(rec) = 0; } /* original code ... for (i = 0; i < nrecs; ++i) { if (i < nrecs / 2) rec = HFS_NODEREC(right, i); else rec = HFS_NODEREC(*left, i); HFS_RECKEYLEN(rec) = 0; } */ n_compact(left); n_compact(&right); n_search(&right, record); if (right.rnum >= 0) n_insertx(&right, record, *reclen); else { n_search(left, record); n_insertx(left, record, *reclen); } /* store the new/modified nodes */ if (bt_putnode(left) < 0 || bt_putnode(&right) < 0) return -1; /* create an index record for the new node in the parent */ n_index(right.bt, HFS_NODEREC(right, 0), right.nnum, record, reclen); /* update link pointers */ if (left->bt->hdr.bthLNode == left->nnum) { left->bt->hdr.bthLNode = right.nnum; left->bt->flags |= HFS_UPDATE_BTHDR; } if (right.nd.ndFLink) { node n; n.bt = right.bt; n.nnum = right.nd.ndFLink; if (bt_getnode(&n) < 0) return -1; n.nd.ndBLink = right.nnum; if (bt_putnode(&n) < 0) return -1; } return 0; }