Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Imports */
- #include <stdio.h>
- #include <stdlib.h>
- #import <math.h>
- /* End Imports */
- /* Defines */
- #define PLAYER_WEIGHT_MAX 20
- #define PLAYER_WEIGHT_FLUX 12
- #define WORLD_GRID_SIZE 8192
- #define WORLD_AXIS 3
- #define WORLD_OFFSET 4096
- #define FREQUENCY_DEPTH 20
- #define PREDICTION_DEPTH 30
- #define UNITS_PER_SQUARE 16
- /* End Defines */
- /* Structs */
- typedef struct Neuron {
- Dendrite * root;
- unsigned long long frequency;
- int x, int y, int z
- };
- typedef struct NeuronList {
- NeuronList * left;
- NeuronList * right;
- Neuron * neuron;
- }
- typedef struct Dendrite{
- Dendrite * left;
- Dendrite * right;
- unsigned long long frequency;
- Frequency * root;
- };
- typedef struct Frequency {
- Frequency * left;
- Frequency * right;
- unsigned long long frequency;
- PlayerWeight * proot;
- Synapse * root;
- };
- typedef struct Synapse {
- Synapse * left;
- Synapse * right;
- PlayerWeight * root;
- Neuron * neuron;
- };
- typedef struct PlayerWeight {
- PlayerWeight * left;
- PlayerWeight * right;
- long playerId;
- int count;
- };
- typedef struct PlayerPath {
- PlayerPath * prev;
- Neuron * neuron;
- };
- typedef struct SynapseTree {
- SynapseOffset * left;
- SynapseOffset * right;
- float value;
- Synapse * synapse;
- };
- typedef struct FrequencyOffset {
- FrequencyOffset * left;
- FrequencyOffset * right;
- unsigned long long offset;
- Frequency * frequency;
- };
- typedef struct TreeDictRoot {
- TreeDict * root;
- int count;
- };
- typedef struct TreeDict {
- TreeDict * left;
- TreeDict * right;
- int id;
- long pointer;
- void * data;
- };
- /* End Structs */
- /* Prototypes */
- PlayerPath * createPath(Player * p, NeuronList * neuronListRoot);
- PlayerPath * updatePath(Player * p, PlayerPath * prev, neuronList, neuronListRoot);
- void updateNeuron(Player * p, PlayerPath * path, unsigned long long pathFreq);
- PlayerPath * generatePrediction(Player * p, PlayerPath * root) ;
- PlayerPath * generatePrediction(Player * p, PlayerPath * root,PlayerPath * prediction, int count);
- Neuron * getNeuron(int x, int y, int z, NeuronList * neuronListRoot);
- Neuron * getNeuron(NeuronList * neuron, unsigned long long frequency,int x, int y, int z);
- SynapseTree * createSynapseTree(Frequency * freq, Player * p);
- SynapseTree * buildSynapseTree(SynapseTree * tree, Synapse * root, Player * p);
- SynapseTree * addSynapseTree(SynapseTree * tree, Synapse * root,float weight);
- Synapse * getSynapse(Frequency * freq, Player * p, bool retNew);
- Synapse * getSynapse(Frequency * freq, Player * p);
- Synapse * updateSynapse(Frequency * freq, Player * p, Neuron * n);
- Synapse * updateSynapse(Synapse * root,Player * p, Neuron * n);
- FrequencyOffset * addFrequencyOffset(FrequencyOffset * ret, Frequency * freq, unsigned long long offset);
- FrequencyOffset * fillFrequencyOffset(FrequencyOffset * ret, Frequency * freq, Player * p, unsigned long long parentFreq,bool sniff,bool retNew);
- FrequencyOffset * createFrequencyOffset(Frequency * freq, Player * p, unsigned long long parentFreq, bool retNew);
- FrequencyOffset * getFrequency(FrequencyOffset * freq,Player * p);
- Frequency * getFrequency(Dendrite * dend,unsigned long long parentFreq,Player * p,bool retNew);
- Frequency * getFrequency(Dendrite * dend,unsigned long long p,Player * pl);
- Dendrite * getDendrite(Dendrite * dend, unsigned long long parentFreq, bool retNew);
- Dendrite * getDendrite(Neuron * neuron,unsigned long long parentFreq,bool retNew);
- Dendrite * getDendrite(Neuron * neuron,unsigned long long parentFreq);
- void deallocateFrequencyOffsetTree(FrequencyOffset * root);
- void deallocatePlayerPath(PlayerPath * path);
- void deallocateSynapseTree(SynapseTree * tree);
- void removePathNodes(PlayerPath * root,int count);
- void exportNetwork(NeuronList * root, char * fileName);
- void importNetwork(NeuronList ** root, char * fileName);
- float frequencyWeight(PlayerWeight * weight);
- PlayerWeight * getPlayerWeight(PlayerWeight * pw, Player * p);
- unsigned long long pathFrequency(PlayerPath * path);
- int transformCoord(int xyz);
- /* End Prototypes */
- /* Methods & functions */
- void collapseNeuronList(NeuronList * root, TreeDictRoot * nodeDict, TreeDictRoot * dendriteDict, TreeDictRoot * frequencyDict, TreeDictRoot * synapseDict, TreeDictRoot * playerWeightDict) {
- if (!root) return;
- collapseNeuron(root->neuron,nodeDict);
- collapseDendriteList(root->neuron->root,dendriteDict,frequencyDict,synapseDict,playerWeightDict);
- collapseNeuronList(root->left,nodeDict,dendriteDict,frequencyDict,synapseDict,playerWeightDict);
- collapseNeuronList(root->right,nodeDict,dendriteDict,frequencyDict,synapseDict,playerWeightDict);
- }
- void collapseNeuron(Neuron * n, TreeDictRoot * nodeDict) {
- if (nodeDict->root == NULL) {
- nodeDict->root = malloc(sizeof(TreeDict));
- nodeDict->root->id = nodeDict->count++;
- nodeDict->root->pointer = (long)neuron;
- nodeDict->root->data = neuron;
- }
- }
- void collapseDendriteList(Dendrite * root, TreeDictRoot * dendriteDict, TreeDictRoot * frequencyDict, TreeDictRoot * synapseDict, TreeDictRoot * playerWeightDict) {
- }
- void collapseFrequencyList(Frequency * root, TreeDictRoot * frequencyDict, TreeDictRoot * synapseDict, TreeDictRoot * playerWeightDict) {
- }
- void collapseSynapseList(Synapse * root, TreeDictRoot * synapseDict, TreeDictRoot * playerWeightDict) {
- }
- void collapsePlayerWeightList(PlayerWeight * root, TreeDictRoot * playerWeightDict) {
- }
- void exportNetwork(NeuronList * root, char * fileName) {
- TreeDictRoot * nodeDict = malloc(sizeof(TreeDictRoot));
- TreeDictRoot * dendriteDict = malloc(sizeof(TreeDictRoot));
- TreeDictRoot * frequencyDict = malloc(sizeof(TreeDictRoot));
- TreeDictRoot * synapseDict = malloc(sizeof(TreeDictRoot));
- TreeDictRoot * playerWeightDict = malloc(sizeof(TreeDictRoot));
- nodeDict->count = 0;
- dendriteDict->count = 0;
- frequencyDict->count = 0;
- synapseDict->count = 0;
- playerWeightDict->count = 0;
- collapseNeuronList(root,nodeDict,dendriteDict,frequencyDict,synapseDict,playerWeightDict);
- //HELP: Need help coming up with a way to export a NeuronList
- }
- void importNetwork(NeuronList ** root, char * fileName) {
- //HELP: Need help coming up with a way to import a NeuronList
- }
- PlayerPath * generatePrediction(Player * p, PlayerPath * root) {
- return generatePrediction(p, root, NULL, 0);
- }
- void removePathNodes(PlayerPath * root,int count) {
- if (!count) return;
- PlayerPath * temp = root->prev;
- free(root);
- root = temp;
- removePathNodes(root,--count);
- }
- PlayerPath * generatePrediction(Player * p, PlayerPath * root,PlayerPath * prediction, int count) {
- if (count == PREDICTION_DEPTH) {
- removePathNodes(root,count);
- return prediction;
- }
- Frequency * freq = getFrequency(getDendrite(root->neuron,root->prev->neuron->frequency),pathFrequency(root->prev),p,false);
- if (!freq) {
- removePathNodes(root, count);
- return prediction;
- }
- Synapse * syn = getSynapse(freq,p);
- if (!syn) {
- removePathNodes(root, count);
- return prediction;
- }
- PlayerPath * ret = malloc(sizeof(PlayerPath));
- ret->neuron = syn->neuron;
- ret->prev = root;
- PlayerPath * rett = prediction;
- prediction = malloc(sizeof(PlayerPath));
- prediction->neuron = syn->neuron;
- if (rett)
- prediction->prev = rett;
- else
- prediction->prev = prediction;
- return generatePrediction(p,ret,prediction,++count);
- //TODO: Fix getFrequency to only create a new frequency if retNew == true - DONE
- }
- Neuron * getNeuron(int x, int y, int z, NeuronList * neuronListRoot) {
- x = transformCoord(x);
- y = transformCoord(y);
- z = transformCoord(z);
- unsigned long long frequency = z * WORLD_GRID_SIZE^2 + y * WORLD_GRID_SIZE^1 + x;
- return getNeuron(neuronListRoot, frequency,x,y,z);
- }
- Neuron * getNeuron(NeuronList * neuron, unsigned long long frequency,int x, int y, int z) {
- if (neuron->neuron->frequency == frequency) return neuron->neuron;
- else if (neuron->neuron->frequency > frequency) {
- if (!neuron->left) {
- neuron->left = malloc(sizeof(NeuronList));
- neuron->left->neuron = malloc(sizeof(Neuron));
- neuron->left->neuron->frequency = frequency;
- neuron->left->neuron->x = x;
- neuron->left->neuron->y = y;
- neuron->left->neuron->z = z;
- return neuron->left->neuron
- }
- return getNeuron(neuron->left,frequency);
- }
- else {
- if (!neuron->right) {
- neuron->right = malloc(sizeof(NeuronList));
- neuron->right->neuron = malloc(sizeof(Neuron));
- neuron->right->neuron->frequency = frequency;
- neuron->right->neuron->x = x;
- neuron->right->neuron->y = y;
- neuron->right->neuron->z = z;
- return neuron->right->neuron
- }
- return getNeuron(neuron->left,frequency);
- }
- }
- PlayerPath * createPath(Player * p, NeuronList * neuronListRoot) {
- return updatePath(p,NULL);
- }
- PlayerPath * updatePath(Player * p, PlayerPath * prev, NeuronList * neuronListRoot) {
- PlayerPath * ret;
- ret = malloc(sizeof(PlayerPath));
- if (prev == NULL) {
- ret->prev = ret;
- prev = ret;
- }
- else
- ret->prev = prev;
- ret->neuron = getNeuron(p->x,p->y,p->z,neuronListRoot);
- updateNeuron(p,ret,pathFrequency(prev->prev));
- return ret;
- }
- void updateNeuron(Player * p, PlayerPath * path, unsigned long long pathFreq) {
- //getDendrite(path->neuron,path->prev->neuron->frequency,true),pathFreq));
- updateSynapse(getFrequency(getDendrite(path->prev->neuron,path->prev->prev->neuron->frequency),pathFreq,p,true)->root,p,path->neuron);
- getDendrite(path->neuron,path->prev->neuron);
- }
- Synapse * updateSynapse(Frequency * freq, Player * p, Neuron * n) {
- return updateSynapse(p,n,freq->root)
- }
- Synapse * updateSynapse(Synapse * root,Player * p, Neuron * n) {
- if (!root) {
- root = malloc(sizeof(Synapse));
- root->root = getPlayerWeight(NULL,p);
- root->neuron = n;
- }
- if (root->neuron->frequency == n->frequency) {
- return root;
- }
- else if (root->neuron->frequency > n->frequency) {
- return updateSynapse(root->left,p,n);
- }
- else {
- return updateSynapse(root->right,p,n);
- }
- }
- SynapseTree * createSynapseTree(Frequency * freq, Player * p) {
- return buildSynapseTree(NULL,ret,freq->root,p);
- }
- SynapseTree * buildSynapseTree(SynapseTree * tree, Synapse * root, Player * p) {
- if (!root) return tree;
- float weight = frequencyWeight(getPlayerWeight(root->root,p));
- addSynapseTree(tree,root,weight)
- buildSynapseTree(tree,root->left,p);
- buildSynapseTree(tree,root->right,p);
- return tree;
- }
- SynapseTree * addSynapseTree(SynapseTree * tree, Synapse * root,float weight) {
- if (!root) return tree;
- if (!tree) {
- tree = malloc(sizeof(SynapseTree));
- tree->synapse = root;
- tree->value = weight;
- return tree;
- }
- if (tree->value > weight) {
- if (tree->left == NULL) {
- tree->left = malloc(sizeof(SynapseTree));
- tree->left->value = weight;
- tree->left->synapse = root;
- return tree;
- }
- addSynapseTree(tree->left,root,weight);
- }
- if (tree->value < weight) {
- if (tree->right = NULL) {
- tree->right = malloc(sizeof(SynapseTree));
- tree->right->value = weight;
- tree->right->synapse = root;
- return tree;
- }
- addSynapseTree(tree->right,root,weight);
- }
- return tree;
- }
- Synapse * getSynapse(Frequency * freq, Player * p, bool retNew) {
- createSynapseTree(freq->synapse,p,retNew)
- }
- Synapse * getSynapse(Frequency * freq, Player * p) {
- createSynapseTree(freq->synapse,p,false);
- }
- PlayerWeight * getPlayerWeight(PlayerWeight * pw, Player * p) {
- //TODO: Replace steamID variable with real
- if (pw == NULL) {
- pw = malloc(sizeof(PlayerWeight));
- pw->playerId = p->steamID;
- pw->count = 1;
- pw->weight = 1;
- return pw;
- }
- if (pw->playerId > p->steamID) {
- if (pw->left == NULL) {
- pw->left = malloc(sizeof(PlayerWeight));
- pw->left->playerId = p->steamID;
- pw->left->count = 1;
- pw->left->weight = 1;
- return pw->left;
- }
- return getPlayerWeight(pw->left, p);
- }
- if (pw->playerId < p->steamID) {
- if (pw->right == NULL) {
- pw->right = malloc(sizeof(PlayerWeight));
- pw->right->playerId = p->steamID;
- pw->right->count = 1;
- pw->right->weight = 1;
- return pw->right;
- }
- return getPlayerWeight(pw->right, p);
- }
- pw->count++;
- return pw;
- }
- FrequencyOffset * addFrequencyOffset(FrequencyOffset * ret, Frequency * freq, unsigned long long offset) {
- if (ret->offset > offset) {
- if (ret->left == NULL) {
- ret->left = malloc(sizeof(FrequencyOffset));
- ret->left->frequency = freq;
- ret->left->offset = offset;
- return ret->left;
- }
- else {
- return addFrequencyOffset(ret->left,freq,offset);
- }
- }
- if (ret->offset < offset) {
- if (ret->right == NULL) {
- ret->right = malloc(sizeof(FrequencyOffset));
- ret->right->frequency = freq;
- ret->right->offset = offset;
- return ret->right;
- }
- else {
- return addFrequencyOffset(ret->right,freq,offset);
- }
- }
- return ret;
- }
- FrequencyOffset * fillFrequencyOffset(FrequencyOffset * ret, Frequency * freq, Player * p, unsigned long long parentFreq,bool sniff,bool retNew) {
- if (sniff) {
- if (freq->frequency > parentFreq) {
- if (freq->right != NULL)
- fillFrequencyOffset(freq->right,p,parentFreq,false,retNew);
- if (freq->left == NULL) {
- if (retNew) {
- freq->left = malloc(sizeof(Frequency));
- freq->left->frequency = parentFreq;
- freq->left->proot = malloc(sizeof(PlayerWeight));
- freq->left->proot->count = 1;
- freq->left->proot->playerId = p->steamId;
- fillFrequencyOffset(ret,freq->left,p,parentFreq,false,retNew);
- }
- return ret;
- }
- else {
- fillFrequencyOffset(ret,freq->left,p,parentFreq,true,retNew);
- }
- }
- if (freq->frequency < parentFreq) {
- if (freq->left != NULL)
- fillFrequencyOffset(freq->left,p,parentFreq,false,retNew);
- if (freq->right == NULL) {
- if (retNew) {
- freq->right = malloc(sizeof(Frequency));
- freq->right->frequency = parentFreq;
- freq->right->proot = malloc(sizeof(PlayerWeight));
- freq->right->proot->count = 1;
- freq->right->proot->playerId = p->steamId;
- fillFrequencyOffset(ret,freq->right,p,parentFreq,false,retNew);
- }
- return ret;
- }
- else {
- fillFrequencyOffset(ret,freq->right,p,parentFreq,true,retNew);
- }
- }
- }
- else {
- if (freq->left != NULL)
- fillFrequencyOffset(ret,freq->left,p,parentFreq,false,retNew);
- if (freq->right != NULL)
- fillFrequencyOffset(ret,freq->right,p,parentFreq,false,retNew);
- }
- addFrequencyOffset(ret,freq,ceil(abs(parentFreq - freq->frequency) * frequencyWeight(getPlayerWeight(freq->proot,p))))
- return ret;
- }
- FrequencyOffset * createFrequencyOffset(Frequency * freq, Player * p, unsigned long long parentFreq, bool retNew) {
- FrequencyOffset * ret = malloc(sizeof(FrequencyOffset));
- ret->frequency = freq;
- ret->offset = ceil(abs(parentFreq - freq->frequency) * frequencyWeight(getPlayerWeight(freq->proot,p)))
- return fillFrequencyOffset(ret,freq,p,parentFreq,true,retNew);
- }
- FrequencyOffset * getFrequency(FrequencyOffset * freq,Player * p) {
- if (freq->offset > 0) {
- if (freq->left == NULL) {
- return freq;
- }
- else {
- return getFrequency(freq->left,p);
- }
- }
- if (freq->offset < 0) {
- if (freq->right == NULL) {
- return freq;
- }
- else {
- return getFrequency(freq->right,p);
- }
- }
- return freq;
- }
- Frequency * getFrequency(Dendrite * dend,unsigned long long parentFreq,Player * p,bool retNew) {
- Frequency * freq = dend->root;
- if (freq == NULL) {
- freq = malloc(sizeof(Frequency));
- freq->frequency = parentFreq;
- }
- FrequencyOffset * root = createFrequencyOffset(freq,p,parentFreq,retNew);
- Frequency * ret = getFrequency(root,p)->frequency;
- deallocateFrequencyOffsetTree(root);
- return ret;
- }
- Frequency * getFrequency(Dendrite * dend,unsigned long long p,Player * pl) {
- return getFrequency(dend,p,pl,false);
- }
- void deallocateFrequencyOffsetTree(FrequencyOffset * root) {
- if (!root) return;
- deallocateFrequencyOffsetTree(root->left);
- deallocateFrequencyOffsetTree(root->right);
- free(root);
- }
- void deallocatePlayerPath(PlayerPath * path) {
- if (!path) return;
- if (path != path->prev)
- deallocatePlayerPath(path->prev);
- free(path);
- }
- void deallocateSynapseTree(SynapseTree * tree) {
- if (!tree) return;
- deallocateSynapseTree(tree->left);
- deallocateSynapseTree(tree->right);
- free(tree);
- }
- /*************************************************/
- Dendrite * getDendrite(Dendrite * dend, unsigned long long parentFreq, bool retNew) {
- if (dend->frequency == parentFreq)
- return dend;
- if (dend->frequency > parentFreq)
- if (dend->left == NULL) {
- dend->left = malloc(sizeof(Dendrite));
- dend->left->frequency = parentFreq;
- return retNew ? dend->left : dend;
- }
- else
- return getDendrite(dend->left,parentFreq);
- if (dend->frequency < parentFreq)
- if (dend->right == NULL) {
- dend->right = malloc(sizeof(Dendrite));
- dend->right->frequency = parentFreq;
- return retNew ? dend->right : dend;
- }
- else
- return getDendrite(dend->right,parentFreq);
- }
- Dendrite * getDendrite(Neuron * neuron,unsigned long long parentFreq,bool retNew) {
- Dendrite * dend = neuron->root;
- if (dend == NULL) {
- dend = malloc(sizeof(Dendrite));
- dend->frequency = parentFreq;
- neuron->root = dend;
- }
- return getDendrite(dend,parentFreq,retNew);
- }
- Dendrite * getDendrite(Neuron * neuron,unsigned long long parentFreq) {
- return getDendrite(neuron,parentFreq,false);
- }
- unsigned long long pathFrequency(PlayerPath * path) {
- PlayerPath * iter = path;
- unsigned long long frequency = 0;
- for (int i = 0; i < FREQUENCY_DEPTH; i++) {
- frequency += iter->neuron->frequency;
- iter = iter->prev;
- }
- return frequency;
- }
- float frequencyWeight(PlayerWeight * weight) {
- if (weight == NULL)
- return 1f;
- return 1f - ((PlAYER_WEIGHT_MAX - (PLAYER_WEIGHT_FLUX / (pow(4f,weight->count) / pow(3.6f,weight->count)))) / 100);
- }
- int transformCoord(int xyz) {
- return (int)((xyz + WORLD_OFFSET) / UNITS_PER_SQUARE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement