19
20admin :- true. 21wizard :- true. :- true. 23
24:- op(200,fx,'$'). 25
26:- use_module(library(editline)). 27:- initialization('$toplevel':setup_readline,now). 28
29:- user:ensure_loaded((.. / parser_sharing)). 30:- consult(adv_debug). 31:- consult(adv_util). 32:- consult(adv_io). 33
34:- consult(adv_model). 35:- consult(adv_percept). 36
37:- consult(adv_inst). 38:- consult(adv_edit). 39
40:- consult(adv_action). 41:- consult(adv_agent). 42:- consult(adv_eng2cmd). 43:- consult(adv_floyd). 44:- consult(adv_log2eng). 45:- consult(adv_physics). 46:- consult(adv_plan). 47:- consult(adv_state). 48:- consult(adv_data). 49
52
53
54:- export(console_player/1). 55console_player(Agent):-
56 current_input(InStream),
57 adv:console_info(_Id, _Alias, InStream, _OutStream, _Host, _Peer, Agent),!.
58console_player(Agent):-
59 Agent = 'player~1',
60 (( \+ adv:console_info(_Id, _Alias, _InStream, _OutStream, _Host, _Peer, Agent))).
61
62:- thread_local(adv:current_agent/1). 63current_player(Agent):- adv:current_agent(Agent),!.
64current_player(Agent):- thread_self(Id),adv:console_info(Id,_Alias,_InStream,_OutStream,_Host,_Peer, Agent).
65current_player('player~1').
66:- export(current_player/1). 67
68
69adventure_init :-
70 use_module(library(editline)),
71 ignore(notrace(catch(('$toplevel':setup_readline),_,true))),
72 73 dmust((
74 test_ordering,
75 init_logging,
76 (retractall(advstate(_));true),
77 istate(S0),
78 init_objects(S0, S1),
79 80 asserta(advstate(S1)))), !,
81 player_format('=============================================~n', []),
82 player_format('INIT STATE~n', []),
83 player_format('=============================================~n', []),
84 printable_state(S1,SP),
85 pprint(SP, general),!.
86
87
88adventure:-
89 adventure_init,
90 player_format('=============================================~n', []),
91 player_format('Welcome to Marty\'s Prolog Adventure Prototype~n', []),
92 player_format('=============================================~n', []),
93 94 mainloop,
95 96 adv:input_log(FH),
97 close(FH).
98
99adventure :-
100 adv:input_log(FH),
101 close(FH),
102 player_format('adventure FAILED~n', []),
103 !, fail.
104
105
106main(S0, S9) :-
107 notrace((nb_setval(advstate,S0))),
108 update_telnet_clients(S0,S1),
109 ((nb_setval(advstate,S1),
110 111 get_live_agents(LiveAgents, S1),
112 ttyflush)),
113 114 apply_all(LiveAgents, run_agent_pass_1(), S1, S2),
115 apply_all(LiveAgents, run_agent_pass_2(), S2, S9),
116 notrace((nb_setval(advstate,S9))),
117 !. 118main(S0, S0) :-
119 bugout('main FAILED~n', general).
120
121:- dynamic(adv:agent_conn/4). 122
123update_telnet_clients(S0,S2):-
124 retract(adv:agent_conn(Agent,Named,_Alias,Info)),
125 create_agent_conn(Agent,Named,Info,S0,S1),
126 update_telnet_clients(S1,S2).
127update_telnet_clients(S0,S0).
128
129
130
131:- dynamic(adv:console_tokens/2). 132telnet_decide_action(Agent, Mem0, Mem0):-
133 134 thought(todo([Action|_]), Mem0),
135 (declared(h(_Spatial, in, Agent, Here), Mem0)->true;Here=somewhere),
136 bugout('~w @ ~w telnet: Already about to: ~w~n', [Agent, Here, Action], telnet).
137
138telnet_decide_action(Agent, Mem0, Mem1) :-
139 140 retract(adv:console_tokens(Agent, Words)), !,
141 dmust((parse(Words, Action, Mem0),
142 nop(bugout('Telnet TODO ~p~n', [Agent: Words->Action], telnet)),
143 add_todo(Action, Mem0, Mem1))), !.
144telnet_decide_action(Agent, Mem, Mem) :-
145 nop(bugout('~w: Can\'t think of anything to do.~n', [Agent], telnet)).
146
147
149:- initialization(adventure, main). 151
152mainloop :-
153 repeat,
154 once(dmust((
155 retract(advstate(S0)),
156 main(S0, S1),
157 asserta(advstate(S1)),
158 must_output_state(S1)))),
159 declared(quit, S1),
160 !. 161
186
187
190:- nop(ensure_loaded('adv_main_commands')). 192
193
194save_term(Filename, Term) :-
195 \+ access_file(Filename, exist),
196 open(Filename, write, FH),
197 write(FH, Term),
198 close(FH),
199 player_format('Saved to file "~w".~n', [Filename]).
200save_term(Filename, _) :-
201 access_file(Filename, exist),
202 player_format('Save FAILED! Does file "~w" already exist?~n', [Filename]).
203save_term(Filename, _) :-
204 player_format('Failed to state(Spatial, open, t) file "~w" for saving.~n', [Filename])