src/hcode/btech/econ.c

Go to the documentation of this file.
00001 
00002 /*
00003  * $Id: econ.c,v 1.1.1.1 2005/01/11 21:18:06 kstevens Exp $
00004  *
00005  * Author: Markus Stenberg <fingon@iki.fi>
00006  *
00007  *  Copyright (c) 1996 Markus Stenberg
00008  *  Copyright (c) 1998-2002 Thomas Wouters
00009  *  Copyright (c) 2000-2002 Cord Awtry
00010  *       All rights reserved
00011  *
00012  * Created: Sat Oct  5 14:06:02 1996 fingon
00013  * Last modified: Sat Apr 19 13:54:56 1997 fingon
00014  *
00015  */
00016 
00017 /* Idea:
00018    Store the parts in an attribute on object
00019 
00020    Format:
00021    [id,brand,count]{,[id,brand,count],..}
00022  */
00023 
00024 #include "mech.h"
00025 
00026 extern char *silly_atr_get(int id, int flag);
00027 extern void silly_atr_set(int id, int flag, char *dat);
00028 
00029 /* entry = pointer to [ */
00030 static void remove_entry(char *alku, char *entry)
00031 {
00032         char *j;
00033 
00034         if(!(j = strstr(entry, "]")))
00035                 return;
00036         j++;
00037         if(*j) {
00038                 /* Move the remainder of the string, including the terminating NUL,
00039                    but not including the separating comma */
00040                 j++;
00041                 memmove(entry, j, strlen(j) + 1);
00042         } else {
00043                 if(entry == alku)
00044                         *alku = '\0';
00045                 else
00046                         *(entry - 1) = '\0';
00047         }
00048 }
00049 
00050 static void add_entry(char *to, char *data)
00051 {
00052         if(*to)
00053                 sprintf(to + strlen(to), ",[%s]", data);
00054         else
00055                 sprintf(to, "[%s]", data);
00056 }
00057 
00058 static char *find_entry(char *s, int i, int b)
00059 {
00060         char buf[MBUF_SIZE];
00061 
00062         sprintf(buf, "[%d,%d,", i, b);
00063         return strstr(s, buf);
00064 }
00065 
00066 extern char *get_parts_short_name(int, int);
00067 
00068 void econ_change_items(dbref d, int id, int brand, int num)
00069 {
00070         char *t, *u;
00071         int base = 0, i1, i2, i3;
00072 
00073         if(!Good_obj(d))
00074                 return;
00075         if(brand)
00076                 if(get_parts_short_name(id, brand) == get_parts_short_name(id, 0))
00077                         brand = 0;
00078         t = silly_atr_get(d, A_ECONPARTS);
00079         if((u = find_entry(t, id, brand))) {
00080                 if(sscanf(u, "[%d,%d,%d]", &i1, &i2, &i3) == 3)
00081                         base += i3;
00082                 remove_entry(t, u);
00083         }
00084         base += num;
00085         if(base <= 0) {
00086                 if(u)
00087                         silly_atr_set(d, A_ECONPARTS, t);
00088                 return;
00089         }
00090         if(!(IsActuator(id)))
00091                 add_entry(t, tprintf("%d,%d,%d", id, brand, base));
00092         silly_atr_set(d, A_ECONPARTS, t);
00093         if(IsActuator(id))
00094                 econ_change_items(d, Cargo(S_ACTUATOR), brand, base);
00095         /* Successfully changed */
00096 }
00097 
00098 int econ_find_items(dbref d, int id, int brand)
00099 {
00100         char *t, *u;
00101         int i1, i2, i3;
00102 
00103         if(!Good_obj(d))
00104                 return 0;
00105         if(brand)
00106                 if(get_parts_short_name(id, brand) == get_parts_short_name(id, 0))
00107                         brand = 0;
00108         t = silly_atr_get(d, A_ECONPARTS);
00109         if((u = find_entry(t, id, brand)))
00110                 if(sscanf(u, "[%d,%d,%d]", &i1, &i2, &i3) == 3)
00111                         return i3;
00112         return 0;
00113 }
00114 
00115 void econ_set_items(dbref d, int id, int brand, int num)
00116 {
00117         int i;
00118 
00119         if(!Good_obj(d))
00120                 return;
00121         i = econ_find_items(d, id, brand);
00122         if(i != num)
00123                 econ_change_items(d, id, brand, num - i);
00124 }

Generated on Mon May 28 04:25:21 2007 for BattletechMUX by  doxygen 1.4.7