void CImageGrid::Create (int iCount, int cxCellWidth, int cyCellHeight) // Create // // Create the grid { ASSERT(iCount > 0); m_iCount = iCount; m_cxCellWidth = cxCellWidth; m_cyCellHeight = cyCellHeight; // Make a square out of all the images. m_iCellColumns = mathSqrt(iCount); if (m_iCellColumns * m_iCellColumns < iCount) m_iCellColumns++; m_iCellRows = AlignUp(iCount, m_iCellColumns) / m_iCellColumns; // Compute the size of the image int cxWidth = m_iCellColumns * cxCellWidth; int cyHeight = m_iCellRows * cyCellHeight; m_Image.Create(cxWidth, cyHeight); }
ICCItem *fnMath (CEvalContext *pCtx, ICCItem *pArgs, DWORD dwData) // fnMath // // Simple integer arithmetic // // (sqrt int1) { CCodeChain *pCC = pCtx->pCC; // Compute switch (dwData) { case FN_MATH_SQRT: { int iValue = pArgs->GetElement(0)->GetIntegerValue(); if (iValue >= 0) return pCC->CreateInteger(mathSqrt(iValue)); else return pCC->CreateError(CONSTLIT("Imaginary number"), pArgs->GetElement(0)); } default: ASSERT(false); return pCC->CreateNil(); } }
int IntVectorToPolar (int x, int y, int *retiRadius) // IntVectorToPolar // // Returns the angle from 0-359 and radius { int iAngle; int iRadius; int iSqrRadius = (x * x) + (y * y); // If we are at the origin then the angle is undefined if (iSqrRadius == 0) { iAngle = 0; iRadius = 0; } else { iRadius = mathSqrt(iSqrRadius); if (x >= 0.0) iAngle = (((int)(180 * asin((float)y / (float)iRadius) / g_Pi)) + 360) % 360; else iAngle = 180 - ((int)(180 * asin((float)y / (float)iRadius) / g_Pi)); } ASSERT(iAngle >= 0 && iAngle < 360); // Done if (retiRadius) *retiRadius = iRadius; return iAngle; }
ALERROR CNPUniverse::CreateNullPointNetwork (int iNullPoints) // CreateNullPointNetwork // // Creates a network of null points { ALERROR error; int iSide, iNullPointsCreated, x, y, iNextNP; CNPNullPoint **pSpace = NULL; CObjectArray NPStack(false); // We start by figuring out how // much space we need to fit all the null points iSide = g_iSpacing * mathSqrt(iNullPoints); // Now create an array and initialize it to 0 pSpace = new CNPNullPoint * [iSide * iSide]; ZeroMemory(pSpace, iSide * iSide * sizeof(DWORD)); // Prepare a stack of null points that need connections // We start with a single null point at the center of the space x = iSide / 2; y = iSide / 2; if (error = CNPNullPoint::Create(m_NullPoints.RegisterEntry(), x, y, &pSpace[x * iSide + y])) goto Fail; m_NullPoints.SetEntry(pSpace[x * iSide + y]->GetUNID(), pSpace[x * iSide + y]); if (error = NPStack.AppendObject(pSpace[x * iSide + y], NULL)) goto Fail; iNextNP = 0; // Pull objects from the stack and move to random hexes creating // new null points. Keep on looping until we're done iNullPointsCreated = 1; while (iNullPointsCreated < iNullPoints) { // Pull a null point from the stack CNPNullPoint *pNullPoint = (CNPNullPoint *)NPStack.GetObject(iNextNP++); // Figure out how many connections we want out of here. The probability // is: // 1/6 chance: 1 link // 1/3 chance: 2 links // 1/3 chance: 3 links // 1/6 chance: 4 links int iConnections = mathRandom(1, 2) + mathRandom(0, 2); // Make sure we never create more than 6 connections iConnections = min(iConnections, g_iHexDirectionsCount - pNullPoint->GetLinkCount()); // Create the connections while (iConnections > 0 && iNullPointsCreated < iNullPoints) { // Create a connection in a random direction that // we don't already have a connection to. do { HexMove(pNullPoint->GetX(), pNullPoint->GetY(), mathRandom(0, g_iHexDirectionsCount - 1), &x, &y); ASSERT(x >= 0 && y >= 0 && x < iSide && y < iSide); } while (pSpace[x * iSide + y] && pNullPoint->IsLinkedTo(pSpace[x * iSide + y])); // If the destination is empty then add a null point there if (pSpace[x * iSide + y] == NULL) { if (error = CNPNullPoint::Create(m_NullPoints.RegisterEntry(), x, y, &pSpace[x * iSide + y])) goto Fail; m_NullPoints.SetEntry(pSpace[x * iSide + y]->GetUNID(), pSpace[x * iSide + y]); // If we're not at the edge of space, add the null point // to the stack so that we can connect this one too. if (x > 0 && y > 0 && x < iSide-1 && y < iSide-1) { if (error = NPStack.AppendObject(pSpace[x * iSide + y], NULL)) goto Fail; } iNullPointsCreated++; } // Now connect to the destination if (error = pNullPoint->CreateLinkTo(pSpace[x * iSide + y])) goto Fail; iConnections--; } } delete [] pSpace; pSpace = NULL; return NOERROR; Fail: if (pSpace) delete [] pSpace; return error; }
void GenerateWeaponEffectChart (CUniverse &Universe, CXMLElement *pCmdLine) { int i; // Compute the list of weapons to show, making sure we filter to weapons // and missiles only. CItemTypeTable Selection; if (!Selection.Filter(pCmdLine->GetAttribute(CRITERIA_ATTRIB)) || (Selection.IsAll() && !Selection.Filter(CONSTLIT("wm")))) { printf("No entries match criteria.\n"); return; } Selection.Sort(); // Ship to use DWORD dwPlatformUNID; if (!pCmdLine->FindAttributeInteger(SHIP_UNID_ATTRIB, (int *)&dwPlatformUNID)) dwPlatformUNID = WEAPON_PLATFORM_UNID; // Compute some metrics int iFramesPerItem = 10; int cxFrameHorzMargin = 10; int cxMaxDistPerTick = (int)(STD_SECONDS_PER_UPDATE * (LIGHT_SECOND / g_KlicksPerPixel)); int cyFrame = 64; int cxFrame = (2 * cxFrameHorzMargin) + (iFramesPerItem * cxMaxDistPerTick); int iHitEffectFramesPerItem = 5; int cxHitEffect = 128; int cyHitEffect = 128; int cyRowTitle = 20; int cyRow = cyRowTitle + Max(ITEM_ICON_HEIGHT, cyFrame * iFramesPerItem); int cxRow = ITEM_ICON_WIDTH + cxFrame; int iColumns = Max(1, mathSqrt(Selection.GetCount())); int iRows = (Selection.GetCount() + (iColumns - 1)) / iColumns; int cxImage = cxRow * iColumns; int cyImage = cyRow * iRows; // Initialize the output COutputChart Output; Output.SetContentSize(cxImage, cyImage); Output.SetOutputFilespec(pCmdLine->GetAttribute(CONSTLIT("output"))); // Initialize fonts Output.SetStyleFont(STYLE_TITLE, pCmdLine->GetAttribute(CONSTLIT("font"))); Output.SetStyleColor(STYLE_TITLE, CG32bitPixel(0xFF, 0xFF, 0xFF)); // Prepare the universe CSystem *pSystem; if (Universe.CreateEmptyStarSystem(&pSystem) != NOERROR) { printf("ERROR: Unable to create empty star system.\n"); return; } // Create a target in the center of the system CSpaceObject *pStation; CStationType *pTargetType = Universe.FindStationType(TARGET_UNID); if (pTargetType == NULL || pSystem->CreateStation(pTargetType, NULL, CVector(), &pStation) != NOERROR) { printf("ERROR: Unable to create station.\n"); return; } // Create the weapon platform some distance away CSovereign *pPlatformSovereign = Universe.FindSovereign(PLAYER_SOVEREIGN_UNID); CShip *pPlatform; if (pPlatformSovereign == NULL || pSystem->CreateShip(dwPlatformUNID, NULL, NULL, pPlatformSovereign, CVector(-5.0 * LIGHT_SECOND, 0.), CVector(), 0, NULL, NULL, &pPlatform) != NOERROR) { printf("ERROR: Unable to create weapons platform.\n"); return; } // Set the attacker to hold IShipController *pController = pPlatform->GetController(); if (pController == NULL) { printf("ERROR: No controller for ship.\n"); return; } pController->AddOrder(IShipController::orderHold, NULL, IShipController::SData()); pPlatform->SetControllerEnabled(false); // Install the largest possible reactor on the ship CItemType *pReactorType = Universe.FindItemType(REACTOR_UNID); if (pReactorType) { CItem ReactorItem(pReactorType, 1); CItemListManipulator ItemList(pPlatform->GetItemList()); ItemList.AddItem(ReactorItem); pPlatform->OnComponentChanged(comCargo); pPlatform->ItemsModified(); pPlatform->InvalidateItemListAddRemove(); pPlatform->InstallItemAsDevice(ItemList); } // Set the POV Universe.SetPOV(pStation); pSystem->SetPOVLRS(pStation); // Prepare system Universe.UpdateExtended(); Universe.GarbageCollectLibraryBitmaps(); Universe.StartGame(true); // Output each weapon int xOrigin; int yOrigin; CG32bitImage &Image = Output.GetOutputImage(&xOrigin, &yOrigin); const CG16bitFont &TitleFont = Output.GetStyleFont(STYLE_TITLE); CG32bitPixel rgbTitleColor = Output.GetStyleColor(STYLE_TITLE); for (i = 0; i < Selection.GetCount(); i++) { CItemType *pType = Selection.GetItemType(i); // Compute the metrics of this row int xRow = xOrigin + (i % iColumns) * cxRow; int yRow = yOrigin + (i / iColumns) * cyRow; // Paint the weapon title Image.Fill(xRow, yRow, cxRow, cyRow, CG32bitPixel(0x40, 0x40, 0x40)); TitleFont.DrawText(Image, xRow + 8, yRow, rgbTitleColor, pType->GetNounPhrase()); // Paint the frames PaintWeaponFrames(Image, pType, pPlatform, iFramesPerItem, xRow + ITEM_ICON_WIDTH + cxFrameHorzMargin, yRow + cyRowTitle, cxMaxDistPerTick, cyFrame); } // Done Output.Output(); }