OSDN Git Service

Nazghul-0.7.1
[nazghul-jp/nazghul-jp.git] / src / skill_set.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_set.h"
24 #include "skill_set_entry.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
34 static void skill_set_del(struct skill_set *skset)
35 {
36         struct list *list;
37
38         assert(!skset->refcount);
39
40         if (skset->name) {
41                 free(skset->name);
42         }
43         
44         list = skset->skills.next;
45         while (list != &skset->skills) {
46                 struct skill_set_entry *ssent;
47
48                 ssent = list_entry(list, struct skill_set_entry, list);
49                 list = list->next;
50                 list_remove(&ssent->list);
51                 skill_set_entry_unref(ssent);
52         }
53
54         free(skset);
55 }
56
57 struct skill_set *skill_set_new(void)
58 {
59         struct skill_set *skset = (struct skill_set*)calloc(1, sizeof(*skset));
60         assert(skset);
61         list_init(&skset->list);
62         list_init(&skset->skills);
63         skset->refcount = 1;
64         return skset;
65 }
66
67 void skill_set_set_name(struct skill_set *skset, char *val)
68 {
69         assert(val);
70         if (skset->name) {
71                 free(skset->name);
72         }
73         skset->name = strdup(val);
74         assert(skset->name);
75 }
76
77 void skill_set_ref(struct skill_set *skset)
78 {
79         skset->refcount++;
80 }
81
82 void skill_set_unref(struct skill_set *skset)
83 {
84         assert(skset->refcount > 0);
85         skset->refcount--;
86         if (!skset->refcount) {
87                 skill_set_del(skset);
88         }
89 }
90
91 void skill_set_add_skill(struct skill_set *skset, struct skill *skill,
92                          int level)
93 {
94         struct skill_set_entry *ssent = skill_set_entry_new(skill, level);
95         assert(ssent);
96         list_add_tail(&skset->skills, &ssent->list);
97 }