From 86acb03ad9955d47238e0e8c64c0fabc82c06895 Mon Sep 17 00:00:00 2001 From: meixiongfeng <766881921@qq.com> Date: Tue, 7 Nov 2023 17:48:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=A4=E4=B8=AA=E7=89=B9=E6=AE=8A=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E5=9C=A8=E4=B8=80=E8=B5=B7=E4=B9=9F=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E6=B6=88=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/entertainment/room.go | 24 ++-- modules/entertainment/xxlPlat.go | 216 +++++++++++++++++++------------ 2 files changed, 150 insertions(+), 90 deletions(-) diff --git a/modules/entertainment/room.go b/modules/entertainment/room.go index 199003d2b..a19b1726a 100644 --- a/modules/entertainment/room.go +++ b/modules/entertainment/room.go @@ -295,22 +295,28 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr this.player2.Ps = 0 } } else { + bCanChange := false oid1 = this.chessboard.Plat[req.Curid].Oid oid2 = this.chessboard.Plat[req.Targetid].Oid - // 交换元素 - if b := this.chessboard.SwapGirde(req.Curid, req.Targetid); !b { // 交换格子 - errdata = &pb.ErrorData{ - Code: pb.ErrorCode_EntertainCantSwap, // 不能交换 直接返回 - Title: pb.ErrorCode_EntertainCantSwap.ToString(), + if b, m := this.chessboard.CheckSpecialElemChange(req.Curid, req.Targetid, color); !b { + // 交换元素 + if b = this.chessboard.SwapGirde(req.Curid, req.Targetid); !b { // 交换格子 + errdata = &pb.ErrorData{ + Code: pb.ErrorCode_EntertainCantSwap, // 不能交换 直接返回 + Title: pb.ErrorCode_EntertainCantSwap.ToString(), + } + return } - return + } else { + bCanChange = true + szMap = append(szMap, m...) } - if m, b := this.chessboard.CheckMap(color, true); len(m) > 0 { - // curScore += score szMap = append(szMap, m...) bAddPs = b - } else { // 不能消除 + bCanChange = true + } + if !bCanChange { // 不能消除 this.chessboard.SwapGirde(req.Targetid, req.Curid) // 换到原来的位置 errdata = &pb.ErrorData{ Code: pb.ErrorCode_EntertainCantSwap, // 不能交换 直接返回 diff --git a/modules/entertainment/xxlPlat.go b/modules/entertainment/xxlPlat.go index 0359d353c..6f9f02ed1 100644 --- a/modules/entertainment/xxlPlat.go +++ b/modules/entertainment/xxlPlat.go @@ -51,73 +51,32 @@ func (this *MapData) GetRandType() int32 { } // 玩法带入 -func (this *MapData) CreateGride(index int32, bDrop bool) (girde *pb.GirdeData) { +func (this *MapData) CreateGride(count int) (girdes []*pb.GirdeData) { var ( id int32 conf *cfg.GameBlockData err error ) // 权重带入 - this.oid++ - girde = &pb.GirdeData{ // 默认值 - Oid: this.oid, - Color: 1, - Cid: 1, - Score: 1, - Special: 0, - } - if this.iType == 4 && bDrop { - var count int32 - for _, v := range this.Plat { - if v.Special == 5 || v.Special == 6 || - v.Special == 7 || v.Special == 8 { - count++ - if count >= 2 { - break - } - } - } - if count < 2 { - n, _ := rand.Int(rand.Reader, big.NewInt(6)) // 随机一个颜色 - n1, _ := rand.Int(rand.Reader, big.NewInt(4)) // 随机一个消除类型 - if conf, err := this.module.configure.GetGameBlock(int32(n.Int64()+1), int32(n1.Int64()+5)); err == nil { - id = conf.Key - } - } else { - id = this.GetRandType() + for i := 0; i < count; i++ { + this.oid++ + girde := &pb.GirdeData{ // 默认值 + Oid: this.oid, + Color: 1, + Cid: 1, + Score: 1, + Special: 0, } - } else if this.iType == 3 && bDrop { // 创建棋盘的时候此玩法类型执行效率低下 稍后采用同颜色替换模式处理 - // 最多2个 - var count int32 - for _, v := range this.Plat { - if v.Special == 4 { - count++ - if count >= 2 { - break - } - } - } - if count < 2 { - n, _ := rand.Int(rand.Reader, big.NewInt(6)) // 随机一个颜色 - if conf, err := this.module.configure.GetGameBlock(int32(n.Int64()+1), 4); err == nil { - id = conf.Key - } - } else { - id = this.GetRandType() - } - - } else { id = this.GetRandType() + conf, err = this.module.configure.GetGameBlockByKey(id) + if err == nil { + girde.Color = conf.Color + girde.Cid = conf.Key + girde.Special = conf.Type + girde.Score = conf.Score + girdes = append(girdes, girde) + } } - conf, err = this.module.configure.GetGameBlockByKey(id) - if err != nil { - return - } - - girde.Color = conf.Color - girde.Cid = conf.Key - girde.Special = conf.Type - girde.Score = conf.Score return } @@ -127,8 +86,8 @@ func (this *MapData) InitMap(module *Entertainment, iType int32) { this.iType = iType this.oid = 1000 // 方便观察 从1000开始 this.Plat = make([]*pb.GirdeData, Width*Height) - for i := 0; i < Width*Height; i++ { - this.Plat[i] = this.CreateGride(int32(i), false) + for i, v := range this.CreateGride(Total) { + this.Plat[i] = v } if iType > 1 { var sp int32 @@ -161,6 +120,60 @@ func (this *MapData) InitMap(module *Entertainment, iType int32) { //this.SetMap() // 方便测试固定地图 } +// 交换之前先判断是不是特殊元素的交换 +func (this *MapData) CheckSpecialElemChange(oldId, newId int32, color int32) (bSwap bool, szMap []*pb.MapData) { + this.operElem = []int32{} // 初始化 + if (oldId%Height+1 < Total && oldId+1 == newId) || + (oldId%Height > 0 && oldId-1 == newId) || + (oldId+Width < Total && oldId+Width == newId) || + (oldId-Width >= 0 && oldId-Width == newId) { + bSwap = true + x := make(map[int]struct{}) + if this.Plat[newId].Special != 0 && this.Plat[oldId].Special != 0 { // 都不为0 + tmp := new(pb.GirdeData) + *tmp = *this.Plat[newId] + this.Plat[newId] = this.Plat[oldId] + this.Plat[oldId] = tmp + this.operElem = append(this.operElem, newId) + this.operElem = append(this.operElem, oldId) + x = this.SpecialElem(int(oldId), this.Plat[oldId].Special) + xc := this.SpecialElem(int(newId), this.Plat[newId].Special) + for k := range xc { + x[k] = struct{}{} + } + var energy int32 + var curScore int32 + for id := range x { + if this.Plat[id].Color == color { + energy++ + } + curScore += this.Plat[id].Score + this.Plat[id] = &pb.GirdeData{} + this.operElem = append(this.operElem, int32(id)) + } + for id := range x { + if this.Plat[id].Color == color { + energy++ + } + curScore += this.Plat[id].Score + this.Plat[id] = &pb.GirdeData{} + this.operElem = append(this.operElem, int32(id)) + } + if this.DropGirde() { + + szMap = append(szMap, &pb.MapData{ + Data: this.GetPalatData(), + CurSocre: curScore, + CurEnergy: energy, + }) + } + } else { + bSwap = false + } + } + return +} + // 交换2个元素(参数 oid ) func (this *MapData) SwapGirde(oldId, newId int32) (bSwap bool) { this.operElem = []int32{} // 初始化 @@ -198,13 +211,7 @@ func (this *MapData) Check5X() (bEliminate bool, xiaochu []int, s map[int]int) { //var xiaochu []int // 即将消除的key s = make(map[int]int) for k, v := range this.Plat { - // b := false - // for _, e := range this.operElem { - // if e == int32(k) { - // b = true - // break - // } - // } + if v.Cid == 0 { continue } @@ -256,15 +263,8 @@ func (this *MapData) Check4X() (bEliminate bool, xiaochu []int, s map[int]int) { ) s = make(map[int]int) - //var xiaochu []int // 即将消除的key for k, v := range this.Plat { - // b := false - // for _, e := range this.operElem { - // if e == int32(k) { - // b = true - // break - // } - // } + if v.Cid == 0 { continue } @@ -478,8 +478,6 @@ func (this *MapData) CheckMap(color int32, bSkill bool) (szMap []*pb.MapData, xc CurSocre: curScore, CurEnergy: energy, }) - //this.Debugf() - } // 检查掉落 @@ -493,6 +491,8 @@ func (this *MapData) CheckMap(color int32, bSkill bool) (szMap []*pb.MapData, xc // 下落 生成新的格子 (返回掉落所获得的分数) func (this *MapData) DropGirde() bool { + // 需要填充的格子 + var fill []int bDrop := false for i := 0; i < Width; i++ { for j := 0; j < Height; j++ { @@ -510,12 +510,66 @@ func (this *MapData) DropGirde() bool { } for m := j + add; m < Height; m++ { k1 := i*Width + m - this.Plat[k1] = this.CreateGride(int32(k1), true) + fill = append(fill, k1) + //this.Plat[k1] = this.CreateGride(true) } break } } } + sz := this.CreateGride(len(fill)) + for pos, id := range fill { + this.Plat[id] = sz[pos] + } + if this.iType > 1 { + var sp int32 + var mp map[int]struct{} + mp = make(map[int]struct{}, 0) + var count int32 + + for pos, v := range this.Plat { + if this.iType == 4 { + if v.Special == 5 || v.Special == 6 || + v.Special == 7 || v.Special == 8 { + mp[pos] = struct{}{} + count++ + if count >= 2 { + break + } + } + } else if this.iType == 3 { + if v.Special == 4 { + mp[pos] = struct{}{} + count++ + if count >= 2 { + break + } + } + } + } + if len(mp) < 2 { + for { + n1, _ := rand.Int(rand.Reader, big.NewInt(int64(len(fill)))) + mp[fill[n1.Int64()]] = struct{}{} + if len(mp) >= 2 { + break + } + } + if this.iType == 3 { + sp = 4 + } else if this.iType == 4 { + n1, _ := rand.Int(rand.Reader, big.NewInt(4)) // 随机一个消除类型 + sp = int32(n1.Int64()) + 5 + } + for key := range mp { + if conf, err := this.module.configure.GetGameBlock(this.Plat[key].Color, sp); err == nil { + this.Plat[key].Cid = conf.Key + this.Plat[key].Special = conf.Type + this.Plat[key].Score = conf.Score + } + } + } + } return bDrop } @@ -690,9 +744,9 @@ func (this *MapData) SetMap() { 5, 1, 2, 3, 1, 2, 2, 3, 4, 3, 5, 6, 1, 6, 1, 4, 1, 4, 2, 3, 6, - 1, 5, 1, 5, 6, 1, 4, - 6, 6, 4, 6, 4, 3, 3, - 3, 3, 1, 2, 1, 2, 5, + 1, 5, 6, 5, 6, 1, 4, + 6, 6, 3, 6, 4, 3, 3, + 3, 3, 1, 3, 1, 2, 5, // 1, 4, 2, 5, 4, 5, 2, // 3, 2, 3, 5, 2, 1, 4, // 2, 5, 5, 4, 5, 4, 1,