Author Topic: Items Cast Magic Spells RANDOMLY (wip)  (Read 703 times)

Zakyrus

  • Entity
  • Magical Dreamer (+1250)
  • *
  • Posts: 1411
  • ...is trying to become a better MAN, and demi-god!
    • View Profile
Items Cast Magic Spells RANDOMLY (wip)
« on: January 17, 2025, 11:40:02 am »
YUP. Joe got bored, and this topic has to happen someday! haha!

This MAY OR MAY NOT WORK(prolly doesn't! BEWARE!) but here's a inclination of development path. Hopefully, somebody zealous about this idea, will help discover/pioneer futher. ;D

Quote
To implement this functionality directly into the **SNES Chrono Trigger ROM** using an **.asm patch**, we'll be working at the assembly level, which means writing a patch that modifies the ROM's behavior directly.

Since you're aiming for a sword that has a 10% chance to cast **Lightning2** when used, we will have to modify the **item use logic** in Chrono Trigger's ROM and add a random chance calculation. We will also need to handle the interaction with the **spell casting** system to trigger **Lightning2** when the sword is used.

### High-Level Steps:

1. **Modify the item structure**: Create a new item (the Lightning Sword) with an ID and description.
2. **Add the chance mechanic**: Implement a 10% chance to cast **Lightning2**.
3. **Hook into the item usage logic**: When the sword is used, check if the user is Crono and determine if the spell should be cast.
4. **Trigger the Lightning2 spell**: Call the logic for casting the **Lightning2** spell when the condition is met.

### Tools You'll Need:
1. **ASM Editor**: A tool like **Tiled, Hex Workshop, or any SNES assembler** like **ASM6** or **xkas**.
2. **ROM Header**: You’ll need to know if the ROM is **headered** or **unheadered** since that affects the memory addresses.
3. **Debugging Tools**: Tools like **Snes9x** or **bsnes** with debugging capabilities to step through the code.

Let's walk through the process of implementing this patch in **assembly** for the **SNES Chrono Trigger ROM**.

---

### Step 1: Define the Lightning Sword Item

The first thing we need is to define the **Lightning Sword** in the item table and assign it a unique item ID. You’ll need to locate where Chrono Trigger stores item data and modify the appropriate structure.

#### 1.1 Find the Item Table in the ROM

Chrono Trigger's **item table** is located at a specific memory location in the ROM. It’s typically somewhere around `$07C000` in the ROM (but this may vary depending on the ROM version or header state). Use a disassembler or a debugger to find the exact location.

#### 1.2 Add the Lightning Sword Data

In the item table, each item is defined by a structure, and we need to add the **Lightning Sword** entry.

Here’s an example of how you might define it in **assembly**:

```asm
; Adding the Lightning Sword to the item table

LightningSwordID = $50  ; Choose an item ID (e.g., $50 for the Lightning Sword)

ItemTable:
    ; Existing items...
    .byte 0x01, 0x02, 0x03, ; Item definitions...
    .byte LightningSwordID  ; This is where we add our new item

LightningSwordData:
    ; Item ID, name, description, stats, etc.
    .byte 0x50   ; Item ID (same as LightningSwordID)
    .byte 0x00   ; Type of item (e.g., weapon)
    .byte 0x32   ; Item attack stat (set appropriate value)
    .byte 0x00   ; Item defense stat (unused)
    .byte 0x01   ; Item effect type (weapon)
    .byte 0x00   ; Other data bytes for item
```

### Step 2: Create the Item Usage Logic

We will now need to hook into the item usage routine. When the player uses an item in battle, the game executes specific routines to handle item effects. We need to modify that so that if the **Lightning Sword** is used, we check for a random chance to cast **Lightning2**.

#### 2.1 Find Item Usage Code

Search for the function in the ROM that handles **item usage** during battle. Typically, it’s located in the battle logic area, but you can find it by stepping through the ROM in a debugger (such as **Snes9x** or **bsnes** with debugging enabled). The routine will look something like this:

```asm
; Hypothetical item use routine in battle
ItemUseRoutine:
    ...
    ; Code to identify item used and trigger effects
    lda $00A2 ; Load item ID from battle status
    cmp #$50  ; Check if the item is the Lightning Sword (ID $50)
    beq CastLightning2
    ...
    ror $00A2  ; Default item behavior
    jmp $xxxx  ; Go to next item logic
```

#### 2.2 Implement the Random Chance Logic

We need to introduce a **random chance** mechanic here to determine whether **Lightning2** is cast. We can use the game's existing RNG (Random Number Generator) routine or create a simple one if it doesn't exist.

```asm
CastLightning2:
    ; Generate a random number (let's assume this is at $1234)
    lda $1234   ; Load RNG value
    and #$0F    ; Mask to get a value between 0-15 (range 16)
    cmp #$01    ; Check if RNG is 1 (10% chance out of 16)
    bcc NoCast  ; If the value is greater than 1, skip casting

    ; Trigger Lightning2 spell effect
    jsr CastSpell    ; This is the call that triggers spell casting in battle
    lda #$02         ; Use 02 for Lightning2's spell ID
    sta $07D2        ; Store spell ID in appropriate memory location
    jsr ExecuteSpell ; Execute the spell (this will cast it in battle)

NoCast:
    ; If no spell is cast, just return
    ror $00A2
    jmp $xxxx
```

### Step 3: Triggering the Lightning2 Spell

The **Lightning2** spell will need to be triggered within the battle system. This involves calling the game's spell casting routine, which could look something like this:

```asm
ExecuteSpell:
    ; Assuming $07D2 stores the spell ID, use it to cast the spell
    lda $07D2     ; Load spell ID (Lightning2)
    jsr BattleSpellCast  ; Call the routine to apply the spell effect
    ror $07D2
    jmp $xxxx
```

The above code assumes that **BattleSpellCast** is the function that handles spell casting logic during battle.

### Step 4: Modify Crono's Equipment to Use the Sword

Finally, you need to assign the **Lightning Sword** to **Crono** in the **party equipment** setup. This will likely involve modifying the equipment setup or character initialization code.

This can typically be done by assigning the **Lightning Sword** ID to Crono's equipment slots:

```asm
; Assign Lightning Sword to Crono's weapon slot
CronoEquip:
    lda #$50   ; Lightning Sword ID
    sta $xxxx  ; Store in Crono's weapon slot
```

### Testing and Debugging

After writing your patch, you need to **assemble** it into a new ROM file using tools like **xkas** or **asm6**. Once you’ve patched the ROM, you should test it thoroughly in an emulator like **Snes9x** or **bsnes**. Pay close attention to:
- The **random chance** for **Lightning2** to cast.
- Ensure the sword is correctly added to Crono’s inventory.
- Ensure the **Lightning2** spell works correctly in combat.

### Final Thoughts

Creating an **.asm patch** like this involves directly modifying the game’s code and requires knowledge of the game's assembly structure. We’ve gone through the essential steps of defining the sword, adding the random chance mechanic, hooking into the item usage routine, and triggering the spell cast. The most challenging part of assembly work often lies in tracking down the right locations for modification, and understanding how the ROM interacts with the game's logic.


~Z
« Last Edit: January 17, 2025, 11:40:51 am by Zakyrus »

Zakyrus

  • Entity
  • Magical Dreamer (+1250)
  • *
  • Posts: 1411
  • ...is trying to become a better MAN, and demi-god!
    • View Profile
Re: Items Cast Magic Spells RANDOMLY (wip)
« Reply #1 on: January 17, 2025, 11:43:53 am »
This is also still UNTESTED: (but relatively close)...

Quote
### What We Need:
1. **Define the Lightning Sword** in the item table.
2. **Implement the random chance mechanic** for casting **Lightning2** when the sword is used.
3. **Modify Crono's equipment** so that he starts with the Lightning Sword.
4. **Patch the ROM** using a **hex editor** (e.g., **HXD** or **Hex Workshop**).

Let’s go through each step for **hex modifications**.

---

### Step 1: Define the Lightning Sword in the Item Table

First, we need to add a **new item** for the **Lightning Sword** in the **item table**.

#### 1.1 Locate the Item Table

The **item table** in **Chrono Trigger** starts at **address `0x07C000`** (this can vary slightly depending on the version and header type). The item table contains entries for all items in the game, each consisting of several bytes for the item ID, item type, and effects.

#### 1.2 Add the Lightning Sword

- **Item ID**: You can choose an unused ID (e.g., `0x50` for Lightning Sword).
- **Item Type**: Set it to **Weapon** (generally `0x01` for weapons).
- **Attack Value**: For the sword’s attack, you can set this to a reasonable value, like `0x32` (50 in decimal).
- **Description and other effects**: These can be left as-is for now, as the main goal is to add the item and its stats.

Here’s how the **Lightning Sword** might be added at **ID 0x50**:

| Offset (Example) | Data (Byte Values) | Description                      |
|------------------|--------------------|----------------------------------|
| `0x07C000 + X`   | `0x50`             | Item ID (Lightning Sword)        |
| `0x07C001 + X`   | `0x01`             | Item Type (Weapon)               |
| `0x07C002 + X`   | `0x32`             | Attack Value (50 in decimal)     |
| `0x07C003 + X`   | `0x00`             | Defense (unused)                 |
| `0x07C004 + X`   | `0x00`             | Effect Type (can be unused for now) |

#### Where to Insert:
- Open the ROM in a **hex editor**.
- Navigate to the **item table** around address `0x07C000`.
- Find a free spot or unused item slot and insert the Lightning Sword’s data.

For example:
```hex
07C000: 50 01 32 00 00 00 00 00 ... (Item data for Lightning Sword)
```

---

### Step 2: Implement the Random Chance for Lightning2

To add a **10% chance** for **Lightning2** to cast when the sword is used, we need to modify the **item usage routine**.

#### 2.1 Locate Item Use Code

The item use code is part of the **battle logic**, and it handles item effects during combat. It is responsible for calling the effects when a player uses an item.

The relevant code is usually found within the **battle routines**, and you’ll need to modify the section that handles **item effects**.

In the **hex editor**, we need to find the code that:
1. Identifies the item used (in this case, the Lightning Sword).
2. Checks for a **random value** to determine if the effect occurs.
3. Casts **Lightning2** if the chance is met.

#### 2.2 Add Random Chance Logic

We can assume the RNG is stored in memory at a known address (for example, **`0x0070`** or similar).

To implement the random chance, we will compare the **random number** to determine if it’s less than or equal to `0x1A` (26 in decimal), which gives us a 10% chance. Here's how you can manually modify the hex code for the **random check** and **spell casting**.

For example, let’s modify a section of the item-use code:

- **Random check**: Load the RNG value and compare it to `0x1A` (for 10% chance).
- **If true**, cast **Lightning2** (we will assume **`0x02`** is the spell ID for **Lightning2**).

#### Hex Logic to Modify:

1. **Compare the random value** to `0x1A` (10% chance out of 256).
2. If true, execute the spell casting routine.

For example:

```asm
LDA $random_value      ; Load random value from RNG (e.g., $0070)
CMP #$1A               ; Compare random value with 26 (10% of 256)
BEQ cast_lightning2     ; If random value <= 26, jump to cast Lightning2
```

#### 2.3 Modify the Hex:

Once you locate the code that handles item usage, replace it with logic that generates a random number, compares it, and casts **Lightning2** if the condition is met.

In the **hex editor**, this could look like:

- **Original** (before modification):
  ```hex
  (Some other item usage logic)
  ```

- **Modified** (after adding 10% chance and Lightning2 cast):
  ```hex
  (Code that loads RNG, compares with 0x1A, and casts Lightning2)
  ```

---

### Step 3: Trigger the Lightning2 Spell

To trigger the **Lightning2** spell, you need to call the **spell casting routine**. Chrono Trigger uses specific memory addresses and functions to cast spells during battle.

We’ll assume **`0x02`** is the ID for **Lightning2**.

#### 3.1 Call the Spell Routine

After the random check, you’ll trigger **Lightning2** by calling the appropriate battle spell routine.

Example:
```asm
JSR CastSpell       ; Jump to the function that handles spell casting
LDA #$02           ; Load the ID for Lightning2 (0x02)
STA $SpellID       ; Store spell ID in the spell handler
JSR ExecuteSpell   ; Execute the spell casting
```

This code calls the spell casting routine and makes Crono cast **Lightning2** when the chance condition is met.

#### 3.2 Modify the Hex:

After adding the random chance check, insert the spell casting logic in the correct location. In the hex editor, the change could look like this:

- **Before** (default item usage):
  ```hex
  (Original code that handles item effects)
  ```

- **After** (random chance and spell cast):
  ```hex
  (Random check code for 10% chance)
  JSR CastSpell
  LDA #$02
  STA $SpellID
  JSR ExecuteSpell
  ```

---

### Step 4: Assign the Lightning Sword to Crono

To give Crono the **Lightning Sword** at the start of the game, you need to modify his starting equipment data. This is stored at a known location in the ROM (typically around **0x080000**).

#### 4.1 Locate Crono’s Equipment Data

Look for the address where Crono's equipment is stored (usually in the **player equipment table**). For example, if Crono's **weapon slot** is at **0x080000**, set it to the **Lightning Sword ID** (`0x50`).

#### 4.2 Modify the Hex:

Using the hex editor, set Crono's weapon slot to `0x50` (the ID of the Lightning Sword).

Example:
```hex
080000: 50 00 00 00
```

This assigns the **Lightning Sword** to Crono at the start of the game.

---

### Step 5: Apply the Patch and Test

After making the hex edits:

1. Save the modified ROM.
2. Load it in an **emulator** like **Snes9x** or **bsnes**.
3. Check that Crono starts with the **Lightning Sword**.
4. In battle, test the sword’s usage. **Lightning2** should be cast **10% of the time** when using the sword.

---

### Final Thoughts

These steps involve editing the **hex** directly in the **ROM** to achieve the desired effect of a **Lightning Sword** with a 10% chance of casting **Lightning2**. The process involves:

- Adding the **item** to the item table.
- Implementing the **random chance** check for **Lightning2**.
- Triggering the **spell** cast when the chance condition is met.
- Assigning the **Lightning Sword** to **Crono** at the start of the game.

~Z

Zakyrus

  • Entity
  • Magical Dreamer (+1250)
  • *
  • Posts: 1411
  • ...is trying to become a better MAN, and demi-god!
    • View Profile
Re: Items Cast Magic Spells RANDOMLY (wip)
« Reply #2 on: January 17, 2025, 11:52:45 am »
Closer? :picardno

Code: [Select]
LDA $0070         ; Load RNG value (from address 0x0070)
CMP #$1A          ; Compare RNG with 26 (10% chance)
BEQ cast_lightning2 ; If RNG <= 26, jump to cast Lightning2

; If RNG is greater than 26, continue normal item use logic
; (Add existing item use code here)

cast_lightning2:
JSR CastSpell       ; Jump to spell casting function
LDA #$02            ; Load Lightning2 spell ID (0x02)
STA $SpellID        ; Store spell ID
JSR ExecuteSpell    ; Execute the spell


ChatGPT stuff (MAY/or NOT BE ACCURATE--let's VERIFY!)

Quote
To create a **simple `.asm patch** for Chrono Trigger that adds the functionality to the **Lightning Sword**—making it cast **Lightning2** with a 10% chance when used—we'll follow these steps:

1. **Locate the item usage routine** in the ROM that checks item IDs and applies effects.
2. **Insert the RNG comparison and spell cast code** for the 10% chance to cast **Lightning2**.

### Overview of the Steps:

- **Step 1**: Write the assembly code to compare the RNG and trigger **Lightning2** if the RNG condition is met.
- **Step 2**: Insert the patch in the ROM, targeting the section where item usage occurs.
- **Step 3**: Provide a `.asm` patch file that can be applied directly.

### 1. Writing the Assembly Code for RNG and Casting Lightning2

We will assume the following:

- **RNG** is at memory address `0x0070`.
- **Lightning2**'s spell ID is `0x02`.
- **Item ID for the Lightning Sword** is `0x50`.
- The routine will be triggered when using the Lightning Sword in battle, checking the RNG, and casting **Lightning2** if the RNG is <= `0x1A` (10% chance).

Here’s the simple assembly code for this:

```asm
; Check if item used is Lightning Sword (ID 0x50)
LDA $ItemID          ; Load the item ID used
CMP #$50             ; Compare with 0x50 (Lightning Sword)
BNE SkipLightning2   ; If not Lightning Sword, skip

; Check random chance (10%)
LDA $0070            ; Load RNG value (from address 0x0070)
CMP #$1A             ; Compare RNG with 26 (10% chance)
BEQ CastLightning2   ; If RNG <= 26, jump to cast Lightning2

SkipLightning2:
; Continue with normal item use logic (this could be other item effects)
RTS                  ; Return from subroutine

; Cast Lightning2 spell (if RNG check passed)
CastLightning2:
JSR CastSpell        ; Jump to spell casting function
LDA #$02             ; Load Lightning2 spell ID (0x02)
STA $SpellID         ; Store spell ID
JSR ExecuteSpell     ; Execute the spell
RTS                  ; Return from subroutine
```

### 2. Explanation of the Code

- **LDA $ItemID**: This loads the ID of the item being used into the accumulator. We compare it to `0x50` (the ID of the Lightning Sword).
- **CMP #$50**: If the item is not the Lightning Sword, we skip the random chance check (`BNE SkipLightning2`).
- **LDA $0070**: Loads the **RNG** value from address `0x0070`.
- **CMP #$1A**: Compares the RNG value with `0x1A` (26 in decimal), which gives a **10% chance**.
- **BEQ CastLightning2**: If the RNG is less than or equal to `0x1A`, we jump to the `CastLightning2` label.
- **JSR CastSpell**: This will trigger the spell casting function.
- **LDA #$02**: Loads the spell ID for **Lightning2** (`0x02`).
- **STA $SpellID**: Stores the spell ID into the memory location that handles spells.
- **JSR ExecuteSpell**: Executes the spell.
- **RTS**: Returns from the subroutine.

If the RNG check fails (i.e., the random number is greater than `0x1A`), the code will skip to the normal item usage routine, allowing other item effects to occur.

### 3. Creating the `.asm` Patch File

Here’s the basic structure for an **.asm patch** file. The patch should target a section of the ROM where items are used and where the item ID is checked.

```asm
; Lightning Sword Random Lightning2 Effect (10% chance)
; Patch for Chrono Trigger

ORG $XXXXXX           ; Replace XXXXXX with the correct address of the item usage routine

; Check if item used is Lightning Sword (ID 0x50)
LDA $ItemID          ; Load the item ID used
CMP #$50             ; Compare with 0x50 (Lightning Sword)
BNE SkipLightning2   ; If not Lightning Sword, skip

; Check random chance (10%)
LDA $0070            ; Load RNG value (from address 0x0070)
CMP #$1A             ; Compare RNG with 26 (10% chance)
BEQ CastLightning2   ; If RNG <= 26, jump to cast Lightning2

SkipLightning2:
; Continue with normal item use logic (this could be other item effects)
RTS                  ; Return from subroutine

; Cast Lightning2 spell (if RNG check passed)
CastLightning2:
JSR CastSpell        ; Jump to spell casting function
LDA #$02             ; Load Lightning2 spell ID (0x02)
STA $SpellID         ; Store spell ID
JSR ExecuteSpell     ; Execute the spell
RTS                  ; Return from subroutine
```

### 4. Applying the Patch

To apply the patch, you’ll need to:

1. **Identify the location** in the ROM where item usage routines are located. This typically involves disassembling the game or finding the right offsets from resources like **Temporal Flux** or other modding tools.
2. **Use a patching tool** like **xDelta** or **Lunar IPS** to apply the `.asm` patch to your ROM.

### Final Steps

- Open your ROM in a hex editor or use a disassembler to find the item usage logic.
- Insert the provided **.asm code** at the correct address in the ROM, as specified by the `ORG $XXXXXX` directive in the patch.
- Apply the patch using a tool like **xDelta** or **Lunar IPS**.
- Test the ROM to verify the **Lightning Sword** works as expected, casting **Lightning2** with a 10% chance.

---

### Conclusion

With this `.asm patch`, you are directly modifying the item usage routine in **Chrono Trigger** to give the **Lightning Sword** a **10% chance** to cast **Lightning2**. By inserting the correct code for RNG checking and spell casting, you can modify the ROM and achieve your goal.


~Z
« Last Edit: January 17, 2025, 11:55:01 am by Zakyrus »

Mauron

  • Guru of Reason Emeritus
  • Errare Explorer (+1500)
  • *
  • Posts: 1797
  • Nu-chan
    • View Profile
    • Hi trig!
Re: Items Cast Magic Spells RANDOMLY (wip)
« Reply #3 on: January 17, 2025, 05:46:02 pm »
It's as usual, close on general concepts, but wrong on anything specific. Also it seems really unsure of what
Code: [Select]
ror does.

Zakyrus

  • Entity
  • Magical Dreamer (+1250)
  • *
  • Posts: 1411
  • ...is trying to become a better MAN, and demi-god!
    • View Profile
Re: Items Cast Magic Spells RANDOMLY (wip)
« Reply #4 on: January 19, 2025, 06:16:18 pm »
It's as usual, close on general concepts, but wrong on anything specific. Also it seems really unsure of what
Code: [Select]
ror does.

 :picardno

ror? WTFLOL.

Maybe that's Random on Read, or something? /hmmmm

~Z

Mauron

  • Guru of Reason Emeritus
  • Errare Explorer (+1500)
  • *
  • Posts: 1797
  • Nu-chan
    • View Profile
    • Hi trig!
Re: Items Cast Magic Spells RANDOMLY (wip)
« Reply #5 on: January 20, 2025, 02:51:15 pm »
ror is a bitwise shift, short for ROtate Right. The SNES also contains the instruction rol for ROtate Left.

These commands shift all bits one position left or right, with the highest (left-most) or lowest (right-most) being shifted into the carry flag, and the carry flag being shifted to the lowest or highest bit.

Then there's this bit of code.

Code: [Select]
ror $00A2  ; Default item behaviorThat's not going to be the default. It's a weird snippet.

Zakyrus

  • Entity
  • Magical Dreamer (+1250)
  • *
  • Posts: 1411
  • ...is trying to become a better MAN, and demi-god!
    • View Profile
Re: Items Cast Magic Spells RANDOMLY (wip)
« Reply #6 on: January 22, 2025, 01:49:23 pm »
OooooOOOohhh.. That is EARLY C++ PROGRAMMING:  RHC(Right Hand Coordinate), LHC(Left Hand Coordinate)..bitwise shifting....the DREAM-TEAM utilized this for SNES ChronoTrigger?! IMPRESSIVE! FUCK YES! This might be even easier to finalize, that means the remainer of UNKONB BITS can be figured out---"      "     'a little bit(pun intended) easier'   "   "

Think of it is: AS MOST THE DATA IS THE RIGHT TRIANGLE, (then the way data is impacted)--got ORIGAMI FOLDED "SOME-WHERE ELSE for A 'region of TIME' ..." yes, I'm starting to To REALIZE.
:)

~Z
« Last Edit: January 22, 2025, 01:51:43 pm by Zakyrus »

Mauron

  • Guru of Reason Emeritus
  • Errare Explorer (+1500)
  • *
  • Posts: 1797
  • Nu-chan
    • View Profile
    • Hi trig!
Re: Items Cast Magic Spells RANDOMLY (wip)
« Reply #7 on: January 22, 2025, 04:21:33 pm »
We've known about bitwise logic in Chrono Trigger for a long time. The point was ChatGPT was adding it in a way that didn't make sense.

Just so we're clear, the output in this topic doesn't really amount to more than theorycrafting with a little psuedocode.

Zakyrus

  • Entity
  • Magical Dreamer (+1250)
  • *
  • Posts: 1411
  • ...is trying to become a better MAN, and demi-god!
    • View Profile
Re: Items Cast Magic Spells RANDOMLY (wip)
« Reply #8 on: January 23, 2025, 02:59:34 pm »
The point was ChatGPT was adding it in a way that didn't make sense.

Right. THIS is the development I am working on making to ensure, ROM data and hacking variables aren't BUTCHERED *before* having AI develop plugins for GAMES on 'the fly'.

We all remember "The FLY" movies, lol.

~Z