1/*
    2% NomicMUD: A MUD server written in Prolog
    3% Maintainer: Douglas Miles
    4% Dec 13, 2035
    5%
    6% Bits and pieces:
    7%
    8% LogicMOO, Inform7, FROLOG, Guncho, PrologMUD and Marty's Prolog Adventure Prototype
    9% 
   10% Copyright (C) 2004 Marty White under the GNU GPL 
   11% Sept 20,1999 - Douglas Miles
   12% July 10,1996 - John Eikenberry 
   13%
   14% Logicmoo Project changes:
   15%
   16% Main file.
   17%
   18*/
   19
   20%:- ensure_loaded(adv_main).
   21
   22:- meta_predicate(memorize_edit(3,*,*,*)).   23memorize_edit(Pred3, Figment, M0, M2) :- assertion(\+ is_list(Figment)),
   24   Figment =.. [Name,Value], OldFigment =.. [Name,OldValue],
   25   (forget(OldFigment, M0, M1) 
   26     -> ( call(Pred3, OldValue,Value,NewValue), NewFigment =.. [Name,NewValue])
   27     ; (NewFigment=Figment, M0=M1)),
   28   memorize(NewFigment, M1, M2).
   29
   30memorize_appending(Figment, M0, M2) :-  memorize_edit(append,Figment, M0, M2).
   31
   32% Manipulate memories (M stands for Memories)
   33memorize(Figment, M0, M1) :- assertion(\+ is_list(Figment)), notrace(append([Figment], M0, M1)).
   34% memorize(Figment, M0, M1) :- notrace(append([Figment], M0, M1)).
   35forget(Figment, M0, M1) :- select(Figment, M0, M1).
   36forget_always(Figment, M0, M1) :- select_always(Figment, M0, M1).
   37%forget_default(Figment, Default, M0, M1) :-
   38% select_default(Figment, Default, M0, M1).
   39thought(Figment, M) :- declared(Figment, M).
   40
   41
   42in_agent_model(Agent, Fact, State):- in_model(Fact, State)*-> true ; (agent_thought_model(Agent, ModelData, State), in_model(Fact, ModelData)).
   43
   44in_model(E, L):- quietly(in_model0(E, L)).
   45in_model0(E, L):- \+ is_list(L),declared_link(declared, E, L).
   46in_model0(E, L):- compound(E),E = holds_at(_,_),!, member(E, L).
   47in_model0(E, L):- member(EE, L), same_element(EE,E).
   48same_element(E, E) :- !.
   49same_element(holds_at(E,T), E):- nonvar(T).
   50
   51
   52
   53:- defn_state_getter(agent_thought_model(agent,model)).   54agent_thought_model(Agent, ModelData, M0):- var(M0), get_advstate(State),!, member(memories(Agent,M0),State), agent_thought_model(Agent, ModelData, M0).
   55agent_thought_model(Agent, ModelData, M0):- \+ is_list(M0), !, declared_link(agent_thought_model(Agent), ModelData, M0).
   56agent_thought_model(Agent, ModelData, M0) :- memberchk(inst(Agent),M0), ModelData = M0, !.
   57agent_thought_model(Agent, ModelData, M0):- declared(memories(Agent,M1),M0),!,
   58  agent_thought_model(Agent, ModelData, M1).
   59
   60
   61
   62% TODO: change agent storage into a term:
   63% mind(AgentName, AgentType, History, ModelData, Goals /*, ToDo*/)
   64
   65% -------- Model updating predicates (here M stands for ModelData)
   66
   67% Fundamental predicate that actually modifies the list:
   68update_relation( NewHow, Item, NewParent, Timestamp, M0, M2) :-
   69 remove_old_info( NewHow, Item, NewParent, Timestamp, M0, M1),
   70 append([(h(NewHow, Item, NewParent))], M1, M2).
   71
   72remove_old_info( _NewHow, '<mystery>'(_, _, _), _NewParent, _Timestamp, M0, M0) :- !.
   73remove_old_info( _NewHow, Item, _NewParent, _Timestamp, M0, M2) :- 
   74 select_always((h(_OldHow, Item, _OldWhere)), M0, M1),
   75 select_always(h(_OldHow2, Item, _OldWhere2), M1, M2).
   76
   77
   78remove_children(_At, '<mystery>'(_, _, _), _Object, _Timestamp, M0, M0):- !.
   79remove_children( At, _, Object, Timestamp, M0, M2):- 
   80  select((h(At, _, Object)), M0, M1), !,
   81  remove_children( At, _, Object, Timestamp, M1, M2).
   82remove_children( _At, _, _Object, _Timestamp, M0, M0).
   83
   84% Batch-update relations.
   85
   86update_relations(Prep, '<mystery>'(How,What,Object2), Object, Timestamp, M0, M1):-
   87  \+ in_model((h(What, _Child, Object2)), M0), 
   88  % \+ in_model((h(What, Object2, _Parent), _), M0),
   89  update_relation( Prep, '<mystery>'(How,What,Object2), Object, Timestamp, M0, M1).
   90
   91update_relations(_NewHow, '<mystery>'(_,_,_), _NewParent, _Timestamp, M, M).
   92update_relations(_NewHow, [], _NewParent, _Timestamp, M, M).
   93update_relations( NewHow, [Item|Tail], NewParent, Timestamp, M0, M2) :-
   94 update_relation( NewHow, Item, NewParent, Timestamp, M0, M1),
   95 update_relations( NewHow, Tail, NewParent, Timestamp, M1, M2).
   96
   97
   98% If dynamic topology needs remembering, use
   99%  h(exit(E), Here, [There1|ThereTail], Timestamp)
  100realize_model_exit(At, From, _Timestamp, M0, M2) :-
  101 forget((h(exit(At), From, To)), M0, M1),
  102 append([(h(exit(At), From, To))], M1, M2).
  103realize_model_exit(At, From, _Timestamp, M0, M1) :-
  104 append([(h(exit(At), From, '<mystery>'(exit, At, From)))], M0, M1).
  105
  106update_model_exit(At, From, To, _Timestamp, M0, M2) :-
  107 select_always((h(exit(At), From, _To)), M0, M1),
  108 append([(h(exit(At), From, To))], M1, M2).
  109
  110
  111% Model exits from Here.
  112update_model_exits([], _From, _T, M, M).
  113update_model_exits([Exit|Tail], From, Timestamp, M0, M2) :-
  114 realize_model_exit(Exit, From, Timestamp, M0, M1),
  115 update_model_exits(Tail, From, Timestamp, M1, M2).
  116
  117update_model(Knower, arriving(Agent, In, Here, Walk, ExitNameReversed), Timestamp, Mem, M0, M2) :-  
  118   \+ in_model(h(exit(ExitNameReversed), Here, _There), M0),
  119   realize_model_exit(ExitNameReversed, Here, Timestamp, M0, M1),
  120   update_model(Knower, arriving(Agent, In, Here, Walk, ExitNameReversed), Timestamp, Mem, M1, M2).
  121
  122% Match only the most recent Figment in Memory.
  123%last_thought(Figment, Memory) :- % or member1(F, M), or memberchk(Term, List)
  124% copy_term(Figment, FreshFigment),
  125% append(RecentMemory, [Figment|_Tail], Memory),
  126% \+ member(FreshFigment, RecentMemory).
  127
  128update_model(Knower, arriving(Agent, At, Here, _, ExitNameReversed), Timestamp, Mem, M0, M2) :-  Knower == Agent,    
  129  % According to model, where was I?
  130  must(in_model(h(_Was, Agent, There), M0)),
  131  % TODO: Handle goto(Agent, walk, on, table)
  132  % reverse_dir(ExitNameReversed, ExitName, advstate),
  133  % At did I get Here?  
  134  must(append(RecentMem, [attempting(go_dir(Agent, _, ExitName))|OlderMem], Mem)), % find figment
  135  \+ member(attempting(go_dir(Agent, _, _)), RecentMem),               % guarrantee recentness
  136  memberchk(timestamp(_T1,_OldNow), OlderMem),               % get associated stamp
  137  %player_format(Agent, '~p moved: goto(Agent, walk, ~p, ~p) from ~p leads to ~p~n',
  138  %       [Agent, AtGo, Dest, There, Here]),
  139  update_model_exit(ExitName, There, Here, Timestamp, M0, M11), % Model the path.
  140  update_model_exit(ExitNameReversed, Here, There, Timestamp, M11, M1), 
  141  update_relation(At, Agent, Here, Timestamp, M1, M2), !. % And update location.
  142
  143update_model(Knower, arriving(Agent, In, Here, Walk, ExitNameReversed), Timestamp, Mem, M0, M2) :-  
  144   \+ in_model(h(In, Agent, Here), M0),
  145   update_relations( In, [Agent], Here, Timestamp, M0, M1),
  146   update_model(Knower, arriving(Agent, In, Here, Walk, ExitNameReversed), Timestamp, Mem, M1, M2).
  147
  148update_model(_Agent, moved(_Doer, _How, Object,_From, At, To), Timestamp, _Mem, M0, M1) :-
  149  update_relation(At, Object, To, Timestamp, M0, M1).
  150
  151update_model(Agent, Event, Timestamp, Memory, M0, M2) :- fail,
  152  implications(event , ( Event), Preconds, Postconds),   
  153    (satisfy_each(preCond(_),Preconds, M0, _)  ->
  154      satisfy_each(postCond(_),Postconds, M0, M1) -> M0\=@= M1), !,
  155    update_model(Agent, Event, Timestamp, Memory, M1, M2).
  156
  157
  158update_model(Agent, carrying(Agent, Objects), Timestamp, _Memory, M0, M1) :-
  159 update_relations( held_by, Objects, Agent, Timestamp, M0, M1).
  160update_model(Agent, wearing(Agent, Objects), Timestamp, _Memory, M0, M1) :-
  161 update_relations( worn_by, Objects, Agent, Timestamp, M0, M1).
  162
  163update_model(Agent, percept(Agent, _Sense, _Depth, child_list(Object, At, Children)), Timestamp, _Mem, M0, M2) :-
  164 must_det((remove_children( At, Children, Object, Timestamp, M0, M1),
  165   update_relations( At, Children, Object, Timestamp, M1, M2))).
  166update_model(Agent, percept_props(Agent, _Sense, Object, _Depth, PropList), _Stamp, _Mem, M0, M2) :-
  167 apply_mapl_rest_state(updateprop(Object), PropList, [], M0, M2).
  168update_model(_Agent, props(Object, PropList), _Stamp, _Mem, M0, M2) :-
  169 apply_mapl_rest_state(updateprop(Object), PropList, [], M0, M2).
  170
  171
  172update_model(Agent, percept(Agent2,_,_,_Info), _Timestamp, _Mem, M0, M0):- Agent \=@= Agent2, !.
  173% Model exits from Here.
  174update_model(Agent, percept(Agent,_,_,exit_list(in, Here, ExitRelations)), Timestamp, _Mem, M0, M4) :-
  175  update_model_exits(ExitRelations, Here, Timestamp, M0, M4).
  176
  177% Model objects seen Here
  178update_model(Agent, percept(Agent, _Sense, child_list(_Depth, Here, Prep, Objects)), Timestamp, _Mem, M0, M3):- !,
  179   update_relations(Prep, Objects, Here, Timestamp, M0, M3). 
  180
  181update_model(_Agent, [], _Timestamp, _Memory, M, M).
  182update_model(Agent, [Percept|Tail], Timestamp, Memory, M0, M2) :-
  183 update_model(Agent, Percept, Timestamp, Memory, M0, M1),
  184 update_model( Agent, Tail, Timestamp, Memory, M1, M2),!.
  185update_model(_Agent, failure(_,_), _Timestamp, _Mem, M0, M0) :- !.
  186update_model(_Agent, success(_,_), _Timestamp, _Mem, M0, M0) :- !.
  187update_model(_Agent, failure(_), _Timestamp, _Mem, M0, M0) :- !.
  188update_model(_Agent, emoted(_,_,_,_), _Timestamp, _Mem, M0, M0) :- !.
  189update_model(_Agent, emote(_,_,_,_), _Timestamp, _Mem, M0, M0) :- !.
  190update_model(_Agent, msg(_), _Timestamp, _Mem, M0, M0) :- !.
  191
  192update_model(Agent, time_passes(Target), Timestamp, _Memory, M, M):-
  193 nop(bugout1(unused_update_model(Agent, time_passes(Target), Timestamp, M))).
  194
  195update_model(Agent, Percept, Timestamp, _Memory, M, M):-
  196 nop(bugout1(failed_update_model(Agent, Percept, Timestamp), model)).
  197
  198
  199well_remembered(none).
  200
  201maybe_remember(Percept, M0, M0):- functor(Percept,F,_),well_remembered(F),!.
  202%maybe_remember(percept_props(Whom,see,What,Depth,_List),M0,M1):- maybe_remember(percept_props(Whom,see,WhatDepth,),M0,M1),!.
  203maybe_remember(Percept, M0, M1):- append([Percept], M0, M1).
  204
  205each_update_model(_Agent, [], _Timestamp, _Memory, M, M).
  206each_update_model( Agent, [Percept|Tail], Timestamp, Memory, M0, M3) :-
  207 maybe_remember(Percept, M0, M1),
  208 update_model(Agent, Percept, Timestamp, Memory, M1, M2), 
  209 each_update_model( Agent, Tail, Timestamp, Memory, M2, M3)