<commit_before>
#include <nanogui/nanogui.h>
#if defined(WIN32)
#include <windows.h>
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif
#include <nanogui/glutil.h>
#include <iostream>
#include <vector>
#include <memory>
#include <utility>
#include "optional.hpp"
#include "matrix.h"
#include "shader.h"
#include "crosshair_shader.h"
#include "block.h"
#include "chunk.h"
#include "world.h"
#include "chunk_shader.h"
#include "sky_shader.h"
#include "selection_shader.h"
#include "hud.h"
#include "hud_shader.h"
#include "textures.h"
#include "client.h"
#include "util.h"

#define KONSTRUCTS_APP_TITLE "Konstructs"
#define KONSTRUCTS_APP_WIDTH 854
#define KONSTRUCTS_APP_HEIGHT 480
#define MAX_PENDING_CHUNKS 256
#define KONSTRUCTS_KEY_FORWARD 'W'
#define KONSTRUCTS_KEY_BACKWARD 'S'
#define KONSTRUCTS_KEY_LEFT 'A'
#define KONSTRUCTS_KEY_RIGHT 'D'
#define KONSTRUCTS_KEY_JUMP GLFW_KEY_SPACE
#define KONSTRUCTS_KEY_FLY GLFW_KEY_TAB
#define KONSTRUCTS_KEY_INVENTORY 'E'
using std::cout;
using std::cerr;
using std::endl;
using namespace konstructs;
using nonstd::optional;
using nonstd::nullopt;
using std::pair;

void print_usage();

