bool IDockScreenDisplay::EvalBool (const CString &sCode, bool *retbResult, CString *retsError) // EvalBool // // Evaluates the given string { CCodeChainCtx Ctx; Ctx.SetScreen(m_pDockScreen); Ctx.SaveAndDefineSourceVar(m_pLocation); Ctx.SaveAndDefineDataVar(m_pData); char *pPos = sCode.GetPointer(); ICCItem *pExp = Ctx.Link(sCode, 1, NULL); ICCItem *pResult = Ctx.Run(pExp); // LATER:Event Ctx.Discard(pExp); if (pResult->IsError()) { *retsError = pResult->GetStringValue(); Ctx.Discard(pResult); return false; } *retbResult = !pResult->IsNil(); Ctx.Discard(pResult); return true; }
CSpaceObject *IDockScreenDisplay::EvalListSource (const CString &sString, CString *retsError) // EvalListSource // // Returns the object from which we should display items { char *pPos = sString.GetPointer(); // See if we need to evaluate if (*pPos == '=') { CCodeChainCtx Ctx; Ctx.SetScreen(this); Ctx.SaveAndDefineSourceVar(m_pLocation); Ctx.SaveAndDefineDataVar(m_pData); ICCItem *pExp = Ctx.Link(sString, 1, NULL); ICCItem *pResult = Ctx.Run(pExp); // LATER:Event Ctx.Discard(pExp); if (pResult->IsError()) { *retsError = pResult->GetStringValue(); Ctx.Discard(pResult); return NULL; } // Convert to an object pointer CSpaceObject *pSource; if (strEquals(pResult->GetStringValue(), DATA_FROM_PLAYER)) pSource = m_pPlayer->GetShip(); else if (strEquals(pResult->GetStringValue(), DATA_FROM_STATION) || strEquals(pResult->GetStringValue(), DATA_FROM_SOURCE)) pSource = m_pLocation; else pSource = Ctx.AsSpaceObject(pResult); Ctx.Discard(pResult); return pSource; } // Otherwise, compare to constants else if (strEquals(sString, DATA_FROM_PLAYER)) return m_pPlayer->GetShip(); else return m_pLocation; }
int CShieldClass::FireGetMaxHP (CInstalledDevice *pDevice, CSpaceObject *pSource, int iMaxHP) const // FireGetMaxHP // // Fire GetMaxHP event { SEventHandlerDesc Event; if (FindEventHandlerShieldClass(evtGetMaxHP, &Event)) { ASSERT(pSource); ASSERT(pDevice); CCodeChainCtx Ctx; Ctx.SaveAndDefineSourceVar(pSource); Ctx.SaveAndDefineItemVar(pSource->GetItemForDevice(pDevice)); Ctx.DefineInteger(CONSTLIT("aMaxHP"), iMaxHP); ICCItem *pResult = Ctx.Run(Event); if (pResult->IsError()) pSource->ReportEventError(GET_MAX_HP_EVENT, pResult); else if (!pResult->IsNil()) iMaxHP = Max(0, pResult->GetIntegerValue()); Ctx.Discard(pResult); } return iMaxHP; }
COverlayType *CDeviceClass::FireGetOverlayType (CItemCtx &ItemCtx) const // FireGetOverlayType // // Fire GetOverlayType event { SEventHandlerDesc Event; if (FindEventHandlerDeviceClass(evtGetOverlayType, &Event)) { // Setup arguments CCodeChainCtx Ctx; Ctx.SaveAndDefineSourceVar(ItemCtx.GetSource()); Ctx.SaveAndDefineItemVar(ItemCtx); ICCItem *pResult = Ctx.Run(Event); DWORD dwUNID = 0; if (pResult->IsError()) ItemCtx.GetSource()->ReportEventError(GET_OVERLAY_TYPE_EVENT, pResult); else if (!pResult->IsNil()) dwUNID = pResult->GetIntegerValue(); Ctx.Discard(pResult); // Done return COverlayType::AsType(g_pUniverse->FindDesignType(dwUNID)); } else return GetOverlayType(); }
int CArmorClass::FireGetMaxHP (CItemCtx &ItemCtx, int iMaxHP) const // FireGetMaxHP // // Fire GetMaxHP event { SEventHandlerDesc Event; if (FindEventHandlerArmorClass(evtGetMaxHP, &Event)) { // Setup arguments CCodeChainCtx Ctx; Ctx.SaveAndDefineSourceVar(ItemCtx.GetSource()); Ctx.SaveAndDefineItemVar(ItemCtx); Ctx.DefineInteger(CONSTLIT("aMaxHP"), iMaxHP); ICCItem *pResult = Ctx.Run(Event); if (pResult->IsError()) ItemCtx.GetSource()->ReportEventError(GET_MAX_HP_EVENT, pResult); else if (!pResult->IsNil()) iMaxHP = Max(0, pResult->GetIntegerValue()); Ctx.Discard(pResult); } return iMaxHP; }
ICCItem *CMission::FireOnReward (ICCItem *pData) // FireOnReward // // Fire <OnReward> // // Callers are responsible for discarding the result, if not NULL. { SEventHandlerDesc Event; if (FindEventHandler(EVENT_ON_REWARD, &Event)) { CCodeChainCtx Ctx; Ctx.SaveAndDefineSourceVar(this); Ctx.SaveAndDefineDataVar(pData); ICCItem *pResult = Ctx.Run(Event); if (pResult->IsError()) { ReportEventError(EVENT_ON_REWARD, pResult); Ctx.Discard(pResult); return NULL; } return pResult; } return NULL; }
ICCItem *CMission::FireOnDeclined (void) // FireOnDeclined // // Fire <OnDeclined>. We return the result of the event, which might contain // instructions for the mission screen. // // Callers are responsible for discarding the result, if not NULL. { SEventHandlerDesc Event; if (FindEventHandler(EVENT_ON_DECLINED, &Event)) { CCodeChainCtx Ctx; Ctx.SaveAndDefineSourceVar(this); ICCItem *pResult = Ctx.Run(Event); if (pResult->IsError()) { ReportEventError(EVENT_ON_DECLINED, pResult); Ctx.Discard(pResult); return NULL; } return pResult; } return NULL; }
void CEffectCreator::InitPainterParameters (CCreatePainterCtx &Ctx, IEffectPainter *pPainter) // InitPainterParameters // // Initialize painter parameters { SEventHandlerDesc Event; if (FindEventHandlerEffectType(evtGetParameters, &Event)) { CCodeChainCtx CCCtx; CCCtx.SaveAndDefineDataVar(Ctx.GetData()); ICCItem *pResult = CCCtx.Run(Event); if (pResult->IsError()) ::kernelDebugLogMessage(CONSTLIT("EffectType %x GetParameters: %s"), GetUNID(), (LPSTR)pResult->GetStringValue()); else if (pResult->IsSymbolTable()) { int i; CCSymbolTable *pTable = (CCSymbolTable *)pResult; for (i = 0; i < pTable->GetCount(); i++) { CString sParam = pTable->GetKey(i); ICCItem *pValue = pTable->GetElement(i); CEffectParamDesc Value; if (pValue->IsNil()) Value.InitNull(); else if (pValue->IsInteger()) Value.InitInteger(pValue->GetIntegerValue()); else if (pValue->IsIdentifier()) { CString sValue = pValue->GetStringValue(); char *pPos = sValue.GetASCIIZPointer(); // If this is a color, parse it if (*pPos == '#') Value.InitColor(::LoadRGBColor(sValue)); // Otherwise, a string else Value.InitString(sValue); } pPainter->SetParam(Ctx, sParam, Value); } } else ::kernelDebugLogMessage(CONSTLIT("EffectType %x GetParameters: Expected struct result."), GetUNID()); CCCtx.Discard(pResult); } }
ALERROR CDesignCollection::CreateTemplateTypes (SDesignLoadCtx &Ctx) // CreateTemplateTypes // // This is called inside of BindDesign to create all template types { ALERROR error; int i; // Create an appropriate context for running code CCodeChainCtx CCCtx; // Loop over all active types looking for templates. // NOTE: We cannot use the type-specific arrays because they have not been // set up yet (remember that we are inside of BindDesign). for (i = 0; i < m_AllTypes.GetCount(); i++) { CDesignType *pTemplate = m_AllTypes.GetEntry(i); if (pTemplate->GetType() != designTemplateType) continue; // Get the function to generate the type source CString sSource; SEventHandlerDesc Event; if (pTemplate->FindEventHandler(GET_TYPE_SOURCE_EVENT, &Event)) { ICCItem *pResult = CCCtx.Run(Event); if (pResult->IsError()) { Ctx.sError = strPatternSubst(CONSTLIT("GetTypeSource (%x): %s"), pTemplate->GetUNID(), pResult->GetStringValue()); return ERR_FAIL; } else if (pResult->IsNil()) sSource = NULL_STR; else sSource = pResult->GetStringValue(); CCCtx.Discard(pResult); } // Define the type if (!sSource.IsBlank()) { if (error = AddDynamicType(pTemplate->GetExtension(), pTemplate->GetUNID(), sSource, true, &Ctx.sError)) return error; } } return NOERROR; }
bool CWeaponFireDesc::FireOnDamageOverlay (SDamageCtx &Ctx, CEnergyField *pOverlay) // FireOnDamageOverlay // // Fire OnDamageOverlay event. Returns TRUE if we should skip further overlay damage { SEventHandlerDesc Event; if (FindEventHandler(evtOnDamageOverlay, &Event)) { // Setup arguments CCodeChainCtx CCCtx; CCCtx.SaveAndDefineSourceVar(Ctx.pObj); CCCtx.DefineInteger(CONSTLIT("aArmorSeg"), Ctx.iSectHit); CCCtx.DefineInteger(CONSTLIT("aOverlayID"), pOverlay->GetID()); CCCtx.DefineSpaceObject(CONSTLIT("aCause"), Ctx.pCause); CCCtx.DefineSpaceObject(CONSTLIT("aAttacker"), Ctx.Attacker.GetObj()); CCCtx.DefineSpaceObject(CONSTLIT("aOrderGiver"), (Ctx.Attacker.GetObj() ? Ctx.Attacker.GetObj()->GetOrderGiver(Ctx.Attacker.GetCause()) : NULL)); CCCtx.DefineVector(CONSTLIT("aHitPos"), Ctx.vHitPos); CCCtx.DefineInteger(CONSTLIT("aHitDir"), Ctx.iDirection); CCCtx.DefineInteger(CONSTLIT("aDamageHP"), Ctx.iDamage); CCCtx.DefineString(CONSTLIT("aDamageType"), GetDamageShortName(Ctx.Damage.GetDamageType())); CCCtx.DefineItemType(CONSTLIT("aWeaponType"), Ctx.pDesc->GetWeaponType()); ICCItem *pResult = CCCtx.Run(Event); if (pResult->IsError()) Ctx.pObj->ReportEventError(ON_DAMAGE_OVERLAY_EVENT, pResult); // If we return Nil, then we continue processing bool bResult; if (pResult->IsNil()) bResult = false; // Otherwise, the result is the damage left else { Ctx.iDamage = pResult->GetIntegerValue(); bResult = true; } CCCtx.Discard(pResult); return bResult; } else return false; }
bool CWeaponFireDesc::FireOnFragment (const CDamageSource &Source, CSpaceObject *pShot, const CVector &vHitPos, CSpaceObject *pNearestObj, CSpaceObject *pTarget) // FireOnFragment // // Event fires when a shot fragments. If we return TRUE then we skip the default // fragmentation event. { SEventHandlerDesc Event; if (FindEventHandler(evtOnFragment, &Event)) { // Setup arguments CCodeChainCtx CCCtx; CCCtx.SaveAndDefineSourceVar(pShot); CCCtx.DefineSpaceObject(CONSTLIT("aNearestObj"), pNearestObj); CCCtx.DefineSpaceObject(CONSTLIT("aTargetObj"), pTarget); CCCtx.DefineVector(CONSTLIT("aHitPos"), vHitPos); CCCtx.DefineInteger(CONSTLIT("aHitDir"), (pShot ? pShot->GetRotation() : 0)); CCCtx.DefineItemType(CONSTLIT("aWeaponType"), GetWeaponType()); CCCtx.DefineString(CONSTLIT("aWeaponFragment"), m_sUNID); CSpaceObject *pAttacker = Source.GetObj(); CCCtx.DefineSpaceObject(CONSTLIT("aCause"), pShot); CCCtx.DefineSpaceObject(CONSTLIT("aAttacker"), pAttacker); CCCtx.DefineSpaceObject(CONSTLIT("aOrderGiver"), (pAttacker ? pAttacker->GetOrderGiver(Source.GetCause()) : NULL)); ICCItem *pResult = CCCtx.Run(Event); if (pResult->IsError()) pShot->ReportEventError(ON_FRAGMENT_EVENT, pResult); // If we return Nil, then we continue processing bool bResult; if (pResult->IsNil()) bResult = false; // Otherwise, we skip fragmentation else bResult = true; CCCtx.Discard(pResult); return bResult; } else return false; }
void CArmorClass::FireOnArmorDamage (CItemCtx &ItemCtx, SDamageCtx &Ctx) // FireOnArmorDamage // // Fires OnArmorDamage event { SEventHandlerDesc Event; if (FindEventHandlerArmorClass(evtOnArmorDamage, &Event)) { // Setup arguments CCodeChainCtx CCCtx; CCCtx.SaveAndDefineSourceVar(ItemCtx.GetSource()); CCCtx.SaveAndDefineItemVar(ItemCtx); CCCtx.DefineInteger(CONSTLIT("aArmorHP"), Ctx.iHPLeft); CCCtx.DefineInteger(CONSTLIT("aArmorSeg"), Ctx.iSectHit); CCCtx.DefineSpaceObject(CONSTLIT("aAttacker"), Ctx.Attacker.GetObj()); CCCtx.DefineSpaceObject(CONSTLIT("aCause"), Ctx.pCause); CCCtx.DefineDamageEffects(CONSTLIT("aDamageEffects"), Ctx); CCCtx.DefineInteger(CONSTLIT("aDamageHP"), Ctx.iDamage); CCCtx.DefineString(CONSTLIT("aDamageType"), GetDamageShortName(Ctx.Damage.GetDamageType())); CCCtx.DefineInteger(CONSTLIT("aHitDir"), Ctx.iDirection); CCCtx.DefineVector(CONSTLIT("aHitPos"), Ctx.vHitPos); CCCtx.DefineSpaceObject(CONSTLIT("aOrderGiver"), (Ctx.Attacker.GetObj() ? Ctx.Attacker.GetObj()->GetOrderGiver(Ctx.Attacker.GetCause()) : NULL)); CCCtx.DefineItemType(CONSTLIT("aWeaponType"), Ctx.pDesc->GetWeaponType()); ICCItem *pResult = CCCtx.Run(Event); // If we return Nil, then nothing if (pResult->IsNil()) NULL; // If we return an integer, then this is the damage that armor should take else if (pResult->IsInteger()) Ctx.iDamage = pResult->GetIntegerValue(); // If we return a list, then we it to be a DamageEffects list (modifications to // aDamageEffects) else if (pResult->IsList()) LoadDamageEffectsFromItem(pResult, Ctx); CCCtx.Discard(pResult); } }
void CMission::FireOnStart (void) // FireOnStart // // Fire <OnStarted> { SEventHandlerDesc Event; if (FindEventHandler(EVENT_ON_STARTED, &Event)) { CCodeChainCtx Ctx; Ctx.SaveAndDefineSourceVar(this); ICCItem *pResult = Ctx.Run(Event); if (pResult->IsError()) ReportEventError(EVENT_ON_STARTED, pResult); Ctx.Discard(pResult); } }
void CAdventureDesc::FireOnGameStart (void) // FireOnGameStart // // Fire OnGameStart event { SEventHandlerDesc Event; if (FindEventHandler(ON_GAME_START_EVENT, &Event)) { CCodeChainCtx Ctx; // Run code ICCItem *pResult = Ctx.Run(Event); if (pResult->IsError()) kernelDebugLogMessage("OnGameStart error: %s", pResult->GetStringValue()); Ctx.Discard(pResult); } }
void CShieldClass::FireOnShieldDown (CInstalledDevice *pDevice, CSpaceObject *pSource) // FireOnShieldDown // // Fire OnShieldDown event { SEventHandlerDesc Event; if (FindEventHandlerShieldClass(evtOnShieldDown, &Event)) { CCodeChainCtx Ctx; Ctx.SaveAndDefineSourceVar(pSource); Ctx.SaveAndDefineItemVar(pSource->GetItemForDevice(pDevice)); ICCItem *pResult = Ctx.Run(Event); if (pResult->IsError()) pSource->ReportEventError(ON_SHIELD_DOWN_EVENT, pResult); Ctx.Discard(pResult); } }
void CMission::FireCustomEvent (const CString &sEvent) // FireCustomEvent // // Fires a custom timed event { SEventHandlerDesc Event; if (FindEventHandler(sEvent, &Event)) { CCodeChainCtx Ctx; Ctx.SetEvent(eventDoEvent); Ctx.SaveAndDefineSourceVar(this); ICCItem *pResult = Ctx.Run(Event); if (pResult->IsError()) ReportEventError(sEvent, pResult); Ctx.Discard(pResult); } }
void CMission::FireOnSetPlayerTarget (const CString &sReason) // FireOnSetPlayerTarget // // Fire <OnSetPlayerTarget> { SEventHandlerDesc Event; if (FindEventHandler(EVENT_ON_SET_PLAYER_TARGET, &Event)) { CCodeChainCtx Ctx; Ctx.SaveAndDefineSourceVar(this); Ctx.DefineString(STR_A_REASON, sReason); ICCItem *pResult = Ctx.Run(Event); if (pResult->IsError()) ReportEventError(EVENT_ON_SET_PLAYER_TARGET, pResult); Ctx.Discard(pResult); } }
void CMission::FireOnReward (ICCItem *pData) // FireOnReward // // Fire <OnReward> { SEventHandlerDesc Event; if (FindEventHandler(EVENT_ON_REWARD, &Event)) { CCodeChainCtx Ctx; Ctx.SaveAndDefineSourceVar(this); Ctx.SaveAndDefineDataVar(pData); ICCItem *pResult = Ctx.Run(Event); if (pResult->IsError()) ReportEventError(EVENT_ON_REWARD, pResult); Ctx.Discard(pResult); } }
ALERROR CExtension::ExecuteGlobals (SDesignLoadCtx &Ctx) // ExecuteGlobals // // Execute the globals { DEBUG_TRY int i; CCodeChainCtx CCCtx; // Add a hook so that all lambda expressions defined in this global block // are wrapped with something that sets the extension UNID to the context. if (m_iType != extBase) CCCtx.SetGlobalDefineWrapper(this); // Run the code (which will likely define a bunch of functions) for (i = 0; i < m_Globals.GetCount(); i++) { ICCItem *pResult = CCCtx.Run(m_Globals[i].pCode); if (pResult->IsError()) { Ctx.sError = strPatternSubst(CONSTLIT("%s globals: %s"), m_Globals[i].sFilespec, pResult->GetStringValue()); return ERR_FAIL; } CCCtx.Discard(pResult); } // Done return NOERROR; DEBUG_CATCH }
void CAdventureDesc::FireOnGameEnd (const CGameRecord &Game, const SBasicGameStats &BasicStats) // FireOnGameEnd // // Fire OnGameEnd event { SEventHandlerDesc Event; if (FindEventHandler(ON_GAME_END_EVENT, &Event)) { CCodeChainCtx Ctx; // Initialize variables Ctx.DefineInteger(CONSTLIT("aScore"), Game.GetScore()); Ctx.DefineInteger(CONSTLIT("aResurrectCount"), Game.GetResurrectCount()); Ctx.DefineInteger(CONSTLIT("aSystemsVisited"), BasicStats.iSystemsVisited); Ctx.DefineInteger(CONSTLIT("aEnemiesDestroyed"), BasicStats.iEnemiesDestroyed); Ctx.DefineInteger(CONSTLIT("aBestEnemiesDestroyed"), BasicStats.iBestEnemyDestroyedCount); if (BasicStats.dwBestEnemyDestroyed) Ctx.DefineInteger(CONSTLIT("aBestEnemyClass"), BasicStats.dwBestEnemyDestroyed); else Ctx.DefineNil(CONSTLIT("aBestEnemyClass")); Ctx.DefineString(CONSTLIT("aEndGameReason"), Game.GetEndGameReason()); Ctx.DefineString(CONSTLIT("aEpitaph"), Game.GetEndGameEpitaph()); Ctx.DefineString(CONSTLIT("aEpitaphOriginal"), Game.GetEndGameEpitaph()); Ctx.DefineString(CONSTLIT("aTime"), Game.GetPlayTimeString()); // Invoke ICCItem *pResult = Ctx.Run(Event); if (pResult->IsError()) kernelDebugLogMessage("OnGameEnd error: %s", pResult->GetStringValue()); Ctx.Discard(pResult); } }
void CMission::FireOnStop (const CString &sReason, ICCItem *pData) // FireOnStop // // Fire <OnCompleted> { SEventHandlerDesc Event; if (FindEventHandler(EVENT_ON_COMPLETED, &Event)) { CCodeChainCtx Ctx; Ctx.SaveAndDefineSourceVar(this); Ctx.SaveAndDefineDataVar(pData); Ctx.DefineString(STR_A_REASON, sReason); ICCItem *pResult = Ctx.Run(Event); if (pResult->IsError()) ReportEventError(EVENT_ON_COMPLETED, pResult); Ctx.Discard(pResult); } }
ALERROR CLanguageDataBlock::InitFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc) // InitFromXML // // Initializes from an XML block { int i; for (i = 0; i < pDesc->GetContentElementCount(); i++) { CXMLElement *pItem = pDesc->GetContentElement(i); CString sID = pItem->GetAttribute(ID_ATTRIB); if (sID.IsBlank()) { Ctx.sError = strPatternSubst(CONSTLIT("Invalid id in <Language> block")); return ERR_FAIL; } if (strEquals(pItem->GetTag(), TEXT_TAG)) { // Link the code CCodeChainCtx CCCtx; ICCItem *pCode = CCCtx.Link(pItem->GetContentText(0), 0, NULL); if (pCode->IsError()) { Ctx.sError = strPatternSubst(CONSTLIT("Language id: %s : %s"), sID, pCode->GetStringValue()); return ERR_FAIL; } // Add an entry bool bIsNew; SEntry *pEntry = m_Data.SetAt(sID, &bIsNew); if (!bIsNew) { Ctx.sError = strPatternSubst(CONSTLIT("Duplicate <Language> element: %s"), sID); return ERR_FAIL; } // If pCode is a string and not an identifier, then we can just // store it directly. if (pCode->IsIdentifier() && pCode->IsQuoted()) { pEntry->pCode = NULL; pEntry->sText = pCode->GetStringValue(); } // Otherwise we store the code else pEntry->pCode = pCode->Reference(); // Done CCCtx.Discard(pCode); } else if (strEquals(pItem->GetTag(), MESSAGE_TAG)) { // Add an entry bool bIsNew; SEntry *pEntry = m_Data.SetAt(sID, &bIsNew); if (!bIsNew) { Ctx.sError = strPatternSubst(CONSTLIT("Duplicate <Language> element: %s"), sID); return ERR_FAIL; } // Set the text pEntry->pCode = NULL; pEntry->sText = pItem->GetAttribute(TEXT_ATTRIB); } else { Ctx.sError = strPatternSubst(CONSTLIT("Invalid element in <Language> block: <%s>"), pItem->GetTag()); return ERR_FAIL; } } return NOERROR; }
CLanguageDataBlock::ETranslateResult CLanguageDataBlock::Translate (CSpaceObject *pObj, const CString &sID, TArray<CString> *retText, CString *retsText) const // Translate // // Translates an entry to either a string or an array of strings. { int i; // If we can't find this ID then we can't translate SEntry *pEntry = m_Data.GetAt(sID); if (pEntry == NULL) return resultNotFound; // If we don't want the text back then all we need to do is return that we // have the text. if (retText == NULL && retsText == NULL) return resultFound; // If we don't need to run code then we just return the string. if (pEntry->pCode == NULL) { if (retsText) { *retsText = ::ComposePlayerNameString(pEntry->sText, g_pUniverse->GetPlayerName(), g_pUniverse->GetPlayerGenome()); return resultString; } else return resultFound; } // Otherwise we have to run some code CCodeChainCtx Ctx; Ctx.SaveAndDefineSourceVar(pObj); Ctx.DefineString(CONSTLIT("aTextID"), sID); ICCItem *pResult = Ctx.Run(pEntry->pCode); // LATER:Event ETranslateResult iResult; // Nil if (pResult->IsNil()) iResult = resultNotFound; // List of strings else if (pResult->GetCount() > 1) { if (retText) { CString sPlayerName = g_pUniverse->GetPlayerName(); GenomeTypes iPlayerGenome = g_pUniverse->GetPlayerGenome(); retText->DeleteAll(); retText->InsertEmpty(pResult->GetCount()); for (i = 0; i < pResult->GetCount(); i++) retText->GetAt(i) = ::ComposePlayerNameString(pResult->GetElement(i)->GetStringValue(), sPlayerName, iPlayerGenome); iResult = resultArray; } else iResult = resultFound; } // String else { if (retsText) { *retsText = ::ComposePlayerNameString(pResult->GetStringValue(), g_pUniverse->GetPlayerName(), g_pUniverse->GetPlayerGenome()); iResult = resultString; } else iResult = resultFound; } // Done Ctx.Discard(pResult); return iResult; }
void CCommandLineDisplay::OnKeyDown (int iVirtKey, DWORD dwKeyState) // OnKeyDown // // Handle WM_KEYDOWN { switch (iVirtKey) { case VK_BACK: InputBackspace(); break; case VK_DELETE: InputDelete(); break; case VK_RETURN: { CString sInput = GetInput(); if (!sInput.IsBlank()) { CCodeChain &CC = g_pUniverse->GetCC(); InputEnter(); CCodeChainCtx Ctx; ICCItem *pCode = Ctx.Link(sInput, 0, NULL); ICCItem *pResult = Ctx.Run(pCode); CString sOutput; if (pResult->IsIdentifier()) sOutput = pResult->Print(&CC, PRFLAG_NO_QUOTES | PRFLAG_ENCODE_FOR_DISPLAY); else sOutput = CC.Unlink(pResult); Ctx.Discard(pResult); Ctx.Discard(pCode); Output(sOutput); } break; } case VK_TAB: { AutoCompleteSearch(); break; } case VK_UP: InputHistoryUp(); break; case VK_DOWN: InputHistoryDown(); break; case VK_LEFT: if (m_iCursorPos > 0) m_iCursorPos--; m_bInvalid = true; break; case VK_RIGHT: if (m_iCursorPos < m_sInput.GetLength()) m_iCursorPos++; m_bInvalid = true; break; case VK_HOME: m_iCursorPos = 0; m_bInvalid = true; break; case VK_END: m_iCursorPos = m_sInput.GetLength(); m_bInvalid = true; break; case VK_PRIOR: if (m_iScrollPos < 2*MAX_LINES) m_iScrollPos++; m_bInvalid = true; break; case VK_NEXT: if (m_iScrollPos > 0) m_iScrollPos--; m_bInvalid = true; break; } }
void CRandomEnhancementGenerator::EnhanceItem (CItem &Item) // EnhanceItem // // Enhances the given item { // See if this item is enhanced if (mathRandom(1, 100) > m_iChance) return; // If we have code, execute the code to figure out the mod if (m_pCode) { CCodeChainCtx Ctx; // Save the previous value of gItem Ctx.SaveItemVar(); Ctx.DefineItem(Item); // Execute the code ICCItem *pResult = Ctx.Run(m_pCode); // LATER:Event // If we have an error, report it DWORD dwMods; if (pResult->IsError()) { CString sError = strPatternSubst(CONSTLIT("Generate Enhancement: %s"), pResult->GetStringValue()); kernelDebugLogMessage(sError); dwMods = 0; } // Otherwise, the result code is the mods else dwMods = (DWORD)pResult->GetIntegerValue(); // Done with code Ctx.Discard(pResult); // Enhance the item Item.AddEnhancement(dwMods); } // Otherwise, if we have a constant mod, apply that else if (m_dwMods) { Item.AddEnhancement(m_dwMods); } // Otherwise, we need to generate a random mod appropriate to the // particular item else { DWORD dwMods; CItemType *pType = Item.GetType(); int iRoll = mathRandom(1, 100); switch (pType->GetCategory()) { case itemcatArmor: case itemcatShields: if (iRoll < 40) // HP + 10% dwMods = 0x0101; else if (iRoll < 65) // HP + 20% dwMods = 0x0102; else if (iRoll < 80) // HP + 30% dwMods = 0x0103; else if (iRoll < 90) // Damage Adj at 50% (and 75%) dwMods = 0x0A05 + (mathRandom(0, 3) << 4); else if (iRoll < 95) // Immunity to special ion attacks dwMods = 0x0C00; else // Regeneration dwMods = 0x0200; break; case itemcatWeapon: if (iRoll < 50) // Damage + 10% dwMods = 0x0101; else if (iRoll < 80) // Damage + 20% dwMods = 0x0102; else if (iRoll < 95) // Damage + 30% dwMods = 0x0103; else // Delay time at 70% of original dwMods = 0x1003; break; default: dwMods = 0; } Item.AddEnhancement(dwMods); } }
ALERROR CDockScreenCustomList::OnInitList (SInitCtx &Ctx, CString *retsError) // OnInitList // // Initialize list { // Get the list element CXMLElement *pListData = Ctx.pDesc->GetContentElementByTag(LIST_TAG); if (pListData == NULL) return ERR_FAIL; // See if we define a custom row height CString sRowHeight; if (pListData->FindAttribute(ROW_HEIGHT_ATTRIB, &sRowHeight)) { CString sResult; if (!EvalString(sRowHeight, false, eventNone, &sResult)) { *retsError = sResult; return ERR_FAIL; } int cyRow = strToInt(sResult, -1); if (cyRow > 0) m_pItemListControl->SetRowHeight(cyRow); } // Get the list to show CCodeChain &CC = g_pUniverse->GetCC(); ICCItem *pExp = CC.Link(pListData->GetContentText(0), 0, NULL); // Evaluate the function CCodeChainCtx CCCtx; CCCtx.SetScreen(m_pDockScreen); CCCtx.SaveAndDefineSourceVar(m_pLocation); CCCtx.SaveAndDefineDataVar(m_pData); ICCItem *pResult = CCCtx.Run(pExp); // LATER:Event CCCtx.Discard(pExp); if (pResult->IsError()) { *retsError = pResult->GetStringValue(); return ERR_FAIL; } // Set this expression as the list m_pItemListControl->SetList(CC, pResult); CCCtx.Discard(pResult); // Position the cursor on the next relevant item SelectNextItem(); // Give the screen a chance to start at a different item (other // than the first) CString sInitialItemFunc = pListData->GetAttribute(INITIAL_ITEM_ATTRIB); if (!sInitialItemFunc.IsBlank()) { bool bMore = IsCurrentItemValid(); while (bMore) { bool bResult; if (!EvalBool(sInitialItemFunc, &bResult, retsError)) return ERR_FAIL; if (bResult) break; bMore = SelectNextItem(); } } return NOERROR; }
void CShieldClass::FireOnShieldDamage (CItemCtx &ItemCtx, SDamageCtx &Ctx) // FireOnShieldDamage // // Fire OnShieldDamage { SEventHandlerDesc Event; if (FindEventHandlerShieldClass(evtOnShieldDamage, &Event)) { // Setup arguments CCodeChainCtx CCCtx; CCCtx.SaveAndDefineSourceVar(ItemCtx.GetSource()); CCCtx.SaveAndDefineItemVar(ItemCtx); CCCtx.DefineInteger(CONSTLIT("aArmorSeg"), Ctx.iSectHit); CCCtx.DefineSpaceObject(CONSTLIT("aCause"), Ctx.pCause); CCCtx.DefineSpaceObject(CONSTLIT("aAttacker"), Ctx.Attacker.GetObj()); CCCtx.DefineSpaceObject(CONSTLIT("aOrderGiver"), (Ctx.Attacker.GetObj() ? Ctx.Attacker.GetObj()->GetOrderGiver(Ctx.Attacker.GetCause()) : NULL)); CCCtx.DefineVector(CONSTLIT("aHitPos"), Ctx.vHitPos); CCCtx.DefineInteger(CONSTLIT("aHitDir"), Ctx.iDirection); CCCtx.DefineInteger(CONSTLIT("aDamageHP"), Ctx.iDamage); CCCtx.DefineString(CONSTLIT("aDamageType"), GetDamageShortName(Ctx.Damage.GetDamageType())); CCCtx.DefineItemType(CONSTLIT("aWeaponType"), Ctx.pDesc->GetWeaponType()); CCCtx.DefineInteger(CONSTLIT("aShieldHP"), Ctx.iHPLeft); CCCtx.DefineInteger(CONSTLIT("aShieldDamageHP"), Ctx.iShieldDamage); CCCtx.DefineInteger(CONSTLIT("aArmorDamageHP"), Ctx.iDamage - Ctx.iAbsorb); if (Ctx.bReflect) { CCCtx.DefineString(CONSTLIT("aShieldReflect"), STR_SHIELD_REFLECT); CCCtx.DefineInteger(CONSTLIT("aOriginalShieldDamageHP"), Ctx.iOriginalShieldDamage); CCCtx.DefineInteger(CONSTLIT("aOriginalArmorDamageHP"), Ctx.iDamage - Ctx.iOriginalAbsorb); } else { CCCtx.DefineNil(CONSTLIT("aShieldReflect")); CCCtx.DefineInteger(CONSTLIT("aOriginalShieldDamageHP"), Ctx.iShieldDamage); CCCtx.DefineInteger(CONSTLIT("aOriginalArmorDamageHP"), Ctx.iDamage - Ctx.iAbsorb); } ICCItem *pResult = CCCtx.Run(Event); // If we return Nil, then nothing if (pResult->IsNil()) NULL; // If an error, report it else if (pResult->IsError()) ItemCtx.GetSource()->ReportEventError(ON_SHIELD_DAMAGE_EVENT, pResult); // If we return a list, then modify variables else if (pResult->IsList()) { // A single value means we modified the damage to armor if (pResult->GetCount() == 1) { if (strEquals(pResult->GetElement(0)->GetStringValue(), STR_SHIELD_REFLECT)) { Ctx.bReflect = true; Ctx.iAbsorb = Ctx.iDamage; Ctx.iShieldDamage = 0; } else { Ctx.iShieldDamage = Max(0, Min(pResult->GetElement(0)->GetIntegerValue(), Ctx.iHPLeft)); if (Ctx.bReflect) { Ctx.bReflect = false; Ctx.iAbsorb = Ctx.iOriginalAbsorb; } } } // Two values mean we modified both damage to armor and shield damage else if (pResult->GetCount() == 2) { Ctx.bReflect = false; Ctx.iShieldDamage = Max(0, Min(pResult->GetElement(0)->GetIntegerValue(), Ctx.iHPLeft)); Ctx.iAbsorb = Max(0, Ctx.iDamage - Max(0, pResult->GetElement(1)->GetIntegerValue())); } // Otherwise, we deal with reflection else { Ctx.bReflect = strEquals(pResult->GetElement(0)->GetStringValue(), STR_SHIELD_REFLECT); Ctx.iShieldDamage = Max(0, Min(pResult->GetElement(1)->GetIntegerValue(), Ctx.iHPLeft)); Ctx.iAbsorb = Max(0, Ctx.iDamage - Max(0, pResult->GetElement(2)->GetIntegerValue())); } } CCCtx.Discard(pResult); } }
bool CWeaponFireDesc::FireOnDamageShields (SDamageCtx &Ctx, int iDevice) // FireOnDamageShields // // Fire OnDamageShields event. Returns TRUE if we should skip further shields damage { SEventHandlerDesc Event; if (FindEventHandler(evtOnDamageShields, &Event)) { // Setup arguments CCodeChainCtx CCCtx; CItemListManipulator ItemList(Ctx.pObj->GetItemList()); CShip *pShip = Ctx.pObj->AsShip(); if (pShip) pShip->SetCursorAtDevice(ItemList, iDevice); CCCtx.SaveAndDefineSourceVar(Ctx.pObj); CCCtx.DefineInteger(CONSTLIT("aArmorSeg"), Ctx.iSectHit); CCCtx.DefineInteger(CONSTLIT("aDevice"), iDevice); CCCtx.DefineItem(CONSTLIT("aDeviceItem"), ItemList.GetItemAtCursor()); CCCtx.DefineSpaceObject(CONSTLIT("aCause"), Ctx.pCause); CCCtx.DefineSpaceObject(CONSTLIT("aAttacker"), Ctx.Attacker.GetObj()); CCCtx.DefineSpaceObject(CONSTLIT("aOrderGiver"), (Ctx.Attacker.GetObj() ? Ctx.Attacker.GetObj()->GetOrderGiver(Ctx.Attacker.GetCause()) : NULL)); CCCtx.DefineVector(CONSTLIT("aHitPos"), Ctx.vHitPos); CCCtx.DefineInteger(CONSTLIT("aHitDir"), Ctx.iDirection); CCCtx.DefineInteger(CONSTLIT("aDamageHP"), Ctx.iDamage); CCCtx.DefineString(CONSTLIT("aDamageType"), GetDamageShortName(Ctx.Damage.GetDamageType())); CCCtx.DefineItemType(CONSTLIT("aWeaponType"), Ctx.pDesc->GetWeaponType()); CCCtx.DefineInteger(CONSTLIT("aShieldHP"), Ctx.iHPLeft); CCCtx.DefineInteger(CONSTLIT("aShieldDamageHP"), Ctx.iShieldDamage); CCCtx.DefineInteger(CONSTLIT("aArmorDamageHP"), Ctx.iDamage - Ctx.iAbsorb); if (Ctx.bReflect) { CCCtx.DefineString(CONSTLIT("aShieldReflect"), STR_SHIELD_REFLECT); CCCtx.DefineInteger(CONSTLIT("aOriginalShieldDamageHP"), Ctx.iOriginalShieldDamage); CCCtx.DefineInteger(CONSTLIT("aOriginalArmorDamageHP"), Ctx.iDamage - Ctx.iOriginalAbsorb); } else { CCCtx.DefineNil(CONSTLIT("aShieldReflect")); CCCtx.DefineInteger(CONSTLIT("aOriginalShieldDamageHP"), Ctx.iShieldDamage); CCCtx.DefineInteger(CONSTLIT("aOriginalArmorDamageHP"), Ctx.iDamage - Ctx.iAbsorb); } ICCItem *pResult = CCCtx.Run(Event); // If we return Nil, then we continue processing bool bResult; if (pResult->IsNil()) bResult = false; // If this is an integer, we pass damage to armor else if (pResult->IsInteger()) { Ctx.iDamage = pResult->GetIntegerValue(); bResult = true; } // If we return a list, then modify variables else if (pResult->IsList()) { // A single value means we modified the damage to armor if (pResult->GetCount() == 1) { if (strEquals(pResult->GetElement(0)->GetStringValue(), STR_SHIELD_REFLECT)) { Ctx.bReflect = true; Ctx.iAbsorb = Ctx.iDamage; Ctx.iShieldDamage = 0; } else { Ctx.iShieldDamage = Max(0, Min(pResult->GetElement(0)->GetIntegerValue(), Ctx.iHPLeft)); if (Ctx.bReflect) { Ctx.bReflect = false; Ctx.iAbsorb = Ctx.iOriginalAbsorb; } } } // Two values mean we modified both damage to armor and shield damage else if (pResult->GetCount() == 2) { Ctx.bReflect = false; Ctx.iShieldDamage = Max(0, Min(pResult->GetElement(0)->GetIntegerValue(), Ctx.iHPLeft)); Ctx.iAbsorb = Max(0, Ctx.iDamage - Max(0, pResult->GetElement(1)->GetIntegerValue())); } // Otherwise, we deal with reflection else { Ctx.bReflect = strEquals(pResult->GetElement(0)->GetStringValue(), STR_SHIELD_REFLECT); Ctx.iShieldDamage = Max(0, Min(pResult->GetElement(1)->GetIntegerValue(), Ctx.iHPLeft)); Ctx.iAbsorb = Max(0, Ctx.iDamage - Max(0, pResult->GetElement(2)->GetIntegerValue())); } // Proceed with processing bResult = false; } // If this is the string "reflect" then we reflect else if (strEquals(pResult->GetStringValue(), STR_SHIELD_REFLECT)) { Ctx.bReflect = true; Ctx.iAbsorb = Ctx.iDamage; Ctx.iShieldDamage = 0; bResult = false; } // Otherwise, error else { Ctx.pObj->ReportEventError(ON_DAMAGE_OVERLAY_EVENT, pResult); bResult = true; } CCCtx.Discard(pResult); return bResult; } else return false; }
void RunFile (const CString &sFilespec, bool bNoLogo) { ALERROR error; CCodeChain &CC = g_pUniverse->GetCC(); CCodeChainCtx Ctx; // Verify the file CString sRunFile = sFilespec; if (!strEndsWith(sRunFile, CONSTLIT(".")) && pathGetExtension(sRunFile).IsBlank()) sRunFile.Append(CONSTLIT(".tlisp")); // Open the file CFileReadBlock InputFile(sRunFile); if (error = InputFile.Open()) { printf("error : Unable to open file '%s'.\n", sRunFile.GetASCIIZPointer()); return; } if (!bNoLogo) printf("%s\n", sRunFile.GetASCIIZPointer()); // Parse CString sInputFile(InputFile.GetPointer(0), InputFile.GetLength(), TRUE); CString sOutput; int iOffset = 0; while (true) { int iCharCount; ICCItem *pCode = Ctx.Link(sInputFile, iOffset, &iCharCount); if (pCode->IsNil()) break; else if (pCode->IsError()) { printf("error : %s\n", pCode->GetStringValue().GetASCIIZPointer()); Ctx.Discard(pCode); return; } iOffset += iCharCount; // Execute ICCItem *pResult = Ctx.Run(pCode); // Compose output if (pResult->IsIdentifier()) sOutput = pResult->Print(&CC, PRFLAG_NO_QUOTES | PRFLAG_ENCODE_FOR_DISPLAY); else sOutput = CC.Unlink(pResult); // Free Ctx.Discard(pResult); Ctx.Discard(pCode); } // Output result printf("%s\n", sOutput.GetASCIIZPointer()); }
void Run (CUniverse &Universe, CXMLElement *pCmdLine) { ALERROR error; int i; CCodeChain &CC = g_pUniverse->GetCC(); bool bNoLogo = pCmdLine->GetAttributeBool(NO_LOGO_SWITCH); // Prepare the universe CTopologyNode *pNode = g_pUniverse->GetFirstTopologyNode(); if (pNode == NULL) { printf("ERROR: No topology node found.\n"); return; } CSystem *pSystem; if (error = g_pUniverse->CreateStarSystem(pNode, &pSystem)) { printf("ERROR: Unable to create star system.\n"); return; } // Set the POV CSpaceObject *pPOV = pSystem->GetObject(0); g_pUniverse->SetPOV(pPOV); pSystem->SetPOVLRS(pPOV); // Prepare system g_pUniverse->UpdateExtended(); g_pUniverse->GarbageCollectLibraryBitmaps(); CString sCommand = pCmdLine->GetAttribute(RUN_SWITCH); CString sRunFile = pCmdLine->GetAttribute(RUN_FILE_SWITCH); // If this is a run file, then we parse it and run it if (!sRunFile.IsBlank() && !strEquals(sRunFile, CONSTLIT("true"))) { TArray<CString> Files; ParseStringList(sRunFile, PSL_FLAG_ALLOW_WHITESPACE, &Files); for (i = 0; i < Files.GetCount(); i++) RunFile(Files[i], bNoLogo); } // If we have a command, invoke it else if (!sCommand.IsBlank() && !strEquals(sCommand, CONSTLIT("True"))) { CCodeChainCtx Ctx; ICCItem *pCode = Ctx.Link(sCommand, 0, NULL); ICCItem *pResult = Ctx.Run(pCode); CString sOutput; if (pResult->IsIdentifier()) sOutput = pResult->Print(&CC, PRFLAG_NO_QUOTES | PRFLAG_ENCODE_FOR_DISPLAY); else sOutput = CC.Unlink(pResult); Ctx.Discard(pResult); Ctx.Discard(pCode); // Output result printf("%s\n", sOutput.GetASCIIZPointer()); } // Otherwise, we enter a command loop else { // Welcome if (!bNoLogo) { printf("(help) for function help.\n"); printf("\\q to quit.\n\n"); } // Loop while (true) { char szBuffer[1024]; if (!bNoLogo) printf(": "); gets_s(szBuffer, sizeof(szBuffer)-1); CString sCommand(szBuffer); // Escape codes if (*sCommand.GetASCIIZPointer() == '\\') { // Quit command if (strStartsWith(sCommand, CONSTLIT("\\q"))) break; else if (strStartsWith(sCommand, CONSTLIT("\\?")) || strStartsWith(sCommand, CONSTLIT("\\h"))) { printf("TLisp Shell\n\n"); printf("\\h Show this help\n"); printf("\\q Quit\n"); printf("\n(help) for function help.\n"); } } // Null command else if (sCommand.IsBlank()) NULL; // Command else { CCodeChainCtx Ctx; ICCItem *pCode = Ctx.Link(sCommand, 0, NULL); ICCItem *pResult = Ctx.Run(pCode); CString sOutput; if (pResult->IsIdentifier()) sOutput = pResult->Print(&CC, PRFLAG_NO_QUOTES | PRFLAG_ENCODE_FOR_DISPLAY); else sOutput = CC.Unlink(pResult); Ctx.Discard(pResult); Ctx.Discard(pCode); // Output result printf("%s\n", sOutput.GetASCIIZPointer()); } } } }