Skip to content
Snippets Groups Projects
tw2.js 64.8 KiB
Newer Older
var startup_path='.';
try { throw new Error() } catch(e) { startup_path=e.fileName }
startup_path=startup_path.replace(/[\/\\][^\/\\]*$/,'');
startup_path=backslash(startup_path);

load("recordfile.js");
load("lockfile.js");
load(startup_path+"filename.js");
load(fname("gamesettings.js"));
var Settings=new GameSettings();

load(fname("structs.js"));

var on_at=time();
var i;

var Commodities=[
	{
		 name:"Ore"
		,abbr:"Ore"
		,disp:"Ore......."
		,price:10
	}
	,{
		 name:"Organics"
		,abbr:"Org"
		,disp:"Organics.."
		,price:20
	}
	,{
		 name:"Equipment"
		,abbr:"Equ"
		,disp:"Equipment."
		,price:35
	}
];

var sectors=new RecordFile(fname("sectors.dat"), SectorProperties);
var ports=new RecordFile(fname("ports.dat"), PortProperties);
var players=new RecordFile(fname("players.dat"), PlayerProperties);
var playerLocation=new RecordFile(fname("player-loc.dat"), PlayerLocation);
var teams=new RecordFile(fname("teams.dat"), TeamProperties);
var planets=new RecordFile(fname("planets.dat"), PlanetProperties);
var twmsg=new File(fname("twmesg.dat"));
var twopeng=new File(fname("twopeng.dat"));
twopeng.open("a", true);
twmsg.open("a", true);
var twpmsg=new RecordFile(fname("twpmesg.dat"), PlayerMessageProperties);
var twrmsg=new RecordFile(fname("twrmesg.dat"), RadioMessageProperties);
var cabals=new RecordFile(fname("cabal.dat"), CabalProperties);

var today=system.datestr(system.datestr());

js.on_exit("do_exit()");
/* Run maintenance */
if(Settings.MaintLastRan != system.datestr()) {
	Settings.MaintLastRan = system.datestr();
	Settings.save();
	console.attributes="Y";
	console.writeln("Running maintence program...");
	console.crlf();
	console.crlf();
	console.writeln("Trade Wars maintence program");
	console.writeln("  by Chris Sherrick (PTL)");
	console.crlf();
	console.writeln("This program is run once per day.");
	twmsg.writeln(system.timestr()+": "+user.alias+": Maintence program ran");
	console.writeln("Deleting inactive players");

	var oldest_allowed=today-Settings.DaysInactivity*86400;
	for(i=1; i<players.length; i++) {
		var p=players.Get(i);
		if(p.UserNumber > 0) {
			if((!file_exists(system.data_dir+format("user/%04d.tw2",p.UserNumber))) || (system.datestr(p.LastOnDay) < oldest_allowed && p.KilledBy != 0)) {
				DeletePlayer(p)
			}
		}
	}
	console.crlf();
	MoveCabal();

	RankPlayers();
}

console.attributes="C";
console.crlf();
console.crlf();
console.center("Trade Wars (v.ii)");
console.center("By Chris Sherrick (PTL)");
console.center("Copyright 1986 Chris Sherrick");
console.crlf();
console.center(system.name);
console.center("Sysop  "+system.operator);
console.crlf();
console.crlf();
console.printfile(fname("twopeng.asc"));
if(file_exists(fname("twopeng.dat")))
	console.printfile(fname("twopeng.dat"));
console.crlf();
console.attributes="W";
console.writeln("Initializing...");
console.writeln("Searching my records for your name.");
var player;
var firstnew;
for(i=1; i<players.length; i++) {
	player=players.Get(i);
	if(player.UserNumber == user.number && (!file_exists(system.data_dir+format("user/%04d.tw2",player.UserNumber))))
		DeletePlayer(player);
	if(player.UserNumber==0 && firstnew==undefined)
		firstnew=player;
	if(player.UserNumber == user.number)
		break;
}
if(player==undefined || player.UserNumber!=user.number) {
	console.attributes="G";
	console.writeln("I can't find your record, so I am assuming you are a new player.");
	console.attributes="M";
	console.writeln("Entering a new player...");
	player=firstnew;
	if(player==undefined) {
		console.writeln("I'm sorry but the game is full.");
		console.writeln("Please leave a message for the Sysop so");
		console.writeln("he can save a space for you when one opens up.");
		twmsg.writeln(system.timestr()+": New player not allowed - game full.");
		exit(0);
	}
	console.crlf();
	console.writeln("Notice: If you don't play this game for "+Settings.DaysInactivity+" days, you will");
	console.writeln("be deleted to make room for someone else.");
	console.crlf();
	console.writeln("Your ship is being initialized.");

	/* Lock the player file and mark us online... */
	if(!Lock(players.file.name, bbs.node_num, true, 10)) {
		console.writeln("!!!Unable to lock player data!");
		twmsg.writeln("!!!Unable to lock player data!");
		exit(1);
	}
	player.ReInit();
	player.Online=true;
	player.Put();
	Unlock(players.file.name);

	twmsg.writeln(system.timestr()+" "+user.alias+": New Player logged on");
	Instructions();
}
else {
	/* Lock the player file and mark us online... */
	if(!Lock(players.file.name, bbs.node_num, true, 10)) {
		console.writeln("!!!Unable to lock player data!");
		twmsg.writeln("!!!Unable to lock player data!");
		exit(1);
	}
	player.Online=true;
	player.Put();
	Unlock(players.file.name);

	console.crlf();		/* TODO: BASIC magic... N$ appears empty tw2.bas: 242 */
	twmsg.writeln(system.timestr()+" "+user.alias+": Logged on");
	if(today < system.datestr(player.LastOnDay)) {
		console.writeln("I'm sorry, but you won't be allowed on for another " + parseInt((system.datestr(player.LastOnDay)-today)/86400) + " day(s)");
		exit(0);
	}
	if(today==system.datestr(player.LastOnDay)) {
		if(player.KilledBy != -99) {
			console.writeln("You have been on today.");
			if(player.TurnsLeft<1) {
				console.writeln("You don't have any turns left today. You will be allowed to play tomorrow.");
				exit(0);
			}
			if(player.KilledBy==player.Record) {
				console.writeln("You killed yourself today. You will be allowed to play tomorrow.");
				exit(0);
			}
			console.writeln("The following happened to your ship since your last time on:");
			var count=0;
			for(i=0; i<twpmsg.length; i++) {
				var msg=twpmsg.Get(i);

				if(msg.To==player.Record && !msg.Read) {
					count++;
					if(msg.From==-98)
						console.writeln("A deleted player destroyed "+msg.Destroyed+" fighters.");
					else if(msg.From==-1) {
						console.attributes="R";
						console.writeln("The Cabal destroyed "+msg.Destroyed+" fighters.");
					}
					else {
						var otherplayer=players.Get(msg.From);

						console.writeln(otherplayer.Alias+" "+(otherplayer.TeamNumber?" Team["+otherplayer.TeamNumber+"] ":"")+"destroyed "+msg.Destroyed+" of your fighters.");
					}
					msg.Read=true;
					msg.Put();
				}
			}
			if(count==0)
				console.writeln("Nothing");
		}
	}
	else {
		player.TurnsLeft=Settings.TurnsPerDay;
	}
	player.LastOnDay=system.datestr();
	if(player.KilledBy != 0) {
		switch(player.KilledBy) {
				console.attributes="R";
				console.writeln("You managed to kill yourself on your last time on.");
				break;
			case -1:
				console.attributes="R";
				console.writeln("You have been killed by the Cabal!");
				break;
			case -98:
				console.writeln("You have been killed by a deleted player.");
				break;
			default:
				var otherplayer=players.Get(player.KilledBy);

				console.writeln(otherplayer.Alias+" destroyed your ship!");
				break;
		}
		player.ReInit();
	}
}
file_touch(system.data_dir+format("user/%04d.tw2",player.UserNumber))
ReadRadio();
if(player.Sector < 1 || player.Sector >= sectors.length) {
	console.writeln("You are being moved to sector 1");
	player.Sector=1;
}
location=playerLocation.Get(player.Record);
location.Sector=player.Sector;
location.Put();

