Author Topic: Advanced magic, freeform system  (Read 26886 times)

Lummox JR

  • Newcomer
  • Posts: 29
  • Karma: +0/-0
    • View Profile
    • Dungeon Crawlers
Advanced magic, freeform system
« on: June 13, 2008, 04:36:34 AM »
I'm not sure how relevant this is to Roguelikes in particular as to RPGs in general, but I've been thinking lately about magic systems. I had an idea I wanted to write up as an article on my own Roguelike development blog, but every time I try to pin down a good design the whole thing falls apart.

Essentially, it struck me that a more reasonable, well-recognized form of magic in fantasy worlds would be one that requires study, has arcane and hidden knowledge, and in which new things can be discovered. Most fiction puts magic in those terms, but when translated into a game it always ends up as a preset list of spells that do specific things, often attempting balance but never achieving it. So I thought, screw balance, let's just think of magic in more practical terms: an alphabet, or RNA sequences, spelling out the building blocks of a spell. This alphabet would be deterministic, such that transposing letters would produce a different spell, using certain sequences would affect the mana and time requirements differently, and spells have to be discovered.

Where I ran aground is that it's fairly difficult to contain an entire set of spells in a single alphabet. Key concepts would be elements to act on, targets like people and objects, spell action as in a transfer of energy or a change of physical properties or some kind of transformation or illusion etc., and method of spell delivery. It should also be possible to, via another spell, bind a spell into an item or person, so you could create an enchanted weapon or some such.

Perhaps a better analogy, different from an alphabet, would be chemistry. In chemistry many compounds are formed by taking smaller molecules and forming them together in a certain way to create a unique topography. So the spellcrafter would, in trying to design a Wall of Ice spell, want something that would act on thin air, drain heat, do so very rapidly, and pick a target location and orientation of his choice. The latter might be swapped out, for ease of use, with a way of forming the spell directly between the caster and a chosen target. An explorer wanting to brighten a dark underground room would want a spell that acts on the nearby walls, summons bright light, and lingers for a certain time (perhaps hours).

I know there are various magic systems out there, but I suspect few of them require players to essentially become "magic programmers", which in a very real sense is what fictional wizards are: mana hackers. I'm thinking some kind of simplified grammar could be used to convert freeform spellcasting from a problem of natural language interpretation into a problem of simple parsing, not unlike the way a text adventure operates.

In adventure-game syntax, this is how I picture some spells would work:

Heal: Transfer life from air to target
Incinerate: Summon much heat at target for 10 seconds
Terrify: Show target's greatest fear to target
Break: Rupture target moderately
Shatter: Rupture target greatly
Teleport self away: Jump me to any large empty place

I think my own mental picture of the possible lists of spells is fairly incomplete, and without that it'd be hard to describe such spells in consistent terms. Feedback and related thoughts are much appreciated.

Korimyr the Rat

  • Newcomer
  • Posts: 5
  • Karma: +0/-0
    • ICQ Messenger - 95128707
    • AOL Instant Messenger - korimyrtherat
    • Yahoo Instant Messenger - korimyr
    • View Profile
    • Email
Re: Advanced magic, freeform system
« Reply #1 on: June 13, 2008, 07:38:49 AM »
I think my own mental picture of the possible lists of spells is fairly incomplete, and without that it'd be hard to describe such spells in consistent terms. Feedback and related thoughts are much appreciated.

It would be almost impossible to code every logical combination of spell components unless you were working from a very limited, specific set. On the other hand, this idea is certified 100% pure natural awesome-- and would be very interesting even with a very limited, specific set of spell parameters.

You should probably look at the spell creation guidelines from a few tabletop games to get an idea. I know that Shadowrun and HARP would have very useful rules to examine, and the Epic Magic system from 3.0 D&D would be a good source as well.

AmnEn

  • Rogueliker
  • ***
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: Advanced magic, freeform system
« Reply #2 on: June 13, 2008, 11:04:10 AM »
I know that UO had some basic system behind their spells. And while the player wasn't required to spell them out himself, their characters did. I think they had about 26 specific Spellwords, each having its own meaning. Combining the spellwords made for more powerful spells or even completely reversed the effect.

For example they had:
In which stood for create, make or cause
Mani which stood for Life and Healing

