server/src/study.c

811 lines
23 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#define TEACH_ALL 1
#define TEACH_FRIENDS
#include <platform.h>
#include <kernel/config.h>
#include "study.h"
#include <kernel/alchemy.h>
#include <kernel/building.h>
2012-06-03 22:39:42 +02:00
#include <kernel/curse.h>
2010-08-08 10:06:34 +02:00
#include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/magic.h>
#include <kernel/message.h>
#include <kernel/move.h>
#include <kernel/order.h>
#include <kernel/plane.h>
#include <kernel/pool.h>
#include <kernel/race.h>
#include <kernel/region.h>
#include <kernel/skill.h>
#include <kernel/terrain.h>
#include <kernel/unit.h>
/* util includes */
#include <util/attrib.h>
#include <util/base36.h>
#include <util/language.h>
#include <util/parser.h>
#include <util/rand.h>
#include <util/umlaut.h>
/* libc includes */
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define TEACHNUMBER 10
2011-03-07 08:02:35 +01:00
static skill_t getskill(const struct locale *lang)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
return findskill(getstrtoken(), lang);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
magic_t getmagicskill(const struct locale * lang)
2010-08-08 10:06:34 +02:00
{
void **tokens = get_translations(lang, UT_MAGIC);
2010-08-08 10:06:34 +02:00
variant token;
2011-03-07 08:02:35 +01:00
const char *s = getstrtoken();
2010-08-08 10:06:34 +02:00
if (tokens && s && s[0]) {
if (findtoken(*tokens, s, &token) == E_TOK_SUCCESS) {
2011-03-07 08:02:35 +01:00
return (magic_t) token.i;
2010-08-08 10:06:34 +02:00
} else {
char buffer[3];
buffer[0] = s[0];
buffer[1] = s[1];
buffer[2] = '\0';
if (findtoken(*tokens, buffer, &token) == E_TOK_SUCCESS) {
2011-03-07 08:02:35 +01:00
return (magic_t) token.i;
2010-08-08 10:06:34 +02:00
}
}
}
return M_NONE;
}
/* ------------------------------------------------------------- */
/* familiars and toads are not migrants */
bool is_migrant(unit * u)
2010-08-08 10:06:34 +02:00
{
if (u_race(u) == u->faction->race)
2011-03-07 08:02:35 +01:00
return false;
2010-08-08 10:06:34 +02:00
if (fval(u_race(u), RCF_UNDEAD | RCF_ILLUSIONARY))
2011-03-07 08:02:35 +01:00
return false;
if (is_familiar(u))
return false;
if (u_race(u) == new_race[RC_TOAD])
2011-03-07 08:02:35 +01:00
return false;
2010-08-08 10:06:34 +02:00
return true;
}
/* ------------------------------------------------------------- */
bool magic_lowskill(unit * u)
2010-08-08 10:06:34 +02:00
{
return (u_race(u) == new_race[RC_TOAD]) ? true : false;
2010-08-08 10:06:34 +02:00
}
/* ------------------------------------------------------------- */
2011-03-07 08:02:35 +01:00
int study_cost(unit * u, skill_t sk)
2010-08-08 10:06:34 +02:00
{
static int cost[MAXSKILLS];
int stufe, k = 50;
2011-03-07 08:02:35 +01:00
if (cost[sk] == 0) {
2010-08-08 10:06:34 +02:00
char buffer[256];
sprintf(buffer, "skills.cost.%s", skillnames[sk]);
cost[sk] = get_param_int(global.parameters, buffer, -1);
}
2011-03-08 08:44:20 +01:00
if (cost[sk] >= 0) {
2011-03-07 08:02:35 +01:00
return cost[sk];
2011-03-08 08:44:20 +01:00
}
2010-08-08 10:06:34 +02:00
switch (sk) {
2011-03-08 08:44:20 +01:00
case SK_SPY:
return 100;
case SK_TACTICS:
case SK_HERBALISM:
case SK_ALCHEMY:
return 200;
case SK_MAGIC: /* Die Magiekosten betragen 50+Summe(50*Stufe) */
/* 'Stufe' ist dabei die naechste zu erreichende Stufe */
2011-03-08 08:44:20 +01:00
stufe = 1 + get_level(u, SK_MAGIC);
return k * (1 + ((stufe + 1) * stufe / 2));
default:
return 0;
2010-08-08 10:06:34 +02:00
}
}
/* ------------------------------------------------------------- */
2011-03-07 08:02:35 +01:00
static void init_learning(struct attrib *a)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
a->data.v = calloc(sizeof(teaching_info), 1);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static void done_learning(struct attrib *a)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
free(a->data.v);
2010-08-08 10:06:34 +02:00
}
const attrib_type at_learning = {
2011-03-07 08:02:35 +01:00
"learning",
init_learning, done_learning, NULL, NULL, NULL,
ATF_UNIQUE
2010-08-08 10:06:34 +02:00
};
2011-03-07 08:02:35 +01:00
static int study_days(unit * student, skill_t sk)
2010-08-08 10:06:34 +02:00
{
int speed = 30;
if (u_race(student)->study_speed) {
speed += u_race(student)->study_speed[sk];
2011-03-07 08:02:35 +01:00
if (speed < 30) {
skill *sv = get_skill(student, sk);
if (sv == 0) {
2010-08-08 10:06:34 +02:00
speed = 30;
}
}
}
return student->number * speed;
}
static int
2011-03-07 08:02:35 +01:00
teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk,
bool report, int *academy)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
teaching_info *teach = NULL;
attrib *a;
2010-08-08 10:06:34 +02:00
int n;
/* learning sind die Tage, die sie schon durch andere Lehrer zugute
* geschrieben bekommen haben. Total darf dies nicht ueber 30 Tage pro Mann
2011-03-07 08:02:35 +01:00
* steigen.
*
* n ist die Anzahl zusaetzlich gelernter Tage. n darf max. die Differenz
2011-03-07 08:02:35 +01:00
* von schon gelernten Tagen zum MAX(30 Tage pro Mann) betragen. */
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
if (magic_lowskill(student)) {
2010-08-08 10:06:34 +02:00
cmistake(teacher, teacher->thisorder, 292, MSG_EVENT);
return 0;
}
n = 30 * student->number;
2010-08-08 10:06:34 +02:00
a = a_find(student->attribs, &at_learning);
2011-03-07 08:02:35 +01:00
if (a != NULL) {
teach = (teaching_info *) a->data.v;
2010-08-08 10:06:34 +02:00
n -= teach->value;
}
n = MIN(n, nteaching);
if (n != 0) {
2011-03-07 08:02:35 +01:00
struct building *b = inside_building(teacher);
const struct building_type *btype = b ? b->type : NULL;
2010-08-08 10:06:34 +02:00
int index = 0;
2011-03-07 08:02:35 +01:00
if (teach == NULL) {
2010-08-08 10:06:34 +02:00
a = a_add(&student->attribs, a_new(&at_learning));
2011-03-07 08:02:35 +01:00
teach = (teaching_info *) a->data.v;
2010-08-08 10:06:34 +02:00
} else {
2011-03-07 08:02:35 +01:00
while (teach->teachers[index] && index != MAXTEACHERS)
++index;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
if (index < MAXTEACHERS)
teach->teachers[index++] = teacher;
if (index < MAXTEACHERS)
teach->teachers[index] = NULL;
2010-08-08 10:06:34 +02:00
teach->value += n;
/* Solange Akademien groessenbeschraenkt sind, sollte Lehrer und
* Student auch in unterschiedlichen Gebaeuden stehen duerfen */
2010-08-08 10:06:34 +02:00
if (btype == bt_find("academy")
2011-03-07 08:02:35 +01:00
&& student->building && student->building->type == bt_find("academy")) {
2010-08-08 10:06:34 +02:00
int j = study_cost(student, sk);
j = MAX(50, j * 2);
/* kann Einheit das zahlen? */
if (get_pooled(student, oldresourcetype[R_SILVER], GET_DEFAULT, j) >= j) {
/* Jeder Schueler zusaetzlich +10 Tage wenn in Uni. */
teach->value += (n / 30) * 10; /* learning erhoehen */
/* Lehrer zusaetzlich +1 Tag pro Schueler. */
2011-03-07 08:02:35 +01:00
if (academy)
*academy += n;
} /* sonst nehmen sie nicht am Unterricht teil */
2010-08-08 10:06:34 +02:00
}
/* Teaching ist die Anzahl Leute, denen man noch was beibringen kann. Da
* hier nicht n verwendet wird, werden die Leute gezaehlt und nicht die
2011-03-07 08:02:35 +01:00
* effektiv gelernten Tage. -> FALSCH ? (ENNO)
*
* Eine Einheit A von 11 Mann mit Talent 0 profitiert vom ersten Lehrer B
* also 10x30=300 tage, und der zweite Lehrer C lehrt fuer nur noch 1x30=30
* Tage (damit das Maximum von 11x30=330 nicht ueberschritten wird).
2011-03-07 08:02:35 +01:00
*
* Damit es aber in der Ausfuehrung nicht auf die Reihenfolge drauf ankommt,
2011-03-07 08:02:35 +01:00
* darf der zweite Lehrer C keine weiteren Einheiten D mehr lehren. Also
* wird student 30 Tage gutgeschrieben, aber teaching sinkt auf 0 (300-11x30 <=
* 0).
*
* Sonst traete dies auf:
2011-03-07 08:02:35 +01:00
*
* A: lernt B: lehrt A C: lehrt A D D: lernt
*
* Wenn B vor C dran ist, lehrt C nur 30 Tage an A (wie oben) und
* 270 Tage an D.
*
* Ist C aber vor B dran, lehrt C 300 tage an A, und 0 tage an D,
* und B lehrt auch 0 tage an A.
*
* Deswegen darf C D nie lehren duerfen.
2011-03-07 08:02:35 +01:00
*
* -> Das ist wirr. wer hat das entworfen?
* Besser waere, man macht erst vorab alle zuordnungen, und dann
* die Talentaenderung (enno).
2011-03-07 08:02:35 +01:00
*/
2010-08-08 10:06:34 +02:00
nteaching = MAX(0, nteaching - student->number * 30);
}
return n;
}
2011-03-07 08:02:35 +01:00
int teach_cmd(unit * u, struct order *ord)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
static const curse_type *gbdream_ct = NULL;
plane *pl;
region *r = u->region;
int teaching, i, j, count, academy = 0;
2010-08-08 10:06:34 +02:00
skill_t sk = NOSKILL;
2011-03-07 08:02:35 +01:00
if (gbdream_ct == 0)
gbdream_ct = ct_find("gbdream");
2010-08-08 10:06:34 +02:00
if (gbdream_ct) {
if (get_curse(u->region->attribs, gbdream_ct)) {
2011-03-07 08:02:35 +01:00
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "gbdream_noteach", ""));
2010-08-08 10:06:34 +02:00
return 0;
}
}
if ((u_race(u)->flags & RCF_NOTEACH) || fval(u, UFL_WERE)) {
2010-08-08 10:06:34 +02:00
cmistake(u, ord, 274, MSG_EVENT);
return 0;
}
pl = rplane(r);
if (pl && fval(pl, PFL_NOTEACH)) {
cmistake(u, ord, 273, MSG_EVENT);
return 0;
}
teaching = u->number * 30 * TEACHNUMBER;
2011-03-07 08:02:35 +01:00
if ((i = get_effect(u, oldpotiontype[P_FOOL])) > 0) { /* Trank "Dumpfbackenbrot" */
2010-08-08 10:06:34 +02:00
i = MIN(i, u->number * TEACHNUMBER);
/* Trank wirkt pro Schueler, nicht pro Lehrer */
2010-08-08 10:06:34 +02:00
teaching -= i * 30;
change_effect(u, oldpotiontype[P_FOOL], -i);
j = teaching / 30;
2011-03-07 08:02:35 +01:00
ADDMSG(&u->faction->msgs, msg_message("teachdumb", "teacher amount", u, j));
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
if (teaching == 0)
return 0;
2010-08-08 10:06:34 +02:00
count = 0;
init_tokens(ord);
skip_token();
#if TEACH_ALL
2011-03-07 08:02:35 +01:00
if (getparam(u->faction->locale) == P_ANY) {
unit *student = r->units;
2010-08-08 10:06:34 +02:00
skill_t teachskill[MAXSKILLS];
int i = 0;
do {
sk = getskill(u->faction->locale);
2011-03-07 08:02:35 +01:00
teachskill[i++] = sk;
} while (sk != NOSKILL);
2010-08-08 10:06:34 +02:00
while (teaching && student) {
if (student->faction == u->faction) {
#ifdef NEW_DAEMONHUNGER_RULE
2011-03-07 08:02:35 +01:00
if (LongHunger(student))
continue;
2010-08-08 10:06:34 +02:00
#else
2011-03-07 08:02:35 +01:00
if (fval(student, UFL_HUNGER))
continue;
2010-08-08 10:06:34 +02:00
#endif
if (get_keyword(student->thisorder) == K_STUDY) {
/* Input ist nun von student->thisorder !! */
init_tokens(student->thisorder);
skip_token();
sk = getskill(student->faction->locale);
2011-03-07 08:02:35 +01:00
if (sk != NOSKILL && teachskill[0] != NOSKILL) {
for (i = 0; teachskill[i] != NOSKILL; ++i)
if (sk == teachskill[i])
break;
2010-08-08 10:06:34 +02:00
sk = teachskill[i];
}
2011-03-07 08:02:35 +01:00
if (sk != NOSKILL
&& eff_skill_study(u, sk,
r) - TEACHDIFFERENCE > eff_skill_study(student, sk, r)) {
2010-08-08 10:06:34 +02:00
teaching -= teach_unit(u, student, teaching, sk, true, &academy);
}
}
}
student = student->next;
}
#ifdef TEACH_FRIENDS
while (teaching && student) {
2011-03-07 08:02:35 +01:00
if (student->faction != u->faction
&& alliedunit(u, student->faction, HELP_GUARD)) {
2010-08-08 10:06:34 +02:00
#ifdef NEW_DAEMONHUNGER_RULE
2011-03-07 08:02:35 +01:00
if (LongHunger(student))
continue;
2010-08-08 10:06:34 +02:00
#else
2011-03-07 08:02:35 +01:00
if (fval(student, UFL_HUNGER))
continue;
2010-08-08 10:06:34 +02:00
#endif
if (get_keyword(student->thisorder) == K_STUDY) {
/* Input ist nun von student->thisorder !! */
init_tokens(student->thisorder);
skip_token();
sk = getskill(student->faction->locale);
2011-03-07 08:02:35 +01:00
if (sk != NOSKILL
&& eff_skill_study(u, sk, r) - TEACHDIFFERENCE >= eff_skill(student,
sk, r)) {
2010-08-08 10:06:34 +02:00
teaching -= teach_unit(u, student, teaching, sk, true, &academy);
}
}
}
student = student->next;
}
#endif
2011-03-07 08:02:35 +01:00
} else
2010-08-08 10:06:34 +02:00
#endif
{
char zOrder[4096];
2011-03-07 08:02:35 +01:00
order *new_order;
2010-08-08 10:06:34 +02:00
zOrder[0] = '\0';
init_tokens(ord);
skip_token();
while (!parser_end()) {
2011-03-07 08:02:35 +01:00
unit *u2 = getunit(r, u->faction);
bool feedback;
2010-08-08 10:06:34 +02:00
++count;
/* Falls die Unit nicht gefunden wird, Fehler melden */
if (!u2) {
char tbuf[20];
2011-03-07 08:02:35 +01:00
const char *uid;
const char *token;
2010-08-08 10:06:34 +02:00
/* Finde den string, der den Fehler verursacht hat */
parser_pushstate();
init_tokens(ord);
skip_token();
2011-03-07 08:02:35 +01:00
for (j = 0; j != count - 1; ++j) {
2010-08-08 10:06:34 +02:00
/* skip over the first 'count' units */
getunit(r, u->faction);
}
token = getstrtoken();
/* Beginne die Fehlermeldung */
if (isparam(token, u->faction->locale, P_TEMP)) {
2010-08-08 10:06:34 +02:00
token = getstrtoken();
sprintf(tbuf, "%s %s", LOC(u->faction->locale,
2011-03-07 08:02:35 +01:00
parameters[P_TEMP]), token);
2010-08-08 10:06:34 +02:00
uid = tbuf;
} else {
uid = token;
2010-08-08 10:06:34 +02:00
}
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "unitnotfound_id",
2011-03-07 08:02:35 +01:00
"id", uid));
2010-08-08 10:06:34 +02:00
parser_popstate();
continue;
}
2011-03-07 08:02:35 +01:00
feedback = u->faction == u2->faction
|| alliedunit(u2, u->faction, HELP_GUARD);
2010-08-08 10:06:34 +02:00
/* Neuen Befehl zusammenbauen. TEMP-Einheiten werden automatisch in
* ihre neuen Nummern uebersetzt. */
2011-03-07 08:02:35 +01:00
if (zOrder[0])
strcat(zOrder, " ");
2010-08-08 10:06:34 +02:00
strcat(zOrder, unitid(u2));
if (get_keyword(u2->thisorder) != K_STUDY) {
ADDMSG(&u->faction->msgs,
msg_feedback(u, ord, "teach_nolearn", "student", u2));
continue;
}
/* Input ist nun von u2->thisorder !! */
parser_pushstate();
init_tokens(u2->thisorder);
skip_token();
sk = getskill(u2->faction->locale);
parser_popstate();
if (sk == NOSKILL) {
ADDMSG(&u->faction->msgs,
msg_feedback(u, ord, "teach_nolearn", "student", u2));
continue;
}
/* u is teacher, u2 is student */
2011-03-07 08:02:35 +01:00
if (eff_skill_study(u2, sk, r) > eff_skill_study(u, sk,
r) - TEACHDIFFERENCE) {
2010-08-08 10:06:34 +02:00
if (feedback) {
2011-03-07 08:02:35 +01:00
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "teach_asgood",
"student", u2));
2010-08-08 10:06:34 +02:00
}
continue;
}
if (sk == SK_MAGIC) {
/* ist der Magier schon spezialisiert, so versteht er nur noch
2011-03-07 08:02:35 +01:00
* Lehrer seines Gebietes */
sc_mage *mage1 = get_mage(u);
sc_mage *mage2 = get_mage(u2);
if (!mage2 || !mage1 || (mage2->magietyp != M_GRAY
&& mage1->magietyp != mage2->magietyp)) {
2010-08-08 10:06:34 +02:00
if (feedback) {
2011-03-07 08:02:35 +01:00
ADDMSG(&u->faction->msgs, msg_feedback(u, ord,
"error_different_magic", "target", u2));
2010-08-08 10:06:34 +02:00
}
continue;
}
}
teaching -= teach_unit(u, u2, teaching, sk, false, &academy);
}
new_order = create_order(K_TEACH, u->faction->locale, "%s", zOrder);
replace_order(&u->orders, ord, new_order);
2011-03-07 08:02:35 +01:00
free_order(new_order); /* parse_order & set_order have each increased the refcount */
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
if (academy && sk != NOSKILL) {
academy = academy / 30; /* anzahl gelehrter wochen, max. 10 */
learn_skill(u, sk, academy / 30.0 / TEACHNUMBER);
2010-08-08 10:06:34 +02:00
}
return 0;
}
2011-03-07 08:02:35 +01:00
static double study_speedup(unit * u)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
#define MINTURN 5 /* 5 */
#define OFSTURN 2 /* 2 */
if (turn > MINTURN) {
2010-08-08 10:06:34 +02:00
static int speed_rule = -1;
2011-03-07 08:02:35 +01:00
if (speed_rule < 0) {
2010-08-08 10:06:34 +02:00
speed_rule = get_param_int(global.parameters, "study.speedup", 0);
}
2011-03-07 08:02:35 +01:00
if (speed_rule == 1) {
2010-08-08 10:06:34 +02:00
double learn_age = OFSTURN;
int i;
2011-03-07 08:02:35 +01:00
for (i = 0; i != u->skill_size; ++i) {
skill *sv = u->skills + i;
double learn_time = sv->level * (sv->level + 1) / 2.0;
2010-08-08 10:06:34 +02:00
learn_age += learn_time;
}
if (learn_age < turn) {
2011-03-07 08:02:35 +01:00
return 2.0 - learn_age / turn;
2010-08-08 10:06:34 +02:00
}
}
}
return 1.0;
}
2011-03-07 08:02:35 +01:00
int learn_cmd(unit * u, order * ord)
2010-08-08 10:06:34 +02:00
{
region *r = u->region;
int p;
magic_t mtyp;
int l;
int studycost, days;
double multi = 1.0;
2011-03-07 08:02:35 +01:00
attrib *a = NULL;
teaching_info *teach = NULL;
2010-08-08 10:06:34 +02:00
int money = 0;
skill_t sk;
int maxalchemy = 0;
static int learn_newskills = -1;
2011-03-07 08:02:35 +01:00
if (learn_newskills < 0) {
const char *str = get_param(global.parameters, "study.newskills");
if (str && strcmp(str, "false") == 0)
learn_newskills = 0;
else
learn_newskills = 1;
2010-08-08 10:06:34 +02:00
}
if ((u_race(u)->flags & RCF_NOLEARN) || fval(u, UFL_WERE)) {
2011-03-07 08:02:35 +01:00
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_race_nolearn", "race",
u_race(u)));
2010-08-08 10:06:34 +02:00
return 0;
}
init_tokens(ord);
skip_token();
sk = getskill(u->faction->locale);
if (sk < 0) {
cmistake(u, ord, 77, MSG_EVENT);
return 0;
}
if (SkillCap(sk) && SkillCap(sk) <= effskill(u, sk)) {
2011-03-07 08:02:35 +01:00
cmistake(u, ord, 771, MSG_EVENT);
2010-08-08 10:06:34 +02:00
return 0;
}
/* Hack: Talente mit Malus -99 koennen nicht gelernt werden */
if (u_race(u)->bonus[sk] == -99) {
2010-08-08 10:06:34 +02:00
cmistake(u, ord, 771, MSG_EVENT);
return 0;
}
2011-03-07 08:02:35 +01:00
if (learn_newskills == 0) {
skill *sv = get_skill(u, sk);
if (sv == NULL) {
2010-08-08 10:06:34 +02:00
/* we can only learn skills we already have */
cmistake(u, ord, 771, MSG_EVENT);
return 0;
}
}
/* snotlings koennen Talente nur bis T8 lernen */
if (u_race(u) == new_race[RC_SNOTLING]) {
2011-03-07 08:02:35 +01:00
if (get_level(u, sk) >= 8) {
2010-08-08 10:06:34 +02:00
cmistake(u, ord, 308, MSG_EVENT);
return 0;
}
}
p = studycost = study_cost(u, sk);
a = a_find(u->attribs, &at_learning);
2011-03-07 08:02:35 +01:00
if (a != NULL) {
teach = (teaching_info *) a->data.v;
2010-08-08 10:06:34 +02:00
}
/* keine kostenpflichtigen Talente fuer Migranten. Vertraute sind
2011-03-07 08:02:35 +01:00
* keine Migranten, wird in is_migrant abgefangen. Vorsicht,
* studycost darf hier noch nicht durch Akademie erhoeht sein */
2010-08-08 10:06:34 +02:00
if (studycost > 0 && !ExpensiveMigrants() && is_migrant(u)) {
2011-03-07 08:02:35 +01:00
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_migrants_nolearn",
""));
2010-08-08 10:06:34 +02:00
return 0;
}
/* Akademie: */
{
2011-03-07 08:02:35 +01:00
struct building *b = inside_building(u);
const struct building_type *btype = b ? b->type : NULL;
2010-08-08 10:06:34 +02:00
if (btype && btype == bt_find("academy")) {
2010-08-08 10:06:34 +02:00
studycost = MAX(50, studycost * 2);
}
}
if (sk == SK_MAGIC) {
2011-03-07 08:02:35 +01:00
if (u->number > 1) {
2010-08-08 10:06:34 +02:00
cmistake(u, ord, 106, MSG_MAGIC);
return 0;
}
2011-03-07 08:02:35 +01:00
if (is_familiar(u)) {
/* Vertraute zaehlen nicht zu den Magiern einer Partei,
* koennen aber nur Graue Magie lernen */
2010-08-08 10:06:34 +02:00
mtyp = M_GRAY;
2011-03-07 08:02:35 +01:00
if (!is_mage(u))
create_mage(u, mtyp);
2010-08-08 10:06:34 +02:00
} else if (!has_skill(u, SK_MAGIC)) {
int mmax = skill_limit(u->faction, SK_MAGIC);
/* Die Einheit ist noch kein Magier */
2011-03-07 08:02:35 +01:00
if (count_skill(u->faction, SK_MAGIC) + u->number > mmax) {
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_max_magicians",
"amount", mmax));
2010-08-08 10:06:34 +02:00
return 0;
}
mtyp = getmagicskill(u->faction->locale);
if (mtyp == M_NONE || mtyp == M_GRAY) {
/* wurde kein Magiegebiet angegeben, wird davon
* ausgegangen, dass das normal gelernt werden soll */
2011-03-07 08:02:35 +01:00
if (u->faction->magiegebiet != 0) {
2010-08-08 10:06:34 +02:00
mtyp = u->faction->magiegebiet;
} else {
/* Es wurde kein Magiegebiet angegeben und die Partei
* hat noch keins gewaehlt. */
2010-08-08 10:06:34 +02:00
mtyp = getmagicskill(u->faction->locale);
2011-03-07 08:02:35 +01:00
if (mtyp == M_NONE) {
2010-08-08 10:06:34 +02:00
cmistake(u, ord, 178, MSG_MAGIC);
return 0;
}
}
}
2011-03-07 08:02:35 +01:00
if (mtyp != u->faction->magiegebiet) {
2010-08-08 10:06:34 +02:00
/* Es wurde versucht, ein anderes Magiegebiet zu lernen
2011-03-07 08:02:35 +01:00
* als das der Partei */
if (u->faction->magiegebiet != 0) {
2010-08-08 10:06:34 +02:00
cmistake(u, ord, 179, MSG_MAGIC);
return 0;
} else {
/* Lernt zum ersten mal Magie und legt damit das
2011-03-07 08:02:35 +01:00
* Magiegebiet der Partei fest */
2010-08-08 10:06:34 +02:00
u->faction->magiegebiet = mtyp;
}
}
2011-03-07 08:02:35 +01:00
if (!is_mage(u))
create_mage(u, mtyp);
2010-08-08 10:06:34 +02:00
} else {
/* ist schon ein Magier und kein Vertrauter */
if (u->faction->magiegebiet == 0) {
/* die Partei hat noch kein Magiegebiet gewaehlt. */
2010-08-08 10:06:34 +02:00
mtyp = getmagicskill(u->faction->locale);
2011-03-07 08:02:35 +01:00
if (mtyp == M_NONE) {
2010-08-08 10:06:34 +02:00
mtyp = getmagicskill(u->faction->locale);
2011-03-07 08:02:35 +01:00
if (mtyp == M_NONE) {
2010-08-08 10:06:34 +02:00
cmistake(u, ord, 178, MSG_MAGIC);
return 0;
}
}
/* Legt damit das Magiegebiet der Partei fest */
u->faction->magiegebiet = mtyp;
}
}
}
if (sk == SK_ALCHEMY) {
maxalchemy = eff_skill(u, SK_ALCHEMY, r);
if (!has_skill(u, SK_ALCHEMY)) {
int amax = skill_limit(u->faction, SK_ALCHEMY);
if (count_skill(u->faction, SK_ALCHEMY) + u->number > amax) {
2011-03-07 08:02:35 +01:00
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_max_alchemists",
"amount", amax));
2010-08-08 10:06:34 +02:00
return 0;
}
}
}
if (studycost) {
int cost = studycost * u->number;
money = get_pooled(u, oldresourcetype[R_SILVER], GET_DEFAULT, cost);
money = MIN(money, cost);
}
if (money < studycost * u->number) {
2011-03-07 08:02:35 +01:00
studycost = p; /* Ohne Univertreurung */
2010-08-08 10:06:34 +02:00
money = MIN(money, studycost);
2011-03-07 08:02:35 +01:00
if (p > 0 && money < studycost * u->number) {
2010-08-08 10:06:34 +02:00
cmistake(u, ord, 65, MSG_EVENT);
multi = money / (double)(studycost * u->number);
}
}
2011-03-07 08:02:35 +01:00
if (teach == NULL) {
2010-08-08 10:06:34 +02:00
a = a_add(&u->attribs, a_new(&at_learning));
2011-03-07 08:02:35 +01:00
teach = (teaching_info *) a->data.v;
2010-08-08 10:06:34 +02:00
teach->teachers[0] = 0;
}
2011-03-07 08:02:35 +01:00
if (money > 0) {
2010-08-08 10:06:34 +02:00
use_pooled(u, oldresourcetype[R_SILVER], GET_DEFAULT, money);
ADDMSG(&u->faction->msgs, msg_message("studycost",
2011-03-07 08:02:35 +01:00
"unit region cost skill", u, u->region, money, sk));
2010-08-08 10:06:34 +02:00
}
if (get_effect(u, oldpotiontype[P_WISE])) {
l = MIN(u->number, get_effect(u, oldpotiontype[P_WISE]));
teach->value += l * 10;
change_effect(u, oldpotiontype[P_WISE], -l);
}
if (get_effect(u, oldpotiontype[P_FOOL])) {
l = MIN(u->number, get_effect(u, oldpotiontype[P_FOOL]));
teach->value -= l * 30;
change_effect(u, oldpotiontype[P_FOOL], -l);
}
if (p != studycost) {
/* ist_in_gebaeude(r, u, BT_UNIVERSITAET) == 1) { */
/* p ist Kosten ohne Uni, studycost mit; wenn
2011-03-07 08:02:35 +01:00
* p!=studycost, ist die Einheit zwangsweise
* in einer Uni */
2010-08-08 10:06:34 +02:00
teach->value += u->number * 10;
}
2011-03-07 08:02:35 +01:00
if (is_cursed(r->attribs, C_BADLEARN, 0)) {
2010-08-08 10:06:34 +02:00
teach->value -= u->number * 10;
}
multi *= study_speedup(u);
days = study_days(u, sk);
days = (int)((days + teach->value) * multi);
/* the artacademy currently improves the learning of entertainment
2011-03-07 08:02:35 +01:00
of all units in the region, to be able to make it cumulative with
with an academy */
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
if (sk == SK_ENTERTAINMENT
&& buildingtype_exists(r, bt_find("artacademy"), false)) {
2010-08-08 10:06:34 +02:00
days *= 2;
}
2011-03-07 08:02:35 +01:00
if (fval(u, UFL_HUNGER))
days /= 2;
2010-08-08 10:06:34 +02:00
while (days) {
2011-03-07 08:02:35 +01:00
if (days >= u->number * 30) {
2010-08-08 10:06:34 +02:00
learn_skill(u, sk, 1.0);
2011-03-07 08:02:35 +01:00
days -= u->number * 30;
2010-08-08 10:06:34 +02:00
} else {
2011-03-07 08:02:35 +01:00
double chance = (double)days / u->number / 30;
2010-08-08 10:06:34 +02:00
learn_skill(u, sk, chance);
days = 0;
}
}
2011-03-07 08:02:35 +01:00
if (a != NULL) {
if (teach != NULL) {
2010-08-08 10:06:34 +02:00
int index = 0;
2011-03-07 08:02:35 +01:00
while (teach->teachers[index] && index != MAXTEACHERS) {
unit *teacher = teach->teachers[index++];
2010-08-08 10:06:34 +02:00
if (teacher->faction != u->faction) {
bool feedback = alliedunit(u, teacher->faction, HELP_GUARD);
2010-08-08 10:06:34 +02:00
if (feedback) {
ADDMSG(&teacher->faction->msgs, msg_message("teach_teacher",
2011-03-07 08:02:35 +01:00
"teacher student skill level", teacher, u, sk,
effskill(u, sk)));
2010-08-08 10:06:34 +02:00
}
ADDMSG(&u->faction->msgs, msg_message("teach_student",
2011-03-07 08:02:35 +01:00
"teacher student skill", teacher, u, sk));
2010-08-08 10:06:34 +02:00
}
}
}
a_remove(&u->attribs, a);
a = NULL;
}
2011-03-07 08:02:35 +01:00
fset(u, UFL_LONGACTION | UFL_NOTMOVING);
2010-08-08 10:06:34 +02:00
/* Anzeigen neuer Traenke */
2010-08-08 10:06:34 +02:00
/* Spruchlistenaktualiesierung ist in Regeneration */
if (sk == SK_ALCHEMY) {
2011-03-07 08:02:35 +01:00
const potion_type *ptype;
faction *f = u->faction;
2010-08-08 10:06:34 +02:00
int skill = eff_skill(u, SK_ALCHEMY, r);
2011-03-07 08:02:35 +01:00
if (skill > maxalchemy) {
for (ptype = potiontypes; ptype; ptype = ptype->next) {
2010-08-08 10:06:34 +02:00
if (skill == ptype->level * 2) {
2011-03-07 08:02:35 +01:00
attrib *a = a_find(f->attribs, &at_showitem);
while (a && a->type == &at_showitem && a->data.v != ptype)
a = a->next;
if (a == NULL || a->type != &at_showitem) {
2010-08-08 10:06:34 +02:00
a = a_add(&f->attribs, a_new(&at_showitem));
2011-03-07 08:02:35 +01:00
a->data.v = (void *)ptype->itype;
2010-08-08 10:06:34 +02:00
}
}
}
}
2011-03-07 08:02:35 +01:00
} else if (sk == SK_MAGIC) {
sc_mage *mage = get_mage(u);
2010-08-08 10:06:34 +02:00
if (!mage) {
mage = create_mage(u, u->faction->magiegebiet);
}
}
return 0;
}