if(player.Credits > 25000) {
	console.crlf();
	console.writeln("Tax time! You are being taxed 5000 credits to help support the resistance");
	console.writeln("against the evil Cabal.");
	player.Credits -= 5000;
}

var exit_tw2=false;

while(player.KilledBy==0 && exit_tw2==false) {
	if(EnterSector()) {
		if(CheckSector())
			Menu();
	}
}

function Menu()
{
	/* 22000 */
	while(1) {
		console.crlf();
deuce's avatar
deuce committed
		console.attributes="HC";
		console.write("Command (?=Help)? ");
		var valid=new Array('A','C','D',/*'E' TODO: Editor ,*/'F','G','I','L','M','P','Q','T','Z','?');
		var sector=sectors.Get(player.Sector);
		var i;
		for(i=0; i<sector.Warps.length; i++) {
			if(sector.Warps[i]>0)
				valid.push(sector.Warps[i].toString());
		}
		var inp=InputFunc(valid)
		switch(inp) {
			case '':
				console.writeln("? = Help");
				break;
			case 'A':
				/* 25000 */
				console.writeln("<Attack>");
				var count=0;
				var i;

				if(player.Fighters < 1) {
					console.writeln("You don't have any fighters.");
					break;
				}
				for(i=1;i<players.length; i++) {
					var otherloc=playerLocation.Get(i);
deuce's avatar
deuce committed
					if(otherloc.Sector==player.Sector) {
						var otherplayer=players.Get(i);
						if(otherplayer.Sector==player.Sector
								&& otherplayer.Record!=player.Record
								&& otherplayer.KilledBy!=0
								&& otherplayer.UserNumber!=0
								&& !otherplayer.Online) {
							count++;
							console.write("Attack "+otherplayer.Alias+" (Y/N)[Y]? ");
							if(GetKeyEcho()!='N') {
								console.writeln("<Yes>");
								break;
							}
						}
					}
					otherplayer=null;
				}
				if(otherplayer!=null) {
					DoBattle(otherplayer, otherplayer.TeamNumber > 0);
					if(otherplayer.Fighters==0) {
						KilledBy(otherplayer, player, true);
						/* 15600 player salvages ship from otherplayer */
						var salvaged=parseInt(otherplayer.Holds/4)+1;
						if(player.Holds + salvaged > 50)
							salvaged=50-player.Holds;
						if(salvaged < 1)
							console.writeln("You destroyed the ship, but you can't salvage anything from it");
						else {
							var j;

							console.writeln("You destroyed the ship and salvaged these cargo holds:");
							var holds=new Array(Commodities.length+1);
							for(i=0; i<holds.length+1; i++)
								holds[i]=0;
							for(i=0; i<otherplayer.Holds; i++) {
								var limit=0;
								var r=random(otherplayer.Holds)+1;
								for(j=0; j<Commodities.length; j++) {
									limit += otherplayer.Commodities[j];
									if(r<limit) {
										otherplayer.Commodities[j]--;
										holds[j]++;
										r=0;
										break;
									}
								}
								if(r==0)
									holds[Commodities.length]++;
							}
							if(holds[Commodities.length]>0) {
								console.writeln("  "+holds[Commodities.length]+" empty.");
								player.Holds+=holds[Commodities.length];
							}
							for(j=0; j<Commodities.length; j++) {
								if(holds[j]>0) {
									console.writeln("  "+holds[j]+" with "+Commodities[j].name.toLowerCase()+".");
									player.Holds+=holds[j];
									player.Commodities[j]+=holds[j];
								}
							}
						}						
					}
				}
				else {
					if(count)
						console.writeln("There are no other ships in here");
					else
						console.writeln("There are no ships here to attack.");
				}
				break;
			case 'C':
				/* 33640 */
				console.writeln("<Computer>");
				console.crlf();
deuce's avatar
deuce committed
				console.attributes="HW";
				console.writeln("<Computer activated>");
				var showhelp=true;
				var exitcomputer=false;
				while(!exitcomputer) {
					if(showhelp) {
						console.crlf();
						console.printfile(fname("computer.asc"));
deuce's avatar
deuce committed
					console.attributes="HW";
					console.write("Computer command (?=help)? ");
					switch(InputFunc(['?','1','2','3','4','5'])) {
						case '?':
							showhelp=true;
							break;
						case '1':
							/* 33990 */
							console.crlf();
							console.attributes="Y";
							console.writeln("<Computer deactivated>");
							exitcomputer=true;
							break;
						case '2':
							/* 33780 */
							console.write("What sector number is the port in? ");
							var sec=console.getnum(sectors.length-1);
							if(sec > 0 && sec < sectors.length) {
								var sector=sectors.Get(sec);
								if(sector.Port==0 || (sector.Fighters>0 && sector.Fighters!=player.Record)) {
									console.crlf();
									console.writeln("I have no information about that port.");
									break;
								}
								if(sec==1) {
									SolReport();
								}
								else {
									PortReport(sector);
								}
							}
							break;
						case '3':
							/* 33830 */
							console.write("What sector do you want to get to? ");
							var sec=console.getnum(sectors.length-1);
							if(sec > 0 && sec < sectors.length) {
								if(sec==player.Sector) {
									console.writeln("You are in that sector.");
									break;
								}
								console.crlf();
								console.writeln("Computing shortest path.");
								var path=ShortestPath(player.Sector, sec);
								if(path==null) {
									console.writeln("There was an error in computation between sectors.");
								}
								else {
									console.writeln("The shortest path from sector "+player.Sector+" to sector "+sec+" is:");
									var i;
									for(i in path) {
										if(i!=0) 
											console.write(" - ");
										console.write(path[i]);
									}
								}
							}
							break;
						case '4':
							/* 33900 */
							console.crlf();
							console.write("Ranking Players.");
							var ranked=RankPlayers();
							console.crlf();
deuce's avatar
deuce committed

							console.attributes="W";
							console.writeln("Player Rankings: "+system.timestr());
							console.writeln("Rank     Value      Team  Player");
							console.writeln("==== ============= ====== ======")
							for(rank in ranked) {
								var p=players.Get(ranked[rank].Record);
								console.writeln(format("%4d %13d %-6s %s"
										,rank+1
										,ranked[rank].Score
										,p.TeamNumber==0?"":format("  %-2d  "
										,p.TeamNumber),p.Alias));
							}
							break;
						case '5':
							/* 33960 */
							console.crlf();
							console.writeln("Warming up sub-space radio.");
							mswait(500);
							console.crlf();
							console.write("Who do you wish to send this message to (search string)? ");
							var sendto=console.getstr(42);
							var p=MatchPlayer(sendto);
							if(p==null)
								break;
							console.writeln("Tuning in to " + p.Alias + "'s frequency.");
							console.crlf();
							console.writeln("Due to the distances involved, messages are limited to 74 chars.");
							console.writeln("Pressing [ENTER] with no input quits");
							console.writeln("  [------------------------------------------------------------------------]");
deuce's avatar
deuce committed
							console.attributes="HY";
							console.write("? ");
deuce's avatar
deuce committed
							console.attributes="W";
							var msg=console.getstr(74);
							if(msg==null)
								break;
							msg=msg.replace(/\s*$/,'');
							if(msg=='')
								break;
							RadioMessage(player.Record, p.Record, msg);
							break;
						default:
							console.crlf();
							console.writeln("Invalid command.  Enter ? for help");
					}
				}
				break;
			case 'D':
				console.writeln("<Display>");
				DisplaySector(player.Sector);
				break;
//			case 'E':
//				/* TODO: 22800 */
//				if(user.level < 90)
//					break;
//				console.writeln("<TW Editor>");
//				console.write("Do you wish to use the editor? Y/N [N] ");
//				switch(GetKeyEcho()) {
//					case 'Y':
//						console.writeln("Running Tradewars ][ Editor...");
//						/* TODO: TWEdit */
//				}
//				break;
			case 'F':
				/* 24000 */
				console.writeln("<Drop/Take Fighters>");
				if(player.Sector < 8) {
					console.writeln("You can't leave fighters in the Union (sectors 1-7)");
					break;
				}
				var sector=sectors.Get(player.Sector);
				if(sector.Fighters > 0 && sector.FighterOwner != player.Record) {
					console.writeln("There are already fighters in this sector!");
					break;
				}
				console.writeln("You have " + (player.Fighters+sector.Fighters) + " fighters available.");
				console.write("How many fighters do you want defending this sector? ");
				var newf=player.Fighters+sector.Fighters;
				if(newf > 9999)
					newf=9999;
				var newf=console.getnum(newf);
				if(newf >= 0 && newf <=player.Fighters+sector.Fighters) {
					if((player.Fighters+sector.Fighters)-newf > 9999) {
						console.writeln("Too many ships in your fleet!  You are limited to 9999");
						break;
					}
					player.Fighters=(player.Fighters+sector.Fighters)-newf;
					sector.Fighters=newf;
					if(sector.Fighters > 0)
						sector.FighterOwner=player.Record;
					sector.Put();
					player.Put();
					console.writeln("Done.  You have " + player.Fighters + " fighter(s) in your fleet.");
				}
				break;
			case 'G':
				/* 27500 */
				console.writeln("<Gamble>");
deuce's avatar
deuce committed
				console.attributes="HW";
				console.writeln("You have "+player.Credits+" credits.");
				console.writeln("How much do you want to gamble at double or nothing (50-50 odds)");
				console.write("[0]? ");
				var gamble=console.getnum(player.Credits > 99999?99999:player.Credits);
				if(gamble > 0 && gamble <=player.Credits) {
					console.write("Flipping the coin...");
					mswait(250);
					if(random(2)==1) {
						console.crlf();
						player.Credits-=gamble;
						console.writeln("Sorry, you lost.");
					}
					else {
						console.crlf();
						player.Credits+=gamble;
						console.writeln("You won!");
					}
					player.Put();
					console.writeln("You now have "+player.Credits+" credits.");
				}
				break;
			case 'I':
				console.writeln("<Info>");
				PlayerInfo(player.Record);
				break;
			case 'L':
				/* 31000 */
				console.writeln("<Land/Create planet>");
				console.crlf();
deuce's avatar
deuce committed
				console.attributes="HY";
				console.writeln("Landing...");
				var sector=sectors.Get(player.Sector);
				var planet=null;
				if(sector.Planet==0) {
					/* TODO: Race condition? */
					CreatePlanet(sector);
				}
				else {
					/* 32310 */
					/* Lock the planet file and ensure we can land... */
					if(!Lock(planets.file.name, bbs.node_num, true, 5)) {
						console.writeln("The spaceport is busy.  Try again later.");
						break;
					}
					planet=planets.Get(sector.Planet);
					if(planet.OccupiedBy != 0) {
						console.writeln("The spaceport is busy.  Try again later.");
						Unlock(planets.file.name);
						break;
					}
					planet.OccupiedBy=player.Record;
					planet.Put();
					Unlock(ports.file.name);
					player.Landed=true;
					player.Put();

					Production(planet);
					PlanetReport(planet);
deuce's avatar
deuce committed
					console.attributes="HW";
					PlanetMenu(planet);

					if(!Lock(planets.file.name, bbs.node_num, true, 10))
						twmsg.writeln("!!! Error locking ports file!");
					planet.OccupiedBy=0;
					planet.Put();
					Unlock(planets.file.name);
					player.Landed=false;
					player.Put();
				}
				break;
			case 'M':
				/* 23000 */
				console.writeln("<Move>");
				if(player.TurnsLeft < 1) {
					console.writeln("I'm sorry but you don't have any turns left.");
					break;
				}
deuce's avatar
deuce committed
				console.attributes="HW";
				console.write("Warps lead to: ");
				var sector=sectors.Get(player.Sector);
				var i;
				var count=0;
				for(i=0; i<sector.Warps.length; i++) {
					if(sector.Warps[i]>0) {
						if(count>0)
							console.write(", ");
						console.write(sector.Warps[i]);
						count++;
					}
				}
				console.crlf();
				console.write("To which sector? ");
				var to=console.getnum(sectors.length-1);
				break;
			case 'P':
				console.writeln("<Port>");
deuce's avatar
deuce committed
				console.attributes="HR";
				console.crlf();
				console.writeln("Docking...");
				if(player.TurnsLeft<1) {
					console.writeln("Sorry, but you don't have any turns left.");
					break;
				}
				var sector=sectors.Get(player.Sector);
				if(sector.Port<1) {
					console.writeln("There are no ports in this sector.");
					break;
				}
				player.TurnsLeft--;
				player.Ported=true;
				player.Put();
				console.writeln("One turn deducted");
				if(player.Sector==1) {
					/* 33200 */
					var prices=SolReport();
deuce's avatar
deuce committed
					console.attributes="HR";
					console.writeln("You have " + player.Credits + " credits.");
					console.crlf();
					/* Holds */
					var maxbuy=parseInt(player.Credits/prices[0]);
					if(player.Holds+maxbuy>50)
						maxbuy=50-player.Holds;
					if(maxbuy > 0) {
						console.write("How many holds do you want to buy [0]? ");
						var buy=console.getnum(maxbuy);
						if(buy>0) {
							player.Holds+=buy;
							player.Credits -= buy*prices[0];
							player.Put();
							console.writeln("You have " + player.Credits + " credits.");
						}
					}
					/* Fighters */
					var maxbuy=parseInt(player.Credits/prices[1]);
					if(player.Fighters+maxbuy>9999)
						maxbuy=9999-player.Fighters;
					if(maxbuy > 0) {
						console.write("How many fighters do you want to buy [0]? ");
						var buy=console.getnum(maxbuy);
						if(buy>0) {
							player.Fighters+=buy;
							player.Credits -= buy*prices[1];
							player.Put();
							console.writeln("You have " + player.Credits + " credits.");
						}
					}
					/* Turns */
					var maxbuy=parseInt(player.Credits/prices[2]);
					if(maxbuy > 0) {
						console.write("How many turns do you want to buy [0]? ");
						var buy=console.getnum(maxbuy);
						if(buy>0) {
							player.TurnsLeft+=buy;
							player.Credits -= buy*prices[2];
							player.Put();
							console.writeln("You have " + player.Credits + " credits.");
						}
					}
				}
				else {
					var sector=sectors.Get(player.Sector);
					
					/* Lock the port file and ensure we can dock... */
					if(!Lock(ports.file.name, bbs.node_num, true, 5)) {
						console.writeln("The port is busy.  Try again later.");
						break;
					}
					var port=ports.Get(sector.Port);
					if(port.OccupiedBy != 0) {
						console.writeln("The port is busy.  Try again later.");
						Unlock(ports.file.name);
						break;
					}
					port.OccupiedBy=player.Record;
					port.Put();
					Unlock(ports.file.name);
					Production(port);
					var sale=PortReport(sector);
deuce's avatar
deuce committed
					console.attributes="HR";
deuce's avatar
deuce committed
					console.attributes="HY";
					/* Sell... */
					for(i=0; i<Commodities.length; i++) {
						if(sale[i].Vary < 0) {
							var amount=Transact(i, sale[i].Price, sale[i].Vary, sale[i].Number);
							if(amount >= 0) {
								count++;
								port.Commodities[i] += amount;
								port.Put();
							}
						}
					}
					/* Buy... */
					for(i=0; i<Commodities.length; i++) {
						if(sale[i].Vary > 0) {
							var amount=Transact(i, sale[i].Price, sale[i].Vary, sale[i].Number);
							if(amount >= 0) {
								count++;
								port.Commodities[i] -= amount;
								port.Put();
							}
						}
					}
					if(count==0) {
						console.attributes="IC";
						console.writeln("You don't have anything they want, and they don't have anything you can buy");
						console.attributes="C";
					}

					if(!Lock(ports.file.name, bbs.node_num, true, 10))
						twmsg.writeln("!!! Error locking ports file!");
					port.OccupiedBy=0;
					port.Put();
					Unlock(ports.file.name);
				}
				player.Ported=false;
				player.Put();
				break;
			case 'T':
				/* 32799 */
deuce's avatar
deuce committed
				console.attributes="HW";
				console.writeln("<Team menu>");
				TeamMenu();
				break;
			case '?':
				console.attributes="C";
				console.writeln("<Help>");
				console.crlf();
				console.printfile(fname("main.asc"));
				break;

			case 'Z':
				console.writeln("<Instructions>");
				Instructions();
				break;
			case 'Q':
				console.attributes="W";
				console.writeln("<Quit>");
				console.attributes="W";
				console.write("Are you sure (Y/N)? ");
				if(console.getkeys("YN")=='Y') {
					exit_tw2=true;
					return;
				}
				break;
			default:
				if(inp.search(/^[0-9]*$/)!=-1) {
					if(MoveTo(parseInt(inp)))
						return;
				}
				break;
		}
	}
}

function CheckSector()
{
	if(player.Sector==85) {
		console.crlf();
		console.attributes="W";
		console.writeln("Congratulations!  You've defeated the cabal and gained 100,000 points!");
		player.Points += 100000;
		player.Put();
		console.writeln("Unfortunately, victory is fleeting...");

		var sector=sectors.Get(85);
		sector.Fighters=3000;
		sector.FighterOwner=-1;
		sector.Put();
		twopeng.writeln("Congratulations go to "+player.Alias+
           " who invaded the Cabal empire on " + system.datestr() + 
		   " and received 100,000 points!");
		return(false);
	}
	return(true);
}

function EnterSector()	/* 20000 */
{
	var sector=sectors.Get(player.Sector);
	var fighterteam=-1;

	console.attributes="Y";
	DisplaySector(sector.Record);
	if(sector.FighterOwner > 0) {
		var otherplayer=players.Get(sector.FighterOwner);
		if(otherplayer.TeamNumber > 0)
			fighterteam=otherplayer.TeamNumber;
	}
	while(sector.FighterOwner != player.Record && sector.Fighters > 0 && fighterteam != player.TeamNumber) {
		console.writeln("You have to destroy the fighters before you can enter this sector.");
		console.writeln("Fighters: "+player.Fighters+" / "+sector.Fighters);
		console.write("Option? (A,D,I,Q,R,?):? ");
		switch(InputFunc(['A','D','I','Q','R','?'])) {
			case 'A':
				console.writeln("<Attack>");
				var otherteam=false;

				if(sector.FighterOwner > 0) {
					var otherplayer=players.Get(sector.FighterOwner);
					if(otherplayer.TeamNumber>0)
						otherteam-true;
				}
				var killed=DoBattle(sector, otherteam);
				if(killed > 0) {
					player.Points+=k*100;
					console.writeln("You just recieved "+(k*100)+" points for that.");
				}
				if(sector.Fighters==0)
					console.writeln("You destroyed all the fighters.");
				break;
			case 'D':
				console.writeln("<Display>");
				DisplaySector(player.Sector);
				break;
			case 'I':
				console.writeln("<Info>");
				PlayerInfo(player.Record);
				break;
			case 'Q':
				console.write("Are you sure (Y/N)? ");
				if(console.getkeys("YN")=='Y') {
					exit_tw2=true;
					return(false);
				}
				break;
			case 'R':
				console.writeln("<Retreat>");
				if(player.LastIn<1 || player.LastIn>=sectors.length)
					player.LastIn=random(sectors.length-1)+1;
				if(player.Fighters<1) {
					if(random(2)==1) {
						console.writeln("You escaped!");
						player.Sector=player.LastIn;
						player.Put();
						location.Sector=player.Sector; location.Put();
						return(false);
					}
					console.attributes="R";
					console.writeln("You didn't escape!");
					console.writeln("Your ship has been destroyed!");
					console.crlf();
					console.attributes="R";
					console.writeln("You will start over tomorrow with a new ship.");
					KilledBy(player, player, true);
					return(false);
				}
				else {
					player.Fighters--;
					console.writeln("You have "+player.Fighters+" fighter(s) left.");
					player.Sector=player.LastIn;
					player.Put();
					location.Sector=player.Sector; location.Put();
					return(false);
				}
				break;
			case '':
				console.writeln("?=<Help>");
				break;
			case '?':
				console.writeln("<Help>");
				console.crlf();
				console.printfile(fname("entersector.asc"));
				break;
			default:
				console.crlf();
				console.writeln("Invalid command.  ? = Help");
		}
	}
	return(true);
}

function PlayerInfo(num)
{
	var p=players.Get(num);

deuce's avatar
deuce committed
	console.attributes="HW";
	console.write("   Pilot's Name: "+p.Alias);
	if(p.PlayerNumber>0)
		console.write("  Team ["+p.TeamNumber+"]");
	console.crlf();
deuce's avatar
deuce committed
	console.writeln("       Fighters: "+p.Fighters);
deuce's avatar
deuce committed
	console.attributes="HG";
	console.writeln("Sector Location: "+p.Sector);
	console.writeln("     Turns left: "+p.TurnsLeft);
	console.writeln("    Cargo Holds: "+p.Holds);
	console.attributes="HR";
	var i;
	for(i=0; i<Commodities.length; i++)
deuce's avatar
deuce committed
		console.writeln("     # with "+Commodities[i].abbr+": "+p.Commodities[i]+" ");
	console.attributes="HM";
deuce's avatar
deuce committed
	console.writeln("        Credits: "+p.Credits);
	console.writeln("    Door points: "+p.Points);
}

function DisplaySector(secnum)
{
	var sector=sectors.Get(secnum);
	var i;
	var count=0;

	console.crlf();
deuce's avatar
deuce committed
	console.attributes="HY";
	console.writeln("Sector "+secnum);
	console.attributes="HR";
	console.write("Port   ");
	if(sector.Port > 0) {
		var port=ports.Get(sector.Port);
		console.write(port.Name+", class "+port.Class)
	}
	else
		console.write("None");
	console.crlf();
deuce's avatar
deuce committed
	console.attributes="HB";
	if(sector.Planet) {
		var planet=planets.Get(sector.Planet);
deuce's avatar
deuce committed
		console.writeln("Planet "+planet.Name);
deuce's avatar
deuce committed
	console.attributes="HC";
	console.writeln("Other Ships");
	console.attributes="C";
	for(i=1;i<players.length;i++) {
		var otherloc=playerLocation.Get(i);
		if(otherloc.Sector==secnum) {
			var otherplayer=players.Get(i);
			if(otherplayer.UserNumber > 0 && otherplayer.Sector==secnum) {
				if(otherplayer.Record==player.Record)
					continue;
				if(otherplayer.KilledBy!=0)
					continue;

				count++;
				console.crlf();
				console.write("   "+otherplayer.Alias);
				if(otherplayer.TeamNumber>0)
					console.write("  Team ["+otherplayer.TeamNumber+"]");
				console.write(" with "+otherplayer.Fighters+" fighters");
				if(otherplayer.Landed)
					console.write(" (on planet)");
				else if(otherplayer.Ported)
					console.write(" (docked)");
				else if(otherplayer.Online)
					console.write(" (online)");
			}
deuce's avatar
deuce committed
		console.write("   None");
deuce's avatar
deuce committed
	console.attributes="HG";
	console.writeln("Fighters defending sector");
	console.attributes="G";
	if(sector.Fighters==0)
deuce's avatar
deuce committed
		console.writeln("   None");
deuce's avatar
deuce committed
		console.write("   "+sector.Fighters);
		if(sector.FighterOwner<0)
			console.writeln(" (Cabal)");
		else if(sector.FighterOwner==player.Record)
			console.writeln(" (yours)");
		else {
			var otherplayer=players.Get(sector.FighterOwner);
			console.write(" (belong to "+otherplayer.Alias);
			if(otherplayer.TeamNumber)