And whilt the tutorial focuses on items, the principles can be extended to any file type in the game. Every file--spells, stores, even the worldmap--uses the basic format of static block with offsets and numbers for blocks of items/effects/areas/whatever. Item patches appear to be the most common type of patch so I've selected them as the example.
Since I seem to be the most ardent supporter of "80 lines of tp2 patch code > overwriting one item" I've been asked to try and put together a patching tutorial to help others learn how to patch. The code itself is intimidating at first, but once you understand the logic behind it and the common pitfalls it becomes a lot easier to understand.
Pros/Cons of Patching vs Overwriting
Patching is generally superior to overwriting as it goes much further to ensure your work will be compatible with the work of other modders. A properly done patch will ensure you won't have to have a lot of conditional blocks in your install to account for other mods and won't have to worry about install order.
The downside to patching is that item descriptions can not be patched. If you include an item file with your mod, you know exactly what it does and that the description will be accurate. If you patch a file, the potential is there that it has been patched by another mod, meaning that you can either change the description to what you think it will be or not change it at all. Either option can, at best, leave the item standing out as mod content or, at worst, wildly mis-label the item.
The goal is to alter the item purely through WeiDU tp2 patching. Depending on what your specific task, this can be immensely difficult or fairly easy. Before proceeding, I would suggest you open an item file with Near Infinity and ensure Tools > Show Offsets is checked or view the itm file format in the IESDP. Patching requires examining and manipulating hex offsets, which means having something that can perform hex calculations would be very useful. If you use Windows, I would suggest using the PowerToys calculator (available for free from Microsoft) as it allows you to perform calculations with hex input and output. I would also suggest you take a look at japheth's tutorial on using READ/WRITE WeiDU commands in the WeiDU documentation.
The General Approach
It's important to understand how an item file is structured before proceeding. There are four basic sections to an item file:
- Basic info
Anything in the file prior to hex offset 0x72 contains the basic info for the item. Patching anything in here is very straightforward as the offsets never change and it's simply a matter of reading and writing to the listed offsets. Following the basic info are the...
- Abilities (aka Extension Headers)
Following the global effects are item abilities. Abilities are things in an item that the player must select to use--for example, using a sword in melee is an ability, drinking a potion is also an ability, or an item's once per day goodies are abilities. To accompllish their abilities, they also need a set of effects, but the effects for abilities are last in the file. Abilities are 0x38 bytes long in the file.
- Global Effects (aka Equipping Effects)
Global effects are next in the item file. Global effects in an item file are always active and generally help define the item beyond the basic, static info. For example, an armor's basic armor class is set by a global effect, as are the colors of a shield. Some magical effects that occur when the item is equipped are also handled by global effects. Effects are 0x30 bytes in length.
- Extended effects (aka Feature Blocks)
Following the global effects in the item file are another set of effects. These effects, unlike the global ones, are related to the abilities of the item. It is important to recognize that extended effects are the same as global effects, but referenced in completely different fashion.
Celestial Fury also has three abilities: it can be used for a melee attack, can cast lightning strike once per day, and can cast blindness once per day. These translate into three abilities in the item file. The actual ability part of the file control how the abilities can be used and how often, but not what they actually do when activated. Basic melee/ranged information is included, such as damage and damage type, but anything beyond this (such as CF's extra lightning damage on a hit) is handled by the effects headers.
CF has four global effects. Three set various item colors (making the hilt red for example) and the fourth makes it glow.
The global effects are followed by the extended effects for the item abilities. In the extended effects you'll find CF's extra melee effects, such as the lightning damage and stun. (The actual melee damage, recall, in one of the few bits of info in the ability itself). The effects needed to actually make CF's two once-per-day abilities work and actually do something are also here in the extended effects.
How It All Comes Together
At this point it's important to understand how the game interprets item files. After 0x72 in the item file, the file could be anything--it could simply end if there are no abilities or effects, there could be 7 abilities, there could be 2 global effects, etc. So understanding how the game discerns the abilities and effects part of the item file is critical.
There are four key fields in the basic info part of the file:
- 0x64 - Ability Offset
This field tells the engine at what offset the abilities for the item begin. This value is always 0x72, as the abilities always follow the end of the 'basic info' part of the file. Even if there are no abilities for the item, this will be 0x72. For the sake of being cautious, though, always write your code on the assumption that this can be a non-0x72 value as its possible that a mod or patch may alter this value. The sample code provided with this tutorial will be written assuming this can be any value.
- 0x68 - Number of Abilities
This field tells the engine how many abilities an item has. This is important, for if an item has four actual abilities but this field says 3, one of the abilities will be ignored.
- 0x6a - Effects Offset
Similar to the abilities offset field, this tells the engine where the effects begin in the item file.
- 0x70 - Number of Global Effects
This field tells the engine how many effects are global effects.