1 #include <Choam.h>
2
3 #include <Game.h>
4 #include <House.h>
5 #include <globals.h>
6
7 #include <FileClasses/TextManager.h>
8
9 #include <algorithm>
10
11 // change starport prices every minute
12 #define CHOAM_CHANGE_PRICETIME (MILLI2CYCLES(60*1000))
13
14 // change amount of available units every 30s
15 #define CHOAM_CHANGE_AMOUNT (MILLI2CYCLES(30*1000))
16
Choam(House * pHouse)17 Choam::Choam(House* pHouse) : house(pHouse) {
18 }
19
~Choam()20 Choam::~Choam() {
21 ;
22 }
23
save(OutputStream & stream) const24 void Choam::save(OutputStream& stream) const {
25 stream.writeUint32(availableItems.size());
26 for(const BuildItem& buildItem : availableItems) {
27 buildItem.save(stream);
28 }
29 }
30
load(InputStream & stream)31 void Choam::load(InputStream& stream) {
32 Uint32 num = stream.readUint32();
33 for(Uint32 i=0;i<num;i++) {
34 BuildItem tmp;
35 tmp.load(stream);
36 availableItems.push_back(tmp);
37 }
38 }
39
getPrice(Uint32 itemID) const40 int Choam::getPrice(Uint32 itemID) const {
41 for(const BuildItem& buildItem : availableItems) {
42 if(buildItem.itemID == itemID) {
43 return buildItem.price;
44 }
45 }
46
47 return 0;
48 }
49
isCheap(Uint32 itemID) const50 bool Choam::isCheap(Uint32 itemID) const {
51 return (getPrice(itemID) < currentGame->objectData.data[itemID][house->getHouseID()].price * FixPt(1,3)); // A bit of logic to make starports better
52 }
53
getNumAvailable(Uint32 itemID) const54 int Choam::getNumAvailable(Uint32 itemID) const {
55 for(const BuildItem& buildItem : availableItems) {
56 if(buildItem.itemID == itemID) {
57 return buildItem.num;
58 }
59 }
60
61 return INVALID;
62 }
63
setNumAvailable(Uint32 itemID,int newValue)64 bool Choam::setNumAvailable(Uint32 itemID, int newValue) {
65 for(BuildItem& buildItem : availableItems) {
66 if(buildItem.itemID == itemID) {
67 buildItem.num = newValue;
68 return (buildItem.num > 0);
69 }
70 }
71
72 return false;
73 }
74
addItem(Uint32 itemID,int num)75 void Choam::addItem(Uint32 itemID, int num) {
76 BuildItem tmp;
77 tmp.itemID = itemID;
78 tmp.num = num;
79 availableItems.push_back(tmp);
80 }
81
update()82 void Choam::update() {
83 if(availableItems.empty()) {
84 return;
85 }
86
87 if((currentGame->getGameCycleCount() % CHOAM_CHANGE_AMOUNT) == 0) {
88 int index = currentGame->randomGen.rand((Uint32) 0, availableItems.size() - 1);
89 availableItems[index].num = std::min(availableItems[index].num + 1, (Uint32) 10);
90 }
91
92
93 if((currentGame->getGameCycleCount() % CHOAM_CHANGE_PRICETIME) == 0) {
94 for(BuildItem& buildItem : availableItems) {
95 int price = currentGame->objectData.data[buildItem.itemID][house->getHouseID()].price;
96
97 const int min_mod = 2;
98 const int max_mod = 8;
99
100 int rand1 = currentGame->randomGen.rand(min_mod, max_mod);
101 int rand2 = currentGame->randomGen.rand(min_mod, max_mod);
102
103 price = std::min((rand1+rand2)*(price/10), 999);
104
105 buildItem.price = price;
106 }
107
108 if((pLocalHouse == house) && (house->hasStarPort())) {
109 currentGame->addToNewsTicker(_("New Starport prices"));
110 }
111 }
112 }
113