User:PhaethonH/PH inventory.prolog
From Egs Mayhem
< User:PhaethonH(Difference between revisions)
Current revision as of 03:13, 13 August 2007
%#! gprolog --entry-goal "consult('./PH_inventory.prolog')" % phaethon@EGC.prolog % GNU Prolog. :- dynamic(maxitemid/1, logtime/1). % Convenience predicate -- make GNU Prolog stop complaining about deliberate singletons. ignore(_). % These predicates affect dynamic predicates. newitemid(IDnum) :- maxitemid(M), N is (M+1), IDnum is M, retractall(maxitemid(_)), asserta(maxitemid(N)). ts(X) :- retractall(logtime(_)), asserta(logtime(X)). % Affects dynamic database. obtain(Giver, Item) :- obtain(Giver, Item, []). obtain(Giver, Count, Item) :- number(Count), obtain(Giver, Count, Item, []). obtain(Giver, Item, Descriptions) :- newitemid(ID), describe(ID, Descriptions), logtime(TS), assertz(ts(TS, obtain(ID))), assertz(item(ID, Item)), assertz(blame(ID, Giver)). obtain(_, 0, _). obtain(Giver, Count, Item, Descriptions) :- number(Count), Count > 0, obtain(Giver, Item, Descriptions), Oneless is (Count-1), obtain(Giver, Oneless, Item, Descriptions). %Recurse! obtain(_, 0, _, _). describe(_, []). describe(ID, [Desc|Rest]) :- assertz(attrib(ID, Desc)), describe(ID, Rest). % Inventory listing. % Determine letter-coding of inventory id number -- probably buggy. item_header(N, Header) :- N > 0, N < (1+26), Cc is (N + 96), char_code(Ch, Cc), Header = Ch. item_header(N, Header) :- N > 26, N < (1 + 26*2), Cc is (N + 64 - 26), char_code(Ch, Cc), Header = Ch. item_header(N, Header) :- N > (26*2), Header = N. % string.join() strjoin(ResultString, _, []) :- ResultString = "". strjoin(ResultString, _, [C]) :- atomic(C), name(C, ResultString) ; strjoin(ResultString, "", []). strjoin(ResultString, JoinString, [H|T]) :- atomic(H), name(H, SrcStr), strjoin(RestStr, JoinString, T), length(RestStr, Len), (Len > 0 -> append(JoinString, RestStr, SuffixStr) ; SuffixStr = RestStr), append(SrcStr, SuffixStr, ResultString) ; strjoin(ResultString, JoinString, T). % Return string description of item #N (Opts affects description verbosity). item_line(N, Opts, Line) :- item(N, ItemName), ignore(Opts), findall(X, attrib(N, X), [ItemNoun|ItemAttribs]), ignore(ItemNoun), strjoin(ItemAttribStr, ", ", ItemAttribs), length(ItemAttribStr, IASL), (IASL > 0 -> append(" (", ItemAttribStr, S1), append(S1, ")", S2) ; S2 = ""), name(ItemAttrib, S2), atom_concat(ItemName, ItemAttrib, Line), true. % List description of item #N. inv_item(N) :- maxitemid(Max), N < Max, item_header(N, ItemHeader), write(' '), write(ItemHeader), write(') '), item_line(N, [], ItemLine), write(ItemLine), nl, !, Next is (N+1), inv_item(Next). % pretty-print inventory. inv :- maxitemid(Max), ItemCount is (Max-1), write('Current inventory'), write(' ('), write(ItemCount), write(' items)'), write(':'), nl, inv_item(1); write('(end)\ '), true. % Old inventory listing. inv0 :- listing(item), listing(blame). % blamelist blame_item(N) :- maxitemid(Max), N < Max, ts(TS, obtain(N)), blame(N, W), item(N, Item), % write(' '), write(N), write('. '), write('* '), write('['), write(TS), write('] '), write(W), write(' provided '), write(Item), nl, !, Next is (N+1), blame_item(Next). blame :- write('Blame list:'), nl, blame_item(1); write('(end)\ '), true. % Run through all transactions to update dynamic database. :- initialization(transactions). %:- initialization(init). % Record of inventory transactions. transactions :- assertz(maxitemid(1)), % First item collection ts('2007.08.11 05:12'), obtain('Tyris', 'TF Gun', [tfgun]), obtain('Tyris', 'Fish', [fish, dead]), obtain('Tyris', 'Squirrel teddy', [teddy, squirrel]), obtain('Tyris', 6, 'can of soda', [can, soda, unlabeled]), obtain('Tyris', 'red crystal inscribed with TF-gun code for Otter V1', [crystal, red, for(tfgun), tfgun('Otter V1')]), ts('2007.08.11 05:57'), obtain('Kalga', 'yarn ball of distraction', [ball, yarn, distraction]), ts('2007.08.11 08:02'), obtain('Cheez', 'shiny hubcap', [hubcap, shiny]), obtain('Cheez', 'can of mackerel', [can, mackerel]), obtain('Cheez', 'bag of salt-n-glass cookies', [bag, cookie, salt_n_glass]), ts('2007.08.11 12:46'), obtain('littlebeast', 'Infinite Scroll of Doom-Ish', [scroll, infinite, 'doom-ish']), obtain('littlebeast', 'Infinite Pen of Doom-Ish', [pen, infinite, 'doom-ish']), ts('2007.08.11 16:08'), obtain('littlebeast', 'DD/RR mod for TF Gun', [dd_rr_mod, 'DD/RR mod', for(tfgun)]), ts('2007.08.11 18:33'), obtain('Jenerix525', 'mirror with silver handle', [mirror, 'silver handle', 'not magical']), % Add above this line. true. % After consulting/loading this file: % % * To generate inventory list: %| ?- inv. % % * To generate blame list: %| ?- blame.