Add buildPoints to the structures currentBuildPts, due to construction work by the droid Also can deconstruct (demolish) a building if passed negative buildpoints
| 910 | /// Add buildPoints to the structures currentBuildPts, due to construction work by the droid |
| 911 | /// Also can deconstruct (demolish) a building if passed negative buildpoints |
| 912 | void structureBuild(STRUCTURE *psStruct, DROID *psDroid, int buildPoints, int buildRate) |
| 913 | { |
| 914 | bool checkResearchButton = psStruct->status == SS_BUILT; // We probably just started demolishing, if this is true. |
| 915 | int prevResearchState = 0; |
| 916 | if (checkResearchButton) |
| 917 | { |
| 918 | prevResearchState = intGetResearchState(); |
| 919 | } |
| 920 | |
| 921 | if (psDroid && !aiCheckAlliances(psStruct->player, psDroid->player)) |
| 922 | { |
| 923 | // Enemy structure |
| 924 | return; |
| 925 | } |
| 926 | else if (psStruct->pStructureType->type != REF_FACTORY_MODULE) |
| 927 | { |
| 928 | for (unsigned player = 0; player < MAX_PLAYERS; player++) |
| 929 | { |
| 930 | for (const DROID *psCurr : apsDroidLists[player]) |
| 931 | { |
| 932 | // An enemy droid is blocking it |
| 933 | if ((STRUCTURE *) orderStateObj(psCurr, DORDER_BUILD) == psStruct |
| 934 | && !aiCheckAlliances(psStruct->player, psCurr->player)) |
| 935 | { |
| 936 | return; |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | psStruct->buildRate += buildRate; // buildRate = buildPoints/GAME_UPDATES_PER_SEC, but might be rounded up or down each tick, so can't use buildPoints to get a stable number. |
| 942 | if (psStruct->currentBuildPts <= 0 && buildPoints > 0) |
| 943 | { |
| 944 | // Just starting to build structure, need power for it. |
| 945 | bool haveEnoughPower = requestPowerFor(psStruct, structPowerToBuildOrAddNextModule(psStruct)); |
| 946 | if (!haveEnoughPower) |
| 947 | { |
| 948 | buildPoints = 0; // No power to build. |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | int newBuildPoints = psStruct->currentBuildPts + buildPoints; |
| 953 | ASSERT(newBuildPoints <= 1 + 3 * (int)structureBuildPointsToCompletion(*psStruct), "unsigned int underflow?"); |
| 954 | CLIP(newBuildPoints, 0, structureBuildPointsToCompletion(*psStruct)); |
| 955 | |
| 956 | if (psStruct->currentBuildPts > 0 && newBuildPoints <= 0) |
| 957 | { |
| 958 | // Demolished structure, return some power. |
| 959 | addPower(psStruct->player, structureTotalReturn(psStruct)); |
| 960 | } |
| 961 | |
| 962 | int deltaBody = quantiseFraction(9 * psStruct->structureBody(), 10 * structureBuildPointsToCompletion(*psStruct), newBuildPoints, psStruct->currentBuildPts); |
| 963 | psStruct->currentBuildPts = newBuildPoints; |
| 964 | psStruct->body = std::max<int>(psStruct->body + deltaBody, 1); |
| 965 | |
| 966 | //check if structure is built |
| 967 | if (buildPoints > 0 && psStruct->currentBuildPts >= structureBuildPointsToCompletion(*psStruct)) |
| 968 | { |
| 969 | buildingComplete(psStruct); |
no test coverage detected