-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifepaths.pl
179 lines (140 loc) · 5.89 KB
/
lifepaths.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
:- use_module(gold_revised).
lifepath(Id) :- lifepath(Id, _, _, _).
lp_stock(id(_, Setting), Stock) :-
setting(Setting, Stock).
lp_leads(id(Name, Setting), Leads):-
lifepath(id(Name, Setting), _, _, LpLeads),
(LpLeads = any_except(Exclude)
-> (
setting(Setting, Stock),
findall(S, setting(S, Stock), AllSettings),
subtract(AllSettings, Exclude, Leads)
)
;
(
lifepath(id(Name, Setting), _, _, Leads)
)
).
lp_years(Id, Years):- lifepath(Id, _, Years, _).
% born is a common enough cast that its worth handling specifically
is_born_lifepath(id(son_of_a_gun, _)).
is_born_lifepath(id(Id, _)) :-
(atom_concat(born_, _, Id);
atom_concat(_, "_born", Id)),
!.
requirement_is_constraint(constraint(_)).
% exists(Goal, List) checks if any item in the List satisfies Goal
% roughly the existential version of foreach.
exists(_, []) :- fail.
exists(Goal, [Head|Tail]) :-
call(Goal, Head), !; exists(Goal, Tail).
% satisfy_requirement resolves individual lifepath requirements
satisfy_requirement(flag(FlagName), CharProps, Lifepaths) :-
(
exists([Lifepath]>>lifepath_provides(Lifepath, flag(FlagName)), Lifepaths)
; (memberchk(flag(FlagName), CharProps))
), !.
satisfy_requirement(trait(TraitName), _, Lifepaths) :-
exists([Lifepath]>>lifepath_provides(Lifepath, trait(TraitName)), Lifepaths).
satisfy_requirement(lifepath(Lifepath), _, Lifepaths) :-
member(id(Lifepath, _), Lifepaths), !.
satisfy_requirement(setting(Setting), _, Lifepaths) :-
member(id(_, Setting), Lifepaths), !.
satisfy_requirement(position(N), _, Lifepaths) :-
length(Lifepaths, Len),
Len =:= N - 1.
satisfy_requirement(min(age, N), _, Lifepaths) :-
character_age(Lifepaths, Age),
N =< Age.
satisfy_requirement(max(age, N), _, Lifepaths) :-
character_age(Lifepaths, Age),
Age =< N.
% boolean requirements operations
satisfy_requirement(not(Requirement), CharProps, Lifepaths) :-
\+ Requirement = constraint(_),
\+ satisfy_requirement(Requirement, CharProps, Lifepaths).
satisfy_requirement(or(Requirements), CharProps, Lifepaths) :-
exists(([Requirement]>>satisfy_requirement(Requirement, CharProps, Lifepaths)), Requirements).
satisfy_requirement(and(Requirements), CharProps, Lifepaths) :-
foreach(member(Requirement, Requirements), satisfy_requirement(Requirement, CharProps, Lifepaths)).
% constraints are collected and checked later. skip them now.
satisfy_requirement(constraint(_), _, _).
map_satreq(CharProps, Lifepaths, Constraint) :-
satisfy_requirement(Constraint, CharProps, Lifepaths).
satisfies_requirements(Lifepath, CharProps, ChosenLifepaths, Constraints) :-
% lifepaths with no requirements are automatically satisfied
(
\+ lifepath_requires(Lifepath, _),
Constraints = []
)
;
% otherwise satisfy the specified
(
findall(
Requirements, (
lifepath_requires(Lifepath, Requirements),
maplist(map_satreq(CharProps, ChosenLifepaths), Requirements)
),
AllReqs
),
member(Reqs, AllReqs),
findall(C, (member(R, Reqs), (constraint(C) = R)), Constraints)
)
.
satisfy_constraint(max(lifepaths, N), Lifepaths) :-
length(Lifepaths, LN),
LN =< N.
satisfy_constraint(min(age, N), Lifepaths) :-
character_age(Lifepaths, Age),
N =< Age.
satisfies_constraints([], _).
satisfies_constraints(Constraints, Lifepaths) :-
member(Constraint, Constraints),
satisfy_constraint(Constraint, Lifepaths), !.
% available_lifepaths produces a an available lifepath to choose
% (and any constraints) based on the lifepaths taken so far.
% constraints are extracted from requirements, and are used to check
% a character once all lifepaths have been chosen.
available_lifepath([], _, Available, []) :-
lifepath(Available),
is_born_lifepath(Available).
available_lifepath(ChosenLifepaths, CharProps, Available, Constraints) :-
ChosenLifepaths = [LastLp|_],
LastLp = id(_, LastSetting),
lp_leads(LastLp, Leads),
id(_, Setting) = Available,
member(Setting, [LastSetting|Leads]),
lifepath(Available), % confirm that the lifepath exists.
\+ is_born_lifepath(Available),
satisfies_requirements(Available, CharProps, ChosenLifepaths, Constraints).
character_age([], 0).
character_age([Lifepath|Lifepaths], Age) :-
lp_years(Lifepath, Years),
character_age(Lifepaths, PriorAge),
Age is PriorAge + Years.
character_path_(_, [], _, []).
character_path_(CharProps, [First|Rest], Selected, Constraints) :-
available_lifepath(Selected, CharProps, First, NewConstraints),
append(NewConstraints, LaterConstraints, Constraints),
character_path_(CharProps, Rest, [First|Selected], LaterConstraints).
normalize_lifepath_name(-(Name, Setting), id(Name, Setting)) :- !.
normalize_lifepath_name(id(Name, Setting), id(Name, Setting)) :- !.
normalize_lifepath_name(Name, Normalized) :-
(
findall(Setting, lifepath(id(Name, Setting), _, _, _), [Setting]),
Normalized = id(Name, Setting), !
) ; throw(ambiguous_lifepath(Name)).
% currently we only support one type of prop, and that is flags
normalize_character_property(flag(Prop), flag(Prop)).
normalize_character_property(Prop, flag(Prop)).
% character_path(LifepathNames)
%
% character_path checks a set of lifepaths against the constraints.
% This is the main event.
character_path(_, []) :- fail.
character_path(CharProps, LifePaths) :-
maplist(normalize_lifepath_name, LifePaths, NormalizedLifePaths),
maplist(normalize_character_property, CharProps, NormalizedProps),
character_path_(NormalizedProps, NormalizedLifePaths, [], Constraints),
satisfies_constraints(Constraints, NormalizedLifePaths).
character_path(Lifepaths) :- character_path([], Lifepaths).