CBuildingHandler.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "../stdafx.h"
  2. #include "../CGameInfo.h"
  3. #include "CBuildingHandler.h"
  4. #include "CLodHandler.h"
  5. #include <sstream>
  6. std::string readTo(std::string in, unsigned int &it, char end)
  7. {
  8. int pom = it;
  9. int last = in.find_first_of(end,it);
  10. it+=(1+last-it);
  11. return in.substr(pom,last-pom);
  12. }
  13. unsigned int readNr(std::string in, unsigned int &it)
  14. {
  15. int last=it;
  16. for(;last<in.size();last++)
  17. if(in[last]=='\t' || in[last]=='\n' || in[last]==' ' || in[last]=='\r' || in[last]=='\n')
  18. break;
  19. if(last==in.size())
  20. throw new std::exception("Cannot read number...");
  21. std::stringstream ss(in.substr(it,last-it));
  22. it+=(1+last-it);
  23. ss >> last;
  24. return last;
  25. }
  26. CBuilding * readBg(std::string &buf, unsigned int& it)
  27. {
  28. CBuilding * nb = new CBuilding();
  29. for(int res=0;res<7;res++)
  30. nb->resources[res] = readNr(buf,it);
  31. nb->refName = readTo(buf,it,'\n');
  32. return nb;
  33. }
  34. void CBuildingHandler::loadBuildings()
  35. {
  36. std::string buf = CGameInfo::mainObj->bitmaph->getTextFile("BUILDING.TXT"), temp;
  37. unsigned int andame = buf.size(), it=0; //buf iterator
  38. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//read 2 lines of file info
  39. //read 9 special buildings for every faction
  40. for(int i=0;i<F_NUMBER;i++)
  41. {
  42. temp = readTo(buf,it,'\n');//read blank line and faction name
  43. temp = readTo(buf,it,'\n');
  44. for(int bg = 0; bg<9; bg++)
  45. {
  46. CBuilding *nb = readBg(buf,it);
  47. buildings[i][bg+17] = nb;
  48. }
  49. }
  50. //reading 17 neutral (common) buildings
  51. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//neutral buildings - skip 3 lines
  52. for(int bg = 0; bg<17; bg++)
  53. {
  54. CBuilding *nb = readBg(buf,it);
  55. for(int f=0;f<F_NUMBER;f++)
  56. buildings[f][bg] = new CBuilding(*nb);
  57. delete nb;
  58. }
  59. //reading 14 per faction dwellings
  60. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//dwellings - skip 2 lines
  61. for(int i=0;i<F_NUMBER;i++)
  62. {
  63. temp = readTo(buf,it,'\n');//read blank line
  64. temp = readTo(buf,it,'\n');// and faction name
  65. for(int bg = 0; bg<14; bg++)
  66. {
  67. CBuilding *nb = readBg(buf,it);
  68. buildings[i][bg+30] = nb;
  69. }
  70. }
  71. /////done reading BUILDING.TXT*****************************
  72. buf = CGameInfo::mainObj->bitmaph->getTextFile("BLDGNEUT.TXT");
  73. andame = buf.size(), it=0;
  74. for(int b=0;b<15;b++)
  75. {
  76. std::string name = readTo(buf,it,'\t'),
  77. description = readTo(buf,it,'\n');
  78. for(int fi=0;fi<F_NUMBER;fi++)
  79. {
  80. buildings[fi][b]->name = name;
  81. buildings[fi][b]->description = description;
  82. }
  83. }
  84. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//silo,blacksmith,moat - useless???
  85. //shipyard with the ship
  86. std::string name = readTo(buf,it,'\t'),
  87. description = readTo(buf,it,'\n');
  88. for(int fi=0;fi<F_NUMBER;fi++)
  89. {
  90. buildings[fi][20]->name = name;
  91. buildings[fi][20]->description = description;
  92. }
  93. for(int fi=0;fi<F_NUMBER;fi++)
  94. {
  95. buildings[fi][16]->name = readTo(buf,it,'\t'),
  96. buildings[fi][16]->description = readTo(buf,it,'\n');
  97. }
  98. /////done reading "BLDGNEUT.TXT"******************************
  99. buf = CGameInfo::mainObj->bitmaph->getTextFile("BLDGSPEC.TXT");
  100. andame = buf.size(), it=0;
  101. for(int f=0;f<F_NUMBER;f++)
  102. {
  103. for(int b=0;b<9;b++)
  104. {
  105. buildings[f][17+b]->name = readTo(buf,it,'\t');
  106. buildings[f][17+b]->description = readTo(buf,it,'\n');
  107. }
  108. buildings[f][26] = new CBuilding();//grail
  109. buildings[f][26]->name = readTo(buf,it,'\t');
  110. buildings[f][26]->description = readTo(buf,it,'\n');
  111. buildings[f][15]->name = readTo(buf,it,'\t'); //resource silo
  112. buildings[f][15]->description = readTo(buf,it,'\n');//resource silo
  113. }
  114. /////done reading BLDGSPEC.TXT*********************************
  115. buf = CGameInfo::mainObj->bitmaph->getTextFile("DWELLING.TXT");
  116. andame = buf.size(), it=0;
  117. for(int f=0;f<F_NUMBER;f++)
  118. {
  119. for(int b=0;b<14;b++)
  120. {
  121. buildings[f][30+b]->name = readTo(buf,it,'\t');
  122. buildings[f][30+b]->description = readTo(buf,it,'\n');
  123. }
  124. }
  125. }