Freeciv-3.3
Loading...
Searching...
No Matches
tech.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <stdlib.h> /* exit */
19#include <string.h>
20#include <math.h>
21
22/* utility */
23#include "fcintl.h"
24#include "iterator.h"
25#include "log.h"
26#include "mem.h" /* free */
27#include "shared.h" /* ARRAY_SIZE */
28#include "string_vector.h"
29#include "support.h"
30
31/* common */
32#include "game.h"
33#include "research.h"
34
35#include "tech.h"
36
37
39 struct iterator base;
41 const struct advance *array[A_LAST];
42 const struct advance **current, **end;
43};
44#define ADVANCE_REQ_ITER(it) ((struct advance_req_iter *) it)
45
47 struct iterator base;
49 const struct advance *array[A_LAST];
50 const struct advance **current, **end;
51};
52#define ADVANCE_ROOT_REQ_ITER(it) ((struct advance_root_req_iter *) it)
53
54/* the advances array is now setup in:
55 * server/ruleset.c (for the server)
56 * client/packhand.c (for the client)
57 */
59
61
63
64/**********************************************************************/
67const struct advance *advance_array_last(void)
68{
69 if (game.control.num_tech_types > 0) {
71 }
72 return NULL;
73}
74
75/**********************************************************************/
82
83/**********************************************************************/
94
95/**********************************************************************/
99{
101 return padvance->item_number;
102}
103
104/**********************************************************************/
108{
109 if (atype != A_FUTURE
111 /* This isn't an error; some callers depend on it. */
112 return NULL;
113 }
114
115 return &advances[atype];
116}
117
118/**********************************************************************/
122 enum tech_req require)
123{
125 fc_assert_ret_val(tech >= A_NONE && tech < A_LAST, -1);
126 if (A_NEVER == advances[tech].require[require]) {
127 /* out of range */
128 return A_LAST;
129 }
130 return advance_number(advances[tech].require[require]);
131}
132
133/**********************************************************************/
137 enum tech_req require)
138{
141 return padvance->require[require];
142}
143
144/**********************************************************************/
153{
154 if (padvance == NULL) {
155 return NULL;
156 }
157
158 if (padvance->item_number == A_FUTURE) {
159 return padvance;
160 }
161
162 if (A_NEVER == padvance->require[AR_ONE]
163 || A_NEVER == padvance->require[AR_TWO]) {
164 return NULL;
165 }
166
167 return padvance;
168}
169
170/**********************************************************************/
180
181/**********************************************************************/
186{
189 return padvance;
190 }
192
193 return NULL;
194}
195
196/**********************************************************************/
201{
202 const char *qname = Qn_(name);
203
206 return padvance;
207 }
209
210 return NULL;
211}
212
213/**********************************************************************/
217{
219 return BV_ISSET(advance_by_number(tech)->flags, flag);
220}
221
222/**********************************************************************/
226{
228 "Invalid tech_cost_style %d", game.info.tech_cost_style);
229
231 int num_reqs = 0;
232 bool min_req = TRUE;
233
235 (void) preq; /* Compiler wants us to do something with 'preq'. */
236 num_reqs++;
238 padvance->num_reqs = num_reqs;
239
240 switch (game.info.tech_cost_style) {
242 case TECH_COST_LINEAR:
244 break;
246 if (-1 != padvance->cost) {
247 min_req = FALSE;
248 break;
249 }
250 fc__fallthrough; /* No break. */
252 padvance->cost = game.info.base_tech_cost * (1.0 + num_reqs)
253 * sqrt(1.0 + num_reqs) / 2;
254 break;
256 if (-1 != padvance->cost) {
257 min_req = FALSE;
258 break;
259 }
260 fc__fallthrough; /* No break. */
263 / (1 + sqrt(sqrt(num_reqs + 1))) - 0.5);
264 break;
265 }
266
267 if (min_req && padvance->cost < game.info.min_tech_cost) {
269 }
270
271 /* Class cost */
272 if (padvance->tclass != NULL) {
273 padvance->cost = padvance->cost * padvance->tclass->cost_pct / 100;
274 }
276}
277
278/**********************************************************************/
282{
283 return tech == A_FUTURE;
284}
285
286/**********************************************************************/
291{
292 return valid_advance(padvance) != nullptr
294}
295
296/**********************************************************************/
300const char *advance_name_translation(const struct advance *padvance)
301{
302 return name_translation_get(&padvance->name);
303}
304
305/**********************************************************************/
309const char *advance_rule_name(const struct advance *padvance)
310{
311 return rule_name_get(&padvance->name);
312}
313
314/**********************************************************************/
318{
319 int i;
320
321 for (i = 0; i < MAX_NUM_TECH_CLASSES; i++) {
322 tech_classes[i].idx = i;
324 }
325}
326
327/**********************************************************************/
331{
333 return NULL;
334 }
335
336 return &tech_classes[idx];
337}
338
339/**********************************************************************/
344{
345 return name_translation_get(&ptclass->name);
346}
347
348/**********************************************************************/
352const char *tech_class_rule_name(const struct tech_class *ptclass)
353{
354 return rule_name_get(&ptclass->name);
355}
356
357/**********************************************************************/
362{
363 const char *qname = Qn_(name);
364 int i;
365
366 for (i = 0; i < game.control.num_tech_classes; i++) {
368
370 return ptclass;
371 }
372 }
373
374 return NULL;
375}
376
377/**********************************************************************/
381{
382 int i;
383
384 for (i = 0; i < MAX_NUM_USER_TECH_FLAGS; i++) {
386 }
387}
388
389/**********************************************************************/
393{
394 int i;
395
396 for (i = 0; i < MAX_NUM_USER_TECH_FLAGS; i++) {
398 }
399}
400
401/**********************************************************************/
404void set_user_tech_flag_name(enum tech_flag_id id, const char *name,
405 const char *helptxt)
406{
407 int tfid = id - TECH_USER_1;
408
410
411 if (user_tech_flags[tfid].name != NULL) {
414 }
415
416 if (name && name[0] != '\0') {
418 }
419
420 if (user_tech_flags[tfid].helptxt != NULL) {
421 FC_FREE(user_tech_flags[tfid].helptxt);
423 }
424
425 if (helptxt && helptxt[0] != '\0') {
427 }
428}
429
430/**********************************************************************/
433const char *tech_flag_id_name_cb(enum tech_flag_id flag)
434{
436 return NULL;
437 }
438
439 return user_tech_flags[flag-TECH_USER_1].name;
440}
441
442/**********************************************************************/
445const char *tech_flag_helptxt(enum tech_flag_id id)
446{
447 fc_assert(id >= TECH_USER_1 && id <= TECH_USER_LAST);
448
450}
451
452/**********************************************************************/
465
466/**********************************************************************/
469void techs_init(void)
470{
471 struct advance *a_none = &advances[A_NONE];
472 struct advance *a_future = &advances[A_FUTURE];
473 int i;
474
475 memset(advances, 0, sizeof(advances));
476 for (i = 0; i < ARRAY_SIZE(advances); i++) {
479 advances[i].cost = -1;
481 advances[i].tclass = 0;
482
484 }
485
486 /* Initialize dummy tech A_NONE */
487 /* TRANS: "None" tech */
488 name_set(&a_none->name, NULL, N_("?tech:None"));
489 a_none->require[AR_ONE] = a_none;
490 a_none->require[AR_TWO] = a_none;
491 a_none->require[AR_ROOT] = A_NEVER;
492
493 name_set(&a_future->name, NULL, "Future");
494 a_future->require[AR_ONE] = A_NEVER;
495 a_future->require[AR_TWO] = A_NEVER;
496 a_future->require[AR_ROOT] = A_NEVER;
497}
498
499/**********************************************************************/
502static void tech_free(Tech_type_id tech)
503{
504 struct advance *p = &advances[tech];
505
506 if (NULL != p->helptext) {
508 p->helptext = NULL;
509 }
510
511 if (p->bonus_message) {
513 p->bonus_message = NULL;
514 }
515}
516
517/**********************************************************************/
520void techs_free(void)
521{
522 int i;
523
527
528 for (i = 0; i < ARRAY_SIZE(advances); i++) {
530 }
531}
532
533/**********************************************************************/
537{
538 return sizeof(struct advance_req_iter);
539}
540
541/**********************************************************************/
544static void *advance_req_iter_get(const struct iterator *it)
545{
546 return (void *) *ADVANCE_REQ_ITER(it)->current;
547}
548
549/**********************************************************************/
552static void advance_req_iter_next(struct iterator *it)
553{
555 const struct advance *padvance = *iter->current, *preq;
556 enum tech_req req;
557 bool new = FALSE;
558
559 for (req = AR_ONE; req < AR_SIZE; req++) {
561 if (NULL != preq
563 && !BV_ISSET(iter->done, advance_number(preq))) {
565 if (new) {
566 *iter->end++ = preq;
567 } else {
568 *iter->current = preq;
569 new = TRUE;
570 }
571 }
572 }
573
574 if (!new) {
575 iter->current++;
576 }
577}
578
579/**********************************************************************/
582static bool advance_req_iter_valid(const struct iterator *it)
583{
584 const struct advance_req_iter *iter = ADVANCE_REQ_ITER(it);
585
586 return iter->current < iter->end;
587}
588
589/**********************************************************************/
593 const struct advance *goal)
594{
595 struct iterator *base = ITERATOR(it);
596
600
601 BV_CLR_ALL(it->done);
602 it->current = it->array;
603 *it->current = goal;
604 it->end = it->current + 1;
605
606 return base;
607}
608
609/************************************************************************/
613{
614 return sizeof(struct advance_root_req_iter);
615}
616
617/************************************************************************/
620static void *advance_root_req_iter_get(const struct iterator *it)
621{
622 return
624}
625
626/************************************************************************/
629static bool advance_root_req_iter_valid(const struct iterator *it)
630{
632
633 return iter->current < iter->end;
634}
635
636/************************************************************************/
640{
642
643 /* Precondition: either iteration has already finished, or iter->current
644 * points at a tech with an interesting root_req (which means its
645 * requirements may have more). */
646 while (advance_root_req_iter_valid(it)) {
647 const struct advance *padvance = *iter->current;
648 enum tech_req req;
649 bool new = FALSE;
650
651 for (req = AR_ONE; req < AR_SIZE; req++) {
652 const struct advance *preq
654
655 if (NULL != preq
657 && !BV_ISSET(iter->done, advance_number(preq))) {
658
660 /* Do we need to look at this subtree at all? If it has A_NONE as
661 * root_req, further root_reqs can't propagate through it, so no. */
663 /* Yes, this subtree needs iterating over at some point, starting
664 * with preq (whose own root_req we'll consider in a bit) */
665 if (!new) {
666 *iter->current = preq;
667 new = TRUE;
668 } else {
669 *iter->end++ = preq; /* make a note for later */
670 }
671 }
672 }
673 }
674 if (!new) {
675 /* Didn't find an interesting new subtree. */
676 iter->current++;
677 }
678 /* Precondition: *current has been moved on from where we started, and
679 * it has an interesting root_req or it wouldn't be on the list; but
680 * it may be one that we've already yielded. */
683 AR_ROOT);
684 if (!BV_ISSET(iter->rootdone, root)) {
685 /* A previously unseen root_req. Stop and yield it. */
686 break;
687 } /* else keep looking */
688 }
689 }
690}
691
692/************************************************************************/
696 const struct advance *goal)
697{
698 struct iterator *base = ITERATOR(it);
699
703
704 BV_CLR_ALL(it->done);
705 BV_CLR_ALL(it->rootdone);
706 it->current = it->array;
708 /* First root_req to return is goal's own, and there may be more
709 * for next() to find. */
710 *it->current = goal;
711 it->end = it->current + 1;
712 } else {
713 /* No root_reqs -- go straight to invalid state */
714 it->end = it->current;
715 }
716
717 return base;
718}
#define BV_CLR_ALL(bv)
Definition bitvector.h:103
#define BV_SET(bv, bit)
Definition bitvector.h:89
#define BV_ISSET(bv, bit)
Definition bitvector.h:86
char * incite_cost
Definition comments.c:76
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2956
int Tech_type_id
Definition fc_types.h:236
#define MAX_NUM_TECH_CLASSES
Definition fc_types.h:63
#define Qn_(String)
Definition fcintl.h:89
#define N_(String)
Definition fcintl.h:69
void user_flag_init(struct user_flag *flag)
Definition game.c:829
void user_flag_free(struct user_flag *flag)
Definition game.c:838
struct civ_game game
Definition game.c:61
const char * name
Definition inputfile.c:127
#define ITERATOR(p)
Definition iterator.h:37
#define fc_assert_msg(condition, message,...)
Definition log.h:182
#define fc_assert_ret(condition)
Definition log.h:192
#define fc_assert(condition)
Definition log.h:177
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_strdup(str)
Definition mem.h:43
static void name_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name)
static const char * rule_name_get(const struct name_translation *ptrans)
static const char * name_translation_get(const struct name_translation *ptrans)
#define ARRAY_SIZE(x)
Definition shared.h:85
void strvec_destroy(struct strvec *psv)
bv_techs done
Definition tech.c:40
const struct advance ** end
Definition tech.c:42
const struct advance ** current
Definition tech.c:42
struct iterator base
Definition tech.c:39
const struct advance * array[A_LAST]
Definition tech.c:41
const struct advance * array[A_LAST]
Definition tech.c:49
const struct advance ** current
Definition tech.c:50
bv_techs rootdone
Definition tech.c:48
bv_techs done
Definition tech.c:48
const struct advance ** end
Definition tech.c:50
struct iterator base
Definition tech.c:47
void * ruledit_dlg
Definition tech.h:122
struct requirement_vector research_reqs
Definition tech.h:132
struct tech_class * tclass
Definition tech.h:125
double cost
Definition tech.h:145
struct advance * require[AR_SIZE]
Definition tech.h:127
char * bonus_message
Definition tech.h:140
struct strvec * helptext
Definition tech.h:135
bool inherited_root_req
Definition tech.h:128
bv_tech_flags flags
Definition tech.h:134
int num_reqs
Definition tech.h:151
Tech_type_id item_number
Definition tech.h:120
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
enum tech_leakage_style tech_leakage
enum tech_cost_style tech_cost_style
bool ruledit_disabled
Definition tech.h:115
int idx
Definition tech.h:113
char * name
Definition game.h:74
char * helptxt
Definition game.h:75
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define fc__fallthrough
Definition support.h:119
Tech_type_id advance_count_real(void)
Definition tech.c:78
struct advance * advance_by_number(const Tech_type_id atype)
Definition tech.c:107
const char * tech_flag_id_name_cb(enum tech_flag_id flag)
Definition tech.c:433
void tech_classes_init(void)
Definition tech.c:317
const char * tech_class_rule_name(const struct tech_class *ptclass)
Definition tech.c:352
void set_user_tech_flag_name(enum tech_flag_id id, const char *name, const char *helptxt)
Definition tech.c:404
bool is_future_tech(Tech_type_id tech)
Definition tech.c:281
static void advance_req_iter_next(struct iterator *it)
Definition tech.c:552
struct advance * valid_advance(struct advance *padvance)
Definition tech.c:152
void techs_precalc_data(void)
Definition tech.c:225
struct advance * advance_by_translated_name(const char *name)
Definition tech.c:185
bool advance_has_flag(Tech_type_id tech, enum tech_flag_id flag)
Definition tech.c:216
size_t advance_root_req_iter_sizeof(void)
Definition tech.c:612
const char * tech_class_name_translation(const struct tech_class *ptclass)
Definition tech.c:343
#define ADVANCE_ROOT_REQ_ITER(it)
Definition tech.c:52
static struct user_flag user_tech_flags[MAX_NUM_USER_TECH_FLAGS]
Definition tech.c:62
struct advance advances[A_ARRAY_SIZE]
Definition tech.c:58
struct iterator * advance_root_req_iter_init(struct advance_root_req_iter *it, const struct advance *goal)
Definition tech.c:695
struct iterator * advance_req_iter_init(struct advance_req_iter *it, const struct advance *goal)
Definition tech.c:592
static void * advance_root_req_iter_get(const struct iterator *it)
Definition tech.c:620
static void tech_free(Tech_type_id tech)
Definition tech.c:502
struct advance * advance_requires(const struct advance *padvance, enum tech_req require)
Definition tech.c:136
const char * advance_name_translation(const struct advance *padvance)
Definition tech.c:300
struct advance * valid_advance_by_number(const Tech_type_id id)
Definition tech.c:176
static bool advance_root_req_iter_valid(const struct iterator *it)
Definition tech.c:629
bool techs_have_fixed_costs(void)
Definition tech.c:460
Tech_type_id advance_required(const Tech_type_id tech, enum tech_req require)
Definition tech.c:121
struct tech_class * tech_class_by_number(const int idx)
Definition tech.c:330
void techs_init(void)
Definition tech.c:469
const char * advance_rule_name(const struct advance *padvance)
Definition tech.c:309
struct tech_class tech_classes[MAX_NUM_TECH_CLASSES]
Definition tech.c:60
const struct advance * advance_array_last(void)
Definition tech.c:67
static void * advance_req_iter_get(const struct iterator *it)
Definition tech.c:544
Tech_type_id advance_index(const struct advance *padvance)
Definition tech.c:89
const char * tech_flag_helptxt(enum tech_flag_id id)
Definition tech.c:445
static bool advance_req_iter_valid(const struct iterator *it)
Definition tech.c:582
void techs_free(void)
Definition tech.c:520
struct advance * advance_by_rule_name(const char *name)
Definition tech.c:200
static void advance_root_req_iter_next(struct iterator *it)
Definition tech.c:639
void user_tech_flags_init(void)
Definition tech.c:380
size_t advance_req_iter_sizeof(void)
Definition tech.c:536
#define ADVANCE_REQ_ITER(it)
Definition tech.c:44
struct tech_class * tech_class_by_rule_name(const char *name)
Definition tech.c:361
bool is_regular_advance(struct advance *padvance)
Definition tech.c:290
Tech_type_id advance_number(const struct advance *padvance)
Definition tech.c:98
void user_tech_flags_free(void)
Definition tech.c:392
#define A_ARRAY_SIZE
Definition tech.h:47
#define A_FUTURE
Definition tech.h:46
#define A_NEVER
Definition tech.h:51
#define advance_index_iterate_end
Definition tech.h:244
#define advance_req_iterate(_goal, _padvance)
Definition tech.h:295
#define MAX_NUM_USER_TECH_FLAGS
Definition tech.h:103
#define advance_iterate_all_end
Definition tech.h:275
tech_req
Definition tech.h:105
@ AR_TWO
Definition tech.h:107
@ AR_ROOT
Definition tech.h:108
@ AR_ONE
Definition tech.h:106
@ AR_SIZE
Definition tech.h:109
#define advance_iterate_all(_p)
Definition tech.h:274
#define advance_req_iterate_end
Definition tech.h:299
#define A_FIRST
Definition tech.h:44
#define A_NONE
Definition tech.h:43
#define advance_iterate(_p)
Definition tech.h:271
#define advance_iterate_end
Definition tech.h:272
#define A_LAST
Definition tech.h:45
#define advance_index_iterate(_start, _index)
Definition tech.h:240