billr@saab.CNA.TEK.COM (Bill Randle) (02/07/91)
Submitted-by: Tom Boutell <boutell@freezer.it.udel.edu> Posting-number: Volume 12, Issue 23 Archive-name: bt2/Part02 Supersedes: bt: Volume 11, Issue 75-77 Environment: INET sockets, curses #! /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 2 (of 2)." # Contents: bt.doc interface.c robot.c # Wrapped by billr@saab on Wed Feb 6 17:28:32 1991 PATH=/bin:/usr/bin:/usr/ucb ; export PATH if test -f 'bt.doc' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'bt.doc'\" else echo shar: Extracting \"'bt.doc'\" \(13945 characters\) sed "s/^X//" >'bt.doc' <<'END_OF_FILE' XThe Broken Throne X XA multiplayer, realtime game of conquest for the Unix operating system X XBy Tom Boutell, 11/14/90 XLatest Revision (1.11) 2/4/91 X XPermission granted to freely copy and alter the code, provided that new Xversions are cleared through me before release. I will be acting as a Xsource of information, and would appreciate copies of all changes. X XFIRST X XSee the readme file for instructions on building the game! This file Xcovers the rules of the game, not installation. X XTHE STORY X XEmperor Lettuce Prei of Squaria has died, leaving no heir. The nobles Xof the land scramble to reunite the pieces of the Broken Throne; do it Xfirst and become the next Emperor! X XABOUT THE GAME X XBroken Throne is a strategic wargame which runs in "real time." Every Xfew seconds, "action points" are transmitted to the players, which allow Xthem to do things- recruiting troops, moving troops, attacking, and Xconstructing and destroying cities. The object is to seize control of Xall of your opponents' cities; when a player controls no cities, that Xplayer is out of the game. Gameplay is quite rapid, and the map is Xcontinuously updated on the screen. All that is required to play is Xan ordinary terminal with curses screen control; any terminal that can Xrun a visual editor (like vi or emacs) will do fine. If the terminal Xis especially large, the game will take advantage of this, displaying Xmore space between locations and adding extra lines to the output window. XThis is handy if you are running Xwindows. X XThe map of Broken Throne is 16x16, consisting of square "hexes." Each Xhex may contain one of several types of terrain: X X. Plains XT Town XM Mountains (passable) XF Forest XC City XS Swamp X^ Crags (impassable mountains) X XOn the screen, a hex is displayed like this: X XaC05 X XWhere the lowercase letter is the letter of the player who controls the Xhex, the capital letter (or . or ^ ) is the terrain type, and the number Xfollowing is the number of troops. Unowned territories have no lowercase Xletter, and territories without troops have no following number. X XSTARTING OUT X XTo launch the game, someone must launch the server program. This is done Xby typing btserver #ofplayers &, where #ofplayers is replaced by the Xactual number of combatants and the & sign puts the server in the background. XPutting the server in the background is important because it allows the Xplayer who runs the server to play in the game as well. X XThen, the individual players each need to launch the client program. This Xis done by typing btclient addressOfServer, where addressOfServer is the XInternet address of the machine on which the server is being run. X XFor instance, in our local network, bt resides in a subdirectory of my Xown directory, so a player types: X Xcd ~boutell/bt <-- Of course this should be replaced by the path of your X *own* Broken Throne directory. Xbtserver 2 & ... To launch the game for two players, then each X player moves to the bt directory and types: Xbtclient toffee <-- Where "toffee" is the name of the machine the game X is running on. EVEN IF YOU ARE ALL ON THE SAME MACHINE X IT IS STILL NECESSARY TO SPECIFY THE NAME OF THE MACHINE. X ONLY ONE PLAYER LAUNCHES THE SERVER. X XThe player who is running the server should start up his client last, in Xorder to keep his screen from being "munged" as the server announces Xplayers. X XOTHER PORT NUMBERS X XIf you do not want to use the default port (2727), or if there is another XBT game in progress on the same machine, you will need to specify a port Xnumber. Port numbers should be between 1000 and 9999, the same on both Xclient and server. Enter the port number after the number of players Xor machine address, and before the & in the case of the server. X XROBOT PLAYERS X XIf you can't find opponents or want an added challenge, use the robot Xplayer! It's launched just like the client, but plays by itself, so Xput it in background so you can launch your own client and take it on. XFor example: X Xbtrobot toffee & X XIt also accepts port numbers if needed. You can launch as many robots Xas you wish; include them in the player count. The server can't tell them Xfrom human players. X XIMPORTANT NOTE ON USING ROBOTS: If all human players leave a game in which Xrobots are still playing, they'll keep going, and they never quit! So Xthe person who launched them should type "jobs" to list the programs Xrunning, and kill each robot: "kill %n" where n was the listed job Xnumber of the robot. When all the robots are dead, the server will say X"Ending," letting you know that the game is completely over. X XThe source is in the file robot.c. Have fun modifying it; it plays well Xearly on but has a rotten endgame, or rather, *no* real endgame. It's Xintended to be a jumping- off point for other programmers. X XONCE THE GAME STARTS: X XOnce the client is launched, a blank map will be displayed. Within a moment Xor two it should be replaced by the full map; be patient if there are Xseveral players. X XWhen the game first begins, the player is told which lowercase letter Xrepresents them. Initially the player controls one city and no troops. XIn order to acquire troops, the r (recruit) command should be used Ximmediately. A cursor will appear at the city's location. This cursor Xcan be moved with the standard vi/ moria/ hack movement keys: X X k X | X h-*-l X | X j X XThe space bar is used to actually select a location at which to Xrecruit. Since the cursor starts out at the first city, the player should Xjust tap the space bar after pressing r to recruit. Several troops should Xappear in the city. X XIt is then possible to fan out and claim control of towns. The m (move) Xcommand is used to do this. The player should press m, then move the cursor Xto the source of the troops, initially the first city, and press the space Xbar. Then the cursor should be moved to the destination of the troops, Xwhich can be no more than one hex away; diagonal moves are acceptable. XWhen the space bar is pressed again, the troops will be moved. XOnce a new town is reached, the player can use the r command here to recruit Xmore troops. X XRecruitment can only take place every twenty ticks in any given town or city. XFound in bt.h is the constant _SPEED, which determines how often a "tick" Xtakes place, awarding action points and bringing the next recruitment closer. XThe player can attempt to recruit early, but runs the risk of failure, and Xwill be forced to wait the full time again if recruitment does not succeed. XThe number of troops recruited is a function of population. X XAction points are required to move troops! The player will soon find Xthemselves running low on them while exploring the area. This is because Xmovement in unfamiliar territory costs 4 action points per army moved. XMovement in the player's own territory costs only 2 action points per Xarmy moved. When each tick takes place, the player will receive 2 action Xpoints, plus 1 action point for each city under the player's control. X XAction points are displayed for each player at the bottom of the screen: X XA: 6,13 X XThis denotes that player A has six hexes (territories) and thirteen Xaction points. X XThe cursor can be moved freely while considering what command to enter Xnext. The only time you cannot move the cursor is during the entry of a Xnumber of troops, or a text message to another player. X XCOMBAT X XCombat in the Broken Throne is simple; the player simply moves troops into Xenemy territory, and the game resolves the conflict. Each round of battle, Xthere is a 50% chance the attacker will lose an army, and a 50% chance Xthe defender will; this continues until one side has no more troops. XUsually, however, the odds of combat are modified by the terrain of the Xtwo hexes, as detailed below. If the attacker seizes an opponent's last Xcity, that opponent is out of the game! X XArmies are broken down into units in battle, five units representing Xeach army. This makes the odds of battle somewhat more realistic while Xstill allowing for occasional freak outcomes. X XTERRAIN X XThe different terrain types have different roles in the game. Plains Xhexes (' . ') have no effect on combat, but provide substantial population. XThe number of troops a town or city can recruit depends on the surrounding Xpopulation either controlled by the player or not controlled. X XTown hexes (' T ') are the basic source of troops in the game, since Xrecruitment can only take place here and in cities. They have a strong Xdefensive advantage, 10%, but no offensive advantage. X XMountain hexes (' M ') are difficult to move through. When moving troops Xinto them, some troops will usually be balked by conditions in the passes, Xand the action points will still be spent. However, a player in the mountains Xenjoys a 10% advantage, both defending against attack and when striking Xdown out of the mountains. X XForest hexes (' F ') hide enemy troops from view. A player can only tell Xwho the forest is controlled by; the exact numbers of troops are hidden. XForces in a forest enjoy a 5% advantage when attacking out of the forest. XForces in the forest have a 10% defensive advantage as well. X XCity hexes (' C ') are the strongest hexes in the game, with a 15% defensive Xadvantage, though only a 5% offensive advantage. The player can recruit Xtroops at cities, and cities also provide an extra action point per tick; Xthe more cities the player controls, the more resources they have, and Xthe more quickly they can move armies. Cities can be constructed out of Xtowns, at a cost of typically 90 action points! This value can be set Xin the file bt.h, and is the constant _CONSTRUCTCOST. If a player loses Xcontrol of all cities, that player is out of the game, and their existing Xtroops disband, leaving their territory free for the taking! Although Xbuilding cities is expensive, the action points they provide can quickly Xmake up for the cost. It is very inexpensive to destroy a city, however, Xso they should be built in safe areas. X XSwamp hexes (' S ') are the armpit of Broken Throne! Troops are very Xunhappy here; the swamp will often gobble one army of the advancing Xforce. Swamps have no population, and unless in the player's way, aren't Xworth slogging through. Troops in a swamp are at a 10% DISadvantage, Xboth attacking and defending! X XCrag hexes (' ^ ') are impassable mountains. It is not possible to Xenter these hexes, and so they do not affect combat. X XCOMMAND REFERENCE& SOME NEW COMMANDS X Xc (location): construct a city. This requires, usually, 90 action points, X unless the value has been changed in bt.h before compiling. X Can only be used in towns. Xd (location): destroy a city or town. Usually costs 10 action points. Xq: quit the game. You must confirm this. Xt (player letter) (message): Tell a player something. Type their player X letter (case is not important) after striking t, then your message to X them (one line). The message will appear in their output window. Xt # (message): If you type a '#' sign instead of a letter, the message X will go to all players, including yourself. Xm (location) (location) (# of troops): move troops. The game will prompt X for the locations from which and to which troops are to be moved, then X for the number of troops. Xr (location): recruit troops. See notes above on the rules of recruitment. XM (capitalized): redraw a Messy screen. This is useful after a talk X request messes up your display. X XWINNING& LOSING X XYou win the game when no other players remain. You lose the game if you Xcontrol no cities. If your last city is taken (towns don't count), it Xdoesn't matter how many troops you still have, so be careful to defend Xthem! When a player is eliminated, any remaining troops and territories Xare dissolved and available to the first taker. X XIf you have lost the game, you can continue to watch the game for as long Xas you like, before striking 'q' to actually leave the program. X XIf you win, the game will not automatically halt itself. You need to type X'q' to quit the game as the last player. This allows the game to be launched Xby one person on one terminal to test it out. X XThe player who launched the server should see the message "Ending" after Xthe last player quits. If not, that player should do a ps -aux command Xand look for a lingering copy of the server, then kill that copy of the Xserver. (There have been no problems of this sort in the latest release Xto my knowledge.) X XIF YOU ABORT THE PROGRAM X XIf you control-c a client, the other players can continue, but you will be Xout of the game. You will not be able to see what you type unless you quit Xproperly with the 'q' command. Type 'tset' to repair this. X XRUNNING ON LARGE TERMINALS& UNDER X-WINDOWS X XUnder X- Windows and on other systems providing large terminals, the game Xwill take advantage of the terminal's size to provide more spacing and Xmore room for output messages. The wider and deeper, the better. The game Xwill *not* take notice if you resize the window while it is running, however. XYou probably don't want a window wider than about 120 characters, though, Xsince it becomes difficult to see which locations are next to each other. X XSUNVIEW X XPerformance under Sunview is a mixed thing, apparently. On our somewhat Xolder Sunview, there are problems with Curses that prevent it from running Xproperly, but under Sunview 3.0 I'm told it works fine. X XFINDING OPPONENTS X XI'm starting a Broken Throne contact service! If you want to find opponents, Xwrite me at boutell@freezer.it.udel.edu. The game can be played over the XInternet; I've played decent games against European players. I'll be Xin touch with folks who write me. Bug reports should also go to the above Xaddress. And remember, I'm usually up for a game! (I'm not the best player, Xeither; the best I'm aware of is a friend of mine here who's been playing Xsince the beginning. HE'S the one you should worry about.) X X X END_OF_FILE if test 13945 -ne `wc -c <'bt.doc'`; then echo shar: \"'bt.doc'\" unpacked with wrong size! fi # end of 'bt.doc' fi if test -f 'interface.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'interface.c'\" else echo shar: Extracting \"'interface.c'\" \(9191 characters\) sed "s/^X//" >'interface.c' <<'END_OF_FILE' X X/* interface.c: Socket communications support& medium- level message X * passing functions. Copyright (C) 1990 Tom Boutell on original portions. X * All low- level socket code drawn with appreciation from: X * SOCK.C X * Copyright (C)1989 Dr Evil Laboratories X * This code written by Ray Moody, Roy Riggs, Mitch Adler, X * Bill Burdick, and Steven Grady X * No one makes any guarantees about anything. This file maybe X * freely distributed and modified as long as this header remains intact. X */ X X#include <stdio.h> X#include <sys/types.h> X#include <sys/time.h> X#include <sys/socket.h> X#include <netinet/in.h> X#include <string.h> X#include <ctype.h> X#include <signal.h> X#include <netdb.h> X#include <varargs.h> X X#include "pack.h" X#include "bt.h" X#include "btserver.h" X#include "interface.h" X X#define DEBUG X#undef DEBUG X#ifdef DEBUG X#define debug printf X#else X#define debug 0+ X#define perror 0+ X#endif X X#define SECONDSLIMIT 0L X#define MICROSECONDSLIMIT 1L X X#define LINE_LEN 1024 X#define LOST_CARRIER_MSG "F" X X#define NONE (fd_set *) NULL X#define NEVER (struct timeval *) NULL X#define IGNORE (struct sockaddr *) NULL Xint currplayer; Xfd_set active; Xstruct sockaddr_in sc_in; Xint s; Xchar cur_input[LINE_LEN]; Xchar *curhostname = ""; Xint playerids[20]; Xchar outputline[256]; Xvoid setupinterface(portnumber) X int portnumber; X{ X int current; X init_socket(portnumber); X playertext=(outputline+1); X signal(SIGPIPE,broken); X for (current=1; (current<=totalplayers); current++) { X playerids[current]=new_player(0); X players[current].live=1; X printf("Player %d has joined.\n",current); X outputline[0]=_YOUARE; X outputline[1]=64+current; X outputline[2]=0; X w_p(playerids[current],outputline,strlen(&outputline[0])+1); X } X} X Xvoid tellplayer(player) X int player; X{ X outputline[0]=_TEXT; X w_p(playerids[player],outputline,strlen(&outputline[0])+1); X} X Xvoid broadcast(messagetype,details) X char messagetype; X void* details; X{ X location where; X location* wherep; X int thisplayer; X int current; X outputline[0]=messagetype; X switch (messagetype) { X X case _HEXSTATUS: X wherep=(location*) details; X where=*wherep; X outputline[1]=64+where.x; X outputline[2]=64+where.y; X outputline[3]=64+map[where.x][where.y].terrain; X packint(4,map[where.x][where.y].population); X packint(7,map[where.x][where.y].lastuse); X packint(10,map[where.x][where.y].troops); X outputline[13]=map[where.x][where.y].owner+64; X outputline[14]=NULL; X break; X case _LOOK: X where=(*(location*)details); X outputline[1]=64+where.x; X outputline[2]=64+where.y; X outputline[3]=NULL; X break; X case _PLAYERSTATUS: X thisplayer=(*(int*)details); X outputline[1]=thisplayer+64; X packint(2,players[thisplayer].action); X packint(5,players[thisplayer].hexes); X packint(8,players[thisplayer].troops); X packint(11,players[thisplayer].population); X packint(14,players[thisplayer].citadels); X outputline[17]=players[thisplayer].start.x+64; X outputline[18]=players[thisplayer].start.y+64; X outputline[19]=NULL; X break; X case _PLAYERDEAD: X thisplayer=(*(int*)details); X outputline[1]=thisplayer+64; X outputline[2]=NULL; X break; X case _ACTION: X outputline[1]=NULL; X break; X case _STARTUP: X outputline[1]=totalplayers+64; X outputline[2]=NULL; X break; X case _TEXT: X strcpy(&outputline[1],(char*) details); X break; X case _END: X outputline[1]=NULL; X } X for (current=0; (current<=totalplayers); current++) { X if (outputline[0]==_END) { X } X if (players[current].live) { X w_p(playerids[current],outputline,strlen(outputline)+1); X } X } X} X Xint getrequest(thisplayer,requesttype,specific) X int* thisplayer; X char* requesttype; X char* specific; X{ X char* received; X static int sweep; X int oldsweep; X sweep++; X if ((sweep>totalplayers) || (sweep<=0)) X sweep=1; X oldsweep=sweep; X while (players[sweep].live==0) { X sweep++; X if (sweep>totalplayers) X sweep=1; X if (sweep==oldsweep) X return 0; X } X received=read_player(playerids[sweep]); X *thisplayer=sweep; X if (*received==NULL) X return 0; X *requesttype=received[0]; X strcpy(specific,(received+=1)); X return 1; X } X X Xvoid shutdowninterface() { X disconnect_all(); X} X Xvoid broken() { X int count; X for (count=1; (count<=totalplayers); count++) { X if (playerids[count]==currplayer) { X killplayer(count); X players[currplayer].live=0; X break; X } X } X} X X/* - PD Socket code begins here. X * init_socket() - intialize our socket, port is the port number to use X * call this once at the beginning of your code X */ X Xint init_socket(port) X int port; X{ X setbuf(stdout, (char *)0); X if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { X perror("socket"); X return 0; X } X sc_in.sin_family = AF_INET; X sc_in.sin_addr.s_addr = INADDR_ANY; X sc_in.sin_port = htons((u_short) port); X if (bind(s, (struct sockaddr *) &sc_in, sizeof(sc_in)) < 0) { X perror("bind"); X return 0; X } X if (listen(s, 5) < 0) { X perror("listen"); X return 0; X } X X FD_ZERO(&active); X X return 1; X} X X X/* X * disconnect_all() - throws everyone off X */ X Xint disconnect_all() X{ X register int i; X X for (i = 0; i < FD_SETSIZE; i++) { X if (FD_ISSET(i, &active)) X (void) disconnect(i); X } X return 1; X} X X X/* X * shut-down -- kills all connections and exits the pgm X */ X Xint shut_down() X{ X (void) disconnect_all(); X exit(0); X} X X X/* hostfrom() - returns a string containing an ascii name for the host X * that the passed socket s is connected to. Note that the X * string is in static space. If you want to munge with it, X * make a copy. X */ X Xchar *hostfrom(i) X int i; X{ X struct sockaddr_in from; X int fromlen = sizeof(from); X struct hostent *host; X struct in_addr addr; X X if (getpeername(i, &from, &fromlen) < 0) { X perror("getpeername"); X return NULL; X } X X addr = from.sin_addr; X if ((host = gethostbyaddr(&addr, sizeof(addr), AF_INET)) == NULL) { X debug("gethostbyaddr failed"); X return NULL; X } X X return curhostname = host->h_name; X} X X X/* X * hostname() - returns curhostname X */ X Xchar *hostname() X{ X return curhostname; X} X X X/* X * new_player() - call this routine in your main loop to allow new X * players to join. returns a playerId or -1 if no one X * wants to join. X * if wait == 0, then put your process to sleep until X * someone new tries to connect. X */ X Xint new_player(wait) X int wait; /* 0 - wait for activity, else don't wait */ X{ X fd_set readfds; X int j; X struct timeval *pWaitTime, waitTime; X X pWaitTime = &waitTime; X X if (0 == wait) { X pWaitTime = NEVER; X } else { X pWaitTime->tv_sec = SECONDSLIMIT; X pWaitTime->tv_usec = MICROSECONDSLIMIT; X } X X bcopy((char *) &active, (char *) &readfds, sizeof(active)); X X FD_SET(s, &readfds); X X if (select(FD_SETSIZE, &readfds, NONE, NONE, pWaitTime) < 0) { X perror("select"); X return -1; X } X if (FD_ISSET(s, &readfds)) { X if ((j = accept(s, IGNORE, (int *) 0)) < 0) { X return -1; X } X FD_SET(j, &active); X curhostname = hostfrom(j); X debug("Test Host=%s\n", curhostname); X return j; X } else { X return -1; X } X} X X X/* X * disconnect() - drop the player with the given id X */ X Xint disconnect(id) X int id; X{ X if (FD_ISSET(id, &active)) { X debug("** Just dropped %d\n", id); X FD_CLR(id, &active); X if (close(id) < 0) { X perror("close"); X return 0; X } X } else { X debug("** Just tried to drop someone not connected\n"); X return 0; X } X return 1; X} X X X/* X * read_player() - This routine returns the next string from the player X * connected to descriptor playerFd. If there is no X * input it returns the empty string. If the connection X * is lost the string LOST_CARRIER_MSG is returned. X * NOTE: control characters are replaced by spaces X * and it is null terminated at the first nl/cr X */ X Xchar *read_player(playerFd) X int playerFd; X{ X fd_set readfds; X struct timeval waitTime; X X waitTime.tv_sec = SECONDSLIMIT; X waitTime.tv_usec = MICROSECONDSLIMIT; X X bcopy((char *) &active, (char *) &readfds, sizeof(active)); X X if (select(FD_SETSIZE, &readfds, NONE, NONE, &waitTime) < 0) { X perror("select"); X return(""); X } X X if (FD_ISSET(playerFd, &readfds)) { X int nbytes, i; X X nbytes = read(playerFd, cur_input, LINE_LEN); X if (nbytes < 0) { X perror("read"); X disconnect(playerFd); X return(LOST_CARRIER_MSG); X } else if (nbytes == 0) { X disconnect(playerFd); X return(LOST_CARRIER_MSG); X } else { X if (nbytes != LINE_LEN) { X cur_input[nbytes] = '\0'; X } else { X cur_input[LINE_LEN - 1] = '\0'; X } X X return((char *) cur_input); X } X } X return(""); X} X X X/* X * write_player() - write to id, a string with the given length X */ X Xint w_p(id, str, len) X int id, len; X char *str; X{ X if (id > -1) { X currplayer=id; X if (!FD_ISSET(id, &active)) { X debug("** Tried to write to closed id #%d.\n", id); X return 0; X } else if (write(id, str, len) < 0) { X perror("Write"); X return 0; X } X } X return 1; X} X X X X END_OF_FILE if test 9191 -ne `wc -c <'interface.c'`; then echo shar: \"'interface.c'\" unpacked with wrong size! fi # end of 'interface.c' fi if test -f 'robot.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'robot.c'\" else echo shar: Extracting \"'robot.c'\" \(9814 characters\) sed "s/^X//" >'robot.c' <<'END_OF_FILE' X/* Robot player for Broken Throne- 12/90 by Tom Boutell X This robot is basically a hacked client. You can use it as a X starting point for robots of your own! */ X X X#include <stdio.h> X#include <sys/types.h> X#include <sys/socket.h> X#include <netinet/in.h> X#include <netdb.h> X#include <signal.h> X#include <sys/time.h> X#include <fcntl.h> X#include <errno.h> X#include "bt.h" X#include "pack.h" X#include <ctype.h> X#include <math.h> X#define _MAPX 16 X#define _MAPY 16 X X#define _HEXSTATUS 65 X#define _PLAYERSTATUS 66 X#define _PLAYERDEAD 67 X#define _ACTION 68 X#define _TEXT 69 X#define _STARTUP 70 X#define _END 71 X#define _YOUARE 72 X#define _MOVE 65 X#define _RECRUIT 66 X#define _QUIT 67 X#define _PRIVATE 68 X#define _TELLALL 69 X#define _DISCONNECT 70 X#define _CONSTRUCT 71 X#define _DESTROY 72 X X#define SECONDSLIMIT 0L X#define MICROSECONDSLIMIT 1L Xhex map[_MAPX][_MAPY]; Xint lastrecruit[_MAPX][_MAPY]; Xplayer players[20]; Xint playernumber; Xplayer* me; Xint totalplayers; Xint currenttime; Xint living; Xint port; Xint playerFd; Xint promptoffset; Xfd_set server; Xint inputstate; Xchar* host; Xint connectstream(); Xint inputwaiting(); Xvoid setupmap(); Xvoid readnullterm(); Xvoid endprogram(); Xint inrange(); Xint legal(); Xvoid recruit(); Xvoid move(); Xint getmessage(); Xchar specificspace[256]; Xchar outputline[256]; Xchar* specific; X Xmain(argc,argv) X int argc; X char* argv[]; X { X int done; X char messagetype; X location at; X location from,to; X int theplayer; X int x,y,roll; X int xexp,yexp,xnew,ynew; X int xbest,ybest,sbest,sthis; X int tried; X int offset; X int count; X if (argc>1) { X host=argv[1]; X if (argc>2) X port=atoi(argv[2]); X else X port=2727; X } X else { X printf("Usage: btrobot hostaddress portnumber\n"); X exit(1); X } X X if (connectstream()==0) { X perror("Socket unopenable.\n"); X exit(1); X } X setupmap(); X done=0; X currenttime=0; X specific=specificspace; X while (!getmessage(&messagetype,specific)); X playernumber=specific[0]-64; X me=&players[playernumber]; X xexp=0; X yexp=0; X while (!getmessage(&messagetype,specific)); X totalplayers=specific[0]-64; X for (count=1; (count<=totalplayers); count++) X players[count].live=1; X X while (done!=2) { X while (getmessage(&messagetype,specific)) { X offset=0; X switch ((int) messagetype) { X case _HEXSTATUS: X striplocation(&at,specific,&offset); X map[at.x][at.y].terrain=specific[offset]-64; X offset++; X stripint(&map[at.x][at.y].population,specific,&offset); X stripint(&map[at.x][at.y].lastuse,specific,&offset); X stripint(&map[at.x][at.y].troops,specific,&offset); X map[at.x][at.y].owner=specific[offset]-64; X offset++; X break; X case _PLAYERSTATUS: X theplayer=specific[offset]-64; X offset++; X stripint(&players[theplayer].action,specific,&offset); X stripint(&players[theplayer].hexes,specific,&offset); X stripint(&players[theplayer].troops,specific,&offset); X stripint(&players[theplayer].population,specific,&offset); X stripint(&players[theplayer].citadels,specific,&offset); X striplocation(&players[theplayer].start,specific,&offset); X break; X case _PLAYERDEAD: X theplayer=specific[0]-64; X players[theplayer].live=0; X for (y=0; (y<_MAPY); y++) { X for (x=0; (x<_MAPX); x++) { X if (map[x][y].owner==theplayer) { X map[x][y].troops=0; X map[x][y].owner=0; X } X } X } X if (theplayer==playernumber) { X done=2; X } X break; X case _TEXT: X break; X case _ACTION: X for (theplayer=1; (theplayer<=totalplayers); theplayer++) { X if (players[theplayer].live==1) { X players[theplayer].action+=(2+players[theplayer].citadels); X } X } X currenttime++; X break; X case _END: X done=2; X break; X } X } X sleep(1); X /* Now it's time to think. */ X /* Sweep our towns and do useful things: */ X for (y=0; (y<_MAPY); y++) { X for (x=0; (x<_MAPX); x++) { X if (map[x][y].owner==playernumber) { X if ((map[x][y].terrain==2) || (map[x][y].terrain==5)) { X if ((currenttime-lastrecruit[x][y])>20) { X recruit(x,y); X } X } X } X } X } X /* osmose troops around the board. */ X if ((me->action)>5) { X tried=0; X while (tried==0) { X xexp++; X if (xexp==_MAPX) { X xexp=0; X yexp++; X if (yexp==_MAPY) { X yexp=0; X tried=1; X } X } X if ((map[xexp][yexp].owner==playernumber) && (map[xexp][yexp].troops>0)) { X xbest=-1; X ybest=-1; X sbest=-9999; X for (y=-1; (y<=1); y++) { X for (x=-1; (x<=1); x++) { X sthis=0; X if (legal(xexp+x,yexp+y)) { X if (map[xexp+x][yexp+y].terrain==6) X sthis=sthis-50; X if (map[xexp+x][yexp+y].owner!=playernumber) X sthis=sthis+30; X if (map[xexp+x][yexp+y].terrain==2) X sthis=sthis+20; X if (map[xexp+x][yexp+y].terrain==5) X sthis=sthis+25; X if ((sthis>sbest) || X ((sthis==sbest) && (((rand()*512) % 2)==0))) { X xbest=xexp+x; X ybest=yexp+y; X sbest=sthis; X } X } X } X } X xnew=xbest; X ynew=ybest; X if ((xnew!=xexp) || (ynew!=yexp)) { X if ((map[xexp][yexp].terrain!=6) || (((rand()*512) % 10)>7)) { X move(xexp,yexp,xnew,ynew,1); X tried=1; X } X } X } X } X } X } X /* Terminating code - needs to be called on CTRLC also. */ X endprogram(); X} X Xvoid recruit(x,y) Xint x,y; X{ X outputline[0]=_RECRUIT; X outputline[1]=x+64; X outputline[2]=y+64; X outputline[3]=NULL; X write(playerFd,outputline,strlen(outputline)+1); X lastrecruit[x][y]=currenttime; X} X Xvoid move (x1,y1,x2,y2,troops) Xint x1,y1,x2,y2,troops; X{ X outputline[0]=_MOVE; X outputline[1]=x1+64; X outputline[2]=y1+64; X outputline[3]=x2+64; X outputline[4]=y2+64; X packint(5,troops); X outputline[8]=0; X write(playerFd,outputline,strlen(outputline)+1); X} X Xvoid endprogram() { X outputline[0]=_DISCONNECT; X outputline[1]=NULL; X write(playerFd,outputline,strlen(outputline)+1); X close(playerFd); X} X Xint legal(x,y) Xint x,y; X{ X if (inrange(x,0,_MAPX-1) && inrange(y,0,_MAPY-1)) X return 1; X else X return 0; X} X Xint inrange(x,low,high) X int x; X int low; X int high; X{ X if (x<low || x>high) X return 0; X return 1; X} X Xint getmessage(messagetype,specific) X char* messagetype; X char* specific; X{ X char in; X int pos; X *messagetype=0; X *specific=0; X if (!inputwaiting(&server,playerFd)) { X return 0; X } X read(playerFd,messagetype,1); X pos=0; X readnullterm(playerFd,specific); X if (*messagetype==0) X return 0; X else X return 1; X} X Xvoid readnullterm(fd,specific) X int fd; X char* specific; X{ X int done; X char* current; X current=specific; X done=0; X while (done==0) { X while (!inputwaiting(&server,playerFd)); X read(playerFd,current,1); X if (*current==0) X done=1; X current++; X } X} X Xvoid setupmap(x,y) X int x; X int y; X{ X for (y=0; (y<_MAPY); y++) { X for (x=0; (x<_MAPX); x++) { X lastrecruit[x][y]=-100; X map[x][y].terrain=7; X map[x][y].troops=0; X map[x][y].population=0; X map[x][y].owner=0; X map[x][y].lastuse=0; X } X } X} X Xint connectstream() X{ X int s; X struct sockaddr_in saddr; X struct in_addr host_address; X if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) { X perror("socket create"); X return(0); X } X X saddr.sin_family = AF_INET; X if (!get_host_address(host,&host_address)) { X printf("Bad or missing server address.\n"); X exit(1); X } X bcopy(&host_address,&saddr.sin_addr,sizeof(struct in_addr)); X /* saddr.sin_addr.s_addr = htonl(inet_addr(host)); Old method */ X saddr.sin_port = htons(port); X X if (connect(s, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in)) < 0) { X perror("connect"); X return(0); X } X FD_SET(s,&server); X playerFd=s; X return(1); X} X Xint inputwaiting(sfdset,sfd) X fd_set* sfdset; X int sfd; X{ X int errrec; X fd_set readfds; X struct timeval waitTime; X waitTime.tv_sec=SECONDSLIMIT; X waitTime.tv_usec=MICROSECONDSLIMIT; X bcopy((char *) sfdset, (char *) &readfds,sizeof(*sfdset)); X if (select(FD_SETSIZE,&readfds,NULL,NULL,&waitTime)<0) { X if (errno!=EINTR) { X perror("select"); X } X return 0; X } X if (FD_ISSET(sfd,&readfds)) X return 1; X else X return 0; X} X X X/* get_host_address: borrowed with appreciation from Tinytalk. Does a nice X job of getting around the various stupidities of the inetaddr routine, X et cetera. */ X Xint get_host_address(name, addr) /* Get a host address. */ X register char *name; X register struct in_addr *addr; X{ X struct hostent *blob; X union { /* %#@!%!@%#!@ idiot who designed */ X long signed_thingy; /* the inetaddr routine.... */ X unsigned long unsigned_thingy; X } thingy; X X if (*name == '\0') { X fprintf(stderr, "%% No host address specified.\n"); X return (0); X } X X if ((*name >= '0') && (*name <= '9')) { /* IP address. */ X addr->s_addr = inet_addr(name); X thingy.unsigned_thingy = addr->s_addr; X if (thingy.signed_thingy == -1) { X fprintf(stderr, "%% Couldn't find host %s .\n", name); X return (0); X } X } X else { /* Host name. */ X blob = gethostbyname(name); X X if (blob == NULL) { X fprintf(stderr, "%% Couldn't find host %s .\n", name); X return (0); X } X X bcopy(blob->h_addr, addr, sizeof(struct in_addr)); X } X X return (1); /* Success. */ X} X X END_OF_FILE if test 9814 -ne `wc -c <'robot.c'`; then echo shar: \"'robot.c'\" unpacked with wrong size! fi # end of 'robot.c' fi echo shar: End of archive 2 \(of 2\). cp /dev/null ark2isdone 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