2018-07-05 20:06:32 +02:00
|
|
|
|
#include <platform.h>
|
|
|
|
|
|
2019-06-24 21:41:04 +02:00
|
|
|
|
#include "kernel/config.h"
|
2018-07-05 20:06:32 +02:00
|
|
|
|
#include "kernel/faction.h"
|
2018-07-14 15:56:44 +02:00
|
|
|
|
#include "kernel/messages.h"
|
2018-07-05 20:06:32 +02:00
|
|
|
|
#include "kernel/order.h"
|
|
|
|
|
#include "kernel/region.h"
|
|
|
|
|
#include "kernel/unit.h"
|
2020-09-11 21:28:29 +02:00
|
|
|
|
#include "kernel/pool.h"
|
|
|
|
|
#include "kernel/item.h"
|
2018-07-05 20:06:32 +02:00
|
|
|
|
|
2018-09-29 18:13:32 +02:00
|
|
|
|
#include "util/keyword.h"
|
2018-07-05 20:06:32 +02:00
|
|
|
|
#include "util/log.h"
|
2021-02-14 18:27:24 +01:00
|
|
|
|
#include "util/stats.h"
|
2018-07-05 20:06:32 +02:00
|
|
|
|
|
|
|
|
|
#include "automate.h"
|
2018-07-14 15:56:44 +02:00
|
|
|
|
#include "laws.h"
|
2018-07-05 20:06:32 +02:00
|
|
|
|
#include "study.h"
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2018-07-11 21:03:00 +02:00
|
|
|
|
#include <assert.h>
|
2018-07-05 20:06:32 +02:00
|
|
|
|
|
2019-06-26 21:31:47 +02:00
|
|
|
|
static int cmp_scholars(const void *lhs, const void *rhs) {
|
2018-07-09 03:31:13 +02:00
|
|
|
|
const scholar *a = (const scholar *)lhs;
|
|
|
|
|
const scholar *b = (const scholar *)rhs;
|
2020-03-01 14:24:21 +01:00
|
|
|
|
int diff = b->level - a->level;
|
|
|
|
|
return (diff != 0) ? diff : b->u->number - a->u->number;
|
2018-07-05 20:06:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-26 21:31:47 +02:00
|
|
|
|
int autostudy_init(scholar scholars[], int max_scholars, unit **units, skill_t *o_skill)
|
2018-07-07 17:29:22 +02:00
|
|
|
|
{
|
2018-11-04 09:08:50 +01:00
|
|
|
|
unit *unext = NULL, *u = *units;
|
|
|
|
|
faction *f = u->faction;
|
2018-07-09 03:31:13 +02:00
|
|
|
|
int nscholars = 0;
|
2019-06-26 21:31:47 +02:00
|
|
|
|
skill_t skill = NOSKILL;
|
2018-11-04 09:08:50 +01:00
|
|
|
|
while (u) {
|
2019-06-26 21:31:47 +02:00
|
|
|
|
if (!fval(u, UFL_MARK)) {
|
|
|
|
|
keyword_t kwd = init_order(u->thisorder, u->faction->locale);
|
|
|
|
|
if (kwd == K_AUTOSTUDY) {
|
2018-11-04 09:08:50 +01:00
|
|
|
|
if (f == u->faction) {
|
2019-06-26 21:31:47 +02:00
|
|
|
|
unext = u->next;
|
2021-05-21 04:14:39 +02:00
|
|
|
|
if (long_order_allowed(u, false)) {
|
2019-06-26 21:31:47 +02:00
|
|
|
|
scholar * st = scholars + nscholars;
|
|
|
|
|
skill_t sk = getskill(u->faction->locale);
|
|
|
|
|
if (skill == NOSKILL && sk != NOSKILL) {
|
|
|
|
|
skill = sk;
|
|
|
|
|
if (o_skill) {
|
|
|
|
|
*o_skill = skill;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (check_student(u, u->thisorder, sk)) {
|
|
|
|
|
if (sk == skill) {
|
|
|
|
|
fset(u, UFL_MARK);
|
|
|
|
|
st->level = (short)effskill_study(u, sk);
|
|
|
|
|
st->learn = 0;
|
|
|
|
|
st->u = u;
|
|
|
|
|
if (++nscholars >= max_scholars) {
|
|
|
|
|
log_warning("you must increase MAXSCHOLARS");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
fset(u, UFL_MARK);
|
2018-11-04 09:08:50 +01:00
|
|
|
|
}
|
2018-10-14 11:48:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-05 20:06:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-04 09:08:50 +01:00
|
|
|
|
u = u->next;
|
2018-07-05 20:06:32 +02:00
|
|
|
|
}
|
2019-06-26 21:31:47 +02:00
|
|
|
|
while (unext && unext->faction != f) {
|
|
|
|
|
unext = unext->next;
|
|
|
|
|
}
|
2018-11-04 09:08:50 +01:00
|
|
|
|
*units = unext;
|
2018-07-09 03:31:13 +02:00
|
|
|
|
if (nscholars > 0) {
|
|
|
|
|
qsort(scholars, nscholars, sizeof(scholar), cmp_scholars);
|
2018-07-07 20:56:35 +02:00
|
|
|
|
}
|
2018-07-09 03:31:13 +02:00
|
|
|
|
return nscholars;
|
2018-07-07 17:29:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 21:03:00 +02:00
|
|
|
|
static void teaching(scholar *s, int n) {
|
|
|
|
|
assert(n <= s->u->number);
|
2020-02-25 20:35:55 +01:00
|
|
|
|
// doppelter Effekt mit Lehrer:
|
2020-03-01 14:24:21 +01:00
|
|
|
|
s->learn += n;
|
2018-07-16 10:53:12 +02:00
|
|
|
|
s->u->flags |= UFL_LONGACTION;
|
2018-07-11 21:03:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void learning(scholar *s, int n) {
|
|
|
|
|
assert(n <= s->u->number);
|
|
|
|
|
s->learn += n;
|
2018-07-16 10:53:12 +02:00
|
|
|
|
s->u->flags |= UFL_LONGACTION;
|
2018-07-11 21:03:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-09 03:31:13 +02:00
|
|
|
|
void autostudy_run(scholar scholars[], int nscholars)
|
|
|
|
|
{
|
2018-07-11 21:03:00 +02:00
|
|
|
|
int ti = 0;
|
|
|
|
|
while (ti != nscholars) {
|
2020-03-01 14:24:21 +01:00
|
|
|
|
int t, ts = 0, tt = 0, si = ti, mint = 0, ns = 0;
|
|
|
|
|
for (t = ti; t != nscholars; ++t) {
|
|
|
|
|
ts += scholars[t].u->number; /* count total scholars */
|
2018-07-11 21:03:00 +02:00
|
|
|
|
mint = (ts + 10) / 11; /* need a minimum of ceil(ts/11) teachers */
|
2020-02-25 20:35:55 +01:00
|
|
|
|
for (; mint > tt && si != nscholars; ++si) {
|
2018-07-11 21:03:00 +02:00
|
|
|
|
tt += scholars[si].u->number;
|
|
|
|
|
}
|
2018-07-09 03:31:13 +02:00
|
|
|
|
}
|
2020-03-01 14:24:21 +01:00
|
|
|
|
if (mint < tt) {
|
|
|
|
|
/* die Einheit si-1 hat einen Mix aus Lehrer und Sch<63>ler */
|
|
|
|
|
--si;
|
|
|
|
|
ns = tt - mint;
|
|
|
|
|
}
|
2018-07-11 21:03:00 +02:00
|
|
|
|
/* now si splits the teachers and students 1:10 */
|
|
|
|
|
/* first student must be 2 levels below first teacher: */
|
2020-03-01 14:24:21 +01:00
|
|
|
|
for (; si != nscholars; ++si) {
|
2018-11-06 19:28:14 +01:00
|
|
|
|
if (scholars[si].level + TEACHDIFFERENCE <= scholars[ti].level) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-03-01 14:24:21 +01:00
|
|
|
|
ns = 0;
|
2018-07-11 21:03:00 +02:00
|
|
|
|
}
|
2018-11-06 19:28:14 +01:00
|
|
|
|
/* now si is the first unit we can teach, if we can teach any */
|
2020-03-01 14:24:21 +01:00
|
|
|
|
if (si == nscholars) {
|
2018-11-06 19:28:14 +01:00
|
|
|
|
/* there are no students, so standard learning for everyone */
|
2020-03-01 14:24:21 +01:00
|
|
|
|
for (t = ti; t != nscholars; ++t) {
|
2018-07-11 21:03:00 +02:00
|
|
|
|
learning(scholars + t, scholars[t].u->number);
|
2018-07-09 03:31:13 +02:00
|
|
|
|
}
|
2018-07-11 21:03:00 +02:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* invariant: unit ti can still teach i students */
|
2018-12-04 21:16:47 +01:00
|
|
|
|
int i = scholars[ti].u->number * STUDENTS_PER_TEACHER;
|
2018-07-11 21:03:00 +02:00
|
|
|
|
/* invariant: unit si has n students that can still be taught */
|
2020-03-01 14:24:21 +01:00
|
|
|
|
int s, n = (ns > 0) ? ns : scholars[si].u->number;
|
|
|
|
|
for (t = ti, s = si; t != si && s != nscholars; ) {
|
2018-11-06 19:28:14 +01:00
|
|
|
|
if (i >= n) {
|
2018-07-11 21:03:00 +02:00
|
|
|
|
/* t has more than enough teaching capacity for s */
|
|
|
|
|
i -= n;
|
|
|
|
|
teaching(scholars + s, n);
|
2020-03-01 14:24:21 +01:00
|
|
|
|
learning(scholars + s, scholars[s].u->number);
|
2018-07-11 21:03:00 +02:00
|
|
|
|
/* next student, please: */
|
2020-03-01 14:24:21 +01:00
|
|
|
|
if (++s == nscholars) {
|
2020-02-25 20:35:55 +01:00
|
|
|
|
/* no more students */
|
|
|
|
|
break;
|
2018-07-11 21:03:00 +02:00
|
|
|
|
}
|
|
|
|
|
n = scholars[s].u->number;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2018-12-01 10:29:48 +01:00
|
|
|
|
/* a part of s gets credited and we need a new teacher: */
|
2018-07-11 21:03:00 +02:00
|
|
|
|
teaching(scholars + s, i);
|
2018-12-01 10:29:48 +01:00
|
|
|
|
/* we still need to teach n students in this unit: */
|
|
|
|
|
n -= i;
|
|
|
|
|
i = 0;
|
2018-07-11 21:03:00 +02:00
|
|
|
|
/* we want a new teacher for s. if any exists, it's next in the sequence. */
|
|
|
|
|
if (++t == si) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (scholars[t].level - TEACHDIFFERENCE < scholars[s].level) {
|
2018-12-01 10:29:48 +01:00
|
|
|
|
/* no remaining teacher can teach this student, so we skip ahead */
|
2018-07-11 21:03:00 +02:00
|
|
|
|
do {
|
2018-12-01 10:29:48 +01:00
|
|
|
|
/* remaining students learn without a teacher: */
|
|
|
|
|
learning(scholars + s, n);
|
2020-03-01 14:24:21 +01:00
|
|
|
|
if (++s == nscholars) {
|
2018-10-07 08:22:56 +02:00
|
|
|
|
break;
|
2018-07-11 21:03:00 +02:00
|
|
|
|
}
|
|
|
|
|
n = scholars[s].u->number;
|
|
|
|
|
} while (scholars[t].level - TEACHDIFFERENCE < scholars[s].level);
|
|
|
|
|
}
|
|
|
|
|
i = scholars[t].u->number * STUDENTS_PER_TEACHER;
|
2018-07-09 03:31:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-06 19:28:14 +01:00
|
|
|
|
if (i > 0) {
|
|
|
|
|
int remain = (STUDENTS_PER_TEACHER * scholars[t].u->number - i + STUDENTS_PER_TEACHER - 1) / STUDENTS_PER_TEACHER;
|
|
|
|
|
/* teacher has remaining time */
|
|
|
|
|
learning(scholars + t, remain);
|
|
|
|
|
}
|
|
|
|
|
++t;
|
|
|
|
|
for (; t < si; ++t) {
|
2020-02-25 20:35:55 +01:00
|
|
|
|
/* teachers that did not teach */
|
2018-11-06 19:28:14 +01:00
|
|
|
|
learning(scholars + t, scholars[t].u->number);
|
|
|
|
|
}
|
2020-03-01 14:24:21 +01:00
|
|
|
|
for (; s < nscholars; ++s) {
|
2020-02-25 20:35:55 +01:00
|
|
|
|
/* students that were not taught */
|
|
|
|
|
learning(scholars + s, scholars[s].u->number);
|
|
|
|
|
}
|
2018-07-09 03:31:13 +02:00
|
|
|
|
}
|
2020-03-01 14:24:21 +01:00
|
|
|
|
ti = nscholars;
|
2018-07-09 03:31:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void do_autostudy(region *r)
|
|
|
|
|
{
|
2019-06-24 21:41:04 +02:00
|
|
|
|
static int config;
|
|
|
|
|
static int batchsize = MAXSCHOLARS;
|
2019-06-26 21:31:47 +02:00
|
|
|
|
static int max_scholars;
|
|
|
|
|
scholar scholars[MAXSCHOLARS];
|
|
|
|
|
unit *u;
|
|
|
|
|
|
2019-06-24 21:41:04 +02:00
|
|
|
|
if (config_changed(&config)) {
|
|
|
|
|
batchsize = config_get_int("automate.batchsize", MAXSCHOLARS);
|
|
|
|
|
assert(batchsize <= MAXSCHOLARS);
|
|
|
|
|
}
|
2019-06-26 21:31:47 +02:00
|
|
|
|
for (u = r->units; u; u = u->next) {
|
2021-05-21 04:14:39 +02:00
|
|
|
|
if (is_paused(u->faction)) continue;
|
2019-06-26 21:31:47 +02:00
|
|
|
|
if (!fval(u, UFL_MARK)) {
|
|
|
|
|
unit *ulist = u;
|
|
|
|
|
int sum_scholars = 0;
|
|
|
|
|
while (ulist) {
|
|
|
|
|
skill_t skill = NOSKILL;
|
|
|
|
|
int i, nscholars = autostudy_init(scholars, batchsize, &ulist, &skill);
|
|
|
|
|
assert(ulist == NULL || ulist->faction == u->faction);
|
|
|
|
|
sum_scholars += nscholars;
|
|
|
|
|
if (sum_scholars > max_scholars) {
|
|
|
|
|
stats_count("automate.max_scholars", sum_scholars - max_scholars);
|
|
|
|
|
max_scholars = sum_scholars;
|
|
|
|
|
}
|
|
|
|
|
autostudy_run(scholars, nscholars);
|
|
|
|
|
for (i = 0; i != nscholars; ++i) {
|
|
|
|
|
int days = STUDYDAYS * scholars[i].learn;
|
2020-09-11 21:28:29 +02:00
|
|
|
|
int money = learn_skill(scholars[i].u, skill, days, 0);
|
|
|
|
|
if (money > 0) {
|
|
|
|
|
use_pooled(u, get_resourcetype(R_SILVER), GET_DEFAULT, money);
|
|
|
|
|
ADDMSG(&u->faction->msgs, msg_message("studycost",
|
2021-01-02 20:15:19 +01:00
|
|
|
|
"unit region cost skill", scholars[i].u, r, money, skill));
|
2020-09-11 21:28:29 +02:00
|
|
|
|
}
|
2019-06-26 21:31:47 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-04 09:08:50 +01:00
|
|
|
|
}
|
2019-06-26 21:31:47 +02:00
|
|
|
|
freset(u, UFL_MARK);
|
2018-07-05 20:06:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|