class Konstructs: public nanogui::Screen {
public:
    Konstructs(const string &hostname, const string &nick, const string &hash) :
        nanogui::Screen(Eigen::Vector2i(KONSTRUCTS_APP_WIDTH, KONSTRUCTS_APP_HEIGHT), KONSTRUCTS_APP_TITLE),
        player(0, Vector3f(0.0f, 0.0f, 0.0f), 0.0f, 0.0f),
        px(0), py(0),
        model_factory(blocks),
        client(),
        radius(10),
        fov(60.0f),
        near_distance(0.125f),
        sky_shader(radius, fov, SKY_TEXTURE, near_distance),
        chunk_shader(radius, fov, BLOCK_TEXTURES, SKY_TEXTURE, near_distance),
        hud_shader(17, 14, INVENTORY_TEXTURE, BLOCK_TEXTURES, FONT_TEXTURE),
        selection_shader(radius, fov, near_distance, 0.52),
        day_length(600),
        last_frame(glfwGetTime()),
        looking_at(nullopt),
        hud(17, 14),
        hud_interaction(false),
        menu_state(0),
        hud_selection(0) {

        using namespace nanogui;
        performLayout(mNVGContext);
        glfwSetInputMode(mGLFWWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
        if (nick.size() > 0 && hash.size() > 0 && hostname.size() > 0) {
            setup_connection(nick, hash, hostname);
        } else {
            init_menu(nick, hash, hostname);
        }
        blocks.is_plant[SOLID_BLOCK] = 0;
        blocks.is_obstacle[SOLID_BLOCK] = 1;
        blocks.is_transparent[SOLID_BLOCK] = 0;
        memset(&fps, 0, sizeof(fps));
        hud.set_background(Vector2i(4, 0), 3);
        hud.set_background(Vector2i(5, 0), 3);
        hud.set_background(Vector2i(6, 0), 3);
        hud.set_background(Vector2i(7, 0), 3);
        hud.set_background(Vector2i(8, 0), 3);
        hud.set_background(Vector2i(9, 0), 3);
        hud.set_background(Vector2i(10, 0), 3);
        hud.set_background(Vector2i(11, 0), 3);
        hud.set_background(Vector2i(12, 0), 3);
    }

    ~Konstructs() {
    }

    virtual bool scrollEvent(const Vector2i &p, const Vector2f &rel) {
        if (hud_selection <= 0 && rel[1] > 0) return true;
        if (hud_selection >= 8 && rel[1] < 0) return true;
        hud.set_background({ 4 + hud_selection, 0 }, 2);
        hud_selection -= 1 * rel[1];
        hud.set_background({ 4 + hud_selection, 0 }, 3);
        client.inventory_select(hud_selection);
    }

    virtual bool mouseButtonEvent(const Vector2i &p, int button, bool down, int modifiers) {
        if(hud_interaction) {
            if(down) {
                double x, y;
                glfwGetCursorPos(mGLFWWindow, &x, &y);

                auto clicked_at = hud_shader.clicked_at(x, y, mSize.x(), mSize.y());

                if(clicked_at) {
                    Vector2i pos = *clicked_at;
                    if(hud.active(pos)) {
                        int index = pos[0] + pos[1] * 17;
                        client.click_inventory(index);
                    }
                }
            }
        } else if(menu_state) {
            // menu open
        } else {
            glfwSetInputMode(mGLFWWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
            if(looking_at) {
                auto &l = *looking_at;
                if(button == GLFW_MOUSE_BUTTON_1 && down) {
                    client.click_at(1, l.second.position, 1);
                } else if(button == GLFW_MOUSE_BUTTON_2 && down) {
                    client.click_at(1, l.first.position, 2);
                } else if(button == GLFW_MOUSE_BUTTON_2 && down) {
                    client.click_at(1, l.second.position, 3);
                }
            }
        }
        return Screen::mouseButtonEvent(p, button, down, modifiers);
    }

    virtual bool keyboardEvent(int key, int scancode, int action, int modifiers) {
        if (Screen::keyboardEvent(key, scancode, action, modifiers))
            return true;
        if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
            if(hud_interaction) {
                close_hud();
            } else {
                glfwSetInputMode(mGLFWWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
            }
        } else if (key == GLFW_KEY_F1 && action == GLFW_PRESS) {
            // TODO: implement this again when time has come ;)
            /*if (!menu_state) {
                init_menu("","","");
            } else {
                hide_menu();
            }*/
        } else if (key == KONSTRUCTS_KEY_FLY && action == GLFW_PRESS) {
            player.fly();
        } else if(key == KONSTRUCTS_KEY_INVENTORY && action == GLFW_PRESS) {
            if(hud_interaction) {
                close_hud();
            } else if (client.is_connected()){
                client.click_at(0, Vector3i::Zero(), 3);
            }
        } else if(key > 48 && key < 58 && action == GLFW_PRESS) {
            client.inventory_select(key - 49);
        } else {
            return false;
        }
        return true;
    }

    virtual void draw(NVGcontext *ctx) {
        Screen::draw(ctx);
    }

    virtual void drawContents() {
        using namespace nanogui;
        update_fps(&fps);
        if (client.is_connected()) {
            handle_network();
            handle_keys();
        }
        handle_mouse();
        looking_at = player.looking_at(world, blocks);
        glClear(GL_DEPTH_BUFFER_BIT);
        for(auto model : model_factory.fetch_models()) {
            chunk_shader.add(model);
        }
        sky_shader.render(player, mSize.x(), mSize.y(), time_of_day());
        glClear(GL_DEPTH_BUFFER_BIT);
        int faces = chunk_shader.render(player, mSize.x(), mSize.y(),
                                        daylight(), time_of_day());
        if(looking_at) {
            selection_shader.render(player, mSize.x(), mSize.y(),
                                    looking_at->second.position);
        }
        //cout << "Faces: " << faces << " FPS: " << fps.fps << endl;
        glClear(GL_DEPTH_BUFFER_BIT);
        if(!hud_interaction && !menu_state)
            crosshair_shader.render(mSize.x(), mSize.y());
        double mx, my;
        glfwGetCursorPos(mGLFWWindow, &mx, &my);
        hud_shader.render(mSize.x(), mSize.y(), mx, my, hud, blocks);
    }
<commit_msg>Do not render selected block, if hud or menu open.<commit_after>
#include <nanogui/nanogui.h>
#if defined(WIN32)
#include <windows.h>
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif
#include <nanogui/glutil.h>
#include <iostream>
#include <vector>
#include <memory>
#include <utility>
#include "optional.hpp"
#include "matrix.h"
#include "shader.h"
#include "crosshair_shader.h"
#include "block.h"
#include "chunk.h"
#include "world.h"
#include "chunk_shader.h"
#include "sky_shader.h"
#include "selection_shader.h"
#include "hud.h"
#include "hud_shader.h"
#include "textures.h"
#include "client.h"
#include "util.h"

#define KONSTRUCTS_APP_TITLE "Konstructs"
#define KONSTRUCTS_APP_WIDTH 854
#define KONSTRUCTS_APP_HEIGHT 480
#define MAX_PENDING_CHUNKS 256
#define KONSTRUCTS_KEY_FORWARD 'W'
#define KONSTRUCTS_KEY_BACKWARD 'S'
#define KONSTRUCTS_KEY_LEFT 'A'
#define KONSTRUCTS_KEY_RIGHT 'D'
#define KONSTRUCTS_KEY_JUMP GLFW_KEY_SPACE
#define KONSTRUCTS_KEY_FLY GLFW_KEY_TAB
#define KONSTRUCTS_KEY_INVENTORY 'E'
using std::cout;
using std::cerr;
using std::endl;
using namespace konstructs;
using nonstd::optional;
using nonstd::nullopt;
using std::pair;

void print_usage();

class Konstructs: public nanogui::Screen {
public:
    Konstructs(const string &hostname, const string &nick, const string &hash) :
        nanogui::Screen(Eigen::Vector2i(KONSTRUCTS_APP_WIDTH, KONSTRUCTS_APP_HEIGHT), KONSTRUCTS_APP_TITLE),
        player(0, Vector3f(0.0f, 0.0f, 0.0f), 0.0f, 0.0f),
        px(0), py(0),
        model_factory(blocks),
        client(),
        radius(10),
        fov(60.0f),
        near_distance(0.125f),
        sky_shader(radius, fov, SKY_TEXTURE, near_distance),
        chunk_shader(radius, fov, BLOCK_TEXTURES, SKY_TEXTURE, near_distance),
        hud_shader(17, 14, INVENTORY_TEXTURE, BLOCK_TEXTURES, FONT_TEXTURE),
        selection_shader(radius, fov, near_distance, 0.52),
        day_length(600),
        last_frame(glfwGetTime()),
        looking_at(nullopt),
        hud(17, 14),
        hud_interaction(false),
        menu_state(0),
        hud_selection(0) {

        using namespace nanogui;
        performLayout(mNVGContext);
        glfwSetInputMode(mGLFWWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
        if (nick.size() > 0 && hash.size() > 0 && hostname.size() > 0) {
            setup_connection(nick, hash, hostname);
        } else {
            init_menu(nick, hash, hostname);
        }
        blocks.is_plant[SOLID_BLOCK] = 0;
        blocks.is_obstacle[SOLID_BLOCK] = 1;
        blocks.is_transparent[SOLID_BLOCK] = 0;
        memset(&fps, 0, sizeof(fps));
        hud.set_background(Vector2i(4, 0), 3);
        hud.set_background(Vector2i(5, 0), 3);
        hud.set_background(Vector2i(6, 0), 3);
        hud.set_background(Vector2i(7, 0), 3);
        hud.set_background(Vector2i(8, 0), 3);
        hud.set_background(Vector2i(9, 0), 3);
        hud.set_background(Vector2i(10, 0), 3);
        hud.set_background(Vector2i(11, 0), 3);
        hud.set_background(Vector2i(12, 0), 3);
    }

    ~Konstructs() {
    }

    virtual bool scrollEvent(const Vector2i &p, const Vector2f &rel) {
        if (hud_selection <= 0 && rel[1] > 0) return true;
        if (hud_selection >= 8 && rel[1] < 0) return true;
        hud.set_background({ 4 + hud_selection, 0 }, 2);
        hud_selection -= 1 * rel[1];
        hud.set_background({ 4 + hud_selection, 0 }, 3);
        client.inventory_select(hud_selection);
    }

    virtual bool mouseButtonEvent(const Vector2i &p, int button, bool down, int modifiers) {
        if(hud_interaction) {
            if(down) {
                double x, y;
                glfwGetCursorPos(mGLFWWindow, &x, &y);

                auto clicked_at = hud_shader.clicked_at(x, y, mSize.x(), mSize.y());

                if(clicked_at) {
                    Vector2i pos = *clicked_at;
                    if(hud.active(pos)) {
                        int index = pos[0] + pos[1] * 17;
                        client.click_inventory(index);
                    }
                }
            }
        } else if(menu_state) {
            // menu open
        } else {
            glfwSetInputMode(mGLFWWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
            if(looking_at) {
                auto &l = *looking_at;
                if(button == GLFW_MOUSE_BUTTON_1 && down) {
                    client.click_at(1, l.second.position, 1);
                } else if(button == GLFW_MOUSE_BUTTON_2 && down) {
                    client.click_at(1, l.first.position, 2);
                } else if(button == GLFW_MOUSE_BUTTON_2 && down) {
                    client.click_at(1, l.second.position, 3);
                }
            }
        }
        return Screen::mouseButtonEvent(p, button, down, modifiers);
    }

    virtual bool keyboardEvent(int key, int scancode, int action, int modifiers) {
        if (Screen::keyboardEvent(key, scancode, action, modifiers))
            return true;
        if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
            if(hud_interaction) {
                close_hud();
            } else {
                glfwSetInputMode(mGLFWWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
            }
        } else if (key == GLFW_KEY_F1 && action == GLFW_PRESS) {
            // TODO: implement this again when time has come ;)
            /*if (!menu_state) {
                init_menu("","","");
            } else {
                hide_menu();
            }*/
        } else if (key == KONSTRUCTS_KEY_FLY && action == GLFW_PRESS) {
            player.fly();
        } else if(key == KONSTRUCTS_KEY_INVENTORY && action == GLFW_PRESS) {
            if(hud_interaction) {
                close_hud();
            } else if (client.is_connected()){
                client.click_at(0, Vector3i::Zero(), 3);
            }
        } else if(key > 48 && key < 58 && action == GLFW_PRESS) {
            client.inventory_select(key - 49);
        } else {
            return false;
        }
        return true;
    }

    virtual void draw(NVGcontext *ctx) {
        Screen::draw(ctx);
    }

    virtual void drawContents() {
        using namespace nanogui;
        update_fps(&fps);
        if (client.is_connected()) {
            handle_network();
            handle_keys();
        }
        handle_mouse();
        looking_at = player.looking_at(world, blocks);
        glClear(GL_DEPTH_BUFFER_BIT);
        for(auto model : model_factory.fetch_models()) {
            chunk_shader.add(model);
        }
        sky_shader.render(player, mSize.x(), mSize.y(), time_of_day());
        glClear(GL_DEPTH_BUFFER_BIT);
        int faces = chunk_shader.render(player, mSize.x(), mSize.y(),
                                        daylight(), time_of_day());
        if(looking_at && !hud_interaction && !menu_state) {
            selection_shader.render(player, mSize.x(), mSize.y(),
                                    looking_at->second.position);
        }
        //cout << "Faces: " << faces << " FPS: " << fps.fps << endl;
        glClear(GL_DEPTH_BUFFER_BIT);
        if(!hud_interaction && !menu_state)
            crosshair_shader.render(mSize.x(), mSize.y());
        double mx, my;
        glfwGetCursorPos(mGLFWWindow, &mx, &my);
        hud_shader.render(mSize.x(), mSize.y(), mx, my, hud, blocks);
    }

{'fraction_non_alphanumeric': 0.12770919067215364, 'fraction_numerical': 0.01906721536351166, 'mean_word_length': 2.259780907668232, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5672462', 'n_tokens_mistral': 5148, 'n_tokens_neox': 4929, 'n_words': 955}