-
Notifications
You must be signed in to change notification settings - Fork 0
/
activeobject.go
230 lines (203 loc) · 8.26 KB
/
activeobject.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2014,2015,2016,2017,2018,2019,2020,2021 SeukWon Kang (kasworld@gmail.com)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package activeobject manage actor object
package activeobject
import (
"fmt"
"unsafe"
"github.com/kasworld/g2rand"
"github.com/kasworld/goguelike-single/config/gameconst"
"github.com/kasworld/goguelike-single/config/gamedata"
"github.com/kasworld/goguelike-single/enum/achievetype"
"github.com/kasworld/goguelike-single/enum/achievetype_vector_float64"
"github.com/kasworld/goguelike-single/enum/aotype"
"github.com/kasworld/goguelike-single/enum/condition_vector_int"
"github.com/kasworld/goguelike-single/enum/factiontype"
"github.com/kasworld/goguelike-single/enum/fieldobjacttype_vector_int"
"github.com/kasworld/goguelike-single/enum/potiontype"
"github.com/kasworld/goguelike-single/enum/potiontype_vector_int"
"github.com/kasworld/goguelike-single/enum/respawntype"
"github.com/kasworld/goguelike-single/enum/scrolltype"
"github.com/kasworld/goguelike-single/enum/scrolltype_vector_int"
"github.com/kasworld/goguelike-single/enum/towerachieve_vector_float64"
"github.com/kasworld/goguelike-single/enum/turnaction_vector_int"
"github.com/kasworld/goguelike-single/game/activeobject/activebuff"
"github.com/kasworld/goguelike-single/game/activeobject/aoturndata"
"github.com/kasworld/goguelike-single/game/activeobject/turnresult"
"github.com/kasworld/goguelike-single/game/aoactreqrsp"
"github.com/kasworld/goguelike-single/game/bias"
"github.com/kasworld/goguelike-single/game/carryingobject"
"github.com/kasworld/goguelike-single/game/csprotocol"
"github.com/kasworld/goguelike-single/game/floor4clientman"
"github.com/kasworld/goguelike-single/game/gamei"
"github.com/kasworld/goguelike-single/game/inventory"
"github.com/kasworld/goguelike-single/lib/idu64str"
"github.com/kasworld/uuidstr"
)
var SysAOIDMaker = idu64str.New("SysAOID")
// var UserAOIDMaker = idu64str.New("UserAOID")
var _ gamei.ActiveObjectI = &ActiveObject{}
func (ao *ActiveObject) String() string {
return fmt.Sprintf("ActiveObject[%v %v]",
ao.nickName,
ao.uuid,
)
}
// ActiveObject is act by ai or user action
type ActiveObject struct {
// mutex sync.RWMutex `prettystring:"hide"`
rnd *g2rand.G2Rand `prettystring:"hide"`
uuid string // aouuid
nickName string
homefloor gamei.FloorI
aoType aotype.ActiveObjType
respawnType respawntype.RespawnType
bornFaction factiontype.FactionType `prettystring:"simple"`
ai *ServerAIState // for server side ai
isAIInUse bool
towerAchieveStat *towerachieve_vector_float64.TowerAchieveVector_float64 `prettystring:"simple"`
achieveStat achievetype_vector_float64.AchieveTypeVector_float64 `prettystring:"simple"`
potionStat potiontype_vector_int.PotionTypeVector_int `prettystring:"simple"`
scrollStat scrolltype_vector_int.ScrollTypeVector_int `prettystring:"simple"`
foActStat fieldobjacttype_vector_int.FieldObjActTypeVector_int `prettystring:"simple"`
aoActionStat turnaction_vector_int.TurnActionVector_int `prettystring:"simple"`
conditionStat condition_vector_int.ConditionVector_int `prettystring:"simple"`
// info known to activeobject
floor4ClientMan *floor4clientman.Floor4ClientMan `prettystring:"simple"`
currentFloor gamei.FloorI
remainTurn2Rebirth int
chat string
chatOldTurnCount int
ap float64 // action point to use, -inf ~ 1
// battle relate
battleExp float64
currentBias bias.Bias `prettystring:"simple"`
hp float64
sp float64
inven *inventory.Inventory
buffManager *activebuff.BuffManager
expCopy4Sort float64
// valid in a Turn
needTANoti bool // ao move, sight change , floor change, floor aged .. etc
turnResultList []turnresult.TurnResult
req2Handle unsafe.Pointer // aoactreqrsp.Act
turnActReqRsp *aoactreqrsp.ActReqRsp `prettystring:"simple"`
aoClientCache *csprotocol.ActiveObjClient `prettystring:"simple"`
AOTurnData *aoturndata.ActiveObjTurnData
}
func newActiveObj(
seed int64,
homefloor gamei.FloorI,
towerAchieveStat *towerachieve_vector_float64.TowerAchieveVector_float64,
) *ActiveObject {
ao := &ActiveObject{
rnd: g2rand.NewWithSeed(seed),
towerAchieveStat: towerAchieveStat,
homefloor: homefloor,
// battle
hp: 100,
sp: 100,
inven: inventory.New(towerAchieveStat),
buffManager: activebuff.New(),
floor4ClientMan: floor4clientman.New(),
AOTurnData: &aoturndata.ActiveObjTurnData{},
}
ao.bornFaction = factiontype.FactionType(ao.rnd.Intn(factiontype.FactionType_Count))
ao.currentBias = bias.Bias(ao.bornFaction.FactorBase()).MakeAbsSumTo(gameconst.ActiveObjBaseBiasLen)
return ao
}
func NewUserActiveObj(seed int64, homefloor gamei.FloorI, nickname string,
towerAchieveStat *towerachieve_vector_float64.TowerAchieveVector_float64,
) *ActiveObject {
ao := newActiveObj(seed, homefloor, towerAchieveStat)
ao.uuid = uuidstr.New()
// ao.uuid = UserAOIDMaker.New()
ao.nickName = nickname
ao.isAIInUse = false
ao.aoType = aotype.User
ao.respawnType = respawntype.ToCurrentFloor
ao.ai = ao.NewServerAI()
ao.addRandFactionCarryObjEquip(ao.nickName, ao.currentBias.NearFaction(), gameconst.InitCarryObjEquipCount*2)
ao.addRandPotion(gameconst.InitPotionCount * 2)
ao.addRandScroll(gameconst.InitScrollCount * 2)
ao.addInitGold()
ao.updateActiveObjTurnData()
return ao
}
func NewSystemActiveObj(seed int64, homefloor gamei.FloorI,
towerAchieveStat *towerachieve_vector_float64.TowerAchieveVector_float64,
) *ActiveObject {
ao := newActiveObj(seed, homefloor, towerAchieveStat)
ao.uuid = SysAOIDMaker.New()
ao.nickName = gamedata.ActiveObjNameList[ao.rnd.Intn(len(gamedata.ActiveObjNameList))]
ao.isAIInUse = true
ao.aoType = aotype.System
ao.respawnType = respawntype.ToHomeFloor
ao.ai = ao.NewServerAI()
ao.addRandFactionCarryObjEquip(ao.nickName, ao.currentBias.NearFaction(), gameconst.InitCarryObjEquipCount)
ao.addRandPotion(gameconst.InitPotionCount)
ao.addRandScroll(gameconst.InitScrollCount)
ao.addInitGold()
ao.updateActiveObjTurnData()
return ao
}
func (ao *ActiveObject) Cleanup() {
ao.isAIInUse = false
}
func (ao *ActiveObject) Rebirth() {
ao.hp = ao.AOTurnData.HPMax * gameconst.RebirthHPRate
ao.sp = ao.AOTurnData.SPMax * gameconst.RebirthSPRate
ao.SetNeedTANoti()
if ao.aoType == aotype.System {
eqCount, potionCount, scrollCount := ao.inven.GetTypeCount()
if eqCount < gameconst.InitCarryObjEquipCount {
ao.addRandFactionCarryObjEquip(ao.nickName, ao.currentBias.NearFaction(), gameconst.InitCarryObjEquipCount-eqCount)
}
if potionCount < gameconst.InitPotionCount {
ao.addRandPotion(gameconst.InitPotionCount - potionCount)
}
if scrollCount < gameconst.InitScrollCount {
ao.addRandScroll(gameconst.InitScrollCount - scrollCount)
}
ao.addInitGold()
}
ao.ResetPlan(ao.ai)
}
func (ao *ActiveObject) addInitGold() {
if ao.inven.GetWalletValue() < gameconst.InitGoldMean {
v := ao.rnd.NormFloat64Range(gameconst.InitGoldMean, gameconst.InitGoldMean/2)
if v < 1 {
v = 1
}
ao.inven.AddToWallet(carryingobject.NewMoney(v))
ao.achieveStat.Add(achievetype.MoneyGet, float64(v))
}
}
func (ao *ActiveObject) addRandFactionCarryObjEquip(basename string, ft factiontype.FactionType, n int) {
for ; n > 0; n-- {
po := carryingobject.NewRandFactionEquipObj(basename, ft, ao.rnd)
ao.inven.AddToBag(po)
}
}
func (ao *ActiveObject) addRandPotion(n int) {
for ; n > 0; n-- {
n := ao.rnd.Intn(potiontype.TotalPotionMakeRate)
po := carryingobject.NewPotionByMakeRate(n)
ao.inven.AddToBag(po)
}
}
func (ao *ActiveObject) addRandScroll(n int) {
for ; n > 0; n-- {
n := ao.rnd.Intn(scrolltype.TotalScrollMakeRate)
po := carryingobject.NewScrollByMakeRate(n)
ao.inven.AddToBag(po)
}
}