Bugfixes an replace_order

This commit is contained in:
Enno Rehling 2005-05-22 12:26:19 +00:00
parent 2a04a9a22c
commit 7ed6f06a15
4 changed files with 77 additions and 54 deletions

View File

@ -801,8 +801,9 @@ build_building(unit * u, const building_type * btype, int want, order * ord)
building * b = NULL;
/* einmalige Korrektur */
static char buffer[8 + IDSIZE + 1 + NAMESIZE + 1];
const char *string2;
order * new_order;
const char *btname = NULL;
order * new_order = NULL;
const struct locale * lang = u->faction->locale;
if (eff_skill(u, SK_BUILDING, r) == 0) {
cmistake(u, ord, 101, MSG_PRODUCE);
@ -881,9 +882,7 @@ build_building(unit * u, const building_type * btype, int want, order * ord)
if (c!=0) strcat(ch++, ",");
n = cons->materials[c].number / cons->reqsize;
sprintf(ch, " %d %s", n?n:1,
locale_string(u->faction->locale,
resname(cons->materials[c].type, cons->materials[c].number!=1))
);
LOC(lang, resname(cons->materials[c].type, cons->materials[c].number!=1)));
ch = ch+strlen(ch);
}
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "build_required",
@ -901,7 +900,7 @@ build_building(unit * u, const building_type * btype, int want, order * ord)
/* at this point, the building size is increased. */
if (b==NULL) {
/* build a new building */
b = new_building(btype, r, u->faction->locale);
b = new_building(btype, r, lang);
b->type = btype;
fset(b, BLD_MAINTAINED);
@ -910,31 +909,42 @@ build_building(unit * u, const building_type * btype, int want, order * ord)
u->building = b;
fset(u, UFL_OWNER);
newbuilding = 1;
newbuilding = true;
}
if (b->type->name) {
string2 = LOC(u->faction->locale, b->type->_name);
} else {
string2 = LOC(u->faction->locale, buildingtype(b, b->size));
if( newbuilding && b->type->maxsize != -1 )
if (newbuilding) {
if (b->type->name==NULL) {
btname = LOC(lang, b->type->_name);
} else {
btname = LOC(lang, buildingtype(b, b->size));
if (b->type->maxsize != -1) {
want = b->type->maxsize - b->size;
}
}
}
if( want == INT_MAX )
sprintf(buffer, "%s %s %s", locale_string(u->faction->locale, keywords[K_MAKE]), string2, buildingid(b));
else if( want-built <= 0 )
strcpy(buffer, locale_string(u->faction->locale, "defaultorder"));
else
sprintf(buffer, "%s %d %s %s", locale_string(u->faction->locale, keywords[K_MAKE]), want-built, string2, buildingid(b));
if (want-built <= 0) {
/* gebäude fertig */
strcpy(buffer, locale_string(lang, "defaultorder"));
new_order = parse_order(buffer, lang);
} else if (want!=INT_MAX) {
/* reduzierte restgröße */
sprintf(buffer, "%s %d %s %s", LOC(lang, keywords[K_MAKE]), want-built, btname, buildingid(b));
new_order = parse_order(buffer, lang);
} else if (btname) {
/* Neues Haus, Befehl mit Gebäudename */
sprintf(buffer, "%s %s %s", LOC(lang, keywords[K_MAKE]), btname, buildingid(b));
new_order = parse_order(buffer, u->faction->locale);
}
new_order = parse_order(buffer, u->faction->locale);
if (new_order) {
#ifdef LASTORDER
set_order(&u->lastorder, new_order);
set_order(&u->lastorder, new_order);
#else
replace_order(ord, new_order);
free_order(new_order);
replace_order(&u->orders, ord, new_order);
free_order(new_order);
#endif
}
b->size += built;
update_lighthouse(b);
@ -1025,7 +1035,7 @@ create_ship(region * r, unit * u, const struct ship_type * newtype, int want, or
#ifdef LASTORDER
set_order(&u->lastorder, new_order);
#else
replace_order(ord, new_order);
replace_order(&u->orders, ord, new_order);
free_order(new_order);
#endif

View File

@ -914,7 +914,7 @@ cycle_route(order * ord, unit *u, int gereist)
#ifdef LASTORDER
set_order(&u->lastorder, norder);
#else
replace_order(ord, norder);
replace_order(&u->orders, ord, norder);
free_order(norder);
#endif
}

View File

@ -14,6 +14,7 @@
#include "eressea.h"
#include "order.h"
#include "skill.h"
/* libc includes */
#include <assert.h>
@ -21,7 +22,7 @@
#include <stdlib.h>
#include <string.h>
#undef SHORT_STRINGS
#define SHORT_STRINGS
#ifdef SHARE_ORDERS
# define ORD_KEYWORD(ord) (ord)->data->_keyword
@ -68,16 +69,19 @@ release_data(order_data * data)
#endif
void
replace_order(order * dst, const order * src)
replace_order(order ** dlist, order * orig, const order * src)
{
#ifdef SHARE_ORDERS
release_data(dst->data);
++src->data->_refcount;
#else
if (dst->data._str) free(dst->data._str);
#endif
dst->data = src->data;
dst->_persistent = src->_persistent;
while (*dlist!=NULL) {
order * dst = *dlist;
if (dst->data==orig->data) {
order * cpy = copy_order(src);
*dlist = cpy;
cpy->next = dst->next;
dst->next = 0;
free_order(dst);
}
dlist = &(*dlist)->next;
}
}
keyword_t
@ -86,11 +90,7 @@ get_keyword(const order * ord)
if (ord==NULL) {
return NOKEYWORD;
}
#ifdef SHARE_ORDERS
return ord->data->_keyword;
#else
return ord->data._keyword;
#endif
return ORD_KEYWORD(ord);
}
char *
@ -135,7 +135,7 @@ free_order(order * ord)
}
order *
copy_order(order * src)
copy_order(const order * src)
{
if (src!=NULL) {
order * ord = (order*)malloc(sizeof(order));
@ -175,6 +175,8 @@ create_data(keyword_t kwd, const char * s, const char * sptr, int lindex)
{
order_data * data;
const struct locale * lang = locale_array[lindex]->lang;
/* learning, only one order_data per skill required */
if (kwd==K_STUDY) {
skill_t sk = findskill(parse_token(&sptr), lang);
if (sk!=NOSKILL) {
@ -184,13 +186,20 @@ create_data(keyword_t kwd, const char * s, const char * sptr, int lindex)
locale_array[lindex]->study_orders[sk] = data;
data->_keyword = kwd;
data->_lindex = lindex;
data->_str = s?strdup(s):NULL;
#ifdef SHORT_STRINGS
data->_str = strdup(skillname(sk, lang));
#else
sprintf(buf, "%s %s", LOC(lang, keywords[kwd]), skillname(sk, lang));
data->_str = strdup(buf);
#endif
data->_refcount = 1;
}
++data->_refcount;
return data;
}
}
/* orders with no parameter, only one order_data per order required */
if (kwd!=NOKEYWORD && *sptr == 0) {
data = locale_array[lindex]->short_orders[kwd];
if (data == NULL) {
@ -198,7 +207,11 @@ create_data(keyword_t kwd, const char * s, const char * sptr, int lindex)
locale_array[lindex]->short_orders[kwd] = data;
data->_keyword = kwd;
data->_lindex = lindex;
data->_str = s?strdup(s):NULL;
#ifdef SHORT_STRINGS
data->_str = NULL;
#else
data->_str = strdup(LOC(lang, keywords[kwd]));
#endif
data->_refcount = 1;
}
++data->_refcount;

View File

@ -25,7 +25,7 @@ extern "C" {
* implemented yet) saving approx. 50% of all string-related memory.
*/
#undef SHARE_ORDERS
#define SHARE_ORDERS
#ifdef SHARE_ORDERS
struct order_data;
@ -52,23 +52,23 @@ typedef struct order {
#endif
/* constructor */
extern struct order * parse_order(const char * s, const struct locale * lang);
extern void replace_order(order * dst, const order * src);
extern order * parse_order(const char * s, const struct locale * lang);
extern void replace_order(order ** dst, order * orig, const order * src);
/* reference counted copies of orders: */
extern struct order * copy_order(struct order * ord);
extern void free_order(struct order * ord);
extern void free_orders(struct order ** olist);
extern order * copy_order(const order * ord);
extern void free_order(order * ord);
extern void free_orders(order ** olist);
/* access functions for orders */
extern keyword_t get_keyword(const struct order * ord);
extern void set_order(struct order ** destp, struct order * src);
extern char * getcommand(const struct order * ord);
extern boolean is_persistent(const struct order *ord);
extern boolean is_exclusive(const struct order *ord);
extern keyword_t get_keyword(const order * ord);
extern void set_order(order ** destp, order * src);
extern char * getcommand(const order * ord);
extern boolean is_persistent(const order *ord);
extern boolean is_exclusive(const order *ord);
extern boolean is_repeated(const order * ord);
extern char * write_order(const struct order * ord, const struct locale * lang, char * buffer, size_t size);
extern char * write_order(const order * ord, const struct locale * lang, char * buffer, size_t size);
#ifdef __cplusplus
}
#endif