diff --git a/source/graphics/ShaderProgram.h b/source/graphics/ShaderProgram.h index fdb7247b02..68d8103c71 100644 --- a/source/graphics/ShaderProgram.h +++ b/source/graphics/ShaderProgram.h @@ -63,7 +63,7 @@ public: { friend class CShaderProgramARB; private: - Binding(int v, int f) : vertex(v), fragment(f) { } + Binding(int v, int f) : vertex((i16)v), fragment((i16)f) { } i16 vertex; i16 fragment; public: diff --git a/source/lib/os_path.h b/source/lib/os_path.h index a96a86159f..c7b3d377a8 100644 --- a/source/lib/os_path.h +++ b/source/lib/os_path.h @@ -46,7 +46,7 @@ static inline std::string OsString(const OsPath& path) for(size_t i = 0; i < wstring.length(); i++) { ENSURE(wstring[i] <= UCHAR_MAX); - string[i] = wstring[i]; + string[i] = (char)wstring[i]; } return string; } diff --git a/source/lib/timer.h b/source/lib/timer.h index 63aab7eae3..e87daca01a 100644 --- a/source/lib/timer.h +++ b/source/lib/timer.h @@ -199,13 +199,13 @@ retry: std::wstring ToString() const { - ENSURE(m_cycles >= 0.0); + ENSURE(m_cycles >= 0); return StringForCycles(m_cycles); } double ToSeconds() const { - return m_cycles / os_cpu_ClockFrequency(); + return (double)m_cycles / os_cpu_ClockFrequency(); } private: diff --git a/source/maths/Fixed.h b/source/maths/Fixed.h index 5d8b8297ca..bd16e37c1b 100644 --- a/source/maths/Fixed.h +++ b/source/maths/Fixed.h @@ -148,11 +148,13 @@ public: static CFixed FromString(const CStr8& s); static CFixed FromString(const CStrW& s); + /// Convert to float. May be lossy - float can't represent all values. float ToFloat() const { - return value / (float)fract_pow2; + return (float)value / (float)fract_pow2; } + /// Convert to double. Won't be lossy - double can precisely represent all values. double ToDouble() const { return value / (double)fract_pow2; diff --git a/source/ps/Overlay.h b/source/ps/Overlay.h index d2473d8c06..84f564752d 100644 --- a/source/ps/Overlay.h +++ b/source/ps/Overlay.h @@ -51,7 +51,7 @@ struct CColor // For passing to CRenderer: SColor4ub AsSColor4ub() const { - return SColor4ub((int)(r*255.0), (int)(g*255.0), (int)(b*255.0), (int)(a*255.0)); + return SColor4ub((u8)(r*255.0), (u8)(g*255.0), (u8)(b*255.0), (u8)(a*255.0)); } float r, g, b, a; diff --git a/source/ps/utf16string.h b/source/ps/utf16string.h index f5f06d55a9..90779043f8 100644 --- a/source/ps/utf16string.h +++ b/source/ps/utf16string.h @@ -61,7 +61,7 @@ struct utf16_traits { const char_type* end=s; while (*end) end++; - return end-s; + return (size_t)(end-s); } static const char_type* find(const char_type* s, size_t n, const char_type& a) diff --git a/source/simulation2/components/CCmpAIManager.cpp b/source/simulation2/components/CCmpAIManager.cpp index 57bfce518d..e840fd87eb 100644 --- a/source/simulation2/components/CCmpAIManager.cpp +++ b/source/simulation2/components/CCmpAIManager.cpp @@ -141,7 +141,7 @@ private: u8* img = buf.get() + hdr_size; for (size_t i = 0; i < data.size(); ++i) - img[i] = (data[i] * 255) / max; + img[i] = (u8)((data[i] * 255) / max); tex_write(&t, filename); tex_free(&t); @@ -359,7 +359,7 @@ public: void SerializeState(ISerializer& serializer) { - serializer.NumberU32_Unbounded("num ais", m_Players.size()); + serializer.NumberU32_Unbounded("num ais", (u32)m_Players.size()); for (size_t i = 0; i < m_Players.size(); ++i) { @@ -367,7 +367,7 @@ public: serializer.NumberI32_Unbounded("player", m_Players[i]->m_Player); serializer.ScriptVal("data", m_Players[i]->m_Obj); - serializer.NumberU32_Unbounded("num commands", m_Players[i]->m_Commands.size()); + serializer.NumberU32_Unbounded("num commands", (u32)m_Players[i]->m_Commands.size()); for (size_t j = 0; j < m_Players[i]->m_Commands.size(); ++j) { CScriptVal val = m_ScriptInterface.ReadStructuredClone(m_Players[i]->m_Commands[j]); @@ -527,7 +527,7 @@ public: { const CMessageProgressiveLoad& msgData = static_cast (msg); - *msgData.total += m_TemplateNames.size(); + *msgData.total += (int)m_TemplateNames.size(); if (*msgData.progressed) break; @@ -535,7 +535,7 @@ public: if (ContinueLoadEntityTemplates()) *msgData.progressed = true; - *msgData.progress += m_TemplateLoadedIdx; + *msgData.progress += (int)m_TemplateLoadedIdx; break; } diff --git a/source/simulation2/components/CCmpFootprint.cpp b/source/simulation2/components/CCmpFootprint.cpp index 8dbb34f52f..789172b288 100644 --- a/source/simulation2/components/CCmpFootprint.cpp +++ b/source/simulation2/components/CCmpFootprint.cpp @@ -163,20 +163,20 @@ public: entity_angle_t initialAngle = cmpPosition->GetRotation().Y; // Max spawning distance in tiles - const size_t maxSpawningDistance = 4; + const i32 maxSpawningDistance = 4; if (m_Shape == CIRCLE) { // Expand outwards from foundation - for (size_t dist = 0; dist <= maxSpawningDistance; ++dist) + for (i32 dist = 0; dist <= maxSpawningDistance; ++dist) { // The spawn point should be far enough from this footprint to fit the unit, plus a little gap - entity_pos_t clearance = spawnedRadius + entity_pos_t::FromInt(2 + CELL_SIZE*dist); + entity_pos_t clearance = spawnedRadius + entity_pos_t::FromInt(2 + (int)CELL_SIZE*dist); entity_pos_t radius = m_Size0 + clearance; // Try equally-spaced points around the circle in alternating directions, starting from the front - const ssize_t numPoints = 31 + 2*dist; - for (ssize_t i = 0; i < (numPoints+1)/2; i = (i > 0 ? -i : 1-i)) // [0, +1, -1, +2, -2, ... (np-1)/2, -(np-1)/2] + const i32 numPoints = 31 + 2*dist; + for (i32 i = 0; i < (numPoints+1)/2; i = (i > 0 ? -i : 1-i)) // [0, +1, -1, +2, -2, ... (np-1)/2, -(np-1)/2] { entity_angle_t angle = initialAngle + (entity_angle_t::Pi()*2).Multiply(entity_angle_t::FromInt(i)/(int)numPoints); @@ -197,15 +197,15 @@ public: sincos_approx(initialAngle, s, c); // Expand outwards from foundation - for (size_t dist = 0; dist <= maxSpawningDistance; ++dist) + for (i32 dist = 0; dist <= maxSpawningDistance; ++dist) { // The spawn point should be far enough from this footprint to fit the unit, plus a little gap - entity_pos_t clearance = spawnedRadius + entity_pos_t::FromInt(2 + CELL_SIZE*dist); + entity_pos_t clearance = spawnedRadius + entity_pos_t::FromInt(2 + (int)CELL_SIZE*dist); - for (size_t edge = 0; edge < 4; ++edge) + for (i32 edge = 0; edge < 4; ++edge) { // Try equally-spaced points along the edge in alternating directions, starting from the middle - const ssize_t numPoints = 9 + 2*dist; + const i32 numPoints = 9 + 2*dist; // Compute the direction and length of the current edge CFixedVector2D dir; @@ -236,7 +236,7 @@ public: CFixedVector2D center = initialPos - dir.Perpendicular().Multiply(sy/2 + clearance); dir = dir.Multiply((sx + clearance*2) / (int)(numPoints-1)); - for (ssize_t i = 0; i < (numPoints+1)/2; i = (i > 0 ? -i : 1-i)) // [0, +1, -1, +2, -2, ... (np-1)/2, -(np-1)/2] + for (i32 i = 0; i < (numPoints+1)/2; i = (i > 0 ? -i : 1-i)) // [0, +1, -1, +2, -2, ... (np-1)/2, -(np-1)/2] { CFixedVector2D pos (center + dir*i); diff --git a/source/simulation2/components/CCmpMinimap.cpp b/source/simulation2/components/CCmpMinimap.cpp index 8bae030fee..8040c00668 100644 --- a/source/simulation2/components/CCmpMinimap.cpp +++ b/source/simulation2/components/CCmpMinimap.cpp @@ -87,9 +87,9 @@ public: if (colour.IsOk()) { m_UsePlayerColour = false; - m_R = colour.GetChild("@r").ToInt(); - m_G = colour.GetChild("@g").ToInt(); - m_B = colour.GetChild("@b").ToInt(); + m_R = (u8)colour.GetChild("@r").ToInt(); + m_G = (u8)colour.GetChild("@g").ToInt(); + m_B = (u8)colour.GetChild("@b").ToInt(); } else { @@ -176,9 +176,10 @@ public: if (cmpPlayer.null()) break; CColor colour = cmpPlayer->GetColour(); - m_R = (int)(colour.r*255.0); - m_G = (int)(colour.g*255.0); - m_B = (int)(colour.b*255.0); + m_R = (u8)(colour.r*255.0); + m_G = (u8)(colour.g*255.0); + m_B = (u8)(colour.b*255.0); + // TODO: probably should avoid using floating-point here break; } diff --git a/source/simulation2/components/CCmpMotionBall.cpp b/source/simulation2/components/CCmpMotionBall.cpp index 5b069665b6..6487665eff 100644 --- a/source/simulation2/components/CCmpMotionBall.cpp +++ b/source/simulation2/components/CCmpMotionBall.cpp @@ -94,8 +94,7 @@ void CCmpMotionBall::Move(fixed dt) float x = pos.X.ToFloat(); float z = pos.Z.ToFloat(); - CVector3D normal; - GetSimContext().GetTerrain().CalcNormal(x / CELL_SIZE, z / CELL_SIZE, normal); + CVector3D normal = GetSimContext().GetTerrain().CalcExactNormal(x, z); // Flatten the vector, to get the downhill force float g = 10.f; CVector3D force(g * normal.X, 0.f, g * normal.Z); @@ -107,8 +106,8 @@ void CCmpMotionBall::Move(fixed dt) float dt_ = dt.ToFloat(); - m_SpeedX *= pow(drag, dt_); - m_SpeedZ *= pow(drag, dt_); + m_SpeedX *= powf(drag, dt_); + m_SpeedZ *= powf(drag, dt_); cmpPosition->MoveTo(entity_pos_t::FromFloat(x + m_SpeedX * dt_), entity_pos_t::FromFloat(z + m_SpeedZ * dt_)); } diff --git a/source/simulation2/components/CCmpObstruction.cpp b/source/simulation2/components/CCmpObstruction.cpp index 156a26b6d3..81eda03dff 100644 --- a/source/simulation2/components/CCmpObstruction.cpp +++ b/source/simulation2/components/CCmpObstruction.cpp @@ -40,6 +40,9 @@ public: DEFAULT_COMPONENT_ALLOCATOR(Obstruction) + typedef ICmpObstructionManager::tag_t tag_t; + typedef ICmpObstructionManager::flags_t flags_t; + // Template state: enum { @@ -48,15 +51,15 @@ public: } m_Type; entity_pos_t m_Size0; // radius or width entity_pos_t m_Size1; // radius or depth - u8 m_TemplateFlags; + flags_t m_TemplateFlags; // Dynamic state: bool m_Active; // whether the obstruction is obstructing or just an inactive placeholder bool m_Moving; entity_id_t m_ControlGroup; - ICmpObstructionManager::tag_t m_Tag; - u8 m_Flags; + tag_t m_Tag; + flags_t m_Flags; static std::string GetSchema() { @@ -127,13 +130,13 @@ public: m_Flags = m_TemplateFlags; if (paramNode.GetChild("DisableBlockMovement").ToBool()) - m_Flags &= ~ICmpObstructionManager::FLAG_BLOCK_MOVEMENT; + m_Flags &= (flags_t)(~ICmpObstructionManager::FLAG_BLOCK_MOVEMENT); if (paramNode.GetChild("DisableBlockPathfinding").ToBool()) - m_Flags &= ~ICmpObstructionManager::FLAG_BLOCK_PATHFINDING; + m_Flags &= (flags_t)(~ICmpObstructionManager::FLAG_BLOCK_PATHFINDING); m_Active = paramNode.GetChild("Active").ToBool(); - m_Tag = ICmpObstructionManager::tag_t(); + m_Tag = tag_t(); m_Moving = false; m_ControlGroup = GetEntityId(); } @@ -194,12 +197,12 @@ public: data.x, data.z, data.a, m_Size0, m_Size1, m_Flags); else m_Tag = cmpObstructionManager->AddUnitShape(GetEntityId(), - data.x, data.z, m_Size0, m_Flags | (m_Moving ? ICmpObstructionManager::FLAG_MOVING : 0), m_ControlGroup); + data.x, data.z, m_Size0, (flags_t)(m_Flags | (m_Moving ? ICmpObstructionManager::FLAG_MOVING : 0)), m_ControlGroup); } else if (!data.inWorld && m_Tag.valid()) { cmpObstructionManager->RemoveShape(m_Tag); - m_Tag = ICmpObstructionManager::tag_t(); + m_Tag = tag_t(); } break; } @@ -212,7 +215,7 @@ public: break; // error cmpObstructionManager->RemoveShape(m_Tag); - m_Tag = ICmpObstructionManager::tag_t(); + m_Tag = tag_t(); } break; } @@ -244,7 +247,7 @@ public: pos.X, pos.Y, cmpPosition->GetRotation().Y, m_Size0, m_Size1, m_Flags); else m_Tag = cmpObstructionManager->AddUnitShape(GetEntityId(), - pos.X, pos.Y, m_Size0, m_Flags | (m_Moving ? ICmpObstructionManager::FLAG_MOVING : 0), m_ControlGroup); + pos.X, pos.Y, m_Size0, (flags_t)(m_Flags | (m_Moving ? ICmpObstructionManager::FLAG_MOVING : 0)), m_ControlGroup); } else if (!active && m_Active) { @@ -259,7 +262,7 @@ public: return; // error cmpObstructionManager->RemoveShape(m_Tag); - m_Tag = ICmpObstructionManager::tag_t(); + m_Tag = tag_t(); } } // else we didn't change the active status @@ -270,14 +273,14 @@ public: if (disabled) { // Remove the blocking flags - m_Flags &= ~ICmpObstructionManager::FLAG_BLOCK_MOVEMENT; - m_Flags &= ~ICmpObstructionManager::FLAG_BLOCK_PATHFINDING; + m_Flags &= (flags_t)(~ICmpObstructionManager::FLAG_BLOCK_MOVEMENT); + m_Flags &= (flags_t)(~ICmpObstructionManager::FLAG_BLOCK_PATHFINDING); } else { // Add the blocking flags if the template had enabled them - m_Flags |= (m_TemplateFlags & ICmpObstructionManager::FLAG_BLOCK_MOVEMENT); - m_Flags |= (m_TemplateFlags & ICmpObstructionManager::FLAG_BLOCK_PATHFINDING); + m_Flags = (flags_t)(m_Flags | (m_TemplateFlags & ICmpObstructionManager::FLAG_BLOCK_MOVEMENT)); + m_Flags = (flags_t)(m_Flags | (m_TemplateFlags & ICmpObstructionManager::FLAG_BLOCK_PATHFINDING)); } // Reset the shape with the new flags (kind of inefficiently - we diff --git a/source/simulation2/components/CCmpObstructionManager.cpp b/source/simulation2/components/CCmpObstructionManager.cpp index 3bb387b04e..2c2cd5bad9 100644 --- a/source/simulation2/components/CCmpObstructionManager.cpp +++ b/source/simulation2/components/CCmpObstructionManager.cpp @@ -51,7 +51,7 @@ struct UnitShape entity_id_t entity; entity_pos_t x, z; entity_pos_t r; // radius of circle, or half width of square - u8 flags; + ICmpObstructionManager::flags_t flags; entity_id_t group; // control group (typically the owner entity, or a formation controller entity) (units ignore collisions with others in the same group) }; @@ -64,7 +64,7 @@ struct StaticShape entity_pos_t x, z; // world-space coordinates CFixedVector2D u, v; // orthogonal unit vectors - axes of local coordinate space entity_pos_t hw, hh; // half width/height in local coordinate space - u8 flags; + ICmpObstructionManager::flags_t flags; }; /** @@ -245,10 +245,10 @@ public: } } - virtual tag_t AddUnitShape(entity_id_t ent, entity_pos_t x, entity_pos_t z, entity_pos_t r, u8 flags, entity_id_t group) + virtual tag_t AddUnitShape(entity_id_t ent, entity_pos_t x, entity_pos_t z, entity_pos_t r, flags_t flags, entity_id_t group) { UnitShape shape = { ent, x, z, r, flags, group }; - size_t id = m_UnitShapeNext++; + u32 id = m_UnitShapeNext++; m_UnitShapes[id] = shape; MakeDirtyUnit(flags); @@ -257,7 +257,7 @@ public: return UNIT_INDEX_TO_TAG(id); } - virtual tag_t AddStaticShape(entity_id_t ent, entity_pos_t x, entity_pos_t z, entity_angle_t a, entity_pos_t w, entity_pos_t h, u8 flags) + virtual tag_t AddStaticShape(entity_id_t ent, entity_pos_t x, entity_pos_t z, entity_angle_t a, entity_pos_t w, entity_pos_t h, flags_t flags) { fixed s, c; sincos_approx(a, s, c); @@ -265,7 +265,7 @@ public: CFixedVector2D v(s, c); StaticShape shape = { ent, x, z, u, v, w/2, h/2, flags }; - size_t id = m_StaticShapeNext++; + u32 id = m_StaticShapeNext++; m_StaticShapes[id] = shape; MakeDirtyStatic(flags); @@ -350,7 +350,7 @@ public: if (moving) shape.flags |= FLAG_MOVING; else - shape.flags &= ~FLAG_MOVING; + shape.flags &= (flags_t)~FLAG_MOVING; MakeDirtyDebug(); } @@ -468,7 +468,7 @@ private: * Mark all previous Rasterise()d grids as dirty, if they depend on this shape. * Call this when a static shape has changed. */ - void MakeDirtyStatic(u8 flags) + void MakeDirtyStatic(flags_t flags) { if (flags & (FLAG_BLOCK_PATHFINDING|FLAG_BLOCK_FOUNDATION)) ++m_DirtyID; @@ -480,7 +480,7 @@ private: * Mark all previous Rasterise()d grids as dirty, if they depend on this shape. * Call this when a unit shape has changed. */ - void MakeDirtyUnit(u8 flags) + void MakeDirtyUnit(flags_t flags) { if (flags & (FLAG_BLOCK_PATHFINDING|FLAG_BLOCK_FOUNDATION)) ++m_DirtyID; @@ -690,8 +690,8 @@ bool CCmpObstructionManager::TestUnitShape(const IObstructionTestFilter& filter, */ static void NearestTile(entity_pos_t x, entity_pos_t z, u16& i, u16& j, u16 w, u16 h) { - i = clamp((x / (int)CELL_SIZE).ToInt_RoundToZero(), 0, w-1); - j = clamp((z / (int)CELL_SIZE).ToInt_RoundToZero(), 0, h-1); + i = (u16)clamp((x / (int)CELL_SIZE).ToInt_RoundToZero(), 0, w-1); + j = (u16)clamp((z / (int)CELL_SIZE).ToInt_RoundToZero(), 0, h-1); } /** @@ -699,8 +699,8 @@ static void NearestTile(entity_pos_t x, entity_pos_t z, u16& i, u16& j, u16 w, u */ static void TileCenter(u16 i, u16 j, entity_pos_t& x, entity_pos_t& z) { - x = entity_pos_t::FromInt(i*(int)CELL_SIZE + CELL_SIZE/2); - z = entity_pos_t::FromInt(j*(int)CELL_SIZE + CELL_SIZE/2); + x = entity_pos_t::FromInt(i*(int)CELL_SIZE + (int)CELL_SIZE/2); + z = entity_pos_t::FromInt(j*(int)CELL_SIZE + (int)CELL_SIZE/2); } bool CCmpObstructionManager::Rasterise(Grid& grid) @@ -810,7 +810,7 @@ bool CCmpObstructionManager::Rasterise(Grid& grid) // Any tiles outside or very near the edge of the map are impassable // WARNING: CCmpRangeManager::LosIsOffWorld needs to be kept in sync with this - const ssize_t edgeSize = 3; // number of tiles around the edge that will be off-world + const u16 edgeSize = 3; // number of tiles around the edge that will be off-world u8 edgeFlags = TILE_OBSTRUCTED_PATHFINDING | TILE_OBSTRUCTED_FOUNDATION | TILE_OUTOFBOUNDS; @@ -844,13 +844,13 @@ bool CCmpObstructionManager::Rasterise(Grid& grid) for (u16 i = 0; i < i0+edgeSize; ++i) grid.set(i, j, edgeFlags); for (u16 j = 0; j < grid.m_H; ++j) - for (u16 i = i1-edgeSize+1; i < grid.m_W; ++i) + for (u16 i = (u16)(i1-edgeSize+1); i < grid.m_W; ++i) grid.set(i, j, edgeFlags); for (u16 j = 0; j < j0+edgeSize; ++j) - for (u16 i = i0+edgeSize; i < i1-edgeSize+1; ++i) + for (u16 i = (u16)(i0+edgeSize); i < i1-edgeSize+1; ++i) grid.set(i, j, edgeFlags); - for (u16 j = j1-edgeSize+1; j < grid.m_H; ++j) - for (u16 i = i0+edgeSize; i < i1-edgeSize+1; ++i) + for (u16 j = (u16)(j1-edgeSize+1); j < grid.m_H; ++j) + for (u16 i = (u16)(i0+edgeSize); i < i1-edgeSize+1; ++i) grid.set(i, j, edgeFlags); } @@ -966,7 +966,7 @@ void CCmpObstructionManager::RenderSubmit(SceneCollector& collector) { m_DebugOverlayLines.push_back(SOverlayLine()); m_DebugOverlayLines.back().m_Color = defaultColour; - float a = atan2(it->second.v.X.ToFloat(), it->second.v.Y.ToFloat()); + float a = atan2f(it->second.v.X.ToFloat(), it->second.v.Y.ToFloat()); SimRender::ConstructSquareOnGround(GetSimContext(), it->second.x.ToFloat(), it->second.z.ToFloat(), it->second.hw.ToFloat()*2, it->second.hh.ToFloat()*2, a, m_DebugOverlayLines.back(), true); } diff --git a/source/simulation2/components/CCmpPathfinder.cpp b/source/simulation2/components/CCmpPathfinder.cpp index 319f22e15f..7f30a62e73 100644 --- a/source/simulation2/components/CCmpPathfinder.cpp +++ b/source/simulation2/components/CCmpPathfinder.cpp @@ -31,6 +31,7 @@ #include "simulation2/MessageTypes.h" #include "simulation2/components/ICmpObstruction.h" #include "simulation2/components/ICmpObstructionManager.h" +#include "simulation2/components/ICmpTerrain.h" #include "simulation2/components/ICmpWaterManager.h" #include "simulation2/serialization/SerializeTemplates.h" @@ -77,7 +78,7 @@ void CCmpPathfinder::Init(const CParamNode& UNUSED(paramNode)) // happen in another thread. Either way this probably // will require some adjustment and rethinking. const CParamNode pathingSettings = externalParamNode.GetChild("Pathfinder"); - m_MaxSameTurnMoves = pathingSettings.GetChild("MaxSameTurnMoves").ToInt(); + m_MaxSameTurnMoves = (u16)pathingSettings.GetChild("MaxSameTurnMoves").ToInt(); const CParamNode::ChildrenMap& passClasses = externalParamNode.GetChild("Pathfinder").GetChild("PassabilityClasses").GetChildren(); @@ -305,8 +306,12 @@ const Grid& CCmpPathfinder::GetPassabilityGrid() void CCmpPathfinder::UpdateGrid() { + CmpPtr cmpTerrain(GetSimContext(), SYSTEM_ENTITY); + if (cmpTerrain.null()) + return; // error + // If the terrain was resized then delete the old grid data - if (m_Grid && m_MapSize != GetSimContext().GetTerrain().GetTilesPerSide()) + if (m_Grid && m_MapSize != cmpTerrain->GetTilesPerSide()) { SAFE_DELETE(m_Grid); SAFE_DELETE(m_ObstructionGrid); @@ -316,11 +321,7 @@ void CCmpPathfinder::UpdateGrid() // Initialise the terrain data when first needed if (!m_Grid) { - // TOOD: these bits should come from ICmpTerrain - ssize_t size = GetSimContext().GetTerrain().GetTilesPerSide(); - - ENSURE(size >= 1 && size <= 0xffff); // must fit in 16 bits - m_MapSize = size; + m_MapSize = cmpTerrain->GetTilesPerSide(); m_Grid = new Grid(m_MapSize, m_MapSize); m_ObstructionGrid = new Grid(m_MapSize, m_MapSize); } @@ -353,12 +354,12 @@ void CCmpPathfinder::UpdateGrid() if (obstruct & ICmpObstructionManager::TILE_OBSTRUCTED_PATHFINDING) t |= 1; else - t &= ~1; + t &= (TerrainTile)~1; if (obstruct & ICmpObstructionManager::TILE_OBSTRUCTED_FOUNDATION) t |= 2; else - t &= ~2; + t &= (TerrainTile)~2; } } @@ -417,10 +418,10 @@ void CCmpPathfinder::UpdateGrid() } // Expand influences on land to find shore distance - for (size_t y = 0; y < m_MapSize; ++y) + for (u16 y = 0; y < m_MapSize; ++y) { u16 min = shoreMax; - for (size_t x = 0; x < m_MapSize; ++x) + for (u16 x = 0; x < m_MapSize; ++x) { if (!waterGrid.get(x, y)) { @@ -433,7 +434,7 @@ void CCmpPathfinder::UpdateGrid() ++min; } } - for (size_t x = m_MapSize; x > 0; --x) + for (u16 x = m_MapSize; x > 0; --x) { if (!waterGrid.get(x-1, y)) { @@ -447,10 +448,10 @@ void CCmpPathfinder::UpdateGrid() } } } - for (size_t x = 0; x < m_MapSize; ++x) + for (u16 x = 0; x < m_MapSize; ++x) { u16 min = shoreMax; - for (size_t y = 0; y < m_MapSize; ++y) + for (u16 y = 0; y < m_MapSize; ++y) { if (!waterGrid.get(x, y)) { @@ -463,7 +464,7 @@ void CCmpPathfinder::UpdateGrid() ++min; } } - for (size_t y = m_MapSize; y > 0; --y) + for (u16 y = m_MapSize; y > 0; --y) { if (!waterGrid.get(x, y-1)) { @@ -600,22 +601,20 @@ void CCmpPathfinder::ProcessShortRequests(const std::vector longRequests; - if (m_AsyncLongPathRequests.size() <= moveCount) + if ((i32)m_AsyncLongPathRequests.size() <= moveCount) { m_AsyncLongPathRequests.swap(longRequests); - moveCount = longRequests.size(); + moveCount = (i32)longRequests.size(); } else { @@ -626,23 +625,23 @@ void CCmpPathfinder::ProcessSameTurnMoves() ProcessLongRequests(longRequests); - m_SameTurnMovesCount += moveCount; + m_SameTurnMovesCount = (u16)(m_SameTurnMovesCount + moveCount); } if (!m_AsyncShortPathRequests.empty()) { // Figure out how many moves we can do now - moveCount = m_MaxSameTurnMoves - m_SameTurnMovesCount; + i32 moveCount = m_MaxSameTurnMoves - m_SameTurnMovesCount; if (moveCount <= 0) return; // Copy the short request elements we are going to process into a new array std::vector shortRequests; - if (m_AsyncShortPathRequests.size() <= moveCount) + if ((i32)m_AsyncShortPathRequests.size() <= moveCount) { m_AsyncShortPathRequests.swap(shortRequests); - moveCount = shortRequests.size(); + moveCount = (i32)shortRequests.size(); } else { @@ -653,7 +652,7 @@ void CCmpPathfinder::ProcessSameTurnMoves() ProcessShortRequests(shortRequests); - m_SameTurnMovesCount += moveCount; + m_SameTurnMovesCount = (u16)(m_SameTurnMovesCount + moveCount); } } diff --git a/source/simulation2/components/CCmpPathfinder_Common.h b/source/simulation2/components/CCmpPathfinder_Common.h index d1f2974011..207720a5a1 100644 --- a/source/simulation2/components/CCmpPathfinder_Common.h +++ b/source/simulation2/components/CCmpPathfinder_Common.h @@ -277,8 +277,8 @@ public: */ void NearestTile(entity_pos_t x, entity_pos_t z, u16& i, u16& j) { - i = clamp((x / (int)CELL_SIZE).ToInt_RoundToZero(), 0, m_MapSize-1); - j = clamp((z / (int)CELL_SIZE).ToInt_RoundToZero(), 0, m_MapSize-1); + i = (u16)clamp((x / (int)CELL_SIZE).ToInt_RoundToZero(), 0, m_MapSize-1); + j = (u16)clamp((z / (int)CELL_SIZE).ToInt_RoundToZero(), 0, m_MapSize-1); } /** @@ -286,8 +286,8 @@ public: */ static void TileCenter(u16 i, u16 j, entity_pos_t& x, entity_pos_t& z) { - x = entity_pos_t::FromInt(i*(int)CELL_SIZE + CELL_SIZE/2); - z = entity_pos_t::FromInt(j*(int)CELL_SIZE + CELL_SIZE/2); + x = entity_pos_t::FromInt(i*(int)CELL_SIZE + (int)CELL_SIZE/2); + z = entity_pos_t::FromInt(j*(int)CELL_SIZE + (int)CELL_SIZE/2); } static fixed DistanceToGoal(CFixedVector2D pos, const CCmpPathfinder::Goal& goal); diff --git a/source/simulation2/components/CCmpPathfinder_Tile.cpp b/source/simulation2/components/CCmpPathfinder_Tile.cpp index 761d537c50..a5a85fe12e 100644 --- a/source/simulation2/components/CCmpPathfinder_Tile.cpp +++ b/source/simulation2/components/CCmpPathfinder_Tile.cpp @@ -60,13 +60,13 @@ public: void SetStatusClosed() { status = STATUS_CLOSED; } // Get pi,pj coords of predecessor to this tile on best path, given i,j coords of this tile - u16 GetPredI(u16 i) { return i+dpi; } - u16 GetPredJ(u16 j) { return j+dpj; } + u16 GetPredI(u16 i) { return (u16)(i + dpi); } + u16 GetPredJ(u16 j) { return (u16)(j + dpj); } // Set the pi,pj coords of predecessor, given i,j coords of this tile void SetPred(u16 pi_, u16 pj_, u16 i, u16 j) { - dpi = pi_-i; - dpj = pj_-j; + dpi = (i8)((int)pi_ - (int)i); + dpj = (i8)((int)pj_ - (int)j); #if PATHFIND_DEBUG // predecessor must be adjacent ENSURE(pi_-i == -1 || pi_-i == 0 || pi_-i == 1); @@ -114,14 +114,14 @@ public: virtual void ProcessTile(ssize_t i, ssize_t j) { - if (m_Pathfinder.m_Grid && !IS_PASSABLE(m_Pathfinder.m_Grid->get(i, j), m_Pathfinder.m_DebugPassClass)) + if (m_Pathfinder.m_Grid && !IS_PASSABLE(m_Pathfinder.m_Grid->get((int)i, (int)j), m_Pathfinder.m_DebugPassClass)) RenderTile(CColor(1, 0, 0, 0.6f), false); if (m_Pathfinder.m_DebugGrid) { - PathfindTile& n = m_Pathfinder.m_DebugGrid->get(i, j); + PathfindTile& n = m_Pathfinder.m_DebugGrid->get((int)i, (int)j); - float c = clamp(n.GetStep() / (float)m_Pathfinder.m_DebugSteps, 0.f, 1.f); + float c = clamp((float)n.GetStep() / (float)m_Pathfinder.m_DebugSteps, 0.f, 1.f); if (n.IsOpen()) RenderTile(CColor(1, 1, c, 0.6f), false); @@ -387,7 +387,7 @@ void CCmpPathfinder::ComputePath(entity_pos_t x0, entity_pos_t z0, const Goal& g // a large circle then the heuristics will aim us directly outwards); // otherwise just aim at the center point. (We'll never try moving outwards to a square shape.) if (goal.type == Goal::CIRCLE) - state.rGoal = (goal.hw / (int)CELL_SIZE).ToInt_RoundToZero(); + state.rGoal = (u16)(goal.hw / (int)CELL_SIZE).ToInt_RoundToZero(); else state.rGoal = 0; @@ -461,13 +461,13 @@ void CCmpPathfinder::ComputePath(entity_pos_t x0, entity_pos_t z0, const Goal& g u32 g = state.tiles->get(i, j).cost; if (i > 0) - ProcessNeighbour(i, j, i-1, j, g, state); + ProcessNeighbour(i, j, (u16)(i-1), j, g, state); if (i < m_MapSize-1) - ProcessNeighbour(i, j, i+1, j, g, state); + ProcessNeighbour(i, j, (u16)(i+1), j, g, state); if (j > 0) - ProcessNeighbour(i, j, i, j-1, g, state); + ProcessNeighbour(i, j, i, (u16)(j-1), g, state); if (j < m_MapSize-1) - ProcessNeighbour(i, j, i, j+1, g, state); + ProcessNeighbour(i, j, i, (u16)(j+1), g, state); } // Reconstruct the path (in reverse) diff --git a/source/simulation2/components/CCmpPathfinder_Vertex.cpp b/source/simulation2/components/CCmpPathfinder_Vertex.cpp index 416dd7bb62..aabdd816ae 100644 --- a/source/simulation2/components/CCmpPathfinder_Vertex.cpp +++ b/source/simulation2/components/CCmpPathfinder_Vertex.cpp @@ -382,8 +382,8 @@ static void AddTerrainEdges(std::vector& edgesAA, std::vector& ver // (The inner edges are redundant but it's easier than trying to split the squares apart.) if (any) { - CFixedVector2D v0 = CFixedVector2D(fixed::FromInt(i * CELL_SIZE) - r, fixed::FromInt(j * CELL_SIZE) - r); - CFixedVector2D v1 = CFixedVector2D(fixed::FromInt((i+1) * CELL_SIZE) + r, fixed::FromInt((j+1) * CELL_SIZE) + r); + CFixedVector2D v0 = CFixedVector2D(fixed::FromInt(i * (int)CELL_SIZE) - r, fixed::FromInt(j * (int)CELL_SIZE) - r); + CFixedVector2D v1 = CFixedVector2D(fixed::FromInt((i+1) * (int)CELL_SIZE) + r, fixed::FromInt((j+1) * (int)CELL_SIZE) + r); Edge e = { v0, v1 }; edgesAA.push_back(e); } @@ -409,32 +409,32 @@ static void AddTerrainEdges(std::vector& edgesAA, std::vector& ver { case TileEdge::BOTTOM: { - v0 = CFixedVector2D(fixed::FromInt(i * CELL_SIZE) - r, fixed::FromInt(j * CELL_SIZE) - r); - v1 = CFixedVector2D(fixed::FromInt((i+1) * CELL_SIZE) + r, fixed::FromInt(j * CELL_SIZE) - r); + v0 = CFixedVector2D(fixed::FromInt(i * (int)CELL_SIZE) - r, fixed::FromInt(j * (int)CELL_SIZE) - r); + v1 = CFixedVector2D(fixed::FromInt((i+1) * (int)CELL_SIZE) + r, fixed::FromInt(j * (int)CELL_SIZE) - r); vert.p.X = v0.X - EDGE_EXPAND_DELTA; vert.p.Y = v0.Y - EDGE_EXPAND_DELTA; vert.quadInward = QUADRANT_TR; vertexes.push_back(vert); vert.p.X = v1.X + EDGE_EXPAND_DELTA; vert.p.Y = v1.Y - EDGE_EXPAND_DELTA; vert.quadInward = QUADRANT_TL; vertexes.push_back(vert); break; } case TileEdge::TOP: { - v0 = CFixedVector2D(fixed::FromInt((i+1) * CELL_SIZE) + r, fixed::FromInt((j+1) * CELL_SIZE) + r); - v1 = CFixedVector2D(fixed::FromInt(i * CELL_SIZE) - r, fixed::FromInt((j+1) * CELL_SIZE) + r); + v0 = CFixedVector2D(fixed::FromInt((i+1) * (int)CELL_SIZE) + r, fixed::FromInt((j+1) * (int)CELL_SIZE) + r); + v1 = CFixedVector2D(fixed::FromInt(i * (int)CELL_SIZE) - r, fixed::FromInt((j+1) * (int)CELL_SIZE) + r); vert.p.X = v0.X + EDGE_EXPAND_DELTA; vert.p.Y = v0.Y + EDGE_EXPAND_DELTA; vert.quadInward = QUADRANT_BL; vertexes.push_back(vert); vert.p.X = v1.X - EDGE_EXPAND_DELTA; vert.p.Y = v1.Y + EDGE_EXPAND_DELTA; vert.quadInward = QUADRANT_BR; vertexes.push_back(vert); break; } case TileEdge::LEFT: { - v0 = CFixedVector2D(fixed::FromInt(i * CELL_SIZE) - r, fixed::FromInt((j+1) * CELL_SIZE) + r); - v1 = CFixedVector2D(fixed::FromInt(i * CELL_SIZE) - r, fixed::FromInt(j * CELL_SIZE) - r); + v0 = CFixedVector2D(fixed::FromInt(i * (int)CELL_SIZE) - r, fixed::FromInt((j+1) * (int)CELL_SIZE) + r); + v1 = CFixedVector2D(fixed::FromInt(i * (int)CELL_SIZE) - r, fixed::FromInt(j * (int)CELL_SIZE) - r); vert.p.X = v0.X - EDGE_EXPAND_DELTA; vert.p.Y = v0.Y + EDGE_EXPAND_DELTA; vert.quadInward = QUADRANT_BR; vertexes.push_back(vert); vert.p.X = v1.X - EDGE_EXPAND_DELTA; vert.p.Y = v1.Y - EDGE_EXPAND_DELTA; vert.quadInward = QUADRANT_TR; vertexes.push_back(vert); break; } case TileEdge::RIGHT: { - v0 = CFixedVector2D(fixed::FromInt((i+1) * CELL_SIZE) + r, fixed::FromInt(j * CELL_SIZE) - r); - v1 = CFixedVector2D(fixed::FromInt((i+1) * CELL_SIZE) + r, fixed::FromInt((j+1) * CELL_SIZE) + r); + v0 = CFixedVector2D(fixed::FromInt((i+1) * (int)CELL_SIZE) + r, fixed::FromInt(j * (int)CELL_SIZE) - r); + v1 = CFixedVector2D(fixed::FromInt((i+1) * (int)CELL_SIZE) + r, fixed::FromInt((j+1) * (int)CELL_SIZE) + r); vert.p.X = v0.X + EDGE_EXPAND_DELTA; vert.p.Y = v0.Y - EDGE_EXPAND_DELTA; vert.quadInward = QUADRANT_TL; vertexes.push_back(vert); vert.p.X = v1.X + EDGE_EXPAND_DELTA; vert.p.Y = v1.Y + EDGE_EXPAND_DELTA; vert.quadInward = QUADRANT_BL; vertexes.push_back(vert); break; @@ -523,7 +523,7 @@ void CCmpPathfinder::ComputeShortPath(const IObstructionTestFilter& filter, } case CCmpPathfinder::Goal::SQUARE: { - float a = atan2(goal.v.X.ToFloat(), goal.v.Y.ToFloat()); + float a = atan2f(goal.v.X.ToFloat(), goal.v.Y.ToFloat()); SimRender::ConstructSquareOnGround(GetSimContext(), goal.x.ToFloat(), goal.z.ToFloat(), goal.hw.ToFloat()*2, goal.hh.ToFloat()*2, a, m_DebugOverlayShortPathLines.back(), true); break; } @@ -800,7 +800,7 @@ void CCmpPathfinder::ComputeShortPath(const IObstructionTestFilter& filter, // Hack: If we started *inside* a shape then perhaps headed to its corner (e.g. the unit // was very near another unit), don't restrict further pathing. if (vertexes[n].quadInward && !(curr.id == START_VERTEX_ID && g < fixed::FromInt(8))) - vertexes[n].quadOutward = ((~vertexes[n].quadInward) & quad); + vertexes[n].quadOutward = ((~vertexes[n].quadInward) & quad) & 0xF; if (n == GOAL_VERTEX_ID) vertexes[n].p = npos; // remember the new best goal position @@ -829,7 +829,7 @@ void CCmpPathfinder::ComputeShortPath(const IObstructionTestFilter& filter, // If this is an axis-aligned shape, the path must continue in the same quadrant // direction (but not go into the inside of the shape). if (vertexes[n].quadInward) - vertexes[n].quadOutward = ((~vertexes[n].quadInward) & quad); + vertexes[n].quadOutward = ((~vertexes[n].quadInward) & quad) & 0xF; if (n == GOAL_VERTEX_ID) vertexes[n].p = npos; // remember the new best goal position diff --git a/source/simulation2/components/CCmpPosition.cpp b/source/simulation2/components/CCmpPosition.cpp index ca63383536..b566c58130 100644 --- a/source/simulation2/components/CCmpPosition.cpp +++ b/source/simulation2/components/CCmpPosition.cpp @@ -395,7 +395,7 @@ public: float rotY = m_RotY.ToFloat(); float delta = rotY - m_InterpolatedRotY; // Wrap delta to -M_PI..M_PI - delta = fmod(delta + (float)M_PI, 2*(float)M_PI); // range -2PI..2PI + delta = fmodf(delta + (float)M_PI, 2*(float)M_PI); // range -2PI..2PI if (delta < 0) delta += 2*(float)M_PI; // range 0..2PI delta -= (float)M_PI; // range -M_PI..M_PI // Clamp to max rate diff --git a/source/simulation2/components/CCmpProjectileManager.cpp b/source/simulation2/components/CCmpProjectileManager.cpp index 80cf6f569a..c54d3fc5bf 100644 --- a/source/simulation2/components/CCmpProjectileManager.cpp +++ b/source/simulation2/components/CCmpProjectileManager.cpp @@ -196,7 +196,7 @@ void CCmpProjectileManager::LaunchProjectile(entity_id_t source, CFixedVector3D projectile.targetEnt = targetEnt; CVector3D offset = projectile.target - projectile.pos; - float horizDistance = hypot(offset.X, offset.Z); + float horizDistance = hypotf(offset.X, offset.Z); projectile.speedFactor = 1.f; projectile.timeLeft = horizDistance / speed.ToFloat(); @@ -221,7 +221,7 @@ void CCmpProjectileManager::AdvanceProjectile(Projectile& projectile, float dt, // To prevent arrows going crazily far after missing the target, // apply a bit of drag to them - projectile.speedFactor *= pow(1.0f - 0.4f*projectile.speedFactor, dt); + projectile.speedFactor *= powf(1.0f - 0.4f*projectile.speedFactor, dt); } else { @@ -282,7 +282,7 @@ void CCmpProjectileManager::AdvanceProjectile(Projectile& projectile, float dt, else axis.Normalize(); - float angle = acos(up.Dot(delta)); + float angle = acosf(up.Dot(delta)); CMatrix3D transform; CQuaternion quat; diff --git a/source/simulation2/components/CCmpRangeManager.cpp b/source/simulation2/components/CCmpRangeManager.cpp index cd26bb9775..b07379a0d5 100644 --- a/source/simulation2/components/CCmpRangeManager.cpp +++ b/source/simulation2/components/CCmpRangeManager.cpp @@ -200,7 +200,7 @@ public: // 2-bit ELosState per player, starting with player 1 (not 0!) up to player MAX_LOS_PLAYER_ID (inclusive) std::vector m_LosState; - static const int MAX_LOS_PLAYER_ID = 16; + static const player_id_t MAX_LOS_PLAYER_ID = 16; // Special static visibility data for the "reveal whole map" mode // (TODO: this is usually a waste of memory) @@ -377,7 +377,8 @@ public: LosAdd(msgData.to, it->second.visionRange, pos); } - it->second.owner = msgData.to; + ENSURE(-128 <= msgData.to && msgData.to <= 127); + it->second.owner = (i8)msgData.to; break; } @@ -421,7 +422,7 @@ public: m_WorldZ0 = z0; m_WorldX1 = x1; m_WorldZ1 = z1; - m_TerrainVerticesPerSide = vertices; + m_TerrainVerticesPerSide = (i32)vertices; ResetDerivedData(false); } @@ -467,10 +468,10 @@ public: entity_pos_t minRange, entity_pos_t maxRange, std::vector owners, int requiredInterface) { - size_t id = m_QueryNext++; + tag_t id = m_QueryNext++; m_Queries[id] = ConstructQuery(source, minRange, maxRange, owners, requiredInterface); - return (tag_t)id; + return id; } virtual void DestroyActiveQuery(tag_t tag) @@ -774,14 +775,14 @@ public: // Draw the max range circle m_DebugOverlayLines.push_back(SOverlayLine()); m_DebugOverlayLines.back().m_Color = (q.enabled ? enabledRingColour : disabledRingColour); - SimRender::ConstructCircleOnGround(GetSimContext(), pos.X.ToFloat(), pos.Y.ToDouble(), q.maxRange.ToFloat(), m_DebugOverlayLines.back(), true); + SimRender::ConstructCircleOnGround(GetSimContext(), pos.X.ToFloat(), pos.Y.ToFloat(), q.maxRange.ToFloat(), m_DebugOverlayLines.back(), true); // Draw the min range circle if (!q.minRange.IsZero()) { m_DebugOverlayLines.push_back(SOverlayLine()); m_DebugOverlayLines.back().m_Color = (q.enabled ? enabledRingColour : disabledRingColour); - SimRender::ConstructCircleOnGround(GetSimContext(), pos.X.ToFloat(), pos.Y.ToDouble(), q.minRange.ToFloat(), m_DebugOverlayLines.back(), true); + SimRender::ConstructCircleOnGround(GetSimContext(), pos.X.ToFloat(), pos.Y.ToFloat(), q.minRange.ToFloat(), m_DebugOverlayLines.back(), true); } // Draw a ray from the source to each matched entity @@ -911,9 +912,9 @@ public: // For each tile, if it is owned by a valid player then update the LOS // for every vertex around that tile, to mark them as explored - for (size_t j = 0; j < grid.m_H; ++j) + for (u16 j = 0; j < grid.m_H; ++j) { - for (size_t i = 0; i < grid.m_W; ++i) + for (u16 i = 0; i < grid.m_W; ++i) { u8 p = grid.get(i, j); if (p > 0 && p <= MAX_LOS_PLAYER_ID) @@ -979,7 +980,7 @@ public: m_LosState[idx] |= ((LOS_VISIBLE | LOS_EXPLORED) << (2*(owner-1))); } - counts[idx] += 1; + counts[idx] = (u16)(counts[idx] + 1); // ignore overflow; the player should never have 64K units } } @@ -996,7 +997,7 @@ public: for (i32 idx = idx0; idx <= idx1; ++idx) { - counts[idx] -= 1; + counts[idx] = (u16)(counts[idx] - 1); // Decreasing from non-zero to zero - move from visible+explored to explored if (counts[idx] == 0) @@ -1196,23 +1197,23 @@ public: } } - void LosAdd(i8 owner, entity_pos_t visionRange, CFixedVector2D pos) + void LosAdd(player_id_t owner, entity_pos_t visionRange, CFixedVector2D pos) { if (visionRange.IsZero() || owner <= 0 || owner > MAX_LOS_PLAYER_ID) return; - LosUpdateHelper(owner, visionRange, pos); + LosUpdateHelper((u8)owner, visionRange, pos); } - void LosRemove(i8 owner, entity_pos_t visionRange, CFixedVector2D pos) + void LosRemove(player_id_t owner, entity_pos_t visionRange, CFixedVector2D pos) { if (visionRange.IsZero() || owner <= 0 || owner > MAX_LOS_PLAYER_ID) return; - LosUpdateHelper(owner, visionRange, pos); + LosUpdateHelper((u8)owner, visionRange, pos); } - void LosMove(i8 owner, entity_pos_t visionRange, CFixedVector2D from, CFixedVector2D to) + void LosMove(player_id_t owner, entity_pos_t visionRange, CFixedVector2D from, CFixedVector2D to) { if (visionRange.IsZero() || owner <= 0 || owner > MAX_LOS_PLAYER_ID) return; @@ -1221,14 +1222,14 @@ public: { // If it's a very large move, then simply remove and add to the new position - LosUpdateHelper(owner, visionRange, from); - LosUpdateHelper(owner, visionRange, to); + LosUpdateHelper((u8)owner, visionRange, from); + LosUpdateHelper((u8)owner, visionRange, to); } else { // Otherwise use the version optimised for mostly-overlapping circles - LosUpdateHelperIncremental(owner, visionRange, from, to); + LosUpdateHelperIncremental((u8)owner, visionRange, from, to); } } diff --git a/source/simulation2/components/CCmpTerrain.cpp b/source/simulation2/components/CCmpTerrain.cpp index 9cadf75435..b209629cd9 100644 --- a/source/simulation2/components/CCmpTerrain.cpp +++ b/source/simulation2/components/CCmpTerrain.cpp @@ -79,9 +79,18 @@ public: return m_Terrain->GetExactGroundLevel(x, z); } - virtual uint32_t GetVerticesPerSide() + virtual u16 GetTilesPerSide() { - return m_Terrain->GetVerticesPerSide(); + ssize_t tiles = m_Terrain->GetTilesPerSide(); + ENSURE(1 <= tiles && tiles <= 65535); + return (u16)tiles; + } + + virtual u16 GetVerticesPerSide() + { + ssize_t vertices = m_Terrain->GetVerticesPerSide(); + ENSURE(1 <= vertices && vertices <= 65535); + return (u16)vertices; } virtual CTerrain* GetCTerrain() @@ -93,27 +102,30 @@ public: { // TODO: should refactor this code to be nicer + u16 tiles = GetTilesPerSide(); + u16 vertices = GetVerticesPerSide(); + CmpPtr cmpObstructionManager(GetSimContext(), SYSTEM_ENTITY); if (!cmpObstructionManager.null()) { cmpObstructionManager->SetBounds(entity_pos_t::Zero(), entity_pos_t::Zero(), - entity_pos_t::FromInt(m_Terrain->GetTilesPerSide()*CELL_SIZE), - entity_pos_t::FromInt(m_Terrain->GetTilesPerSide()*CELL_SIZE)); + entity_pos_t::FromInt(tiles*(int)CELL_SIZE), + entity_pos_t::FromInt(tiles*(int)CELL_SIZE)); } CmpPtr cmpRangeManager(GetSimContext(), SYSTEM_ENTITY); if (!cmpRangeManager.null()) { cmpRangeManager->SetBounds(entity_pos_t::Zero(), entity_pos_t::Zero(), - entity_pos_t::FromInt(m_Terrain->GetTilesPerSide()*CELL_SIZE), - entity_pos_t::FromInt(m_Terrain->GetTilesPerSide()*CELL_SIZE), - m_Terrain->GetVerticesPerSide()); + entity_pos_t::FromInt(tiles*(int)CELL_SIZE), + entity_pos_t::FromInt(tiles*(int)CELL_SIZE), + vertices); } - MakeDirty(0, 0, m_Terrain->GetTilesPerSide()+1, m_Terrain->GetTilesPerSide()+1); + MakeDirty(0, 0, tiles+1, tiles+1); } - virtual void MakeDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1) + virtual void MakeDirty(i32 i0, i32 j0, i32 i1, i32 j1) { CMessageTerrainChanged msg(i0, j0, i1, j1); GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg); diff --git a/source/simulation2/components/CCmpTerritoryInfluence.cpp b/source/simulation2/components/CCmpTerritoryInfluence.cpp index dd7ec0a9de..029bedb2c5 100644 --- a/source/simulation2/components/CCmpTerritoryInfluence.cpp +++ b/source/simulation2/components/CCmpTerritoryInfluence.cpp @@ -29,7 +29,7 @@ public: DEFAULT_COMPONENT_ALLOCATOR(TerritoryInfluence) - int m_Cost; + i32 m_Cost; u32 m_Weight; u32 m_Radius; @@ -75,7 +75,7 @@ public: Init(paramNode); } - virtual int GetCost() + virtual i32 GetCost() { return m_Cost; } diff --git a/source/simulation2/components/CCmpTerritoryManager.cpp b/source/simulation2/components/CCmpTerritoryManager.cpp index cbfc1f8419..b6dce86d42 100644 --- a/source/simulation2/components/CCmpTerritoryManager.cpp +++ b/source/simulation2/components/CCmpTerritoryManager.cpp @@ -97,7 +97,9 @@ public: CParamNode externalParamNode; CParamNode::LoadXML(externalParamNode, L"simulation/data/territorymanager.xml"); - m_ImpassableCost = externalParamNode.GetChild("TerritoryManager").GetChild("ImpassableCost").ToInt(); + int impassableCost = externalParamNode.GetChild("TerritoryManager").GetChild("ImpassableCost").ToInt(); + ENSURE(0 <= impassableCost && impassableCost <= 255); + m_ImpassableCost = (u8)impassableCost; m_BorderThickness = externalParamNode.GetChild("TerritoryManager").GetChild("BorderThickness").ToFixed().ToFloat(); m_BorderSeparation = externalParamNode.GetChild("TerritoryManager").GetChild("BorderSeparation").ToFixed().ToFloat(); } @@ -259,21 +261,21 @@ static void FloodFill(Grid& grid, Grid& costGrid, OpenQueue& openTiles, u16 x = tile.id.first; u16 z = tile.id.second; if (x > 0) - ProcessNeighbour(falloff, x-1, z, tile.rank, false, grid, openTiles, costGrid); + ProcessNeighbour(falloff, (u16)(x-1), z, tile.rank, false, grid, openTiles, costGrid); if (x < tilesW-1) - ProcessNeighbour(falloff, x+1, z, tile.rank, false, grid, openTiles, costGrid); + ProcessNeighbour(falloff, (u16)(x+1), z, tile.rank, false, grid, openTiles, costGrid); if (z > 0) - ProcessNeighbour(falloff, x, z-1, tile.rank, false, grid, openTiles, costGrid); + ProcessNeighbour(falloff, x, (u16)(z-1), tile.rank, false, grid, openTiles, costGrid); if (z < tilesH-1) - ProcessNeighbour(falloff, x, z+1, tile.rank, false, grid, openTiles, costGrid); + ProcessNeighbour(falloff, x, (u16)(z+1), tile.rank, false, grid, openTiles, costGrid); if (x > 0 && z > 0) - ProcessNeighbour(falloff, x-1, z-1, tile.rank, true, grid, openTiles, costGrid); + ProcessNeighbour(falloff, (u16)(x-1), (u16)(z-1), tile.rank, true, grid, openTiles, costGrid); if (x > 0 && z < tilesH-1) - ProcessNeighbour(falloff, x-1, z+1, tile.rank, true, grid, openTiles, costGrid); + ProcessNeighbour(falloff, (u16)(x-1), (u16)(z+1), tile.rank, true, grid, openTiles, costGrid); if (x < tilesW-1 && z > 0) - ProcessNeighbour(falloff, x+1, z-1, tile.rank, true, grid, openTiles, costGrid); + ProcessNeighbour(falloff, (u16)(x+1), (u16)(z-1), tile.rank, true, grid, openTiles, costGrid); if (x < tilesW-1 && z < tilesH-1) - ProcessNeighbour(falloff, x+1, z+1, tile.rank, true, grid, openTiles, costGrid); + ProcessNeighbour(falloff, (u16)(x+1), (u16)(z+1), tile.rank, true, grid, openTiles, costGrid); } } @@ -285,8 +287,8 @@ void CCmpTerritoryManager::CalculateTerritories() return; CmpPtr cmpTerrain(GetSimContext(), SYSTEM_ENTITY); - uint32_t tilesW = cmpTerrain->GetVerticesPerSide() - 1; - uint32_t tilesH = cmpTerrain->GetVerticesPerSide() - 1; + u16 tilesW = cmpTerrain->GetTilesPerSide(); + u16 tilesH = cmpTerrain->GetTilesPerSide(); SAFE_DELETE(m_Territories); m_Territories = new Grid(tilesW, tilesH); @@ -298,9 +300,9 @@ void CCmpTerritoryManager::CalculateTerritories() ICmpPathfinder::pass_class_t passClassUnrestricted = cmpPathfinder->GetPassabilityClass("unrestricted"); ICmpPathfinder::pass_class_t passClassDefault = cmpPathfinder->GetPassabilityClass("default"); const Grid& passGrid = cmpPathfinder->GetPassabilityGrid(); - for (u32 j = 0; j < tilesH; ++j) + for (u16 j = 0; j < tilesH; ++j) { - for (u32 i = 0; i < tilesW; ++i) + for (u16 i = 0; i < tilesW; ++i) { u16 g = passGrid.get(i, j); u8 cost; @@ -364,8 +366,8 @@ void CCmpTerritoryManager::CalculateTerritories() CmpPtr cmpPosition(GetSimContext(), *eit); CFixedVector2D pos = cmpPosition->GetPosition2D(); - int i = clamp((pos.X / (int)CELL_SIZE).ToInt_RoundToNegInfinity(), 0, (int)tilesW-1); - int j = clamp((pos.Y / (int)CELL_SIZE).ToInt_RoundToNegInfinity(), 0, (int)tilesH-1); + u16 i = (u16)clamp((pos.X / (int)CELL_SIZE).ToInt_RoundToNegInfinity(), 0, tilesW-1); + u16 j = (u16)clamp((pos.Y / (int)CELL_SIZE).ToInt_RoundToNegInfinity(), 0, tilesH-1); CmpPtr cmpTerritoryInfluence(GetSimContext(), *eit); u32 weight = cmpTerritoryInfluence->GetWeight(); @@ -418,8 +420,8 @@ void CCmpTerritoryManager::CalculateTerritories() */ static void NearestTile(entity_pos_t x, entity_pos_t z, u16& i, u16& j, u16 w, u16 h) { - i = clamp((x / (int)CELL_SIZE).ToInt_RoundToZero(), 0, w-1); - j = clamp((z / (int)CELL_SIZE).ToInt_RoundToZero(), 0, h-1); + i = (u16)clamp((x / (int)CELL_SIZE).ToInt_RoundToZero(), 0, w-1); + j = (u16)clamp((z / (int)CELL_SIZE).ToInt_RoundToZero(), 0, h-1); } /** @@ -427,8 +429,8 @@ static void NearestTile(entity_pos_t x, entity_pos_t z, u16& i, u16& j, u16 w, u */ static void TileCenter(u16 i, u16 j, entity_pos_t& x, entity_pos_t& z) { - x = entity_pos_t::FromInt(i*(int)CELL_SIZE + CELL_SIZE/2); - z = entity_pos_t::FromInt(j*(int)CELL_SIZE + CELL_SIZE/2); + x = entity_pos_t::FromInt(i*(int)CELL_SIZE + (int)CELL_SIZE/2); + z = entity_pos_t::FromInt(j*(int)CELL_SIZE + (int)CELL_SIZE/2); } // TODO: would be nice not to duplicate those two functions from CCmpObstructionManager.cpp @@ -440,7 +442,7 @@ void CCmpTerritoryManager::RasteriseInfluences(CComponentManager::InterfaceList& { ICmpTerritoryInfluence* cmpTerritoryInfluence = static_cast(it->second); - int cost = cmpTerritoryInfluence->GetCost(); + i32 cost = cmpTerritoryInfluence->GetCost(); if (cost == -1) continue; @@ -465,7 +467,7 @@ void CCmpTerritoryManager::RasteriseInfluences(CComponentManager::InterfaceList& entity_pos_t x, z; TileCenter(i, j, x, z); if (Geometry::PointIsInSquare(CFixedVector2D(x - square.x, z - square.z), square.u, square.v, halfSize)) - grid.set(i, j, cost); + grid.set(i, j, (u8)cost); } } @@ -492,9 +494,9 @@ std::vector CCmpTerritoryManager::Compu }; // Try to find an assigned tile - for (int j = 0; j < grid.m_H; ++j) + for (u16 j = 0; j < grid.m_H; ++j) { - for (int i = 0; i < grid.m_W; ++i) + for (u16 i = 0; i < grid.m_W; ++i) { u8 owner = grid.get(i, j); if (owner) @@ -507,10 +509,13 @@ std::vector CCmpTerritoryManager::Compu boundaries.back().owner = owner; std::vector& points = boundaries.back().points; - int dir = 0; // 0 == bottom edge of tile, 1 == right, 2 == top, 3 == left + u8 dir = 0; // 0 == bottom edge of tile, 1 == right, 2 == top, 3 == left - int cdir = dir; - int ci = i, cj = j; + u8 cdir = dir; + u16 ci = i, cj = j; + + u16 maxi = (u16)(grid.m_W-1); + u16 maxj = (u16)(grid.m_H-1); while (true) { @@ -522,31 +527,31 @@ std::vector CCmpTerritoryManager::Compu switch (cdir) { case 0: - if (ci < grid.m_W-1 && cj > 0 && grid.get(ci+1, cj-1) == owner) + if (ci < maxi && cj > 0 && grid.get(ci+1, cj-1) == owner) { ++ci; --cj; cdir = 3; } - else if (ci < grid.m_W-1 && grid.get(ci+1, cj) == owner) + else if (ci < maxi && grid.get(ci+1, cj) == owner) ++ci; else cdir = 1; break; case 1: - if (ci < grid.m_W-1 && cj < grid.m_H-1 && grid.get(ci+1, cj+1) == owner) + if (ci < maxi && cj < maxj && grid.get(ci+1, cj+1) == owner) { ++ci; ++cj; cdir = 0; } - else if (cj < grid.m_H-1 && grid.get(ci, cj+1) == owner) + else if (cj < maxj && grid.get(ci, cj+1) == owner) ++cj; else cdir = 2; break; case 2: - if (ci > 0 && cj < grid.m_H-1 && grid.get(ci-1, cj+1) == owner) + if (ci > 0 && cj < maxj && grid.get(ci-1, cj+1) == owner) { --ci; ++cj; @@ -578,7 +583,7 @@ std::vector CCmpTerritoryManager::Compu // Zero out this whole territory with a simple flood fill, so we don't // process it a second time - std::vector > tileStack; + std::vector > tileStack; #define ZERO_AND_PUSH(i, j) STMT(grid.set(i, j, 0); tileStack.push_back(std::make_pair(i, j)); ) @@ -591,20 +596,20 @@ std::vector CCmpTerritoryManager::Compu if (ti > 0 && grid.get(ti-1, tj) == owner) ZERO_AND_PUSH(ti-1, tj); - if (ti < grid.m_W-1 && grid.get(ti+1, tj) == owner) + if (ti < maxi && grid.get(ti+1, tj) == owner) ZERO_AND_PUSH(ti+1, tj); if (tj > 0 && grid.get(ti, tj-1) == owner) ZERO_AND_PUSH(ti, tj-1); - if (tj < grid.m_H-1 && grid.get(ti, tj+1) == owner) + if (tj < maxj && grid.get(ti, tj+1) == owner) ZERO_AND_PUSH(ti, tj+1); if (ti > 0 && tj > 0 && grid.get(ti-1, tj-1) == owner) ZERO_AND_PUSH(ti-1, tj-1); - if (ti > 0 && tj < grid.m_H-1 && grid.get(ti-1, tj+1) == owner) + if (ti > 0 && tj < maxj && grid.get(ti-1, tj+1) == owner) ZERO_AND_PUSH(ti-1, tj+1); - if (ti < grid.m_W-1 && tj > 0 && grid.get(ti+1, tj-1) == owner) + if (ti < maxi && tj > 0 && grid.get(ti+1, tj-1) == owner) ZERO_AND_PUSH(ti+1, tj-1); - if (ti < grid.m_W-1 && tj < grid.m_H-1 && grid.get(ti+1, tj+1) == owner) + if (ti < maxi && tj < maxj && grid.get(ti+1, tj+1) == owner) ZERO_AND_PUSH(ti+1, tj+1); } @@ -703,7 +708,7 @@ void TerritoryOverlay::ProcessTile(ssize_t i, ssize_t j) if (!m_TerritoryManager.m_Territories) return; - u8 id = m_TerritoryManager.m_Territories->get(i, j); + u8 id = m_TerritoryManager.m_Territories->get((int)i, (int)j); float a = 0.2f; switch (id) diff --git a/source/simulation2/components/CCmpUnitMotion.cpp b/source/simulation2/components/CCmpUnitMotion.cpp index 5a8ec09fd0..9175d88c4c 100644 --- a/source/simulation2/components/CCmpUnitMotion.cpp +++ b/source/simulation2/components/CCmpUnitMotion.cpp @@ -1042,7 +1042,7 @@ bool CCmpUnitMotion::PathIsShort(const ICmpPathfinder::Path& path, CFixedVector2 CFixedVector2D pos = from; entity_pos_t distLeft = minDistance; - for (ssize_t i = path.m_Waypoints.size()-1; i >= 0; --i) + for (ssize_t i = (ssize_t)path.m_Waypoints.size()-1; i >= 0; --i) { // Check if the next path segment is longer than the requested minimum CFixedVector2D waypoint(path.m_Waypoints[i].x, path.m_Waypoints[i].z); diff --git a/source/simulation2/components/CCmpVisualActor.cpp b/source/simulation2/components/CCmpVisualActor.cpp index 8d3f3c83f9..f35cb80626 100644 --- a/source/simulation2/components/CCmpVisualActor.cpp +++ b/source/simulation2/components/CCmpVisualActor.cpp @@ -351,7 +351,7 @@ public: // Save some data from the old unit CColor shading = m_Unit->GetModel().GetShadingColor(); - size_t playerID = m_Unit->GetModel().GetPlayerID(); + player_id_t playerID = m_Unit->GetModel().GetPlayerID(); // Replace with the new unit GetSimContext().GetUnitManager().DeleteUnit(m_Unit); diff --git a/source/simulation2/components/ICmpObstructionManager.h b/source/simulation2/components/ICmpObstructionManager.h index 9abb27e9db..50f8189dbd 100644 --- a/source/simulation2/components/ICmpObstructionManager.h +++ b/source/simulation2/components/ICmpObstructionManager.h @@ -79,6 +79,11 @@ public: FLAG_MOVING = (1 << 4) // indicates this unit is currently moving }; + /** + * Bitmask of EFlag values. + */ + typedef u8 flags_t; + /** * Set the bounds of the world. * Any point outside the bounds is considered obstructed. @@ -97,7 +102,7 @@ public: * @param flags a set of EFlags values * @return a valid tag for manipulating the shape */ - virtual tag_t AddStaticShape(entity_id_t ent, entity_pos_t x, entity_pos_t z, entity_angle_t a, entity_pos_t w, entity_pos_t h, u8 flags) = 0; + virtual tag_t AddStaticShape(entity_id_t ent, entity_pos_t x, entity_pos_t z, entity_angle_t a, entity_pos_t w, entity_pos_t h, flags_t flags) = 0; /** * Register a unit shape. @@ -109,7 +114,7 @@ public: * @param moving whether the unit is currently moving through the world or is stationary * @return a valid tag for manipulating the shape */ - virtual tag_t AddUnitShape(entity_id_t ent, entity_pos_t x, entity_pos_t z, entity_angle_t r, u8 flags, entity_id_t group) = 0; + virtual tag_t AddUnitShape(entity_id_t ent, entity_pos_t x, entity_pos_t z, entity_angle_t r, flags_t flags, entity_id_t group) = 0; /** * Adjust the position and angle of an existing shape. @@ -262,6 +267,9 @@ public: class IObstructionTestFilter { public: + typedef ICmpObstructionManager::tag_t tag_t; + typedef ICmpObstructionManager::flags_t flags_t; + virtual ~IObstructionTestFilter() {} /** @@ -271,7 +279,7 @@ public: * @param flags set of EFlags for the shape * @param group the control group (typically the shape's unit, or the unit's formation controller, or 0) */ - virtual bool Allowed(ICmpObstructionManager::tag_t tag, u8 flags, entity_id_t group) const = 0; + virtual bool Allowed(tag_t tag, flags_t flags, entity_id_t group) const = 0; }; /** @@ -280,7 +288,7 @@ public: class NullObstructionFilter : public IObstructionTestFilter { public: - virtual bool Allowed(ICmpObstructionManager::tag_t UNUSED(tag), u8 UNUSED(flags), entity_id_t UNUSED(group)) const + virtual bool Allowed(tag_t UNUSED(tag), flags_t UNUSED(flags), entity_id_t UNUSED(group)) const { return true; } @@ -292,7 +300,7 @@ public: class StationaryObstructionFilter : public IObstructionTestFilter { public: - virtual bool Allowed(ICmpObstructionManager::tag_t UNUSED(tag), u8 flags, entity_id_t UNUSED(group)) const + virtual bool Allowed(tag_t UNUSED(tag), flags_t flags, entity_id_t UNUSED(group)) const { return !(flags & ICmpObstructionManager::FLAG_MOVING); } @@ -313,7 +321,7 @@ public: { } - virtual bool Allowed(ICmpObstructionManager::tag_t UNUSED(tag), u8 flags, entity_id_t group) const + virtual bool Allowed(tag_t UNUSED(tag), flags_t flags, entity_id_t group) const { if (group == m_Group) return false; @@ -330,13 +338,13 @@ public: */ class SkipTagObstructionFilter : public IObstructionTestFilter { - ICmpObstructionManager::tag_t m_Tag; + tag_t m_Tag; public: - SkipTagObstructionFilter(ICmpObstructionManager::tag_t tag) : m_Tag(tag) + SkipTagObstructionFilter(tag_t tag) : m_Tag(tag) { } - virtual bool Allowed(ICmpObstructionManager::tag_t tag, u8 UNUSED(flags), entity_id_t UNUSED(group)) const + virtual bool Allowed(tag_t tag, flags_t UNUSED(flags), entity_id_t UNUSED(group)) const { return tag.n != m_Tag.n; } @@ -347,14 +355,14 @@ public: */ class SkipTagFlagsObstructionFilter : public IObstructionTestFilter { - ICmpObstructionManager::tag_t m_Tag; - u8 m_Mask; + tag_t m_Tag; + flags_t m_Mask; public: - SkipTagFlagsObstructionFilter(ICmpObstructionManager::tag_t tag, u8 mask) : m_Tag(tag), m_Mask(mask) + SkipTagFlagsObstructionFilter(tag_t tag, flags_t mask) : m_Tag(tag), m_Mask(mask) { } - virtual bool Allowed(ICmpObstructionManager::tag_t tag, u8 flags, entity_id_t UNUSED(group)) const + virtual bool Allowed(tag_t tag, flags_t flags, entity_id_t UNUSED(group)) const { return (tag.n != m_Tag.n && (flags & m_Mask) != 0); } diff --git a/source/simulation2/components/ICmpTerrain.h b/source/simulation2/components/ICmpTerrain.h index fd5b13e632..e5573b050c 100644 --- a/source/simulation2/components/ICmpTerrain.h +++ b/source/simulation2/components/ICmpTerrain.h @@ -35,7 +35,17 @@ public: virtual float GetExactGroundLevel(float x, float z) = 0; - virtual uint32_t GetVerticesPerSide() = 0; + /** + * Returns number of tiles per side on the terrain. + * Return value is always non-zero. + */ + virtual u16 GetTilesPerSide() = 0; + + /** + * Returns number of vertices per side on the terrain. + * Return value is always non-zero. + */ + virtual u16 GetVerticesPerSide() = 0; virtual CTerrain* GetCTerrain() = 0; @@ -51,7 +61,7 @@ public: * exclusive upper bound) have been changed. CMessageTerrainChanged will be * sent to any components that care about terrain changes. */ - virtual void MakeDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1) = 0; + virtual void MakeDirty(i32 i0, i32 j0, i32 i1, i32 j1) = 0; DECLARE_INTERFACE_TYPE(Terrain) }; diff --git a/source/simulation2/components/ICmpTerritoryInfluence.h b/source/simulation2/components/ICmpTerritoryInfluence.h index 7c6ab827cb..f4f666d6e2 100644 --- a/source/simulation2/components/ICmpTerritoryInfluence.h +++ b/source/simulation2/components/ICmpTerritoryInfluence.h @@ -28,7 +28,7 @@ public: * in [0, 255] to indicate overriding the normal cost of the terrain * under the entity's obstruction. */ - virtual int GetCost() = 0; + virtual i32 GetCost() = 0; virtual u32 GetWeight() = 0; diff --git a/source/simulation2/helpers/Grid.h b/source/simulation2/helpers/Grid.h index 1ce1eb1c21..fc087a2f46 100644 --- a/source/simulation2/helpers/Grid.h +++ b/source/simulation2/helpers/Grid.h @@ -80,18 +80,18 @@ public: memset(m_Data, 0, m_W*m_H*sizeof(T)); } - void set(size_t i, size_t j, const T& value) + void set(int i, int j, const T& value) { #if GRID_BOUNDS_DEBUG - ENSURE(i < m_W && j < m_H); + ENSURE(0 <= i && i < m_W && 0 <= j && j < m_H); #endif m_Data[j*m_W + i] = value; } - T& get(size_t i, size_t j) const + T& get(int i, int j) const { #if GRID_BOUNDS_DEBUG - ENSURE(i < m_W && j < m_H); + ENSURE(0 <= i && i < m_W && 0 <= j && j < m_H); #endif return m_Data[j*m_W + i]; } @@ -113,7 +113,7 @@ class SparseGrid enum { BucketBits = 4, BucketSize = 1 << BucketBits }; - T* GetBucket(size_t i, size_t j) + T* GetBucket(int i, int j) { size_t b = (j >> BucketBits) * m_BW + (i >> BucketBits); if (!m_Data[b]) @@ -129,8 +129,8 @@ public: { ENSURE(m_W && m_H); - m_BW = (m_W + BucketSize-1) >> BucketBits; - m_BH = (m_H + BucketSize-1) >> BucketBits; + m_BW = (u16)((m_W + BucketSize-1) >> BucketBits); + m_BH = (u16)((m_H + BucketSize-1) >> BucketBits); m_Data = new T*[m_BW*m_BH]; memset(m_Data, 0, m_BW*m_BH*sizeof(T*)); @@ -150,18 +150,18 @@ public: memset(m_Data, 0, m_BW*m_BH*sizeof(T*)); } - void set(size_t i, size_t j, const T& value) + void set(int i, int j, const T& value) { #if GRID_BOUNDS_DEBUG - ENSURE(i < m_W && j < m_H); + ENSURE(0 <= i && i < m_W && 0 <= j && j < m_H); #endif GetBucket(i, j)[(j % BucketSize)*BucketSize + (i % BucketSize)] = value; } - T& get(size_t i, size_t j) + T& get(int i, int j) { #if GRID_BOUNDS_DEBUG - ENSURE(i < m_W && j < m_H); + ENSURE(0 <= i && i < m_W && 0 <= j && j < m_H); #endif return GetBucket(i, j)[(j % BucketSize)*BucketSize + (i % BucketSize)]; } diff --git a/source/simulation2/helpers/Render.cpp b/source/simulation2/helpers/Render.cpp index 06221829ef..db63115ebf 100644 --- a/source/simulation2/helpers/Render.cpp +++ b/source/simulation2/helpers/Render.cpp @@ -87,9 +87,9 @@ void SimRender::ConstructCircleOnGround(const CSimContext& context, float x, flo for (size_t i = 0; i <= numPoints; ++i) // use '<=' so it's a closed loop { - float a = i * 2 * (float)M_PI / numPoints; - float px = x + radius * sin(a); - float pz = z + radius * cos(a); + float a = (float)i * 2 * (float)M_PI / (float)numPoints; + float px = x + radius * sinf(a); + float pz = z + radius * cosf(a); float py = std::max(water, cmpTerrain->GetExactGroundLevel(px, pz)) + heightOffset; overlay.m_Coords.push_back(px); overlay.m_Coords.push_back(py); @@ -100,15 +100,15 @@ void SimRender::ConstructCircleOnGround(const CSimContext& context, float x, flo // This method splits up a straight line into a number of line segments each having a length ~= CELL_SIZE static void SplitLine(std::vector >& coords, float x1, float y1, float x2, float y2) { - float length = sqrt(SQR(x1 - x2) + SQR(y1 - y2)); + float length = sqrtf(SQR(x1 - x2) + SQR(y1 - y2)); size_t pieces = ((int)length) / CELL_SIZE; if (pieces > 0) { - float xPieceLength = (x1 - x2) / pieces; - float yPieceLength = (y1 - y2) / pieces; + float xPieceLength = (x1 - x2) / (float)pieces; + float yPieceLength = (y1 - y2) / (float)pieces; for (size_t i = 1; i <= (pieces - 1); ++i) { - coords.push_back(std::make_pair(x1 - (xPieceLength * i), y1 - (yPieceLength * i))); + coords.push_back(std::make_pair(x1 - (xPieceLength * (float)i), y1 - (yPieceLength * (float)i))); } } coords.push_back(std::make_pair(x2, y2)); @@ -131,8 +131,8 @@ void SimRender::ConstructSquareOnGround(const CSimContext& context, float x, flo water = cmpWaterMan->GetExactWaterLevel(x, z); } - float c = cos(a); - float s = sin(a); + float c = cosf(a); + float s = sinf(a); std::vector > coords; diff --git a/source/simulation2/helpers/Spatial.h b/source/simulation2/helpers/Spatial.h index c15ccd933a..e34733c545 100644 --- a/source/simulation2/helpers/Spatial.h +++ b/source/simulation2/helpers/Spatial.h @@ -60,13 +60,13 @@ public: { ENSURE(toMin.X <= toMax.X && toMin.Y <= toMax.Y); - size_t i0 = GetI0(toMin.X); - size_t j0 = GetJ0(toMin.Y); - size_t i1 = GetI1(toMax.X); - size_t j1 = GetJ1(toMax.Y); - for (size_t j = j0; j <= j1; ++j) + u32 i0 = GetI0(toMin.X); + u32 j0 = GetJ0(toMin.Y); + u32 i1 = GetI1(toMax.X); + u32 j1 = GetJ1(toMax.Y); + for (u32 j = j0; j <= j1; ++j) { - for (size_t i = i0; i <= i1; ++i) + for (u32 i = i0; i <= i1; ++i) { std::vector& div = m_Divisions.at(i + j*m_DivisionsW); div.push_back(item); @@ -83,17 +83,17 @@ public: { ENSURE(fromMin.X <= fromMax.X && fromMin.Y <= fromMax.Y); - size_t i0 = GetI0(fromMin.X); - size_t j0 = GetJ0(fromMin.Y); - size_t i1 = GetI1(fromMax.X); - size_t j1 = GetJ1(fromMax.Y); - for (size_t j = j0; j <= j1; ++j) + u32 i0 = GetI0(fromMin.X); + u32 j0 = GetJ0(fromMin.Y); + u32 i1 = GetI1(fromMax.X); + u32 j1 = GetJ1(fromMax.Y); + for (u32 j = j0; j <= j1; ++j) { - for (size_t i = i0; i <= i1; ++i) + for (u32 i = i0; i <= i1; ++i) { std::vector& div = m_Divisions.at(i + j*m_DivisionsW); - for (size_t n = 0; n < div.size(); ++n) + for (u32 n = 0; n < div.size(); ++n) { if (div[n] == item) { @@ -155,13 +155,13 @@ public: ENSURE(posMin.X <= posMax.X && posMin.Y <= posMax.Y); - size_t i0 = GetI0(posMin.X); - size_t j0 = GetJ0(posMin.Y); - size_t i1 = GetI1(posMax.X); - size_t j1 = GetJ1(posMax.Y); - for (size_t j = j0; j <= j1; ++j) + u32 i0 = GetI0(posMin.X); + u32 j0 = GetJ0(posMin.Y); + u32 i1 = GetI1(posMax.X); + u32 j1 = GetJ1(posMax.Y); + for (u32 j = j0; j <= j1; ++j) { - for (size_t i = i0; i <= i1; ++i) + for (u32 i = i0; i <= i1; ++i) { std::vector& div = m_Divisions.at(i + j*m_DivisionsW); ret.insert(ret.end(), div.begin(), div.end()); @@ -192,40 +192,40 @@ private: // (avoiding out-of-bounds accesses, and rounding correctly so that // points precisely between divisions are counted in both): - size_t GetI0(entity_pos_t x) + u32 GetI0(entity_pos_t x) { return Clamp((x / m_DivisionSize).ToInt_RoundToInfinity()-1, 0, (int)m_DivisionsW-1); } - size_t GetJ0(entity_pos_t z) + u32 GetJ0(entity_pos_t z) { return Clamp((z / m_DivisionSize).ToInt_RoundToInfinity()-1, 0, (int)m_DivisionsH-1); } - size_t GetI1(entity_pos_t x) + u32 GetI1(entity_pos_t x) { return Clamp((x / m_DivisionSize).ToInt_RoundToNegInfinity(), 0, (int)m_DivisionsW-1); } - size_t GetJ1(entity_pos_t z) + u32 GetJ1(entity_pos_t z) { return Clamp((z / m_DivisionSize).ToInt_RoundToNegInfinity(), 0, (int)m_DivisionsH-1); } - size_t GetIndex0(CFixedVector2D pos) + u32 GetIndex0(CFixedVector2D pos) { return GetI0(pos.X) + GetJ0(pos.Y)*m_DivisionsW; } - size_t GetIndex1(CFixedVector2D pos) + u32 GetIndex1(CFixedVector2D pos) { return GetI1(pos.X) + GetJ1(pos.Y)*m_DivisionsW; } entity_pos_t m_DivisionSize; std::vector > m_Divisions; - size_t m_DivisionsW; - size_t m_DivisionsH; + u32 m_DivisionsW; + u32 m_DivisionsH; template friend struct SerializeSpatialSubdivision; }; diff --git a/source/simulation2/scripting/EngineScriptConversions.cpp b/source/simulation2/scripting/EngineScriptConversions.cpp index 3e43414b6a..b7cc5124e6 100644 --- a/source/simulation2/scripting/EngineScriptConversions.cpp +++ b/source/simulation2/scripting/EngineScriptConversions.cpp @@ -216,7 +216,7 @@ template<> jsval ScriptInterface::ToJSVal >(JSContext* cx, const Grid< if (!obj) return JSVAL_VOID; - size_t len = val.m_W * val.m_H; + jsuint len = val.m_W * val.m_H; JSObject *darray = js_CreateTypedArray(cx, js::TypedArray::TYPE_UINT16, len); if (!darray) return JSVAL_VOID; diff --git a/source/simulation2/serialization/DebugSerializer.cpp b/source/simulation2/serialization/DebugSerializer.cpp index e85ae715e2..fb06a9214a 100644 --- a/source/simulation2/serialization/DebugSerializer.cpp +++ b/source/simulation2/serialization/DebugSerializer.cpp @@ -39,7 +39,7 @@ // across platforms, we want to convert to a canonical form. // TODO: we just do e+0xx now; ought to handle varying precisions and inf and nan etc too template -std::string canonfloat(T value, size_t prec) +std::string canonfloat(T value, int prec) { std::stringstream str; str << std::setprecision(prec) << value; diff --git a/source/simulation2/system/ComponentTest.h b/source/simulation2/system/ComponentTest.h index 2cbb121c8b..12282a9b7e 100644 --- a/source/simulation2/system/ComponentTest.h +++ b/source/simulation2/system/ComponentTest.h @@ -177,7 +177,12 @@ public: return 50.f; } - virtual uint32_t GetVerticesPerSide() + virtual u16 GetTilesPerSide() + { + return 16; + } + + virtual u16 GetVerticesPerSide() { return 17; } @@ -187,7 +192,7 @@ public: return NULL; } - virtual void MakeDirty(ssize_t UNUSED(i0), ssize_t UNUSED(j0), ssize_t UNUSED(i1), ssize_t UNUSED(j1)) + virtual void MakeDirty(i32 UNUSED(i0), i32 UNUSED(j0), i32 UNUSED(i1), i32 UNUSED(j1)) { } diff --git a/source/sound/SoundGroup.h b/source/sound/SoundGroup.h index 2c7c2212a6..bbdddd4d10 100644 --- a/source/sound/SoundGroup.h +++ b/source/sound/SoundGroup.h @@ -95,7 +95,7 @@ public: void Update(float TimeSinceLastFrame); // Set a flag using a value from eSndGrpFlags - inline void SetFlag(int flag) { m_Flags |= flag; } + inline void SetFlag(int flag) { m_Flags = (unsigned char)(m_Flags | flag); } // Test flag, returns true if flag is set. inline bool TestFlag(int flag) { return (m_Flags & flag) != 0; }