]> git.pond.sub.org Git - empserver/log
empserver
3 years agoarm: Rework "cannot carry nukes" test for robustness
Markus Armbruster [Fri, 11 May 2018 16:57:47 +0000 (18:57 +0200)]
arm: Rework "cannot carry nukes" test for robustness

We reject satellites, ABMs, anti-ship missiles, and SAMs.  That's
enumerating badness.  More robust replacement: accept only bomber,
tactical, cargo, except for anti-ship missiles.

Throw in PLN_LAUNCHED sanity checking while there.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoef_verify: Reject invalid plane flag combinations
Markus Armbruster [Mon, 4 Jan 2021 10:09:38 +0000 (11:09 +0100)]
ef_verify: Reject invalid plane flag combinations

Any plane may have capabilities VTOL, helo, light.

Capability missile requires VTOL.

Anti-ballistic missiles have capabilities missile, SDI.

Anti-satellite missiles have capabilities missile, satellite.

Surface-to-air missiles have capabilities missile, intercept.

Anti-ship missiles have capabilities missile, marine, and may have
tactical.

Surface-to-surface missiles have capability missile, and may have
tactical.

Satellites have capability satellite, and may have spy, image.

Ordinary planes may have capabilities bomber, tactical, intercept,
cargo, spy, image, ASW, para, escort, mine, sweep.  Capability para
requires cargo; see para().

Only "missile requires VTOL" is enforced.  Enforce the rest.

Excluding P_O when asking for P_N is now redundant.  Drop that from
msl_abm_intercept().

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agolaunch interception: Drop support for ABM, a-sat consuming shells
Markus Armbruster [Mon, 4 Jan 2021 09:58:31 +0000 (10:58 +0100)]
launch interception: Drop support for ABM, a-sat consuming shells

