/file/game.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 
#pragma once
#ifdef __linux__
#else
#include <windows.h>
#endif
#include <sstream>
#include <iostream>
#include <unordered_map>
#ifdef WIN32
// this works
//#include <concurrent_unordered_map.h>
//using namespace Concurrency;
// this might work?
//#include "show/glew.h" // LAND
#else
// this does not work
//#include "tbb/concurrent_unordered_map.h"
//using namespace tbb;
// this might work but probably not?
//#include <GLES3/gl3.h> // LAND
#endif
#define RATE 30
#define SIZE 2000 // reboot park.exe if you change this
#define YARN 2 // reboot park.exe if you change this
/* I'm going to use one thread to modify position and
 * another slightly offset later to calculate the matrix
 * while the render thread is drawing the world.
 * NOT SURE IT'S A GOOD IDEA!
 */
using namespace std;

struct Vec3 { float x = 0, y = 0, z = 0; };
struct Quat { float x = 0, y = 0, z = 0, w = 0; };

const int UNIT = 1; // character mesh VAO from .dae
const int PART = 2; // static mesh VAO from .obj
const int SKIN = 3; // texture from .tga
const int MOVE = 4; // animation from .dae
const int TONE = 5; // sound from .wav
const int BONE = 6; // bone attachement point
const int SALT = 7; // id/salt with find/user (salt is both sign hash salt and session key)
const int NAME = 8; // id/name with find/user

/* game -> engine, these are found on the Game struct */
typedef void (*SEND)(const char *message); // using
typedef void (*PLAY)(int tone, Vec3 spot, Vec3 pace);
typedef int (*FIND)(int type, const char *name);
typedef const char * (*USER)(int type, int id);
typedef Quat (*LOOK)();

/* data structures concurrently changed by all threads */
struct Node {
    int *mesh, *skin; // model, texture
    Vec3 *spot, *pace; // position, velocity
    Quat *look, *spin; // look direction, rotation speed (to be used later in deterministic physics)
    float *zoom; // scale
};

struct Link {
    char **json; // properties
    int *unit; // for each item id you link the unit id that holds that item:
    int *bone; // game->gear.unit[4] = 2;
               // game->gear.bone[4] = game->find(BONE, "left_hand");
               // means item 4 is held by unit 2 in left hand.
};

struct Game {
    const int size = SIZE;
    const int yarn = YARN;

    char **name, **chat;
    Node unit, item, part; // character, sword or gun, house or tree
    int *move, *lead; // character animation frames and offset
    Link gear; // link item to character bone

    SEND send; PLAY play; FIND find;
    USER user; LOOK look;
};

/* do not edit above */

// GAME CODE
// |
// v

//concurrent_
unordered_map<string, int> salt_pull;
//concurrent_
unordered_map<int, string> salt_push;

string move(Game *game, int id, const char *move, float pace) {
    game->move[id] = game->find(MOVE, move);
    game->unit.pace[id].x = pace;
    stringstream ss;
    ss << "move|";
    ss << game->unit.spot[id].x << "," << game->unit.spot[id].y << "," << game->unit.spot[id].z << ";";
    ss << game->unit.pace[id].x << "," << game->unit.pace[id].y << "," << game->unit.pace[id].z << ";";
    ss << game->unit.look[id].x << "," << game->unit.look[id].y << "," << game->unit.look[id].z << "," << game->unit.look[id].w;
    ss << ";" << move;
    return ss.str();
}

void salt(int id, string key) {
    salt_push[id] = move(key); // move(key)
    salt_pull[key] = id;
}

bool once = true;
bool slow = false;
bool fast = false;
bool walk = false;

/* once per hot-deploy of the .dll/.so
 */
void _init(Game *game) {
    cout << ".dll/.so hot-deployed!" << endl;
    game->unit.mesh[0] = game->find(UNIT, "pirate");
    game->unit.skin[0] = game->find(SKIN, "texture_1");
    salt(0, "fd43");
    salt(1, "y2dt");
    salt(2, "u248");
}

/* once per frame
 * this is where you calculate movement
 */
void _tick(Game *game, int yarn) {
    //game->push("yo");
    //Quat view = game->look();
    if(once) {
     cout << "2" << endl;
     once = false;
    }
    //cout << "!" << flush;
}

#ifdef WIN32
#define NOINLINE __declspec (noinline)
#else
#define NOINLINE __attribute__ ((noinline))
#endif

NOINLINE void make_crash_yo() {
    int *foo = (int*)-1; // make a bad pointer
    printf("%d\n", *foo); // causes segfault
}

NOINLINE void add_some_depth() {
    int a = 0;
    make_crash_yo();
}

/* once per binary move action press keyboard/mouse,
 * thumb stick gets abstracted down to WASD with ctrl
 * /shift for fast/slow.
 */
void _push(Game *game, int code, bool down) {
    //cout << test << endl;
    if(code == 16) fast = down; // shift
    if(code == 17) slow = down; // control
    if(code == 'W') walk = down;
    if(walk && (code == 16 || code == 17 || code == 'W') || code == 'W') {
        const char *pace = fast ? "walk_fast" : slow ? "walk_slow" : "walk";
        float speed = fast ? 4.0f : slow ? 1.0f : 2.0f;
        string back = move(game, 0, walk ? pace : "idle", walk ? speed : 0.0f);
        game->send(back.c_str());
    }
    cout << code << endl;
    if(code == 52) { add_some_depth(); }
}

/* once per network message input
 */
void _pull(Game *game, const char *message) {
    // TODO
}

// ^
// |
// GAME CODE

/* engine -> game
 * do not edit below.
 * leave this at bottom.
 */
extern "C" {
    void init(Game *game) { _init(game); }
    void tick(Game *game, int yarn) { _tick(game, yarn); }
    void push(Game *game, int code, bool down) { _push(game, code, down); }
    void pull(Game *game, const char *message) { _pull(game, message); }
}