So if you casted a healing spell, your character shouted In Mani. If you however wanted that spell to harm your target, your character would use "An Mani", "An" standing for negate. Greater Healing consisted of "In Vas Mani" (Vas - Great).

-> http://uo.stratics.com/content/basics/spells.shtml
With just the limited set of the spellwords, basically every spell you can think about could be created. You might want to give it a short read because I think its a lot like your idea though still quite basic and the player doesn't get to choose the words.
Furthermore, you don't need specific words for targetting. The aspect of targetting is usually achieved by gestures and pointing. So to speak, every basic spell consists of two components: Speech and Gesture. Treating them seperately helps in keeping the Spellsystem understandable and basic while still allowing complex spells.


Rabiat

  • Rogueliker
  • ***
  • Posts: 88
  • Karma: +0/-0
    • View Profile
Re: Advanced magic, freeform system
« Reply #3 on: June 13, 2008, 11:05:51 AM »
Heheh, "mana hacking".  ;D

The concept appears similar to an idea I've had for procedurally generated objects (in particular, NPCs and items). If you break down an object into its components, or characteristics, random objects can be constructed by assigning random values to each characteristic. The same thing could apply to spells.

If you manage to come up with a set of sensible characteristics for spells, and assign meaningful values to them, you could allow players to compose spells according to a predetermined syntax. The easiest encoding I can come up with is a simple list of values, in which every position represents a characteristic. For example:

Position 1: mana consumption; determines spell power.
Values: 1 - 9, representing for how many magic points the player wishes to cast the spell.
Example: cast a Fire spell for 1 MP to light a cigarette; cast same spell for 4 MP to burninate a peasant.

Position 2: target
Values: 1 (target PC), 2 (target NPC), 3 (target wielded item), 4 (area effect)
Example: cast a Frost spell with target 2 to freeze an NPC; cast same spell with target 1 if you're on fire; cast same spell with target 4 to freeze nearby water.

Position 3: spell type
Values: 1 (conjuration), 2 (enchantment), 3 (divination) etc.
Example: cast a Fire spell of type 1 to throw a fireball, or type 2 to light an area, or type 3 to reveal unseen heat sources on the map.

Position 4: effect; allows the caster to reverse the effect of a spell.
Values: 1 (positive effect), 2 (negative effect)
Example: cast a Heal spel with effect 1 to heal wounds; cast same spell with effect 2 to inflict pain.

Position 5: element; determines the spell's elemental flavour.
Values: 1 (air), 2 (water/ice), 3 (earth), 4 (fire)
Example: cast a Bolt spell with element 4 to hurl a fireball, or with element 2 for a bolt of frost.

These are just silly examples, but you get the idea. A spell would be encoded as a five digit sequence. A spell "12112" could be used to target a small frost bolt at a NPC; "64313" would reveal the entire map; accidentally casting "33221" would curse the weapon you're wielding.

This example is too simple to accommodate or encode spells in such a way that they make sense. You'd have to define the common characteristics for your spells carefully, and come up with sensible values for each characteristic. Once you have those, you'd probably have to come up with a better grammar than a string of digits. One game that did this quite nicely is Loom. It allowed the player to cast spells by playing notes. Although most spells were pre-defined sequences, there was some logical arrangement; e.g. playing a tune backwards would produce the opposite effect.

Finally, you don't want to code the effects of every possible combination. You'd have to define as many generalisations as possible, although there are bound to be lots of specific exceptions. Similar spells could probably be handled as groups. There could be a general rule that higher mana consumption would make the spell more powerful, but more difficult to cast. And for nonsensical sequences, the spell parser could decide to produce some harmless random side effect based on some values in the input.
« Last Edit: June 13, 2008, 11:41:40 AM by Rabiat »

Gamer_2k4

  • Rogueliker
  • ***
  • Posts: 86
  • Karma: +0/-0
    • View Profile
Re: Advanced magic, freeform system
« Reply #4 on: June 13, 2008, 01:12:57 PM »
Dungeon Master's system works kind of like this, with the player selecting different runes to cast a spell:
http://en.wikipedia.org/wiki/Dungeon_Master_Spells

