void ireon::kd_tree::MManager<T>::FreeObjectList( ObjectList<T>* a_List ) { ObjectList<T>* list = a_List; while (list->getNext()) list = list->getNext(); list->setNext( m_OList ); m_OList = a_List; }
int ireon::kd_tree::KdTree<T>::FindNearest( Ray& a_Ray, real& a_Dist, T*& a_Prim , kdstack<T> * const m_Stack, const aabb* extendBox) { real tnear = 0, tfar = a_Dist, t; int retval = 0; Vector3 p1 = extendBox->getPos(); Vector3 p2 = p1 + extendBox->getSize(); Vector3 D = a_Ray.getDirection(), O = a_Ray.getOrigin(); for ( int i = 0; i < 3; i++ ) if (D.val[i] < 0) { if (O.val[i] < p1.val[i]) return 0; } else if (O.val[i] > p2.val[i]) return 0; // clip ray segment to box for ( int i = 0; i < 3; i++ ) { real pos = O.val[i] + tfar * D.val[i]; if (D.val[i] < 0) { // clip end point if (pos < p1.val[i]) tfar = tnear + (tfar - tnear) * ((O.val[i] - p1.val[i]) / (O.val[i] - pos)); // clip start point if (O.val[i] > p2.val[i]) tnear += (tfar - tnear) * ((O.val[i] - p2.val[i]) / (tfar * D.val[i])); } else { // clip end point if (pos > p2.val[i]) tfar = tnear + (tfar - tnear) * ((p2.val[i] - O.val[i]) / (pos - O.val[i])); // clip start point if (O.val[i] < p1.val[i]) tnear += (tfar - tnear) * ((p1.val[i] - O.val[i]) / (tfar * D.val[i])); } if (tnear > tfar) return 0; } // init stack int entrypoint = 0, exitpoint = 1; // init traversal KdTreeNode<T>* farchild, *currnode; currnode = getRoot(); m_Stack[entrypoint].t = tnear; if (tnear > 0.0f) m_Stack[entrypoint].pb = O + D * tnear; else m_Stack[entrypoint].pb = O; m_Stack[exitpoint].t = tfar; m_Stack[exitpoint].pb = O + D * tfar; m_Stack[exitpoint].node = 0; // traverse kd-tree while (currnode) { while (!currnode->isLeaf()) { real splitpos = currnode->getSplitPos(); int axis = currnode->getAxis(); if (m_Stack[entrypoint].pb.val[axis] <= splitpos) { if (m_Stack[exitpoint].pb.val[axis] <= splitpos) { currnode = currnode->getLeft(); continue; } if (m_Stack[exitpoint].pb.val[axis] == splitpos) { currnode = currnode->getRight(); continue; } currnode = currnode->getLeft(); farchild = currnode + 1; // GetRight(); } else { if (m_Stack[exitpoint].pb.val[axis] > splitpos) { currnode = currnode->getRight(); continue; } farchild = currnode->getLeft(); currnode = farchild + 1; // GetRight(); } t = (splitpos - O.val[axis]) / D.val[axis]; int tmp = exitpoint++; if (exitpoint == entrypoint) exitpoint++; m_Stack[exitpoint].prev = tmp; m_Stack[exitpoint].t = t; m_Stack[exitpoint].node = farchild; m_Stack[exitpoint].pb.val[axis] = splitpos; int nextaxis = m_Mod[axis + 1]; int prevaxis = m_Mod[axis + 2]; m_Stack[exitpoint].pb.val[nextaxis] = O.val[nextaxis] + t * D.val[nextaxis]; m_Stack[exitpoint].pb.val[prevaxis] = O.val[prevaxis] + t * D.val[prevaxis]; } ObjectList<T>* list = currnode->getList(); real dist = m_Stack[exitpoint].t; while (list) { T* pr = list->getPrimitive(); int result; m_Intersections++; if (result = intersect( pr, a_Ray, dist )) { retval = result; a_Dist = dist; a_Prim = pr; } list = list->getNext(); } if (retval) return retval; entrypoint = exitpoint; currnode = m_Stack[exitpoint].node; exitpoint = m_Stack[entrypoint].prev; } return 0; }
// ----------------------------------------------------------------- // Name : setCurrentAvatar // ----------------------------------------------------------------- void LevelUpDlg::setCurrentAvatar(AvatarData * pAvatar) { m_pCurrentAvatar = pAvatar; short iSpecialLevels[MAX_LEVELS] = { 0, 1, -1, 2, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1, -1 }; deleteAllComponents(); // Init position for components int iWidth = getWidth(); int yPxl = 10; // Top label char sText[LABEL_MAX_CHARS]; i18n->getText("LEVEL_UP", sText, LABEL_MAX_CHARS); guiLabel * pLbl = new guiLabel(); pLbl->init(sText, H1_FONT, H1_COLOR, "TopLabe", 0, 0, iWidth - 10, 0, m_pLocalClient->getDisplay()); pLbl->moveTo((iWidth - pLbl->getWidth()) / 2, yPxl); addComponent(pLbl); // Text yPxl += pLbl->getHeight() + 15; u16 uLevel = pAvatar->getRealLevel() + 1; assert(uLevel > 1); char sBuf[256]; i18n->getText("(s)_LEVELED_UP_AND_IS_(d)", sBuf, 256); snprintf(sText, LABEL_MAX_CHARS, sBuf, pAvatar->m_sCustomName, (int)uLevel); pLbl = new guiLabel(); pLbl->init(sText, TEXT_FONT, TEXT_COLOR, "CenterLabe", 0, 0, iWidth - 10, 0, m_pLocalClient->getDisplay()); pLbl->moveTo(5, yPxl); addComponent(pLbl); Edition * pEdition = m_pLocalClient->getDataFactory()->findEdition(pAvatar->m_sEdition); assert(pEdition != NULL); m_iSpecialLevel = iSpecialLevels[uLevel-1]; if (m_iSpecialLevel >= 0) { u8 uTreeType = -1; char sPhraseKey[128] = ""; switch (m_iSpecialLevel) { case 1: uTreeType = PROGRESSION_MAGIC; wsafecpy(sPhraseKey, 128, "CHOOSE_MAGIC_PATH"); break; case 2: case 3: uTreeType = PROGRESSION_TRAIT; wsafecpy(sPhraseKey, 128, "CHOOSE_CHARACTER_TRAIT"); break; } assert(uTreeType >= 0); // Text i18n->getText(sPhraseKey, sText, LABEL_MAX_CHARS); yPxl += pLbl->getHeight() + 2 * SPACING; pLbl = new guiLabel(); pLbl->init(sText, TEXT_FONT, TEXT_COLOR, "", SPACING, yPxl, iWidth - 2 * SPACING, 0, getDisplay()); addComponent(pLbl); // Open tree combo yPxl += pLbl->getHeight() + SPACING; guiComboBox * pCombo = guiComboBox::createDefaultComboBox("OpenTreeCombo", m_pLocalClient->getInterface(), getDisplay()); pCombo->moveTo(SPACING, yPxl); addComponent(pCombo); ObjectList * pTrees = new ObjectList(false); pEdition->getAllTreesByType(pTrees, uTreeType); ProgressionTree * pTree = (ProgressionTree*) pTrees->getFirst(0); while (pTree != NULL) { pTree->findLocalizedElement(sText, LABEL_MAX_CHARS, i18n->getCurrentLanguageName(), "name"); guiButton * pBtn = pCombo->addString(sText, "OpenTreeButton"); pBtn->setAttachment(pTree); pTree->getDescription(sText, LABEL_MAX_CHARS, m_pLocalClient); pBtn->setTooltipText(sText); pTree = (ProgressionTree*) pTrees->getNext(0); } delete pTrees; yPxl += pCombo->getHeight() + SPACING; } else { int xTree = SPACING; int btnSize = 64; int treeWidth = 2 * btnSize + btnSize / 3; int treeTop = yPxl + pLbl->getHeight() + 2 * SPACING; // Trees for (int i = 0; i < NB_PROGRESSION_TREES; i++) { if (strcmp(pAvatar->m_pProgression[i].sTreeName, "") == 0) continue; ProgressionTree * pTree = pEdition->findProgressionTree(pAvatar->m_pProgression[i].sTreeName); assert(pTree != NULL); // Top label yPxl = treeTop; pTree->findLocalizedElement(sText, LABEL_MAX_CHARS, i18n->getCurrentLanguageName(), "name"); pLbl = new guiLabel(); pLbl->init(sText, H2_FONT, H2_COLOR, "", 0, 0, 0, 0, getDisplay()); pLbl->moveTo(xTree + treeWidth / 2 - pLbl->getWidth() / 2, yPxl); addComponent(pLbl); // Choices s32 iTexChoice = m_pLocalClient->getDisplay()->getTextureEngine()->loadTexture("progression_choice"); s32 iTexChoiceLeft = m_pLocalClient->getDisplay()->getTextureEngine()->loadTexture("progression_choice_"); s32 iTexChoiceRight = m_pLocalClient->getDisplay()->getTextureEngine()->loadTexture("progression_choice_r"); int iChoiceWidth = m_pLocalClient->getDisplay()->getTextureEngine()->getTexture(iTexChoice)->m_iWidth; // image is centered on texture, so we take its width bool bLastChosenLeft = true; // branch image yPxl += pLbl->getHeight() + SPACING; guiImage * pImg = new guiImage(); pImg->init(iTexChoice, "", xTree + treeWidth / 2 - iChoiceWidth / 2, yPxl, -1, -1, getDisplay()); addComponent(pImg); // Get tree root choices yPxl += pImg->getHeight(); int xPxl = xTree; bool bFirstCol = true; ProgressionElement * pChosen = getChosenElementAtLevel(pTree, i, 0); ProgressionElement * pElt = (ProgressionElement*) pTree->m_pElements[0]->getFirst(0); while (pElt != NULL) { addChoiceButton(xPxl, yPxl, btnSize, pElt, pChosen == NULL ? 2 : (pChosen == pElt ? 1 : 0)); if (pChosen == pElt) bLastChosenLeft = bFirstCol; if (bFirstCol) xPxl += btnSize + btnSize / 3; else { xPxl = xTree; yPxl += btnSize + SPACING; } bFirstCol = !bFirstCol; pElt = (ProgressionElement*) pTree->m_pElements[0]->getNext(0); } if (!bFirstCol) yPxl += btnSize + SPACING; int iLevel = 1; while (pChosen != NULL) { // branch image pImg = new guiImage(); pImg->init(bLastChosenLeft ? iTexChoiceLeft : iTexChoiceRight, "", xTree + treeWidth / 2 - iChoiceWidth / 2, yPxl, -1, -1, getDisplay()); addComponent(pImg); yPxl += pImg->getHeight(); xPxl = xTree; bFirstCol = true; ProgressionElement * pPrevChosen = pChosen; pChosen = getChosenElementAtLevel(pTree, i, iLevel++); pElt = (ProgressionElement*) pPrevChosen->m_pChildren->getFirst(0); while (pElt != NULL) { addChoiceButton(xPxl, yPxl, btnSize, pElt, pChosen == NULL ? 2 : (pChosen == pElt ? 1 : 0)); if (pChosen == pElt) bLastChosenLeft = bFirstCol; if (bFirstCol) xPxl += btnSize + btnSize / 3; else { xPxl = xTree; yPxl += btnSize + SPACING; } bFirstCol = !bFirstCol; pElt = (ProgressionElement*) pPrevChosen->m_pChildren->getNext(0); } if (!bFirstCol) yPxl += btnSize + SPACING; } xTree += treeWidth + SPACING; } } // Create button "Do it later" yPxl += SPACING; float fx = getWidth() / 8; guiButton * pBtn = guiButton::createDefaultNormalButton(i18n->getText("DO_IT_LATER", sText, 256), "DoItLaterButton", m_pLocalClient->getDisplay()); pBtn->moveTo((int)fx, yPxl); pBtn->setWidth(2*fx); pBtn->setTooltipText(i18n->getText("DO_IT_LATER_EXP", sText, 256)); addComponent(pBtn); // Create button "Ok" pBtn = (guiButton*) pBtn->clone(); pBtn->setText(i18n->getText1stUp("OK", sText, 256)); pBtn->setId("OkButton"); pBtn->moveBy((int)(4 * fx), 0); pBtn->setEnabled(false); addComponent(pBtn); // Size setHeight(yPxl + pBtn->getHeight() + SPACING); // Show frame m_pLocalClient->getInterface()->setUniqueDialog(this); }
void ireon::kd_tree::KdTree<T>::subdivide( KdTreeNode<T>* node, const aabb& box, int depth, int a_Prims ) { // recycle used split list nodes //add sPool_ at end sList_ if (sList_) { SplitList* list = sList_; while (list->next) list = list->next; list->next = sPool_; sPool_ = sList_, sList_ = 0; } // determine split axis Vector3 s = box.getSize(); if ((s.x >= s.y) && (s.x >= s.z)) node->setAxis( 0 ); else if ((s.y >= s.x) && (s.y >= s.z)) node->setAxis( 1 ); int axis = node->getAxis(); // make a list of the split position candidates ObjectList<T>* l = node->getList(); real p1, p2; real pos1 = box.getPos().val[axis]; real pos2 = box.getPos().val[axis] + box.getSize().val[axis]; bool* pright = new bool[a_Prims]; float* eleft = new float[a_Prims], *eright = new float[a_Prims]; T** parray = new T*[a_Prims]; real etleft, etright; int aidx = 0; while (l) { T* p = parray[aidx] = l->getPrimitive(); pright[aidx] = true; etleft = eleft[aidx]; etright = eright[aidx]; p->calculateRange( etleft, etright, axis ); eleft[aidx] = (float)etleft; eright[aidx] = (float)etright; aidx++; for ( int i = 0; i < 3; i++ ) { p1 = (float)p->vertice( i )->cell[axis]; if ((p1 >= pos1) && (p1 <= pos2)) insertSplitPos( p1 ); } l = l->getNext(); } // determine n1count / n2count for each split position aabb b1, b2, b3 = box, b4 = box; SplitList* splist = sList_; float b3p1 = b3.getPos().val[axis]; float b4p2 = b4.getPos().val[axis] + b4.getSize().val[axis]; Vector3 foo; while (splist) { foo = b4.getPos(); foo.val[axis] = splist->splitpos; b4.setPos(foo); foo = b4.getSize(); foo.val[axis] = pos2 - splist->splitpos; b4.setSize(foo); foo = b3.getSize(); foo.val[axis] = splist->splitpos - pos1; b3.setSize(foo); float b3p2 = b3.getPos().val[axis] + b3.getSize().val[axis]; float b4p1 = b4.getPos().val[axis]; for ( int i = 0; i < a_Prims; i++ ) if (pright[i]) { T* p = parray[i]; if ((eleft[i] <= b3p2) && (eright[i] >= b3p1)) if (p->intersectBox( b3 )) splist->n1count++; if ((eleft[i] <= b4p2) && (eright[i] >= b4p1)) if (p->intersectBox( b4 )) splist->n2count++; else pright[i] = false; } else splist->n1count++; splist = splist->next; } delete[] pright; // calculate surface area for current node real SAV = 0.5f / (box.w() * box.d() + box.w() * box.h() + box.d() * box.h()); // calculate cost for not splitting real Cleaf = a_Prims * 1.0f; // determine optimal split plane position splist = sList_; real lowcost = 10000; real bestpos = 0; while (splist) { // calculate child node extends foo = b4.getPos(); foo.val[axis] = splist->splitpos; b4.setPos(foo); foo = b4.getSize(); foo.val[axis] = pos2 - splist->splitpos; b4.setSize(foo); foo = b3.getSize(); foo.val[axis] = splist->splitpos - pos1; b3.setSize(foo); // calculate child node cost real SA1 = 2 * (b3.w() * b3.d() + b3.w() * b3.h() + b3.d() * b3.h()); real SA2 = 2 * (b4.w() * b4.d() + b4.w() * b4.h() + b4.d() * b4.h()); real splitcost = 0.3f + 1.0f * (SA1 * SAV * splist->n1count + SA2 * SAV * splist->n2count); // update best cost tracking variables if (splitcost < lowcost) { lowcost = splitcost; bestpos = splist->splitpos; b1 = b3, b2 = b4; } splist = splist->next; } if (lowcost > Cleaf) { delete[] eleft; delete[] eright; delete[] parray; return; } node->setSplitPos( bestpos ); // construct child nodes KdTreeNode<T>* left = s_MManager->NewKdTreeNodePair(); int n1count = 0, n2count = 0, total = 0; // assign primitives to both sides float b1p1 = b1.getPos().val[axis]; float b2p2 = b2.getPos().val[axis] + b2.getSize().val[axis]; float b1p2 = b1.getPos().val[axis] + b1.getSize().val[axis]; float b2p1 = b2.getPos().val[axis]; for ( int i = 0; i < a_Prims; i++ ) { T* p = parray[i]; total++; if ((eleft[i] <= b1p2) && (eright[i] >= b1p1)) if (p->intersectBox( b1 )) { left->add( p ); n1count++; } if ((eleft[i] <= b2p2) && (eright[i] >= b2p1)) if (p->intersectBox( b2 )) { (left + 1)->add( p ); n2count++; } } delete[] eleft; delete[] eright; delete[] parray; s_MManager->FreeObjectList( node->getList() ); node->setLeft( left ); node->setLeaf( false ); if (depth < MAXTREEDEPTH) { if (n1count > 2) subdivide( left, b1, depth + 1, n1count ); if (n2count > 2) subdivide( left + 1, b2, depth + 1, n2count ); } }
// ----------------------------------------------------------------- // Name : autoStartGame // ----------------------------------------------------------------- void DebugManager::autoStartGame() { // Build client data int nbClients = 1; ClientData * clients = new ClientData[nbClients]; int iClient = 0; clients[iClient].bLocal = true; // Re-init map data MapReader * pMapReader = new MapReader(m_pLocalClient); pMapReader->init("standard.lua"); ObjectList * pMapParameters = new ObjectList(true); pMapReader->getMapParameters(pMapParameters, LABEL_MAX_CHARS); int * pCustomParams = NULL; if (pMapParameters->size > 0) pCustomParams = new int[pMapParameters->size]; // Map custom parameters int i = 0; MapReader::MapParameters * pParam = (MapReader::MapParameters*) pMapParameters->getFirst(0); while (pParam != NULL) { pCustomParams[i++] = pParam->defaultValueIndex; pParam = (MapReader::MapParameters*) pMapParameters->getNext(0); } // Init map generator (we will not delete it here, as the pointer now belong to Server object) pMapReader->setMapParameters(pCustomParams, pMapParameters->size, 2); delete[] pCustomParams; MapReader::deleteMapParameters(pMapParameters); delete pMapParameters; // Init server Server * pServer = m_pLocalClient->initServer("", 1, clients, pMapReader, -1, -1); delete[] clients; if (pServer == NULL) { notifyErrorMessage("Error: server could not be initialized."); return; } // Build players data ObjectList * pServerPlayers = pServer->getSolver()->getPlayersList(); // Create neutral player char sName[NAME_MAX_CHARS]; i18n->getText("NEUTRA", sName, NAME_MAX_CHARS); Player * pPlayer = new Player(0, 0, pServer->getSolver()->getGlobalSpellsPtr()); wsafecpy(pPlayer->m_sProfileName, NAME_MAX_CHARS, sName); pPlayer->m_Color = rgb(0.5, 0.5, 0.5); wsafecpy(pPlayer->m_sBanner, 64, "blason1"); pServer->getSolver()->setNeutralPlayer(pPlayer); // Human players int playerId = 1; for (int fdfdf = 0; fdfdf < 2; fdfdf++) { // Create player object pPlayer = new Player(playerId, 0, pServer->getSolver()->getGlobalSpellsPtr()); snprintf(pPlayer->m_sProfileName, NAME_MAX_CHARS, "test%d", playerId); Profile * pProfile = m_pLocalClient->getDataFactory()->findProfile(pPlayer->m_sProfileName); AvatarData * pAvatar = (AvatarData*) pProfile->getAvatarsList()->getFirst(0); pPlayer->m_Color = rgb(1, 1, 1); pAvatar->getBanner(pPlayer->m_sBanner, 64); pServerPlayers->addLast(pPlayer); // Set Avatar CoordsMap pos = pMapReader->getPlayerPosition(playerId-1); pServer->getSolver()->setInitialAvatar(pAvatar->clone(m_pLocalClient), pPlayer, pos); // Add spells that are equipped Profile::SpellData * pSpellDesc = (Profile::SpellData*) pProfile->getSpellsList()->getFirst(0); while (pSpellDesc != NULL) { AvatarData * pOwner = pSpellDesc->m_pOwner; if (pOwner != NULL && strcmp(pAvatar->m_sEdition, pOwner->m_sEdition) == 0 && strcmp(pAvatar->m_sObjectId, pOwner->m_sObjectId) == 0) pServer->getSolver()->addInitialPlayerSpell(pPlayer, pSpellDesc->m_sEdition, pSpellDesc->m_sName); pSpellDesc = (Profile::SpellData*) pProfile->getSpellsList()->getNext(0); } // Add equipped artifacts Artifact * pArtifact = (Artifact*) pProfile->getArtifactsList()->getFirst(0); while (pArtifact != NULL) { AvatarData * pOwner = pArtifact->m_pOwner; if (pOwner != NULL && strcmp(pAvatar->m_sEdition, pOwner->m_sEdition) == 0 && strcmp(pAvatar->m_sObjectId, pOwner->m_sObjectId) == 0) { Unit * pAvatarInGame = pPlayer->getAvatar(); assert(pAvatarInGame != NULL); ArtifactEffect * pEffect = (ArtifactEffect*) pArtifact->getArtifactEffects()->getFirst(0); while (pEffect != NULL) { switch (pEffect->getType()) { case ARTIFACT_EFFECT_CHARAC: { bool bFound = true; long val = pAvatarInGame->getValue(((ArtifactEffect_Charac*)pEffect)->m_sKey, false, &bFound); if (bFound) pAvatarInGame->setBaseValue(((ArtifactEffect_Charac*)pEffect)->m_sKey, max(0, val + ((ArtifactEffect_Charac*)pEffect)->m_iModifier)); else { char sError[1024]; snprintf(sError, 1024, "Warning: artifact %s tries to modify characteristic that doesn't exist (%s)", pArtifact->m_sObjectId, ((ArtifactEffect_Charac*)pEffect)->m_sKey); m_pLocalClient->getDebug()->notifyErrorMessage(sError); } break; } case ARTIFACT_EFFECT_SPELL: { Spell * pSpell = m_pLocalClient->getDataFactory()->findSpell(((ArtifactEffect_Spell*)pEffect)->m_sSpellEdition, ((ArtifactEffect_Spell*)pEffect)->m_sSpellName); if (pSpell != NULL) pServer->getSolver()->addInitialPlayerSpell(pPlayer, ((ArtifactEffect_Spell*)pEffect)->m_sSpellEdition, ((ArtifactEffect_Spell*)pEffect)->m_sSpellName); else { char sError[1024]; snprintf(sError, 1024, "Warning: artifact %s tries to add spell that doesn't exist (%s)", pArtifact->m_sObjectId, ((ArtifactEffect_Spell*)pEffect)->m_sSpellName); m_pLocalClient->getDebug()->notifyErrorMessage(sError); } break; } case ARTIFACT_EFFECT_SKILL: { Skill * pSkill = new Skill(((ArtifactEffect_Skill*)pEffect)->m_sSkillEdition, ((ArtifactEffect_Skill*)pEffect)->m_sSkillName, ((ArtifactEffect_Skill*)pEffect)->m_sSkillParameters, pServer->getDebug()); if (pSkill != NULL && pSkill->isLoaded()) pAvatarInGame->addSkill(pSkill); else { char sError[1024]; snprintf(sError, 1024, "Warning: artifact %s tries to add skill that doesn't exist or that can't be loaded (%s)", pArtifact->m_sObjectId, ((ArtifactEffect_Skill*)pEffect)->m_sSkillName); m_pLocalClient->getDebug()->notifyErrorMessage(sError); } break; } } pEffect = (ArtifactEffect*) pArtifact->getArtifactEffects()->getNext(0); } } pArtifact = (Artifact*) pProfile->getArtifactsList()->getNext(0); } playerId++; } delete pMapReader; pServer->onInitFinished(); }