1 //  Copyright (C) 2008, 2009, 2014, 2017 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 //  02110-1301, USA.
17 
18 #include <sstream>
19 #include <iostream>
20 #include <fstream>
21 #include "ucompose.hpp"
22 
23 #include "CreateScenarioRandomize.h"
24 
25 #include "File.h"
26 #include "citylist.h"
27 #include "city.h"
28 #include "ruin.h"
29 #include "temple.h"
30 #include "signpost.h"
31 #include "armysetlist.h"
32 #include "playerlist.h"
33 #include "SightMap.h"
34 #include "reward.h"
35 #include "rnd.h"
36 #include "keeper.h"
37 
38 //#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<std::flush;}
39 #define debug(x)
40 
CreateScenarioRandomize()41 CreateScenarioRandomize::CreateScenarioRandomize()
42 {
43     // Fill the namelists
44     bool success = true;
45 
46     d_citynames = new NameList("citynames.xml", "city");
47     d_templenames = new NameList("templenames.xml", "temple");
48     d_ruinnames = new NameList("ruinnames.xml", "ruin");
49     d_signposts = new NameList("signposts.xml", "signpost");
50 
51     if (!success)
52     {
53         std::cerr <<"CreateScenarioRandomize: Didn't succeed in reading object names. Aborting!\n";
54         exit(-1);
55     }
56 }
57 
popRandomCityName()58 Glib::ustring CreateScenarioRandomize::popRandomCityName()
59 {
60   Glib::ustring name = d_citynames->popRandomName().c_str();
61   if (name == "")
62     return City::getDefaultName();
63   return name;
64 }
65 
pushRandomCityName(Glib::ustring name)66 void CreateScenarioRandomize::pushRandomCityName(Glib::ustring name)
67 {
68   d_citynames->push_back(name);
69 }
70 
popRandomRuinName()71 Glib::ustring CreateScenarioRandomize::popRandomRuinName()
72 {
73   Glib::ustring name = d_ruinnames->popRandomName().c_str();
74   if (name == "")
75     return Ruin::getDefaultName();
76   return name;
77 }
78 
pushRandomRuinName(Glib::ustring name)79 void CreateScenarioRandomize::pushRandomRuinName(Glib::ustring name)
80 {
81   d_ruinnames->push_back(name);
82 }
83 
popRandomTempleName()84 Glib::ustring CreateScenarioRandomize::popRandomTempleName()
85 {
86   Glib::ustring name = d_templenames->popRandomName().c_str();
87   if (name == "")
88     return Temple::getDefaultName();
89   return name;
90 }
91 
pushRandomTempleName(Glib::ustring name)92 void CreateScenarioRandomize::pushRandomTempleName(Glib::ustring name)
93 {
94   d_templenames->push_back(name);
95 }
96 
popRandomSignpost()97 Glib::ustring CreateScenarioRandomize::popRandomSignpost()
98 {
99   return d_signposts->popRandomName().c_str();
100 }
101 
pushRandomSignpost(Glib::ustring name)102 void CreateScenarioRandomize::pushRandomSignpost(Glib::ustring name)
103 {
104   d_signposts->push_back(name);
105 }
106 
getRandomCityIncome(bool capital)107 guint32 CreateScenarioRandomize::getRandomCityIncome(bool capital)
108 {
109   if (capital)
110     return 33 + (Rnd::rand() % 8);
111   else
112     return 15 + (Rnd::rand() % 12);
113 }
114 
getRandomRuinKeeper(Vector<int> pos)115 Keeper* CreateScenarioRandomize::getRandomRuinKeeper(Vector<int> pos)
116 {
117   const ArmyProto *a = Keeper::randomRuinDefender();
118   if (a)
119     return new Keeper (a, pos);
120   return NULL;
121 }
122 
get_direction(int xdir,int ydir)123 Glib::ustring CreateScenarioRandomize::get_direction(int xdir, int ydir)
124 {
125   if (xdir >= 1 && ydir >= 1)
126     return _("southeast");
127   else if (xdir >= 1 && ydir == 0)
128     return _("east");
129   else if (xdir >= 1 && ydir <= -1)
130     return _("northeast");
131   else if (xdir == 0 && ydir >= 1)
132     return _("south");
133   else if (xdir == 0 && ydir <= -1)
134     return _("north");
135   else if (xdir <= -1 && ydir >= 1)
136     return _("southwest");
137   else if (xdir <= -1 && ydir == 0)
138     return _("west");
139   else if (xdir <= -1 && ydir <= -1)
140     return _("northwest");
141   return _("nowhere");
142 }
143 
getDynamicSignpost(Signpost * signpost)144 Glib::ustring CreateScenarioRandomize::getDynamicSignpost(Signpost *signpost)
145 {
146   int xdir, ydir;
147   Vector<int> signpostPos = signpost->getPos();
148   City *nearCity = Citylist::getInstance()->getNearestCity(signpostPos);
149   if (nearCity == NULL)
150     return _("nowhere");
151 
152   Vector<int> cityPos = nearCity->getPos();
153   xdir = cityPos.x - signpostPos.x;
154   ydir = cityPos.y - signpostPos.y;
155   Glib::ustring dir = get_direction(xdir, ydir);
156   return String::ucompose(_("%1 lies to the %2"), nearCity->getName(), dir);
157 }
158 
getNewRandomReward()159 Reward *CreateScenarioRandomize::getNewRandomReward()
160 {
161   return Reward::createRandomReward(false, false);
162 }
163 
adjustBaseGold(int base_gold)164 int CreateScenarioRandomize::adjustBaseGold (int base_gold)
165 {
166   int gold = base_gold + ((Rnd::rand() % 7) - 4);
167   if (gold < 0)
168     gold = 0;
169   return gold;
170 }
171 
getBaseGold(int difficulty,int * base_gold)172 void CreateScenarioRandomize::getBaseGold (int difficulty, int *base_gold)
173 {
174   if (difficulty < 50)
175     *base_gold = 131;
176   else if (difficulty < 60)
177     *base_gold = 129;
178   else if (difficulty < 70)
179     *base_gold = 127;
180   else if (difficulty < 80)
181     *base_gold = 125;
182   else if (difficulty < 90)
183     *base_gold = 123;
184   else
185     *base_gold = 121;
186 }
187 
getPlayerName(Shield::Colour id)188 Glib::ustring CreateScenarioRandomize::getPlayerName(Shield::Colour id)
189 {
190   Glib::ustring name = "";
191   switch (id)
192     {
193     case Shield::WHITE:
194       name = _("The Sirians");
195       break;
196     case Shield::GREEN:
197       name = _("Elvallie");
198       break;
199     case Shield::YELLOW:
200       name = _("Storm Giants");
201       break;
202     case Shield::DARK_BLUE:
203       name = _("Horse Lords");
204       break;
205     case Shield::ORANGE:
206       name = _("Grey Dwarves");
207       break;
208     case Shield::LIGHT_BLUE:
209       name = _("The Selentines");
210       break;
211     case Shield::RED:
212       name = _("Orcs of Kor");
213       break;
214     case Shield::BLACK:
215       name = _("Lord Bane");
216       break;
217     case Shield::NEUTRAL:
218       name = _("Neutrals");
219       break;
220     }
221   return name;
222 }
223 
cleanup()224 void CreateScenarioRandomize::cleanup()
225 {
226   if (d_citynames)
227     delete d_citynames;
228   if (d_signposts)
229     delete d_signposts;
230   if (d_templenames)
231     delete d_templenames;
232   if (d_ruinnames)
233     delete d_ruinnames;
234 }
235