Advertisement
lucasgautheron

Untitled

Jun 5th, 2011
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. struct servermap
  2. {
  3.     const char *name;
  4.     int minplayers, maxplayers, bestplayers;
  5.     vector<int> modes;
  6.     int lastplayedmillis;
  7.     int lastmodesmillis[GMODE_NUM];
  8.     int bestmode; // auto generated;
  9.    
  10.     servermap() : name(""), minplayers(0), maxplayers(0), bestplayers(0), lastplayedmillis(0), bestmode(-1)
  11.     {
  12.         loopi(GMODE_NUM) { modes[i] = 0; lastmodesmillis[i] = 0; }
  13.     }
  14. };
  15.  
  16. vector<servermap *>servermaps;
  17.  
  18. // when the cfg file is read
  19. void smap_add(const char *map, ...)
  20. {
  21.     servermap smap;
  22.     smap.name = newstring(map);
  23.     servermaps.add(smap);
  24. }
  25.  
  26. // when a game starts
  27. void smap_update(const char *map, int gamemode)
  28. {
  29.     if(gamemode < 0 || gamemode >= GMODE_NUM) return;
  30.     loopv(servermaps) if(!strcmp(servermaps[i]->name, map))
  31.     {
  32.         servermaps[i]->lastmodesmillis[gamemode] = servermaps[i]->lastplayedmillis = servmillis;
  33.     }
  34. }
  35.  
  36. // can a specified map/mode be voted now ?
  37. bool fitsmap(char *map, int mode)
  38. {
  39.     int players_count = numclients();
  40.     loopv(servermaps) if(!strcmp(servermaps[i], map))
  41.     {
  42.         servermap *smap = sevrermaps[i];
  43.         if(players_count < smap->minplayers || players_count > smap->maxplayers || !modes.find(mode)) return false;
  44.         return true;
  45.     }
  46.     return false;
  47. }
  48.  
  49. // get the ideal map to be played
  50. servermap *smap_best()
  51. {
  52.     int bestsmapscore = 0, servermap *bestsmap = NULL;
  53.     loopv(servermaps)
  54.     {
  55.         int smapscore = 0;
  56.         serversmap *smap = servermaps[i];
  57.         int players_count = numclients();
  58.         if(players_count < smap->minplayers || players_count > smap->maxplayers) continue; // too many / not enough players
  59.         if(!modes.length()) continue; // no game mode available
  60.        
  61.         smapscore += 100 - pow(abs(players_count-smap->bestplayers), 2); // diff = 0 : 100 pts, diff = 5 : 75 pts, diff = 10 : 0 pts and then minus points (max = 100 pts !)
  62.         smapscore += sqrt(servmillis - smap->lastplayedmillis) / 10; // 1 hour = 3600 sec = 189 points, 15 minutes = 94 points
  63.         smapscore += rnd(50);
  64.        
  65.         oldestmodemillis = 0;
  66.         loopv(modes) if(smap->bestmode < 0 || oldestmodemillis < smap->lastmodesmillis[modes[i]])
  67.         {
  68.             smap->bestmode = modes[i];
  69.             oldestmodemillis = smap->lastmodesmillis[modes[i]];
  70.         }
  71.        
  72.         if(!bestsmap || bestsmapscore <= smapscore)
  73.         {
  74.             bestsmapscore = smapscore;
  75.             bestmap = smap;
  76.         }
  77.     }
  78.    
  79.     return bestsmap;
  80. }
  81.  
  82. // format : mapname:minplayers:maxplayers:bestplayers:"list of modes num":
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement