-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmap_slime_chunks.c
156 lines (126 loc) · 4.16 KB
/
map_slime_chunks.c
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
/*
Copyright (c) 2014 Timothy Goddard
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
* Generates a PGM file, indicating the probabilities of each chunk
* spawning slimes. The middle pixel is always cx 0, cz 0. Each
* pixel is a chunk.
*/
#define RADIUS 500
#define TOP -RADIUS
#define LEFT -RADIUS
#define BOTTOM RADIUS
#define RIGHT RADIUS
#define NUM_THREADS 8
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "slime_algo.h"
typedef struct {
const unsigned long* seeds;
int seedCount;
int start_z;
int end_z;
int* counts;
} run_state;
void *processing_thread(void* data) {
run_state *state = (run_state*) data;
unsigned long seed;
int i, x, z;
unsigned width = RIGHT - LEFT + 1;
for (z = state->start_z; (z < state->end_z) && (z <= BOTTOM); ++z) {
for (x = LEFT; x <= RIGHT; ++x) {
int count = 0;
unsigned long chunk_val = calc_chunk_val(x, z);
for (i = 0; i < state->seedCount; i++) {
unsigned long seed = state->seeds[i];
count += matches(chunk_val, seed);
}
state->counts[((z - TOP) * width) + (x - LEFT)] = count;
}
}
pthread_exit(0);
}
int main(int argc, char** argv) {
int width = RIGHT - LEFT + 1;
int height = BOTTOM - TOP + 1;
int* counts = (int*) malloc(sizeof(int) * width * height);
int x, z;
// Zero counts
memset(counts, 0, sizeof(int) * width * height);
// Load seed list
int max_seeds = 64;
unsigned long seed;
unsigned long* seeds = (unsigned long*) malloc(max_seeds * sizeof(unsigned long));
int seedCount = 0;
while (scanf("%lu\n", &seed) > 0) {
seeds[seedCount++] = seed;
if (seedCount > max_seeds) {
max_seeds *= 2;
seeds = (unsigned long*) realloc((void*) seeds, max_seeds * sizeof(unsigned long));
}
}
// Run threads to populate counts
pthread_t threads[NUM_THREADS];
run_state threadStates[NUM_THREADS];
int start_z = TOP;
int total_z = (BOTTOM - TOP + 1);
int i;
for (i = 0; i < NUM_THREADS; i++) {
threadStates[i].seeds = seeds;
threadStates[i].seedCount = seedCount;
threadStates[i].start_z = start_z + i * (total_z / NUM_THREADS);
if (i == NUM_THREADS - 1) {
threadStates[i].end_z = BOTTOM + 1;
} else {
threadStates[i].end_z = start_z + (i + 1) * (total_z / NUM_THREADS);
}
threadStates[i].counts = counts;
pthread_create(&threads[i], NULL, processing_thread, (void*) &threadStates[i]);
}
for (i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
// Image header
printf("P2\n");
printf("%d %d\n", RIGHT - LEFT + 1, BOTTOM - TOP + 1);
printf("255\n");
fflush(stdout);
// Output scaled concentrations
for (z = TOP; z <= BOTTOM; ++z) {
for (x = LEFT; x <= RIGHT; ++x) {
if (x != LEFT) {
printf(" ");
}
int count = counts[(z - TOP) * width + x - LEFT];
int shown;
if (count == 0) {
shown = 0;
} else if (count == seedCount) {
shown = 255;
} else {
float prop = ((float) count) / (float) (seedCount);
shown = (int) round(prop * 253) + 1;
}
printf("%d", shown);
}
printf("\n");
}
return 0;
}