-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifeview.cpp
504 lines (438 loc) · 13.6 KB
/
lifeview.cpp
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Copyright (c) 2017-2023 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <luz@plan44.ch>
//
// This file is part of p44lrgraphics.
//
// p44lrgraphics 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 3 of the License, or
// (at your option) any later version.
//
// p44lrgraphics 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 p44lrgraphics. If not, see <http://www.gnu.org/licenses/>.
//
#include "lifeview.hpp"
#include "utils.hpp"
using namespace p44;
// MARK: ===== LifeView
LifeView::LifeView() :
mGenerationInterval(777*MilliSecond),
mLastGeneration(Never),
mStaticcount(0),
mMaxStatic(23),
mMinStatic(10),
mMinPopulation(9)
{
// original coloring scheme was yellow-green for newborn cells
setForegroundColor({ 128, 255, 0, 0 });
mHueGradient = -330.0/360*100;
mSatGradient = 0;
mBriGradient = -25;
}
LifeView::~LifeView()
{
}
void LifeView::clear()
{
stopAnimations();
mCells.clear();
prepareCells();
}
bool LifeView::prepareCells()
{
PixelPoint csz = getContentSize();
int numCells = csz.x*csz.y;
if (numCells<=0) return false;
if (numCells!=mCells.size()) {
mCells.clear();
for (int i=0; i<numCells; ++i) {
mCells.push_back(0);
}
makeDirty();
}
return true;
}
void LifeView::setGenerationInterval(MLMicroSeconds aInterval)
{
mGenerationInterval = aInterval;
}
#define MIN_SPAWN_START 3
MLMicroSeconds LifeView::step(MLMicroSeconds aPriorityUntil, MLMicroSeconds aNow)
{
MLMicroSeconds nextCall = inherited::step(aPriorityUntil, aNow);
if (mAlpha>0 && mGenerationInterval!=Never && aNow>=mLastGeneration+mGenerationInterval) {
mLastGeneration = aNow;
nextGeneration();
updateNextCall(nextCall, aNow+mGenerationInterval);
}
return nextCall;
}
void LifeView::recalculateColoring()
{
pixelToHsb(mForegroundColor, mHue, mSaturation, mBrightness);
inherited::recalculateColoring();
}
void LifeView::nextGeneration()
{
calculateGeneration();
if (mDynamics==0) {
mStaticcount++;
LOG(LOG_NOTICE, "No dynamics for %d cycles, population is %d", mStaticcount, mPopulation);
if (mStaticcount>mMaxStatic || (mPopulation<mMinPopulation && mStaticcount>mMinStatic)) {
revive();
}
}
else {
if (mStaticcount>0) {
mStaticcount -= 2;
if (mStaticcount<0) mStaticcount = 0;
LOG(LOG_NOTICE, "Dynamics (%+d) in this cycle, population is %d, reduced staticount to %d", mDynamics, mPopulation, mStaticcount);
}
else {
LOG(LOG_INFO, "Dynamics (%+d) in this cycle, population is %d", mDynamics, mPopulation);
}
}
makeDirty();
}
void LifeView::revive()
{
// shoot in some new cells
createRandomCells((int)mCells.size()/20+1,(int)mCells.size()/6+1);
makeDirty();
}
int LifeView::cellindex(int aX, int aY, bool aWrap)
{
if (aX<0) {
if (!aWrap) return (int)mCells.size(); // out of range
aX += mContent.dx;
}
else if (aX>=mContent.dx) {
if (!aWrap) return (int)mCells.size(); // out of range
aX -= mContent.dx;
}
if (aY<0) {
if (!aWrap) return (int)mCells.size(); // out of range
aY += mContent.dy;
}
else if (aY>=mContent.dy) {
if (!aWrap) return (int)mCells.size(); // out of range
aY -= mContent.dy;
}
return aY*mContent.dx+aX;
}
void LifeView::calculateGeneration()
{
mPopulation = 0;
mDynamics = 0;
if (!prepareCells()) return;
// cell age 0 : dead for longer
// cell age 1 : killed in this cycle
// cell age 2...n : living, with
// - 2 = created out of void
// - 3 = spawned
// - 4..n aged
// first age all living cells by 1, clean those that were killed in last cycle
for (int i=0; i<mCells.size(); ++i) {
int age = mCells[i];
if (age==1) {
mCells[i] = 0;
}
else if (age==2) {
mCells[i] = 4; // skip 3
}
else if (age>2) {
mCells[i]++; // just age normally
}
}
// apply rules
for (int x=0; x<mContent.dx; ++x) {
for (int y=0; y<mContent.dy; y++) {
int ci = cellindex(x, y, false);
// calculate number of neighbours
int nn = 0;
for (int dx=-1; dx<2; dx+=1) {
for (int dy=-1; dy<2; dy+=1) {
if (dx!=0 || dy!=0) {
// one of the 8 neighbours
int nci = cellindex(x+dx, y+dy, true);
if (nci<mCells.size() && (mCells[nci]>3 || mCells[nci]==1)) {
// is a living neighbour (exclude newborns of this cycle, include those that will die at end of this cycle
nn++;
}
}
}
}
// rules for living cells:
if (mCells[ci]>0) {
// - Any live cell with fewer than two live neighbours dies,
// as if caused by underpopulation.
// - Any live cell with more than three live neighbours dies, as if by overpopulation.
if (nn<2 || nn>3) {
mCells[ci] = 1; // will die at end of this cycle (but still alive for calculation)
mDynamics--; // a dying cell is a transition
}
else {
mPopulation++; // lives on, counts for population
}
// - Any live cell with two or three live neighbours lives on to the next generation.
}
// rule for dead cells:
else {
// - Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
if (nn==3) {
mCells[ci] = 3; // spawned
mDynamics++; // a new cell is a transition
mPopulation++; // now lives, counts for population
}
}
}
}
}
void LifeView::createRandomCells(int aMinCells, int aMaxCells)
{
if (!prepareCells()) return;
int numcells = aMinCells + rand() % (aMaxCells-aMinCells+1);
while (numcells-- > 0) {
int ci = rand() % mCells.size();
mCells[ci] = 2; // created out of void
}
makeDirty();
}
typedef struct {
int x;
int y;
} PixPos;
typedef struct {
const char* name;
int numpix;
const PixPos *pixels;
} PixPattern;
static const PixPos dot[] = { {0,0} };
static const PixPos blinker[] = { {0,-1}, {0,0}, {0,1} };
static const PixPos toad[] = { {1,0}, {2,0}, {3,0}, {0,1}, {1,1}, {2,1} };
static const PixPos beacon[] = { {0,0}, {1,0}, {0,1}, {3,2}, {2,3}, {3,3} };
static const PixPos pentadecathlon[] = {
{-1,-5}, {0,-5}, {1,-5},
{0,-4},
{0,-3},
{-1,-2}, {0,-2}, {1,-2},
{-1,0}, {0,0}, {1,0},
{-1,1}, {0,1}, {1,1},
{-1,3}, {0,3}, {1,3},
{0,4},
{0,5},
{-1,6}, {0,6}, {1,6}
};
static const PixPos rpentomino[] = { {0,-1}, {1,-1}, {-1,0}, {0,0}, {1,1} };
static const PixPos diehard[] = { {-3,0}, {-2,0}, {-2,1}, {2,1}, {3,-1}, {3,1}, {4,1} };
static const PixPos acorn[] = { {-3,1}, {-2,-1}, {-2,1}, {0,0}, {1,1}, {2,1}, {3,1} };
static const PixPos glider[] = { {0,-1}, {1,0}, {-1,1}, {0,1}, {1,1} };
#define PAT(n, x) { n, sizeof(x)/sizeof(PixPos), x }
static const PixPattern patterns[] = {
PAT("dot", dot), // single pixel
PAT("blinker", blinker),
PAT("toad", toad),
PAT("beacon", beacon),
PAT("pentadecathlon", pentadecathlon),
PAT("rpentomino", rpentomino),
PAT("diehard", diehard),
PAT("acorn", acorn),
PAT("glider", glider)
};
#define NUMPATTERNS (sizeof(patterns)/sizeof(PixPattern))
void LifeView::placePattern(uint16_t aPatternNo, bool aWrap, int aCenterX, int aCenterY, int aOrientation)
{
if (!prepareCells()) return;
if (aPatternNo>=NUMPATTERNS) return;
if (aCenterX<0) aCenterX = rand() % mContent.dx;
if (aCenterY<0) aCenterY = rand() % mContent.dy;
if (aOrientation<0) aOrientation = rand() % 4;
for (int i=0; i<patterns[aPatternNo].numpix; i++) {
int x,y;
PixPos px = patterns[aPatternNo].pixels[i];
switch (aOrientation) {
default: x=aCenterX+px.x; y=aCenterY+px.y; break;
case 1: x=aCenterX+px.y; y=aCenterY-px.x; break;
case 2: x=aCenterX-px.x; y=aCenterY-px.y; break;
case 3: x=aCenterX-px.y; y=aCenterY+px.x; break;
}
int ci = cellindex(x, y, aWrap);
mCells[ci] = 2; // created out of void
}
}
PixelColor LifeView::contentColorAt(PixelPoint aPt)
{
PixelColor pix = transparent;
// simplest colorisation: from yellow (young) to red
int ci = cellindex(aPt.x, aPt.y, false);
if (ci>=mCells.size()) return pix; // out of range
pix = mBackgroundColor;
int age = mCells[ci];
if (age<2) return pix; // dead, background
// fixed, not dependent on color set
if (age==2) {
// artificially created
pix.b = 255;
pix.r = 200;
pix.g = 220;
return pix;
}
// dependent on foreground color
double cellHue;
double cellBrightness;
double cellSaturation;
if (age==3) {
// just born - leave the color as-is
cellHue = mHue;
cellBrightness = 1.0;
cellSaturation = mSaturation;
}
else {
age -= 3;
if (age>57) age = 57; // limit
cellHue = cyclic(mHue+mHueGradient*660/7.3+(mHueGradient*660/66*age),0,360);
if (cellHue<0) cellHue+=360;
cellBrightness = limited(mBrightness+(mBriGradient/10*age),0,1);
cellSaturation = limited(mSaturation+(mSatGradient/10*age),0,1);
}
pix = hsbToPixel(cellHue, cellSaturation, cellBrightness);
return pix;
}
#if ENABLE_VIEWCONFIG
// MARK: ===== view configuration
ErrorPtr LifeView::configureView(JsonObjectPtr aViewConfig)
{
JsonObjectPtr o;
ErrorPtr err = inherited::configureView(aViewConfig);
if (Error::isOK(err)) {
#if !ENABLE_P44SCRIPT
if (aViewConfig->get("generationinterval", o)) {
mGenerationInterval = o->doubleValue()*Second;
}
if (aViewConfig->get("maxstatic", o)) {
mMaxStatic = o->int32Value();
}
if (aViewConfig->get("minstatic", o)) {
mMinStatic = o->int32Value();
}
if (aViewConfig->get("minpopulation", o)) {
mMinPopulation = o->int32Value();
}
#endif // !ENABLE_P44SCRIPT
if (aViewConfig->get("addrandom", o)) {
int c = o->int32Value();
createRandomCells(c/3, c);
}
if (aViewConfig->get("addpattern", o)) {
int p = o->int32Value();
placePattern(p);
}
}
return err;
}
#endif // ENABLE_VIEWCONFIG
#if ENABLE_VIEWSTATUS && !ENABLE_P44SCRIPT
/// @return the current status of the view, in the same format as accepted by configure()
JsonObjectPtr LifeView::viewStatus()
{
JsonObjectPtr status = inherited::viewStatus();
status->add("generationinterval", JsonObject::newDouble(getGenerationIntervalS());
status->add("maxstatic", JsonObject::newInt32(getMaxStatic()));
status->add("minstatic", JsonObject::newInt32(getMinStatic()));
status->add("minpopulation", JsonObject::newInt32(getMinPopulation()));
return status;
}
#endif // ENABLE_VIEWSTATUS
#if ENABLE_P44SCRIPT
using namespace P44Script;
ScriptObjPtr LifeView::newViewObj()
{
// base class with standard functionality
return new LifeViewObj(this);
}
// addrandom(amount)
FUNC_ARG_DEFS(addrandom, { numeric|optionalarg }, { numeric|optionalarg} );
static void addrandom_func(BuiltinFunctionContextPtr f)
{
LifeViewObj* v = dynamic_cast<LifeViewObj*>(f->thisObj().get());
assert(v);
int max = f->arg(0)->intValue();
if (max<=0) {
v->life()->revive();
}
else {
int min = max/3;
if (f->arg(1)->defined()) {
min = max;
max = f->arg(1)->intValue();
}
v->life()->createRandomCells(min, max);
}
f->finish();
}
// placepattern(pattern, [ x, y, orientation])
FUNC_ARG_DEFS(placepattern, { numeric|text }, { numeric|null|optionalarg }, { numeric|null|optionalarg }, { numeric|null|optionalarg } );
static void placepattern_func(BuiltinFunctionContextPtr f)
{
LifeViewObj* v = dynamic_cast<LifeViewObj*>(f->thisObj().get());
assert(v);
int pat;
if (f->arg(0)->hasType(numeric)) {
pat = f->arg(0)->intValue();
}
else {
for (pat=0; pat<NUMPATTERNS; pat++) {
if (uequals(f->arg(0)->stringValue(), patterns[pat].name)) break;
}
}
if (pat<NUMPATTERNS) {
int x = -1; // random center X
if (f->arg(1)->defined()) x = f->arg(1)->intValue();
int y = -1; // random center Y
if (f->arg(2)->defined()) y = f->arg(2)->intValue();
int o = -1; // random orientation
if (f->arg(3)->defined()) o = f->arg(3)->intValue();
v->life()->placePattern(pat, true, x, y, o);
}
f->finish();
}
#define ACCESSOR_CLASS LifeView
static ScriptObjPtr property_accessor(BuiltInMemberLookup& aMemberLookup, ScriptObjPtr aParentObj, ScriptObjPtr aObjToWrite, const struct BuiltinMemberDescriptor* aMemberDescriptor)
{
ACCFN_DEF
LifeViewPtr view = reinterpret_cast<ACCESSOR_CLASS*>(reinterpret_cast<LifeViewObj*>(aParentObj.get())->life().get());
ACCFN acc = reinterpret_cast<ACCFN>(aMemberDescriptor->memberAccessInfo);
view->announceChanges(true);
ScriptObjPtr res = acc(*view, aObjToWrite);
view->announceChanges(false);
return res;
}
ACC_IMPL_DBL(GenerationIntervalS);
ACC_IMPL_INT(MaxStatic);
ACC_IMPL_INT(MinStatic);
ACC_IMPL_INT(MinPopulation);
static const BuiltinMemberDescriptor lifeMembers[] = {
FUNC_DEF_W_ARG(addrandom, executable),
FUNC_DEF_W_ARG(placepattern, executable),
// property accessors
ACC_DECL("generationinterval", numeric|lvalue, GenerationIntervalS),
ACC_DECL("maxstatic", numeric|lvalue, MaxStatic),
ACC_DECL("minstatic", numeric|lvalue, MinStatic),
ACC_DECL("minpopulation", numeric|lvalue, MinPopulation),
{ NULL } // terminator
};
static BuiltInMemberLookup* sharedLifeMemberLookupP = NULL;
LifeViewObj::LifeViewObj(P44ViewPtr aView) :
inherited(aView)
{
registerSharedLookup(sharedLifeMemberLookupP, lifeMembers);
}
#endif // ENABLE_P44SCRIPT