Merge pull request #869 from ennorehling/develop

insect warning
This commit is contained in:
Enno Rehling 2019-08-25 15:19:10 +02:00 committed by GitHub
commit ea4f402ddc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 83 additions and 51 deletions

View file

@ -259,10 +259,9 @@ static int tolua_dice_rand(lua_State * L)
static int tolua_get_season(lua_State * L) static int tolua_get_season(lua_State * L)
{ {
int turnno = (int)tolua_tonumber(L, 1, 0); int turn_no = (int)tolua_tonumber(L, 1, 0);
gamedate gd; season_t season = calendar_season(turn_no);
get_gamedate(turnno, &gd); tolua_pushstring(L, seasonnames[season]);
tolua_pushstring(L, seasonnames[gd.season]);
return 1; return 1;
} }

View file

@ -1758,12 +1758,8 @@ static void breedtrees(unit * u, int raw)
{ {
int n, i, skill, planted = 0; int n, i, skill, planted = 0;
const resource_type *rtype; const resource_type *rtype;
int current_season; season_t current_season = calendar_season(turn);
region *r = u->region; region *r = u->region;
gamedate date;
get_gamedate(turn, &date);
current_season = date.season;
/* Baeume zuechten geht nur im Fruehling */ /* Baeume zuechten geht nur im Fruehling */
if (current_season != SEASON_SPRING) { if (current_season != SEASON_SPRING) {

View file

@ -5,10 +5,11 @@
#include "economy.h" #include "economy.h"
#include "recruit.h" #include "recruit.h"
#include <util/message.h> #include <kernel/attrib.h>
#include <kernel/building.h> #include <kernel/building.h>
#include <kernel/item.h> #include <kernel/calendar.h>
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/messages.h> #include <kernel/messages.h>
#include <kernel/order.h> #include <kernel/order.h>
#include <kernel/pool.h> #include <kernel/pool.h>
@ -20,9 +21,9 @@
#include <kernel/terrainid.h> #include <kernel/terrainid.h>
#include <kernel/unit.h> #include <kernel/unit.h>
#include <kernel/attrib.h>
#include <util/language.h> #include <util/language.h>
#include <util/macros.h> #include <util/macros.h>
#include <util/message.h>
#include <CuTest.h> #include <CuTest.h>
#include <tests.h> #include <tests.h>
@ -494,6 +495,7 @@ static void test_recruit_insect(CuTest *tc) {
u = test_create_unit(f, test_create_region(0, 0, NULL)); u = test_create_unit(f, test_create_region(0, 0, NULL));
u->thisorder = create_order(K_RECRUIT, f->locale, "%d", 1); u->thisorder = create_order(K_RECRUIT, f->locale, "%d", 1);
CuAssertIntEquals(tc, SEASON_AUTUMN, calendar_season(1083));
msg = can_recruit(u, f->race, u->thisorder, 1083); /* Autumn */ msg = can_recruit(u, f->race, u->thisorder, 1083); /* Autumn */
CuAssertPtrEquals(tc, NULL, msg); CuAssertPtrEquals(tc, NULL, msg);

View file

@ -18,7 +18,7 @@ int months_per_year = 12;
const char *seasonnames[CALENDAR_SEASONS] = { "winter", "spring", "summer", "fall" }; const char *seasonnames[CALENDAR_SEASONS] = { "winter", "spring", "summer", "fall" };
char **weeknames = NULL; char **weeknames = NULL;
char **weeknames2 = NULL; char **weeknames2 = NULL;
int *month_season = NULL; season_t *month_season = NULL;
const char *calendar_month(int index) const char *calendar_month(int index)
{ {
@ -27,6 +27,13 @@ const char *calendar_month(int index)
return result; return result;
} }
season_t calendar_season(int now) {
gamedate date;
get_gamedate(now, &date);
return date.season;
}
const char *calendar_era(void) const char *calendar_era(void)
{ {
static char result[20]; static char result[20];

View file

@ -5,12 +5,12 @@
extern "C" { extern "C" {
#endif #endif
enum { typedef enum season_t {
SEASON_WINTER, SEASON_WINTER,
SEASON_SPRING, SEASON_SPRING,
SEASON_SUMMER, SEASON_SUMMER,
SEASON_AUTUMN SEASON_AUTUMN
}; } season_t;
#define CALENDAR_SEASONS 4 #define CALENDAR_SEASONS 4
extern const char *seasonnames[CALENDAR_SEASONS]; extern const char *seasonnames[CALENDAR_SEASONS];
@ -26,16 +26,18 @@ extern "C" {
typedef struct gamedate { typedef struct gamedate {
int turn; int turn;
int year; int year;
int season; season_t season;
int month; int month;
int week; int week;
} gamedate; } gamedate;
const gamedate *get_gamedate(int turn, gamedate * gd); const gamedate *get_gamedate(int turn, gamedate * gd);
void calendar_cleanup(void); season_t calendar_season(int turn);
const char *calendar_month(int index);
const char *calendar_era(void); void calendar_cleanup(void);
int first_turn(void); const char *calendar_month(int index);
const char *calendar_era(void);
int first_turn(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -65,26 +65,59 @@ static void test_calendar(CuTest * tc)
test_teardown(); test_teardown();
} }
static void setup_calendar() {
int i;
months_per_year = 4;
weeks_per_month = 2;
free(month_season);
month_season = calloc(months_per_year, sizeof(int));
for (i = 0; i != 4; ++i) {
month_season[i] = (season_t)i;
}
}
static void test_calendar_season(CuTest * tc) static void test_calendar_season(CuTest * tc)
{
test_setup();
setup_calendar();
CuAssertIntEquals(tc, SEASON_WINTER, calendar_season(0));
CuAssertIntEquals(tc, SEASON_WINTER, calendar_season(1));
CuAssertIntEquals(tc, SEASON_SPRING, calendar_season(2));
CuAssertIntEquals(tc, SEASON_SPRING, calendar_season(3));
CuAssertIntEquals(tc, SEASON_SUMMER, calendar_season(4));
CuAssertIntEquals(tc, SEASON_SUMMER, calendar_season(5));
CuAssertIntEquals(tc, SEASON_AUTUMN, calendar_season(6));
CuAssertIntEquals(tc, SEASON_AUTUMN, calendar_season(7));
CuAssertIntEquals(tc, SEASON_WINTER, calendar_season(8));
free(month_season);
month_season = NULL;
test_teardown();
}
static void test_gamedate(CuTest * tc)
{ {
gamedate gd; gamedate gd;
test_setup(); test_setup();
month_season = calloc(months_per_year, sizeof(int)); setup_calendar();
get_gamedate(0, &gd); get_gamedate(0, &gd);
CuAssertIntEquals(tc, 1, gd.year); CuAssertIntEquals(tc, 1, gd.year);
CuAssertIntEquals(tc, 0, gd.season); CuAssertIntEquals(tc, SEASON_WINTER, gd.season);
CuAssertIntEquals(tc, 0, gd.month); CuAssertIntEquals(tc, 0, gd.month);
CuAssertIntEquals(tc, 0, gd.week); CuAssertIntEquals(tc, 0, gd.week);
month_season[1] = 1;
get_gamedate(weeks_per_month + 1, &gd); get_gamedate(weeks_per_month + 1, &gd);
CuAssertIntEquals(tc, 1, gd.year); CuAssertIntEquals(tc, 1, gd.year);
CuAssertIntEquals(tc, 1, gd.season); CuAssertIntEquals(tc, SEASON_SPRING, gd.season);
CuAssertIntEquals(tc, 1, gd.month); CuAssertIntEquals(tc, 1, gd.month);
CuAssertIntEquals(tc, 1, gd.week); CuAssertIntEquals(tc, 1, gd.week);
free(month_season);
month_season = NULL;
test_teardown(); test_teardown();
} }
@ -94,5 +127,6 @@ CuSuite *get_calendar_suite(void)
SUITE_ADD_TEST(suite, test_calendar_config); SUITE_ADD_TEST(suite, test_calendar_config);
SUITE_ADD_TEST(suite, test_calendar); SUITE_ADD_TEST(suite, test_calendar);
SUITE_ADD_TEST(suite, test_calendar_season); SUITE_ADD_TEST(suite, test_calendar_season);
SUITE_ADD_TEST(suite, test_gamedate);
return suite; return suite;
} }

View file

@ -630,7 +630,6 @@ static void fix_resource_levels(region *r) {
} }
} }
} }
} }
} }

View file

@ -613,7 +613,7 @@ growing_trees_e3(region * r, const int current_season,
} }
static void static void
growing_trees(region * r, const int current_season, const int last_weeks_season) growing_trees(region * r, const season_t current_season, const season_t last_weeks_season)
{ {
int grownup_trees, i, seeds, sprout; int grownup_trees, i, seeds, sprout;
attrib *a; attrib *a;
@ -755,7 +755,7 @@ growing_trees(region * r, const int current_season, const int last_weeks_season)
} }
static void static void
growing_herbs(region * r, const int current_season, const int last_weeks_season) growing_herbs(region * r, const int current_season, const season_t last_weeks_season)
{ {
/* Jetzt die Kraeutervermehrung. Vermehrt wird logistisch: /* Jetzt die Kraeutervermehrung. Vermehrt wird logistisch:
* *
@ -849,20 +849,12 @@ void nmr_warnings(void)
void demographics(void) void demographics(void)
{ {
region *r; region *r;
static int last_weeks_season = -1;
static int current_season = -1;
int plant_rules = config_get_int("rules.grow.formula", 2); int plant_rules = config_get_int("rules.grow.formula", 2);
int horse_rules = config_get_int("rules.horses.growth", 1); int horse_rules = config_get_int("rules.horses.growth", 1);
int peasant_rules = config_get_int("rules.peasants.growth", 1); int peasant_rules = config_get_int("rules.peasants.growth", 1);
const struct building_type *bt_harbour = bt_find("harbour"); const struct building_type *bt_harbour = bt_find("harbour");
season_t current_season = calendar_season(turn);
if (current_season < 0) { season_t last_weeks_season = calendar_season(turn - 1);
gamedate date;
get_gamedate(turn, &date);
current_season = date.season;
get_gamedate(turn - 1, &date);
last_weeks_season = date.season;
}
for (r = regions; r; r = r->next) { for (r = regions; r; r = r->next) {
++r->age; /* also oceans. no idea why we didn't always do that */ ++r->age; /* also oceans. no idea why we didn't always do that */

View file

@ -351,9 +351,14 @@ message *can_recruit(unit *u, const race *rc, order *ord, int now)
} }
if (rc == get_race(RC_INSECT)) { if (rc == get_race(RC_INSECT)) {
gamedate date;
get_gamedate(now, &date); /* in Gletschern, Eisbergen gar nicht rekrutieren */
if (date.season == SEASON_WINTER && r->terrain != newterrain(T_DESERT)) { if (r_insectstalled(r)) {
return msg_error(u, ord, 97);
}
/* in Wüsten ganzjährig rekrutieren, sonst im Winter nur mit Trank */
if (r->terrain != newterrain(T_DESERT) && calendar_season(now) == SEASON_WINTER) {
bool usepotion = false; bool usepotion = false;
unit *u2; unit *u2;
@ -367,10 +372,6 @@ message *can_recruit(unit *u, const race *rc, order *ord, int now)
return msg_error(u, ord, 98); return msg_error(u, ord, 98);
} }
} }
/* in Gletschern, Eisbergen gar nicht rekrutieren */
if (r_insectstalled(r)) {
return msg_error(u, ord, 97);
}
} }
if (is_cursed(r->attribs, &ct_riotzone)) { if (is_cursed(r->attribs, &ct_riotzone)) {
/* Die Region befindet sich in Aufruhr */ /* Die Region befindet sich in Aufruhr */

View file

@ -1466,14 +1466,14 @@ void report_warnings(faction *f, int now)
} }
if (f->race == get_race(RC_INSECT)) { if (f->race == get_race(RC_INSECT)) {
gamedate date; season_t season = calendar_season(now + 1);
get_gamedate(now + 1, &date);
if (date.season == SEASON_WINTER) { if (season == SEASON_WINTER) {
ADDMSG(&f->msgs, msg_message("nr_insectwinter", "")); ADDMSG(&f->msgs, msg_message("nr_insectwinter", ""));
} }
else if (date.season == SEASON_AUTUMN) { else if (season == SEASON_AUTUMN) {
if (get_gamedate(now + 2 + 2, &date)->season == SEASON_WINTER) { /* warning: next turn is the last week of autumn */
if (calendar_season(now + 2) == SEASON_WINTER) {
ADDMSG(&f->msgs, msg_message("nr_insectfall", "")); ADDMSG(&f->msgs, msg_message("nr_insectfall", ""));
} }
} }

View file

@ -277,7 +277,7 @@ static void test_reset(void) {
void test_create_calendar(void) { void test_create_calendar(void) {
config_set_int("game.start", 184); config_set_int("game.start", 184);
months_per_year = 9; months_per_year = 9;
month_season = malloc(sizeof(int) * months_per_year); month_season = malloc(sizeof(season_t) * months_per_year);
if (!month_season) abort(); if (!month_season) abort();
month_season[0] = SEASON_SUMMER; month_season[0] = SEASON_SUMMER;
month_season[1] = SEASON_AUTUMN; month_season[1] = SEASON_AUTUMN;