% ag_server6.erl - (Silver) Adventure Game Server -module(ag_server6). -author('Alan G. Labouseur'). -type direction() :: north | south | east | west. %-------- % Public %-------- -export([start/0, goToLocale/2, move/2]). start() -> io:fwrite("Starting AG server.~n",[]), spawn(fun serverLoop/0). goToLocale(ServerPid, Destination) -> rpc(ServerPid, Destination). -spec move(integer(), direction()) -> integer(). % This is not enforced at runtime. It's for Dializer. move(CurrentLocale, Direction) -> io:fwrite("Attempting to move ~w from location ~w.~n",[Direction, CurrentLocale]), NewLocaleNumber = mapper(CurrentLocale, Direction), NewLocaleDesciption = describe(NewLocaleNumber), io:fwrite("Moved to location ~w: ~s.~n",[NewLocaleNumber, NewLocaleDesciption]). %--------- % Private %--------- mapper(0, north) -> 1; mapper(0, south) -> 2; mapper(0, west) -> 5; mapper(1, south) -> 0; mapper(1, west) -> 5; mapper(2, north) -> 0; mapper(2, east) -> 4; mapper(3, south) -> 4; mapper(4, north) -> 3; mapper(4, west) -> 2; mapper(5, north) -> 1; mapper(5, east) -> 0; mapper(_, _) -> -1. describe(0) -> "Inn of the Last Home"; describe(1) -> "Yellow Brick Road"; describe(2) -> "Depths of the Earth"; describe(3) -> "Vault of the Drow"; describe(4) -> "Mouth of the Yawning Cave"; describe(5) -> "West Lake"; describe(-1) -> "You cannot go that way"; describe(Loc) -> io:format("Oops! Unknown locale: ~w.", [Loc]). rpc(ToPid, Request) -> ToPid ! {self(), Request}, receive {ToPid, Response} -> Response % This waits for a response from ToPid. end. serverLoop() -> receive { FromPid, {0, L} } -> FromPid ! {self(), io:format("You are standing west of a house. Inventory: ~w.~n", [L])}, serverLoop(); { FromPid, {1, L} } -> FromPid ! {self(), io:format("You have come to the base of a large tree. Inventory: ~w.~n", [L])}, serverLoop(); { FromPid, {_, L} } -> FromPid ! {self(), io:format("You are lost in unknown locale. Good going, Indiana. Perhaps one of these items will help:~w.~n", [L])}, serverLoop() end.