My personal (planned) magic system is a bit different from the standard, but not quite what you're talking about either.  Basically, the player is given knowledge of very basic spells as the game goes on, and he can create new spells from the pre-existing ones.  Here's a description with a few examples:
http://roguebasin.roguelikedevelopment.org/index.php?title=Labyrinth_of_Reptoran#Magic

It's a little outdated (for example, there are no classes anymore and therefore no class powers), but the examples should show what I'm hoping to accomplish.
Gamer_2k4

Lummox JR

  • Newcomer
  • Posts: 29
  • Karma: +0/-0
    • View Profile
    • Dungeon Crawlers
Re: Advanced magic, freeform system
« Reply #5 on: June 13, 2008, 02:06:09 PM »
I generally like the positional idea, but more as a grammar that can be modified by its contents--the idea being that certain letters/runes/words in key positions can change what's expected next. In this format, a spell can be complex, maybe even taking hours of character time to recite or perform, and depth has no limit. But being able to say that one position is a verb-type and another is a noun-type means transposition greatly alters a spell's characteristics, which I like. Or a postion might be noun-or-adverb, so you might have say 20 verbs and 12 nouns, 5 adverbs, and some other misc. modifiers or "dead code". Words in certain positions could even be treated as a jump, turning the spell into a sort of Turing machine.

Rabiat

  • Rogueliker
  • ***
  • Posts: 88
  • Karma: +0/-0
    • View Profile
Re: Advanced magic, freeform system
« Reply #6 on: June 14, 2008, 08:51:52 AM »
From what you're saying, it seems to me you're trying to define the syntax for a magic (programming) language. This reminds me of Ursula LeGuin's concept of magic in her Earthsea novels. If that's what you're going for, my suggestion above doesn't do justice to the complexity of what you're trying to accomplish.

From a linguistic point of view, you'd probably want to define a formal grammar consisting of syntactic categories and symbols. Given some rewrite rules for a generative grammar, it's relatively easy to parse imperative sentences such as the ones in the opening post. A stupid example:

Sentence: DitransitiveVerb, DirectObject, "from", SourceObject, "to", TargetObject.
Sentence: TransitiveVerb, DirectObject, "to", TargetObject.
Sentence: IntransitiveVerb, TargetObject, {Adverb}.
DirectObject: {Adjective}, Noun.
SourceObject: Pronoun.
TargetObject: Pronoun.
DitransitiveVerb: "transfer".
TransitiveVerb: "show".
IntransitiveVerb: "rupture".
Adverb: "moderately" | "greatly".
Adjective: "much".
Noun: "life" | "air" | "fear".
Pronoun: "me" | "him" | "them".

Most interactive fiction authoring systems (e.g. Inform or Hugo) provide ways to define grammars such as these, if only as template sentences with slots for arguments.

From a programming point of view, you'd be looking for a similar grammar, perhaps with a more limited syntax. Whether a player casts "rupture target greatly" as an imperative sentence or a function like 'void rupture(npc target, int intensity);' essentially makes to difference.

What's nice about the programming language metaphor is that the caster and his surroundings can be seen as the hardware which is manipulated by the spell. If you want to allow spellcasters to freely manipulate this hardware within the boundaries of magic, you'd have to create an interface language between the player and the game engine. As a game designer you wouldn't want the player to have access to the game engine - "for(;;){increaseHealth(PC,maxint);}" would be cheating - but you would like the player to access a lower level than just "cast(frostBolt,NPC);". Something inbetween, which allows the spellcaster to express "absorb heat from nearby lava pit; if (mana left is greater than 10) { bind heat into fireball; throw fireball at critter; } else { breathe fire at critter for 4 MP; }" would be reasonable.

Spells such as these would be procedurally executed, so as a game developer you don't have to exhaustively code the effects of every possible expression. A spell could fail for any reason 'at runtime'; syntax errors reported by the spel interpreter, hardware failure reported by the game engine (absence of a heat source or NPC, or low mana), or functional bugs in the spell code such as "transfer(excruciating pain,balrog,me);".

Lummox JR

  • Newcomer
  • Posts: 29
  • Karma: +0/-0
    • View Profile
    • Dungeon Crawlers
Re: Advanced magic, freeform system
« Reply #7 on: June 14, 2008, 05:00:11 PM »
Exactly what I'm getting at, Rabiat. Some kind of functional language that operates on a level just above the game engine's direct variables, where each "token" of the language is interpreted positionally. Ideally, it'd be refinable to the point where once you had a set of game variables down pat that could be changed (possibly with shorthands for groups of them), you could encode the entire thing as symbol code like DNA. One of the issues I've been struggling to figure out is how I'd handle complex syntax like grouping clauses, but it occurs to me that the way programming languages do this is with a prefix or postfix notation.

This almost guarantees, though, that the verbs, nouns, and operators have to all be separate tokens or else there'd be no way to tell what was going on at a given point; it does somewhat harm my idea of overlapping their meanings but maybe that's not important. And for constants, either that has to be encoded somehow (perhaps by a special constant token) or constants would have to be drawn from some relative scale.

If a spell parses incorrectly, my thinking is that any unexpected token should be skipped or partly skipped. If it's partly skipped, and a token was, say, a 3-amino chain like DNA, only one of the aminos would be skipped and the spell code from that point would be interpreted in a way the caster never anticipated. There could also be a penalty to mana requirements for a screwed-up spell, so that if you found a valuable spell by accident it'd be worthwhile to try to refine it. This could also apply to popping stuff from the stack, so that if you were to transfer air (add 5 5) which produces an invalid "receiver" for the transfer, the code could reset to the "add" token and then move forward by 1 amino, reinterpreting everything after. A clever spellcaster might be able to achieve multiple effects with something like this, though at the expected cost of mana.

I also like your concept of a fallback spell. That would be fairly intuitive to build into the spell language, e.g. fallback spell1 spell2.

Although I'd rather avoid any verbs with commutative or associative properties (such that two spells would not be identical), I think doing so may be impractical. Any kind of exchange verb would be commutative by nature unless it represented an uneven trade, and the fallback verb presented above would be associative. Not much getting around that, I guess.

Rabiat

  • Rogueliker
  • ***
  • Posts: 88
  • Karma: +0/-0
    • View Profile
Re: Advanced magic, freeform system
« Reply #8 on: June 15, 2008, 09:27:22 AM »
A low-level functional symbolic language for spells could resemble an assembly language. You might be interested in (or be familiar with) Redcode, which is used in Core War. In Redcode, every syntactically valid statement (one instruction with two operands) is considered functionally valid, which is about as free-form as it gets. The Redcode instruction set only allows for arithmetic operations, memory management and control flow, but device I/O could be adopted from any real-world assembly language.

Conceptually, the interpretation and execution of a spell in terms of an instruction set could happen abstractly within the caster's mind. Peripherals (and their I/O) are more interesting. The caster's body, and his physical surroundings, allow themselves to be controlled by the caster's mind through an interface. For this purpose, you could define 'ports' connected to the caster's peripherals, such as a mana battery, a heat source, the target NPC, a 'resource blender' which extracts magical ingredients from available inventory items, or whatever you need.

An interface such as this allows the game designer to define the caster's access level to the game's internal state; for instance, a caster cannot directly decrement a NPC's hit points, but he could request a poisonous potion form his inventory selector, instruct a heat source to evaporate the potion, and call the air around him to send a poisonous cloud toward the NPC. Each 'device' handles the request in terms of the game engine, or reports an error condition if the request cannot be fulfilled. The execution of instructions requires energy from the caster's mana battery; if there is no mana left, the battery supplies its own error condition, similar to a hardware interrupt.

This is about as atomic as my imagination allows me to define spellcasting. I think the concept could be broken down into the machine code underlying an assembly language, which would more closely resemble the DNA idea. I'm not seeing the Matrix all around me just yet though, so I'll leave this as an exercise to those who do. ;)

I'm not sure whether I understand your point about overlapping tokens. It could be the language barrier, or my view of spellcasting has become ridiculously formal since the hardware metaphor came up. I think you'd want to avoid ambiguous semantics, unless you want local ambiguity to be resolved through context or fuzzy logic. Also, I don't think I understand why you'd want your spells to be perfectly transitive (if that's what you're saying). It's hardly possible to remove arithmetic or logical symmetry, and I think it would be cool to see the alternatives casters come up with to achieve the same result. I'm sure that everyone who's ever done anything in Perl can appreciate that. ;)

