/** ** Find the next idle worker, select it, and center on it */ void UiFindIdleWorker() { if (ThisPlayer->FreeWorkers.empty()) { return; } CUnit *unit = ThisPlayer->FreeWorkers[0]; if (LastIdleWorker) { const std::vector<CUnit *> &freeWorkers = ThisPlayer->FreeWorkers; std::vector<CUnit *>::const_iterator it = std::find(freeWorkers.begin(), freeWorkers.end(), LastIdleWorker); if (it != ThisPlayer->FreeWorkers.end()) { if (*it != ThisPlayer->FreeWorkers.back()) { unit = *(++it); } } } if (unit != NULL) { LastIdleWorker = unit; SelectSingleUnit(*unit); UI.StatusLine.Clear(); UI.StatusLine.ClearCosts(); CurrentButtonLevel = 0; PlayUnitSound(*Selected[0], VoiceSelected); SelectionChanged(); UI.SelectedViewport->Center(unit->GetMapPixelPosCenter()); } }
static int CclSelectSingleUnit(lua_State *l) { const int nargs = lua_gettop(l); Assert(nargs == 1); lua_pushvalue(l, 1); CUnit *unit = CclGetUnit(l); lua_pop(l, 1); SelectSingleUnit(*unit); SelectionChanged(); return 0; }
/** ** Find the next idle worker, select it, and center on it */ static void UiFindIdleWorker() { if (ThisPlayer->FreeWorkers.empty()) { return; } CUnit *unit = ThisPlayer->FreeWorkers[0]; if (unit != NULL) { SelectSingleUnit(*unit); UI.StatusLine.Clear(); ClearCosts(); CurrentButtonLevel = 0; PlayUnitSound(*Selected[0], VoiceSelected); SelectionChanged(); UI.SelectedViewport->Center(unit->GetMapPixelPosCenter()); } }
/** ** Select units in a rectangle. ** Proceed in order in none found: ** @li select local player mobile units ** @li select one local player static unit (random) ** @li select one neutral unit (critter, mine...) ** @li select one enemy unit (random) ** ** @param corner_topleft, start of selection rectangle ** @param corner_bottomright end of selection rectangle ** ** @return the number of units found. */ int SelectUnitsInRectangle(const PixelPos &corner_topleft, const PixelPos &corner_bottomright) { const Vec2i t0 = Map.MapPixelPosToTilePos(corner_topleft); const Vec2i t1 = Map.MapPixelPosToTilePos(corner_bottomright); const Vec2i range(2, 2); std::vector<CUnit *> table; Select(t0 - range, t1 + range, table); SelectSpritesInsideRectangle(corner_topleft, corner_bottomright, table); // 1) search for the player units selectable with rectangle if (SelectOrganicUnitsInTable(table)) { const int size = static_cast<int>(table.size()); ChangeSelectedUnits(&table[0], size); return size; } // 2) If no unit found, try a player's unit not selectable by rectangle for (size_t i = 0; i != table.size(); ++i) { CUnit &unit = *table[i]; if (!CanSelectMultipleUnits(*unit.Player)) { continue; } // FIXME: Can we get this? if (!unit.Removed && unit.IsAlive()) { SelectSingleUnit(unit); return 1; } } // 3) If no unit found, try a resource or a neutral critter for (size_t i = 0; i != table.size(); ++i) { CUnit &unit = *table[i]; // Unit visible FIXME: write function UnitSelectable if (!unit.IsVisibleInViewport(*UI.SelectedViewport)) { continue; } const CUnitType &type = *unit.Type; // Buildings are visible but not selectable if (type.Building && !unit.IsVisibleOnMap(*ThisPlayer)) { continue; } if ((type.GivesResource && !unit.Removed)) { // no built resources. SelectSingleUnit(unit); return 1; } } // 4) If no unit found, select an enemy unit (first found) for (size_t i = 0; i != table.size(); ++i) { CUnit &unit = *table[i]; // Unit visible FIXME: write function UnitSelectable if (!unit.IsVisibleInViewport(*UI.SelectedViewport)) { continue; } // Buildings are visible but not selectable if (unit.Type->Building && !unit.IsVisibleOnMap(*ThisPlayer)) { continue; } if (unit.IsAliveOnMap()) { SelectSingleUnit(unit); return 1; } } return 0; }