Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct NODE { NODE * left; NODE * right; int value; } NODE * addNode(NODE * node, int value) { if (value == node->value) return node; if (node->value > value) { if (node->left == NULL) { node->left = malloc(sizeof(NODE)); node->left->value = value; } return addNode(node->left,value); } if (node->value < value) { if (node->right == NULL) { node->right = malloc(sizeof(NODE)); node->right->value = value; } return addNode(node->right,value); } } int main() { int x[] = {12,14,3,2,8,5,7,21,2,3}; NODE * node = malloc(sizeof(NODE)); node->value = x[0]; for (int i = 1; i < 10; i++) { addNode(node,x[i]); } printf("%i",node->value); }
- #import <math.h>
- #define PLAYER_WEIGHT_MAX 20
- #define PLAYER_WEIGHT_FLUX 12
- #define WORLD_GRID_SIZE 8191
- #define WORLD_OFFSET 4096
- #define FREQUENCY_DEPTH 20
- typedef struct Neuron {
- Dendrite * root;
- long frequency;
- } neuronArrayPtr[WORLD_GRID_SIZE ^ 2];
- typedef struct Dendrite{
- Dendrite * left;
- Dendrite * right;
- long frequency;
- Frequency * root;
- };
- typedef struct Frequency {
- Frequency * left;
- Frequency * right;
- 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;
- long offset;
- Frequency * frequency;
- };
- PlayerPath * createPath(Player * p) {
- return updatePath(p,NULL);
- }
- PlayerPath * updatePath(Player * p, PlayerPath * prev) {
- PlayerPath * ret;
- ret = malloc(sizeof(PlayerPath));
- if (prev == NULL)
- ret->prev = ret;
- else
- ret->prev = prev;
- ret->neuron = getNeuron(p->x,p->y);
- updateNeuron(p,ret,pathFrequency(prev));
- return ret;
- }
- void updateNeuron(Player * p, PlayerPath * path, 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,pathFrequency(ret)
- }
- 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,true);
- }
- 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, 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, long parentFreq,bool sniff) {
- if (sniff) {
- if (freq->frequency > parentFreq) {
- if (freq->left == NULL) {
- 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;
- }
- else {
- fillFrequencyOffset(ret,freq->left,p,parentFreq,true);
- if (freq->right != NULL)
- fillFrequencyOffset(freq->right,p,parentFreq,false);
- }
- return ret;
- }
- if (freq->frequency < parentFreq) {
- if (freq->right == NULL) {
- 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;
- }
- else {
- fillFrequencyOffset(ret,freq->right,p,parentFreq,true);
- if (freq->left != NULL)
- fillFrequencyOffset(freq->left,p,parentFreq,false);
- }
- return ret;
- }
- }
- if (freq->left != NULL)
- fillFrequencyOffset(ret,freq->left,p,parentFreq,false);
- if (freq->right != NULL)
- fillFrequencyOffset(ret,freq->right,p,parentFreq,false);
- addFrequencyOffset(ret,freq,ceil(abs(parentFreq - freq->frequency) * frequencyWeight(getPlayerWeight(freq->proot,p))))
- return ret;
- }
- FrequencyOffset * createFrequencyOffset(Frequency * freq, Player * p, long parentFreq) {
- 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);
- }
- 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,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);
- Frequency * ret = getFrequency(root,p)->frequency;
- deallocateFrequencyOffsetTree(root);
- return ret;
- }
- Frequency * getFrequency(Dendrite * dend,long p,Player * pl) {
- return getFrequency(dend,p,pl,false);
- }
- void deallocate
- void deallocateFrequencyOffsetTree(FrequencyOffset * root) {
- if (!root) return;
- deallocateFrequencyOffsetTree(root->left);
- deallocateFrequencyOffsetTree(root->right);
- free(root);
- }
- void deallocatePlayerPath(PlayerPath * path) {
- if (!path) return;
- deallocatePlayerPath(path->prev);
- free(path);
- }
- void deallocateSynapseTree(SynapseTree * tree) {
- if (!tree) return;
- deallocateSynapseTree(tree->left);
- deallocateSynapseTree(tree->right);
- free(tree);
- }
- /*************************************************/
- Dendrite * getDendrite(Dendrite * dend,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,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,long parentFreq) {
- return getDendrite(neuron,parentFreq,false);
- }
- long pathFrequency(PlayerPath * path) {
- PlayerPath * iter = path;
- 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 xy) {
- return xy + WORLD_OFFSET;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement