ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/gods.C
(Generate patch)

Comparing deliantra/server/server/gods.C (file contents):
Revision 1.40 by root, Mon Sep 29 10:32:50 2008 UTC vs.
Revision 1.41 by root, Wed Dec 31 17:35:37 2008 UTC

38 * Returns the id of specified god. 38 * Returns the id of specified god.
39 */ 39 */
40int 40int
41lookup_god_by_name (const char *name) 41lookup_god_by_name (const char *name)
42{ 42{
43 int godnr = -1; 43 shstr_cmp sname (name);
44 size_t nmlen = strlen (name);
45 44
46 if (name && strcmp (name, "none")) 45 if (sname)
47 {
48 godlink *gl;
49
50 for (gl = first_god; gl; gl = gl->next) 46 for (godlink *gl = first_god; gl; gl = gl->next)
51 if (!strncmp (name, gl->name, MIN ((size_t) strlen (gl->name), nmlen))) 47 if (gl->name == name)
52 break;
53
54 if (gl)
55 godnr = gl->id; 48 return gl->id;
56 }
57 49
58 return godnr; 50 return -1;
59} 51}
60 52
61/** 53/**
62 * Returns pointer to specified god's object through pntr_to_god_obj.. 54 * Returns pointer to specified god's object through pntr_to_god_obj..
63 */ 55 */
64object * 56object *
65find_god (const char *name) 57find_god (const char *name)
66{ 58{
67 object *god = NULL;
68
69 if (name) 59 if (name)
70 {
71 godlink *gl;
72
73 for (gl = first_god; gl; gl = gl->next) 60 for (godlink *gl = first_god; gl; gl = gl->next)
74 if (!strcmp (name, gl->name)) 61 if (!strcmp (name, gl->name))
75 break;
76 if (gl)
77 god = pntr_to_god_obj (gl); 62 return pntr_to_god_obj (gl);
78 } 63
79 return god; 64 return 0;
80} 65}
81 66
82/** 67/**
83 * Determines if op worships a god. 68 * Determines if op worships a god.
84 * Returns the godname if they do or "none" if they have no god. 69 * Returns the godname if they do or "none" if they have no god.
85 * In the case of an NPC, if they have no god, we try and guess 70 * In the case of an NPC, if they have no god, we try and guess
86 * who they should worship based on their race. If that fails we 71 * who they should worship based on their race. If that fails we
87 * give them a random one. 72 * give them a random one.
88 */ 73 */
89 74shstr_tmp
90const char *
91determine_god (object *op) 75determine_god (object *op)
92{ 76{
93 int godnr = -1;
94 const char *godname;
95
96 /* spells */ 77 /* spells */
97 if ((op->type == SPELL || op->type == SPELL_EFFECT) && op->title) 78 if ((op->type == SPELL || op->type == SPELL_EFFECT) && op->title)
98 {
99 if (lookup_god_by_name (op->title) >= 0) 79 if (lookup_god_by_name (op->title) >= 0)
100 return op->title; 80 return op->title;
81
82 if (!op->is_player () && op->flag [FLAG_ALIVE])
101 } 83 {
102
103 if (op->type != PLAYER && QUERY_FLAG (op, FLAG_ALIVE))
104 {
105
106 /* find a god based on race */ 84 /* find a god based on race */
107 if (!op->title) 85 if (!op->title)
108 {
109 if (op->race)
110 {
111 godname = get_god_for_race (op->race); 86 op->title = get_god_for_race (op->race);
112
113 if (godname)
114 op->title = godname;
115 }
116 }
117 87
118 /* find a random god */ 88 /* find a random god */
119 if (!op->title) 89 if (!op->title)
120 { 90 {
121 godlink *gl = first_god;
122
123 godnr = rndm (1, gl->id); 91 int godnr = rndm (1, first_god->id);
124 while (gl) 92
125 { 93 for (godlink *gl = first_god; gl && gl->id != godnr; gl = gl->next)
126 if (gl->id == godnr) 94 if (gl->id == godnr)
95 {
96 op->title = gl->name;
127 break; 97 break;
128
129 gl = gl->next;
130 } 98 }
131
132 op->title = gl->name;
133 } 99 }
134 100
135 return op->title; 101 return op->title;
136 } 102 }
137
138 103
139 /* The god the player worships is in the praying skill (native skill 104 /* The god the player worships is in the praying skill (native skill
140 * not skill tool). Since a player can only have one instance of 105 * not skill tool). Since a player can only have one instance of
141 * that skill, once we find it, we can return, either with the 106 * that skill, once we find it, we can return, either with the
142 * title or "none". 107 * title or "none".
143 */ 108 */
144 if (op->type == PLAYER) 109 if (op->type == PLAYER)
145 {
146 for (object *tmp = op->inv; tmp; tmp = tmp->below) 110 for (object *tmp = op->inv; tmp; tmp = tmp->below)
147 if (tmp->type == SKILL && tmp->subtype == SK_PRAYING) 111 if (tmp->type == SKILL && tmp->subtype == SK_PRAYING)
148 { 112 {
149 if (tmp->title) 113 if (tmp->title)
150 return tmp->title; 114 return tmp->title;
151 else 115
152 return "none"; 116 break;
153 } 117 }
154 }
155 118
156 return "none"; 119 return shstr_none;
157} 120}
158 121
159/** 122/**
160 * Returns 1 if s1 and s2 are the same - either both NULL, or strcmp( ) == 0 123 * Returns 1 if s1 and s2 are the same - either both NULL, or strcmp( ) == 0
161 */ 124 */
162static int 125static int
163same_string (const char *s1, const char *s2) 126same_string (const char *s1, const char *s2)
164{ 127{
165 if (s1 == NULL) 128 if (s1 == s2)
166 if (s2 == NULL)
167 return 1; 129 return 1;
130 else if (s1 && s2)
131 return !strcmp (s1, s2);
168 else 132 else
169 return 0;
170 else if (s2 == NULL)
171 return 0; 133 return 0;
172 else
173 return strcmp (s1, s2) == 0;
174} 134}
175 135
176/** 136/**
177 * Checks for any occurrence of the given 'item' in the inventory of 'op' (recursively). 137 * Checks for any occurrence of the given 'item' in the inventory of 'op' (recursively).
178 * Any matching items in the inventory are deleted, and a 138 * Any matching items in the inventory are deleted, and a
1095 */ 1055 */
1096 1056
1097int 1057int
1098get_god (object *priest) 1058get_god (object *priest)
1099{ 1059{
1100 int godnr = lookup_god_by_name (determine_god (priest)); 1060 return lookup_god_by_name (determine_god (priest));
1101
1102 return godnr;
1103} 1061}
1104 1062
1105/** 1063/**
1106 * Returns a string that is the name of the god that should be natively worshipped by a 1064 * Returns a string that is the name of the god that should be natively worshipped by a
1107 * creature of who has race *race 1065 * creature of who has race *race
1108 * if we can't find a god that is appropriate, we return NULL 1066 * if we can't find a god that is appropriate, we return NULL
1109 */ 1067 */
1110const char * 1068shstr_tmp
1111get_god_for_race (const char *race) 1069get_god_for_race (const char *race)
1112{ 1070{
1113 godlink *gl = first_god; 1071 if (race)
1114 const char *godname = NULL; 1072 for (godlink *gl = first_god; gl; gl = gl->next)
1115
1116 if (race == NULL)
1117 return NULL;
1118 while (gl)
1119 {
1120 if (!strcasecmp (gl->arch->race, race)) 1073 if (!strcasecmp (gl->arch->race, race))
1121 { 1074 return gl->name;
1122 godname = gl->name; 1075
1123 break; 1076 return shstr_null;
1124 }
1125 gl = gl->next;
1126 }
1127 return godname;
1128} 1077}
1129 1078
1130/** 1079/**
1131 * Changes the attributes of cone, smite, and ball spells as needed by the code. 1080 * Changes the attributes of cone, smite, and ball spells as needed by the code.
1132 * Returns false if there was no race to assign to the slaying field of the spell, but 1081 * Returns false if there was no race to assign to the slaying field of the spell, but

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines