Add type chart editor & randomizer

2% for immune, 17% for 2x/.5x
This commit is contained in:
Kurt 2018-12-01 21:57:01 -08:00
parent 62517ca742
commit 1f3cbdad71
7 changed files with 483 additions and 2 deletions

View File

@ -0,0 +1,82 @@
using System;
using pkNX.Structures;
using Util = pkNX.Randomization.Util;
namespace pkNX.Game
{
public class TypeChartEditor
{
public byte[] Data;
public int Width => (int)Math.Sqrt(Data.Length);
public int Height => (int)Math.Sqrt(Data.Length);
public TypeChartEditor(byte[] data) => Data = data;
public void Randomize()
{
var rnd = Util.Random;
for (int i = 0; i < Data.Length; i++)
{
var rv = rnd.Next(100);
Data[i] = GetEffectiveness(rv);
}
}
private static byte GetEffectiveness(int rv)
{
if (rv < 2) // 2%
return (byte)TypeEffectiveness.Immune;
if (rv < 19) // 17%
return (byte)TypeEffectiveness.NotVery;
if (rv < 36) // 17%
return (byte)TypeEffectiveness.Super;
return (byte)TypeEffectiveness.Normal;
}
private static readonly uint[] Colors =
{
0xFF000000,
0, // unused
0xFFFF0000,
0, // unused
0xFFFFFFFF,
0, 0, 0, // unused
0xFF008000
};
public byte[] GetTypeChartImageData(int itemsize, int itemsPerRow, byte[] vals, out int width, out int height)
{
width = itemsize * itemsPerRow;
height = itemsize * vals.Length / itemsPerRow;
var bmpData = new byte[4 * width * height];
// loop over area
for (int i = 0; i < vals.Length; i++)
{
int X = i % itemsPerRow;
int Y = i / itemsPerRow;
// Plop into image
byte[] itemColor = BitConverter.GetBytes(Colors[vals[i]]);
for (int x = 0; x < itemsize * itemsize; x++)
{
var ofs = (((Y * itemsize) + (x % itemsize)) * width * 4) + (((X * itemsize) + (x / itemsize)) * 4);
Buffer.BlockCopy(itemColor, 0, bmpData, ofs, 4);
}
}
// slap on a grid
byte[] gridColor = BitConverter.GetBytes(0x17000000);
for (int i = 0; i < width * height; i++)
{
if (i % itemsize == 0 || i / (itemsize * itemsPerRow) % itemsize == 0)
{
var ofs = (i / (itemsize * itemsPerRow) * width * 4) + (i % (itemsize * itemsPerRow) * 4);
Buffer.BlockCopy(gridColor, 0, bmpData, ofs, 4);
}
}
return bmpData;
}
}
}

View File

@ -0,0 +1,10 @@
namespace pkNX.Structures
{
public enum TypeEffectiveness : byte
{
Immune = 0,
NotVery = 2,
Normal = 4,
Super = 8,
}
}

View File

@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using pkNX.Containers;
@ -267,5 +268,34 @@ public void EditTM()
data = list.Write();
FileMitm.WriteAllBytes(path, data);
}
public void EditTypeChart()
{
var path = Path.Combine(ROM.PathExeFS, "main");
var data = FileMitm.ReadAllBytes(path);
var nso = new NSO(data);
byte[] pattern = {0x84, 0x5A, 0xA4, 0xFF};
int ofs = CodePattern.IndexOfBytes(nso.DecompressedRO, pattern);
if (ofs < 0)
{
WinFormsUtil.Alert("Not able to find type chart data in exefs.");
return;
}
ofs += pattern.Length;
var cdata = new byte[18 * 18];
var types = ROM.GetStrings(TextName.Types);
Array.Copy(nso.DecompressedRO, ofs, cdata, 0, cdata.Length);
var chart = new TypeChartEditor(cdata);
var editor = new TypeChart(chart, types);
editor.ShowDialog();
if (!editor.Modified)
return;
chart.Data.CopyTo(nso.DecompressedRO, ofs);
data = nso.Write();
FileMitm.WriteAllBytes(path, data);
}
}
}

View File

@ -0,0 +1,105 @@
namespace pkNX.WinForms
{
partial class TypeChart
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.B_Save = new System.Windows.Forms.Button();
this.B_RTM = new System.Windows.Forms.Button();
this.PB_Chart = new System.Windows.Forms.PictureBox();
this.L_Hover = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.PB_Chart)).BeginInit();
this.SuspendLayout();
//
// B_Save
//
this.B_Save.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.B_Save.Location = new System.Drawing.Point(530, 12);
this.B_Save.Name = "B_Save";
this.B_Save.Size = new System.Drawing.Size(60, 23);
this.B_Save.TabIndex = 1;
this.B_Save.Text = "Save";
this.B_Save.UseVisualStyleBackColor = true;
this.B_Save.Click += new System.EventHandler(this.B_Save_Click);
//
// B_RTM
//
this.B_RTM.Location = new System.Drawing.Point(12, 12);
this.B_RTM.Name = "B_RTM";
this.B_RTM.Size = new System.Drawing.Size(75, 23);
this.B_RTM.TabIndex = 7;
this.B_RTM.Text = "Randomize";
this.B_RTM.UseVisualStyleBackColor = true;
this.B_RTM.Click += new System.EventHandler(this.B_RTM_Click);
//
// PB_Chart
//
this.PB_Chart.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.PB_Chart.Location = new System.Drawing.Point(12, 41);
this.PB_Chart.Name = "PB_Chart";
this.PB_Chart.Size = new System.Drawing.Size(579, 579);
this.PB_Chart.TabIndex = 469;
this.PB_Chart.TabStop = false;
this.PB_Chart.MouseClick += new System.Windows.Forms.MouseEventHandler(this.ClickMouse);
this.PB_Chart.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MoveMouse);
//
// L_Hover
//
this.L_Hover.AutoSize = true;
this.L_Hover.Location = new System.Drawing.Point(93, 17);
this.L_Hover.Name = "L_Hover";
this.L_Hover.Size = new System.Drawing.Size(117, 13);
this.L_Hover.TabIndex = 471;
this.L_Hover.Text = "Effectiveness Summary";
//
// TypeChart
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(602, 627);
this.Controls.Add(this.L_Hover);
this.Controls.Add(this.PB_Chart);
this.Controls.Add(this.B_RTM);
this.Controls.Add(this.B_Save);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.MaximizeBox = false;
this.Name = "TypeChart";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Type Chart";
((System.ComponentModel.ISupportInitialize)(this.PB_Chart)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button B_Save;
private System.Windows.Forms.Button B_RTM;
private System.Windows.Forms.PictureBox PB_Chart;
private System.Windows.Forms.Label L_Hover;
}
}

