diff --git a/MissionEditor/IsoView.cpp b/MissionEditor/IsoView.cpp index fe24b82..0df11a4 100644 --- a/MissionEditor/IsoView.cpp +++ b/MissionEditor/IsoView.cpp @@ -1511,37 +1511,39 @@ void CIsoView::OnMouseMove(UINT nFlags, CPoint point) } else if ((AD.mode == ACTIONMODE_PLACE || AD.mode == ACTIONMODE_RANDOMTERRAIN) && (nFlags & ~MK_CONTROL) == 0 && AD.type != 7 && (AD.type != 6 || (AD.type == 6 && ((AD.data >= 30 && AD.data <= 33) || AD.data == 2 || AD.data == 3)))) // everything placing but not overlay! { FIELDDATA oldData[32][32]; - INFANTRY infData[SUBPOS_COUNT][32][32]; - int i, e; + //INFANTRY infData[SUBPOS_COUNT][32][32]; //if(AD.type!=1 || Map->GetInfantryCountAt(x+y*Map->GetIsoSize())==0) { - for (i = 0; i < 32; i++) { - for (e = 0; e < 32; e++) { + for (auto i = 0; i < 32; i++) { + for (auto e = 0; e < 32; e++) { oldData[i][e] = *Map->GetFielddataAt(i + x + (e + y) * Map->GetIsoSize()); - int z; - for (z = 0; z < SUBPOS_COUNT; z++) - if (oldData[i][e].infantry[z] > -1) - Map->GetInfantryData(oldData[i][e].infantry[z], &infData[z][i][e]); + //for (auto subPos = 0; subPos < SUBPOS_COUNT; subPos++) { + // if (oldData[i][e].infantry[subPos] > -1) { + // Map->GetInfantryData(oldData[i][e].infantry[subPos], &infData[subPos][i][e]); + // } + //} } } PlaceCurrentObjectAt(x, y); RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW); - for (i = 0; i < 32; i++) { - for (e = 0; e < 32; e++) { + for (auto i = 0; i < 32; i++) { + for (auto e = 0; e < 32; e++) { DWORD dwPos = i + x + (e + y) * Map->GetIsoSize(); FIELDDATA cur_field; cur_field = *Map->GetFielddataAt(dwPos); - if (cur_field.aircraft != oldData[i][e].aircraft) + if (cur_field.aircraft != oldData[i][e].aircraft) { Map->DeleteAircraft(cur_field.aircraft); - int z; - for (z = 0; z < SUBPOS_COUNT; z++) - if (cur_field.infantry[z] != oldData[i][e].infantry[z]) { - Map->DeleteInfantry(cur_field.infantry[z]); + } + + for (auto subPos = 0; subPos < SUBPOS_COUNT; subPos++) { + if (cur_field.infantry[subPos] != oldData[i][e].infantry[subPos]) { + Map->DeleteInfantry(cur_field.infantry[subPos]); } + } if (cur_field.node.index != oldData[i][e].node.index) { CString house; @@ -4838,11 +4840,13 @@ void CIsoView::handleMouseActionManageOverlays(int x, int y) // RedrawWindow(NULL,NULL,RDW_INVALIDATE | RDW_UPDATENOW); } -void CIsoView::PlaceCurrentObjectAt(int x, int y) +DWORD CIsoView::PlaceCurrentObjectAt(int x, int y) { + auto constexpr infinitePos = std::numeric_limits::max(); + auto const expectingPos = x + y * Map->GetIsoSize(); if (AD.mode == ACTIONMODE_RANDOMTERRAIN) { - if (Map->GetTerrainAt(x + y * Map->GetIsoSize()) >= 0) { - return; + if (Map->GetTerrainAt(expectingPos) >= 0) { + return infinitePos; } CString s; @@ -4850,65 +4854,69 @@ void CIsoView::PlaceCurrentObjectAt(int x, int y) int n = rand() * rndterrainsrc.size() / RAND_MAX; // safety checks... - if (n >= rndterrainsrc.size()) n = rndterrainsrc.size() - 1; - if (n < 0) n = 0; + if (n >= rndterrainsrc.size()) { + n = rndterrainsrc.size() - 1; + } + if (n < 0) { + n = 0; + } s = rndterrainsrc[n]; - Map->AddTerrain(s, x + y * Map->GetIsoSize()); + Map->AddTerrain(s, expectingPos); - return; + return expectingPos; } switch (MouseActionType(AD.type)) { case MouseActionType::AddInfantry: { - if (Map->GetInfantryCountAt(x + y * Map->GetIsoSize()) >= SUBPOS_COUNT) { - return; + if (Map->GetInfantryCountAt(expectingPos) >= SUBPOS_COUNT) { + return infinitePos; } - Map->AddInfantry(NULL, -1, AD.data_s, currentOwner, x + y * Map->GetIsoSize()); + Map->AddInfantry(NULL, -1, AD.data_s, currentOwner, expectingPos); //RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW); } break; case MouseActionType::AddStructure: { - int n = Map->GetStructureAt(x + y * Map->GetIsoSize()); + int n = Map->GetStructureAt(expectingPos); if (n >= 0) { STDOBJECTDATA sod; Map->GetStdStructureData(n, &sod); if (strcmp(sod.type, "GAPAVE") != NULL) { //isMoving=FALSE; - return; + return infinitePos; } } - Map->AddStructure(NULL, AD.data_s, currentOwner, x + y * Map->GetIsoSize()); + Map->AddStructure(NULL, AD.data_s, currentOwner, expectingPos); //RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW); } break; case MouseActionType::AddAircraft: { - if (Map->GetAirAt(x + y * Map->GetIsoSize()) >= 0) { - return; + if (Map->GetAirAt(expectingPos) >= 0) { + return infinitePos; } - Map->AddAircraft(NULL, AD.data_s, currentOwner, x + y * Map->GetIsoSize()); + Map->AddAircraft(NULL, AD.data_s, currentOwner, expectingPos); //RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW); } break; case MouseActionType::AddVehicle: { - if (Map->GetUnitAt(x + y * Map->GetIsoSize()) >= 0) { - return; + if (Map->GetUnitAt(expectingPos) >= 0) { + return infinitePos; } - Map->AddUnit(NULL, AD.data_s, currentOwner, x + y * Map->GetIsoSize()); + Map->AddUnit(NULL, AD.data_s, currentOwner, expectingPos); //RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW); } break; case MouseActionType::AddTerrain: { - if (Map->GetTerrainAt(x + y * Map->GetIsoSize()) >= 0) { - return; + if (Map->GetTerrainAt(expectingPos) >= 0) { + return infinitePos; } - Map->AddTerrain(AD.data_s, x + y * Map->GetIsoSize()); + Map->AddTerrain(AD.data_s, expectingPos); //RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW); } break; @@ -4919,7 +4927,7 @@ void CIsoView::PlaceCurrentObjectAt(int x, int y) // set owner! BOOL bchanged = FALSE; - int t = Map->GetStructureAt(x + y * Map->GetIsoSize()); + int t = Map->GetStructureAt(expectingPos); if (t >= 0) { STRUCTURE structure; auto const id = Map->GetStructureData(t, &structure); @@ -4928,7 +4936,7 @@ void CIsoView::PlaceCurrentObjectAt(int x, int y) Map->AddStructure(&structure, nullptr, nullptr, 0, std::move(id)); bchanged = TRUE; } - t = Map->GetUnitAt(x + y * Map->GetIsoSize()); + t = Map->GetUnitAt(expectingPos); if (t >= 0) { UNIT unit; auto const id = Map->GetUnitData(t, &unit); @@ -4937,7 +4945,7 @@ void CIsoView::PlaceCurrentObjectAt(int x, int y) Map->AddUnit(&unit, nullptr, nullptr, 0, std::move(id)); bchanged = TRUE; } - t = Map->GetAirAt(x + y * Map->GetIsoSize()); + t = Map->GetAirAt(expectingPos); if (t >= 0) { AIRCRAFT aircraft; auto const id = Map->GetAircraftData(t, &aircraft); @@ -4948,7 +4956,7 @@ void CIsoView::PlaceCurrentObjectAt(int x, int y) } int z; for (z = 0; z < SUBPOS_COUNT; z++) { - t = Map->GetInfantryAt(x + y * Map->GetIsoSize(), z); + t = Map->GetInfantryAt(expectingPos, z); if (t >= 0) { INFANTRY infantry; Map->GetInfantryData(t, &infantry); @@ -4962,11 +4970,12 @@ void CIsoView::PlaceCurrentObjectAt(int x, int y) if (bchanged) { //RedrawWindow(NULL,NULL,RDW_INVALIDATE | RDW_UPDATENOW); } + return infinitePos; } break; #ifdef SMUDGE_SUPP case MouseActionType::AddSmudge: { - if (Map->GetFielddataAt(x + y * Map->GetIsoSize())->smudge >= 0) { - return; + if (Map->GetFielddataAt(expectingPos)->smudge >= 0) { + return infinitePos; } SMUDGE s; @@ -4982,6 +4991,7 @@ void CIsoView::PlaceCurrentObjectAt(int x, int y) default: break; } + return expectingPos; } void CIsoView::OnTimer(UINT_PTR nIDEvent) diff --git a/MissionEditor/IsoView.h b/MissionEditor/IsoView.h index 5bb60b8..c598b6c 100644 --- a/MissionEditor/IsoView.h +++ b/MissionEditor/IsoView.h @@ -164,7 +164,8 @@ public: void AutoLevel(); void FillArea(DWORD dwX, DWORD dwY, DWORD dwID, BYTE bSubTile); BOOL m_NoMove; - void PlaceCurrentObjectAt(int x, int y); + // returns position value + DWORD PlaceCurrentObjectAt(int x, int y); void PlaceTile(const int x, const int y, const UINT nMouseFlags); void ShowAllTileSets(); void HideTileSet(DWORD dwTileSet); diff --git a/MissionEditor/MapData.cpp b/MissionEditor/MapData.cpp index 055b2a7..363b289 100644 --- a/MissionEditor/MapData.cpp +++ b/MissionEditor/MapData.cpp @@ -2182,6 +2182,9 @@ BOOL CMapData::AddNode(NODE* lpNode, WORD dwPos) BOOL CMapData::AddInfantry(INFANTRY* lpInfantry, int suggestedIndex, LPCTSTR lpType, LPCTSTR lpHouse, DWORD dwPos) { + if (dwPos >= fielddata_size) { + return FALSE; + } INFANTRY infantry; if (lpInfantry != NULL) { @@ -2219,35 +2222,38 @@ BOOL CMapData::AddInfantry(INFANTRY* lpInfantry, int suggestedIndex, LPCTSTR lpT if (infantry.pos == "-1") { int subpos = -1; - int i; if (GetInfantryCountAt(dwPos) == 0) { subpos = 0; } else { + // TODO: do not take pos 0, but always use 1~3 +#if 0 int oldInf = GetInfantryAt(dwPos, 0); - if (oldInf > -1) { - INFANTRY inf; - GetInfantryData(oldInf, &inf); + if (oldInf >= 0) { + INFANTRY infExisitingData; + GetInfantryData(oldInf, &infExisitingData); - if (inf.pos == "0") - for (i = 1; i < SUBPOS_COUNT; i++) { - if (GetInfantryAt(dwPos, i) == -1) { + if (infExisitingData.pos == "0") + for (auto i = 1; i < SUBPOS_COUNT; i++) { + // not taken, move existing infantry to the sub position + if (GetInfantryAt(dwPos, i) < 0) { //subpos=i+1; char c[50]; itoa(i, c, 10); - inf.pos = c; + infExisitingData.pos = c; DeleteInfantry(oldInf); - AddInfantry(&inf, oldInf); + AddInfantry(&infExisitingData, oldInf); break; } } } +#endif // if(GetInfantryAt(dwPos, 0)==oldInf) return FALSE; - for (i = 0; i < SUBPOS_COUNT; i++) { + for (auto i = 0; i < SUBPOS_COUNT; i++) { if (GetInfantryAt(dwPos, i) == -1) { subpos = i + 1; break; @@ -2258,10 +2264,8 @@ BOOL CMapData::AddInfantry(INFANTRY* lpInfantry, int suggestedIndex, LPCTSTR lpT if (subpos < 0) { return FALSE; } - char c[50]; - itoa(subpos, c, 10); - infantry.pos = c; + infantry.pos.Format("%d", subpos); } @@ -2305,9 +2309,7 @@ BOOL CMapData::AddInfantry(INFANTRY* lpInfantry, int suggestedIndex, LPCTSTR lpT // reuse slot if (m_infantry[suggestedIndex].deleted) { m_infantry[suggestedIndex] = infantry; - if (dwPos < fielddata_size) { - fielddata[dwPos].infantry[sp] = suggestedIndex; - } + fielddata[dwPos].infantry[sp] = suggestedIndex; bFound = TRUE; } @@ -2318,18 +2320,15 @@ BOOL CMapData::AddInfantry(INFANTRY* lpInfantry, int suggestedIndex, LPCTSTR lpT // yep, found one, replace it if (m_infantry[i].deleted) { m_infantry[i] = infantry; - if (dwPos < fielddata_size) { - fielddata[dwPos].infantry[sp] = i; - } + fielddata[dwPos].infantry[sp] = i; bFound = TRUE; break; } } - + } + if (!bFound) { m_infantry.push_back(infantry); - if (dwPos < fielddata_size) { - fielddata[dwPos].infantry[sp] = m_infantry.size() - 1; - } + fielddata[dwPos].infantry[sp] = m_infantry.size() - 1; } return TRUE; diff --git a/MissionEditor/UserScriptsDlg.cpp b/MissionEditor/UserScriptsDlg.cpp index 2472402..d4b4bc9 100644 --- a/MissionEditor/UserScriptsDlg.cpp +++ b/MissionEditor/UserScriptsDlg.cpp @@ -2277,7 +2277,9 @@ void CUserScriptsDlg::OnOK() if (index >= 0 && index < Map->GetInfantryCount()) { INFANTRY id; Map->GetInfantryData(index, &id); - if (id.deleted == 0) deleted = "0"; + if (id.deleted == 0) { + deleted = "0"; + } } variables[params[0]] = deleted;