1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-12-26 12:06:24 -05:00

Removed some global variables (#611)

* removed some global variables

* fixed tests
This commit is contained in:
Gürkan Kaymak 2020-07-22 22:03:03 +03:00 committed by GitHub
parent 362147848d
commit 80e655964e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 120 additions and 126 deletions

View File

@ -132,9 +132,6 @@ func (p *App) initialize() error {
p.lastTime = d2common.Now()
p.lastScreenAdvance = p.lastTime
config := d2config.Config
d2resource.LanguageCode = config.Language
p.renderer.SetWindowIcon("d2logo.png")
p.terminal.BindLogger()
@ -168,6 +165,7 @@ func (p *App) initialize() error {
return err
}
config := d2config.Config
p.audio.SetVolumes(config.BgmVolume, config.SfxVolume)
if err := p.loadDataDict(); err != nil {

View File

@ -1,7 +1,5 @@
package d2resource
var LanguageCode string
// Paths of the resources inside the mpq files.
const (
// --- Screens ---
@ -199,7 +197,7 @@ const (
// --- Inventory Data ---
Inventory = "/data/global/excel/inventory.txt"
Inventory = "/data/global/excel/inventory.txt"
Weapons = "/data/global/excel/weapons.txt"
Armor = "/data/global/excel/armor.txt"
Misc = "/data/global/excel/misc.txt"
@ -285,7 +283,7 @@ const (
// --- Enemy Data ---
MonStats = "/data/global/excel/monstats.txt"
MonStats2 = "/data/global/excel/monstats2.txt"
MonStats2 = "/data/global/excel/monstats2.txt"
MonPreset = "/data/global/excel/monpreset.txt"
SuperUniques = "/data/global/excel/SuperUniques.txt"

View File

@ -30,8 +30,8 @@ type Player struct {
}
// run speed should be walkspeed * 1.5, since in the original game it is 6 yards walk and 9 yards run.
var baseWalkSpeed = 6.0
var baseRunSpeed = 9.0
const baseWalkSpeed = 6.0
const baseRunSpeed = 9.0
// CreatePlayer creates a new player entity and returns a pointer to it.
func CreatePlayer(id, name string, x, y int, direction int, heroType d2enum.Hero,

View File

@ -14,8 +14,6 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapstamp"
)
var wildernessGrass = d2ds1.FloorShadowRecord{Prop1: 1, Style: 0, Sequence: 0}
func loadPreset(mapEngine *d2mapengine.MapEngine, id, index int) *d2mapstamp.Stamp {
for _, file := range d2datadict.LevelPreset(id).Files {
mapEngine.AddDS1(file)
@ -264,7 +262,7 @@ func generateWilderness1Contents(mapEngine *d2mapengine.MapEngine, rect d2common
for x := 0; x < rect.Width; x++ {
tile := mapEngine.Tile(rect.Left+x, rect.Top+y)
tile.RegionType = d2enum.RegionIdType(levelDetails.LevelType)
tile.Components.Floors = []d2ds1.FloorShadowRecord{wildernessGrass}
tile.Components.Floors = []d2ds1.FloorShadowRecord{{Prop1: 1, Style: 0, Sequence: 0}} // wildernessGrass
tile.PrepareTile(x, y, mapEngine)
}
}

View File

@ -1,28 +0,0 @@
package d2maprenderer
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
)
var imageCacheRecords map[uint32]d2interface.Surface
// InvalidateImageCache the global region image cache. Call this when you are changing regions.
func InvalidateImageCache() {
imageCacheRecords = nil
}
func (mr *MapRenderer) getImageCacheRecord(style, sequence byte, tileType d2enum.TileType, randomIndex byte) d2interface.Surface {
lookupIndex := uint32(style)<<24 | uint32(sequence)<<16 | uint32(tileType)<<8 | uint32(randomIndex)
return imageCacheRecords[lookupIndex]
}
func (mr *MapRenderer) setImageCacheRecord(style, sequence byte, tileType d2enum.TileType, randomIndex byte, image d2interface.Surface) {
lookupIndex := uint32(style)<<24 | uint32(sequence)<<16 | uint32(tileType)<<8 | uint32(randomIndex)
if imageCacheRecords == nil {
imageCacheRecords = make(map[uint32]d2interface.Surface)
}
imageCacheRecords[lookupIndex] = image
}

View File

@ -2,11 +2,12 @@ package d2maprenderer
import (
"errors"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
"image/color"
"log"
"math"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2ds1"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
@ -17,13 +18,13 @@ import (
// MapRenderer manages the game viewport and Camera. It requests tile and entity data from MapEngine and renders it.
type MapRenderer struct {
renderer d2interface.Renderer // Used for drawing operations
mapEngine *d2mapengine.MapEngine // The map engine that is being rendered
palette d2interface.Palette // The palette used for this map
viewport *Viewport // Used for rendering offsets
Camera Camera // Used to determine where on the map we are rendering
mapDebugVisLevel int // Map debug visibility index (0=none, 1=tiles,
// 2=sub-tiles)
renderer d2interface.Renderer // Used for drawing operations
mapEngine *d2mapengine.MapEngine // The map engine that is being rendered
palette d2interface.Palette // The palette used for this map
viewport *Viewport // Used for rendering offsets
Camera Camera // Used to determine where on the map we are rendering
imageCacheRecords map[uint32]d2interface.Surface
mapDebugVisLevel int // Map debug visibility index (0=none, 1=tiles, 2=sub-tiles)
entityDebugVisLevel int // Entity Debug visibility index (0=none, 1=vectors)
lastFrameTime float64 // The last time the map was rendered
currentFrame int // Current render frame (for animations)
@ -489,3 +490,23 @@ func (mr *MapRenderer) ViewportDefault() {
func (mr *MapRenderer) SetCameraTarget(position *d2vector.Position) {
mr.Camera.SetTarget(position)
}
// InvalidateImageCache the global region image cache. Call this when you are changing regions.
func (mr *MapRenderer) InvalidateImageCache() {
mr.imageCacheRecords = nil
}
func (mr *MapRenderer) getImageCacheRecord(style, sequence byte, tileType d2enum.TileType, randomIndex byte) d2interface.Surface {
lookupIndex := uint32(style)<<24 | uint32(sequence)<<16 | uint32(tileType)<<8 | uint32(randomIndex)
return mr.imageCacheRecords[lookupIndex]
}
func (mr *MapRenderer) setImageCacheRecord(style, sequence byte, tileType d2enum.TileType, randomIndex byte, image d2interface.Surface) {
lookupIndex := uint32(style)<<24 | uint32(sequence)<<16 | uint32(tileType)<<8 | uint32(randomIndex)
if mr.imageCacheRecords == nil {
mr.imageCacheRecords = make(map[uint32]d2interface.Surface)
}
mr.imageCacheRecords[lookupIndex] = image
}

View File

@ -27,17 +27,17 @@ type UI struct {
CursorX int // TODO (carrelld) convert dependent code and remove
CursorY int // TODO (carrelld) convert dependent code and remove
pressedWidget Widget
clickSfx d2interface.SoundEffect
}
var singleton UI
var clickSfx d2interface.SoundEffect
func Initialize(inputManager d2interface.InputManager, audioProvider d2interface.AudioProvider) {
sfx, err := audioProvider.LoadSoundEffect(d2resource.SFXButtonClick)
if err != nil {
log.Fatalf("failed to initialize ui: %v", err)
}
clickSfx = sfx
singleton.clickSfx = sfx
singleton.inputManager = inputManager
if err := singleton.inputManager.BindHandler(&singleton); err != nil {
@ -83,7 +83,7 @@ func (u *UI) OnMouseButtonDown(event d2interface.MouseEvent) bool {
if contains(w, singleton.CursorX, singleton.CursorY) && w.GetVisible() && w.GetEnabled() {
w.SetPressed(true)
singleton.pressedWidget = w
clickSfx.Play()
u.clickSfx.Play()
break
}
}

View File

@ -1,19 +1,17 @@
package d2ui
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"strings"
"time"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
)
// TextBox with cursor focus
var focusedTextBox *TextBox
// TextBox represents a text input box
type TextBox struct {
text string
@ -25,6 +23,7 @@ type TextBox struct {
textLabel Label
lineBar Label
filter string
isFocused bool
}
func CreateTextbox(renderer d2interface.Renderer) TextBox {
@ -59,7 +58,7 @@ func (v *TextBox) Render(target d2interface.Surface) {
}
func (v *TextBox) OnKeyChars(event d2interface.KeyCharsEvent) bool {
if !(focusedTextBox == v) || !v.visible || !v.enabled {
if !v.isFocused || !v.visible || !v.enabled {
return false
}
newText := string(event.Chars())
@ -178,5 +177,5 @@ func (v *TextBox) OnActivated(callback func()) {
}
func (v *TextBox) Activate() {
focusedTextBox = v
v.isFocused = true
}

View File

@ -2,12 +2,13 @@ package d2gamescreen
import (
"fmt"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
"log"
"os"
"strings"
"time"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapgen"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapengine"
@ -26,58 +27,60 @@ type regionSpec struct {
extra []int
}
var regions = []regionSpec{
// Act I
{d2enum.RegionAct1Town, 1, 3, []int{}},
{d2enum.RegionAct1Wilderness, 4, 52, []int{
108,
160, 161, 162, 163, 164,
}},
{d2enum.RegionAct1Cave, 53, 107, []int{}},
{d2enum.RegionAct1Crypt, 109, 159, []int{}},
{d2enum.RegionAct1Monestary, 165, 165, []int{}},
{d2enum.RegionAct1Courtyard, 166, 166, []int{256}},
{d2enum.RegionAct1Barracks, 167, 205, []int{}},
{d2enum.RegionAct1Jail, 206, 255, []int{}},
{d2enum.RegionAct1Cathedral, 257, 257, []int{}},
{d2enum.RegionAct1Catacombs, 258, 299, []int{}},
{d2enum.RegionAct1Tristram, 300, 300, []int{}},
func getRegions() []regionSpec {
return []regionSpec{
// Act I
{d2enum.RegionAct1Town, 1, 3, []int{}},
{d2enum.RegionAct1Wilderness, 4, 52, []int{
108,
160, 161, 162, 163, 164,
}},
{d2enum.RegionAct1Cave, 53, 107, []int{}},
{d2enum.RegionAct1Crypt, 109, 159, []int{}},
{d2enum.RegionAct1Monestary, 165, 165, []int{}},
{d2enum.RegionAct1Courtyard, 166, 166, []int{256}},
{d2enum.RegionAct1Barracks, 167, 205, []int{}},
{d2enum.RegionAct1Jail, 206, 255, []int{}},
{d2enum.RegionAct1Cathedral, 257, 257, []int{}},
{d2enum.RegionAct1Catacombs, 258, 299, []int{}},
{d2enum.RegionAct1Tristram, 300, 300, []int{}},
// Act II
{d2enum.RegionAct2Town, 301, 301, []int{}},
{d2enum.RegionAct2Sewer, 302, 352, []int{}},
{d2enum.RegionAct2Harem, 353, 357, []int{}},
{d2enum.RegionAct2Basement, 358, 361, []int{}},
{d2enum.RegionAct2Desert, 362, 413, []int{}},
{d2enum.RegionAct2Tomb, 414, 481, []int{}},
{d2enum.RegionAct2Lair, 482, 509, []int{}},
{d2enum.RegionAct2Arcane, 510, 528, []int{}},
// Act II
{d2enum.RegionAct2Town, 301, 301, []int{}},
{d2enum.RegionAct2Sewer, 302, 352, []int{}},
{d2enum.RegionAct2Harem, 353, 357, []int{}},
{d2enum.RegionAct2Basement, 358, 361, []int{}},
{d2enum.RegionAct2Desert, 362, 413, []int{}},
{d2enum.RegionAct2Tomb, 414, 481, []int{}},
{d2enum.RegionAct2Lair, 482, 509, []int{}},
{d2enum.RegionAct2Arcane, 510, 528, []int{}},
// Act III
{d2enum.RegionAct3Town, 529, 529, []int{}},
{d2enum.RegionAct3Jungle, 530, 604, []int{}},
{d2enum.RegionAct3Kurast, 605, 658, []int{
748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767, 768, 769,
770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791,
792, 793, 794, 795, 796,
}},
{d2enum.RegionAct3Spider, 659, 664, []int{}},
{d2enum.RegionAct3Dungeon, 665, 704, []int{}},
{d2enum.RegionAct3Sewer, 705, 747, []int{}},
// Act III
{d2enum.RegionAct3Town, 529, 529, []int{}},
{d2enum.RegionAct3Jungle, 530, 604, []int{}},
{d2enum.RegionAct3Kurast, 605, 658, []int{
748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767, 768, 769,
770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791,
792, 793, 794, 795, 796,
}},
{d2enum.RegionAct3Spider, 659, 664, []int{}},
{d2enum.RegionAct3Dungeon, 665, 704, []int{}},
{d2enum.RegionAct3Sewer, 705, 747, []int{}},
// Act IV
{d2enum.RegionAct4Town, 797, 798, []int{}},
{d2enum.RegionAct4Mesa, 799, 835, []int{}},
{d2enum.RegionAct4Lava, 836, 862, []int{}},
// Act IV
{d2enum.RegionAct4Town, 797, 798, []int{}},
{d2enum.RegionAct4Mesa, 799, 835, []int{}},
{d2enum.RegionAct4Lava, 836, 862, []int{}},
// Act V -- broken or wrong order
{d2enum.RegonAct5Town, 863, 864, []int{}},
{d2enum.RegionAct5Siege, 865, 879, []int{}},
{d2enum.RegionAct5Barricade, 880, 1002, []int{}},
{d2enum.RegionAct5IceCaves, 1003, 1041, []int{}},
{d2enum.RegionAct5Temple, 1042, 1052, []int{}},
{d2enum.RegionAct5Baal, 1059, 1090, []int{}},
{d2enum.RegionAct5Lava, 1053, 1058, []int{}},
// Act V -- broken or wrong order
{d2enum.RegonAct5Town, 863, 864, []int{}},
{d2enum.RegionAct5Siege, 865, 879, []int{}},
{d2enum.RegionAct5Barricade, 880, 1002, []int{}},
{d2enum.RegionAct5IceCaves, 1003, 1041, []int{}},
{d2enum.RegionAct5Temple, 1042, 1052, []int{}},
{d2enum.RegionAct5Baal, 1059, 1090, []int{}},
{d2enum.RegionAct5Lava, 1053, 1058, []int{}},
}
}
// MapEngineTest represents the MapEngineTest screen
@ -126,9 +129,9 @@ func CreateMapEngineTest(currentRegion,
func (met *MapEngineTest) loadRegionByIndex(n, levelPreset, fileIndex int) {
log.Printf("Loaded region: Type(%d) LevelPreset(%d) FileIndex(%d)", n, levelPreset, fileIndex)
d2maprenderer.InvalidateImageCache()
met.mapRenderer.InvalidateImageCache()
for _, spec := range regions {
for _, spec := range getRegions() {
if spec.regionType != d2enum.RegionIdType(n) {
continue
}
@ -421,7 +424,7 @@ func (met *MapEngineTest) OnKeyDown(event d2interface.KeyEvent) bool {
met.levelPreset = increment(met.levelPreset, met.regionSpec.startPresetIndex, met.regionSpec.endPresetIndex)
d2screen.SetNextScreen(met)
default:
met.currentRegion = increment(met.currentRegion, 0, len(regions))
met.currentRegion = increment(met.currentRegion, 0, len(getRegions()))
d2screen.SetNextScreen(met)
}
@ -437,7 +440,7 @@ func (met *MapEngineTest) OnKeyDown(event d2interface.KeyEvent) bool {
met.levelPreset = decrement(met.levelPreset, met.regionSpec.startPresetIndex, met.regionSpec.endPresetIndex)
d2screen.SetNextScreen(met)
default:
met.currentRegion = decrement(met.currentRegion, 0, len(regions))
met.currentRegion = decrement(met.currentRegion, 0, len(getRegions()))
d2screen.SetNextScreen(met)
}

View File

@ -1,15 +1,16 @@
package d2player
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2gui"
"image"
"image/color"
"log"
"math"
"time"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2gui"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
@ -29,12 +30,13 @@ type Panel interface {
Close()
}
// ID of missile to create when user right clicks.
var missileID = 59
var expBarWidth = 120.0
var staminaBarWidth = 102.0
var globeHeight = 80
var globeWidth = 80
const (
initialMissileID = 59
expBarWidth = 120.0
staminaBarWidth = 102.0
globeHeight = 80
globeWidth = 80
)
var leftMenuRect = d2common.Rectangle{Left: 0, Top: 0, Width: 400, Height: 600}
var rightMenuRect = d2common.Rectangle{Left: 400, Top: 0, Width: 400, Height: 600}
@ -51,6 +53,7 @@ type GameControls struct {
FreeCam bool
lastMouseX int
lastMouseY int
missileID int // ID of missile to create when user right clicks.
// UI
globeSprite *d2ui.Sprite
@ -85,6 +88,7 @@ const (
func NewGameControls(renderer d2interface.Renderer, hero *d2mapentity.Player, mapEngine *d2mapengine.MapEngine,
mapRenderer *d2maprenderer.MapRenderer, inputListener InputCallbackListener, term d2interface.Terminal) *GameControls {
missileID := initialMissileID
term.BindAction("setmissile", "set missile id to summon on right click", func(id int) {
missileID = id
})
@ -118,7 +122,7 @@ func NewGameControls(renderer d2interface.Renderer, hero *d2mapentity.Player, ma
default:
inventoryRecordKey = "Amazon2"
}
inventoryRecord := d2datadict.Inventory[inventoryRecordKey]
gc := &GameControls{
@ -129,6 +133,7 @@ func NewGameControls(renderer d2interface.Renderer, hero *d2mapentity.Player, ma
mapRenderer: mapRenderer,
inventory: NewInventory(inventoryRecord),
heroStatsPanel: NewHeroStatsPanel(renderer, hero.Name(), hero.Class, hero.Stats),
missileID: missileID,
nameLabel: &nameLabel,
zoneChangeText: &zoneLabel,
actionableRegions: []ActionableRegion{
@ -225,11 +230,11 @@ func (g *GameControls) OnMouseButtonRepeat(event d2interface.MouseEvent) bool {
button := event.Button()
isLeft := button == d2enum.MouseButtonLeft
isRight := button == d2enum.MouseButtonRight
lastLeft:= now-lastLeftBtnActionTime
lastRight:= now-lastRightBtnActionTime
lastLeft := now - lastLeftBtnActionTime
lastRight := now - lastRightBtnActionTime
inRect := !g.isInActiveMenusRect(event.X(), event.Y())
shouldDoLeft := lastLeft >= mouseBtnActionsTreshhold
shouldDoRight := lastRight >= mouseBtnActionsTreshhold
shouldDoLeft := lastLeft >= mouseBtnActionsTreshhold
shouldDoRight := lastRight >= mouseBtnActionsTreshhold
if isLeft && shouldDoLeft && inRect {
lastLeftBtnActionTime = now
@ -254,7 +259,7 @@ func (g *GameControls) OnMouseButtonRepeat(event d2interface.MouseEvent) bool {
if isRight && shouldDoRight && inRect {
lastRightBtnActionTime = now
g.inputListener.OnPlayerCast(missileID, px, py)
g.inputListener.OnPlayerCast(g.missileID, px, py)
return true
}
@ -298,7 +303,7 @@ func (g *GameControls) OnMouseButtonDown(event d2interface.MouseEvent) bool {
if event.Button() == d2enum.MouseButtonRight && !g.isInActiveMenusRect(mx, my) {
lastRightBtnActionTime = d2common.Now()
g.inputListener.OnPlayerCast(missileID, px, py)
g.inputListener.OnPlayerCast(g.missileID, px, py)
return true
}