-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixels_cpu.js
148 lines (127 loc) · 4.81 KB
/
pixels_cpu.js
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
// @ts-check
/**
* @import { PixelFight, PixelFightParams, PixelGameData } from "./@types/pixelfight";
*/
import Faction from "./faction.js";
/**
* @implements {PixelFight}
*/
export default class PixelFightCPU {
/** @type {Uint32Array[]} */ bufferA;
/** @type {Uint32Array[]} */ bufferB;
/** @type {0|1} */ useBuffer = 0;
/** @type {[Uint32Array[], Uint32Array[]]} */ buffers;
/** @type {BigUint64Array} */ count;
/** @type {bigint} */ iterations = 0n;
/** @type {PixelFightParams} */ params;
/** @type {HTMLCanvasElement} */ canvas;
/** @type {CanvasRenderingContext2D} */ context;
/**
* @param {PixelFightParams} params
*/
constructor(params) {
this.params = params;
this.canvas = document.createElement("canvas");
this.canvas.width = this.params.width;
this.canvas.height = this.params.height;
const ctx = this.canvas.getContext("2d");
if (!ctx) throw new Error("CanvasContext2D not supported");
this.context = ctx;
this.bufferA = new Array(this.params.width);
this.bufferB = new Array(this.params.width);
this.buffers = [this.bufferA, this.bufferB];
for (let i = 0; i < this.params.height; i++) {
this.bufferA[i] = new Uint32Array(this.params.width);
this.bufferB[i] = new Uint32Array(this.params.height);
}
this.count = new BigUint64Array(this.params.factions.length);
this.reset();
}
/**
* @returns {void}
*/
reset() {
this.iterations = 0n;
this.useBuffer = 0;
this.count.fill(0n, 0, this.params.factions.length);
// "Evenly" distrubute pixels to each side
for (let i = 0; i < this.params.width; i++) {
for (let j = 0; j < this.params.height; j++) {
let angle = Math.atan2(this.params.width/2 - i - 0.5, this.params.height/2 - j - 0.5) * 180 / Math.PI;
while (angle < 0) angle += 360;
const owner = Math.floor((360 - angle) / (360 / this.params.factions.length));
this.bufferA[i][j] = owner;
this.bufferB[i][j] = owner;
this.count[owner]++;
}
}
this.draw();
}
/**
* @returns {void}
*/
step() {
this.iterations++;
// Get and swap buffers
const bufferOld = this.buffers[this.useBuffer];
this.useBuffer = this.useBuffer === 0 ? 1 : 0;
const bufferNew = this.buffers[this.useBuffer];
// Loop for each pixel
for (let i = 0; i < this.params.width; ++ i) {
for (let j = 0; j < this.params.height; ++ j) {
const neighbors = [];
// Check neighbors above
if (i > 0) {
if (j > 0) neighbors.push(bufferOld[i-1][j-1])
neighbors.push(bufferOld[i-1][j]);
if (j < this.params.height-1) neighbors.push(bufferOld[i-1][j+1]);
}
// Check neighbors to the sides
if (j > 0) neighbors.push(bufferOld[i][j-1]);
if (j < this.params.height - 1) neighbors.push(bufferOld[i][j+1]);
// Check neighbors below
if (i < this.params.width-1) {
if (j > 0) neighbors.push(bufferOld[i+1][j-1])
neighbors.push(bufferOld[i+1][j]);
if (j < this.params.height - 1) neighbors.push(bufferOld[i+1][j+1]);
}
// Exchange owners
const oldOwner = bufferOld[i][j];
const newOwner = neighbors[Math.floor(Math.random() * (neighbors.length))];
bufferNew[i][j] = newOwner;
this.count[oldOwner]--;
this.count[newOwner]++;
}
}
this.draw();
this.params.updateGameData({
iterations: this.iterations,
factions: this.params.factions,
counts: this.count,
});
}
/**
* @returns {void}
*/
draw() {
const gameBuffer = this.buffers[this.useBuffer];
const drawBuffer = this.context.createImageData(this.params.width, this.params.height);
for (let i = 0; i < this.params.width; i++) {
for (let j = 0; j < this.params.height; j++) {
let color = this.params.factions[gameBuffer[i][j]].rgb;
let d = (i + j * this.params.width) * 4;
drawBuffer.data[d+0] = color >> 16;
drawBuffer.data[d+1] = (color >> 8) & 255;
drawBuffer.data[d+2] = color & 255;
drawBuffer.data[d+3] = 0xFF;
}
}
this.context.putImageData(drawBuffer, 0, 0);
}
/**
* @returns {HTMLCanvasElement}
*/
getCanvas() {
return this.canvas;
}
}