Lummox JR

  • Newcomer
  • Posts: 29
  • Karma: +0/-0
    • View Profile
    • Dungeon Crawlers
Re: Advanced magic, freeform system
« Reply #9 on: June 16, 2008, 02:40:53 AM »
Heh, I see what you mean about Perl. My boss would say that's probably not a good thing, but then the difficulty of "maintaining spell code" would be a good thing in any given RPG.

By overlapping tokens I was referring to the idea that you might have, say, letters A-Z where A could in one case be a verb and in another be a noun. However this is probably unimportant and it makes constructing any kind of realistic syntax that much harder.

I've never heard of Redcode, but I like that hardware idea. In a magical sense it might be that each object has a set of "registers" (corresponding to various elements, stats, etc.) that could be altered. A spell of polymorph, then, could involve "poking" a specific value into the shape register, or a random poke. However, I would think in terms of spells the "distance" of change matters a lot, so it really shouldn't be possible to create a "health=0" spell.

Already though, I've got an idea forming based on the prefix/postfix idea that should be viable. I think something along those lines has the most potential.

Rabiat

  • Rogueliker
  • ***
  • Posts: 88
  • Karma: +0/-0
    • View Profile
Re: Advanced magic, freeform system
« Reply #10 on: June 16, 2008, 08:20:51 AM »
I think the overlapping tokens problem is what's called lexical ambiguity in computational linguistics. If by 'realistic syntax' you mean 'naturalistic syntax', there's no reason to avoid it. Syntactic ambiguity is more difficult to resolve.

Native speakers resolve ambiguity so quickly they don't even see it's there. Natural language parsers usually resolve structural ambiguity by ordering the ambiguous parse results by reverse frequency of occurrence, and picking the topmost result; i.e. if several syntactic patterns fit the sentence, they pick the pattern that is most often used by native speakers.

Since you have the option of defining the language itself, you can avoid the problem altogether by restricting the grammar in such a way that a word's functional category (e.g. subject/main verb/object) follows from its position in the sentence. If you avoid syntactic ambiguity, lexical ambiguity is no longer an issue.

Lummox JR

  • Newcomer
  • Posts: 29
  • Karma: +0/-0
    • View Profile
    • Dungeon Crawlers
Re: Advanced magic, freeform system
« Reply #11 on: June 23, 2008, 06:37:25 PM »
I've been working on a system for this and it's slowly starting to come together. The language is based on the idea of using prefixes, where you'd enter a verb name or such first and it dictates to the engine what is expected in terms of followup. These are the language primitives I have in mind:

Verb: A command, followed by ancillary info like objects it acts on, etc.
Object: A person or thing.
Place: A location; some verbs provide a location relative to an object.
Number: A number.
Ntype: A property that can be described numerically; this includes life, power, speed, etc. and any elements.
Otype: A property that is not numeric, such as shape, material, color.
Ovalue: A value that can only be described by a hierarchy, i.e. human.tall.male or such. This is something like a huffman tree where one encoding might mean "wolf" and another might mean "stone", but levels of specificity are available so you can have different kinds of wolves and stone. Every ovalue has an otype and is incompatible with different otypes.
Meme: An emotion, like love, fear, etc. The memes for a given object are a set of ovalues.

Using parentheses to help describe how the language works, this is a spell to conjure an illusion of a foe's worst fear, seen only by that foe:

Delusion (Value Fear Shape FOE) (Beside FOE) FOE

The first argument is a sub-command, which finds a shape (otype) the foe fears (meme) and converts it into an appropriate ovalue; the olvalue is the illusion. The second argument is a place. The third is the foe itself.

And this is a possible taming spell, whose effectiveness will vary depending on the creature:

Gain Power +3 (Impress Love ME FOE)

The Gain verb adds to a numerical type (power) belonging to the spell itself. The +3 might not be a real +3 but an exponent, which could mean 2^3 so this spell is 8x as powerful. However much power the spell gains, it will cost that much more in mana and possibly speed; that can be offset by deliberately sacrificing something else, like speed or area of effect. The rest of the spell is the actual taming, which inserts ME (the caster) into the foe's meme list for Love. The foe will start to love me in particular, my shape, my material, my color, etc., everything that describes me, which in turn will affect its preferences for similar beings because my specific ovalues are just subsets of less specific ones.