OSDN Git Service

日本語版
[nazghul-jp/nazghul-jp.git] / src / skill.c
1 /*
2  * nazghul - an old-school RPG engine
3  * Copyright (C) 2007 Gordon McNutt
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Foundation, Inc., 59 Temple Place,
17  * Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Gordon McNutt
20  * gmcnutt@users.sourceforge.net
21  */
22
23 #include "skill.h"
24 #include "closure.h"
25
26 #include <assert.h>
27 #include <config.h>
28 //#ifdef HAVE_MALLOC_H
29 #include <malloc.h>
30 //#endif
31 #include <string.h>
32
33 static void skill_del(struct skill *skill)
34 {
35         struct list *elem;
36
37         assert(!skill->refcount);
38         if (skill->name) {
39                 free(skill->name);
40         }
41         if (skill->desc) {
42                 free(skill->desc);
43         }
44         if (skill->yuse) {
45                 closure_unref(skill->yuse);
46         }
47         if (skill->can_yuse) {
48                 closure_unref(skill->can_yuse);
49         }
50
51         /* delete the list of tools */
52         node_list_unlink_and_unref(&skill->tools);
53
54         /* delete the list of materials */
55         elem = skill->materials.next;
56         while (elem != &skill->materials) {
57                 struct list *tmp = elem->next;
58                 free(elem);
59                 elem = tmp;
60         }
61
62         free(skill);
63 }
64
65 struct skill *skill_new(void)
66 {
67         struct skill *skill = (struct skill*)calloc(1, sizeof(*skill));
68         assert(skill);
69         list_init(&skill->list);
70         node_init(&skill->tools);
71         list_init(&skill->materials);
72         skill->refcount = 1;
73         return skill;
74 }
75
76 void skill_set_name(struct skill *skill, char *val)
77 {
78         assert(val);
79         if (skill->name) {
80                 free(skill->name);
81         }
82         skill->name = strdup(val);
83         assert(skill->name);
84 }
85
86 void skill_set_desc(struct skill *skill, char *val)
87 {
88         assert(val);
89         if (skill->desc) {
90                 free(skill->desc);
91         }
92         skill->desc = strdup(val);
93         assert(skill->name);
94 }
95
96 void skill_ref(struct skill *skill)
97 {
98         skill->refcount++;
99 }
100
101 void skill_unref(struct skill *skill)
102 {
103         assert(skill->refcount > 0);
104         skill->refcount--;
105         if (!skill->refcount) {
106                 skill_del(skill);
107         }
108 }
109
110 void skill_add_tool(struct skill *skill, void *objtype)
111 {
112         struct node *node = node_new(objtype);
113         node_add(&skill->tools, node);
114 }
115
116 void skill_add_material(struct skill *skill, void *objtype, int quan)
117 {
118         struct skill_material *mat;
119         mat = (struct skill_material*)calloc(1, sizeof(*mat));
120         assert(mat);
121         mat->objtype = objtype;
122         mat->quantity = quan;
123         list_add(&skill->materials, &mat->list);
124 }