News:

Simutrans Wiki Manual
The official on-line manual for Simutrans. Read and contribute.

[small patch] customizable starting money depending on starting year

Started by Dwachs, February 05, 2009, 08:30:52 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Dwachs

This patch lets you (or the pakset-maintainers) customize the starting money depending on the starting year:

Putting something like this in (pak)/simuconf.tab
# Starting money of the player. Given in Credit cents (1/100 Cr)
# default value
starting_money = 20000000
# up to ten triples (0..9) of: year, money, interpolation-flag are allowed
# starting_money[i] = year, money, iflag
# if year is between starting_money[i-1].year and starting_money[i].year then:
# then the resulting starting money is either
# (a) equal to starting_money[i-1].year if iflag==0 or
# (b) linearly interpolated between starting_money[i-1].year and starting_money[i].year
# if year is smaller than any given year, the default value in starting_money is used,
# if year is larger than any given year, the money value of the starting_money[] with the largest year is used
starting_money[0] = 1950, 30000000, 1
starting_money[1] = 1960, 40000000
starting_money[2] = 1960, 50000000
starting_money[3] = 1970, 60000000, 1

gives

money = 20000000 before 1950 (here the 'old' variable is taken)
money = 30000000 in 1950
money = 35000000 in 1955
money = 40000000 in 1960
money = 50000000 in 1965 (no interpolation since no 3rd parameter specified for 1960)
money = 60000000 in 1970 and later.

Edit: The maximum amount of money is not touched, it is 2^31-1 ~ 20 millions. Until someone finds a method to convert strings to 64-integers that works across different platforms / compilers.

Parsley, sage, rosemary, and maggikraut.

VS


My projects... Tools for messing with Simutrans graphics. Graphic archive - templates and some other stuff for painters. Development logs for most recent information on what is going on. And of course pak128!

prissi


Spike

Quote from: Dwachs on February 05, 2009, 08:30:52 PM
Edit: The maximum amount of money is not touched, it is 2^31-1 ~ 20 millions. Until someone finds a method to convert strings to 64-integers that works across different platforms / compilers.

Something like that should work (initialize str with ASCII number):

char *str;
uint64 value=0;

while(*str) {
  value *= 10;
  value += *str - '0';
  str++;
}


I did not test this. But it should give an idea.

Frank

Quote from: prissi on June 02, 2009, 09:14:32 PM
Did someone save the patch?

yes

http://simutrans-germany.com/~patches/upload/startingmoney-2295.patch

Index: dataobj/einstellungen.cc
===================================================================
--- dataobj/einstellungen.cc (revision 2295)
+++ dataobj/einstellungen.cc (working copy)
@@ -518,7 +518,54 @@
show_pax = contents.get_int("stop_pedestrians", show_pax ) != 0;
stadtauto_duration = contents.get_int("default_citycar_life", stadtauto_duration); // ten normal years

+ // starting money
starting_money = contents.get_int("starting_money", starting_money );
+ // up to ten blocks year, money, interpolation={0,1} are possible:
+ // starting_money[i]=y,m,int
+ // y .. year
+ // m .. money (in 1/100 Cr)
+ // int .. interpolation: 0 - no interpolation, !=0 linear interpolated
+ // (m) is the starting money for player start after (y), if (i)!=0, the starting money
+ // is linearly interpolated between (y) and the next greater year given in another entry.
+ // starting money for given year is:
+ //
+ int j=0;
+ for(  int i = 0;  i<10;  i++  ) {
+ char name[32];
+ sprintf( name, "starting_money[%i]", i );
+ int *test = contents.get_ints(name);
+ if ((test[0]>1) && (test[0]<=3)) {
+ // insert sorted by years
+ int k=0;
+ for (k=0; k<i; k++) {
+ if (startingmoneyperyear[k].year > test[1]) {
+ for (int l=j; l>=k; l--)
+ memcpy( &startingmoneyperyear[l+1], &startingmoneyperyear[l], sizeof(yearmoney));
+ break;
+ }
+ }
+ startingmoneyperyear[k].year = test[1];
+ startingmoneyperyear[k].money = test[2];
+ if (test[0]==3) {
+ startingmoneyperyear[k].interpol = test[3]!=0;
+ }
+ else {
+ startingmoneyperyear[k].interpol = false;
+ }
+ j++;
+ }
+ else {
+ // invalid entry
+ }
+ delete [] test;
+ }
+ // fill remaining entries
+ for (int i=j+1; i<10; i++) {
+ startingmoneyperyear[i].year = 0;
+ startingmoneyperyear[i].money = 0;
+ startingmoneyperyear[i].interpol = 0;
+ }
+
maint_building = contents.get_int("maintenance_building", maint_building);

numbered_stations = contents.get_int("numbered_stations", numbered_stations ) != 0;
@@ -617,3 +664,53 @@