View File

@ -0,0 +1,126 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using pkNX.Game;
namespace pkNX.WinForms
{
public sealed partial class TypeChart : Form
{
private readonly string[] types;
private readonly TypeChartEditor Editor;
public byte[] Chart { get; set; }
public bool Modified { get; set; }
private int TypeWidth => 32; // px
private int TypeCount => types.Length;
public TypeChart(TypeChartEditor editor, string[] types)
{
Editor = editor;
InitializeComponent();
this.types = types;
Chart = editor.Data;
LoadChart();
}
private void B_Save_Click(object sender, EventArgs e)
{
Modified = true;
SaveChart();
Close();
}
private void SaveChart() => Chart = Editor.Data;
private void B_RTM_Click(object sender, EventArgs e)
{
Editor.Randomize();
Chart = Editor.Data;
LoadChart();
System.Media.SystemSounds.Asterisk.Play();
}
private void LoadChart() => PB_Chart.Image = GetGrid(TypeWidth, TypeCount, Chart);
// gui logic below
private void MoveMouse(object sender, MouseEventArgs e)
{
GetCoordinate((PictureBox)sender, e, out int X, out int Y);
int index = (Y * TypeCount) + X;
if (index >= Chart.Length)
return;
UpdateLabel(X, Y, Chart[index]);
}
private void ClickMouse(object sender, MouseEventArgs e)
{
GetCoordinate((PictureBox)sender, e, out int X, out int Y);
int index = (Y * TypeCount) + X;
if (index >= Chart.Length)
return;
Chart[index] = ToggleEffectiveness(Chart[index], e.Button == MouseButtons.Left);
UpdateLabel(X, Y, Chart[index]);
LoadChart();
}
private void UpdateLabel(int X, int Y, int value)
{
if (value >= effects.Length || X >= types.Length || Y >= types.Length)
return; // clicking and moving outside the box has invalid values
L_Hover.Text = $"[{X:00}x{Y:00}: {value:00}] {types[Y]} attacking {types[X]} {effects[value]}";
}
private readonly string[] effects =
{
"has no effect!",
"",
"is not very effective.",
"",
"does regular damage.",
"",
"",
"",
"is super effective!"
};
public void GetCoordinate(Control sender, MouseEventArgs e, out int X, out int Y)
{
X = e.X / TypeWidth;
Y = e.Y / TypeWidth;
if (e.X == sender.Width - 1 - 2) // tweak because the furthest pixel is unused for transparent effect, and 2 px are used for border
X--;
if (e.Y == sender.Height - 1 - 2)
Y--;
}
public static byte ToggleEffectiveness(byte currentValue, bool increase)
{
byte[] vals = { 0, 2, 4, 8 };
int curIndex = Array.IndexOf(vals, currentValue);
if (curIndex < 0)
return currentValue;
uint shift = (uint)(curIndex + (increase ? 1 : -1));
var newIndex = shift % vals.Length;
return vals[newIndex];
}
public Bitmap GetGrid(int itemsize, int itemsPerRow, byte[] vals)
{
// set up image
byte[] bmpData = Editor.GetTypeChartImageData(itemsize, itemsPerRow, vals, out int width, out int height);
// assemble image
Bitmap b = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData bData = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
System.Runtime.InteropServices.Marshal.Copy(bmpData, 0, bData.Scan0, bmpData.Length);
b.UnlockBits(bData);
return b;
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -35,7 +35,6 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
@ -89,6 +88,12 @@
<Compile Include="Subforms\BTTE.designer.cs">
<DependentUpon>BTTE.cs</DependentUpon>
</Compile>
<Compile Include="Subforms\TypeChart.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Subforms\TypeChart.Designer.cs">
<DependentUpon>TypeChart.cs</DependentUpon>
</Compile>
<Compile Include="Subforms\TMList.cs">
<SubType>Form</SubType>
</Compile>
@ -155,6 +160,9 @@
<EmbeddedResource Include="Subforms\BTTE.resx">
<DependentUpon>BTTE.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Subforms\TypeChart.resx">
<DependentUpon>TypeChart.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Subforms\TMList.resx">
<DependentUpon>TMList.cs</DependentUpon>
</EmbeddedResource>