Atrinik Server  4.0
material.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
32 #include <global.h>
33 
38  {"paper"},
39  {"metal"},
40  {"crystal"},
41  {"leather"},
42  {"wood"},
43  {"organics"},
44  {"stone"},
45  {"cloth"},
46  {"magic material"},
47  {"liquid"},
48  {"soft metal"},
49  {"bone"},
50  {"ice"},
51 };
52 
56 material_real_t materials_real[NUM_MATERIALS_REAL];
57 
61 void
63 {
64  /* First initialize default values to the array */
65  for (int i = 0; i < NUM_MATERIALS_REAL; i++) {
66  materials_real[i].name[0] = '\0';
67  materials_real[i].quality = 100;
68  materials_real[i].type = M_NONE;
69  materials_real[i].def_race = RACE_TYPE_NONE;
70  }
71 
72  char filename[MAX_BUF];
73  snprintf(VS(filename), "%s/materials", settings.libpath);
74 
75  FILE *fp = fopen(filename, "r");
76  if (fp == NULL) {
77  LOG(ERROR,
78  "Could not open materials file %s: %s",
79  filename, strerror(errno));
80  exit(1);
81  }
82 
83  char buf[MAX_BUF];
84  while (fgets(VS(buf), fp)) {
85  if (*buf == '#' || *buf == '\n') {
86  continue;
87  }
88 
89  int i;
90  if (sscanf(buf, "material_real %d\n", &i) != 1) {
91  LOG(ERROR, "Bogus line in materials file: %s", buf);
92  exit(1);
93  }
94 
95  if (i < 0 || i >= NUM_MATERIALS_REAL) {
96  LOG(ERROR,
97  "Materials file contains declaration for material #%d but it "
98  "doesn't exist.", i);
99  exit(1);
100  }
101 
102  int def_race = RACE_TYPE_NONE, type = M_NONE, quality = 100;
103  char name[MAX_BUF] = {'\0'};
104 
105  while (fgets(VS(buf), fp)) {
106  if (strcmp(buf, "end\n") == 0) {
107  break;
108  }
109 
110  if (sscanf(buf, "quality %d\n", &quality) != 1 &&
111  sscanf(buf, "type %d\n", &type) != 1 &&
112  sscanf(buf, "def_race %d\n", &def_race) != 1 &&
113  sscanf(buf, "name %[^\n]", name) != 1) {
114  LOG(ERROR, "Bogus line in materials file: %s", buf);
115  exit(1);
116  }
117  }
118 
119  if (*name != '\0') {
120  snprintf(VS(materials_real[i].name), "%s ", name);
121  }
122 
123  materials_real[i].quality = quality;
124  materials_real[i].type = type;
125  materials_real[i].def_race = def_race;
126  }
127 
128  fclose(fp);
129 }
char name[MAX_BUF]
Definition: material.h:115
uint8_t type
One of operation types.
Definition: sound_ambient.c:45
material_t materials[NROFMATERIALS]
Definition: material.c:37
#define NROFMATERIALS
Definition: material.h:34
#define M_NONE
Definition: material.h:46
char libpath[MAX_BUF]
Definition: global.h:338
material_real_t materials_real[NUM_MATERIALS_REAL]
Definition: material.c:56
void material_init(void)
Definition: material.c:62
#define RACE_TYPE_NONE
Definition: race.h:67
struct settings_struct settings
Definition: init.c:55