summaryrefslogtreecommitdiff
path: root/src/spell_type.cc
blob: 867068f8012b3be0e3c4fbdf8ed4833efcdd0de8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "spell_type.hpp"
#include "range.h"
#include "device_allocation.h"
#include "dice.h"
#include "spells4.hpp"

#include "angband.h"

#include <cassert>
#include <vector>
#include <string>
#include <type_traits>

#define SCHOOL_IDXS_MAX 3

/**
 * Spell type definition.
 */
struct spell_type
{
	cptr name;                      /* Name */
	byte skill_level;               /* Required level (to learn) */
	std::vector<std::string> m_description;       /* List of strings */

	casting_result (*effect_func)();  /* Spell effect function */
	const char* (*info_func)();           /* Information function */
	int (*lasting_func)();         /* Lasting effect function */
	bool_ (*depend_func)();         /* Check dependencies */

	s16b minimum_pval;              /* Minimum required pval for item-based spells */

	enum casting_type casting_type; /* Type of casting required */
	s16b         casting_stat;      /* Stat used for casting */

	bool_        castable_while_blind;
	bool_        castable_while_confused;

	dice_type          device_charges;      /* Number of charges for devices */
	std::vector<device_allocation *> m_device_allocation;	/* Allocation table for devices */

	s16b         random_type;       /* Type of random items in which skill may appear */

	s32b         failure_rate;      /* Failure rate */

	s32b         inertia_difficulty; /* Mana cost when used in Inertia Control */
	s32b         inertia_delay;      /* Delay between castings */

	range_type   mana_range;

	size_t school_idxs_count;
	s32b school_idxs[3];

public:

	spell_type(cptr _name)
		: name(_name)
		, skill_level(0)
		, m_description()
		, effect_func(nullptr)
		, info_func(nullptr)
		, lasting_func(nullptr)
		, depend_func(nullptr)
		, minimum_pval(0)
		, casting_type(USE_SPELL_POINTS)
		, casting_stat(0)
		, castable_while_blind(FALSE)
		, castable_while_confused(FALSE)
		, device_charges({ 0, 0, 0 })
		, m_device_allocation()
		, random_type(-1)
		, failure_rate(0)
		, inertia_difficulty(-1)
		, inertia_delay(-1)
		, mana_range({ -1, -1 })
		, school_idxs_count(0)
		, school_idxs{ -1, -1, -1 }
	{
	}

};

static void school_idx_add_new(spell_type *spell, s32b i)
{
	assert(spell != NULL);
	assert(spell->school_idxs_count < SCHOOL_IDXS_MAX);

	spell->school_idxs[spell->school_idxs_count] = i;
	spell->school_idxs_count++;
}

void spell_type_set_inertia(spell_type *spell, s32b difficulty, s32b delay)
{
	assert(spell != NULL);
	spell->inertia_difficulty = difficulty;
	spell->inertia_delay = delay;
}

void spell_type_init_music(spell_type *spell,
			   s16b minimum_pval,
			   const char* (*info_func)(),
			   casting_result (*effect_func)())
{
	assert(spell != NULL);

	/* Set up callbacks */
	spell->info_func = info_func;
	spell->effect_func = effect_func;

	/* Use spell points, but CHR for success/failure calculations */
	spell->casting_type = USE_SPELL_POINTS;
	spell->casting_stat = A_CHR;
	spell->random_type = SKILL_MUSIC;
	spell->minimum_pval = minimum_pval;
	/* Add school */
	school_idx_add_new(spell, SCHOOL_MUSIC);
}

void spell_type_init_music_lasting(spell_type *spell,
				   s16b minimum_pval,
				   const char* (*info_func)(),
				   casting_result (*effect_func)(),
				   int (*lasting_func)())
{
	spell_type_init_music(
		spell,
		minimum_pval,
		info_func,
		effect_func);

	spell->lasting_func = lasting_func;
}

void spell_type_init_mage(spell_type *spell,
			  random_type random_type,
			  s32b school_idx,
			  const char* (*info_func)(),
			  casting_result (*effect_func)())
{
	assert(spell != NULL);

	spell->info_func = info_func;
	spell->effect_func = effect_func;

	spell->casting_type = USE_SPELL_POINTS;
	spell->casting_stat = A_INT;

	switch (random_type)
	{
	case RANDOM:
		spell->random_type = SKILL_MAGIC;
		break;
	case NO_RANDOM:
		spell->random_type = -1;
		break;
	default:
		/* Cannot happen */
		assert(FALSE);
	}

	/* Add first school */
	spell_type_add_school(spell, school_idx);
}

void spell_type_init_priest(spell_type *spell,
			    s32b school_idx,
			    const char* (*info_func)(),
			    casting_result (*effect_func)())
{
	assert(spell != NULL);

	spell->info_func = info_func;
	spell->effect_func = effect_func;

	spell->random_type = SKILL_SPIRITUALITY;
	spell->casting_type = USE_PIETY;
	spell->casting_stat = A_WIS;

	school_idx_add_new(spell, school_idx);
}

void spell_type_init_device(spell_type *spell,
			    const char* (*info_func)(),
			    casting_result (*effect_func)())
{
	assert(spell != NULL);

	spell_type_init_mage(spell,
			     NO_RANDOM,
			     SCHOOL_DEVICE,
			     info_func,
			     effect_func);
}

