Advertisement
imk0tter

Imk0tter

Dec 10th, 2010
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.88 KB | None | 0 0
  1. 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); }
  2.  
  3. #import <math.h>
  4.  
  5. #define PLAYER_WEIGHT_MAX 20
  6. #define PLAYER_WEIGHT_FLUX 12
  7.  
  8. #define WORLD_GRID_SIZE 8191
  9. #define WORLD_OFFSET 4096
  10.  
  11. #define FREQUENCY_DEPTH 20
  12.  
  13. typedef struct Neuron {
  14.     Dendrite * root;
  15.     long frequency;
  16. } neuronArrayPtr[WORLD_GRID_SIZE ^ 2];
  17.  
  18. typedef struct Dendrite{
  19.     Dendrite * left;
  20.     Dendrite * right;
  21.     long frequency;
  22.     Frequency * root;
  23. };
  24.  
  25. typedef struct Frequency {
  26.     Frequency * left;
  27.     Frequency * right;
  28.     long frequency;
  29.     PlayerWeight * proot;
  30.     Synapse * root;
  31. };
  32.  
  33. typedef struct Synapse {
  34.     Synapse * left;
  35.     Synapse * right;
  36.     PlayerWeight * root;
  37.     Neuron * neuron;
  38. };
  39.  
  40. typedef struct PlayerWeight {
  41.     PlayerWeight * left;
  42.     PlayerWeight * right;
  43.     long playerId;
  44.     int count;
  45. };
  46.  
  47. typedef struct PlayerPath {
  48.     PlayerPath * prev;
  49.     Neuron * neuron;
  50. };
  51. typedef struct SynapseTree {
  52.     SynapseOffset * left;
  53.     SynapseOffset * right;
  54.     float value;
  55.     Synapse * synapse;
  56. };
  57. typedef struct FrequencyOffset {
  58.     FrequencyOffset * left;
  59.     FrequencyOffset * right;
  60.     long offset;
  61.     Frequency * frequency;
  62. };
  63. PlayerPath * createPath(Player * p) {
  64.     return updatePath(p,NULL);
  65. }
  66.  
  67. PlayerPath * updatePath(Player * p, PlayerPath * prev) {
  68.     PlayerPath * ret;
  69.     ret = malloc(sizeof(PlayerPath));
  70.     if (prev == NULL)
  71.         ret->prev = ret;
  72.     else
  73.         ret->prev = prev;
  74.     ret->neuron = getNeuron(p->x,p->y);
  75.     updateNeuron(p,ret,pathFrequency(prev));
  76.     return ret;
  77. }
  78. void updateNeuron(Player * p, PlayerPath * path, long pathFreq) {
  79.     //getDendrite(path->neuron,path->prev->neuron->frequency,true),pathFreq));
  80.     updateSynapse(getFrequency(getDendrite(path->prev->neuron,path->prev->prev->neuron->frequency),pathFreq,p,true)->root,p,path->neuron);
  81. getDendrite(path->neuron,path->prev->neuron,pathFrequency(ret) 
  82. }
  83. Synapse * updateSynapse(Frequency * freq, Player * p, Neuron * n) {
  84.     return updateSynapse(p,n,freq->root)
  85. }
  86. Synapse * updateSynapse(Synapse * root,Player * p, Neuron * n) {
  87.     if (!root) {
  88.         root = malloc(sizeof(Synapse));
  89.         root->root = getPlayerWeight(NULL,p);
  90.         root->neuron = n;
  91.     }
  92.     if (root->neuron->frequency == n->frequency) {
  93.         return root;
  94.     }
  95.     else if (root->neuron->frequency > n->frequency) {
  96.         return updateSynapse(root->left,p,n);
  97.     }
  98.     else {
  99.         return updateSynapse(root->right,p,n);
  100.     }
  101. }
  102. SynapseTree * createSynapseTree(Frequency * freq, Player * p) {
  103.     return buildSynapseTree(NULL,ret,freq->root,p);
  104. }
  105. SynapseTree * buildSynapseTree(SynapseTree * tree, Synapse * root, Player * p) {
  106.     if (!root) return tree;
  107.     float weight = frequencyWeight(getPlayerWeight(root->root,p));
  108.     addSynapseTree(tree,root,weight)
  109.     buildSynapseTree(tree,root->left,p);
  110.     buildSynapseTree(tree,root->right,p);
  111.     return tree;
  112. }
  113. SynapseTree * addSynapseTree(SynapseTree * tree, Synapse * root,float weight) {
  114.     if (!root) return tree;
  115.     if (!tree) {
  116.         tree = malloc(sizeof(SynapseTree));
  117.         tree->synapse = root;
  118.         tree->value = weight;
  119.         return tree;
  120.     }
  121.     if (tree->value > weight) {
  122.         if (tree->left == NULL) {
  123.             tree->left = malloc(sizeof(SynapseTree));
  124.             tree->left->value = weight;
  125.             tree->left->synapse = root;
  126.             return tree;
  127.         }
  128.         addSynapseTree(tree->left,root,weight);
  129.     }
  130.     if (tree->value < weight) {
  131.         if (tree->right = NULL) {
  132.             tree->right = malloc(sizeof(SynapseTree));
  133.             tree->right->value = weight;
  134.             tree->right->synapse = root;
  135.             return tree;
  136.         }
  137.         addSynapseTree(tree->right,root,weight);
  138.     }
  139.     return tree;
  140. }
  141. Synapse * getSynapse(Frequency * freq, Player * p, bool retNew) {
  142.     createSynapseTree(freq->synapse,p,retNew)
  143. }
  144. Synapse * getSynapse(Frequency * freq, Player * p) {
  145.     createSynapseTree(freq->synapse,p,true);
  146. }
  147.  
  148. PlayerWeight * getPlayerWeight(PlayerWeight * pw, Player * p) {
  149.     //TODO: Replace steamID variable with real
  150.     if (pw == NULL) {
  151.         pw = malloc(sizeof(PlayerWeight));
  152.         pw->playerId = p->steamID;
  153.         pw->count = 1;
  154.         pw->weight = 1;
  155.         return pw;
  156.     }
  157.     if (pw->playerId > p->steamID) {
  158.         if (pw->left == NULL) {
  159.             pw->left = malloc(sizeof(PlayerWeight));
  160.             pw->left->playerId = p->steamID;
  161.             pw->left->count = 1;
  162.             pw->left->weight = 1;
  163.             return pw->left;
  164.         }
  165.         return getPlayerWeight(pw->left, p);
  166.     }
  167.     if (pw->playerId < p->steamID) {
  168.         if (pw->right == NULL) {
  169.             pw->right = malloc(sizeof(PlayerWeight));
  170.             pw->right->playerId = p->steamID;
  171.             pw->right->count = 1;
  172.             pw->right->weight = 1;
  173.             return pw->right;
  174.         }
  175.         return getPlayerWeight(pw->right, p);
  176.     }
  177.     pw->count++;
  178.     return pw;
  179. }
  180. FrequencyOffset * addFrequencyOffset(FrequencyOffset * ret, Frequency * freq, long offset) {
  181.     if (ret->offset > offset) {
  182.         if (ret->left == NULL) {
  183.             ret->left = malloc(sizeof(FrequencyOffset));
  184.             ret->left->frequency = freq;
  185.             ret->left->offset = offset;
  186.             return ret->left;
  187.         }
  188.         else {
  189.             return addFrequencyOffset(ret->left,freq,offset);
  190.         }
  191.     }
  192.     if (ret->offset < offset) {
  193.         if (ret->right == NULL) {
  194.             ret->right = malloc(sizeof(FrequencyOffset));
  195.             ret->right->frequency = freq;
  196.             ret->right->offset = offset;
  197.             return ret->right;
  198.         }
  199.         else {
  200.             return addFrequencyOffset(ret->right,freq,offset);
  201.         }
  202.     }
  203.     return ret;
  204. }
  205. FrequencyOffset * fillFrequencyOffset(FrequencyOffset * ret, Frequency * freq, Player * p, long parentFreq,bool sniff) {
  206.     if (sniff) {
  207.         if (freq->frequency > parentFreq) {
  208.             if (freq->left == NULL) {
  209.                 freq->left = malloc(sizeof(Frequency));
  210.                 freq->left->frequency = parentFreq;
  211.                 freq->left->proot = malloc(sizeof(PlayerWeight));
  212.                 freq->left->proot->count = 1;
  213.                 freq->left->proot->playerId = p->steamId;
  214.             }
  215.             else {
  216.                 fillFrequencyOffset(ret,freq->left,p,parentFreq,true);
  217.                 if (freq->right != NULL)
  218.                     fillFrequencyOffset(freq->right,p,parentFreq,false);
  219.             }
  220.             return ret;
  221.         }
  222.         if (freq->frequency < parentFreq) {
  223.             if (freq->right == NULL) {
  224.                 freq->right = malloc(sizeof(Frequency));
  225.                 freq->right->frequency = parentFreq;
  226.                 freq->right->proot = malloc(sizeof(PlayerWeight));
  227.                 freq->right->proot->count = 1;
  228.                 freq->right->proot->playerId = p->steamId;
  229.             }
  230.             else {
  231.                 fillFrequencyOffset(ret,freq->right,p,parentFreq,true);
  232.                 if (freq->left != NULL)
  233.                     fillFrequencyOffset(freq->left,p,parentFreq,false);
  234.             }
  235.             return ret;
  236.         }
  237.     }
  238.     if (freq->left != NULL)
  239.         fillFrequencyOffset(ret,freq->left,p,parentFreq,false);
  240.     if (freq->right != NULL)
  241.         fillFrequencyOffset(ret,freq->right,p,parentFreq,false);
  242.     addFrequencyOffset(ret,freq,ceil(abs(parentFreq - freq->frequency) * frequencyWeight(getPlayerWeight(freq->proot,p))))
  243.     return ret;
  244. }
  245. FrequencyOffset * createFrequencyOffset(Frequency * freq, Player * p, long parentFreq) {
  246.     FrequencyOffset * ret = malloc(sizeof(FrequencyOffset));
  247.     ret->frequency = freq;
  248.     ret->offset = ceil(abs(parentFreq - freq->frequency) * frequencyWeight(getPlayerWeight(freq->proot,p)))
  249.     return fillFrequencyOffset(ret,freq,p,parentFreq,true);
  250.    
  251. }
  252.  
  253. FrequencyOffset * getFrequency(FrequencyOffset * freq,Player * p) {
  254.     if (freq->offset > 0) {
  255.         if (freq->left == NULL) {
  256.             return freq;
  257.         }
  258.         else {
  259.             return getFrequency(freq->left,p);
  260.         }
  261.     }
  262.     if (freq->offset < 0) {
  263.         if (freq->right == NULL) {
  264.             return freq;
  265.         }
  266.         else {
  267.             return getFrequency(freq->right,p);
  268.         }
  269.     }
  270.     return freq;
  271. }
  272.  
  273. Frequency * getFrequency(Dendrite * dend,long parentFreq,Player * p,bool retNew) {
  274.     Frequency * freq = dend->root;
  275.     if (freq == NULL) {
  276.         freq = malloc(sizeof(Frequency));
  277.         freq->frequency = parentFreq;
  278.     }
  279.     FrequencyOffset * root = createFrequencyOffset(freq,p,parentFreq);
  280.     Frequency * ret = getFrequency(root,p)->frequency;
  281.     deallocateFrequencyOffsetTree(root);
  282.     return ret;
  283. }
  284.  
  285. Frequency * getFrequency(Dendrite * dend,long p,Player * pl) {
  286.     return getFrequency(dend,p,pl,false);
  287. }
  288. void deallocate
  289. void deallocateFrequencyOffsetTree(FrequencyOffset * root) {
  290.     if (!root) return;
  291.     deallocateFrequencyOffsetTree(root->left);
  292.     deallocateFrequencyOffsetTree(root->right);
  293.     free(root);
  294. }
  295. void deallocatePlayerPath(PlayerPath * path) {
  296.     if (!path) return;
  297.     deallocatePlayerPath(path->prev);
  298.     free(path);
  299. }
  300. void deallocateSynapseTree(SynapseTree * tree) {
  301.     if (!tree) return;
  302.     deallocateSynapseTree(tree->left);
  303.     deallocateSynapseTree(tree->right);
  304.     free(tree);
  305. }
  306. /*************************************************/
  307. Dendrite * getDendrite(Dendrite * dend,long parentFreq,bool retNew) {
  308.     if (dend->frequency == parentFreq)
  309.         return dend;
  310.     if (dend->frequency > parentFreq)
  311.         if (dend->left == NULL) {
  312.             dend->left = malloc(sizeof(Dendrite));
  313.             dend->left->frequency = parentFreq;
  314.             return retNew ? dend->left : dend;
  315.         }
  316.         else
  317.             return getDendrite(dend->left,parentFreq);
  318.     if (dend->frequency < parentFreq)
  319.         if (dend->right == NULL) {
  320.             dend->right = malloc(sizeof(Dendrite));
  321.             dend->right->frequency = parentFreq;
  322.             return retNew ? dend->right : dend;
  323.         }
  324.         else
  325.             return getDendrite(dend->right,parentFreq);
  326. }
  327.  
  328. Dendrite * getDendrite(Neuron * neuron,long parentFreq,bool retNew) {
  329.     Dendrite * dend = neuron->root;
  330.     if (dend == NULL) {
  331.         dend = malloc(sizeof(Dendrite));
  332.         dend->frequency = parentFreq;
  333.         neuron->root = dend;
  334.     }
  335.     return getDendrite(dend,parentFreq,retNew);
  336. }
  337.  
  338. Dendrite * getDendrite(Neuron * neuron,long parentFreq) {
  339.     return getDendrite(neuron,parentFreq,false);
  340. }
  341.  
  342. long pathFrequency(PlayerPath * path) {
  343.     PlayerPath * iter = path;
  344.     long frequency = 0;
  345.     for (int i = 0; i < FREQUENCY_DEPTH; i++) {
  346.         frequency += iter->neuron->frequency;
  347.         iter = iter->prev;
  348.     }
  349.     return frequency;
  350. }
  351.  
  352. float frequencyWeight(PlayerWeight * weight) {
  353.     if (weight == NULL)
  354.         return 1f;
  355.     return 1f - ((PlAYER_WEIGHT_MAX - (PLAYER_WEIGHT_FLUX / (pow(4f,weight->count) / pow(3.6f,weight->count)))) / 100);
  356. }
  357.  
  358. int transformCoord(int xy) {
  359.     return xy + WORLD_OFFSET;
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement