forked from ThomasMertes/seed7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindigo.dna
More file actions
318 lines (264 loc) · 11.9 KB
/
indigo.dna
File metadata and controls
318 lines (264 loc) · 11.9 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
(********************************************************************)
(* *)
(* dnafight.sd7 Bacterial dna fight programming game *)
(* Copyright (C) 1985 Johannes Gritsch *)
(* *)
(* This program is free software; you can redistribute it and/or *)
(* modify it under the terms of the GNU General Public License as *)
(* published by the Free Software Foundation; either version 2 of *)
(* the License, or (at your option) any later version. *)
(* *)
(* This program is distributed in the hope that it will be useful, *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
(* GNU General Public License for more details. *)
(* *)
(* You should have received a copy of the GNU General Public *)
(* License along with this program; if not, write to the *)
(* Free Software Foundation, Inc., 51 Franklin Street, *)
(* Fifth Floor, Boston, MA 02110-1301, USA. *)
(* *)
(********************************************************************)
const type: actionType is new enum
OnlyEat, Murder, Canibalism, OnlySplit
end enum;
const type: mainDir is direction; (* HERE .. LastDir *)
const type: mainDirArr is array [mainDir] mainDir;
const type: place is new struct
var bactColor: occupant is CLEAR;
var power: occSize is 0;
var power: foodMass is 0;
end struct;
const type: surrounding is array [direction] place;
const type: mvChangeTyp is array [direction] array [direction] direction;
const proc: dna (INDIGO) is func
local
const direction: firstDir is NORTH;
const direction: lastDir is EAST;
const direction: firstDiag is NW;
const direction: lastDiag is SE;
const bactColor: SELF is INDIGO;
(* Parameters *)
const power: ownSizeVal is 8; (* Value of own size *)
const power: hereFoodVal is 4; (* Value of Food at place *)
const power: thereFoodVal is 2; (* Value of Food at neighbour place in main direction *)
const power: overThFoodVal is 1; (* Value of Food at neighbour place in secondary dir *)
const power: killBonus is 1; (* Bonus for killing a stranger *)
const power: cannFine is 4; (* Fine for killing a relative *)
const mvChangeTyp: mvChange is (* new order of movement in Dir NORTH .. EAST *)
(* HERE, NORTH, SOUTH, WEST, EAST, NW, NE, SW, SE *)
[direction]([direction](HERE, HERE, HERE, HERE, HERE, HERE, HERE, HERE, HERE), (* unused *)
(* NORTH *) [direction](NORTH, SOUTH, HERE, NW, NE, SW, SE, WEST, EAST),
(* WEST *) [direction](SOUTH, HERE, NORTH, SW, SE, WEST, EAST, NW, NE),
(* SOUTH *) [direction](WEST, NW, SW, EAST, HERE, NE, NORTH, SE, SOUTH),
(* EAST *) [direction](EAST, NE, SE, HERE, WEST, NORTH, NW, SOUTH, SW));
const mainDirArr: oppositDir is [mainDir](HERE, SOUTH, NORTH, EAST, WEST);
var power: avFood is 0;
var power: avFoodMax is 0;
var power: ownSize is 0;
var power: splHereSize is 0;
var power: splThereSize is 0;
var lifeSpan: ownHunger is 0;
var integer: foodSum is 0;
var integer: foodFields is 0;
var directSet: mvOptDir is directSet.EMPTY_SET;
var directSet: splOptDir is directSet.EMPTY_SET;
var boolean: doSplit is FALSE;
var integer: maxState is 0;
var direction: dir is HERE;
var surrounding: landscape is direction times place.value;
const proc: initSurr (inout surrounding: landscape) is func
local
var direction: dir is HERE;
begin (* initSurr *)
foodSum := 0;
avFoodMax := 0;
foodFields := 9;
ownHunger := hunger;
ownSize := strength(HERE);
splThereSize := ownSize div 2;
splHereSize := ownSize - splThereSize;
for dir range HERE to lastDiag do
landscape[dir].occupant := view(dir);
landscape[dir].occSize := strength(dir);
landscape[dir].foodMass := food(dir);
foodSum +:= landscape[dir].foodMass;
if landscape[dir].occupant = EDGE then
decr(foodFields)
elsif landscape[dir].occupant = CLEAR then
if avFoodMax < landscape[dir].foodMass then
avFoodMax := landscape[dir].foodMass;
end if;
elsif dir <> HERE and
landscape[dir].occSize < avFoodMax and
landscape[dir].occSize <= ownSize then
avFoodMax := landscape[dir].occSize;
end if;
end for;
avFood := foodSum div foodFields + 1;
end func; (* initSurr *)
const proc: setSurr (inout place: surr, in bactColor: setColor, in power: setSize, in power: setFood) is func
begin (* setSurr *)
surr.occupant := setColor;
surr.occSize := setSize;
surr.foodMass := setFood
end func; (* setSurr *)
const func power: showNextSize (in power: ownSize, in place: surrField) is func
result
var power: nextSize is 0;
begin (* showNextSize *)
if surrField.occupant = EDGE then
nextSize := 0;
else
if surrField.occupant = CLEAR then
nextSize := nextSize(ownSize, surrField.foodMass, MAXLIFESPAN);
else
if surrField.occSize > ownSize then
nextSize := 0;
else
nextSize := nextSize(ownSize, surrField.occSize, MAXLIFESPAN);
end if;
end if;
end if;
end func; (* showNextSize *)
const func power: possNextSize (in surrounding: surr, in direction: dir) is func
result
var power: nextSize is 0;
begin (* possNextSize *)
nextSize := nextSize(surr[dir].occSize, surr[dir].foodMass, MAXLIFESPAN);
nextSize := max(nextSize, showNextSize(surr[dir].occSize, surr[left[dir]]));
nextSize := max(nextSize, showNextSize(surr[dir].occSize, surr[right[dir]]));
end func; (* possNextSize *)
const proc: feed (inout place: surr, in power: foe, in bactColor: foeCol) is func
(* assumes surr.occSize to be ownSize, foe to be size of previous owner *)
local
var power: dinner is 0;
begin (* feed *)
if foeCol = CLEAR then
dinner := min(surr.foodMass, surr.occSize);
surr.foodMass := surr.foodMass - dinner;
surr.occSize := nextSize(surr.occSize, dinner, ownHunger);
else
surr.occSize := nextSize(surr.occSize, foe, ownHunger);
end if;
end func; (* feed *)
const func integer: stateVal (in surrounding: surr, in actionType: wotHap) is func
result
var integer: state is 0;
local
var direction: dir is HERE;
var power: foeSize is 0;
begin (* stateVal *)
state := ownSizeVal * surr[HERE].occSize +
hereFoodVal * surr[HERE].foodMass;
for dir range firstDir to lastDir do
case surr[dir].occupant of
when {EDGE}: noop;
when {CLEAR}: state +:= thereFoodVal * surr[dir].foodMass;
when {SELF}: state +:= -cannFine * possNextSize(surr, dir) +
thereFoodVal * surr[dir].foodMass;
otherwise:
if surr[dir].occSize < surr[HERE].occSize then
foeSize := possNextSize(surr, dir);
if foeSize <= surr[HERE].occSize then
state +:= foeSize * thereFoodVal + killBonus +
overThFoodVal * surr[dir].foodMass;
end if;
end if;
end case;
end for;
for dir range firstDiag to lastDiag do
if surr[dir].occupant <> EDGE then
state +:= overThFoodVal * surr[dir].foodMass;
end if;
end for;
end func; (* stateVal *)
const func integer: doMove (in direction: dir, in boolean: doSplit) is func
result
var integer: state is 0;
local
var surrounding: newSurr is direction times place.value;
var surrounding: splitSurr is direction times place.value;
var direction: di is HERE;
var direction: oldPlace is HERE;
var power: splitSize is 0;
begin
if dir = HERE then
newSurr := landscape;
feed(newSurr[HERE], 0, CLEAR);
state := stateVal(newSurr, OnlyEat);
elsif landscape[dir].occupant = EDGE or
landscape[dir].occupant > CLEAR and (landscape[dir].occSize > 0 or doSplit) then
state := 0;
else
oldPlace := oppositDir[dir];
for di range HERE to lastDiag do
newSurr[di] := landscape[mvChange[dir][di]];
end for;
if landscape[left[dir]].occupant = EDGE then
(* movement along the left edge *)
setSurr(newSurr[right[dir]], CLEAR, 0, avFood);
elsif landscape[right[dir]].occupant = EDGE then
(* movement along the right edge *)
setSurr(newSurr[left[dir]], CLEAR, 0, avFood);
else
setSurr(newSurr[right[dir]], CLEAR, 0, avFood);
setSurr(newSurr[left[dir]], CLEAR, 0, avFood);
end if;
if doSplit then
splitSize := ownSize div 2;
newSurr[HERE].occSize := splitSize;
newSurr[oldPlace].occSize := ownSize - splitSize;
splitSurr := landscape;
setSurr(splitSurr[dir], SELF, splitSize, splitSurr[dir].foodMass);
splitSurr[HERE].occSize := ownSize - splitSize;
feed(newSurr[HERE], 0, CLEAR);
feed(newSurr[oldPlace], 0, CLEAR);
feed(splitSurr[dir], 0, CLEAR);
feed(splitSurr[HERE], 0, CLEAR);
state := stateVal(newSurr, OnlySplit) + stateVal(splitSurr, OnlySplit);
else
setSurr(newSurr[HERE], SELF, landscape[dir].occSize, landscape[dir].foodMass);
setSurr(newSurr[oldPlace], CLEAR, 0, landscape[HERE].foodMass);
feed(newSurr[dir], landscape[dir].occSize, landscape[dir].occupant);
state := stateVal(newSurr, actionType conv (ord(landscape[dir].occupant > CLEAR) +
ord(landscape[dir].occupant = SELF)));
end if;
end if;
end func; (* doMove *)
const proc: compare (in direction: dir, in integer: newState,
inout directSet: optDir, inout directSet: notOptDir,
inout integer: maxState) is func
begin (* compare *)
if newState > maxState then
maxState := newState;
optDir := {dir};
notOptDir := directSet.EMPTY_SET;
elsif newState = maxState then
incl(optDir, dir);
end if;
end func; (* compare *)
begin (* dna (INDIGO) *)
initSurr(landscape);
maxState := doMove(HERE, FALSE);
mvOptDir := {HERE};
splOptDir := directSet.EMPTY_SET;
for dir range firstDir to lastDir do
if landscape[dir].occupant <> EDGE then
compare(dir, doMove(dir, FALSE), mvOptDir, splOptDir, maxState);
if landscape[dir].occupant = CLEAR then
compare(dir, doMove(dir, TRUE), splOptDir, mvOptDir, maxState);
end if;
end if;
end for;
if splOptDir <> directSet.EMPTY_SET then
split(ranDir(splOptDir), splHereSize, splThereSize);
else
dir := ranDir(mvOptDir);
if view(dir) <> CLEAR and dir <> HERE then
kill(dir);
else
eat(dir, ownSize);
end if;
end if;
end func; (* dna (INDIGO) *)