Debian Codegraph @

doc/Developers/spells

raw
1 SPELLS:
2 ------------------------------------------------------------------------------
3 Index:
4
5 1. Background
6 2. Spell Objects
7 3. Spell Paths
8 4. Spell Subtypes
9 5. Spell Casting Objects
10 6. Arch Directory Layout
11 7. Programming Notes
12 8. Abilities
13 9. Spell Merging
14
15 ------------------------------------------------------------------------------
16 1. BACKGROUND
17
18 This document describe all things spell and spell related that developers
19 need to know. This document outlines the new spell objects and how
20 this all works.
21
22 ------------------------------------------------------------------------------
23 2. SPELL OBJECTS
24
25 In the old model, each spell had a specific number associated with it.
26 Monster spell abilities, while objects, just said to use spell number
27 X. Given that everything else in the game is object, this hardcoding
28 of spells was less than ideal.
29
30 In the new system, spells are objects. The spell object the player knows
31 has the characteristics of the spell. As a first pass, the existing
32 spells are just converted to object form. As time passes, new spells
33 can be easily added in this system. It also simplifies the code - the
34 spells a player knows are now objects in his inventory. The spell
35 a wand casts is the spell in its inventory. The spell a scroll casts
36 is the spell in its inventory, and the spell you learn by reading
37 a spellbook is the spell in its inventory.
38
39 This model makes it very easy to make variations of some spells. Want
40 to make an extra large fireball? All this is needed is to take the
41 large fireball arch, increase a few parameters, and your all set.
42
43 The following is the fields in the object structure, and how they
44 correspond to a spell. Fields that are not mentioned are not used.
45
46 char *name: Name of the spell (large fireball, icestorm, etc)
47
48 char *lore: Description of the spell to be put in books, or perhaps even
49 to just let the player see. In a sense, this puts documentation of
50 the spells within the object, instead of having to maintain separate
51 files.
52
53 sint16 casting_time : How long it takes to cast the spell (was time)
54 This is the number of ticks it takes for the player to cast the spell,
55 and is thus not a fixed time.
56
57 Suggested casting time values:
58 5: Combat spells (cones, bolts, balls, etc - ones you cast when in
59 danger)
60 10-15: Protection spells, other often cast spells. Summoning spells
61 would fall into this category, as they are often cast when not
62 in immediate danger.
63 ~25: Detection spells, or other spells done when times are safe.
64 Many ability improvement spells might fall into this category.
65
66 These are only suggestions - they more or less match when the spell will
67 be used and usefulness. An attack spell that takes forever to cast
68 isn't very useful. There are really two things to keep in mind - when
69 the spell is cast, and how long the spell itself last. A long lasting
70 spell can have a longer casting time, simply because it isn't cast
71 that often and will last for a while (thus, smart players won't cast
72 it in combat).
73
74 face/anim: If the spells is a generic spell that doesn't have an other_arch
75 pointer, this is the face (or animation to use when the spell is
76 cast.
77
78 uint8 type: SPELL (101) (to denote this is spell object)
79 sint16 resist[] values: For protection spells, resistance they provide.
80 uint32 attacktype: Attacktype of the spell.
81
82 uint32 path_*: spell path information. Note as of this writing, only
83 path_attuned is expected to mean anything. However, I can envision
84 the user of path_repelled/denied to denote that if the caster is
85 attuned to some path, they have penalties in casting this spell.
86
87 sint32 value: Base value of this spell for scroll, spellbooks, wands, rods.
88 sint16 level: Level of the spell
89
90 other_arch: archetype this spell uses. In the case of bolts, this
91 is the bolt that is fired.
92
93 char *skill (new): Skill needed to cast this spell.
94
95 sint32 subtype (new): Sub classification for this spell. It sort of
96 relates to the existing spell number, but in a more general form.
97 At first pass, subtypes would fall into protection spells, bolt
98 spells, ball spells, cone spells, stat increase spells. There
99 are a number of spells which can not be abstracted and are unique
100 in their own right (dimension door, word of recall, polymorph, etc).
101 In these cases, there will be one value for that spell type.
102
103 invisible: Always set to 1, so that the player can't directly see it in their
104 inventory.
105
106 no_drop: Always 1, so when a spell object is destroyed, the spell it
107 contains is dropped and not freed.
108
109
110 From living/stats substructure:
111 Str, Dex, etc: If spell changes and ability, this is the ability it changes.
112 sint16 dam: Base damage the spell does:
113 sint16 sp: how many spellpoints the spell costs
114 sint16 grace: how much grace the spell costs
115 sint16 maxsp/maxgrace: Every 'maxsp/maxgrace' levels beyond its base level,
116 the spell cost doubles (this is pro rated like the old code. For example,
117 if a first level spell costs 5 sp, and maxsp is 5, then at level 6 it
118 would cost 10 sp. However, at level 2, it is 6 sp, level 3 is 7 sp,
119 etc.
120 New fields added to object structure:
121
122 uint8 dam_modifier:
123 for every maxdam levels above what is needed to cast the spell, it does an
124 extra point of damage. Note if you want a spell to double its damage
125 every X levels, the maxdam may have to be quite low (1) if the spell does
126 high damage. For example, suppose you have a 10'th level spell that does
127 50 damage. If dam_modifier is 5, then every 5 levels it does one extra
128 point, so at level 25, it would do 53. If dam_modifier is 1, then at
129 level 25 it would do 65.
130
131 sint16 duration: Base duration of the spell (was bdur)
132 uint8 duration_modifier: Modifies duration of spell, acts the same way
133 as dam_modifier.
134
135 uint8 range: Range of the spell - this could be how far a lightning
136 bolt goes, how big that fireball is, etc. Spells with range 0
137 are personal only spells. Spells with range 1 can touch
138 a person on a neighboring space. range will not change based
139 on level of the caster - if higher range is desired, different
140 spell should be made.
141 uint8 range_modifier: Modifies range of spell, acts ame way as dam_modifier
142
143 TBD:
144 There are also many fields in the spell structure which are not
145 copied - charges, scrolls, scroll_chance, books, defensive,
146 cleric, onself.
147
148 The defensive, and onself can largely be derived from the
149 subtype above. if subtype is protection or healing, it'd be
150 self.
151
152 cleric is basically redundant now - the skill says what skill to use
153 for this spell. Note that a spell could in theory cost both mana
154 and grace - what it costs is not necessarily tied to a skill. However,
155 it would be bad practice to make a wizardry spell use grace. However,
156 I could potentially see hybrid skills that have some various benefits,
157 or if we have say a 'magic' god, perhaps some special spells it grants
158 use mana and not grace.
159
160 So lets next touch on the idea of treasurelist for spell objects.
161
162 As mentioned above, spell objects, like wands, spellbooks, scrolls,
163 etc, contain the spell they cast. The way spells end up in those
164 objects is via a mechanism there is already code for - treasurelists.
165
166 Currently, the treasure generation code notices if a wand, rod, spellbook,
167 or scroll is generated. If so, it calls special code that looks
168 at the field of the spell structure and randomly chooses a spell.
169
170 Instead, this is just replaced with treasurelists. The scroll object
171 would have a 'randomitems scroll', wands a 'randomitems wand',etc. These
172 may be big treasure lists, but that shouldn't post any problem. More
173 likely, these lists would be broken up into smaller pieces (eg,
174 scroll_low_level, scroll_medium_level, scroll_high_level).
175
176 This provides finer resolution than is currently allowed in the spell
177 structure - you could make some spell available only in rods, but not
178 wands. you can better adjust things to better spells show up in
179 tougher dungeons, etc,
180
181 When a spell is cast, often it copies over certain values into the
182 SPELL_EFFECT (102). The subtype of the SPELL_EFFECT, which should generally
183 match that of SPELL itself, determines what the spell does.
184
185 ------------------------------------------------------------------------------
186 3. SPELL PATHS DOCUMENTATION
187
188 Long ago a number of archmages discovered patterns in the web that spells
189 weave in the aether. They found that some spells had structural similarities
190 to others and some of the mages took to studying particular groups of spells.
191 These mages found that by molding their thought patterns to match the patterns
192 of the spells they could better utilise all the spells of the group. Because
193 of their disciplined approach, the mages were described as following spell
194 Paths. As they attuned themselves to particular spell Paths they found that
195 they would become repelled from others, and in some cases found they were
196 denied any access to some paths. The legacy of these mages remains in some
197 of the magical items to be found around the world.
198
199 Technical details:
200 In the same way that players and objects can be protected, immune, and
201 vulnerable to the different types of attacks, they can now be attuned,
202 repelled, or denied access to the different spell Paths. An object that
203 is attuned to a Path cast spells from that Path at 80% of the spell point
204 cost and receives duration/damage bonuses as if the caster were five levels
205 higher. An object that is repelled from a Path casts spells from that Path
206 at 125% of the spell point cost and receives duration/damage bonuses as if
207 the caster were five levels lower (minimum of first level). An object that
208 is denied access to a Path cannot cast any spells from it. The casting
209 time is also modified by 80% and 125% respectively. These values are
210 defined in PATH_SP_MULT (from spells.h), PATH_TIME_MULT (from spells.h), and
211 path_level_mod (from spells.c)
212
213 The Paths themselves are the following:
214 "Nothing",
215 "Protection",
216 "Fire",
217 "Frost",
218 "Electricity",
219 "Missiles",
220 "Self",
221 "Summoning",
222 "Abjuration",
223 "Restoration",
224 "Detonation",
225 "Mind",
226 "Creation",
227 "Teleportation",
228 "Information",
229 "Transmutation",
230 "Transferrence".
231
232 See define.h for the number values corresponding to the Path.
233
234 Some more will be added in the near future. Some spells do not currently
235 belong to a Path, this is probably appropriate for some spells.
236 Paths are inherited just like protection/immunity/vulnerability, ie if a
237 ring contains "path_attuned 1", the wearer becomes attuned to the Path of
238 Protection.
239
240 Paths are quite powerful and shouldn't be given away cheaply. Ideally, most
241 objects with path_attuned attributes should have path_repelled and path_denied
242 attributes as well, to balance out (eg attuned to Fire, repelled from
243 Protection, and denied from Restoration)
244
245 ------------------------------------------------------------------------------
246 4. Spell Subtypes
247
248 This section lists specific spell subtypes and what they really
249 do. The idea is to try and generalize the object types (cones,
250 bolts, balls, etc). However, some number of spells, most notably
251 detection spells, must have their own entries because the code that
252 is actually executed needs to re-act accordingly.
253
254 SP_RAISE_DEAD (1)
255 These spells bring back dead players. These spells are only useful
256 on servers that use PERMADEATH.
257
258 Con: The number of con points the target character loses.
259 exp: the experience loss ratio (eg, exp -= exp/spell->stats.exp.
260 If zero, no exp is lost
261 last_heal: If set, corpse isn't required, just character name
262 randomitems: If set, then when the spell fails, the treasurelist is used
263 to determine what nasty monsters to summon. Note that this
264 list is not processed as a normal list - rather, everything
265 on the list is created, so chance values are meaningless.
266 race: If set, this is used as a treasurelist to determine what
267 class to change the character to (reincarnation). Basically,
268 it just looks up the treasurelist that is using the race name. This
269 should be a treasureone type list.
270
271 SP_RUNE (2)
272 Runes are objects that embed another spell in them, and are activated
273 when someone walks on top of the rune. Runes can be disarmed.
274
275 other_arch: This can be set to one of 3 values:
276 Name of rune (type = rune): If this is the case, rune is put on
277 the map with few changes.
278 name of spell: In this case, the spell is embedded into the the rune.
279 generic_rune object will be used, with the face updated.
280 NULL: in this case, the player specifies the rune to embed.
281 face: Face that the rune object will use.
282
283 If other_arch is set in the spell object, then it is assumed that
284 the sp (or grace) cost in the spell object is the total that should
285 be used. If it is an embedded spell, then the cost will be in
286 addition to that to cast the spell.
287
288 Special rules for runes where player can choose what spell to embed:
289 skill for the embedded spell must match the rune
290 Player must have enough mana/grace to embed the spell as well as cast
291 the actual rune.
292
293 Note that runes created by players/monsters will have the spell put into
294 the rune's inventory. However, archetype runes will still use the other
295 arch, as inventory in archetypes isn't really supported.
296
297 This really works fine - This allows custom spells to be embedded into
298 runes. For archetypes, using the default spell archetypes work fine.
299
300 SP_MAKE_MARK (3): This makes a 'mark' that contains a message. In the old
301 system, this used to be part of the rune code, but it really doesn't
302 have anything in common with runes (except in name).
303
304 other_arch: Object used to store the message.
305
306 SP_BOLT (4):
307 This fires a bolt (other_arch) of the spell object. All of the
308 following values are copied from the spell object and into the
309 bolt object (other_arch in the spell object).
310
311 damage, attacktype, slaying have normal meaning
312
313 range (exp): How many more spaces this will go before it ends.
314 duration (hp): How long it stays on each space.
315 Dex: chane (percentage) the bolt has of forking
316 Con: Chance that the bolt forks left.
317
318 Note if duration is less than range, which it should typically be,
319 then a bolt will only be in a subsection of the entire range.
320
321 A bolt object can have the generator flag set, in which case as
322 the bolt moves along, it will create an other_arch object. The
323 steambolt is an example of this. This can not be set in the spell object,
324 and is instead set in the other_arch field that the spell refers to.
325
326 SP_BULLET (5):
327 Bullets are single objects that go in a direction and hit
328 something. The simplest case here is magic bullet, that hits
329 something and does damage.
330
331 exploding ball type spells are a more complicated variation. These
332 object start out as a bullet, but when they hit something, they
333 explode, into fire or poison or the like. The other_arch field
334 determines what they explode into.
335
336 Values for spell object
337 range: diameter of the explosion (copied into maxhp).
338 Note that for the bullet itself, the range is always set to 50.
339 other_arch: bullet object to create.
340 dam: how much damage the bullet does.
341 attacktype: copied to the bullet.
342 slaying: copied to the bullet.
343 duration: How long the explosion lasts for. Not used for bullet
344 only spells. copied into hp field of the bullet object.
345 food (& dam adjust): Damage the explosion does. copied into the bullet
346 dam_modifier field.
347
348 See notes about explosion below.
349
350 SP_EXPLOSION (6):
351 Objects of subtype explosion are really only useful when attached
352 to a bullet - you don't really want to explode something on yourself.
353
354 Note that the set up of the below values is also the same for
355 SP_CONE types if a cone is attached to a bullet.
356
357 attacktype: from other_arch, but add magic if bullet has it.
358 damage: this is copied over the the bullet dam_modifier. The
359 bullet dam_modifier is based on the 'food' value
360 range: Copied over from stats.maxhp. This determines the radius of the
361 explosion.
362 duration: copied over from stats.hp. This determines how long the
363 explosion lingers.
364
365 Given two objects are created from one spell, tuning this/creating
366 new spells of this type is tricky. We can only use the range for
367 the bullet or explosion. Likewise, adjusting damage is difficult.
368 Ideally, perhaps, some additional fields in the spell object itself
369 are used to fully implement this, eg, these two fields determine the
370 range/damage of the bullet, and these 3 determine range/blast/damage
371 of the ball itself. That is the only real way to make a ball
372 spell fully adjustable.
373
374 SP_CONE (7):
375 sp is the overall direction of the cone is moving in.
376 other_arch: object to create as it moves along.
377 flag_lifesave: if true, object never goes away (eg, lava)
378 duration: This is how many ticks the spell stays on space.
379 flag_stand_still (was food): Causes the object not to progress
380
381 Note that SP_CONE can be the other_arch of a SP_BULLET. In this
382 case, the set up of the cone values is as described in SP_EXPLOSION
383 setting.
384
385 Note 2: The other_arch that is in the cone can really be any object.
386 As such, the code does not modify this object, other than to
387 set proper ownership. As such, you can't set what values will be
388 set in the arch thus created
389
390 vitriol is the best example of both of these notes - it starts out
391 as a bullet, which then turns into a cone, which then drops acid
392 pools.
393
394 SP_BOMB (8):
395 Bombs are as they are described - objects that sit around for
396 a little while before exploding.
397
398 The bomb itself is harmless - its not until it explodes do bad
399 things happen.
400
401 In the bomb object, the following attributes are set up:
402 dam: damage of the flying fragments/explosion
403 range: how far the fragments/explosion extends.
404 duration: How long the explosion lasts.
405 attacktype: Type of damage the bomb does.
406
407 bombs tick at a constant rate, as defined by there speed. They explode
408 when the finish their last animation. There is no way to adjust
409 this rate of ticking in the spell object, other than to point
410 other_arch at a customized bomb.
411
412 A bomb then explodes. It explodes into 'splints' (this is
413 unfortunately hardcode), and the other_arch pointer of the bomb,
414 which default is of SP_EXPLOSION.
415
416 SP_WONDER(9):
417 wonder is a spell that will cast another spell. It will also sometimes
418 cast a cone of flowers (other_arch).
419
420 randomitems: treasure list to look for spell to cast.
421
422 Note old wonder code would only cast spell that would be found in
423 books. This new code can be used to cast most any spell, as defined
424 as in the treasurelist.
425
426 Note 2: Ideally, the flower effect should also be in this treasure list,
427 so the occurrence could be accurately controlled.
428
429 SP_SMITE(10):
430 smite spells are target spells - the spell looks for an appropriate
431 enemy in the direction pointed, and if one is found, hits them with
432 whatever is in the other_arch. It is generally assumed that other_arch
433 will point to another spell.
434
435 other_arch: Object to create.
436 dam, range, duration (& modifiers) are copied into the spell effect
437 that other_arch points to.
438 attacktype: Attacktype to give to the spell effect.
439
440 Special notes if attacktype includes a death attacktype (finger
441 of death):
442 Won't work on undead - makes them strong.
443 the level of the created object is set to the dam (& modifier)
444 of the spell.
445
446
447 SP_MAGIC_MISSILE(11):
448 magic missiles are really just a bullet type object that have
449 some more intelligence in their guiding (but not much). Basically,
450 a magic missile will more or less go in the direction it is fired,
451 but will adjust course to hit a monster. Thus, if something isn't
452 perfectly lined up, magic missile will still hit it.
453 range, attacktype, etc, all have normal meanings.
454
455
456 SP_SUMMON_GOLEM(12):
457 This spell summons a friendly creature/object that helps out the
458 player.
459
460 other_arch: Pet/object this spell creates.
461 race: If set to 'holy servant' or 'avatar', this is now a
462 summon avatar spell, whose monster is determined based on the
463 god being worshipped. Note that these names are just passed
464 to determine_holy_arch()
465 dam: How much damage the golem will do. dam_modifier alters this in the
466 expected way. If zero, damage from the golem is used, but damage
467 modifier will still be added.
468 duration: increases the golems hp, which basically amounts to duration.
469 attacktype: If set, golem gets this attacktype instead of the
470 normal arch attacktype.
471 range_modifier: This adjust the wc and speed of the creature.
472
473 golems lose 1 hp each time they do something. This effectively
474 limits their duration, but does mean it can be extended by healing
475 the golem. Not sure if this is the way to go, or if duration
476 should really just be used.
477
478 Note - the old code had ldam and ldur have multiple meanings, and
479 the code multipled them by 10 internally. This is no longer done -
480 instead, the archetype itself should have the real value (10 times
481 the old compiled in values)
482
483 SP_DIMENSION_DOOR(13):
484 dimension door lets the player move some large number of spaces.
485 One still can not teleport through no magic areas.
486 range: How far we will move the player
487 maxsp, level_modifier: Normal spell meaning.
488
489 SP_MAGIC_MAPPING(14):
490 This draws an overview of the map around the player. There are currently
491 no parameters the spell takes - the results are always the same. One could
492 forsee parameters to change how far one sees, what one sees, etc.
493
494 SP_MAGIC_WALL(15):
495 This spell creates a magic wall. Walls can have a wide set of
496 properties. Many are determined by the other_arch or race (the wall
497 the spell creates).
498
499 other_arch: wall to create.
500
501 race: Name of the object to summon. This should include the %d.
502 eg, 'director_%d'. Note that if the object is non directional,
503 just don't include the %d in the race. Note that if both other_arch
504 and race is set, then other_arch takes precedence
505
506 move_block: If set, then the object can not be created on the space the
507 player is standing on. In all cases, the object can not be
508 created on a space that is blocked by an object.
509
510 is_used_up: If set, then this flag is set in the created object
511
512 tear_down: If set, this flag is then set in created object, as well
513 as flag_alive
514
515 range (and range_modifier): How many spaces in each direction the wall
516 should go. If this is 1, the wall will be 3 spaces total (1 + 1 in
517 each direction). If this is 0, the wall is a single space.
518
519 attacktype: Attacktype to set on the created wall.
520 duration (and duration_modifier): The number of hit points/duration
521 of the wall.
522 dam and dam_modifier: Damage the wall does.
523
524 Exactly how the a attacktype, duration, and damage values play out
525 depend on what is being created and the spell.
526
527 Created object is of type SPELL_EFFECT: attacktype, dam, and duration
528 are copied into fields of same name.
529
530 Created object is alive: duration becomes the objects hit points.
531
532 is_used_up (either in spell, or created object): duration put
533 into objects food value.
534
535 tear_down (in spell only): Spells damage value is copied into
536 objects hp/maxhp
537
538 The level of the created object is always set to half that of
539 the caster level.
540
541 SP_DESTRUCTION(16):
542 Destruction hits all the spaces within some distance of the player
543 with damage.
544
545 range (& modifier): Number of spaces in each direction to do damage to.
546 dam (& modifier): Amount of damage to do to each creature.
547 attacktype: Attacktype to hit the creatures with.
548 other_arch: This can in practice be anything, but is typically just used
549 as an effect to show what spaces where hit.
550 race
551
552 Note: As converted to a spell object, this is now usable by monsters.
553 The rule for what is damaged is basically this:
554 If cast by a player/friendly creature, damages non players/non friendlies.
555 If cast by a monster (non friendly), damages players/friendlies.
556
557 SP_PERCEIVE_SELF(17):
558 This is really just a hook to know to call the right routine - at
559 current time, none of the dam/range/duration fields of the spell
560 have any meaning - only think that is used is the grace and skill
561 pointers.
562 perceive self basically presents information to the player about themselves,
563 eg, race, any depletions, etc. Generally, isn't that useful since most
564 all the same information is available with built in commands.
565
566 SP_WORD_OF_RECALL(18)
567 Word of recall teleports the player back 'home' after some amount
568 of time. Parameters:
569 duration: How many ticks until the player is returned hom.
570 duration_modifier: This _reduces_ the duration above so that the player
571 return home quicker.
572
573
574 SP_INVISIBLE(19):
575 Makes the character invisible to monsters.
576 duration (& duration_modifier): How long to be invisible for.
577 race: What to make the player invisible against. Note this
578 only applies for when players cast invisible - monsters
579 will always be invisible to players, and not care about
580 the race field. race 'undead' has a bit special meaning,
581 as it will chack against the flag_undead field. race can
582 be NULL, in which case it will make the player invisible
583 to everything _but_ undead. Note also that check is done that
584 'race' a subset of the race of the monster (eg, don't use
585 comma separated values).
586 It is not possible to be invisible to both undead and non
587 undead at the same time.
588 make_invisible: if set, this is 'improved' invisible vs temporary
589 invisibility that disappears when attacking.
590
591 SP_PROBE (20): This spell looks in some direction (as specified by the player)
592 and returns information about a monster.
593 range (& modifier): How far off in a direction to look.
594
595 SP_HEALING(21): This spell heals a character/monster. Creature must
596 be where the player is.
597 attacktype: Attacktypes to heal:
598 AT_DISEASE: cure diseases.
599 AT_POISON: remove poisoning.
600 AT_CONFUSION: remove confusion.
601 AT_BLIND: remove blindness
602 stats.food: Food amount to add to character.
603 stats.dam: how much to heal.
604 stats.hp: Number of d6 to roll for healing.
605 last_sp: Regenerate this number of spell points.
606 last_grace: Regenerate this number of grace points.
607 (since the sp and grace fields are used for how much it costs
608 to use the spell, we can't re-use those fields unfortunately)
609 other_arch: seems unused - kept could be used as a spell effect
610 indicator.
611 Note that any number of these fields can be combined.
612
613 SP_CREATE_FOOD(22): This creates food for the player. The food is
614 not worth any money, but otherwise normal food.
615 food: Base amount of food to create.
616 duration_modifier: Creates an addition 50 food/increase of this value.
617
618 The formula for how much food value is created is:
619 food_value=spell_ob->stats.food +
620 + 50 * SP_level_duration_adjust(caster,spell_ob);
621
622 SP_EARTH_TO_DUST(23): This destroys earthwalls near the player.
623 range (& modifier): Radius to destroy earthwalls in. Note that
624 this is a 'square' radius.
625
626 SP_CHANGE_ABILITY(24): this adjust some ability the player has (str,
627 dex, attacktypes, etc). This is only for beneficial effects - this
628 is mostly because of the targetting logic (negative effect would
629 still target friendly creature).
630
631 By default, only one benefit spell of each time can be active at a
632 time (eg, you could have a strength and a dex, but not two strength
633 spells). This is normally tracked by changing the force name to
634 be the same name as the spell, but see the race attribute below.
635
636 If a spell is already in effect, the duration of the existing
637 spell will be increased, but otherwise none of the spell effects
638 will be changed.
639
640 race: If this is set, this is used instead of the spell name when
641 renaming the force. This is useful if you only want one of a group
642 of spells active at once (this is currently used for bless &
643 holy possession - you're allowed only one.) name_pl of the force
644 will always contain the other spell name.
645
646 duration (& modifier): How long the spell lasts for. Note that duration
647 is in ticks. Note old system used damage for duration, and each
648 point of damage was basically 50 ticks. Note also that duration_modifier
649 is a bit difference - this increases durations in 50 ticks - this
650 is because most of these spells are long duration, and increasing
651 a 200 duration spell by 15 ticks if the caster is really high
652 level is pretty meaningless.
653
654 resist_*: The protection the spell grants. Note that the code does
655 no sanity checking on these values, other than to make sure they
656 do not exceed 100. Before spell objects, there was code to put
657 caps on these values. There is no practical way to say what the
658 caps should be if so put on - instead, just make sure that
659 that dam_modifier is a sane value.
660
661 dam_modifier: Increases the resistance granted by 1 point.
662
663 ac: Benefit to armor class. This is copied over unmodified.
664 wc: Benefit to weapon class. This is copied over unmodified
665 hp: Copied over, but damage adjustment added as a bonus. hp
666 in the form of a force increases characters hp regen rate.
667
668 Str, Dex, Con, Wis, Int, Pow, Cha: Attempted adjustment to the
669 stat. These values are number of d3's rolled to adjust. In
670 addition, these values also determine the max benefit based on the
671 recipient - the table is 15 + 5 * attr_value. If 1, then the
672 highest the recipients stat after this would be 20. If 2, highest
673 would be 25, if 3, highest would be 30, etc.
674
675 flying, can_see_in_dark, xrays: Copied over to force -
676 gives caster this ability.
677
678 attacktype: Added to force object, giving the caster the attacktype.
679
680 exp: Copied over - this acts as bonus speed. This value is adjusted
681 so really fast players get less a benefit than slower players.
682
683 SP_BLESS(25):
684 This blesses the character - the character gets various benefits
685 based on the god he worships. Note that BLESS spells use the same
686 type of force object as ability change above, include the race
687 to prevent multiple castings. Note that most of all of the values
688 are copied _from the god_. The value in the spell_object is only
689 really used to know if the value from the god should be copied.
690
691
692 resist_godpower: The character gets the resistance benefits that his
693 god gives (which can be anything). resist_godpower is the upper limit
694 of any benefit the character will get.
695
696 last_grace: If set, players get same attunement as their god.
697
698 attacktype: If non zero, the player gets the attacktype of the god.
699 The character also gets the slaying of the god if attacktype is set.
700
701 ac, wc: Copied over from spell_ob to force.
702
703 SP_CURSE(26):
704 This is a lot like BLESS. Notable difference is that this is a bad
705 effect for the recipient.
706
707 ac,wc: Copied over from spell_ob - should be negative, since these
708 should be penalties.
709
710 last_grace: If set, recipient is denied/repelled to spellpath that the
711 god is denied/repelled with.
712
713 race: Same as for SP_BLESS, in that it can be used to limit number
714 of course type effects.
715
716 range: Range of the spell.
717
718 SP_SUMMON_MONSTER (27):
719 This covers a large subtype of monster/object summoning. At its
720 very basic, this spell will create an object that is unchanged
721 from the archetype, and thus can be used to basically create any object.
722
723 other_arch: Object to create.
724
725 randomitems: If set, a treasurelist to use to select the monster from.
726 The randomitems list for these spells is a bit special - the magic
727 entry is what the monsters effective level is. One can not summon
728 monsters of higher level than they are. Note that magic values
729 should be unique, as the list will use the highest value available.
730 So if two identical magic values are used, for different objects,
731 only the second entry would actually have monsters created.
732 In addition, the magic values should be in order.
733
734 race: If set to "GODCULTMON", this is a special spell that will
735 create monsters associated with the players god. We can't use
736 either of the two fields to denote this, because they point to
737 other structures, and what monsters we should create can not be
738 defined easily in the archs, as it will depend on what god the player
739 worships.
740
741 If multiple fields above are set, the order used is other_arch,
742 random_items, race. Only one of the values will be used - you can't
743 get multiple things created by setting up multiple values.
744
745 attack_movement (move_type): If set, this is set in the monster. Thus, if
746 the object should be a pet, move_type should be set to PET_MOVE.
747
748 dam (& modifier): Number of creatures to create. In the case of
749 spells that use randomitems (which can specify their own nrof), this
750 is acted as additional monsters.
751
752 flag_monster: If set, creature will not be friendly. If cleared,
753 flag_friendly will be set on the monster (using the flag_friendly
754 in the spell_ob itself is problematic in that there is a special
755 list for friendly objects, and we really don't want the spell on that
756 list.
757 The reason not all summoned monsters are friendly is because there are
758 some spells that create monsters specifically meant to be aggressive
759 to the player.
760 If flag_monster is set, the monster/created object is not changed.
761 Only if it is not set, do we set a value (flag_friendly).
762
763 SP_RECHARGE (28):
764 This spell is used to recharge a wand.
765
766 dam (& modifier): Number of 'spell levels' to recharge. Basically,
767 the number of charges increase in the wand by dam / wand spell level.
768 Thus, very low level wands are easier to recharge than high
769 level wands.
770
771 SP_POLYMORPH (29):
772 This spell by default is disabled because of various abuses within
773 the spell.
774
775 The spell normally turns one object into another similar type of
776 object (eg, orc to goblin, sword to dagger, etc).
777
778 range (& modifier) How far the polymorph spell extends.
779 other_arch: Visual effect to draw on the map as it moves along.
780
781 SP_ALCHEMY (30)
782 This turns nearby objects into gold nuggets.
783
784 duration (& modifier): This is multiplied by 1000 to determine the
785 amount of material that will be converted (think of these values
786 as the amount of kilograms to be converted)
787
788 SP_REMOVE_CURSE (31)
789 This removes the cursed/damned status of objects. It only
790 effects objects equipped by the player.
791
792 cursed: uncurse cursed objects.
793 damned: remove damnation.
794
795 SP_IDENTIFY(32)
796 This identifies objects in the players inventory and on the ground.
797 dam (& modifier): The number of objects to identify
798
799 SP_DETECTION(33)
800 This detects nearby objects (including ones in the players inventory).
801 Detection in this context typically means revealing properties about
802 the objects.
803
804 range (& modifier): How big a radius to look for objects. Radius
805 in this context is really a square - the size of the square would
806 be 2*range + 1, with the player being in the center.
807
808 other_arch: The spell effect to draw on the map for spaces that match
809 the object. Note that for some detection types, the face is replaced
810 by the face of the matching object.
811
812 This following attributes determine what objects to show. Note that
813 more than one of these can be set (for a 'detect all spell' for example).
814
815 make_invisible: If true, show invisible objects
816 known_magical: If set, detect magical objects.
817 flag_monster: If set, detect monsters
818 known_cursed: If set, detects cursed objects.
819 race: If set, only detects creatures of this race. if race is
820 'GOD', then it uses the god's slaying race for the match.
821
822 SP_MOOD_CHANGE (34):
823 This changes the 'mood' of a monster. See below for how moods are
824 changed.
825
826 range (& modifier): How for this spell effects, just like SP_DETECT above.
827 race: If set, only monsters of matching race are effected. Can be set
828 to GOD_SLAYING, in which enemy races of the god are effected.
829 if set to GOD_FRIEND, then races aligned with the god are effected.
830 attacktype: Attacktype the spell hits with. The spell doesn't do any
831 damage - this instead is used for saving throw information. Note
832 that the monster uses its best resistance for the saving throw.
833 EG, the more attacktype the spell has, the better the odds.
834
835 berserk (flag): Set the monster to be berserk, which basically means
836 it attacks anything (conflict spell)
837 unaggressive (flag): Sets the monster to be unaggressive. (pacify)
838 undead (flag): By default, undead are immune to mood changes. however, if
839 this flag is set, undead will be effected (limited by other criteria
840 above, like race (if set) and saving throw)
841 no_attack (flag): Make the creature friendly (charm & charm monster))
842 monster (flag): Wakes the monster up, makes it an enemy of the player
843 (aggravation)
844
845 Note that a spell could set multiple of these fields, but it wouldn't
846 really do much - a monster can really only have one mood.
847
848 SP_MOVING_BALL (35): This is really only used as a spell effect. Values
849 of note:
850
851 path_repelled: If set, the player must be attuned to these paths
852 to cast the spell. This can basically be used to control
853 who can cast the spell.
854
855 duration (& modifier): Copied into food of the object - this is
856 basically how long the spell lasts for.
857 dam & attacktype have expected meaning.
858 other_arch: Object that is used for the manifestation of the spell.
859 Must be set for spell to work.
860
861 SP_SWARM (36)
862 swarms are spells that fire other spells. It fires the same spell,
863 but multiple times (eg, 5 lightning bolts).
864
865 Set during casting:
866 duration: Number of d3 to roll to determine number of sub bolts.
867 duration_modifier: Added to duration as a bonus.
868 other_arch: Name of the other spell it fires.
869
870 SP_CHANGE_MANA (37)
871 This gives/takes spellpoints to the target of the spell. If this spell
872 gives sp, the the cost should be more than the sp it gives. target must
873 be in the same or adjacent space.
874
875 dam (& modifier): number of sp to transfer. If dam is negative, then this
876 spell takes away, and dam and modifier represent what percentage of sp
877 to drain (dam_modifier should still be positive). These sp are then
878 given to the caster of the spell.
879
880 This subtypes corresponds to the old transferrence and drain magic
881 spells.
882
883 SP_DISPEL_RUNE (38)
884 This removes runes from the ground. There are currently no
885 tunables for this spell.
886
887 SP_CREATE_MISSILE (39)
888 Creates bolts or arrows.
889
890 dam (& modifier): max plus of arrow that can be created.
891 duration (& modifier): Number of arrows to create.
892
893 Note that making good (highly magical) arrows reduces
894 number of arrows to be made.
895
896 SP_CONSECRATE (40)
897 This converts an altar to that of players god. There are currently
898 no tunables to this spell.
899
900 SP_ANIMATE_WEAPON (41)
901 This spell is similar to the GOLEM spells - it basically creates
902 a golem, but uses a donor weapon for the face, basic attributes,
903 etc. To implement this, it puts the donor weapon in the inventory
904 of the golem, so when the golem dies, the weapon is returned
905 to the ground. Note that in the conversion, I modified this
906 spell to use the weapon 'marked' by the player, instead of the
907 equipped weapon.
908
909 other_arch: The object used to encapsulate the weapon.
910 race: If set, the donor weapon must match this name. If this is not
911 set, then the face of the golem will be set to the weapon.
912 The reason for this is that if race is set, then since it
913 is matching a specific weapon, it is presumed that the
914 other_arch field can be appropriate set.
915 range_adjust: Bonus to wc and speed for the spell
916 duration & modifier: Bonus to creatures hp.
917 dam & modifier: Adjust to damage the golem does.
918
919 SP_LIGHT (42)
920 Arguably, such a basic spell wouldn't seem to need its own subtype.
921 However, looking at the other spells, it really didn't fit in
922 very well elsewhere - the magic_wall code passes most of its
923 parameters to the object it creates. Something like summon_monster
924 doesn't work, because it wants a free space to put the object.
925 And while it would be nice to somehow merge create food, create
926 missile, and this, they are sufficiently different that they don't
927 work very well. So instead, I try to abstract this as much
928 as I can.
929
930 attacktype: If set, and there is a monster on the target
931 space, the monster gets hit with the attacktype and the
932 damage setting. If this happens, the spell progresses no
933 further.
934 dam: As per note above, how much damage this does.
935
936 other_arch: Object to place on the map.
937 duration: set the the 'food' value in the created object -
938 if is_used_up is set in the arch, this is how long the
939 spell lasts for.
940 range: If the created object has a glow radius, range
941 is used to replace it. In this way, we don't make non
942 glowing objects glow, but do make ones that do glow
943 glow brighter.
944
945 SP_CHANGE_MAP_LIGHT (43)
946 This changes the light level on the map. There is only one
947 tunable:
948
949 dam: amount to change the lightlevel. This is passed to
950 change_map_light, as such, negative values are lighter,
951 positive is darker
952
953 SP_FAERY_FIRE (44)
954 Faery fire makes creatures within the area of effect glow.
955 This code uses the cast_destruction routine, but just handles
956 what happens when it finds a valid target.
957
958 range (& modifier): Number of spaces in each direction to do damage to.
959 duration & modifier: Potency of the spell - determines how long the
960 creature will glow for (in 10's of ticks)
961 other_arch: This can in practice be anything, but is typically just used
962 as an effect to show what spaces where hit.
963
964 Note: As converted to a spell object, this is now usable by monsters.
965 The rule for what is damaged is basically this:
966 If cast by a player/friendly creature, damages non players/non friendlies.
967 If cast by a monster (non friendly), damages players/friendlies.
968
969 SP_DISEASE (45)
970 disease spells infect players with a harmful disease.
971
972 range (& modifier): How far to look in the line for matching
973 targets.
974 other_arch: Disease to infect the target with.
975 duration_modifier: This is used to adjust the disease stats
976 in several ways:
977 wc += mod/2
978 magic += mod / 4
979 maxhp, maxgrace += mod
980
981 dam_modifier: Modifiers other aspects of the disease:
982 dam, maxsp, ac, last_eat, hp, sp: modified by full mod
983 last_sp: modifier by mod * 2
984
985 The disease code treats these modified values in its own way.
986
987
988 SP_AURA (46)/SP_ELEM_SHIELD(50):
989 Auras create fields around the players that effect anyone that steps
990 into them. Elemental shields work largely the same, but instead of
991 inserting a bunch of objects onto the map, instead the aura itself
992 (from the player arch) causes the damage.
993
994 other_arch: aura type to insert
995 duration: How long the aura lasts for. For this spell, each
996 duration_modifier is 10 ticks.
997 dam & modifier: How much damage the aura does.
998 attacktype: Attacktype of the aura.
999 resist_*: resistance value to give to the player. This is copied
1000 into a separate FORCE object.
1001
1002
1003 SP_TOWN_PORTAL (47):
1004 This creates two linked portals which the player can use to get
1005 back and forth quickly.
1006
1007 other_arch: Name of the force object to use to mark the first portal
1008 target location (town_portal).
1009 race: Name of the object used to mark where the portals go
1010 (town_portal_active)
1011 slaying: Name of the object used for the actual exit objects
1012 (town_portal_magic)
1013
1014 Note the spell otherwise doesn't have any tunables.
1015 It should be noted that if different archetypes were made it,
1016 it should be possible to have multiple town portal type spells that
1017 lead to different destinations.
1018
1019
1020 Old values:
1021 hp: duration
1022 exp: range
1023 maxhp: set to a ->count variable, so spells can be uniquely identified.
1024
1025 SP_PARTY_SPELL (48):
1026 document, or clean totally broken spell :)
1027
1028 SP_ITEM_CURSE_BLESS (49):
1029 This will curse or bless player's marked item. Curse will not change
1030 weight, just set the cursed flag. Bless will make the item god-given.
1031
1032 cursed: if set, item gets cursed.
1033 blessed: if set, item gets blessed.
1034
1035 Note that if both cursed and blessed are set, cursed only applies.
1036 Also, no check is made to not have an item both cursed and blessed.
1037
1038 ------------------------------------------------------------------------------
1039 5. Spell Casting Objects
1040
1041 As described in section 2 above, objects that cast spells (wands, rods,
1042 scrolls, etc) contain the spell they cast in their inventory. When the player
1043 uses the object, the code looks for an object in the inventory, and uses that
1044 as the type of spell to cast.
1045
1046 Treasure lists are used to determine what spell goes in the object. Thus,
1047 what spells show up in the objects is determined purely by the treasure lists,
1048 and unique lists can be made for potions, scrolls, wands, horns, and even
1049 different lists for heavy and light rods.
1050
1051 The value of the finished object is the value field in the spell object
1052 multiplied by the value object in the original object (rod, wand, scroll).
1053
1054 For items that come in different levels, the value is also adjusted based on
1055 the level of the object based on the difference of level. The
1056 code for this is in common/treasure.c
1057
1058 The nrof field for the treasurelists for these objects have special meanings -
1059 since the spell objects are invisible objects within the spell casting object,
1060 the nrof field has no actual meaning. However, we borrow that meaning for use
1061 in the parent object.
1062
1063 For wands, nrof is used for the number of charges the item has.
1064 For scrolls, nrof is the number of scrolls to make. This overrides the
1065 nrof value for the scroll itself - this allows for fine tuning number
1066 of scrolls that show up for different spells.
1067
1068 RODS/HORNS:
1069 ----------
1070 hp is the amount of 'energy' the rod currently has. when a spell is
1071 cast, hp is reduced by the amount of sp/grace the spell takes up.
1072 speed: how often the rod recharges. There used to be a much more complicated
1073 way of of regenerating charges. Instead, each time a rod activates,
1074 it regenerates 10% of its total power. Thus, a rod of speed 1.0 would
1075 fully recharge in 10 ticks, a rod with speed 0.1 would fully recharge in 100
1076 ticks.
1077
1078 This change in the way rods recharge now mean the speed of a rod can
1079 be set in a map (or elsewhere), and that change would stick.
1080
1081 Within the archetype itself, the maxhp value determines the number of
1082 spells the rod can hold before it needs to recharge again.
1083
1084 POTIONS/DUSTS:
1085 --------------
1086 potions and dusts (which were really just potions with a is_dust flag set)
1087 have been redone in several ways.
1088
1089 First, potions had varying meanings for the same archetype. You could
1090 have potions that improve stats permanently, ones that cast spells,
1091 and dust.
1092
1093
1094 There is now a SPELL_POTION (116) type. This is used for potions that
1095 ast spells. These type of potions really never should have been the
1096 same type in the first place - other than name, they really had
1097 none of the same code.
1098
1099
1100 FIREWALL (62)
1101 ----------------------------
1102 These objects are very basic - they cast a spell whenever they activate.
1103 If they have a spell object in their inventory
1104 (must be first item), that is the spell that is cast. Otherwise, they
1105 cast what other_arch points to.
1106
1107 Firewalls can be activated by buttons, and can also cast spells in specific
1108 directions. The direction the firewalls fire in is stored in the 'sp' field
1109 of the firewall.
1110
1111 Note that FIRECHEST (61) got folded into FIREWALLS, because functionally,
1112 they were identical - just set 'sp 0' in the firechest, and it fires
1113 in a random direction.
1114
1115 ------------------------------------------------------------------------------
1116 6. Arch Directory Layout
1117
1118 This section describes the basic layout of the archetypes in the arch
1119 directory. This explanation is here to try and prevent confusion (where
1120 should this arch go), where would I find an arch, etc.
1121
1122 I thought about this a bit - would it be better to organize spells by
1123 attacktype (eg, all fire spells together, all cold spells, etc), or by type of
1124 spell (bolt spells, bullet spells, cone spells, etc).
1125
1126 I think both methods have valid reasons for and against them. I decided to do
1127 it by spell type because I think it will make it easier to compare spells.
1128
1129 For example, if one bolt spell does 20 damage, and another does 30 damage,
1130 even if by another attacktype, pretty easy to see that the later is more
1131 potent.
1132
1133 This also organizes the spells more by their subtype, which is the more
1134 standard way the arch's have been done in the past. It makes for designing
1135 new spells much easier (you'd just copy a starter arch from the same
1136 directory, and not need to hunt for another one - imagine something like
1137 acid bolt).
1138
1139 That said, the organization (all relative to the arch top level directory)
1140
1141 magic: This directory goes away. There are many things that are magical
1142 that are not spells, so having a directory named magic is IMO not the
1143 best of name:
1144
1145 spell: top level directory. This directory is for the spell archetypes,
1146 and is not meant to contain non related spell code. thus, the
1147 pentagram (actually a teleporter), and the weapon improver scrolls
1148 (not really in any way related to spells) would get relocated.
1149
1150 Bolt: Contains the bolt spells
1151 Bullet: Bullet spells.
1152 Common: Contains objects that are related to spells in many areas,
1153 eg, the burnout is used by many types of spells, and things like
1154 a flaming square or icy square are used for cone, bolt, and
1155 exploding ball spells - those would be located here.
1156 Cone: Contains cone spells
1157 Healing: healing spells
1158 Potions: Contain the various potions.
1159 SeekingBall: spells like ball lightning
1160 Spellbook: spellbook/prayerbook objects.
1161 Wand: Contain the wand, staff, rod archetypes.
1162
1163 There are almost certainly sub types I'm missing that I'll have to fill in.
1164 The idea here is to try to sketch something out that gives me a working
1165 layout to fill things in.
1166
1167 Within each of the spell subtype directories, the entries for the spell
1168 information would be needed. And example below:
1169
1170 spell_lightning_bolt.arc: This is the spell object that goes in the player/
1171 monster inventory that says they know the lightning bolt spell. As
1172 per other documentation, this also contains things like the skill needed
1173 to use this spell, spell point cost, level cost, etc.
1174
1175 lightning_bolt.arc: this is the other_arch of the spell_lightning_bolt.arc.
1176 Few values in this are actually used - most of the values come from
1177 the parent arc. However, this (lightning_bolt.arc) contains the
1178 information like what the animation for the spell look like.
1179
1180 lightning_bolt.base.111.png (and so on) are the images used by the
1181 lightning_bolt.arc arch.
1182
1183 In the case of subtype directories with lots of entries, it is likely that the
1184 directories may then get broken up by things like attacktype of the spells.
1185
1186 ------------------------------------------------------------------------------
1187 7. PROGRAMMING NOTES
1188
1189 The number of top level object types is reduced - instead, many are now
1190 SPELL_EFFECT, with the subtype being the same as the spell that created
1191 them.
1192
1193 The server/time.c still has a dispatch for the SPELL_EFFECT, but it
1194 is in server/spell_util.c which determines how each subtype should
1195 be handled.
1196
1197 I try to keep all the same spell related code together, eg, put the
1198 code that casts the cone as well as moves the cone in the same file
1199 next to each other. This should reduce bugs - if someone changes
1200 one piece, they are more likely to notice the other piece and also update
1201 that. This is better than having the function in a completely different
1202 file.
1203
1204 spell_util.c really only contains very general code - dispatch routines,
1205 funcitons to adjust spells, etc. The actual work is done in either
1206 spell_attack.c, spell_effect.c, rune.c, or pets.c.
1207
1208 basically all the defines are in include/spells.h. This is a much simpler
1209 file than once was here. Remember, all the data now comes from the
1210 archetypes.
1211
1212 ------------------------------------------------------------------------------
1213 8. ABILITIES
1214
1215 In the old code, abilities had some special meaning in terms of what
1216 spells the monster would cast.
1217
1218 In the new system, abilities are no different than spells, and use the
1219 same type/subtype.
1220
1221 Abilities may still be separate for a few reasons: 1) monsters are not
1222 likely to have all the needed skills. 2) sp/grace costs may be
1223 different. 3) Many abilities shouldn't add attacktype magic. 4) Abilities
1224 generally have a very fast casting time, since monsters move slower.
1225
1226 It should be noted that many creatures just use the spell, and not
1227 the ability. Only some spells have ability counterparts.
1228
1229 Note: Before giving spell abilities to monsters, be sure that the monster
1230 will actually use them - the code in monster.c results in monsters only
1231 casting spells of certain subtypes.
1232
1233 maxsp: This increased the likelihood of monster choosing the spell.
1234 (eg, maxsp 3 vs maxsp 1 would mean 3 times as likely). This should
1235 instead by done in the treasurelist, so that more of that ability
1236 is created.
1237
1238 hp/sp: Index for the spell that is used. One was 'short' range vs
1239 long range.
1240
1241 ------------------------------------------------------------------------------
1242 9. SPELL MERGING
1243
1244 In May 2007, I (Mark Wedel) added code added causes spells to merge. This
1245 section briefly defines what was done, performance impact, and other random
1246 thoughts.
1247
1248 Before this merge logic, each spell, no matter how similar, was its own object
1249 on a space. This meant that for some spells, there would be hundreds of
1250 objects on a space. This doesn't seem that large until one looks closely at
1251 the code - for each object inserted, all the other objects on the space had to
1252 be examined, making this on O(n^2) operation.
1253
1254 Even reducing the number of objects on the space makes a significant
1255 difference - the difference between 20 and 10 objects isn't a factor of 2, it
1256 is a factor of 4.
1257
1258 The first step in the process was to write a test program
1259 (test/unit/server/comet_perf.c) to provide consistent test runs - this not
1260 only let me get performance numbers with a known environment, it also let me
1261 generate a test case to make sure that the amount of damage comes out the
1262 same (or close enough - in the revised code, the results were +/- 3%)
1263
1264 In the end, through the various changes, the code ran 70% faster than before.
1265
1266 Breakdown of gains:
1267 Setting op->range in explosion() after propagating spell: 32%
1268 Making was_destroyed() a macro: 9%
1269 was_destroyed() + op->range changes 48%
1270 Allowing spells to merge: 21%
1271
1272 As is noted, many of these changes have interactions - the range +
1273 was_destroyed() makes a bigger change together than separately. And
1274 the spell merging requires op->range=0 change, as described below.
1275
1276 Notes on the changes:
1277
1278 op->range=0 in explosion:
1279 -------------------------
1280 Each time the explosion object got an action, if op->range>0, it would try
1281 to propagate to all neighboring squares. In almost all cases this is a
1282 futile exercise, because if that neighboring square has an instance of this
1283 spell, it won't put another one there (one thing that ok_to_put_more()
1284 checks on). By setting the range to 0, we prevent this attempt. It also
1285 has the side benefit of allowing more spells to merge (we can not merge
1286 spells with different ranges). The one visible change this has is that if a
1287 door/gate is closed but in the explosion area, in the old code, the
1288 explosion would go through the space if that door is open - in the revised
1289 code it won't do that, since it has already attempted to do so and was
1290 unsuccessful.
1291
1292 The op->range check in explosion is actually a very important change -
1293 important enough that we will only merge spells if the range is zero.
1294
1295 This can be illustrated with a small example. Use this diagram of spaces:
1296
1297 12
1298 34
1299
1300 If a spell (A) is cast from the top, it will hit spaces 1&2 at the same time.
1301 If there is another spell (B) cast from the right, it will hit spaces 2 and 4.
1302
1303 When the spells merge, there is now a single spell object on space 2 that
1304 includes the tags of spells A & B. Spell AB@2 will not move to space 1 -
1305 space 1 already has spell A, so ok_to_put_more() says no. Now spell B@4 could
1306 move to 1, but if A@1 moves first, it merges with B@4, so now there is AB@4,
1307 and like AB@2, it will not move to space 1 (spell is already there).
1308
1309 But enforcing the range==0 check, this works fine - if the spells are still
1310 propagating to different spaces, we don't merge. So in the above example,
1311 spells A & B @ 2 do not merge, but instead remain individual spell object - B
1312 can move to 1, A can move to 4, and after doing so, the range to each spell is
1313 set to zero, and they can then merge at that point
1314
1315 Making was_destroyed() a macro:
1316 -------------------------------
1317 This is pretty clear cut - by making it a macro, it reduces the function
1318 call overhead. This was also a trivial 1 line function, so making it
1319 a macro was not particularly messy.
1320
1321 Allowing spells to merge:
1322 -------------------------
1323 The simplest aspect of this is to have like spells be merged in
1324 insert_ob_in_map(). However, as noted above, we won't propagate a spell to a
1325 space if there is already an instance of that spell from the same casting. So
1326 to do this, we needed to record the tags of the parent spell that we merge in,
1327 so the spell_tags array (dynamically allocated) is created. For speed, a
1328 simple hash is used to access the data in that array.
1329
1330 When merging spells, we make sure the spells are similar in many respects -
1331 must be same type of spell, same owner, same level, etc. We then adjust the
1332 damage and duration of the merged spell. We can not merge spells of different
1333 ranges - one can not really associate what effect that should have on damage
1334
1335 There are some player noticeable effects to this - players will not see as many
1336 spell objects on a space they are on, as some could have been merged together.
1337
1338 Also, the test case makes it so that damage for objects not moving within the
1339 spell are the same. With different duration spells, if a character ducks out
1340 quickly from the effect, they will take slightly less damage than before the
1341 changes. Example: two spells with dam 50 each and duration 10 & 20. In the
1342 first case, for the first 10 ticks, you would take 100 damage/tick, and for
1343 the last 10, you would take 50. Under the merging, you would take 75 damage
1344 for all 20 ticks. In most cases, the differences in duration will not be as
1345 extreme, so the actual difference in damage will no be so much.
1346
1347 There is a single tunable to this right now - the SPELL_TAG_SIZE in object.h -
1348 this determines the size of the hash table. In my testing, a size of 16
1349 worked out pretty good. Note that the hashing method is very basic - if we
1350 get a conflict in the hash table between 2 objects, we just say they can not
1351 be merged - we do not use backoff methods, etc, which would complicate other
1352 aspects of the code, notable merging the tables together. So a too small hash
1353 table would result in not enough objects getting merged.
1354
1355 In the test case above, following results on my system with the 16 slot table:
1356 Got 201640 suppressions, 8421 spell merges, 289 full tables
1357 3.623u 0.060s 0:03.90 94.3% 0+0k 0+0io 0pf+0w
1358
1359 With a 32 slot table:
1360 3.462u 0.031s 0:03.98 87.6% 0+0k 0+0io 0pf+0w
1361 Got 201641 suppressions, 8748 spell merges, 249 full tables
1362
1363 Doubling the size of the table makes a minor improvement in full tables as
1364 well as performance (.16 seconds is about 4%). And there is definitely a
1365 trade off - if the tables are very large, it will take longer to merge the
1366 tables when the objects are merged.
1367
1368 One note with the merge code - while the other changes give some definite
1369 benefits, merging should show better results with the new objects - my tests
1370 were done with castings of 30 comets - a reason for this was that with larger
1371 number of comets, the old code would just never finish (it had run for several
1372 minutes before I gave). Running the new code with it casting 100 comets now
1373 actually still had it finish relatively quickly - under 10 seconds. The more
1374 spells around, the more that could be merged to give a benefit - in
1375 comparison, the op->range=0, while reducing the load quite a bit, still would
1376 not reduce the number of objects on the different spaces.
1377
1378 Other Thoughts Related on Merging:
1379 ----------------------------------
1380 After the changes above, this is the top CPU users when run in the test
1381 program:
1382
1383 63.42 1.82 1.82 687168 0.00 0.00 hit_map
1384 17.07 2.31 0.49 1500 0.33 1.86 process_events
1385 2.79 2.39 0.08 37118 0.00 0.00 remove_ob
1386 2.44 2.46 0.07 784262 0.00 0.00 update_object
1387 2.44 2.53 0.07 713910 0.00 0.00 animate_object
1388
1389 ok_to_put_more() could be optimized - store the necessary information (spell
1390 tags, attacktypes) in the MapCell itself, but it is far enough down the
1391 performance chart there isn't a big win there. hit_map() has the same problem
1392 as other routines - it needs to examine every object on the space. And in
1393 most cases, the only things on the space are other spell objects, so it is
1394 wasting time. It could be useful to store more useful information in the
1395 MapCell about what is on the map space so that hit_map() could quickly decide
1396 that there is nothing interesting and just not do anything. In particular,
1397 hit map really only cares about 3 object types right now - living objects,
1398 transports (which may contain living objects) and items that can be destroyed
1399 by spells.
1400