1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-09 09:20:44 +00:00

Direction changes. (#309)

* Take directions in the range of 0-63

* Shift direction handling in animation further back

* Don't need to get the DCC direction here.

Because it was added to animation.

* Check direction is within range
This commit is contained in:
Ziemas 2020-02-23 04:33:58 +01:00 committed by GitHub
parent bdfdeda67b
commit e4c84c4fb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 53 deletions

View File

@ -2,10 +2,58 @@ package d2dcc
var crazyBitTable = []byte{0, 1, 2, 4, 6, 8, 10, 12, 14, 16, 20, 24, 26, 28, 30, 32}
var pixelMaskLookup = []int{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}
var CofToDir4 = []int{0, 1, 2, 3}
var CofToDir8 = []int{4, 0, 5, 1, 6, 2, 7, 3}
var CofToDir16 = []int{4, 8, 0, 9, 5, 10, 1, 11, 6, 12, 2, 13, 7, 14, 3, 15}
var CofToDir32 = []int{
4, 16, 8, 17, 0, 18, 9, 19, 5, 20, 10, 21, 1, 22, 11, 23,
6, 24, 12, 25, 2, 26, 13, 27, 7, 28, 14, 29, 3, 30, 15, 31,
// Direction lookup tables courtesy of necrolis
var dir4 = []int{0x00, 0x00, 0x00, 0x00, 0x0, 0x00, 0x00, 0x00, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
var dir8 = []int{0x04, 0x04, 0x04, 0x04, 0x0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}
var dir16 = []int{0x04, 0x04, 0x08, 0x08, 0x8, 0x08, 0x00, 0x00, 0x00,
0x00, 0x09, 0x09, 0x09, 0x09, 0x05, 0x05, 0x05, 0x05, 0x0A, 0x0A,
0x0A, 0x0A, 0x01, 0x01, 0x01, 0x01, 0x0B, 0x0B, 0x0B, 0x0B, 0x06,
0x06, 0x06, 0x06, 0x0C, 0x0C, 0x0C, 0x0C, 0x02, 0x02, 0x02, 0x02,
0x0D, 0x0D, 0x0D, 0x0D, 0x07, 0x07, 0x07, 0x07, 0x0E, 0x0E, 0x0E,
0x0E, 0x03, 0x03, 0x03, 0x03, 0x0F, 0x0F, 0x0F, 0x0F, 0x04, 0x04}
var dir32 = []int{0x04, 0x10, 0x10, 0x08, 0x8, 0x11, 0x11, 0x00, 0x00,
0x12, 0x12, 0x09, 0x09, 0x13, 0x13, 0x05, 0x05, 0x14, 0x14, 0x0A,
0x0A, 0x15, 0x15, 0x01, 0x01, 0x16, 0x16, 0x0B, 0x0B, 0x17, 0x17,
0x06, 0x06, 0x18, 0x18, 0x0C, 0x0C, 0x19, 0x19, 0x02, 0x02, 0x1A,
0x1A, 0x0D, 0x0D, 0x1B, 0x1B, 0x07, 0x07, 0x1C, 0x1C, 0x0E, 0x0E,
0x1D, 0x1D, 0x03, 0x03, 0x1E, 0x1E, 0x0F, 0x0F, 0x1F, 0x1F, 0x04}
// 64 direction assets don't actually exist? but here it is anyway.
var dir64 = []int{0x04, 0x20, 0x10, 0x21, 0x8, 0x22, 0x11, 0x23, 0x00,
0x24, 0x12, 0x25, 0x09, 0x26, 0x13, 0x27, 0x05, 0x28, 0x14, 0x29,
0x0A, 0x2A, 0x15, 0x2B, 0x01, 0x2C, 0x16, 0x2D, 0x0B, 0x2E, 0x17,
0x2F, 0x06, 0x30, 0x18, 0x31, 0x0C, 0x32, 0x19, 0x33, 0x02, 0x34,
0x1A, 0x35, 0x0D, 0x36, 0x1B, 0x37, 0x07, 0x38, 0x1C, 0x39, 0x0E,
0x3A, 0x1D, 0x3B, 0x03, 0x3C, 0x1E, 0x3D, 0x0F, 0x3E, 0x1F, 0x3F}
func Dir64ToDcc(direction, numDirections int) int {
switch numDirections {
case 4:
return dir4[direction]
case 8:
return dir8[direction]
case 16:
return dir16[direction]
case 32:
return dir32[direction]
case 64:
return dir64[direction]
default:
return 0
}
}

View File

@ -251,12 +251,12 @@ func (a *Animation) GetDirectionCount() int {
}
func (a *Animation) SetDirection(directionIndex int) error {
if directionIndex >= len(a.directions) {
if directionIndex >= 64 {
return errors.New("invalid direction index")
}
a.directionIndex = directionIndex
a.directionIndex = d2dcc.Dir64ToDcc(directionIndex, len(a.directions))
a.frameIndex = 0
return nil
}

View File

@ -3,12 +3,12 @@ package d2asset
import (
"errors"
"fmt"
"math"
"strings"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dcc"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
)
@ -126,8 +126,12 @@ func (c *Composite) createMode(animationMode, weaponClass string, direction int)
return nil, err
}
if direction >= cof.NumberOfDirections {
return nil, errors.New("invalid direction")
// oh god how do i math
offset := (64 / cof.NumberOfDirections) / 2
entityDirection := int(math.Trunc((float64(direction+offset)-64.0)*(-float64(cof.NumberOfDirections)/-64.0) + float64(cof.NumberOfDirections)))
if entityDirection >= cof.NumberOfDirections {
entityDirection = 0
}
animationKey := strings.ToLower(c.object.Token + animationMode + weaponClass)
@ -139,7 +143,7 @@ func (c *Composite) createMode(animationMode, weaponClass string, direction int)
mode := &compositeMode{
animationMode: animationMode,
weaponClass: weaponClass,
direction: direction,
direction: entityDirection,
directionCount: cof.NumberOfDirections,
layers: make([]*Animation, d2enum.CompositeTypeMax),
frameCount: animationData[0].FramesPerDirection,
@ -148,19 +152,7 @@ func (c *Composite) createMode(animationMode, weaponClass string, direction int)
mode.drawOrder = make([][]d2enum.CompositeType, mode.frameCount)
for frame := 0; frame < mode.frameCount; frame++ {
mode.drawOrder[frame] = cof.Priority[direction][frame]
}
var layerDirection int
switch cof.NumberOfDirections {
case 4:
layerDirection = d2dcc.CofToDir4[mode.direction]
case 8:
layerDirection = d2dcc.CofToDir8[mode.direction]
case 16:
layerDirection = d2dcc.CofToDir16[mode.direction]
case 32:
layerDirection = d2dcc.CofToDir32[mode.direction]
mode.drawOrder[frame] = cof.Priority[mode.direction][frame]
}
for _, cofLayer := range cof.CofLayers {
@ -238,7 +230,7 @@ func (c *Composite) createMode(animationMode, weaponClass string, direction int)
layer.SetPlaySpeed(mode.animationSpeed)
layer.PlayForward()
layer.SetBlend(blend)
layer.SetDirection(layerDirection)
layer.SetDirection(direction)
mode.layers[cofLayer.Type] = layer
}
}

View File

@ -70,7 +70,7 @@ func (ac *AnimatedComposite) rotate(angle float64) {
ac.SetMode(newAnimationMode, ac.weaponClass, ac.direction)
}
newDirection := angleToDirection(angle, ac.composite.GetDirectionCount())
newDirection := angleToDirection(angle)
if newDirection != ac.direction {
ac.SetMode(ac.animationMode, ac.weaponClass, newDirection)
}

View File

@ -1,7 +1,6 @@
package d2map
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dcc"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
)
@ -42,21 +41,9 @@ func (ae AnimatedEntity) GetDirection() int {
// rotate sets direction and changes animation
func (ae *AnimatedEntity) rotate(angle float64) {
ae.direction = angleToDirection(angle, ae.animation.GetDirectionCount())
ae.direction = angleToDirection(angle)
var layerDirection int
switch ae.animation.GetDirectionCount() {
case 4:
layerDirection = d2dcc.CofToDir4[ae.direction]
case 8:
layerDirection = d2dcc.CofToDir8[ae.direction]
case 16:
layerDirection = d2dcc.CofToDir16[ae.direction]
case 32:
layerDirection = d2dcc.CofToDir32[ae.direction]
}
ae.animation.SetDirection(layerDirection)
ae.animation.SetDirection(ae.direction)
}
func (ae *AnimatedEntity) Advance(elapsed float64) {

View File

@ -1,9 +1,10 @@
package d2map
import (
"math"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/beefsack/go-astar"
"math"
)
// mapEntity represents an entity on the map that can be animated
@ -130,18 +131,16 @@ func (m *mapEntity) SetTarget(tx, ty float64, done func()) {
}
}
func angleToDirection(angle float64, numberOfDirections int) int {
if numberOfDirections == 0 {
return 0
}
degreesPerDirection := 360.0 / float64(numberOfDirections)
func angleToDirection(angle float64) int {
degreesPerDirection := 360.0 / 64.0
offset := 45.0 - (degreesPerDirection / 2)
newDirection := int((angle - offset) / degreesPerDirection)
if newDirection >= numberOfDirections {
newDirection = newDirection - numberOfDirections
if newDirection >= 64 {
newDirection = newDirection - 64
} else if newDirection < 0 {
newDirection = numberOfDirections + newDirection
newDirection = 64 + newDirection
}
return newDirection