MCPcopy Create free account
hub / github.com/OpenApoc/OpenApoc / killStrandedUnits

Method killStrandedUnits

game/state/battle/battle.cpp:1902–1966  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1900}
1901
1902int Battle::killStrandedUnits(GameState &state, StateRef<Organisation> org, bool preview)
1903{
1904 int countKilled = 0;
1905
1906 for (auto &u : units)
1907 {
1908 if (u.second->owner != org || u.second->isDead())
1909 {
1910 continue;
1911 }
1912 // Find closest exit
1913 float distanceToExit = FLT_MAX;
1914 for (auto &e : exits)
1915 {
1916 float distance = glm::length(u.second->position - (Vec3<float>)e);
1917 if (distance < distanceToExit)
1918 {
1919 distanceToExit = distance;
1920 }
1921 }
1922 // Find closest enemy from org that sees us
1923 float distanceToEnemy = FLT_MAX;
1924 for (auto &owner : participants)
1925 {
1926 if (owner == org || owner->isRelatedTo(org) != Organisation::Relation::Hostile)
1927 {
1928 continue;
1929 }
1930 for (auto &spotted : visibleUnits.at(owner))
1931 {
1932 if (spotted.id != u.first)
1933 {
1934 continue;
1935 }
1936 // Org sees this unit
1937 // Look for closest unit from this org
1938 for (auto &e : units)
1939 {
1940 if (e.second->owner != owner || e.second->isDead())
1941 {
1942 continue;
1943 }
1944 float distance = glm::length(u.second->position - e.second->position);
1945 if (distance < distanceToEnemy)
1946 {
1947 distanceToEnemy = distance;
1948 }
1949 }
1950 // No need to look further, we already processed this org
1951 break;
1952 }
1953 }
1954 // Exit must be three times closer than enemy for escape to be possible
1955 if (distanceToEnemy / 3.0f < distanceToExit)
1956 {
1957 countKilled++;
1958 if (!preview)
1959 {

Callers 1

eventOccurredMethod · 0.80

Calls 3

isRelatedToMethod · 0.80
isDeadMethod · 0.45
dieMethod · 0.45

Tested by

no test coverage detected