~case/dbns

ref: 85d3c74215baa48cb2fd97d3c85ff6060e7c42b1 dbns/src/rare.c -rw-r--r-- 5.8 KiB
85d3c742Zerhyn SBC oopsie 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/****************************************************************************
 * [S]imulated [M]edieval [A]dventure multi[U]ser [G]ame      |   \\._.//   *
 * -----------------------------------------------------------|   (0...0)  *
 * SMAUG 1.4 (C)1994, 1995, 1996, 1998  by Derek Snider      |   ).:.(   *
 * -----------------------------------------------------------|    {o o}    *
 * SMAUG code team: Thoric, Altrag, Blodkai, Narn, Haus,      |   / ' ' \   *
 * Scryn, Rennard, Swordbearer, Gorog, Grishnakh, Nivek,      |~'~.VxvxV.~'~*
 * Tricops and Fireblade                                      |             *
 * ------------------------------------------------------------------------ *
 * Merc 2.1 Diku Mud improvments copyright (C)1992, 1993 by Michael        *
 * Chastain, Michael Quan, and Mitchell Tse.                                *
 * Original Diku Mud copyright (C)1990, 1991 by Sebastian Hammer,          *
 * Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe.     *
 * ------------------------------------------------------------------------ *
 *			   Rare Item Code Module                            *
 *                       Shadroth - Lands of Shadow                         *
 * ------------------------------------------------------------------------ *
 *			  capsule/corp (C)2016, 2017		    	    *
 *                            case@capsulecorp.org			    *
 ****************************************************************************/

#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <dirent.h>
#include "mud.h"

#if defined(KEY)
#undef KEY
#endif

#define KEY(literal, field, value)		\
	if (!strcmp(word, literal))		\
	{                                       \
	field  = value;				\
	fMatch = true;				\
	break;					\
	}

void
scan_equipment(void)
{
	DIR *dp;
	struct dirent *dir_entry;
	char dir_name[100];
	int alpha_loop;

	log_string("Updating rare/unique item counts.....");
	log_string("Checking player files....");
   
	for (alpha_loop = 0; alpha_loop <= 25; alpha_loop++) {
		snprintf(dir_name, sizeof(dir_name), "%s%c", PLAYER_DIR, 'a' + alpha_loop);
		dp = opendir(dir_name);
		if (dp != NULL) {
			dir_entry = readdir(dp);
			while (dir_entry) {
				if (dir_entry->d_name[0] != '.') {
					printf("%s\n", dir_entry->d_name);
					if (scan_players(dir_name, dir_entry->d_name))
						log_string("Found rare/unique items....");
				}
				dir_entry = readdir(dp);
			}
			closedir(dp);
		} else {
			mkdir(dir_name, 0744);
			continue;
		}
	}

	log_string("Checking corpses....");
   
	snprintf(dir_name, sizeof(dir_name), "%s", CORPSE_DIR);
	dp = opendir(dir_name);
	if (dp != NULL) {
		dir_entry = readdir(dp);
		while (dir_entry) {
			if (dir_entry->d_name[0] != '.') {
				if(scan_corpses(dir_name, dir_entry->d_name))
					log_string("Found rare/unique items.");
			}
			dir_entry = readdir(dp);
		}
		closedir(dp);
   
		return;
	} else {
	    mkdir(dir_name, 0744);
	}
}

bool
scan_players(char *dirname, char *filename)
{
	FILE *player_file;
	char file_name[MAX_STRING_LENGTH];
	bool found = false;
	bool fMatch;

	snprintf(file_name, sizeof(file_name), "%s/%s", dirname, filename);

	if ((player_file = fopen(file_name, "r")) == NULL) {
		perror(file_name);
		return found;
	}

	for (;;) {
		int counter = 1;
		char letter;
		char *word;
		char *short_descr;
		OBJ_INDEX_DATA *pObjIndex;

		letter = fread_letter(player_file);

		if ((letter != '#')
		    && (!feof(player_file)))
			continue;

		word = feof(player_file) ? "End" : fread_word(player_file);

		if (!str_cmp(word, "End"))
			break;

		if (!str_cmp(word, "OBJECT")) {
			bool eoo = false;
			while(eoo == false) {
				word = feof(player_file) ? "End" : fread_word(player_file);

				switch(UPPER(word[0]))
				{
				case '*':
					fMatch = true;
					fread_to_eol(player_file);
					break;
				case 'C':
					KEY("Count",       counter,	     fread_number(player_file));
					break;
				case 'E':
					if(!str_cmp(word, "End"))
						eoo = true;
					break;
				case 'S':
					KEY("ShortDescr",  short_descr,       fread_string(player_file));
					break;
				case 'V':
					if (!strcmp(word, "Vnum")) {
						int vnum;

						vnum = fread_number(player_file);
						if ((pObjIndex = get_obj_index(vnum))== NULL) {
							bug("Bad vnum while reading in rare.c");
							break;
						} else {
							if(IS_OBJ_STAT(pObjIndex, ITEM_RARE)|| IS_OBJ_STAT(pObjIndex, ITEM_UNIQUE)) {
								pObjIndex->count += counter;
								found = true;
							}
						}
					}
					break;
				}
			}
		}
	}

	fclose(player_file);
	player_file = NULL;
	return found;
}

bool
scan_corpses(char *dirname, char *filename)
{
	FILE *corpse_file;
	char file_name[MAX_STRING_LENGTH];
	bool found = false;

	snprintf(file_name, sizeof(file_name), "%s/%s", dirname, filename);

	if ((corpse_file = fopen(file_name, "r"))== NULL) {
		perror(file_name);
		return found;
	}

	for (;;) {
		int vnum, counter = 1;
		char letter;
		char *word;
		OBJ_INDEX_DATA *pObjIndex;

		letter = fread_letter(corpse_file);

		if ((letter != '#') && (!feof(corpse_file)))
			continue;

		word   = feof(corpse_file) ? "End" : fread_word(corpse_file);

		if (!str_cmp(word, "End"))
			break;
		if (!str_cmp(word, "OBJECT")) {
			for (;;) {
				word   = feof(corpse_file) ? "End" : fread_word(corpse_file);
 
				if (!str_cmp(word, "End"))
					break;
				if (!str_cmp(word, "Count"))
					counter = fread_number(corpse_file);
				if (!str_cmp(word, "Vnum")) {
					vnum = fread_number(corpse_file);
					if ((get_obj_index(vnum))== NULL) {
						bug("Bad obj vnum in limits: %d",vnum);
					} else {
						pObjIndex = get_obj_index(vnum);
						if (IS_OBJ_STAT(pObjIndex,ITEM_RARE)|| IS_OBJ_STAT(pObjIndex,ITEM_UNIQUE)) {
							pObjIndex->count += counter;
							found = true;
						}
					}
				}
			}
		}
	}

	fclose(corpse_file);
	corpse_file = NULL;
	return found;
}