Advertisement
imk0tter

Imk0tter

Dec 10th, 2010
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.22 KB | None | 0 0
  1. /* Imports */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #import <math.h>
  5. /* End Imports */
  6.  
  7. /* Defines */
  8. #define PLAYER_WEIGHT_MAX 20
  9. #define PLAYER_WEIGHT_FLUX 12
  10.  
  11. #define WORLD_GRID_SIZE 8192
  12. #define WORLD_AXIS 3
  13. #define WORLD_OFFSET 4096
  14. #define FREQUENCY_DEPTH 20
  15. #define PREDICTION_DEPTH 30
  16. #define UNITS_PER_SQUARE 16
  17. /* End Defines */
  18.  
  19. /* Structs */
  20. typedef struct Neuron {
  21.     Dendrite * root;
  22.     unsigned long long frequency;
  23.     int x, int y, int z
  24. };
  25.  
  26. typedef struct NeuronList {
  27.     NeuronList * left;
  28.     NeuronList * right;
  29.     Neuron * neuron;
  30. }
  31. typedef struct Dendrite{
  32.     Dendrite * left;
  33.     Dendrite * right;
  34.     unsigned long long frequency;
  35.     Frequency * root;
  36. };
  37.  
  38. typedef struct Frequency {
  39.     Frequency * left;
  40.     Frequency * right;
  41.     unsigned long long frequency;
  42.     PlayerWeight * proot;
  43.     Synapse * root;
  44. };
  45.  
  46. typedef struct Synapse {
  47.     Synapse * left;
  48.     Synapse * right;
  49.     PlayerWeight * root;
  50.     Neuron * neuron;
  51. };
  52.  
  53. typedef struct PlayerWeight {
  54.     PlayerWeight * left;
  55.     PlayerWeight * right;
  56.     long playerId;
  57.     int count;
  58. };
  59.  
  60. typedef struct PlayerPath {
  61.     PlayerPath * prev;
  62.     Neuron * neuron;
  63. };
  64. typedef struct SynapseTree {
  65.     SynapseOffset * left;
  66.     SynapseOffset * right;
  67.     float value;
  68.     Synapse * synapse;
  69. };
  70. typedef struct FrequencyOffset {
  71.     FrequencyOffset * left;
  72.     FrequencyOffset * right;
  73.     unsigned long long offset;
  74.     Frequency * frequency;
  75. };
  76.  
  77. typedef struct TreeDict {
  78.     TreeDict * left;
  79.     TreeDict * right;
  80.     int id;
  81.     long pointer;
  82.     void * data;
  83. }
  84. /* End Structs */
  85.  
  86. /* Prototypes */
  87. PlayerPath * createPath(Player * p, NeuronList * neuronListRoot);
  88. PlayerPath * updatePath(Player * p, PlayerPath * prev, neuronList, neuronListRoot);
  89. void updateNeuron(Player * p, PlayerPath * path, unsigned long long pathFreq);
  90.  
  91. PlayerPath * generatePrediction(Player * p, PlayerPath * root) ;
  92. PlayerPath * generatePrediction(Player * p, PlayerPath * root,PlayerPath * prediction, int count);
  93.  
  94. Neuron * getNeuron(int x, int y, int z, NeuronList * neuronListRoot);
  95. Neuron * getNeuron(NeuronList * neuron, unsigned long long frequency,int x, int y, int z);
  96.  
  97. SynapseTree * createSynapseTree(Frequency * freq, Player * p);
  98. SynapseTree * buildSynapseTree(SynapseTree * tree, Synapse * root, Player * p);
  99. SynapseTree * addSynapseTree(SynapseTree * tree, Synapse * root,float weight);
  100.  
  101. Synapse * getSynapse(Frequency * freq, Player * p, bool retNew);
  102. Synapse * getSynapse(Frequency * freq, Player * p);
  103.  
  104. Synapse * updateSynapse(Frequency * freq, Player * p, Neuron * n);
  105. Synapse * updateSynapse(Synapse * root,Player * p, Neuron * n);
  106.  
  107. FrequencyOffset * addFrequencyOffset(FrequencyOffset * ret, Frequency * freq, unsigned long long offset);
  108. FrequencyOffset * fillFrequencyOffset(FrequencyOffset * ret, Frequency * freq, Player * p, unsigned long long parentFreq,bool sniff,bool retNew);
  109. FrequencyOffset * createFrequencyOffset(Frequency * freq, Player * p, unsigned long long parentFreq, bool retNew);
  110. FrequencyOffset * getFrequency(FrequencyOffset * freq,Player * p);
  111.  
  112. Frequency * getFrequency(Dendrite * dend,unsigned long long parentFreq,Player * p,bool retNew);
  113. Frequency * getFrequency(Dendrite * dend,unsigned long long p,Player * pl);
  114.  
  115. Dendrite * getDendrite(Dendrite * dend, unsigned long long parentFreq, bool retNew);
  116. Dendrite * getDendrite(Neuron * neuron,unsigned long long parentFreq,bool retNew);
  117. Dendrite * getDendrite(Neuron * neuron,unsigned long long parentFreq);
  118.  
  119. void deallocateFrequencyOffsetTree(FrequencyOffset * root);
  120. void deallocatePlayerPath(PlayerPath * path);
  121. void deallocateSynapseTree(SynapseTree * tree);
  122. void removePathNodes(PlayerPath * root,int count);
  123.  
  124. void exportNetwork(NeuronList * root, char * fileName);
  125. void importNetwork(NeuronList ** root, char * fileName);
  126.  
  127. float frequencyWeight(PlayerWeight * weight);
  128. PlayerWeight * getPlayerWeight(PlayerWeight * pw, Player * p);
  129.  
  130. unsigned long long pathFrequency(PlayerPath * path);
  131. int transformCoord(int xyz);
  132. /* End Prototypes */
  133.  
  134. /* Methods & functions */
  135. void exportNetwork(NeuronList * root, char * fileName) {
  136. //HELP: Need help coming up with a way to export a NeuronList
  137. }
  138. void importNetwork(NeuronList ** root, char * fileName) {
  139. //HELP: Need help coming up with a way to import a NeuronList
  140. }
  141.  
  142. PlayerPath * generatePrediction(Player * p, PlayerPath * root) {
  143.     return generatePrediction(p, root, NULL, 0);
  144. }
  145. void removePathNodes(PlayerPath * root,int count) {
  146.     if (!count) return;
  147.     PlayerPath * temp = root->prev;
  148.     free(root);
  149.     root = temp;
  150.     removePathNodes(root,--count);
  151. }
  152. PlayerPath * generatePrediction(Player * p, PlayerPath * root,PlayerPath * prediction, int count) {
  153.     if (count == PREDICTION_DEPTH) {
  154.         removePathNodes(root,count);
  155.         return prediction;
  156.     }
  157.     Frequency * freq = getFrequency(getDendrite(root->neuron,root->prev->neuron->frequency),pathFrequency(root->prev),p,false);
  158.     if (!freq) {
  159.         removePathNodes(root, count);
  160.         return prediction;
  161.     }
  162.     Synapse * syn = getSynapse(freq,p);
  163.     if (!syn) {
  164.         removePathNodes(root, count);
  165.         return prediction;
  166.     }
  167.     PlayerPath * ret = malloc(sizeof(PlayerPath));
  168.     ret->neuron = syn->neuron;
  169.     ret->prev = root;
  170.     PlayerPath * rett = prediction;
  171.     prediction = malloc(sizeof(PlayerPath));
  172.     prediction->neuron = syn->neuron;
  173.     if (rett)
  174.         prediction->prev = rett;
  175.     else
  176.         prediction->prev = prediction;
  177.     return generatePrediction(p,ret,prediction,++count);
  178.     //TODO: Fix getFrequency to only create a new frequency if retNew == true - DONE
  179.    
  180. }
  181. Neuron * getNeuron(int x, int y, int z, NeuronList * neuronListRoot) {
  182.     x = transformCoord(x);
  183.     y = transformCoord(y);
  184.     z = transformCoord(z);
  185.     unsigned long long frequency = z * WORLD_GRID_SIZE^2 + y * WORLD_GRID_SIZE^1 + x;
  186.     return getNeuron(neuronListRoot, frequency,x,y,z);
  187. }
  188. Neuron * getNeuron(NeuronList * neuron, unsigned long long frequency,int x, int y, int z) {
  189.     if (neuron->neuron->frequency == frequency) return neuron->neuron;
  190.     else if (neuron->neuron->frequency > frequency) {
  191.         if (!neuron->left) {
  192.             neuron->left = malloc(sizeof(NeuronList));
  193.             neuron->left->neuron = malloc(sizeof(Neuron));
  194.             neuron->left->neuron->frequency = frequency;
  195.             neuron->left->neuron->x = x;
  196.             neuron->left->neuron->y = y;
  197.             neuron->left->neuron->z = z;
  198.             return neuron->left->neuron
  199.         }
  200.         return getNeuron(neuron->left,frequency);
  201.     }
  202.     else {
  203.         if (!neuron->right) {
  204.             neuron->right = malloc(sizeof(NeuronList));
  205.             neuron->right->neuron = malloc(sizeof(Neuron));
  206.             neuron->right->neuron->frequency = frequency;
  207.             neuron->right->neuron->x = x;
  208.             neuron->right->neuron->y = y;
  209.             neuron->right->neuron->z = z;
  210.             return neuron->right->neuron
  211.         }
  212.         return getNeuron(neuron->left,frequency);
  213.     }
  214. }
  215. PlayerPath * createPath(Player * p, NeuronList * neuronListRoot) {
  216.     return updatePath(p,NULL);
  217. }
  218.  
  219. PlayerPath * updatePath(Player * p, PlayerPath * prev, NeuronList * neuronListRoot) {
  220.     PlayerPath * ret;
  221.     ret = malloc(sizeof(PlayerPath));
  222.     if (prev == NULL) {
  223.         ret->prev = ret;
  224.         prev = ret;
  225.     }
  226.     else
  227.         ret->prev = prev;
  228.     ret->neuron = getNeuron(p->x,p->y,p->z,neuronListRoot);
  229.     updateNeuron(p,ret,pathFrequency(prev->prev));
  230.     return ret;
  231. }
  232. void updateNeuron(Player * p, PlayerPath * path, unsigned long long pathFreq) {
  233.     //getDendrite(path->neuron,path->prev->neuron->frequency,true),pathFreq));
  234.     updateSynapse(getFrequency(getDendrite(path->prev->neuron,path->prev->prev->neuron->frequency),pathFreq,p,true)->root,p,path->neuron);
  235.     getDendrite(path->neuron,path->prev->neuron);
  236. }
  237. Synapse * updateSynapse(Frequency * freq, Player * p, Neuron * n) {
  238.     return updateSynapse(p,n,freq->root)
  239. }
  240. Synapse * updateSynapse(Synapse * root,Player * p, Neuron * n) {
  241.     if (!root) {
  242.         root = malloc(sizeof(Synapse));
  243.         root->root = getPlayerWeight(NULL,p);
  244.         root->neuron = n;
  245.     }
  246.     if (root->neuron->frequency == n->frequency) {
  247.         return root;
  248.     }
  249.     else if (root->neuron->frequency > n->frequency) {
  250.         return updateSynapse(root->left,p,n);
  251.     }
  252.     else {
  253.         return updateSynapse(root->right,p,n);
  254.     }
  255. }
  256. SynapseTree * createSynapseTree(Frequency * freq, Player * p) {
  257.     return buildSynapseTree(NULL,ret,freq->root,p);
  258. }
  259. SynapseTree * buildSynapseTree(SynapseTree * tree, Synapse * root, Player * p) {
  260.     if (!root) return tree;
  261.     float weight = frequencyWeight(getPlayerWeight(root->root,p));
  262.     addSynapseTree(tree,root,weight)
  263.     buildSynapseTree(tree,root->left,p);
  264.     buildSynapseTree(tree,root->right,p);
  265.     return tree;
  266. }
  267. SynapseTree * addSynapseTree(SynapseTree * tree, Synapse * root,float weight) {
  268.     if (!root) return tree;
  269.     if (!tree) {
  270.         tree = malloc(sizeof(SynapseTree));
  271.         tree->synapse = root;
  272.         tree->value = weight;
  273.         return tree;
  274.     }
  275.     if (tree->value > weight) {
  276.         if (tree->left == NULL) {
  277.             tree->left = malloc(sizeof(SynapseTree));
  278.             tree->left->value = weight;
  279.             tree->left->synapse = root;
  280.             return tree;
  281.         }
  282.         addSynapseTree(tree->left,root,weight);
  283.     }
  284.     if (tree->value < weight) {
  285.         if (tree->right = NULL) {
  286.             tree->right = malloc(sizeof(SynapseTree));
  287.             tree->right->value = weight;
  288.             tree->right->synapse = root;
  289.             return tree;
  290.         }
  291.         addSynapseTree(tree->right,root,weight);
  292.     }
  293.     return tree;
  294. }
  295. Synapse * getSynapse(Frequency * freq, Player * p, bool retNew) {
  296.     createSynapseTree(freq->synapse,p,retNew)
  297. }
  298. Synapse * getSynapse(Frequency * freq, Player * p) {
  299.     createSynapseTree(freq->synapse,p,false);
  300. }
  301.  
  302. PlayerWeight * getPlayerWeight(PlayerWeight * pw, Player * p) {
  303.     //TODO: Replace steamID variable with real
  304.     if (pw == NULL) {
  305.         pw = malloc(sizeof(PlayerWeight));
  306.         pw->playerId = p->steamID;
  307.         pw->count = 1;
  308.         pw->weight = 1;
  309.         return pw;
  310.     }
  311.     if (pw->playerId > p->steamID) {
  312.         if (pw->left == NULL) {
  313.             pw->left = malloc(sizeof(PlayerWeight));
  314.             pw->left->playerId = p->steamID;
  315.             pw->left->count = 1;
  316.             pw->left->weight = 1;
  317.             return pw->left;
  318.         }
  319.         return getPlayerWeight(pw->left, p);
  320.     }
  321.     if (pw->playerId < p->steamID) {
  322.         if (pw->right == NULL) {
  323.             pw->right = malloc(sizeof(PlayerWeight));
  324.             pw->right->playerId = p->steamID;
  325.             pw->right->count = 1;
  326.             pw->right->weight = 1;
  327.             return pw->right;
  328.         }
  329.         return getPlayerWeight(pw->right, p);
  330.     }
  331.     pw->count++;
  332.     return pw;
  333. }
  334.  
  335. FrequencyOffset * addFrequencyOffset(FrequencyOffset * ret, Frequency * freq, unsigned long long offset) {
  336.     if (ret->offset > offset) {
  337.         if (ret->left == NULL) {
  338.             ret->left = malloc(sizeof(FrequencyOffset));
  339.             ret->left->frequency = freq;
  340.             ret->left->offset = offset;
  341.             return ret->left;
  342.         }
  343.         else {
  344.             return addFrequencyOffset(ret->left,freq,offset);
  345.         }
  346.     }
  347.     if (ret->offset < offset) {
  348.         if (ret->right == NULL) {
  349.             ret->right = malloc(sizeof(FrequencyOffset));
  350.             ret->right->frequency = freq;
  351.             ret->right->offset = offset;
  352.             return ret->right;
  353.         }
  354.         else {
  355.             return addFrequencyOffset(ret->right,freq,offset);
  356.         }
  357.     }
  358.     return ret;
  359. }
  360.  
  361. FrequencyOffset * fillFrequencyOffset(FrequencyOffset * ret, Frequency * freq, Player * p, unsigned long long parentFreq,bool sniff,bool retNew) {
  362.     if (sniff) {
  363.         if (freq->frequency > parentFreq) {
  364.             if (freq->right != NULL)
  365.                 fillFrequencyOffset(freq->right,p,parentFreq,false,retNew);
  366.             if (freq->left == NULL) {
  367.                 if (retNew) {
  368.                     freq->left = malloc(sizeof(Frequency));
  369.                     freq->left->frequency = parentFreq;
  370.                     freq->left->proot = malloc(sizeof(PlayerWeight));
  371.                     freq->left->proot->count = 1;
  372.                     freq->left->proot->playerId = p->steamId;
  373.                     fillFrequencyOffset(ret,freq->left,p,parentFreq,false,retNew);
  374.                 }
  375.                 return ret;
  376.             }
  377.             else {
  378.                 fillFrequencyOffset(ret,freq->left,p,parentFreq,true,retNew);
  379.             }
  380.         }
  381.         if (freq->frequency < parentFreq) {
  382.             if (freq->left != NULL)
  383.                 fillFrequencyOffset(freq->left,p,parentFreq,false,retNew);
  384.             if (freq->right == NULL) {
  385.                 if (retNew) {
  386.                     freq->right = malloc(sizeof(Frequency));
  387.                     freq->right->frequency = parentFreq;
  388.                     freq->right->proot = malloc(sizeof(PlayerWeight));
  389.                     freq->right->proot->count = 1;
  390.                     freq->right->proot->playerId = p->steamId;
  391.                     fillFrequencyOffset(ret,freq->right,p,parentFreq,false,retNew);
  392.                 }
  393.                 return ret;
  394.             }
  395.             else {
  396.                 fillFrequencyOffset(ret,freq->right,p,parentFreq,true,retNew);
  397.             }
  398.         }
  399.     }
  400.     else {
  401.         if (freq->left != NULL)
  402.             fillFrequencyOffset(ret,freq->left,p,parentFreq,false,retNew);
  403.         if (freq->right != NULL)
  404.             fillFrequencyOffset(ret,freq->right,p,parentFreq,false,retNew);
  405.     }
  406.     addFrequencyOffset(ret,freq,ceil(abs(parentFreq - freq->frequency) * frequencyWeight(getPlayerWeight(freq->proot,p))))
  407.     return ret;
  408. }
  409. FrequencyOffset * createFrequencyOffset(Frequency * freq, Player * p, unsigned long long parentFreq, bool retNew) {
  410.     FrequencyOffset * ret = malloc(sizeof(FrequencyOffset));
  411.     ret->frequency = freq;
  412.     ret->offset = ceil(abs(parentFreq - freq->frequency) * frequencyWeight(getPlayerWeight(freq->proot,p)))
  413.     return fillFrequencyOffset(ret,freq,p,parentFreq,true,retNew);
  414.    
  415. }
  416.  
  417. FrequencyOffset * getFrequency(FrequencyOffset * freq,Player * p) {
  418.     if (freq->offset > 0) {
  419.         if (freq->left == NULL) {
  420.             return freq;
  421.         }
  422.         else {
  423.             return getFrequency(freq->left,p);
  424.         }
  425.     }
  426.     if (freq->offset < 0) {
  427.         if (freq->right == NULL) {
  428.             return freq;
  429.         }
  430.         else {
  431.             return getFrequency(freq->right,p);
  432.         }
  433.     }
  434.     return freq;
  435. }
  436.  
  437. Frequency * getFrequency(Dendrite * dend,unsigned long long parentFreq,Player * p,bool retNew) {
  438.     Frequency * freq = dend->root;
  439.     if (freq == NULL) {
  440.         freq = malloc(sizeof(Frequency));
  441.         freq->frequency = parentFreq;
  442.     }
  443.     FrequencyOffset * root = createFrequencyOffset(freq,p,parentFreq,retNew);
  444.     Frequency * ret = getFrequency(root,p)->frequency;
  445.     deallocateFrequencyOffsetTree(root);
  446.     return ret;
  447. }
  448.  
  449. Frequency * getFrequency(Dendrite * dend,unsigned long long p,Player * pl) {
  450.     return getFrequency(dend,p,pl,false);
  451. }
  452.  
  453. void deallocateFrequencyOffsetTree(FrequencyOffset * root) {
  454.     if (!root) return;
  455.     deallocateFrequencyOffsetTree(root->left);
  456.     deallocateFrequencyOffsetTree(root->right);
  457.     free(root);
  458. }
  459. void deallocatePlayerPath(PlayerPath * path) {
  460.     if (!path) return;
  461.     if (path != path->prev)
  462.         deallocatePlayerPath(path->prev);
  463.     free(path);
  464. }
  465. void deallocateSynapseTree(SynapseTree * tree) {
  466.     if (!tree) return;
  467.     deallocateSynapseTree(tree->left);
  468.     deallocateSynapseTree(tree->right);
  469.     free(tree);
  470. }
  471. /*************************************************/
  472. Dendrite * getDendrite(Dendrite * dend, unsigned long long parentFreq, bool retNew) {
  473.     if (dend->frequency == parentFreq)
  474.         return dend;
  475.     if (dend->frequency > parentFreq)
  476.         if (dend->left == NULL) {
  477.             dend->left = malloc(sizeof(Dendrite));
  478.             dend->left->frequency = parentFreq;
  479.             return retNew ? dend->left : dend;
  480.         }
  481.         else
  482.             return getDendrite(dend->left,parentFreq);
  483.     if (dend->frequency < parentFreq)
  484.         if (dend->right == NULL) {
  485.             dend->right = malloc(sizeof(Dendrite));
  486.             dend->right->frequency = parentFreq;
  487.             return retNew ? dend->right : dend;
  488.         }
  489.         else
  490.             return getDendrite(dend->right,parentFreq);
  491. }
  492.  
  493. Dendrite * getDendrite(Neuron * neuron,unsigned long long parentFreq,bool retNew) {
  494.     Dendrite * dend = neuron->root;
  495.     if (dend == NULL) {
  496.         dend = malloc(sizeof(Dendrite));
  497.         dend->frequency = parentFreq;
  498.         neuron->root = dend;
  499.     }
  500.     return getDendrite(dend,parentFreq,retNew);
  501. }
  502.  
  503. Dendrite * getDendrite(Neuron * neuron,unsigned long long parentFreq) {
  504.     return getDendrite(neuron,parentFreq,false);
  505. }
  506.  
  507. unsigned long long pathFrequency(PlayerPath * path) {
  508.     PlayerPath * iter = path;
  509.     unsigned long long frequency = 0;
  510.     for (int i = 0; i < FREQUENCY_DEPTH; i++) {
  511.         frequency += iter->neuron->frequency;
  512.         iter = iter->prev;
  513.     }
  514.     return frequency;
  515. }
  516.  
  517. float frequencyWeight(PlayerWeight * weight) {
  518.     if (weight == NULL)
  519.         return 1f;
  520.     return 1f - ((PlAYER_WEIGHT_MAX - (PLAYER_WEIGHT_FLUX / (pow(4f,weight->count) / pow(3.6f,weight->count)))) / 100);
  521. }
  522.  
  523. int transformCoord(int xyz) {
  524.     return (int)((xyz + WORLD_OFFSET) / UNITS_PER_SQUARE);
  525. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement