Advertisement
imk0tter

Imk0tter

Dec 9th, 2010
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.79 KB | None | 0 0
  1. #import <math.h>
  2.  
  3. #define PLAYER_WEIGHT_MAX 20
  4. #define PLAYER_WEIGHT_FLUX 12
  5.  
  6. #define WORLD_GRID_SIZE 8191
  7. #define WORLD_OFFSET 4096
  8.  
  9. #define FREQUENCY_DEPTH 20
  10.  
  11. typedef struct Neuron {
  12.     Dendrite * root;
  13.     long frequency;
  14. } neuronArrayPtr[WORLD_GRID_SIZE ^ 2];
  15.  
  16. typedef struct Dendrite{
  17.     Dendrite * left;
  18.     Dendrite * right;
  19.     long frequency;
  20.     Frequency * root;
  21. };
  22.  
  23. typedef struct Frequency {
  24.     Frequency * left;
  25.     Frequency * right;
  26.     long frequency;
  27.     PlayerWeight * proot;
  28.     Synapse * root;
  29. };
  30.  
  31. typedef struct Synapse {
  32.     Synapse * left;
  33.     Synapse * right;
  34.     PlayerWeight * root;
  35.     Neuron * neuron;
  36. };
  37.  
  38. typedef struct PlayerWeight {
  39.     PlayerWeight * left;
  40.     PlayerWeight * right;
  41.     long playerId;
  42.     int count;
  43. };
  44.  
  45. typedef struct PlayerPath {
  46.     PlayerPath * prev;
  47.     Neuron * neuron;
  48. };
  49.  
  50. typedef struct FrequencyOffset {
  51.     FrequencyOffset * left;
  52.     FrequencyOffset * right;
  53.     long offset;
  54.     Frequency * frequency;
  55. };
  56. PlayerPath * createPath(Player * p) {
  57.     return updatePath(p,NULL);
  58. }
  59.  
  60. PlayerPath * updatePath(Player * p, PlayerPath * prev) {
  61.     PlayerPath * ret;
  62.     ret = malloc(sizeof(PlayerPath));
  63.     if (prev == NULL)
  64.         ret->prev = ret;
  65.     else
  66.         ret->prev = prev;
  67.     ret->neuron = getNeuron(p->x,p->y);
  68.     updateNeuron(p,ret,pathFrequency(prev));
  69.     return ret;
  70. }
  71. void updateNeuron(Player * p, PlayerPath * path, long pathFreq) {
  72.     //getDendrite(path->neuron,path->prev->neuron->frequency,true),pathFreq));
  73.     getSynapse(getFrequency(getDendrite(path->prev->neuron,path->prev->prev->neuron->frequency),pathFreq,p,true),p);   
  74. }
  75.  
  76. PlayerWeight * getPlayerWeight(PlayerWeight * pw, Player * p) {
  77.     //TODO: Replace steamID variable with real 
  78.     if (pw->playerId > p->steamID) {
  79.         if (pw->left == NULL) {
  80.             pw->left = malloc(sizeof(PlayerWeight));
  81.             pw->left->playerId = p->steamID;
  82.             pw->left->count = 1;
  83.             pw->left->weight = 1;
  84.             return pw->left;
  85.         }
  86.         return getPlayerWeight(pw->left, p);
  87.     }
  88.     if (pw->playerId < p->steamID) {
  89.         if (pw->right == NULL) {
  90.             pw->right = malloc(sizeof(PlayerWeight));
  91.             pw->right->playerId = p->steamID;
  92.             pw->right->count = 1;
  93.             pw->right->weight = 1;
  94.             return pw->right;
  95.         }
  96.         return getPlayerWeight(pw->right, p);
  97.     }
  98.     pw->count++;
  99.     return pw;
  100. }
  101. FrequencyOffset * addFrequencyOffset(FrequencyOffset * ret, Frequency * freq, long offset) {
  102.     if (ret->offset > offset) {
  103.         if (ret->left == NULL) {
  104.             ret->left = malloc(sizeof(FrequencyOffset));
  105.             ret->left->frequency = freq;
  106.             ret->left->offset = offset;
  107.             return ret->left;
  108.         }
  109.         else {
  110.             return addFrequencyOffset(ret->left,freq,offset);
  111.         }
  112.     }
  113.     if (ret->offset < offset) {
  114.         if (ret->right == NULL) {
  115.             ret->right = malloc(sizeof(FrequencyOffset));
  116.             ret->right->frequency = freq;
  117.             ret->right->offset = offset;
  118.             return ret->right;
  119.         }
  120.         else {
  121.             return addFrequencyOffset(ret->right,freq,offset);
  122.         }
  123.     }
  124.     return ret;
  125. }
  126. FrequencyOffset * fillFrequencyOffset(FrequencyOffset * ret, Frequency * freq, Player * p, long parentFreq,bool sniff) {
  127.     if (sniff) {
  128.         if (freq->frequency > parentFreq) {
  129.             if (freq->left == NULL) {
  130.                 freq->left = malloc(sizeof(Frequency));
  131.                 freq->left->frequency = parentFreq;
  132.                 freq->left->proot = malloc(sizeof(PlayerWeight));
  133.                 freq->left->proot->count = 1;
  134.                 freq->left->proot->playerId = p->steamId;
  135.             }
  136.             else {
  137.                 fillFrequencyOffset(ret,freq->left,p,parentFreq,true);
  138.                 if (freq->right != NULL)
  139.                     fillFrequencyOffset(freq->right,p,parentFreq,false);
  140.             }
  141.             return ret;
  142.         }
  143.         if (freq->frequency < parentFreq) {
  144.             if (freq->right == NULL) {
  145.                 freq->right = malloc(sizeof(Frequency));
  146.                 freq->right->frequency = parentFreq;
  147.                 freq->right->proot = malloc(sizeof(PlayerWeight));
  148.                 freq->right->proot->count = 1;
  149.                 freq->right->proot->playerId = p->steamId;
  150.             }
  151.             else {
  152.                 fillFrequencyOffset(ret,freq->right,p,parentFreq,true);
  153.                 if (freq->left != NULL)
  154.                     fillFrequencyOffset(freq->left,p,parentFreq,false);
  155.             }
  156.             return ret;
  157.         }
  158.     }
  159.     if (freq->left != NULL)
  160.         fillFrequencyOffset(ret,freq->left,p,parentFreq,false);
  161.     if (freq->right != NULL)
  162.         fillFrequencyOffset(ret,freq->right,p,parentFreq,false);
  163.     addFrequencyOffset(ret,freq,ceil(abs(parentFreq - freq->frequency) * frequencyWeight(freq,getPlayerWeight(freq->proot,p))))
  164.     return ret;
  165. }
  166. FrequencyOffset * createFrequencyOffset(Frequency * freq, Player * p, long parentFreq) {
  167.     FrequencyOffset * ret = malloc(sizeof(FrequencyOffset));
  168.     ret->frequency = freq;
  169.     ret->offset = ceil(abs(parentFreq - freq->frequency) * frequencyWeight(freq,getPlayerWeight(freq->proot,p)))
  170.     return fillFrequencyOffset(ret,freq,p,parentFreq,true);
  171.    
  172. }
  173.  
  174. FrequencyOffset * getFrequency(FrequencyOffset * freq,Player * p) {
  175.     if (freq->offset > 0) {
  176.         if (freq->left == NULL) {
  177.             return freq;
  178.         }
  179.         else {
  180.             return getFrequency(freq->left,p);
  181.         }
  182.     }
  183.     if (freq->offset < 0) {
  184.         if (freq->right == NULL) {
  185.             return freq;
  186.         }
  187.         else {
  188.             return getFrequency(freq->right,p);
  189.         }
  190.     }
  191.     return freq;
  192. }
  193.  
  194. Frequency * getFrequency(Dendrite * dend,long parentFreq,Player * p,bool retNew) {
  195.     Frequency * freq = dend->root;
  196.     if (freq == NULL) {
  197.         freq = malloc(sizeof(Frequency));
  198.         freq->frequency = parentFreq;
  199.     }
  200.     Frequency * ret = getFrequency(createFrequencyOffset(freq,p,parentFreq),p)->frequency;
  201.     return ret;
  202. }
  203.  
  204. Frequency * getFrequency(Dendrite * dend,long p,Player * pl) {
  205.     return getFrequency(dend,p,pl,false);
  206. }
  207. /*************************************************/
  208. Dendrite * getDendrite(Dendrite * dend,long parentFreq,bool retNew) {
  209.     if (dend->frequency == parentFreq)
  210.         return dend;
  211.     if (dend->frequency > parentFreq)
  212.         if (dend->left == NULL) {
  213.             dend->left = malloc(sizeof(Dendrite));
  214.             dend->left->frequency = parentFreq;
  215.             return retNew ? dend->left : dend;
  216.         }
  217.         else
  218.             return getDendrite(dend->left,parentFreq);
  219.     if (dend->frequency < parentFreq)
  220.         if (dend->right == NULL) {
  221.             dend->right = malloc(sizeof(Dendrite));
  222.             dend->right->frequency = parentFreq;
  223.             return retNew ? dend->right : dend;
  224.         }
  225.         else
  226.             return getDendrite(dend->right,parentFreq);
  227. }
  228.  
  229. Dendrite * getDendrite(Neuron * neuron,long parentFreq,bool retNew) {
  230.     Dendrite * dend = neuron->root;
  231.     if (dend == NULL) {
  232.         dend = malloc(sizeof(Dendrite));
  233.         dend->frequency = parentFreq;
  234.         neuron->root = dend;
  235.     }
  236.     return getDendrite(dend,parentFreq,retNew);
  237. }
  238.  
  239. Dendrite * getDendrite(Neuron * neuron,long parentFreq) {
  240.     return getDendrite(neuron,parentFreq,false);
  241. }
  242.  
  243. long pathFrequency(PlayerPath * path) {
  244.     PlayerPath * iter = path;
  245.     long frequency = 0;
  246.     for (int i = 0; i < FREQUENCY_DEPTH; i++) {
  247.         frequency += iter->neuron->frequency;
  248.         iter = iter->prev;
  249.     }
  250.     return frequency;
  251. }
  252.  
  253. float frequencyWeight(Frequency * syn, PlayerWeight * weight) {
  254.     if (weight == NULL)
  255.         return 1f;
  256.     return 1f - ((PlAYER_WEIGHT_MAX - (PLAYER_WEIGHT_FLUX / (pow(4f,weight->count) / pow(3.6f,weight->count)))) / 100);
  257. }
  258.  
  259. float synapseWeight(Synapse * syn, PlayerWeight * weight) {
  260.         if (weight == NULL)
  261.         return 1f;
  262.     return 1f - ((PlAYER_WEIGHT_MAX - (PLAYER_WEIGHT_FLUX / (pow(4f,weight->count) / pow(3.6f,weight->count)))) / 100);
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement