1 //
2 // nazghul - an old-school RPG engine
3 // Copyright (C) 2002, 2003 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 "gob.h"
24 #include "occ.h"
25 #include "object.h"
26 #include "sprite.h"
27 #include "common.h"
28 #include "skill_set.h"
29 
30 #include <string.h>
31 #include <stdlib.h>
32 
occ_del(struct occ * occ)33 static void occ_del(struct occ *occ)
34 {
35 	if (!occ)
36 		return;
37 	if (occ->tag)
38 		free(occ->tag);
39 	if (occ->name)
40 		free(occ->name);
41         if (occ->gob)
42                 gob_del(occ->gob);
43         if (occ->skills)
44                 skill_set_unref(occ->skills);
45         free(occ);
46 }
47 
occ_new(const char * tag,const char * name,float magic,int hp_mod,int hp_mult,int mp_mod,int mp_mult,int hit_mod,int def_mod,int dam_mod,int arm_mod)48 extern struct occ *occ_new(const char *tag,
49                            const char *name,
50                            float magic,
51                            int hp_mod,
52                            int hp_mult,
53                            int mp_mod,
54                            int mp_mult,
55                            int hit_mod,
56                            int def_mod,
57                            int dam_mod,
58                            int arm_mod)
59 {
60 	struct occ *occ = 0;
61 
62 	occ = (struct occ*)calloc(1, sizeof(*occ));;
63         assert(occ);
64 
65         occ->tag = strdup(tag);
66         assert(occ->tag);
67 
68         occ->name = strdup(name);
69         assert(occ->name);
70 
71         occ->magic = magic;
72         occ->hp_mod = hp_mod;
73         occ->hp_mult = hp_mult;
74         occ->mp_mod = mp_mod;
75         occ->mp_mult = mp_mult;
76         occ->hit_mod = hit_mod;
77         occ->def_mod = def_mod;
78         occ->dam_mod = dam_mod;
79         occ->arm_mod = arm_mod;
80 
81         assert(occ->tag);
82         assert(occ->name);
83 
84         return occ;
85 }
86 
occ_unref(struct occ * occ)87 extern void occ_unref(struct occ* occ)
88 {
89         assert(occ->refcount > 0);
90         occ->refcount--;
91         if (! occ->refcount)
92                 occ_del(occ);
93 }
94 
occ_set_skills(struct occ * occ,struct skill_set * skills)95 void occ_set_skills(struct occ *occ, struct skill_set *skills)
96 {
97         if (occ->skills) {
98                 skill_set_unref(skills);
99                 occ->skills = 0;
100         }
101 
102         if (skills) {
103                 skill_set_ref(skills);
104                 occ->skills = skills;
105         }
106 }
107