Their impact on the target does not depend on shell load (it sometimes
did for a-sats until commit cf960a573 "Make anti-sat launch consistent
with interception", v4.3.23).  The shell use is logistical busy-work,
and economically irrelevant.  Remove it.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoconfig: Change asat not to consume shells
Markus Armbruster [Mon, 4 Jan 2021 09:42:42 +0000 (10:42 +0100)]
config: Change asat not to consume shells

The asat's impact on the target does not depend on its shell load (it
sometimes did until commit cf960a573 "Make anti-sat launch consistent
with interception", v4.3.23).  The shell use is logistical busy-work,
and economically irrelevant.  Remove it.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agomission: Don't permit marine missiles on support missions
Markus Armbruster [Mon, 4 Jan 2021 09:29:29 +0000 (10:29 +0100)]
mission: Don't permit marine missiles on support missions

Marine missiles can't actually support, see perform_mission().  Make
the mission command reject them.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agomission: Fix an error message pasto going back to Chainsaw 3
Markus Armbruster [Mon, 4 Jan 2021 09:02:26 +0000 (10:02 +0100)]
mission: Fix an error message pasto going back to Chainsaw 3

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agomission: Don't permit SAMs on escort missions
Markus Armbruster [Mon, 4 Jan 2021 08:58:52 +0000 (09:58 +0100)]
mission: Don't permit SAMs on escort missions

The mission code doesn't treat SAMs specially: they take off, fly out,
maybe fight, fly home, and land.  Landing triggers the oops in
pln_put1().

Letting SAMs escort makes no sense.  Fix the mission command to reject
them.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoinfo/Plane-types: Document SAM, ABM and a-sat missile abilities
Markus Armbruster [Mon, 4 Jan 2021 08:36:55 +0000 (09:36 +0100)]
info/Plane-types: Document SAM, ABM and a-sat missile abilities

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoinfo: Clean up references to long-gone options
Markus Armbruster [Mon, 4 Jan 2021 07:52:10 +0000 (08:52 +0100)]
info: Clean up references to long-gone options

Option DEFENSE_INFRA was replaced in commit 4d1dd6fa6, v4.3.6.  Point
to the replacement instead.

Options SHIPCHOPPERS and XLIGHT were made non-optional in Empire 2.
Don't mention them.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoretreat: Drop unused parameter @code
Markus Armbruster [Sun, 3 Jan 2021 09:55:55 +0000 (10:55 +0100)]
retreat: Drop unused parameter @code

Unused since commit beedf8dce "retreat: Rewrite automatic retreat code
to fix its many bugs", v4.3.33.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agolwp: Include signal.h instead of ucontext.h where possible
Markus Armbruster [Sun, 3 Jan 2021 07:37:52 +0000 (08:37 +0100)]
lwp: Include signal.h instead of ucontext.h where possible

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agosubs/attsub: Drop a stale comment
Markus Armbruster [Sat, 2 Jan 2021 07:37:37 +0000 (08:37 +0100)]
subs/attsub: Drop a stale comment

Stale since commit 6ac9ad66e (v4.3.20) silently dropped dead code for
supply of military.  Dead since commit 689f435af (v4.2.14) dropped
option GRAB_THINGS.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoassault attack board lboard paradrop: Fix defending land unit supply
Markus Armbruster [Sat, 2 Jan 2021 07:06:57 +0000 (08:06 +0100)]
assault attack board lboard paradrop: Fix defending land unit supply

get_dlist() collects defending land units in a list, and resupplies
them.  Bug: it uses a local copy instead of the one in the list.  When
att_fight() writes back the list, the commodities supplied get wiped
out, triggering a seqno oops.  Broken in commit 62b9399cd "subs:
Factor lnd_insque() out of lnd_sel(), ask_olist(), ...", v4.3.33.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoUpdate copyright notice
Markus Armbruster [Fri, 1 Jan 2021 08:38:16 +0000 (09:38 +0100)]
Update copyright notice

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoDrop macros AGREE_FREE, AGREE_PROPOSED, AGREE_SIGNED
Markus Armbruster [Thu, 31 Dec 2020 04:45:27 +0000 (05:45 +0100)]
Drop macros AGREE_FREE, AGREE_PROPOSED, AGREE_SIGNED

Commit 555de3100 (v4.3.0) added these macros so xdump tables loan and
treaty can share an agreement_statuses table.  Treaties are gone since
commit a109de948 (v4.3.33).  Drop the macros.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agodoc/coding: Improve section "Portability" a bit
Markus Armbruster [Wed, 30 Dec 2020 17:01:25 +0000 (18:01 +0100)]
doc/coding: Improve section "Portability" a bit

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoinclude: Move sarg.c stuff to nsc.h to avoid incomplete enum
Markus Armbruster [Wed, 30 Dec 2020 16:04:12 +0000 (17:04 +0100)]
include: Move sarg.c stuff to nsc.h to avoid incomplete enum

Portability issue: prototypes.h uses enum ns_seltype, defined in
nsc.h.  GCC is fine with that; it supports incomplete enum types as an
extension.  Broken in commit 157ae8ec2 "Clean up superfluous include
of nsc.h in prototypes.h", v4.3.27.  Moving sarg.c stuff to nsc.h
fixes it.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agocommon/pathfind: A more portable DPRINTF()
Markus Armbruster [Wed, 30 Dec 2020 12:40:47 +0000 (13:40 +0100)]
common/pathfind: A more portable DPRINTF()

C99 requires at least one argument for the "..." in a variadic macro.
GCC and Clang don't care, but warn with -pedantic.  Solaris cc warns.

The warning is easy to avoid, so do it.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoconfigure: Move terminfo check next to readline check
Markus Armbruster [Tue, 29 Dec 2020 08:56:59 +0000 (09:56 +0100)]
configure: Move terminfo check next to readline check

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agom4/my_lib_readline: Fix check for additional libraries
Markus Armbruster [Tue, 29 Dec 2020 04:56:12 +0000 (05:56 +0100)]
m4/my_lib_readline: Fix check for additional libraries

MY_LIB_READLINE tries each readline-compatible library with a sequence
of additional libraries, starting with none.  If the check with none
fails, all the others checks reuse the cached result, and also fail.
Broken in commit 1cbda2c7d "client: Rewrite readline configuration",
v4.4.0.

Avoid the unwanted chaching.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoMake: Use SHELL to run test scripts, since /bin/sh may suck
Markus Armbruster [Mon, 28 Dec 2020 10:38:20 +0000 (11:38 +0100)]
Make: Use SHELL to run test scripts, since /bin/sh may suck

The test scripts need a POSIX-compliant Bourne shell.  /bin/sh isn't
on some systems, such as Solaris.  Run them with $(SHELL).

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoMake: Get SHELL from Autoconf
Markus Armbruster [Mon, 28 Dec 2020 10:30:59 +0000 (11:30 +0100)]
Make: Get SHELL from Autoconf

The default SHELL isn't POSIX on some systems, such as Solaris.  Use
the one Autoconf chooses instead of the default.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoMake: Slighly less verbose "make check" output
Markus Armbruster [Mon, 28 Dec 2020 10:15:46 +0000 (11:15 +0100)]
Make: Slighly less verbose "make check" output

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoinfo/Empire4.3: Work around "nroff: Out of temp file space"
Markus Armbruster [Mon, 28 Dec 2020 09:21:54 +0000 (10:21 +0100)]
info/Empire4.3: Work around "nroff: Out of temp file space"

Solaris 10's nroff can't deal with the lengthy .NF ... .FI: it
complains, writes truncated output, and terminates
successfully (*boggle*).  Split the .NF ... .FI in two.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoconfigure: Test GCC-specific compiler flags only when using GCC
Markus Armbruster [Mon, 28 Dec 2020 04:07:50 +0000 (05:07 +0100)]
configure: Test GCC-specific compiler flags only when using GCC

Commit 23373d01d "configure: Test compiler flags" (v4.4.0) made
configure test whether compiler flags work, to let us use flags that
work only for some compilers.

Unfortunately, the Sun C compiler accepts invalid options with a
warning, breaking the test.  Observed on Solaris 10.

Fix by testing the compiler flags only when AC_PROG_CC detected GCC.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agobomb: Pass NULL instead of 0 to pln_damage()
Markus Armbruster [Sun, 27 Dec 2020 11:47:20 +0000 (12:47 +0100)]
bomb: Pass NULL instead of 0 to pln_damage()

Neglected in commit 820d755e5 "subs: Change pln_damage()'s parameter
noisy to string prefix", v4.3.33.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agobomb launch interdiction: Fix crash on bombs missing target
Markus Armbruster [Sun, 27 Dec 2020 11:43:19 +0000 (12:43 +0100)]
bomb launch interdiction: Fix crash on bombs missing target

plane damage() prints @noisy with snprintf() even when it's null.  It
doesn't actually use the output then.  Some systems (GNU, Windows)
deal gracefully with printing null strings, others crash.  @noisy is
null when bombers or missiles miss and do collateral damage.  Affects
bomb, launch, and interdiction missions.  Broken in commit 820d755e5
"subs: Change pln_damage()'s parameter noisy to string prefix",
v4.3.33.

Fix by guarding the snprintf().

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoClarify build requirements
Markus Armbruster [Mon, 28 Dec 2020 09:01:44 +0000 (10:01 +0100)]
Clarify build requirements

Document the need for the XSI Extension.  Don't list curses as
required, it's actually optional.  Correct advice for Solaris.  Add
advice for AIX.  Drop references to C89; we actually require C99, and
POSIX.1-2001 provides it.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoFix build for systems that don't provide POSIX.1-2001 by default
Markus Armbruster [Sun, 27 Dec 2020 11:40:05 +0000 (12:40 +0100)]
Fix build for systems that don't provide POSIX.1-2001 by default

We require POSIX.1-2001.  Some systems provide it only with feature
test macro _POSIX_C_SOURCE defined to 200112L.  Since we don't define
it, the build fails there.  Observed on Solaris 10.

We actually require the XSI extension.  The GNU C Library provides it
by default.  With _POSIX_C_SOURCE 200112L, however, you have to define
feature test macro _XOPEN_SOURCE to get it.  But then _POSIX_C_SOURCE
is redundant.

Make configure put #define _XOPEN_SOURCE 600 into config.h.  Drop the
two existing #define _XOPEN_SOURCE 500 in .c files.

Now Solaris cc refuses to play ball unless switched to C99.  Replace
AC_PROG_CC by AC_PROG_CC_STDC to mollify it.

Unfortunately, use of _XOPEN_SOURCE exposes bugs in AIX libc and old
versions of GNU libc:

* AIX defines struct in6_addr's member s6_addr as a macro expanding
  into the actual member.  Without _ALL_SOURCE (the default), the
  expansion is wrong and doesn't compile.  Observed with AIX V7.2.

* GNU lib's IN6_IS_ADDR_V4MAPPED() is unusable without
  _DEFAULT_SOURCE (default) or _GNU_SOURCE.  Observed with Debian 8.
  Tracked at <https://sourceware.org/bugzilla/show_bug.cgi?id=16421>,
  fixed in version 2.25.

Affects just sockaddr_ntop()'s special case from commit 372cdb136 "Use
IPv4 format for IPv4-mapped addresses", v4.3.31.  Disable the special
case and use IPv6 format on such systems.  This is a very minor
usability regression.  Could be avoided, I guess, but it's not worth
the trouble.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoclient: Drop use of getpass()
Markus Armbruster [Sun, 27 Dec 2020 10:57:54 +0000 (11:57 +0100)]
client: Drop use of getpass()

getpass() is traditional Unix, but has been withdrawn from POSIX.  We
still use it when it's available, and fall back to portable code only
when it's not.

The portable code behaves differently: it reads stdin instead of
/dev/tty, and in noncanonical mode.

Simplify things: always use the replacement.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoclient: Make get_password() not echo with POSIX && !HAVE_GETPASS
Markus Armbruster [Sun, 27 Dec 2020 10:48:52 +0000 (11:48 +0100)]
client: Make get_password() not echo with POSIX && !HAVE_GETPASS

getpass() is traditional Unix, but has been withdrawn from POSIX.  We
provide a replacement in case it's missing.  The one for Windows
suppresses echo, like getpass() does.  Implement that for POSIX.  The
other differences to getpass() remain.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoclient: Factor set_echo_if_tty() out of get_password()
Markus Armbruster [Sun, 27 Dec 2020 10:32:48 +0000 (11:32 +0100)]
client: Factor set_echo_if_tty() out of get_password()

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoclient: Move get_password() from login.c to getpass.c
Markus Armbruster [Sun, 27 Dec 2020 09:06:08 +0000 (10:06 +0100)]
client: Move get_password() from login.c to getpass.c

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoclient: New get_password()
Markus Armbruster [Sun, 27 Dec 2020 09:06:08 +0000 (10:06 +0100)]
client: New get_password()

I'm going to drop use of getpass().  As a first step, weaken the bond
by hiding it in a separate function.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoclient: Reorder misc.h, and point to the .c
Markus Armbruster [Sun, 27 Dec 2020 08:55:45 +0000 (09:55 +0100)]
client: Reorder misc.h, and point to the .c

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agolwp: Simplify lwpSigWait() interface
Markus Armbruster [Sun, 27 Dec 2020 08:42:55 +0000 (09:42 +0100)]
lwp: Simplify lwpSigWait() interface

lwpSigWait() was designed to resemble sigwait().  It doesn't anymore.
Drop the awkward argument, and use the return value instead.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agolwp: Rename two variables for clarity
Markus Armbruster [Sun, 27 Dec 2020 08:41:41 +0000 (09:41 +0100)]
lwp: Rename two variables for clarity

Rename LwpSigCaught to LwpPendingSig, and lwpCatchAwaitedSig() to
lwpHandleAwaitedSig().

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agolwp: Rewrite signal wait code for portability and safety
Markus Armbruster [Sun, 27 Dec 2020 08:36:54 +0000 (09:36 +0100)]
lwp: Rewrite signal wait code for portability and safety

LWP's use of sigset_t is problematic.

To iterate over a sigset_t, it uses NSIG, which is not portable: BSD
and System V provide it, but it's not POSIX.

To record signals caught, it updates a sigset_t variable from a signal
handler.  The variable isn't volatile, because we'd have to cast away
volatile for sigaddset().

Replace sigset_t by an array of signal numbers terminated with 0.

Since lwpInitSigWait() needs to store the signal set for
lwpCatchAwaitedSig() anyway, there is no need to pass it to
lwpSigWait().  Drop its parameter.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agolwp: Fix signal wait screwup for multiple different signals
Markus Armbruster [Sun, 27 Dec 2020 08:30:00 +0000 (09:30 +0100)]
lwp: Fix signal wait screwup for multiple different signals

lwpSigWait() clears LwpSigCheck even when signals remain in
LwpSigCaught.  The next empth_wait_for_signal() will then wait until
another one gets caught.  Broken in commit fe2de3d74, v4.3.10.

Fix by clearing it only when LwpSigCaught is empty.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agolwp: Fix handling of sigismember() failure
Markus Armbruster [Sun, 27 Dec 2020 07:18:27 +0000 (08:18 +0100)]
lwp: Fix handling of sigismember() failure

sigismember() fails when passed an invalid or unsupported signal
number.  lwpInitSigWait() and lwpGetSig() treat sigismember() failure
like "is a member".  lwpInitSigWait() will then sigaction()
unsuccessfully.  Harmless.  lwpGetSig() returns the bad signal number
when it's greater than any caught signal's number.  The bad signal
number then gets returned to main(), which shuts down the server.

Fix by treating failure like "is not a member".

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agolwp: Fix unsafe update of LwpSigCaught
Markus Armbruster [Sun, 27 Dec 2020 07:07:24 +0000 (08:07 +0100)]
lwp: Fix unsafe update of LwpSigCaught

lwpCatchAwaitedSig() is not reentrant.  lwpInitSigWait() attempts to
protect it by setting an appropriate signal mask, but screws up.  This
could conceivably lose signals.  Messed up when it got added in commit
7183516d9, v4.3.6.  Fix it.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agolwp: Drop inappropriate oops in lwpSigWait()
Markus Armbruster [Sun, 27 Dec 2020 07:03:25 +0000 (08:03 +0100)]
lwp: Drop inappropriate oops in lwpSigWait()

lwpSigWait()'s contract specifies failure, which means oopsing is
wrong.  Harmless, as its only caller empth_wait_for_signal() oopses on
failure.  Drop it anyway.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoinfo: Use S_ISREG() instead of S_IFREG for readability
Markus Armbruster [Sun, 27 Dec 2020 06:39:46 +0000 (07:39 +0100)]
info: Use S_ISREG() instead of S_IFREG for readability

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoinfo: Add #include <strings.h> for portability
Markus Armbruster [Sun, 27 Dec 2020 06:30:41 +0000 (07:30 +0100)]
info: Add #include <strings.h> for portability

We get strncasecmp() from <string.h>.  This is a BSDism; POSIX
provides it in <strings.h>.  Missed in commit 8fdd0259f2d, v4.2.19.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoMake: Fix build with tool chains that require depcomp
Markus Armbruster [Sat, 26 Dec 2020 08:08:37 +0000 (09:08 +0100)]
Make: Fix build with tool chains that require depcomp

Commit fad8e7f7b "Move auxiliary build tools to build-aux/" moved
depcomp, but neglected to adjust Make.mk.  This broke the build with
tool chains that actually use depcomp.  Modern GCC and Clang don't.
Fix the obvious way.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agobuild-aux/git-version-gen: Refresh from Gnulib commit 4f78c231f4
Markus Armbruster [Sat, 26 Dec 2020 07:34:33 +0000 (08:34 +0100)]
build-aux/git-version-gen: Refresh from Gnulib commit 4f78c231f4

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agobuild-aux INSTALL: Refresh from automake 1.16.3
Markus Armbruster [Sat, 26 Dec 2020 07:29:47 +0000 (08:29 +0100)]
build-aux INSTALL: Refresh from automake 1.16.3

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agom4: Refresh macros from autoconf-archive commit fd1d25c148
Markus Armbruster [Sat, 26 Dec 2020 07:20:57 +0000 (08:20 +0100)]
m4: Refresh macros from autoconf-archive commit fd1d25c148

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoconfig: Enlarge configuration tables that have variable size
Markus Armbruster [Sat, 26 Dec 2020 05:37:07 +0000 (06:37 +0100)]
config: Enlarge configuration tables that have variable size

Configuration tables product, sect-chr, ship-chr, plane-chr, land-chr,
nuke-chr have a bit of extra space deities can use for customizing
their games.  Give them more: enlarge product from 23 to 31
entries (plus one sentinel), sect-chr from 39 to 63, ship-chr from 47
to 127, plane-chr from 47 to 127, land-chr from 31 to 127, and
nuke-chr from 20 to 63.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoconfig: Slightly neater configuration table size macros
Markus Armbruster [Sat, 26 Dec 2020 05:30:24 +0000 (06:30 +0100)]
config: Slightly neater configuration table size macros

Macro SHP_TYPE_MAX is the largest possible ship type number.  It is
only used to size mchr[], and we need + 2 there.  Replace by MCHR_SZ
for simplicity.

Same for LND_TYPE_MAX, PLN_TYPE_MAX, P_MAX, and SCT_TYPE_MAX: replace
by LCHR_SZ, PLCHR_SZ, PCHR_SZ, and DCHR_SZ.

Same for N_MAXNUKE, except that one is more than the largest type
number.  Replace by NCHR_SZ.

For consistency, define ICHR_SZ and INTRCHR_SZ for sizing ichr[] and
intrchr_sz[].

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoAvoid SCT_TYPE_MAX, use ARRAY_SIZE() instead
Markus Armbruster [Sat, 26 Dec 2020 05:27:18 +0000 (06:27 +0100)]
Avoid SCT_TYPE_MAX, use ARRAY_SIZE() instead

The next commit will get rid of SCT_TYPE_MAX.  Prepare.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoNew macro ARRAY_SIZE()
Markus Armbruster [Sat, 26 Dec 2020 05:15:40 +0000 (06:15 +0100)]
New macro ARRAY_SIZE()

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoeconfig: Fix documentation of key assault_penalty
Markus Armbruster [Fri, 25 Dec 2020 12:15:14 +0000 (13:15 +0100)]
econfig: Fix documentation of key assault_penalty

Messed up when keys got documented in 4.2.0.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agoinfo/Empire4.4: Fix more 4.4.0 change log entry typos
Markus Armbruster [Mon, 21 Dec 2020 16:01:29 +0000 (17:01 +0100)]
info/Empire4.4: Fix more 4.4.0 change log entry typos

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Tidy up comments
Markus Armbruster [Sun, 26 Jul 2020 05:52:20 +0000 (07:52 +0200)]
fairland: Tidy up comments

Don't use ALL CAPS for headings.  Reformat them in the style
recommended by doc/coding.  Drop a few comments that aren't pulling
their weight, and fix the odd typo.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Rename fl_move() to drift_capital()
Markus Armbruster [Fri, 16 Oct 2020 14:26:10 +0000 (16:26 +0200)]
fairland: Rename fl_move() to drift_capital()

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Move qprint() and NUMTRIES
Markus Armbruster [Mon, 10 Aug 2020 13:52:02 +0000 (15:52 +0200)]
fairland: Move qprint() and NUMTRIES

qprint() was carelessly added at the end of the file in Empire 2,
where it's under the "Write a script for placing capitals" headline.
Move it to a more sensible place.

NUMTRIES was added behind the global variables in Empire 2.  Move it
before them, and reformat its comment.

The order of forward declarations of static functions looks random.
Reorder them to match the order of definitions.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Fuse capx[], capy[] into cap[]
Markus Armbruster [Fri, 21 Aug 2020 11:42:51 +0000 (13:42 +0200)]
fairland: Fuse capx[], capy[] into cap[]

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Fuse sectx[], secty[] into sect[]
Markus Armbruster [Fri, 21 Aug 2020 11:42:51 +0000 (13:42 +0200)]
fairland: Fuse sectx[], secty[] into sect[]

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Simplify how write_sect() iterates over sectors
Markus Armbruster [Fri, 21 Aug 2020 11:12:11 +0000 (13:12 +0200)]
fairland: Simplify how write_sect() iterates over sectors

Iterating over IDs is slightly simpler than iterating over
coordinates, so do that.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Represent sector ownership more efficiently
Markus Armbruster [Fri, 21 Aug 2020 06:41:14 +0000 (08:41 +0200)]
fairland: Represent sector ownership more efficiently

Replace own[x][y] by own[XYOFFSET(x, y)], and narrow the element type
from int to short.  Takes a fourth the space.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Tweak rural iron, fert, oil for simplicity
Markus Armbruster [Fri, 21 Aug 2020 06:31:56 +0000 (08:31 +0200)]
fairland: Tweak rural iron, fert, oil for simplicity

The previous commit's interpolation tables reproduce the historical
linear functions faithfully.  This exact fit requires an awkward data
point in each of the tables for iron fertility and oil.  Drop them.

No double-valued data points remain.  Adjust struct resource_point
member @res to int.

This results in slightly less rural iron (33 elevations down one
point), fertility (10 elevations down one point) and oil (14
elevations down one point, 5 down two points).  Only resource values
less than 100 are affected.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Compute resources by interpolating tables
Markus Armbruster [Fri, 21 Aug 2020 05:32:36 +0000 (07:32 +0200)]
fairland: Compute resources by interpolating tables

The functions mapping elevation to resources are piecewise linear.
Replace them by linear interpolation on data points defined in tables.
Tables are easier to tweak for deities than code.

The data points are a bit awkward because the functions they replace
are.  The next commit will tweak them to address that.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Tweak sea oil & mountain gold for simplicity
Markus Armbruster [Fri, 21 Aug 2020 06:18:49 +0000 (08:18 +0200)]
fairland: Tweak sea oil & mountain gold for simplicity

The functions mapping elevation to resources are piecewise linear,
except for set_oil(), which randomly adds 1 to sea oil, and
set_gold(), which is isn't linear for mountains, but very close.

Drop the random oil bit, and replace the non-liner gold function by a
linear one.  Both resources decrease at most by one point.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Rewrite complicated, buggy & boring elevation code
Markus Armbruster [Sat, 5 Sep 2020 04:44:12 +0000 (06:44 +0200)]
fairland: Rewrite complicated, buggy & boring elevation code

Land elevation is computed by first placing mountains, then elevating
land to fit.

Mountains placement is a weighted random sampling.  A sector's weight
is its distance to sea capped at five and squared.

Non-mountain, non-capital sectors get assigned equidistant elevations
from 1 up to 97 rounded to integer in an order that depends on
distance to mountain and distance to sea, both capped at 5.  Mountains
get equidistant elevations starting at 98 (see recent commit
"fairland: Fair mountain resources").  Capitals get 36.

Sea elevation is randomly chosen from a range that depends on the
sector's distance to land.  The range increases from [-27,-1] next to
land to [-127,-1] for distance 5 and up.

Without mountains, the result is boring: elevation increases pretty
much linearly with the distance from the coast, i.e. each islands is a
single cone.  With mountains, we get one cone per mountain.  The sea
sea elevations are basically noise.

Worse, it's buggy: mountain placement can place fewer mountains than
requested.  The weighted random sampling assigns weights even to
sectors that cannot be picked (capitals and sectors that have been
picked already).  When the dice land on such a sector, it instead
picks the next one (in island growth order) that can be picked.  If
there is no next one, the mountain is not placed.  This is a fairness
issue.

Impact varies with fairland parameters.  For 60 sector islands with 5%
mountains, around one in 10000 islands is short one mountain in my
testing.  For 10 sector islands with 50% mountains, some 760 out of
10000 islands are short one sector, 80 short two, and five short
three.  The numbers get worse as spikiness increases.

Undocumented misfeature: the placement loop is limited to 1000
iterations, supposedly to catch a runaway loop.  Since the loop
iterates exactly once per mountain, all this accomplishes it limiting
mountains to 1000 per island.

When too few mountains are placed, the loop assigning elevations to
non-mountain, non-capital decrements elevations below 1.  Since
Chainsaw 3, such elevations then get mapped to 1, plastering over the
bug.  We get non-mountain sectors with elevation 1 instead of
mountains.

Rewrite as follows.

Use a simple random hill algorithm to assign raw elevations:
initialize elevation to zero, then randomly raise circular hills on
land / lower circular depressions at sea.  Their size and height
depends on the distance to the coast, capped at 3.  After a sufficient
number of iterations, the resulting terrain has a "natural" look.
This is elevate_prep().

elevate_land() then sorts non-capital sectors by raw elevation.  The
highest become mountains.  Sectors get assigned the same equidistant
elevations as before, just in raw elevation order.

elevate_sea() simply normalizes raw elevation to [-127,-1].

Elevations now show a nice undulating pattern independent of mountain
percentage.

Implementation detail: replace elev[x][y] by elev[XYOFFSET(x, y)], and
narrow the element type from int to short.  Takes a fourth the space.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Tweak rural elevations for simplicity
Markus Armbruster [Wed, 19 Aug 2020 11:31:09 +0000 (13:31 +0200)]
fairland: Tweak rural elevations for simplicity

elevate_land() computes the sequence of elevations 97, 97 - delta,
... in fixed point with a scaling factor of 100.  Switch to
floating-point, because it's simpler.  Elevations (and thus resources)
change slightly due to reduced rounding errors.

Note that we map elevations less than 1 to 1 both before and after the
patch.  Odd.  Turns out this mitigates a bug: mountain placement can
place fewer mountains than it should, and when that happens,
elevations go below 1 here.  The next commit will fix this.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Drop unused elevation band for coastal mountains
Markus Armbruster [Wed, 19 Aug 2020 11:44:00 +0000 (13:44 +0200)]
fairland: Drop unused elevation band for coastal mountains

Elevations 34 and 35 are reserved for coastal mountains.  When an
elevation computed for a non-mountain falls in that band, it's
adjusted to 36.

This is unnecessary since the previous commit; drop.  Test output
shows the dropped elevation adjustment.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Fair mountain resources
Markus Armbruster [Wed, 19 Aug 2020 12:16:14 +0000 (14:16 +0200)]
fairland: Fair mountain resources

For fairness, fairland aims to make islands of the same size consist
of the exact same sectors, just individually arranged.

It fails to achieve its aim for mountains: their elevation is random,
34 or 35 for coastal mountains, and between 98 and 254 (two dice) for
non-coastal ones.  Elevation determines resources.  Mountains get 89
gold on average (between 80 and 93), except for coastal mountains,
which get none.  It didn't really matter until Empire 3 made mountains
produce gold dust.  Since then, it's unfair.

Set mountain elevation to 98 + i * delta, where i is the mountain
number (starting at zero), delta is (127 - 98) / max_mn, and max_mn is
the maximum number of mountains an island could have.  This puts the
highest mountains on the largest islands.

Non-coastal mountains have slightly less gold than before, coastal
mountains have much more.

Mountains can still be unfair, because fairland can screw up their
number.  To be fixed soon.

Avoid elevations 34 and 35 for non-mountain, non-capital sectors
doesn't make sense anymore.  The next commit will get rid of it.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Use zero elevation for "not yet elevated"
Markus Armbruster [Sun, 16 Aug 2020 15:46:54 +0000 (17:46 +0200)]
fairland: Use zero elevation for "not yet elevated"

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Eliminate sectc[][], use adj_land[] instead
Markus Armbruster [Fri, 21 Aug 2020 11:25:16 +0000 (13:25 +0200)]
fairland: Eliminate sectc[][], use adj_land[] instead

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Replace distance_to_land(), distance_to_sea()
Markus Armbruster [Sun, 16 Aug 2020 12:01:32 +0000 (14:01 +0200)]
fairland: Replace distance_to_land(), distance_to_sea()

distance_to_land() searches for closest land sector, and
distance_to_sea() for the closest sea sector.  We already have a more
efficient alternative: the breadth-first search recently added for
spheres of influence can precompute these distances.  Put it to use,
and retire distance_to_land() and distance_to_sea().

distance_to_what() could now be simplified.  Don't bother, because
it'll soon be deleted entirely.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Eliminate macro ELEV, it's an abomination
Markus Armbruster [Mon, 17 Aug 2020 06:39:22 +0000 (08:39 +0200)]
fairland: Eliminate macro ELEV, it's an abomination

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Prefer placing islands away from the edge of the sphere
Markus Armbruster [Mon, 14 Sep 2020 05:27:45 +0000 (07:27 +0200)]
fairland: Prefer placing islands away from the edge of the sphere

When an island gets placed too close to the edge of the sphere of
influence, its side facing the edge will likely be formed by the edge.
Looks unnatural, and can give a clue on the location of the other
continent.

Make place_island() prefer sectors away from the edge: instead of
picking one of the admissible sectors with equal probability, reduce
the probability as we get closer to the edge.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Try harder to deliver the requested amount of land
Markus Armbruster [Tue, 11 Aug 2020 15:58:23 +0000 (17:58 +0200)]
fairland: Try harder to deliver the requested amount of land

Planned island sizes are random with an expected value that matches
the average size requested by the user.  Can be off quite a bit when
the number of islands is small.  Also, actual island size can be
smaller than planned size when space is tight.

Instead of picking random island sizes independently, pick a random
split of their requested total size.

To reduce the probability of islands not growing to their planned
size, grow large islands before smaller ones.

To compensate for inability to grow, carry the difference over to the
next island size.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Make actual island sizes fair
Markus Armbruster [Tue, 11 Aug 2020 14:41:20 +0000 (16:41 +0200)]
fairland: Make actual island sizes fair

The previous commit reduced the difference in island size within the
same batch of islands to at most one.  Eliminate the remaining
difference by shrinking the bigger islands by one sector.

This invalidates the precomputed exclusive zones, so recompute them.

fairland-test needs a tweak to avoid loss of test coverage.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Grow islands interleaved
Markus Armbruster [Tue, 11 Aug 2020 14:16:30 +0000 (16:16 +0200)]
fairland: Grow islands interleaved

The previous commits changed grow_island() to create islands in
batches consisting of one island per continent, all of the same
planned size.  grow_island() still places and grows one island after
the other.  When an island can't grow to the actual size, the others
in the same batch are not affected.  Island size can therefore differ
a lot within the same batch.

Change grow_island() to interleave the work on a batch's island: first
place them all, then add one sector to each in turn.  Stop after all
reached the planned size, or one or more could not be grown further.

This is similar to how we grow continents: drift() places them all,
and grow_continent() adds one sector to each continent in turn.

Island size within the same batch can now differ at most by one
sector.  The next commit will eliminate that remaining difference.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Make planned island sizes fair
Markus Armbruster [Tue, 11 Aug 2020 13:39:31 +0000 (15:39 +0200)]
fairland: Make planned island sizes fair

The previous two commits put the same number of islands closest to
each continent.  This one makes their planned sizes match: instead of
rolling dice separately for each island's size, we roll dice only for
the first continent's islands.  The other continent's islands get the
same sizes.

Actual island sizes still differ when islands can't be grown to their
planned size.  To be addressed next.

fairland-test needs a tweak to avoid loss of test coverage.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Fail when island can't be placed, for fairness
Markus Armbruster [Tue, 11 Aug 2020 14:16:30 +0000 (16:16 +0200)]
fairland: Fail when island can't be placed, for fairness

The previous commit made island distribution more fair by placing
islands close to a continent in turn.  This is still unfair when
fairland can't place all the islands.

Make grow_islands() fail when it can't place all islands, and main()
start over then, just like it does when grow_continents() fails.

Deities can no longer fill the world with islands by asking for a
impossibly high number of islands.  Tolerable loss, I think.

fairland-test needs a tweak to avoid loss of test coverage.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Distribute islands more fairly among continents
Markus Armbruster [Mon, 10 Aug 2020 06:22:21 +0000 (08:22 +0200)]
fairland: Distribute islands more fairly among continents

fairland places islands of random size in random places, subject to
minimum distances.  Results are often less than fair, in particular
when the number of islands per continent is low: some continents have
more land nearby than others.  Increasing distances between islands
doesn't help much.  Deities commonly run fairland until they find the
result acceptable.

The next few commits will tackle this issue.  As a first step, this
one places islands closest to continents in turn, so that each
continent is closest to the same number of islands.  A continent is
closest to an island when it is closest to each of its sectors.

The number of islands must be a multiple of the number of continents
now.

Since fairland may be unable to place all islands, a continent may
still get fewer islands than it should.  The next commit will address
that.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Compute spheres of influence
Markus Armbruster [Sun, 9 Aug 2020 15:13:33 +0000 (17:13 +0200)]
fairland: Compute spheres of influence

A continent's sphere of influence is the set of sectors closer to it
than to any other continent.  The next commit needs this.  Compute the
spheres with a breadth-first search.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Eliminate dirx[], diry[]
Markus Armbruster [Tue, 18 Aug 2020 20:20:56 +0000 (22:20 +0200)]
fairland: Eliminate dirx[], diry[]

dirx[] and diry[] are redundant with diroff[][].  Convert the
remaining uses, and drop the arrays.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Fix island growth and correct its bias
Markus Armbruster [Thu, 13 Aug 2020 06:10:37 +0000 (08:10 +0200)]
fairland: Fix island growth and correct its bias

grow_one_sector() picks a coastal start sector, then moves along the
coast trying to add an adjacent sector to the island.

It operates in spiking mode with a chance of @sp percent.

When spiking, the neighbors with sea on both sides are preferred.  For
instance, when the area around the sector being considered looks like
this

     - .
    - - .
     - .

then the neighbor in direction j is preferred, because it has sea in
directions u and n.  No other neighbor is preferred.

The start sector is the first coastal sector found in a linear search
in growth order, starting at the last sector grown when spiking, or
else at a random sector.  This is new_try().

grow_one_sector() tries adding a neighbor in clockwise order, starting
with a random direction.  When spiking, it goes around the clock two
times, trying only preferred sectors in the first round.

When it can't add a neighbor, it moves to the next coastal sector with
next_coast().

Taken together, this randomly picks one element from the set of
pairs (S, D) where the sector in direction D off base sector S can be
added to the island.  How does the probability distribution look like?

Bias: a sector's probability to be added to the island land increases
with the number of base sectors it is adjacent to.  This tends to fill
in bays and lakes, which is fine.

Bias: coastal sectors following runs of non-coastal ones are more
likely to be picked as start sector.  Perhaps less of an issue when
spiking, where the pick is not intended to be random.

Bias: a pair (S, D) is more likely to be picked when base sector S
follows a run of coastal sectors that aren't base sectors, or
direction D follows a a run of directions that don't permit growth.

The impact of these two biases is not obvious.  I suspect they are the
reason behind the tendency of large islands to curve around obstacles
in a counterclockwise direction.  This can result in multiple islands
wrapping around a central one like layers of an onion.

Bug: the move along the coast is broken.  next_coast() searches for
the first adjacent sea in clockwise order, starting in direction g,
then from there clockwise for a sector belonging to the island.
Amazingly, this does move along the coast in a clockwise direction.
It can get caught in a loop, though.  Consider this island:

        -
     - - -
      -

If we start at the central sector (marked 0), the search along the
coast progresses like this:

        1
     - 0 2
      -

It reaches the central sector again after three moves (to 1, to 2,
back to 0), and stops without having reached the two sectors on the
left.

If we start with the leftmost sector, the search loops: 0, 1, 2, 3, 1,
...

        2
     0 1 3
      -

grow_one_sector() ensures termination by giving up after 200 moves.
Nuts!

Because of this, grow_one_sector() can fail when it shouldn't, either
because the search along the coast stops early, or goes into a loop,
or simply because there's too much coast.  The latter should not
happen in common usage, where island sizes are in the tens, not the
hundreds.

Clean up this hot mess by rewriting grow_one_sector() to pick a sector
adjacent to the island with a clearly defined probability, as follows.

Use weighted random sampling to pick one sector from the set of
possible adjacent sectors.

When spiking, a sector's weight increases with number of adjacent sea
sectors.  This directs the growth away from land, resulting in spikes.

When not spiking, the weight increases with the number of adjacent
land sectors.  This makes the island more rounded.

To visit the adjacent sectors, grow_one_sector() iterates over the
neighbors of all island sectors, skipping neighbors that have been
visited already.

This produces comparable results for low spike percentages.  The weird
onions are gone, though.

Noticeable differences emerge as the spike percentage grows.  Whereas
the old one produces long snakes that tend to curve into themselves,
the new one produces shorter spikes extending from a core, a bit like
tentacles.  Moreover, islands are more centered on their first sector
now.  The probability for coastal capitals is lower, in particular for
moderate spike percentages.

I consider this improvements.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Track adjacent land sectors
Markus Armbruster [Sun, 6 Sep 2020 06:44:09 +0000 (08:44 +0200)]
fairland: Track adjacent land sectors

The next commit will put it to use.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Drop try_to_grow() parameter @extra_dist
Markus Armbruster [Sun, 9 Aug 2020 16:01:19 +0000 (18:01 +0200)]
fairland: Drop try_to_grow() parameter @extra_dist

The previous commit removed the only call passing non-zero
@extra_dist.  Drop the parameter.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Correct island placement bias
Markus Armbruster [Sun, 9 Aug 2020 15:38:36 +0000 (17:38 +0200)]
fairland: Correct island placement bias

A sector is admissible for island placement when land can be grown in
every sector within a certain distance.

place_island() picks a random start sector, then searches linearly for
an admissible sector.  If it finds one, it places the island there.
Else, it reduces the distance by one and tries again.  It fails when
none is found even for distance zero.

Trying with extra distance is obviously meant to reduce the risk of
islands from running into each other without need.  Initial distance
is @di, the minimum distance between continents, which doesn't really
make sense, and is undocumented.

Bug: place_island() never tries the start sector.

Bias: placement probability is higher for sectors immediately
following inadmissible sectors.  Because of that, islands are more
often placed to the right of existing islands.  Players could exploit
that to guide their search for land.

Rewrite place_island() to pick sectors with equal probability,
dropping the undocumented extra distance feature.  If it's missed, we
can bring it back.

The new code visits each sector once.  The old code visits only one
sector in the best case, but each sector several times in the worst
case.  fairland performance improves measurably for crowded setups
with large @di, else it suffers.  For instance, Hvy Fever example
given in the commit before previous runs seven times faster for me.
With @di reduced to 2, its run time more than doubles.  Not that it
matters.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Drop place_island() parameters @xp, @yp
Markus Armbruster [Sun, 9 Aug 2020 15:13:33 +0000 (17:13 +0200)]
fairland: Drop place_island() parameters @xp, @yp

There is no need to pass coordinates back to the caller.  Replace
parameters @xp, @yp by local variables @x, @y.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Precompute "exclusive zone" for speed
Markus Armbruster [Sun, 2 Aug 2020 15:46:26 +0000 (17:46 +0200)]
fairland: Precompute "exclusive zone" for speed

grow_one_sector() and place_island() pass candidate sectors to
try_to_grow() until it succeeds.

try_to_grow() fails when the candidate isn't water, or there are
sectors belonging to other islands within a minimum distance.
It does that the obvious way: it searches for such sectors.

When there is plenty of space, try_to_grow() rarely fails when it
searches.  For each land sector, we visit the sectors within minimum
distance, plus a little extra work for the rare failures.

In a more crowded setup, however, try_to_grow() fails a lot, and we
visit sectors many times more.

Example: Hvy Fever

    8 continents
    continent size: 60
    number of islands: 64
    average size of islands: 10
    spike: 0%
    0% of land is mountain (each continent will have 0 mountains)
    minimum distance between continents: 6
    minimum distance from islands to continents: 3
    World dimensions: 140x68

This is a crowded setup.  With -R 1, try_to_grow() fails almost
750,000 times due to minimum distance, and takes more than 18 Million
iterations.

With default minimum distances 2 and 1, it fails less than 700 times,
taking less than 14,000 iterations.

Instead of searching the surrounding sectors every time we check a
candidate, precompute an "exclusive zone" around each island where
only this island may grow the obvious way: when adding a sector, visit
the sectors within minimum distance and add them to the island's
exclusive zone.

When @extra_distance is non-zero, try_to_grow() still has to search,
Only place_island() passes non-zero @extra_distance.  The next few
commits will get rid of that.

Complication: since the minimum distance for growing islands may
differ from the minimum distance for growing continents, we have to
recompute exclusive zones between growing continents and islands.

For the Hvy Fever example above, this reduces the number of sector
visits by more than 90%, and run time by more than 70% for me.  With
default distances, it's a wash.

Of course, fairland performance is hardly an issue on today's
machines: it takes fairly impractical setups to push run time over a
second.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Change try_to_grow()'s last argument to extra_dist
Markus Armbruster [Sun, 2 Aug 2020 15:38:29 +0000 (17:38 +0200)]
fairland: Change try_to_grow()'s last argument to extra_dist

Callers pass @di for continents, @id for islands, possibly plus some
extra distance.  Pass just the extra distance, and compute the rest in
try_to_grow().  This  prepares for the next commit.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Rename INFINITY to INFINITE_ELEVATION
Markus Armbruster [Sat, 8 Aug 2020 11:59:37 +0000 (13:59 +0200)]
fairland: Rename INFINITY to INFINITE_ELEVATION

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Simplify search for next wilderness to elevate
Markus Armbruster [Sun, 16 Aug 2020 15:42:02 +0000 (17:42 +0200)]
fairland: Simplify search for next wilderness to elevate

To find the wilderness sector to elevate next, elevate_land() searches
the non-mountain, non-capital sectors of the island for one that
maximizes a function of its distance to mountains and to sea.

The search ignores already elevated sectors in a less than obvious
way: 1. it never picks a sector where the function yields -INFINITY or
less, and 2. when elevating a wilderness, its (cached) distances get
reset to values that make the function return a more negative value.

Use a more direct check of "not yet elevated": elevation is still the
initial -INFINITY.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Use INT_MAX as initial minimal distance in iso()
Markus Armbruster [Sun, 26 Jul 2020 05:30:58 +0000 (07:30 +0200)]
fairland: Use INT_MAX as initial minimal distance in iso()

Simpler and more obviously correct than WORLD_X + WORLD_Y.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Nicer & much faster replacement for next_vector()
Markus Armbruster [Sat, 25 Jul 2020 12:08:38 +0000 (14:08 +0200)]
fairland: Nicer & much faster replacement for next_vector()

next_vector() is kind of cute, but it is also unobvious, cumbersome to
use, and slow for arguments greater than one.

Replace it by hexagon_first(), hexagon_next().  The new code takes
O(1) time, whereas the old code takes O(n).  Iterating over a hexagon
changes from

       for (i = 0; i < n; ++i)
           vector[i] = 0;
       do {
           x = x0;
           y = y0;
           for (i = 0; i < n; ++i) {
               x = new_x(x + dirx[vector[i]]);
               y = new_y(y + diry[vector[i]]);
           }
   do stuff with @x, @y...
       } while (next_vector(n));

to

       hexagon_first(&hexit, x0, y0, n, &x, &y);
       do {
   do stuff with @x, @y...
       } while (hexagon_next(&hexit, &x, &y));

In my measurements, it's ~8% slower for n == 1, 25% faster for n == 2,
and more than three times faster for n == 6.

fairland's performance is dominated by it.  Creating worlds for the
Common Fever blitz (distance arguments 3 and 2) and the Hvy Fever
Blitz (distance arguments 6 and 3) on my machine speeds up by a factor
of 1.6 and 2.1, respectively.

Of course, fairland performance is hardly an issue on today's
machines: it takes fairly impractical setups to push run time over a
second.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Report missing and stunted islands
Markus Armbruster [Sat, 25 Jul 2020 06:13:54 +0000 (08:13 +0200)]
fairland: Report missing and stunted islands

fairland can create fewer and smaller islands than the user requested.
Report like this:

    No room for island #13
    6 stunted islands

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Check first two continent sectors properly
Markus Armbruster [Tue, 11 Aug 2020 05:52:45 +0000 (07:52 +0200)]
fairland: Check first two continent sectors properly

grow_continents() places the first two continent sectors without
checking for collisions or minimum distance.  Unlikely to be an issue
in practice, as growing such a continent will almost certainly fail.
Fix it anyway.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Move "is water" check into try_to_grow()
Markus Armbruster [Tue, 11 Aug 2020 05:51:30 +0000 (07:51 +0200)]
fairland: Move "is water" check into try_to_grow()

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Move capital initialization to drift() & simplify
Markus Armbruster [Mon, 10 Aug 2020 15:55:15 +0000 (17:55 +0200)]
fairland: Move capital initialization to drift() & simplify

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Move initialization of elev into create_elevations()
Markus Armbruster [Mon, 10 Aug 2020 15:48:21 +0000 (17:48 +0200)]
fairland: Move initialization of elev into create_elevations()

Move it so the next commit can inline init() into drift().

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Move global variables mc[] into stable(), eliminate mcc
Markus Armbruster [Sun, 26 Jul 2020 10:35:15 +0000 (12:35 +0200)]
fairland: Move global variables mc[] into stable(), eliminate mcc

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Fix "Only managed to grow" error message off by one
Markus Armbruster [Sat, 25 Jul 2020 07:20:09 +0000 (09:20 +0200)]
fairland: Fix "Only managed to grow" error message off by one

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
3 years agofairland: Eliminate global variable @fl_status
Markus Armbruster [Sat, 25 Jul 2020 07:01:56 +0000 (09:01 +0200)]
fairland: Eliminate global variable @fl_status

grow_one_sector() sets @fl_status when it fails to grow a continent.
grow_continents() uses @fl_status to find out whether
grow_one_sector() succeeded, and main() uses it to find out whether
grow_continents() succeeded.

Change grow_continents() to return success / failure.
grow_one_sector() already does.  Use the return values, and eliminate
@fl_status.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>