Xconq Patch1 (part 1 of 2)
Stanley Shebs
shebs at utah-cs.UUCP
Thu Jul 21 09:17:25 AEST 1988
This is the first official patch to version 5 of xconq. It is primarily bug
fixes, but it also includes some new smarts for machine players (try "xconq
-m steppes -e 6"), and a new scenario "monster", which features monsters
stomping on buildings and starting fires (unfortunately the bitmaps are for
X11 only, although making X10 ones would not be hard). There are quite a
few changes in the code:
* Tweaked config.h to account for System V and DG/UX.
* Fixed save bug where transports not written out before their occupants.
* Fixed machine player bug where dead units were looked at sometimes.
* Fixed standing order bug where null pointers dereferenced sometimes.
* Fixed area operation bug where very large arguments caused coredumps.
* Fixed machine player bug where it couldn't resign if no moving units left.
* Fixed display bug where captured unit was not redrawn after capture.
* Fixed X bug where window movement/iconification wedged other players.
* Fixed X11 bug where unit type could not be selected with mouse.
* Fixed X11 interface to use the default screen rather than 0.
* Fixed init bug where particular terrain types had to be on map.
* Fixed init bug where some units with "all-seen" not visible until they move.
* Fixed save bug where numbering of units is too high after restoring.
* Changed to-move so absence of move resource immobilizes rather than kills.
* Changed to-make to specify total amount, to be amortized over schedule.
* Changed period word "spy" to "spy-chance".
* Changed some messages to be suppressed if message string empty.
* Rewrote parts of machine player code:
- Production governed by needs of groups.
- Resignation based on possibility to win rather than weight of territory.
- Doesn't build hundreds of bases in the standard period.
* Added more accurate control over flashing of current position.
* Added a command 'v' to "re-flash" the current position.
* Added some more unit aliveness tests, for paranoia's sake.
* Added a compiletime option to deal with stupid X11 servers.
* Sped up disaster handling slightly.
* Increased size of unit state window slightly.
* Made the monochrome test be for <= 2 colors instead of == 2 colors.
* Simplified the listing of the winning alliance.
To install these patches, first put your Makefile and config.h into safe
places where they won't be overwritten. Then unpack the kit into your xconq
sources; this will install new versions of config.h and mplay.c, as well as
several periods in the library directory. The patch file, xconq.patch1
contains all the source code patches. Run patch(1) on it, merge the new
Makefile config.h with your old options, and recompile everything. What
you will end up with is officially version 5.1.
I want to thank everyone who sent in bug reports and fixes. Not every
reported problem has been resolved (see the ToDo for what remains), but
I wanted to somewhat timely on these most important fixes. As always,
flames, comments, and suggestions welcome - there's still another couple
megabytes of space on the disk...
stan shebs
shebs at cs.utah.edu
#! /bin/sh
# This is a shell archive. Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file". To overwrite existing
# files, type "sh file -c". You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g.. If this archive is complete, you
# will see the following message at the end:
# "End of archive 1 (of 2)."
# Contents: config.h lib/future.per mplay.c
# Wrapped by shebs at defun.utah.edu on Wed Jul 20 17:08:58 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f config.h -a "${1}" != "-c" ; then
echo shar: Will not over-write existing file \"config.h\"
else
echo shar: Extracting \"config.h\" \(6706 characters\)
sed "s/^X//" >config.h <<'END_OF_config.h'
X/* Copyright (c) 1987, 1988 Stanley T. Shebs, University of Utah. */
X/* This program may be used, copied, modified, and redistributed freely */
X/* for noncommercial purposes, so long as this notice remains intact. */
X
X/* RCS $Header: config.h,v 1.5 88/07/20 16:05:05 shebs Exp $ */
X
X/* This file has several things that can be fiddled with; generally, */
X/* the more interesting things to tweak are closer to the front of the file. */
X/* Xconq is no longer wired to X; in fact, the specific graphics interface */
X/* is unknown until link time, and does not affect compilation. */
X
X/* Just a couple places where we care about machine type - but the main */
X/* program does assume argc/argv interface, which is a problem for micros. */
X
X#define UNIX
X
X/* These are wishful thinking at the moment. */
X/* #define ATARI */
X/* #define MAC */
X
X#define BSD /* Berkeley Unix and probably Ultrix too */
X/* #define HPUX */ /* HP's mishmash of features */
X/* #define SYSV */ /* Darth Vader's favorite Unix */
X/* #define DGUX */ /* Data General is almost like BSD */
X
X#ifdef BSD
X#define SELECT2 /* Can use select(2) to wait on several at once. */
X#define USECRMODE /* Use crmode() for raw mode in curses. */
X#endif
X
X#ifdef HPUX
X#define SELECT2 /* Can use select(2) to wait on several at once. */
X#define USECBREAK /* Use cbreak() for raw mode in curses. */
X#endif
X
X#ifdef SYSV
X#define USECBREAK /* Use cbreak() for raw mode in curses. */
X#endif
X
X#ifdef DGUX
X#define SELECT2 /* Can use select(2) to wait on several at once. */
X#define USECRMODE /* Use crmode() for raw mode in curses. */
X#endif DGUX
X
X#include <stdio.h>
X#include <ctype.h>
X
X#ifdef DGUX
X#include <string.h> /* To get malloc declaration */
X#endif DGUX
X
X/* This is where predefined maps/scenarios/periods/fonts can be found. */
X
X#ifndef XCONQLIB
X#define XCONQLIB "/usr/games/lib/xconq"
X#endif XCONQLIB
X
X/* The newsfile always lives in the lib directory. */
X
X#define NEWSFILE "xconq.news"
X
X/* This file is a list of mapfiles to exhibit in menus; it will always be */
X/* in the lib directory. There are no automatic constructors for this */
X/* file, since it should contain only well-tested files added after careful */
X/* deliberation. */
X
X#define MAPFILEFILE "mapfiles"
X
X/* The name of the savefile. It will be put in the current directory. */
X
X#define SAVEFILE "save.xconq"
X
X/* This file gets the parameter listing for the period in use. */
X/* It will also be created in the current directory. */
X
X#define PARMSFILE "parms.xconq"
X
X/* This file gets the game statistics when it's all over. */
X/* It will also be created in the current directory. */
X
X#define STATSFILE "stats.xconq"
X
X/* This file gets a printout of the side's view. */
X
X#define VIEWFILE "view.xconq"
X
X/* This file gets a list of commands as they appear in the help window. */
X
X#define CMDFILE "cmds.xconq"
X
X/* Default random map sizes. Adjust these to taste - 60x60 is a moderate */
X/* length game, 30x30 is short, 360x120 is L-O-N-G ! */
X
X#define DEFAULTWIDTH 60
X#define DEFAULTHEIGHT 30
X
X/* Absolute maximum number of sides that can play. This limit can only be */
X/* extended by changing the representation of views of players from bytes */
X/* to something larger, thereby doubling (at least) space requirements. */
X
X#define MAXSIDES 7
X
X/* Absolute maximum number of kinds of units. (same restriction as above) */
X
X#define MAXUTYPES 30
X
X/* Maximum number of types of natural resources. This number can be set */
X/* higher, in fact I think the only limitation is that there won't be */
X/* enough distinct chars, but more rtypes means larger unit objects. */
X
X#define MAXRTYPES 6
X
X/* Maximum number of terrain types. Must be fewer than 256, but also */
X/* limited by display capabilities. */
X
X#define MAXTTYPES 20
X
X/* Maximum number of random side names that can be defined. */
X
X#define MAXSNAMES 200
X
X/* Maximum number of random unit names that can be defined. */
X
X#define MAXUNAMES 1000
X
X/* The maximum number of mapfiles that can be in menus. (Not a limit on the */
X/* total number of files that can exist, however.) */
X
X#define MAXMAPMENU 100
X
X/* The maximum number of mapfiles that can be loaded into a game (recursive */
X/* loads are not performed and not counted). */
X
X#define MAXLOADED 16
X
X/* Default game length in turns. */
X
X#define DEFAULTTURNS 1000
X
X/* Number of messages displayed at one time. No upper limit I believe, */
X/* but too many won't fit on the screen. The actual numbers of lines */
X/* displayed can be changed by the player, subject to limitations on the */
X/* screen space available. */
X
X#define MAXNOTES 10
X
X/* Default color of text and icons - 0 is for white on black, 1 */
X/* is for black on white. Should be set appropriately for the most */
X/* common monochrome display (color displays always do white on black). */
X/* This is also settable by the player, so the default is just for */
X/* convenience of the majority. */
X
X#define BLACKONWHITE 0
X
X/* When true, displays will use more graphics and less text. This can */
X/* also be toggled by players individually. */
X
X#define GRAPHICAL 0
X
X/* The default fonts can be altered by users, so these are just hints. */
X/* These options do not necessarily apply to non-X versions. */
X
X#define TEXTFONT "9x15"
X#define ICONFONT "xconq"
X
X/* All names, phrases, and messages must be able to fit in statically */
X/* allocated buffers of this size. */
X
X#define BUFSIZE 120
X
X/* If defined, a statistics file is written at the end of the game. */
X/* The numbers therein are only for serious gamers, and the files can */
X/* be embarassing clutter in your directory! */
X
X#define STATISTICS
X
X/* Initial limit on units and cities that can be active at one time. If */
X/* growable option is enabled, will try to grow the array to hold more. */
X
X#define INITMAXUNITS 1000
X/* #define GROWABLE */
X
X/* When this is enabled, machine players will be able to examine humans' */
X/* units rather more closely than is possible in reverse. In particular, */
X/* a machine will know just where the human units are, as well as their */
X/* current attributes (like hit points). */
X
X/* #define CHEAT */
X
X/* When the eye needs to be drawn to a particular spot on the screen, */
X/* the X interfaces will briefly flash an X. The time is set by this */
X/* value, which is measured in milliseconds that the X is visible, and */
X/* the flashing can be disabled entirely by supplying 0 here. */
X
X#define DELAY 100
X
X/* Some X11 servers die if too much is written between output flushes. */
X/* This can be used with any graphics system with a similar problem; */
X/* this option does not affect correctness, but may impact performance. */
X
X#define STUPIDFLUSH
END_OF_config.h
if test 6706 -ne `wc -c <config.h`; then
echo shar: \"config.h\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f lib/future.per -a "${1}" != "-c" ; then
echo shar: Will not over-write existing file \"lib/future.per\"
else
echo shar: Extracting \"lib/future.per\" \(9272 characters\)
sed "s/^X//" >lib/future.per <<'END_OF_lib/future.per'
XXconq 0 -+---- Futuristic period from Jay Scott
XPeriod 0
X
X"around 2200 AD" period-name
X
X"h" "hovercar" "easily built, moves fast and captures cities" utype
X"g" "groundcar" "slow but tough, captures cities" utype
X"s" "saucer" "moves fast, easy to build, but short range" utype
X"d" "destroyer" "heavy aircraft that defeats subs and saucers" utype
X"c" "constructor" "builds bases anywhere" utype
X"m" "mother ship" "carries aircraft and ground units" utype
X"t" "transport sub" "quickly built, carries ground units" utype
X"a" "attack sub" "attacks cities and carries rockets" utype
X"R" "rocket" "missile that can kill most units and hit cities" utype
X"/" "base" "airstrip + port but no production" utype
X"*" "town" "produces but easily captured and may revolt" utype
X"@" "city" "metropolis - hard to capture" utype
X
X"F" "fuel" "used for both movement and combat" rtype
X
X"." "sea" "sky blue" ttype
X"," "shallows" "cyan" ttype
X"=" "swamp" "dark gray" ttype
X"+" "plains" "green" ttype
X"%" "forest" "forest green" ttype
X"~" "desert" "yellow" ttype
X"^" "mountains" "sienna" ttype
X"_" "ice" "white" ttype
X":" "vacuum" "black" ttype
X
Xtrue [ swamp vacuum ] dark
X
Xt* t* nuked ; most terrain won't actually change
Xdesert [ plains forest ] nuked
Xmountains ice nuked
X
X[ 0 69 70 70 70 70 95 99 0 ] t* min-alt
X[ 69 70 72 95 95 95 99 100 0 ] t* max-alt
X[ 0 0 50 20 80 0 0 0 0 ] t* min-wet
X[ 100 100 100 80 100 20 100 100 0 ] t* max-wet
X
Xice edge-terrain
X
X[ / * @ ] "cities" define
X[ h g s d c m t a R ] "movers" define
X
X"hovcraft" h icon-name
X"tank" g icon-name
X"saucer" s icon-name
X"delta" d icon-name
X"builder" c icon-name
X"mothrship" m icon-name
X"sub" t icon-name
X"sub" a icon-name
X"rocket" R icon-name
X"saucerpad" / icon-name
X"town22" * icon-name
X"city22" @ icon-name
X
X[ 1 5 25 ] cities territory
X
X[ 0 2 1 ] cities in-country
X100 * density
X@ first-unit
Xh first-product
X100 [ plains ] [ * @ ] favored
X
X[ 4 6 2 10 10 20 7 12 8 ] movers @ make
X[ 4 6 2 10 10 20 7 12 8 ] movers * make
X3 / c make
Xtrue [ * @ ] maker
X20 u* startup
X50 [ d m ] startup
X100 c startup
X300 R startup
X
X10 fuel cities produce
X[ 30 40 16 16 30 40 200 150 30 500 1000 2000 ] fuel u* storage
X1 [ s d ] fuel consume
X1 u* fuel consume ; not plausible, but helps machine players
X
X-1 u* movers out-length ; so low-capacity units don't lose fuel
X
X100 t* cities productivity
X
X[ 3 1 8 4 3 6 2 3 10 ] movers speed
X
X0 plains h moves
X1 shallows h moves
X0 [ plains desert ] g moves
X0 t* [ s d c m ] moves
X0 [ sea shallows ] [ t a ] moves
X0 t* R moves
X0 t* / moves ; allows base to be constructed on any terrain
X
X1 fuel movers to-move
X
X[ 6 4 10 2 1 ] [ h g s d c ] m capacity
X4 R a capacity
X[ 2 1 ] [ h g ] t capacity
X10 u* / capacity
X100 u* [ * @ ] capacity
X
X0 [ t a ] visibility
Xtrue cities always-seen
X
X[ 1 1 1 2 2 5 2 2 1 10 20 40 ] u* hp
X
X[ 65 60 40 50 50 50 5 5 50 99 99 99 ] u* h hit
X[ 80 60 50 40 50 50 10 10 50 99 99 99 ] u* g hit
X[ 50 40 70 10 70 90 10 10 99 99 99 99 ] u* s hit
X[ 10 10 65 20 70 70 50 50 20 99 99 99 ] u* d hit
X[ 20 20 10 5 20 10 0 0 20 0 0 0 ] u* c hit
X[ 15 10 20 5 40 40 0 0 30 0 0 0 ] u* m hit
X[ 0 0 0 0 0 0 0 0 0 0 0 0 ] u* t hit
X[ 40 60 10 10 20 20 60 20 0 99 99 99 ] u* a hit
X[ 99 99 60 80 90 90 70 70 20 99 99 99 ] u* R hit
X[ 10 10 10 20 0 0 0 10 0 0 0 0 ] u* / hit
X[ 30 30 30 40 0 0 0 20 0 0 0 0 ] u* / hit
X[ 50 50 50 50 0 0 0 50 0 0 0 0 ] u* / hit
X
X1 u* u* damage
X3 cities a damage
X4 u* R damage
X10 cities R damage
X2 u* @ damage
X
X[ 80 70 20 ] cities h capture
X[ 90 80 30 ] cities g capture
X
Xtrue R self-destruct
X
X1 fuel u* hits-with
X1 fuel u* hit-by
X
Xtrue movers can-disband
Xtrue / can-disband
X
Xtrue cities neutral
X
X-50 neutrality
X
Xbegin{notes}
XA science fiction xconq period.
XIt's weirder and wilder than the historical periods provided with the game.
XLots of things happen faster, so the game is often shorter.
X
XThis is in the public domain.
X
X ----- ----- the units ----- -----
X
XHovercar. A fast-moving ground unit that easily captures towns. A
Xhovercar floats on an antigravity field, so it can maneuver easily
Xeven in mountainous terrain or shallow water (though not deep ocean).
XHovercars are invaluable in an invasion for their ability to take
Xcities quickly, but they are easily destroyed by flying saucers or
Xgroundcars.
X
XGroundcar. A slow-moving unit which travels on treads, like today's
Xtanks. It can afford to carry a large shield generator, which makes
Xit tough to destroy. It can negotiate forests by pushing down the
Xtrees, or burning them away with its gun, but it can't move in
Xmountains. Groundcars are ideal for defending your homeland from
Xinvasion. If you have enough on hand, you can often bounce back even
Xafter many of your towns are captured.
X
XSaucer. The flying saucer is a weak but fast-moving aircraft. Saucers
Xare very cheap to produce; one use is to overwhelm stronger units with
Xmob attacks. They can even bombard a city to rubble, unless there's
Xa Defender around. Saucers are also good for recon, within their limited
Xrange.
X
XDefender. A heavy, delta-wing aircraft that's your only safe defense
Xagainst the enemy's swarms of saucers. It uses aerodynamic lift to
Xsupplement its drive, allowing it to carry relatively massive
Xantisubmarine equipment. But it's an easy target for ground units.
X
XConstructor. An automated airborn factory that can build a base from
Xon-site materials in only one turn. It can build a base anywhere, even
Xon water or ice. That means, for instance, that you can build bridges
Xbetween islands for ground units. The constructor is the only way
Xto produce the sophisticated equipment needed for a twenty-second
Xcentury base (you don't know how hard it is to refuel those saucers :-).
XBut it's vulnerable to attack, especially from saucers.
X
XMother ship. Expensive to build, but the only way to mount a major
Xinvasion. A mother ship can carry lots of saucers, lots of ground
Xunits, a couple Defenders and a constructor, all at once. (The constructor
Xis useful for building stepping stone bases toward the enemy.)
XA mass attack is sure to bring down a mother ship, so it deserves
Xsaucer patrols--but you may have to forego patrols if you're trying
Xfor a surprise invasion.
X
XTransport sub. Carries only a few ground units, but much cheaper to
Xproduce than a mother ship. This is how you get your troops to another
Xisland early in the game. And since, like subs in the WWII period,
Xit's invisible until bumped into, it's good for sneak raids on isolated
Xoutposts. A transport submarine can't attack anything by itself.
X
XAttack sub. Strong against transport subs, and, if they're on the coast,
Xground units and cities. It stays underwater and pokes its weapons out,
Xwhich makes it relatively invulnerable to counterattack. It's faster
Xthan a transport sub, and good for wide-ranging exploration. And to top
Xit off, it can carry several rockets to within easy range of enemy cities.
XOn the other hand, all submarines have to be careful of Defenders.
X
XRocket. Actually a remotely guided missile, which does a lot of damage
Xto its target but vaporizes itself in the process. One rocket is
Xenough to take out most units--it's not quite enough by itself
Xto down a mother ship. Three rockets together are sufficient to pound
Xa town back to a base. Only occasionally will a rocket miss its target.
XBesides stomping cities, they're useful for eliminating enemy groundcars
Xand Defenders just before an invasion. Since rockets are easily
Xshot down if spied in mid-flight, it's sensible to launch them from
Xattack subs near their targets. [Because of the way xconq works,
Xmessages about rockets don't always make sense.]
X
X ----- ----- strategy ----- -----
X
XThe game is designed so you need to have at least a few of every
Xkind of unit to do well in a full game. (You may be able to get by
Xwithout transport subs if you start out on a big continent.) If
Xanybody finds they can consistently do well without some kind of unit,
XI want to hear how so I can fix it!
X
XEvery unit has at least one nemesis which can destroy it relatively
Xeasily. Your goal should be to fight every battle at an advantage,
Xpitting each unit against its natural prey--saucers against hovercars,
Xdefenders against saucers, hovercars against defenders.
X
XRockets ensure that the game doesn't drag on too long. Invasion is
Xrisky, but when it works the invader wins quickly. Rockets by contrast
Xare a slow but steady way to nibble at the enemy production base.
X
XMachine players are especially easy to defeat in this period. If you're
Xlucky enough to start near one, you can blitz it with hovercars and
Xsaucers and win in short order. It takes longer if you're far from
Xthe robot. Gaining air superiority is usually the first step.
X
XThe game hasn't been played by enough people for me to tell what
Xstrategies are best in different circumstances. If I've done my
Xjob well, the best plan will depend in detail on the opponent and
Xthe situation, and you'll have to think hard.
X
XI'd appreciate any comments.
X
X Jay Scott, August 1987.
X ...bpa!swatsun!scott
X ...seismo!bpa!swatsun!scott
Xend{notes}
X
Xend
X
END_OF_lib/future.per
if test 9272 -ne `wc -c <lib/future.per`; then
echo shar: \"lib/future.per\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f mplay.c -a "${1}" != "-c" ; then
echo shar: Will not over-write existing file \"mplay.c\"
else
echo shar: Extracting \"mplay.c\" \(35452 characters\)
sed "s/^X//" >mplay.c <<'END_OF_mplay.c'
X/* Copyright (c) 1987, 1988 Stanley T. Shebs, University of Utah. */
X/* This program may be used, copied, modified, and redistributed freely */
X/* for noncommercial purposes, so long as this notice remains intact. */
X
X/* RCS $Header: mplay.c,v 1.3 88/07/20 16:07:59 shebs Exp $ */
X
X/* This file implements all of machine strategy. Not much room for fancy */
X/* tricks, just solid basic play. The code emphasizes avoidance of mistakes */
X/* instead of strategic brilliance, so machine behavior is akin to bulldozer */
X/* plodding. Nevertheless, bulldozers can be very effective when they */
X/* outnumber the human players... */
X
X/* It is also very important to prevent infinite loops, so no action of the */
X/* machine player is 100% certain. */
X
X#include "config.h"
X#include "misc.h"
X#include "dir.h"
X#include "period.h"
X#include "side.h"
X#include "unit.h"
X#include "map.h"
X#include "global.h"
X
X/* Maximum number of unit groups that can be maintained. Need groups for */
X/* both offense and defense. */
X
X#define MAXGROUPS 80
X
X/* the non-group */
X
X#define NOGROUP 0
X
X/* Goals for both groups and individuals. */
X
X#define NOGOAL 0
X#define DRIFT 1
X#define HITTARGET 2
X#define CAPTARGET 3
X#define BESIEGE 4
X#define OCCUPYHEX 5
X#define EXPLORE 6
X#define DEFEND 7
X#define LOAD 8
X#define APPROACH 9
X#define RELOAD 10
X
X/* Groups organize machine player activity at the multiple-unit level. */
X
Xtypedef struct a_group {
X short goal; /* the intended purpose of the group */
X short priority; /* how important the group is */
X short x, y; /* a relevant location */
X short etype; /* type of a unit there (or NOTHING) */
X short area; /* radius of relevance of group activity */
X short member[MAXUTYPES]; /* number of each type in group */
X short need[MAXUTYPES]; /* what group wants but doesn't have */
X} Group;
X
X/* This structure is where machine sides keep all the plans and planning */
X/* related data. */
X/* Group 0 is never actually used (a sort of a dummy for various purposes). */
X
Xtypedef struct a_plan {
X short estimate[MAXSIDES][MAXUTYPES]; /* estimated numbers of units */
X short allies[MAXSIDES][MAXUTYPES]; /* strength of other alliances */
X short cx, cy; /* "centroid" of all our units */
X short lastreplan; /* last turn we rechecked the plans */
X short demand[MAXUTYPES]; /* worth of each utype w.r.t. strategy */
X Group group[MAXGROUPS]; /* all the groups that can be formed */
X} Plan;
X
X/* Encapsulate some pointer-chasing and casting messiness. */
X
X#define side_plan(s) ((Plan *) (s)->plan)
X
X/* Malloced integer array accessors and modifers. */
X
X#define aref(m,x,y) ((m)[(x)+world.width*(y)])
X
X#define aset(m,x,y,v) ((m)[(x)+world.width*(y)] = (v))
X
Xint evaluate_hex(), maximize_worth();
X
Xchar groupbuf[BUFSIZE]; /* buffer for group debugging print */
Xchar shortbuf[BUFSIZE]; /* buffer for short unit description */
X
X/* General collections of numbers used by all machine players. */
X
Xint fraction[MAXTTYPES]; /* percentages of terrain types in world */
Xint bhw[MAXUTYPES][MAXUTYPES]; /* basic worth for hitting */
Xint bcw[MAXUTYPES][MAXUTYPES]; /* basic worth for capturing */
Xint maxoccupant[MAXUTYPES]; /* total capacity of a transport */
Xint *localworth; /* for evaluation of nearby hexes */
X
Xint bestworth = -10000, bestx, besty;
X
XUnit *munit; /* Unit being decided about */
X
XSide *mside; /* Side whose unit is being decided about */
X
X/* List out data about a group (very compactly). */
X
Xchar *
Xgroup_desig(plan, g)
XPlan *plan;
Xint g;
X{
X int e = plan->group[g].etype;
X
X sprintf(groupbuf, "group %d goal %d pri %d ->%d,%d %d (%c)",
X g, plan->group[g].goal, plan->group[g].priority,
X plan->group[g].x, plan->group[g].y, plan->group[g].area,
X (e == NOTHING ? ' ' : utypes[e].uchar));
X return groupbuf;
X}
X
X/* Short unreadable but greppable listing of unit. */
X
Xchar *
Xunit_desig(unit)
XUnit *unit;
X{
X sprintf(shortbuf, "s%d %d %c (%d,%d)",
X side_number(unit->side), unit->number, utypes[unit->type].uchar,
X unit->x, unit->y);
X return shortbuf;
X}
X
X/* Init used by all machine players. Precompute useful information */
X/* relating to unit types in general, and that usually gets referenced */
X/* in inner loops. */
X
Xinit_mplayers()
X{
X int u, u2, t, g;
X Side *side, *side2;
X
X localworth = (int *) malloc(world.width*world.height*sizeof(int));
X for_all_terrain_types(t) {
X fraction[t] = ((ttypes[t].maxalt - ttypes[t].minalt) *
X (ttypes[t].maxwet - ttypes[t].minwet)) / 100;
X }
X for_all_unit_types(u) {
X maxoccupant[u] = 0;
X for_all_unit_types(u2) {
X bhw[u][u2] = basic_hit_worth(u, u2);
X bcw[u][u2] = basic_capture_worth(u, u2);
X maxoccupant[u] += utypes[u].capacity[u2];
X }
X }
X /* tell us about how things rated */
X if (Debug) {
X for_all_terrain_types(t) {
X printf("%3d%% ", fraction[t]);
X }
X printf("\n\n");
X for_all_unit_types(u) {
X for_all_unit_types(u2) printf("%5d", bhw[u][u2]);
X printf("\n");
X }
X printf("\n");
X for_all_unit_types(u) {
X for_all_unit_types(u2) printf("%5d", bcw[u][u2]);
X printf("\n");
X }
X printf("\n");
X }
X /* For all sides, because human might use "robot" option */
X for_all_sides(side) {
X side->plan = (long) (Plan *) malloc(sizeof(Plan));
X side_plan(side)->cx = side_plan(side)->cy = 0;
X for (g = 0; g < MAXGROUPS; ++g) {
X side_plan(side)->group[g].goal = NOGROUP;
X side_plan(side)->group[g].priority = 0;
X }
X side_plan(side)->lastreplan = -100;
X }
X}
X
X/* A crude estimate of the payoff of one unit type hitting on another type. */
X/* This is just for general estimation, since actual worth may depend on */
X/* damage already sustained, unit's goals, etc. */
X
Xbasic_hit_worth(u, e)
Xint u, e;
X{
X int worth, anti;
X
X worth = utypes[u].hit[e] * min(utypes[e].hp, utypes[u].damage[e]);
X if (utypes[e].hp > utypes[u].damage[e]) {
X worth /= utypes[e].hp;
X }
X if (period.counterattack) {
X anti = utypes[e].hit[u] * min(utypes[u].hp, utypes[e].damage[u]);
X if (utypes[u].hp > utypes[e].damage[u]) {
X anti /= utypes[u].hp;
X }
X }
X worth -= anti;
X return worth;
X}
X
X/* A crude estimate of the payoff of one unit type trying to capture. */
X
Xbasic_capture_worth(u, e)
Xint u, e;
X{
X int worth = 0, anti = 0;
X
X if (could_capture(u, e)) {
X worth += utypes[u].capture[e] * utypes[u].hp;
X }
X return worth;
X}
X
X/* At the beginning of each turn, can make plans and review the situation. */
X/* This should be frequent at first, but rather expensive to do always, */
X/* so only some chance of doing it on a random turn. */
X
Xinit_machine_turn(side)
XSide *side;
X{
X if (global.time < 20 || probability(30)) make_strategy(side);
X if (global.time < 20 || probability(50)) review_groups(side);
X if (global.time > 20 || probability(20)) decide_resignation(side);
X}
X
X/* Strategy is based on a study of the entire world map, looking for */
X/* duties and opportunities. */
X
Xmake_strategy(side)
XSide *side;
X{
X int u, i, g, x, y, view, etype, x0, x1, x2, y1, y2, choice;
X int pri, sumx = 0, sumy = 0, n = 0;
X Plan *plan = side_plan(side);
X Side *side2, *eside;
X
X for_all_sides(side2) {
X for_all_unit_types(u) {
X plan->estimate[side_number(side2)][u] = 0;
X }
X }
X for (y = 0; y < world.height; ++y) {
X for (x = 0; x < world.width; ++x) {
X view = side_view(side, x, y);
X if (view != EMPTY && view != UNSEEN) {
X if (side == side_n(vside(view))) {
X sumx += x; sumy += y; n++;
X }
X }
X }
X }
X if (n > 0) {
X plan->cx = sumx / n; plan->cy = sumy / n;
X }
X for (y = 0; y < world.height; ++y) {
X for (x = 0; x < world.width; ++x) {
X view = side_view(side, x, y);
X if (view != EMPTY && view != UNSEEN) {
X eside = side_n(vside(view));
X etype = vtype(view);
X if (!allied_side(side, eside)) {
X choice = attack_type(etype);
X if (eside == NULL && choice == HITTARGET) {
X /* uncapturable neutrals are basically dull */
X } else if (!find_group(side, choice, x, y)) {
X pri = 100 - (100 * distance(x, y, plan->cx, plan->cy))
X / world.width;
X form_group(side, choice, pri+1, x, y, 0, etype);
X }
X } else {
X if (!mobile(etype) && !defended(side, x, y)) {
X form_group(side, DEFEND, 1, x, y, 3, NOTHING);
X }
X }
X if (eside) plan->estimate[side_number(eside)][etype]++;
X }
X }
X }
X if (!world.known) {
X if (!find_group(side, EXPLORE, -1, -1)) {
X x0 = 0 + random(world.width/3);
X x1 = world.width/3 + random(world.width/3);
X x2 = (2*world.width)/3 + random(world.width/3);
X y1 = 0 + random(world.height/3);
X y2 = (2*world.height)/3 + random(world.height/3);
X form_group(side, EXPLORE, 1, x0, y1, 3, NOTHING);
X form_group(side, EXPLORE, 1, x1, y1, 3, NOTHING);
X form_group(side, EXPLORE, 1, x2, y1, 3, NOTHING);
X form_group(side, EXPLORE, 1, x0, y2, 3, NOTHING);
X form_group(side, EXPLORE, 1, x1, y2, 3, NOTHING);
X form_group(side, EXPLORE, 1, x2, y2, 3, NOTHING);
X }
X }
X /* should form a hex occupation group if hex mentioned in win/lose */
X}
X
X/* Decides if unit has nothing covering it. */
X
Xdefended(side, x, y)
XSide *side;
Xint x, y;
X{
X int g;
X Plan *plan = side_plan(side);
X
X for (g = 1; g < MAXGROUPS; ++g) {
X if ((plan->group[g].goal == DEFEND) &&
X (distance(x, y, plan->group[g].x, plan->group[g].y) <=
X plan->group[g].area))
X return TRUE;
X }
X return FALSE;
X}
X
X/* Review existing groups and get rid of useless ones. Start by recomputing */
X/* the members, since we don't update when units die or get transferred. */
X/* (Since at beginning of turn, all units known to be alive.) */
X
Xreview_groups(side)
XSide *side;
X{
X int g, u, u2, e, view, ideal;
X Plan *plan = side_plan(side);
X Group *group;
X Unit *unit;
X
X for_all_unit_types(u) {
X plan->demand[u] = 0;
X }
X for (g = 1; g < MAXGROUPS; ++g) {
X for_all_unit_types(u) {
X plan->group[g].member[u] = 0;
X plan->group[g].need[u] = 0;
X }
X }
X for_all_units(unit) {
X if (unit->side == side) plan->group[unit->group].member[unit->type]++;
X }
X for (g = 1; g < MAXGROUPS; ++g) {
X group = &(plan->group[g]);
X switch (group->goal) {
X case NOGROUP:
X /* a non-existent group */
X break;
X case HITTARGET:
X view = side_view(side, group->x, group->y);
X if (view == EMPTY || view == UNSEEN ||
X side_n(vside(view)) == NULL ||
X allied_side(side, side_n(vside(view)))) {
X disband_group(side, g);
X } else {
X for_all_unit_types(u) {
X if (could_hit(u, group->etype)) {
X e = group->etype;
X if (utypes[u].damage[e] > 0) {
X ideal = (2 * utypes[e].hp * utypes[u].hit[e]) /
X utypes[u].damage[e];
X } else {
X ideal = 0;
X }
X group->need[u] = ideal - group->member[u];
X for_all_unit_types(u2) {
X if (could_carry(u2, u)) {
X ideal = 1;
X group->need[u2] = ideal - group->member[u2];
X }
X }
X }
X }
X }
X break;
X case CAPTARGET:
X view = side_view(side, group->x, group->y);
X if (view == EMPTY || view == UNSEEN ||
X allied_side(side, side_n(vside(view)))) {
X disband_group(side, g);
X } else {
X for_all_unit_types(u) {
X if (could_capture(u, group->etype)) {
X ideal = 200 / utypes[u].capture[group->etype];
X group->need[u] = ideal - group->member[u];
X for_all_unit_types(u2) {
X if (could_carry(u2, u)) {
X ideal = 1;
X group->need[u2] = ideal - group->member[u2];
X }
X }
X }
X }
X }
X break;
X case EXPLORE:
X view = side_view(side, group->x, group->y);
X if (view != UNSEEN) {
X disband_group(side, g);
X } else {
X for_all_unit_types(u) {
X if (mobile(u)) ideal = 3;
X group->need[u] = ideal - group->member[u];
X for_all_unit_types(u2) {
X if (could_carry(u2, u)) {
X ideal = 1;
X group->need[u2] = ideal - group->member[u2];
X }
X }
X }
X }
X break;
X case DEFEND:
X for_all_unit_types(u) {
X ideal = 3;
X group->need[u] = ideal - group->member[u];
X }
X break;
X case OCCUPYHEX:
X /* occupying should only end if no longer a victory condition */
X break;
X default:
X case_panic("group goal", group->goal);
X break;
X }
X }
X for (g = 1; g < MAXGROUPS; ++g) {
X for_all_unit_types(u) {
X plan->demand[u] = group->priority * group->need[u];
X }
X }
X}
X
X/* Sometimes there is no point in going on, but be careful not to be too */
X/* pessimistic. Right now we only give up if no hope at all. */
X
Xdecide_resignation(side)
XSide *side;
X{
X int u, u2, sn1, inrunning = FALSE, opposed, own, odds, chance = 0;
X Side *side1, *side2;
X Plan *plan = side_plan(side);
X
X for_all_sides(side1) {
X sn1 = side_number(side1);
X for_all_unit_types(u) {
X plan->allies[sn1][u] = plan->estimate[sn1][u];
X for_all_sides(side2) {
X if (side1 != side2 && allied_side(side1, side2)) {
X plan->allies[sn1][u] +=
X plan->estimate[side_number(side2)][u];
X }
X }
X }
X }
X if (global.numconds == 0) {
X for_all_unit_types(u) {
X own = plan->allies[side_number(side)][u];
X for_all_unit_types(u2) {
X if (could_make(u, u2) && mobile(u2))
X inrunning = TRUE;
X for_all_sides(side1) {
X if (enemy_side(side, side1)) {
X opposed = plan->allies[side_number(side1)][u2];
X if (own > 0 && opposed > 0) {
X if (could_capture(u, u2) && mobile(u))
X inrunning = TRUE;
X if (could_hit(u, u2) && mobile(u))
X inrunning = TRUE;
X }
X }
X }
X }
X }
X /* should use chance for doubtful situations, like relative strength */
X if (!inrunning || probability(chance)) resign_game(side, NULL);
X } else {
X /* could get pretty complicated... */
X }
X}
X
X/* When forming a group, first pick out an unused group, then bump a lower */
X/* priority group if there's too many. If it's of lower or equal priority, */
X/* then don't form the group at all (failure on equal priorities reduces */
X/* fickleness). */
X
Xform_group(side, goal, priority, x, y, area, etype)
XSide *side;
Xint goal, priority, x, y, area, etype;
X{
X int g, u;
X Plan *plan = side_plan(side);
X
X for (g = 1; g < MAXGROUPS; ++g) {
X if (plan->group[g].goal == NOGROUP) break;
X }
X if (g == MAXGROUPS) {
X for (g = 1; g < MAXGROUPS; ++g) {
X if (priority > plan->group[g].priority) {
X disband_group(side, g);
X break;
X }
X }
X }
X if (g < MAXGROUPS) {
X plan->group[g].goal = goal;
X plan->group[g].priority = priority;
X plan->group[g].x = x;
X plan->group[g].y = y;
X plan->group[g].area = area;
X plan->group[g].etype = etype;
X for_all_unit_types(u) {
X plan->group[g].member[u] = 0;
X plan->group[g].need[u] = 0;
X }
X if (Debug) printf("%d: s%d form %s\n", global.time,
X side_number(side), group_desig(plan, g));
X return g;
X } else {
X return 0;
X }
X}
X
X/* When group's goal accomplished, release the units for other activities. */
X/* Not very efficient to scan all units, but simpler and safer than links. */
X/* (All units are known to be alive here.) */
X
Xdisband_group(side, g)
XSide *side;
Xint g;
X{
X Unit *unit;
X Plan *plan = side_plan(side);
X
X if (Debug) printf("%d: s%d disband %s\n", global.time,
X side_number(side), group_desig(plan, g));
X plan->group[g].goal = NOGROUP;
X plan->group[g].priority = 0;
X for_all_units(unit) {
X if (unit->side == side && unit->group == g) {
X unit->group = NOGROUP;
X unit->goal = NOGOAL;
X if (Debug) printf("%d: %s released from group %d\n",
X global.time, unit_desig(unit), g);
X }
X }
X}
X
X/* Given a goal and argument, see if a group already exists like that. */
X/* (-1 values serve as unbound variables.) */
X
Xfind_group(side, goal, x, y)
XSide *side;
Xint goal, x, y;
X{
X int g;
X
X for (g = 1; g < MAXGROUPS; ++g) {
X if ((side_plan(side)->group[g].goal == goal) &&
X (x == -1 || side_plan(side)->group[g].x == x) &&
X (y == -1 || side_plan(side)->group[g].y == y))
X return g;
X }
X return 0;
X}
X
X/* Decide whether a change of product is desirable. */
X
Xchange_machine_product(unit)
XUnit *unit;
X{
X int u = unit->type;
X
X if (Freeze) {
X return FALSE;
X } else if (utypes[u].maker) {
X if (producing(unit)) {
X if ((unit->built > 5) ||
X ((utypes[u].make[unit->product] * unit->built) > 50)) {
X return TRUE;
X }
X } else {
X return TRUE;
X }
X }
X return FALSE;
X}
X
X/* Machine algorithm for deciding what a unit should build. This routine */
X/* must return the type of unit decided upon. Variety of production is */
X/* important, as is favoring types which can leave the builder other than */
X/* on a transport. Capturers of valuable units are also highly preferable. */
X
Xmachine_product(unit)
XUnit *unit;
X{
X int u = unit->type, u2, type, t, d, x, y, value, bestvalue, besttype, tmp;
X int adjterr[MAXTTYPES];
X
X mside = unit->side;
X for_all_terrain_types(t) adjterr[t] = 0;
X for_all_directions(d) {
X x = wrap(unit->x + dirx[d]); y = unit->y + diry[d];
X adjterr[terrain_at(x, y)]++;
X }
X besttype = period.firstptype;
X bestvalue = 0;
X tmp = FALSE;
X for_all_unit_types(u2) {
X if (could_make(u, u2)) {
X value = side_plan(mside)->demand[u2];
X if (mobile(u2)) {
X for_all_terrain_types(t) {
X if (could_move(u2, t)) {
X value += adjterr[t] * fraction[t];
X tmp = TRUE;
X }
X }
X }
X if (mside->building[u2] > 0) value /= (mside->building[u2] + 1);
X /* might want to adjust by number of existing units? */
X value = (value * (100 - build_time(unit, u2))) / 100;
X if (tmp && value > bestvalue) {
X besttype = u2;
X bestvalue = value;
X }
X }
X }
X type = besttype;
X /* safety check */
X if (!could_make(unit->type, type)) type = NOTHING;
X if (Debug) printf("%d: %s will now build %s units\n",
X global.time, unit_desig(unit),
X (type == NOTHING ? "no" : utypes[type].name));
X return type;
X}
X
X/* Decide on and make a move or set orders for a machine player. */
X
Xmachine_move(unit)
XUnit *unit;
X{
X munit = unit;
X mside = unit->side;
X if (Freeze) {
X order_sentry(unit, 1);
X } else if (humanside(mside)) {
X unit->goal = DRIFT;
X if (maybe_return_home(unit)) return;
X if (probability(50) && short_term(unit)) return;
X search_for_best_move(unit);
X } else {
X if (unit->group == NOGROUP) decide_group(unit);
X if (unit->goal == NOGOAL) decide_goal(unit);
X if (maybe_return_home(unit)) return;
X if (probability(50) && short_term(unit)) return;
X search_for_best_move(unit);
X }
X}
X
X/* Picking the correct units for a group is essential to its success. */
X/* We rate the unit for its suitability for each group based on the needs */
X/* of the group and the capabilities and proximity of the unit. */
X
Xdecide_group(unit)
XUnit *unit;
X{
X int g, u = unit->type, t, suitability, best = 0, bestgroup = 0, dist;
X Plan *plan = side_plan(unit->side);
X
X for (g = 1; g < MAXGROUPS; ++g) {
X suitability = max(0, plan->group[g].need[u]) * plan->group[g].priority;
X switch (plan->group[g].goal) {
X case NOGROUP:
X break;
X case HITTARGET:
X case CAPTARGET:
X dist = distance(unit->x, unit->y,
X plan->group[g].x, plan->group[g].y);
X suitability -= (suitability * dist) / world.width;
X break;
X case EXPLORE:
X suitability = 1;
X if (!mobile(unit->type)) suitability = -100;
X break;
X case DEFEND:
X suitability = 1;
X break;
X case OCCUPYHEX:
X /* assign a group capable of reaching the hex */
X break;
X default:
X case_panic("group goal", plan->group[g].goal);
X break;
X }
X if (suitability > best) {
X best = suitability;
X bestgroup = g;
X }
X }
X unit->group = bestgroup;
X unit->goal = NOGOAL;
X plan->group[bestgroup].member[unit->type]++;
X if (Debug) printf("%d: %s assigned to %s\n", global.time,
X unit_desig(unit), group_desig(plan, bestgroup));
X}
X
X/* Set up goals for units that need them. */
X/* Goals should differ according to unit's role in group... */
X
Xdecide_goal(unit)
XUnit *unit;
X{
X int x, y, area;
X Plan *plan = side_plan(unit->side);
X
X x = plan->group[unit->group].x; y = plan->group[unit->group].y;
X switch (plan->group[unit->group].goal) {
X case NOGOAL:
X /* dubious */
X unit->goal = DRIFT;
X unit->gx = unit->gy = 0;
X break;
X case HITTARGET:
X if (could_hit(unit->type, plan->group[unit->group].etype)) {
X unit->goal = HITTARGET;
X } else if (probability(fullness(unit))) {
X unit->goal = APPROACH;
X } else {
X unit->goal = LOAD;
X }
X unit->gx = x; unit->gy = y;
X break;
X case CAPTARGET:
X if (could_capture(unit->type, plan->group[unit->group].etype)) {
X unit->goal = CAPTARGET;
X } else if (probability(fullness(unit))) {
X unit->goal = APPROACH;
X } else {
X unit->goal = LOAD;
X }
X unit->gx = x; unit->gy = y;
X break;
X case EXPLORE:
X unit->goal = APPROACH;
X area = plan->group[unit->group].area;
X unit->gx = x + random(2*area) - area;
X unit->gy = y + random(2*area) - area;
X break;
X case DEFEND:
X unit->goal = DRIFT;
X area = plan->group[unit->group].area;
X unit->gx = x + random(2*area) - area;
X unit->gy = y + random(2*area) - area;
X break;
X case OCCUPYHEX:
X unit->goal = APPROACH;
X unit->gx = x; unit->gy = y;
X break;
X default:
X case_panic("group goal", plan->group[unit->group].goal);
X break;
X }
X if (Debug) printf("%d: %s in %s gets goal %d->%d,%d\n", global.time,
X unit_desig(unit), group_desig(plan, unit->group),
X unit->goal, unit->gx, unit->gy);
X}
X
X/* See if the location has a unit that can take us in for refueling */
X/* (where's the check for refueling ability?) */
X
Xhaven_p(x, y)
Xint x, y;
X{
X Unit *unit = unit_at(x, y);
X
X return ((unit != NULL && mside == unit->side && alive(unit) &&
X can_carry(unit, munit) && !might_be_captured(unit)));
X}
X
X/* See if the location has a unit that can repair us */
X
Xshop_p(x, y)
Xint x, y;
X{
X Unit *unit = unit_at(x, y);
X
X return (unit != NULL && munit->side == unit->side && alive(unit) &&
X can_carry(unit, munit) && could_repair(unit->type, munit->type) &&
X !might_be_captured(unit));
X}
X
X/* See if we're in a bad way, either on supply or hits, and get to safety */
X/* if possible. If not, then move on to other actions. */
X/* Can't be 100% though, there might be some problem preventing move */
X
Xmaybe_return_home(unit)
XUnit *unit;
X{
X int u = unit->type, ux = unit->x, uy = unit->y, ox, oy, range, success;
X
X if (low_supplies(unit) && probability(98)) {
X range = range_left(unit);
X if (Debug) printf("%s should get supplies - ", unit_desig(unit));
X if ((range * range < numunits) ?
X (search_area(ux, uy, range, haven_p, &ox, &oy)) :
X (find_closest_unit(ux, uy, range, haven_p, &ox, &oy))) {
X order_moveto(unit, ox, oy);
X unit->orders.flags |= SHORTESTPATH;
X unit->orders.flags &=
X ~(ENEMYWAKE|NEUTRALWAKE|SUPPLYWAKE|ATTACKUNIT);
X if (Debug) printf("will resupply at %d,%d\n", ox, oy);
X return TRUE;
X } else {
X if (Debug) printf("but can't\n");
X }
X }
X if (cripple(unit) && probability(98)) {
X /* note that crippled units cannot repair themselves */
X if (Debug) printf("%s badly damaged - ", unit_desig(unit));
X if (unit->transport && could_repair(u, unit->transport->type)) {
X if (Debug) printf("%s will repair\n", unit_desig(unit->transport));
X order_sentry(unit, 1);
X return TRUE;
X } else {
X range = range_left(unit);
X if ((range * range < numunits) ?
X (search_area(ux, uy, range, haven_p, &ox, &oy)) :
X (find_closest_unit(ux, uy, range, shop_p, &ox, &oy))) {
X order_moveto(unit, ox, oy);
X unit->orders.flags &= ~SHORTESTPATH;
X unit->orders.flags &=
X ~(ENEMYWAKE|NEUTRALWAKE|SUPPLYWAKE|ATTACKUNIT);
X if (Debug) printf("will repair at %d,%d\n", ox, oy);
X return TRUE;
X } else {
X if (Debug) printf("but no place to repair\n");
X }
X }
X }
X if (out_of_ammo(unit) >= 0 && probability(80)) {
X if (Debug) printf("%s should reload - ", unit_desig(unit));
X range = range_left(unit);
X if ((range * range < numunits) ?
X (search_area(ux, uy, range, haven_p, &ox, &oy)) :
X (find_closest_unit(ux, uy, range, haven_p, &ox, &oy))) {
X order_moveto(unit, ox, oy);
X unit->orders.flags &= ~SHORTESTPATH;
X unit->orders.flags &=
X ~(ENEMYWAKE|NEUTRALWAKE|SUPPLYWAKE|ATTACKUNIT);
X if (Debug) printf("will go to %d,%d\n", ox, oy);
X return TRUE;
X } else {
X if (Debug) printf("but can't\n");
X }
X }
X return FALSE;
X}
X
X/* Return the distance that we can go by shortest path before running out */
X/* of important supplies. Will return at least 1, since we can *always* */
X/* move one hex to safety. This is a worst-case routine, too complicated */
X/* to worry about units getting refreshed by terrain or whatever. */
X
Xrange_left(unit)
XUnit *unit;
X{
X int u = unit->type, r, least = 12345;
X
X for_all_resource_types(r) {
X if (utypes[u].tomove[r] > 0) least = min(least, unit->supply[r]);
X if (utypes[u].consume[r] > 0)
X least = min(least, unit->supply[r] / utypes[u].consume[r]);
X }
X return (least == 12345 ? 1 : least);
X}
X
X/* Do short-range planning. Only thing here is intended to be for defenders */
X/* protecting a small area (5 moves is arb, should derive from defense */
X/* group area). */
X
Xshort_term(unit)
XUnit *unit;
X{
X int u = unit->type, ux = unit->x, uy = unit->y, range;
X
X switch (unit->goal) {
X case DRIFT:
X range = min(10, 5 * utypes[u].speed);
X if (probability(90)) {
X bestworth = -10000;
X apply_to_area(ux, uy, range, evaluate_hex);
X apply_to_area(ux, uy, range, maximize_worth);
X if (bestworth >= 0) {
X if (Debug) printf("drifting to %d,%d (worth %d)\n",
X bestx, besty, bestworth);
X order_moveto(unit, bestx, besty);
X unit->orders.flags &= ~SHORTESTPATH;
X return TRUE;
X }
X }
X break;
X case LOAD:
X case APPROACH:
X case HITTARGET:
X case CAPTARGET:
X break;
X default:
X case_panic("unit goal", munit->goal);
X break;
X }
X return FALSE;
X}
X
X/* Search for most favorable odds anywhere in the area, but only for */
X/* the remaining moves in this turn. Multi-turn tactics is elsewhere. */
X
Xsearch_for_best_move(unit)
XUnit *unit;
X{
X int ux = unit->x, uy = unit->y, range = unit->movesleft, goal;
X
X if (!mobile(unit->type)) {
X order_sentry(unit, 100);
X return;
X }
X if (Debug) printf("%d: %s ", global.time, unit_desig(unit));
X bestworth = -10000;
X apply_to_area(ux, uy, range, evaluate_hex);
X apply_to_area(ux, uy, range, maximize_worth);
X if (bestworth >= 0) {
X if (unit->transport != NULL && mobile(unit->transport->type)) {
X if (Debug) printf("sleeping on transport\n");
X order_sentry(unit, 5);
X } else if ((ux == bestx && uy == besty) || !can_move(unit)) {
X if (Debug) printf("staying put\n");
X order_sentry(unit, 1);
X } else if (probability(90)) {
X if (Debug) printf("moving to %d,%d (worth %d)\n",
X bestx, besty, bestworth);
X order_moveto(unit, bestx, besty);
X unit->orders.flags &= ~SHORTESTPATH;
X } else {
X if (Debug) printf("hanging around\n");
X order_sentry(unit, random(5));
X }
X } else {
X goal = unit->goal;
X /* jam alternative sometimes... */
X if (probability(95)) goal = DRIFT;
X switch (goal) {
X case DRIFT:
X if (can_produce(unit) && unit->transport == NULL &&
X probability(90)) {
X if (Debug) printf("going to build something\n");
X set_product(unit, machine_product(unit));
X set_schedule(unit);
X order_sentry(unit, unit->schedule+1);
X } else if (probability(90)) {
X if (Debug) printf("going in random direction\n");
X order_movedir(unit, random_dir(), random(3)+1);
X } else {
X if (Debug) printf("hanging around\n");
X order_sentry(unit, random(4)+1);
X }
X break;
X case LOAD:
X if (unit->occupant != NULL) {
X if (Debug) printf("starting off to goal\n");
X unit->goal = APPROACH;
X order_moveto(unit, unit->gx, unit->gy);
X } else {
X if (bestworth >= 0) {
X if (Debug) printf("loading at %d,%d (worth %d)\n",
X bestworth, bestx, besty);
X order_moveto(unit, bestx, besty);
X unit->orders.flags &= ~SHORTESTPATH;
X } else {
X if (Debug) printf("moving slowly about\n");
X order_movedir(unit, random_dir(), 1);
X }
X }
X break;
X case APPROACH:
X case HITTARGET:
X case CAPTARGET:
X if (unit->transport != NULL) {
X if (unit->transport->group == unit->group) {
X if (Debug) printf("riding in transport\n");
X order_sentry(unit, 4);
X } else if (!can_move(unit)) {
X if (Debug) printf("waiting to get off\n");
X order_sentry(unit, 2);
X } else {
X if (Debug) printf("leaving for %d,%d\n",
X unit->gx, unit->gy);
X order_moveto(unit, unit->gx, unit->gy);
X }
X } else {
X if (Debug) printf("approaching %d,%d\n", unit->gx, unit->gy);
X order_moveto(unit, unit->gx, unit->gy);
X }
X break;
X default:
X case_panic("unit goal", munit->goal);
X break;
X }
X }
X}
X
X/* Given a position nearby the unit, evaluate it with respect to goals, */
X/* general characteristics, and so forth. -10000 is very bad, 0 is OK, */
X/* 10000 or so is best possible. */
X
X/* Should downrate hexes within reach of enemy retaliation. */
X/* Should downrate hexes requiring supply consumption to enter/occupy. */
X
Xevaluate_hex(x, y)
Xint x, y;
X{
X bool adjhex, ownhex;
X int view, etype, dist, worth = 0;
X int terr = terrain_at(x, y);
X Side *es;
X Unit *eunit;
X
X view = side_view(mside, x, y);
X dist = distance(munit->x, munit->y, x, y);
X adjhex = (dist == 1);
X ownhex = (dist == 0);
X
X if (y <= 0 || y >= world.height-1) {
X worth = -10000;
X } else {
X switch (munit->goal) {
X case DRIFT:
X if (ownhex) {
X worth = -1;
X } else if (view == UNSEEN) {
X worth = random(100) / dist;
X } else if (view == EMPTY) {
X worth = -100;
X if (impassable(munit, x, y)) worth -= 900;
X } else {
X es = side_n(vside(view));
X etype = vtype(view);
X if (es == NULL) {
X if (could_capture(munit->type, etype)) {
X worth = 20000 / dist;
X } else {
X worth = -10000;
X }
X } else if (!allied_side(mside, es)) {
X worth = 200 + attack_worth(munit, etype);
X worth += threat(mside, etype, x, y);
X worth /= dist;
X } else {
X worth = 0;
X }
X }
X break;
X case LOAD:
X if (ownhex || view == UNSEEN || view == EMPTY) {
X worth = -1;
X } else {
X es = side_n(vside(view));
X if (mside == es) {
X if ((eunit = unit_at(x, y)) != NULL) {
X if (eunit->group == munit->group) {
X worth = 4000;
X worth /= dist;
X }
X }
X } else {
X worth = -100;
X }
X }
X break;
X case APPROACH:
X case HITTARGET:
X case CAPTARGET:
X if (ownhex) {
X worth = -100;
X } else if (view == UNSEEN) {
X worth = random(100) / dist;
X } else if (view == EMPTY) {
X if (impassable(munit, x, y)) worth -= 900;
X } else if (x == munit->gx && y == munit->gy) {
X worth = 10000;
X } else {
X es = side_n(vside(view));
X etype = vtype(view);
X if (es == NULL) {
X if (could_capture(munit->type, etype)) {
X worth = 20000 / dist;
X } else {
X worth = -10000;
X }
X } else if (!allied_side(mside, es)) {
X worth = 200 + attack_worth(munit, etype);
X worth += threat(mside, etype, x, y);
X worth /= dist;
X } else {
X es = side_n(vside(view));
X if (mside == es) {
X if ((eunit = unit_at(x, y)) != NULL) {
X if (eunit->group == munit->group &&
X eunit->goal == LOAD &&
X could_carry(eunit->type, munit->type)) {
X worth = 4000;
X worth /= dist;
X }
X }
X } else {
X worth = -100;
X }
X }
X }
X break;
X default:
X case_panic("unit goal", munit->goal);
X break;
X }
X }
X if ((munit->gx > 0 || munit->gy > 0) &&
X (distance(x, y, munit->gx, munit->gy) <
X distance(munit->x, munit->y, munit->gx, munit->gy))) {
X worth += 1000;
X }
X worth -= 100;
X worth += utypes[munit->type].productivity[terr];
X aset(localworth, x, y, worth);
X}
X
X/* Scan evaluated area looking for best overall hex. */
X
Xmaximize_worth(x, y)
Xint x, y;
X{
X int worth;
X
X worth = aref(localworth, x, y);
X if (worth >= 0) {
X if (worth > bestworth) {
X bestworth = worth; bestx = x; besty = y;
X } else if (worth == bestworth && flip_coin()) {
X bestworth = worth; bestx = x; besty = y;
X }
X }
X}
X
X/* This is a heuristic estimation of the value of one unit type hitting */
X/* on another. Should take cost of production into account as well as the */
X/* chance and significance of any effect. */
X
Xattack_worth(unit, etype)
XUnit *unit;
Xint etype;
X{
X int utype = unit->type, worth;
X
X worth = bhw[utype][etype];
X if (utypes[utype].damage[etype] >= utypes[etype].hp)
X worth *= 2;
X if (utypes[etype].damage[utype] >= unit->hp)
X worth /= (could_capture(utype, etype) ? 1 : 4);
X if (could_capture(utype, etype)) worth *= 4;
X return worth;
X}
X
X
X/* Support functions. */
X
X/* True if unit is in immediate danger of being captured. */
X/* Needs check on capturer transport being seen. */
X
Xmight_be_captured(unit)
XUnit *unit;
X{
X int d, x, y;
X Unit *unit2;
X
X for_all_directions(d) {
X x = wrap(unit->x + dirx[d]); y = unit->y + diry[d];
X if (((unit2 = unit_at(x, y)) != NULL) &&
X (enemy_side(unit->side, unit2->side)) &&
X (could_capture(unit2->type, unit->type))) return TRUE;
X }
X return FALSE;
X}
X
X/* Return true if the given unit type at given position is threatened. */
X
Xthreat(side, u, x0, y0)
XSide *side;
Xint u, x0, y0;
X{
X int d, x, y, view, thr = 0;
X Side *side2;
X
X for_all_directions(d) {
X x = wrap(x0 + dirx[d]); y = y0 + diry[d];
X view = side_view(side, x, y);
X if (view != UNSEEN && view != EMPTY) {
X side2 = side_n(vside(view));
X if (allied_side(side, side2)) {
X if (could_capture(u, vtype(view))) thr += 1000;
X if (bhw[u][vtype(view)] > 0) thr += 100;
X }
X }
X }
X return thr;
X}
X
X/* Test if unit can move out into adjacent hexes. */
X
Xcan_move(unit)
XUnit *unit;
X{
X int d, x, y;
X
X for_all_directions(d) {
X x = wrap(unit->x + dirx[d]); y = limit(unit->y + diry[d]);
X if (could_move(unit->type, terrain_at(x, y))) return TRUE;
X }
X return FALSE;
X}
X
X/* Returns the type of missing supplies. */
X
Xout_of_ammo(unit)
XUnit *unit;
X{
X int u = unit->type, r;
X
X for_all_resource_types(r) {
X if (utypes[u].hitswith[r] > 0 && unit->supply[r] <= 0)
X return r;
X }
X return (-1);
X}
X
X/* Returns the type of attack to plan for. (Should balance relative */
X/* effectiveness of each type of attack.) */
X
Xattack_type(e)
Xint e;
X{
X int u;
X
X if (utypes[e].surrender > 0 || utypes[e].siege > 0) return BESIEGE;
X for_all_unit_types(u) if (could_capture(u, e)) return CAPTARGET;
X return HITTARGET;
X}
X
X/* True if the given unit is a sort that can build other units. */
X
Xcan_produce(unit)
XUnit *unit;
X{
X int p;
X
X for_all_unit_types(p) {
X if (could_make(unit->type, p)) return TRUE;
X }
X return FALSE;
X}
X
X/* Return percentage of capacity. */
X
Xfullness(unit)
XUnit *unit;
X{
X int u = unit->type, o, cap = 0, num = 0, vol = 0;
X Unit *occ;
X
X for_all_unit_types(o) cap += utypes[u].capacity[o];
X for_all_occupants(unit, occ) {
X num++;
X vol += utypes[occ->type].volume;
X }
X if (utypes[u].holdvolume > 0) {
X return ((100 * vol) / utypes[u].holdvolume);
X } else if (cap > 0) {
X return ((100 * num) / cap);
X } else {
X fprintf(stderr, "Fullness ???\n");
X }
X}
X
Xfind_closest_unit(x0, y0, maxdist, pred, rxp, ryp)
Xint x0, y0, maxdist, (*pred)(), *rxp, *ryp;
X{
X Unit *unit;
X
X for_all_units(unit) {
X if (alive(unit) && distance(x0, y0, unit->x, unit->y) <= maxdist) {
X if ((*pred)(unit->x, unit->y)) {
X *rxp = unit->x; *ryp = unit->y;
X return TRUE;
X }
X }
X }
X return FALSE;
X}
END_OF_mplay.c
if test 35452 -ne `wc -c <mplay.c`; then
echo shar: \"mplay.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of archive 1 \(of 2\).
cp /dev/null ark1isdone
MISSING=""
for I in 1 2 ; do
if test ! -f ark${I}isdone ; then
MISSING="${MISSING} ${I}"
fi
done
if test "${MISSING}" = "" ; then
echo You have unpacked both archives.
rm -f ark[1-9]isdone
else
echo You still need to unpack the following archives:
echo " " ${MISSING}
fi
## End of shell archive.
exit 0
More information about the Comp.sources.bugs
mailing list