Advertisement
imk0tter

Imk0tter

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