server/src/automate.c

118 lines
3.2 KiB
C
Raw Normal View History

2018-07-05 20:06:32 +02:00
#include <platform.h>
#include "kernel/faction.h"
#include "kernel/order.h"
#include "kernel/region.h"
#include "kernel/unit.h"
#include "util/log.h"
#include "automate.h"
#include "keyword.h"
#include "study.h"
#include <stdlib.h>
2018-07-09 03:31:13 +02:00
int cmp_scholars(const void *lhs, const void *rhs)
{
const scholar *a = (const scholar *)lhs;
const scholar *b = (const scholar *)rhs;
2018-07-05 20:06:32 +02:00
if (a->sk == b->sk) {
/* sort by level, descending: */
return b->level - a->level;
}
/* order by skill */
return (int)a->sk - (int)b->sk;
}
2018-07-09 03:31:13 +02:00
int autostudy_init(scholar scholars[], int max_scholars, region *r)
{
2018-07-05 20:06:32 +02:00
unit *u;
2018-07-09 03:31:13 +02:00
int nscholars = 0;
2018-07-05 20:06:32 +02:00
for (u = r->units; u; u = u->next) {
keyword_t kwd = getkeyword(u->thisorder);
if (kwd == K_AUTOSTUDY) {
2018-07-09 03:31:13 +02:00
scholar * st = scholars + nscholars;
if (++nscholars == max_scholars) {
log_fatal("you must increase MAXSCHOLARS");
2018-07-05 20:06:32 +02:00
}
st->u = u;
init_order(u->thisorder, u->faction->locale);
st->sk = getskill(u->faction->locale);
st->level = effskill_study(u, st->sk);
2018-07-07 20:56:35 +02:00
st->learn = 0;
2018-07-05 20:06:32 +02:00
}
}
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-09 03:31:13 +02:00
void autostudy_run(scholar scholars[], int nscholars)
{
int i, t, s, ti = 0, si = 0, ts = 0, tt = 0;
skill_t sk = scholars[0].sk;
for (i = ti; i != nscholars && scholars[i].sk == sk; ++i) {
int mint;
ts += scholars[i].u->number; /* count total scholars */
mint = (ts + 10) / 11; /* need a minimum of ceil(ts/11) teachers */
while (mint>tt) {
tt += scholars[si++].u->number;
}
}
/* now si splits the teachers and students 1:10 */
/* first student must be 2 levels below first teacher: */
while (scholars[ti].level - TEACHDIFFERENCE > scholars[si].level) {
tt += scholars[si++].u->number;
}
/* invariant: unit ti can still teach i students */
i = scholars[ti].u->number * 10;
for (t = ti, s = si; t != si && s != nscholars; ++t) {
/* TODO: is there no constant for students per teacher? */
while (s != nscholars) {
int n = scholars[s].u->number;
scholars[s].learn += n;
if (i >= n) {
i -= n;
scholars[s].learn += n;
/* next student */
}
else {
scholars[s].learn += i;
/* go to next suitable teacher */
do {
++t;
}
while (scholars[t].level - TEACHDIFFERENCE < scholars[s].level);
}
}
}
}
#define MAXSCHOLARS 128
2018-07-09 03:31:13 +02:00
void do_autostudy(region *r)
{
scholar scholars[MAXSCHOLARS];
int nscholars = autostudy_init(scholars, MAXSCHOLARS, r);
2018-07-09 03:31:13 +02:00
if (nscholars > 0) {
int i;
2018-07-05 20:06:32 +02:00
skill_t sk = NOSKILL;
2018-07-09 03:31:13 +02:00
for (i = 0; i != nscholars; ++i) {
if (scholars[i].u) {
2018-07-05 20:06:32 +02:00
if (sk == NOSKILL) {
2018-07-09 03:31:13 +02:00
sk = scholars[i].sk;
2018-07-05 20:06:32 +02:00
}
2018-07-09 03:31:13 +02:00
else if (sk != scholars[i].sk) {
2018-07-05 20:06:32 +02:00
continue;
}
2018-07-09 03:31:13 +02:00
scholars[i].u = NULL;
2018-07-05 20:06:32 +02:00
}
}
}
}