for review: changes to gwars
Thomas Charles CONWAY
conway at cs.mu.oz.au
Tue Feb 18 15:41:20 AEDT 1997
For anyone who is interested, comments welcome:
gwars/gwars.m:
read $HOME/.gwrc or ./.gwrc if $HOME is not defined.
At the moment, the only information that can be specified
in the .gwrc file is player definition:
player(Name, Display).
where Name and Display are *strings* (not atoms).
gwars/control.m:
add the stuff to open the displays read from the .gwrc file.
gwars/objects.m:
Implement the "Smart Planet" (TM) alogrithm which places planets
between every pair of ships so that there are no "direct shots".
gwars/*.m:
various other minor changes.
cvs diff: Diffing .
Index: control.m
===================================================================
RCS file: /home/staff/zs/imp/gwars/control.m,v
retrieving revision 1.2
diff -u -r1.2 control.m
--- control.m 1997/02/13 22:21:37 1.2
+++ control.m 1997/02/18 03:08:48
@@ -2,10 +2,10 @@
:- interface.
-:- import_module io, mtk.
+:- import_module io, mtk, std_util, list.
-:- pred control:init(tcl_interp, io__state, io__state).
-:- mode control:init(in, di, uo) is det.
+:- pred control:init(list(pair(string)), tcl_interp, io__state, io__state).
+:- mode control:init(in, in, di, uo) is det.
%------------------------------------------------------------------------------%
@@ -17,14 +17,15 @@
%------------------------------------------------------------------------------%
-control:init(Interp) -->
+control:init(Players, Interp) -->
create_control(Interp),
create_command(Interp, "addplayer", add_player),
create_command(Interp, "removeplayer", remove_player),
create_command(Interp, "newgame", do_newgame),
create_command(Interp, "fire", do_fire),
create_command(Interp, "go_away", do_unwinner),
- create_command(Interp, "play", do_play).
+ create_command(Interp, "play", do_play),
+ add_players(Interp, Players).
%------------------------------------------------------------------------------%
@@ -78,8 +79,26 @@
:- mode add_player(in, in, out, out, di, uo) is det.
add_player(Interp, _Args, tcl_ok, "") -->
- eval(Interp, ".adddisp.nentry get", _, Name0),
+ eval(Interp, ".adddisp.nentry get", _, Name),
eval(Interp, ".adddisp.dentry get", _, Display),
+ do_add_player(Interp, Name, Display).
+
+%------------------------------------------------------------------------------%
+
+:- pred add_players(tcl_interp, list(pair(string)), io__state, io__state).
+:- mode add_players(in, in, di, uo) is det.
+
+add_players(_, []) --> [].
+add_players(Interp, [Name - Display|Rest]) -->
+ do_add_player(Interp, Name, Display),
+ add_players(Interp, Rest).
+
+%------------------------------------------------------------------------------%
+
+:- pred do_add_player(tcl_interp, string, string, io__state, io__state).
+:- mode do_add_player(in, in, in, di, uo) is det.
+
+do_add_player(Interp, Name0, Display) -->
{ basename(Name0, Name) },
{ string__format("
.displays insert end ""%-12s %s""
@@ -422,9 +441,6 @@
:- mode shoot(in, in, in, in, in, in, in, in, out, di, uo) is det.
shoot(X0, Y0, VX0, VY0, N, Planets, Ships, Interp, Result) -->
-% io__stderr_stream(Stderr),
-% { string__format("%2.2f %2.2f\n", [f(X0), f(Y0)], DStr) },
-% io__write_string(Stderr, DStr),
get_global("PlayField", PlayField),
(
{ N > 20000 }
Index: display.m
===================================================================
RCS file: /home/staff/zs/imp/gwars/display.m,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 display.m
--- display.m 1997/02/04 03:18:21 1.1.1.1
+++ display.m 1997/02/18 02:48:02
@@ -2,10 +2,10 @@
:- interface.
-:- import_module io, mtk.
+:- import_module io, mtk, std_util, list.
-:- pred display:init(tcl_interp, io__state, io__state).
-:- mode display:init(in, di, uo) is det.
+:- pred display:init(list(pair(string)), tcl_interp, io__state, io__state).
+:- mode display:init(in, in, di, uo) is det.
%------------------------------------------------------------------------------%
@@ -17,7 +17,7 @@
%------------------------------------------------------------------------------%
-display:init(Interp) -->
- control:init(Interp).
+display:init(Players, Interp) -->
+ control:init(Players, Interp).
%------------------------------------------------------------------------------%
Index: gwars.m
===================================================================
RCS file: /home/staff/zs/imp/gwars/gwars.m,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 gwars.m
--- gwars.m 1997/02/04 03:18:21 1.1.1.1
+++ gwars.m 1997/02/18 04:35:05
@@ -9,6 +9,7 @@
:- implementation.
:- import_module mtk, display, objects, random, globals, map.
+:- import_module std_util, string, list, term, term_io.
main -->
init_globals,
@@ -25,10 +26,62 @@
set_global("RadRange", radrange(2.5, 10.0)),
{ random__init(0, Rnd) },
set_global("Rnd", Rnd),
- main(init, ["gwars"]).
+ get_init_players(InitPlayers),
+ main(init(InitPlayers), ["gwars"]).
:- pred init_playerparams(map(player, playerparams)::out) is det.
init_playerparams(Map) :-
map__init(Map).
+:- pred get_init_players(list(pair(string)), io__state, io__state).
+:- mode get_init_players(out, di, uo) is det.
+
+get_init_players(Players) -->
+ io__get_environment_var("HOME", MHome),
+ (
+ { MHome = yes(Home) },
+ { string__append(Home, "/.gwrc", RCName) }
+
+ ;
+ { MHome = no },
+ { RCName = ".gwrc" }
+ ),
+ io__see(RCName, Result),
+ (
+ { Result = ok },
+ read_players(Players),
+ io__seen
+ ;
+ { Result = error(_) },
+ { Players = [] }
+ ).
+
+:- pred read_players(list(pair(string)), io__state, io__state).
+:- mode read_players(out, di, uo) is det.
+
+read_players(Players) -->
+ term_io__read_term(Result),
+ (
+ { Result = eof },
+ { Players = [] }
+ ;
+ { Result = error(_, _) },
+ { Players = [] }
+ ;
+ { Result = term(_Varset, Term) },
+ ( { term_to_player(Term, Player, Display) } ->
+ { Players = [Player - Display|Players1] },
+ read_players(Players1)
+ ;
+ read_players(Players)
+ )
+ ).
+
+:- pred term_to_player(term::in, string::out, string::out) is semidet.
+
+term_to_player(Term, Player, Display) :-
+ Term = term__functor(term__atom("player"),
+ [PlayerTerm, DisplayTerm], _),
+ PlayerTerm = term__functor(term__string(Player), _, _),
+ DisplayTerm = term__functor(term__string(Display), _, _).
Index: objects.m
===================================================================
RCS file: /home/staff/zs/imp/gwars/objects.m,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 objects.m
--- objects.m 1997/02/04 03:18:23 1.1.1.1
+++ objects.m 1997/02/18 03:28:46
@@ -66,6 +66,8 @@
%------------------------------------------------------------------------------%
+:- type line ---> line(float, float, float, float).
+
create_planets -->
get_global("NumPlanets", NumPlanets),
get_global("PlayField", PlayField),
@@ -73,10 +75,69 @@
get_global("Ships", Ships),
get_global("Rnd", Rnd0),
{ copy(Rnd0, Rnd1) },
- { create_planets(NumPlanets, PlayField, RadRange, Ships,
- [], Planets, Rnd1, Rnd) },
+ { Lambda = lambda([Line::out] is nondet, (
+ list__member(Ship0, Ships),
+ list__member(Ship1, Ships),
+ Ship0 \= Ship1,
+ Ship0 = ship(X0, Y0, _, _),
+ Ship1 = ship(X1, Y1, _, _),
+ ( X0 < X1 ->
+ true
+ ;
+ X0 = X1,
+ Y0 < Y1
+ ),
+ Line = line(X0, X1, Y0, Y1)
+ )) },
+ { solutions(Lambda, Lines) },
+ { list__length(Lines, Intersecting) },
+ { create_intersecting(Lines, PlayField, RadRange, Ships,
+ [], Planets0, Rnd1, Rnd2) },
+ { create_planets(NumPlanets - Intersecting, PlayField, RadRange, Ships,
+ Planets0, Planets, Rnd2, Rnd) },
set_global("Rnd", Rnd),
set_global("Planets", Planets).
+
+:- pred create_intersecting(list(line), playfield, radrange, list(ship),
+ list(planet), list(planet), random__supply, random__supply).
+:- mode create_intersecting(in, in, in, in, in, out, mdi, muo) is det.
+
+create_intersecting([], _, _, _, Planets, Planets, Rnd, Rnd).
+create_intersecting([L|Ls], PlayField, RadRange, Ships,
+ Planets0, Planets, Rnd0, Rnd) :-
+ rnd(D0, Rnd0, Rnd1),
+ L = line(Xmin, Xmax, Ymin, Ymax),
+ X = Xmin + D0*(Xmax - Xmin),
+ Y = Ymin + D0*(Ymax - Ymin),
+ rnd(R0, Rnd1, Rnd3),
+ RadRange = radrange(Rmin, Rmax),
+ R = R0*(Rmax-Rmin)+Rmin,
+ (
+ (
+ % Intersect with an existing planet
+ some [P] (
+ list__member(P, Planets0),
+ P = planet(X1, Y1, R1),
+ Dist = sqrt(sqr(X - X1) + sqr(Y - Y1)),
+ Dist < R + R1
+ )
+ ;
+ % Intersect with a ship
+ some [S] (
+ list__member(S, Ships),
+ S = ship(X1, Y1, R1, _),
+ Dist = sqrt(sqr(X - X1) + sqr(Y - Y1)),
+ Dist < R + R1*1.1
+ )
+ )
+ ->
+ create_intersecting([L|Ls], PlayField, RadRange, Ships,
+ Planets0, Planets, Rnd3, Rnd)
+ ;
+ NewPlanet = planet(X, Y, R),
+ create_intersecting(Ls, PlayField, RadRange, Ships,
+ [NewPlanet|Planets0], Planets, Rnd3, Rnd)
+ ).
:- pred create_planets(int, playfield, radrange, list(ship),
list(planet), list(planet), random__supply, random__supply).
Index: radar.m
===================================================================
RCS file: /home/staff/zs/imp/gwars/radar.m,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 radar.m
--- radar.m 1997/02/04 03:18:22 1.1.1.1
+++ radar.m 1997/02/18 03:11:36
@@ -96,7 +96,6 @@
%------------------------------------------------------------------------------%
-
draw_ships(Interp, Player) -->
get_global("Ships", Ships),
list__foldl(draw_ship(Interp, Player), Ships).
--
Thomas Conway conway at cs.mu.oz.au
AD DEUM ET VINUM Every sword has two edges.
More information about the developers
mailing list