simuconf.close();
}
+
+
+sint64 einstellungen_t::get_starting_money(sint16 year) const {
+ // search entry with startingmoneyperyear[i].year >= year
+ int i; bool found = false;
+ for (i=0; i<10; i++) {
+ if (startingmoneyperyear[i].year!=0) {
+ if (startingmoneyperyear[i].year>=year) {
+ found = true;
+ break;
+ }
+ }
+ else {
+ // nothing found
+ if (i==0) {
+ // no 'starting_money[%i]'-entries in simuconf
+ // use the plain startingmoney variable
+ return starting_money;
+ }
+ else {
+ // year is behind the latest given date
+ return startingmoneyperyear[i-1].money;
+ }
+ }
+ }
+ if (i==0) {
+ if (startingmoneyperyear[0].year==year) {
+ return startingmoneyperyear[0].money;
+ }
+ else {
+ // before any given date
+ // use the plain startingmoney variable
+ return starting_money;
+ }
+ }
+ else {
+ // now: startingmoneyperyear[i-1].year <= year <= startingmoneyperyear[i].year
+ if (startingmoneyperyear[i-1].interpol) {
+ // linear interpolation
+ return startingmoneyperyear[i-1].money +
+ (startingmoneyperyear[i].money-startingmoneyperyear[i-1].money)
+ * (year-startingmoneyperyear[i-1].year) /(startingmoneyperyear[i].year-startingmoneyperyear[i-1].year);
+ }
+ else {
+ // no interpolation
+ return startingmoneyperyear[i-1].money;
+ }
+
+ }
+}
\ No newline at end of file
Index: dataobj/einstellungen.h
===================================================================
--- dataobj/einstellungen.h (revision 2295)
+++ dataobj/einstellungen.h (working copy)
@@ -100,7 +100,14 @@
bool freeplay;

sint64 starting_money;
+
+ typedef struct {
+ sint16 year;
+ sint64 money;
+ bool interpol; } yearmoney;

+ yearmoney startingmoneyperyear[10];
+
/**
* Use numbering for stations?
*
@@ -293,6 +300,7 @@
sint32 get_max_transfers() const { return max_transfers; }

sint64 get_starting_money() const { return starting_money; }
+ sint64 get_starting_money(sint16 year) const;

bool get_random_pedestrians() const { return fussgaenger; }
void set_random_pedestrians( bool f ) { fussgaenger = f; } // NETWORK!
Index: player/ai.cc
===================================================================
--- player/ai.cc (revision 2295)
+++ player/ai.cc (working copy)
@@ -348,7 +348,7 @@
if(besch!=NULL) {
// cost is negative!
sint64 cost = welt->get_einstellungen()->cst_multiply_headquarter*besch->get_level()*besch->get_b()*besch->get_h();
- if(  konto+cost > welt->get_einstellungen()->get_starting_money()  ) {
+ if(  konto+cost > starting_money ) {
// and enough money left ...
koord place = get_headquarter_pos();
if(place!=koord::invalid) {
Index: player/ai_passenger.cc
===================================================================
--- player/ai_passenger.cc (revision 2295)
+++ player/ai_passenger.cc (working copy)
@@ -941,7 +941,7 @@
* The second condition may happen due to extensive replacement operations;
* in such a case it is save enough to expand anyway.
*/
- if(!(konto>0  ||  finance_history_month[0][COST_ASSETS]+konto>welt->get_einstellungen()->get_starting_money())  ) {
+ if(!(konto>0  ||  finance_history_month[0][COST_ASSETS]+konto>starting_money)  ) {
return;
}

@@ -1207,7 +1207,7 @@
// despite its name: try airplane
case NR_BAUE_AIRPORT_ROUTE:
// try airline (if we are wealthy enough) ...
- if(  !air_transport  ||  finance_history_month[1][COST_CASH]<welt->get_einstellungen()->get_starting_money()  ||  !create_air_transport_vehikel( start_stadt, end_stadt )) {
+ if(  !air_transport  ||  finance_history_month[1][COST_CASH]<starting_money  ||  !create_air_transport_vehikel( start_stadt, end_stadt )) {
state = NR_BAUE_CLEAN_UP;
}
else {
Index: player/simplay.cc
===================================================================
--- player/simplay.cc (revision 2295)
+++ player/simplay.cc (working copy)
@@ -81,7 +81,8 @@
player_nr = nr;
set_player_color( nr*8, nr*8+24 );

- konto = welt->get_einstellungen()->get_starting_money();
+ konto = welt->get_einstellungen()->get_starting_money(welt->get_last_year());
+ starting_money = konto;

konto_ueberzogen = 0;
automat = false; // Start nicht als automatischer Spieler
@@ -98,7 +99,7 @@
for (int cost_type=0; cost_type<MAX_PLAYER_COST; cost_type++) {
finance_history_year[year][cost_type] = 0;
if ((cost_type == COST_CASH) || (cost_type == COST_NETWEALTH)) {
- finance_history_year[year][cost_type] = welt->get_einstellungen()->get_starting_money();
+ finance_history_year[year][cost_type] = starting_money;
}
}
}
@@ -107,7 +108,7 @@
for (int cost_type=0; cost_type<MAX_PLAYER_COST; cost_type++) {
finance_history_month[month][cost_type] = 0;
if ((cost_type == COST_CASH) || (cost_type == COST_NETWEALTH)) {
- finance_history_month[month][cost_type] = welt->get_einstellungen()->get_starting_money();
+ finance_history_month[month][cost_type] = starting_money;
}
}
}
Index: player/simplay.h
===================================================================
--- player/simplay.h (revision 2295)
+++ player/simplay.h (working copy)
@@ -104,6 +104,9 @@
*/
sint64 konto;

+ // remember the starting money
+ sint64 starting_money;
+
/**
* Zählt wie viele Monate das Konto schon ueberzogen ist
*