void spell_type_init_demonology(spell_type *spell,
				const char* (*info_func)(),
				casting_result (*effect_func)())
{
	spell_type_init_mage(spell,
			     NO_RANDOM,
			     SCHOOL_DEMON,
			     info_func,
			     effect_func);
}

void spell_type_init_geomancy(spell_type *spell,
			      const char* (*info_func)(),
			      casting_result (*effect_func)(),
			      bool_ (*depend_func)())
{
	spell_type_init_mage(spell,
			     NO_RANDOM,
			     SCHOOL_GEOMANCY,
			     info_func,
			     effect_func);

	spell->depend_func = depend_func;
}

void spell_type_set_difficulty(spell_type *spell, byte skill_level, s32b failure_rate)
{
	assert(spell != NULL);

	spell->skill_level = skill_level;
	spell->failure_rate = failure_rate;
}

void spell_type_set_mana(spell_type *spell, s32b min, s32b max)
{
	assert(spell != NULL);

	range_init(&spell->mana_range, min, max);
}

void spell_type_set_castable_while_blind(spell_type *spell, bool_ value)
{
	assert(spell != NULL);

	spell->castable_while_blind = value;
}

void spell_type_set_castable_while_confused(spell_type *spell, bool_ value)
{
	assert(spell != NULL);

	spell->castable_while_confused = value;
}

void spell_type_describe(spell_type *spell, cptr line)
{
	assert(spell != NULL);

	spell->m_description.push_back(std::string(line));
}

void spell_type_add_school(spell_type *spell, s32b school_idx)
{
	school_idx_add_new(spell, school_idx);
}

void spell_type_set_device_charges(spell_type *spell, cptr charges_s)
{
	assert(spell != NULL);

	dice_parse_checked(&spell->device_charges, charges_s);
}

void spell_type_add_device_allocation(spell_type *spell, struct device_allocation *a)
{
	assert(spell != NULL);
	assert(a != NULL);
	spell->m_device_allocation.push_back(a);
}

spell_type *spell_type_new(cptr name)
{
	spell_type *spell = new spell_type(name);
	assert(spell != NULL);
	return spell;
}

int spell_type_produce_effect_lasting(spell_type *spell)
{
	assert(spell->lasting_func != NULL);
	return spell->lasting_func();
}

casting_result spell_type_produce_effect(spell_type *spell)
{
	assert(spell->effect_func != NULL);
	return spell->effect_func();
}

cptr spell_type_name(spell_type *spell)
{
	assert(spell != NULL);

	return spell->name;
}

int spell_type_skill_level(spell_type *spell)
{
	assert(spell != NULL);

	return spell->skill_level;
}

void spell_type_description_foreach(spell_type *spell, std::function<void (std::string const &text)> callback)
{
	for (auto line: spell->m_description)
	{
		callback(line);
	}
}

long spell_type_roll_charges(spell_type *spell)
{
	return dice_roll(&spell->device_charges);
}

device_allocation *spell_type_device_allocation(spell_type *spell, byte tval)
{
	for (auto device_allocation_ptr: spell->m_device_allocation)
	{
		if (device_allocation_ptr->tval == tval)
		{
			return device_allocation_ptr;
		}
	}

	return NULL;
}

bool_ spell_type_uses_piety_to_cast(spell_type *spell)
{
	assert(spell != NULL);
	return spell->casting_type == USE_PIETY;
}

bool_ spell_type_castable_while_blind(spell_type *spell)
{
	assert(spell != NULL);
	return spell->castable_while_blind;
}

bool_ spell_type_castable_while_confused(spell_type *spell)
{
	assert(spell != NULL);
	return spell->castable_while_confused;
}

s16b spell_type_minimum_pval(spell_type *spell)
{
	return spell->minimum_pval;
}

s16b spell_type_random_type(spell_type *spell)
{
	return spell->random_type;
}

std::vector<s32b> const spell_type_get_schools(spell_type *spell)
{
	std::vector<s32b> school_idxs;
	for (size_t i = 0; i < spell->school_idxs_count; i++)
	{
		school_idxs.push_back(spell->school_idxs[i]);
	}
	return school_idxs;
}

bool_ spell_type_inertia(spell_type *spell, s32b *difficulty, s32b *delay)
{
	if ((spell->inertia_difficulty < 0) ||
	    (spell->inertia_delay < 0))
	{
		return FALSE;
	}

	if (difficulty != NULL)
	{
		*difficulty = spell->inertia_difficulty;
	}

	if (delay != NULL)
	{
		*delay = spell->inertia_delay;
	}

	return TRUE;
}

cptr spell_type_info(spell_type *spell)
{
	assert(spell != NULL);
	
	return spell->info_func();
}

s32b spell_type_failure_rate(spell_type *spell)
{
	assert(spell != NULL);

	return spell->failure_rate;
}

s16b spell_type_casting_stat(spell_type *spell)
{
	assert(spell != NULL);

	return spell->casting_stat;
}

void spell_type_mana_range(spell_type *spell, range_type *range)
{
	assert(spell != NULL);

	if (range != NULL)
	{
		*range = spell->mana_range;
	}
}

bool_ spell_type_dependencies_satisfied(spell_type *spell)
{
	assert(spell != NULL);

	if (spell->depend_func != NULL) {
		return spell->depend_func();
	} else {
		return TRUE;
	}
}