Debian Codegraph @

modules/gameserv/gamecalc.c

raw
1 /*
2 * Copyright (c) 2005-2007 William Pitcock <[email protected]>
3 * Copyright (c) 2006-2007 Jilles Tjoelker <[email protected]>
4 *
5 * Rights to this code are documented in doc/LICENSE.
6 *
7 * Dice generator.
8 *
9 */
10
11 #include "atheme.h"
12 #include "gameserv_common.h"
13
14 DECLARE_MODULE_V1
15 (
16 "gameserv/gamecalc", false, _modinit, _moddeinit,
17 PACKAGE_STRING,
18 VENDOR_STRING
19 );
20
21 static void command_wod(sourceinfo_t *si, int parc, char *parv[]);
22 static void command_nwod(sourceinfo_t *si, int parc, char *parv[]);
23 static void command_df(sourceinfo_t *si, int parc, char *parv[]);
24
25 command_t cmd_wod = { "WOD", N_("WOD-style dice generation."), AC_NONE, 7, command_wod, { .path = "gameserv/roll" } };
26 command_t cmd_nwod = { "NWOD", N_("New WOD-style dice generation."), AC_NONE, 7, command_nwod, { .path = "gameserv/roll" } };
27 command_t cmd_df = { "DF", N_("Fudge-style dice generation."), AC_NONE, 2, command_df, { .path = "gameserv/roll" } };
28
29 void _modinit(module_t * m)
30 {
31 service_named_bind_command("gameserv", &cmd_wod);
32 service_named_bind_command("gameserv", &cmd_nwod);
33 service_named_bind_command("gameserv", &cmd_df);
34
35 service_named_bind_command("chanserv", &cmd_wod);
36 service_named_bind_command("chanserv", &cmd_nwod);
37 service_named_bind_command("chanserv", &cmd_df);
38 }
39
40 void _moddeinit(module_unload_intent_t intent)
41 {
42 service_named_unbind_command("gameserv", &cmd_wod);
43 service_named_unbind_command("gameserv", &cmd_nwod);
44 service_named_unbind_command("gameserv", &cmd_df);
45
46 service_named_unbind_command("chanserv", &cmd_wod);
47 service_named_unbind_command("chanserv", &cmd_nwod);
48 service_named_unbind_command("chanserv", &cmd_df);
49 }
50
51 static void command_wod(sourceinfo_t *si, int parc, char *parv[])
52 {
53 mychan_t *mc;
54 char *arg_dice, *arg_difficulty;
55 int ii = 0;
56 int dice, difficulty;
57 int roll, total, roll_count = 0, i;
58 int success = 0, failure = 0, botches = 0, rerolls = 0;
59 static char buf[BUFSIZE];
60 char *end_p;
61
62 if (!gs_do_parameters(si, &parc, &parv, &mc))
63 return;
64 if (parc < 2)
65 {
66 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "WOD");
67 command_fail(si, fault_needmoreparams, _("Syntax: WOD <dice> <difficulty>"));
68 return;
69 }
70 arg_dice = parv[ii++];
71 arg_difficulty = parv[ii++];
72
73 while (roll_count < 3 && arg_dice != NULL && arg_difficulty != NULL)
74 {
75 success = 0, failure = 0, botches = 0, rerolls = 0;
76 roll_count++;
77
78 dice = atoi(arg_dice);
79 difficulty = atoi(arg_difficulty);
80
81 if (dice > 30 || dice < 1)
82 {
83 command_fail(si, fault_badparams, _("Only 1-30 dice may be thrown at one time."));
84 return;
85 }
86 else if (difficulty > 10 || difficulty < 1)
87 {
88 command_fail(si, fault_badparams, _("Difficulty setting must be between 1 and 10."));
89 return;
90 }
91 else
92 {
93 end_p = buf;
94
95 for (i = 0; i < dice; i++)
96 {
97 roll = (arc4random() % 10) + 1;
98
99 end_p += snprintf(end_p, BUFSIZE - (end_p - buf), "%d ", roll);
100
101 if (roll == 1)
102 {
103 botches++;
104 continue;
105 }
106 else if (roll == 10)
107 rerolls++;
108
109 if (roll >= difficulty)
110 success++;
111 else
112 failure++;
113 }
114
115 rerolls = rerolls - botches;
116 total = success - botches;
117
118 gs_command_report(si, _("%s rolls %d dice at difficulty %d: %s"), si->su->nick, dice, difficulty, buf);
119
120 if (rerolls > 0)
121 gs_command_report(si, _("Successes: %d, Failures: %d, Botches: %d, Total: %d. You may reroll %d if you have a specialty."),
122 success, failure, botches, total, rerolls);
123 else
124 gs_command_report(si, _("Successes: %d, Failures: %d, Botches: %d, Total: %d."),
125 success, failure, botches, total);
126 }
127
128 /* prepare for another go. */
129 arg_dice = parv[ii++];
130 arg_difficulty = parv[ii++];
131 }
132 }
133
134 static void command_nwod(sourceinfo_t *si, int parc, char *parv[])
135 {
136 mychan_t *mc;
137 char *arg_dice, *arg_rerollflag;
138 int ii = 0;
139 int dice, reroll;
140 int roll, total, roll_count = 0, i;
141 int success = 0, failure = 0, botches = 0, rerolls = 0;
142 static char buf[BUFSIZE];
143 char *end_p;
144
145 if (!gs_do_parameters(si, &parc, &parv, &mc))
146 return;
147 if (parc < 2)
148 {
149 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "NWOD");
150 command_fail(si, fault_needmoreparams, _("Syntax: NWOD [-chance] <dice> [-reroll] [reroll]"));
151 return;
152 }
153 arg_dice = parv[ii++];
154 arg_rerollflag = parv[ii++];
155
156 while (roll_count < 3 && arg_dice != NULL)
157 {
158 success = 0, failure = 0, botches = 0, rerolls = 0;
159 roll_count++;
160
161 dice = atoi(arg_dice);
162
163 if (dice == 0 && !strcasecmp(arg_dice, "-chance"))
164 {
165 roll = (arc4random() % 10) + 1;
166
167 if (roll == 1)
168 botches++;
169
170 if (roll == 10)
171 success++;
172
173 gs_command_report(si, _("%s rolls a chance die: %d"), si->su->nick, roll);
174 gs_command_report(si, _("Successes: %d, Failures: %d, Botches: %d."), success, failure, botches);
175 return;
176 }
177
178 if (dice > 30 || dice < 1)
179 {
180 command_fail(si, fault_badparams, _("Only 1-30 dice may be thrown at one time."));
181 return;
182 }
183
184 if (arg_rerollflag != NULL && !strcasecmp(arg_rerollflag, "-reroll") && parv[ii + 1] != NULL)
185 reroll = atoi(parv[ii++]);
186 else
187 reroll = 10;
188
189 {
190 end_p = buf;
191
192 for (i = 0; i < dice; i++)
193 {
194 roll = (arc4random() % 10) + 1;
195
196 end_p += snprintf(end_p, BUFSIZE - (end_p - buf), "%d ", roll);
197
198 if (roll == 1)
199 {
200 botches++;
201 continue;
202 }
203 else if (roll >= reroll)
204 rerolls++;
205
206 if (roll >= 8)
207 success++;
208 else
209 failure++;
210 }
211
212 rerolls = rerolls - botches;
213 total = success - botches;
214
215 gs_command_report(si, _("%s rolls %d dice: %s"), si->su->nick, dice, buf);
216
217 if (rerolls > 0)
218 gs_command_report(si, _("Successes: %d, Failures: %d, Botches: %d, Total: %d. You may reroll %d."),
219 success, failure, botches, total, rerolls);
220 else
221 gs_command_report(si, _("Successes: %d, Failures: %d, Botches: %d, Total: %d."),
222 success, failure, botches, total);
223 }
224
225 /* prepare for another go. */
226 arg_dice = parv[ii++];
227 arg_rerollflag = parv[ii++];
228 }
229 }
230
231 static const char *df_dice_table[3] = { "[-]", "[ ]", "[+]" };
232
233 static void command_df(sourceinfo_t *si, int parc, char *parv[])
234 {
235 mychan_t *mc;
236 char *arg_dice;
237 char buf[BUFSIZE];
238 int i, dice;
239
240 if (!gs_do_parameters(si, &parc, &parv, &mc))
241 return;
242 if (parc < 1)
243 {
244 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "DF");
245 command_fail(si, fault_needmoreparams, _("Syntax: DF <dice>"));
246 return;
247 }
248 arg_dice = parv[0];
249
250 dice = atoi(arg_dice);
251 *buf = '\0';
252
253 if (dice > 30 || dice < 1)
254 {
255 command_fail(si, fault_badparams, _("Only 1-30 dice may be thrown at one time."));
256 return;
257 }
258
259 for (i = 0; i < dice; i++)
260 {
261 int roll = arc4random() % 3;
262
263 if (*buf != '\0')
264 mowgli_strlcat(buf, df_dice_table[roll], BUFSIZE);
265 else
266 mowgli_strlcpy(buf, df_dice_table[roll], BUFSIZE);
267 }
268
269 gs_command_report(si, _("Result: %s"), buf);
270 }
271
272 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
273 * vim:ts=8
274 * vim:sw=8
275 * vim:noexpandtab
276 */