|
| 1 | +/* |
| 2 | + * This file is part of the Minecraft Overviewer. |
| 3 | + * |
| 4 | + * Minecraft Overviewer is free software: you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU General Public License as published |
| 6 | + * by the Free Software Foundation, either version 3 of the License, or (at |
| 7 | + * your option) any later version. |
| 8 | + * |
| 9 | + * Minecraft Overviewer is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 12 | + * Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License along |
| 15 | + * with the Overviewer. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <math.h> |
| 19 | +#include "overlay.h" |
| 20 | + |
| 21 | +typedef struct { |
| 22 | + /* inherits from overlay */ |
| 23 | + RenderPrimitiveOverlay parent; |
| 24 | + int t_invisible; |
| 25 | + int delta_t; |
| 26 | +} RenderPrimitiveHeatmap; |
| 27 | + |
| 28 | +static void get_color(void* data, RenderState* state, |
| 29 | + uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) { |
| 30 | + RenderPrimitiveHeatmap* self = (RenderPrimitiveHeatmap*)data; |
| 31 | + long mtime; |
| 32 | + float _value_f; |
| 33 | + char value; |
| 34 | + PyObject *mtime_pyobj; |
| 35 | + |
| 36 | + // Set default values (will get overridden based on self->mode) |
| 37 | + *r = 255; |
| 38 | + *g = 0; |
| 39 | + *b = 0; |
| 40 | + |
| 41 | + // Get the chunk modified time |
| 42 | + mtime_pyobj = PyObject_CallMethod(state->regionset, "get_chunk_mtime", "ii", state->chunkx, state->chunkz); |
| 43 | + if (mtime_pyobj == NULL || mtime_pyobj == Py_None) { |
| 44 | + *a = 0; |
| 45 | + return; |
| 46 | + } |
| 47 | + mtime = PyLong_AsLong(mtime_pyobj); |
| 48 | + Py_XDECREF(mtime_pyobj); |
| 49 | + |
| 50 | + // Convert the time to a value in the range [0,255] based on t_invisible and delta_t |
| 51 | + _value_f = (mtime - self->t_invisible) / (float)self->delta_t; |
| 52 | + value = _value_f <= 0 ? 0 : (_value_f >= 1 ? 255 : 255*_value_f); |
| 53 | + *a = value; |
| 54 | +} |
| 55 | + |
| 56 | +static bool |
| 57 | +overlay_heatmap_start(void* data, RenderState* state, PyObject* support) { |
| 58 | + RenderPrimitiveHeatmap* self; |
| 59 | + int t_full; |
| 60 | + |
| 61 | + /* first, chain up */ |
| 62 | + bool ret = primitive_overlay.start(data, state, support); |
| 63 | + if (ret != false) |
| 64 | + return ret; |
| 65 | + |
| 66 | + /* now do custom initializations */ |
| 67 | + self = (RenderPrimitiveHeatmap*)data; |
| 68 | + self->parent.get_color = get_color; |
| 69 | + |
| 70 | + if (!render_mode_parse_option(support, "t_invisible", "I", &(self->t_invisible))) |
| 71 | + return true; |
| 72 | + |
| 73 | + if (!render_mode_parse_option(support, "t_full", "I", &t_full)) |
| 74 | + return true; |
| 75 | + |
| 76 | + self->delta_t = t_full - self->t_invisible; |
| 77 | + return false; |
| 78 | +} |
| 79 | + |
| 80 | +static void |
| 81 | +overlay_heatmap_finish(void* data, RenderState* state) { |
| 82 | + /* chain up */ |
| 83 | + primitive_overlay.finish(data, state); |
| 84 | +} |
| 85 | + |
| 86 | +RenderPrimitiveInterface primitive_overlay_heatmap = { |
| 87 | + "overlay-heatmap", |
| 88 | + sizeof(RenderPrimitiveHeatmap), |
| 89 | + overlay_heatmap_start, |
| 90 | + overlay_heatmap_finish, |
| 91 | + NULL, |
| 92 | + NULL, |
| 93 | + overlay_draw, |
| 94 | +}; |
0 commit comments