def genFishs(self): self.genFishDatas = [] centerNLevelNRadius = [] for area in self.init_areas: centerP, direct, endP = area.initArea.getPointNDirect() count = len(area.fishLevels) levelNedges = [] for i in xrange(count): space = self.space level = random.choice(area.fishLevels[i]) levelData = FISH_LEVELS_DATA[level] if i == count - 1: centerP = centerP + (-direct) * (levelData.width/2.0) levelNedges.append((level, centerP)) else: width = levelData.width if i == count - 2: width = width/4.0 space = self.space * 2 elif i == 0: space = self.space else: space = self.space / 2 centerP = centerP + (-direct) * (width/2.0) levelNedges.append((level, centerP)) centerP = centerP + (-direct) * (width/2.0 + space) levelNRadius = [] for level, edgeP in levelNedges: levelNRadius.append((level, centerP.getDist(edgeP))) centerNLevelNRadius.append((centerP, levelNRadius)) longestDuration = 0 for idx, area in enumerate(self.init_areas): centerP, levelNRadius = centerNLevelNRadius[idx] initP, direct, endP = area.initArea.getPointNDirect() #获取初始角度 rad = direct.toRadian() initRot = math.degrees(rad) for i in xrange(len(levelNRadius)): level, radius = levelNRadius[i] deltaAngle = (math.pi*2)/area.counts[i] levelData = FISH_LEVELS_DATA[level] for i in xrange(area.counts[i]): offsetDir = direct.rotateSelfByRadian(deltaAngle*i).normalize() startP = centerP + (offsetDir*radius) curEndP = Point(endP.x, startP.y) + direct * (levelData.width/2.0) duration = curEndP.getDist(startP)/area.speed #优化,把初始化位置都设到屏幕外半个身位 realStartP = Point(initP.x, startP.y) + (-direct) * (levelData.width/2.0) realDuration = curEndP.getDist(realStartP)/area.speed if duration > longestDuration: longestDuration = duration self.genFishDatas.append(fish_array.FishInitData(0, level, levelData.order, initRot, \ realStartP.x, realStartP.y, realDuration, levelData.getMulti(), levelData.getPickedRate(), 0, \ pbAppendRoute([], 0, area.speed, realDuration + TOLERATE_LAG_SECS), \ fish_array.FISH_ARRAY_APPEAR_TICK + (duration - realDuration)*1000)) self.duration = longestDuration + TOLERATE_LAG_SECS super(FishArray, self).genFishs()
def load(self): """ """ fishesProto = fish_data_pb2.FishArrays() f = open(FISH_GEN_FILENAME, 'rb') fishesProto.ParseFromString(f.read()) f.close() idx = 0 for fishArray in fishesProto.fishArrays: fishDatas = [] for fish in fishArray.fishes: levelData = FISH_LEVELS_DATA[fish.level] fishDatas.append(fish_array.FishInitData(idx, fish.level, levelData.order, fish.rot, \ fish.x, fish.y, fish.duration, levelData.getMulti(), levelData.getPickedRate(), 0, \ None, fish.offset)) idx += 1 self.fishArrayDatas.append(FishArrayData(fishArray.duration, fishDatas))
def load(self): """ """ fishesProto = fish_data_pb2.FishBatches() f = open(FISH_GEN_FILENAME, 'rb') fishesProto.ParseFromString(f.read()) f.close() #print fishesProto.fishesBatch[0].fishes[0] idx = 0 for fishesBatch in fishesProto.fishesBatch: sampleFishs = [] for _fish in fishesBatch.fishes: levelData = FISH_LEVELS_DATA[_fish.level] fish = fish_array.FishInitData(idx, _fish.level, levelData.order, _fish.rot, \ _fish.x, _fish.y, _fish.duration, levelData.getMulti() * (_fish.dice if _fish.dice > 0 else 1), levelData.getPickedRate(), _fish.dice, None, _fish.offset) idx += 1 sampleFishs.append(fish) self.level2fishs[_fish.level].append(sampleFishs)
def genFishs(self): self.genFishDatas = [] for idx, initData in enumerate(self.initDatas): level = random.choice(initData.fishLevels) levelData = FISH_LEVELS_DATA[level] startP, direct, endP = self.initArea.getPointNDirect() deltaAngle = (math.pi*2)/initData.count self.duration = (self.longDist/initData.speed) + initData.time for i in xrange(initData.count): curDir = direct.rotateSelfByRadian(deltaAngle*i).normalize() #获取初始角度 rad = curDir.toRadian() initRot = math.degrees(rad) self.genFishDatas.append(fish_array.FishInitData(0, level, levelData.order, initRot, \ startP.x, startP.y, self.duration, levelData.getMulti(), levelData.getPickedRate(), 0, \ pbAppendRoute([], 0, initData.speed, self.duration + TOLERATE_LAG_SECS), \ fish_array.FISH_ARRAY_APPEAR_TICK + idx * self.interval * 1000)) self.dataIdx += 1 self.duration += idx * self.interval + TOLERATE_LAG_SECS super(FishArray, self).genFishs()
def genFishs(self): self.genFishDatas = [] centerLevelNPoints = [None] * len(self.initDatas) initP, direct, endP = self.initArea.getPointNDirect() centerP = initP * 1.0 initData = self.initDatas[0] #获取初始角度 rad = direct.toRadian() initRot = math.degrees(rad) level = random.choice(initData.fishLevels) levelData = FISH_LEVELS_DATA[level] centerP = centerP + (-direct) * (levelData.width / 2.0 + initData.radius) centerLevelNPoints[0] = (level, centerP) centerP = centerP + (-direct) * (levelData.width / 2.0 + initData.radius) initData = self.initDatas[1] level = random.choice(initData.fishLevels) centerLevelNPoints[1] = (level, centerP) initData = self.initDatas[3] level = random.choice(initData.fishLevels) centerLevelNPoints[3] = (level, centerP) initData = self.initDatas[2] level = random.choice(initData.fishLevels) levelData = FISH_LEVELS_DATA[level] centerP = centerP + (-direct) * (levelData.width / 2.0 + initData.radius) centerLevelNPoints[2] = (level, centerP) #外环大鱼 fishLevelAmbient = random.choice([20, 21]) levelDataAmbient = FISH_LEVELS_DATA[fishLevelAmbient] longestDuration = 0 endIdx = len(self.initDatas) - 1 for idx, initData in enumerate(self.initDatas): level, centerP = centerLevelNPoints[idx] deltaAngle = (math.pi * 2) / initData.count levelData = FISH_LEVELS_DATA[level] offsetRad = math.radians(initData.offsetAngle) for i in xrange(initData.count): offsetDir = direct.rotateSelfByRadian(offsetRad + deltaAngle * i).normalize() startP = centerP + (offsetDir * initData.radius) curEndP = Point(endP.x, startP.y) + direct * (levelData.width / 2.0) duration = curEndP.getDist(startP) / initData.speed #优化,把初始化位置都设到屏幕外半个身位 realStartP = Point( initP.x, startP.y) + (-direct) * (levelData.width / 2.0) realDuration = curEndP.getDist(realStartP) / initData.speed if duration > longestDuration: longestDuration = duration self.genFishDatas.append(fish_array.FishInitData(0, level, levelData.order, initRot, \ realStartP.x, realStartP.y, realDuration, levelData.getMulti(), levelData.getPickedRate(), 0, \ pbAppendRoute([], 0, initData.speed, realDuration + TOLERATE_LAG_SECS), \ fish_array.FISH_ARRAY_APPEAR_TICK + (duration - realDuration)*1000)) if idx == endIdx: if i in (1, 2): offsetDir = -direct else: offsetDir = direct startP = startP + (offsetDir * self.offsetInOut) curEndP = Point( endP.x, startP.y) + direct * (levelDataAmbient.width / 2.0) duration = curEndP.getDist(startP) / initData.speed #优化,把初始化位置都设到屏幕外半个身位 realStartP = Point( initP.x, startP.y) + (-direct) * (levelData.width / 2.0) realDuration = curEndP.getDist(realStartP) / initData.speed if duration > longestDuration: longestDuration = duration self.genFishDatas.append(fish_array.FishInitData(0, fishLevelAmbient, levelDataAmbient.order, initRot, \ realStartP.x, realStartP.y, realDuration, levelDataAmbient.getMulti(), levelDataAmbient.getPickedRate(), 0, \ pbAppendRoute([], 0, initData.speed, realDuration + TOLERATE_LAG_SECS), \ fish_array.FISH_ARRAY_APPEAR_TICK + (duration - realDuration)*1000)) self.duration = longestDuration + TOLERATE_LAG_SECS super(FishArray, self).genFishs()
def genFishs(self): longestDuration = 0 self.genFishDatas = [] for area in self.init_stop_areas: level = area.fishLevel levelData = FISH_LEVELS_DATA[level] #获取出生点、方向、离终点的距离数据 startP, direct, endP = area.initArea.getPointNDirect() stopP = area.stopPoint #获取初始角度 rad = direct.toRadian() initRot = math.degrees(rad) #让鱼不要出现在屏幕内 curStartP = startP + (-direct) * (levelData.width / 2.0) curEndP = endP + direct * (levelData.width / 2.0) #计算鱼生存时间 duration = curEndP.getDist(curStartP) / area.speed #优化,把初始化位置都设到屏幕外半个身位 realStartP = startP + (-direct) * (levelData.width / 2.0) realDuration = curEndP.getDist(realStartP) / area.speed #计算到停止点的时间 stopDuration = stopP.getDist(realStartP) / area.speed if duration + stopDuration > longestDuration: longestDuration = duration #节点生成 routes = [] pbAppendRoute(routes, 0, area.speed, stopDuration) pbAppendRoute(routes, 0, 0, area.stopTime) pbAppendRoute(routes, 0, area.speed, realDuration - stopDuration + TOLERATE_LAG_SECS) self.genFishDatas.append(fish_array.FishInitData(0, level, levelData.order, initRot, \ realStartP.x, realStartP.y, realDuration+stopDuration, levelData.getMulti(), levelData.getPickedRate(), 0, \ routes, fish_array.FISH_ARRAY_APPEAR_TICK + (duration - realDuration)*1000)) for area in self.init_rotate_areas: level = area.fishLevel startP, direct, endP = area.initArea.getPointNDirect() rotateP = area.rotatePoint rad = direct.toRadian() initRot = math.degrees(rad) curStartP = startP for i in xrange(area.count): curStartP = curStartP + (-direct) * (levelData.width / 2.0) curEndP = endP + direct * (levelData.width / 2.0) duration = curEndP.getDist(curStartP) / area.speed realStartP = startP + (-direct) * (levelData.width / 2.0) rotateDuration = rotateP.getDist(realStartP) / area.speed realDuration = curEndP.getDist(realStartP) / area.speed inRotateDuration = math.pi * area.radius * 2 * area.rounds * 1.00 / area.speed #旋转时间 rotSpeed = int(360 * area.rounds / inRotateDuration) if duration + inRotateDuration > longestDuration: longestDuration = duration routes = [] pbAppendRoute(routes, 0, area.speed, rotateDuration) pbAppendRoute(routes, rotSpeed, area.speed, inRotateDuration) pbAppendRoute( routes, 0, area.speed, realDuration - rotateDuration - inRotateDuration + TOLERATE_LAG_SECS) self.genFishDatas.append(fish_array.FishInitData(0, level, levelData.order, initRot, \ realStartP.x, realStartP.y, realDuration+inRotateDuration, levelData.getMulti(), levelData.getPickedRate(), 0, \ routes, fish_array.FISH_ARRAY_APPEAR_TICK + (duration - realDuration)*1000)) curStartP = curStartP + (-direct) * (area.space + levelData.width / 2.0) self.duration = longestDuration + TOLERATE_LAG_SECS super(FishArray, self).genFishs()
def genFishs(self): longestDuration = 0 self.genFishDatas = [] for index, area in enumerate(self.init_areas): #获取出生点、方向、离终点的距离数据 startP, direct, endP = area.initArea.getPointNDirect() #获取初始角度 rad = direct.toRadian() initRot = math.degrees(rad) count = 0 level = -1 curStartP = startP i = 0 while True: #获取等级,相邻不同鱼 # fishLevels = [l for l in area.fishLevels if l != level] fishLevels = [l for l in area.fishLevels] if not fishLevels: level = area.fishLevels[0] else: # level = random.choice(fishLevels) if i >= len(area.fishLevels): if index in (0, 1, 2): break i = 0 level = area.fishLevels[i] i += 1 levelData = FISH_LEVELS_DATA[level] #让鱼不要出现在屏幕内 curStartP = curStartP + (-direct) * (levelData.width / 2.0) curEndP = endP + direct * (levelData.width / 2.0) #计算鱼生存时间 duration = curEndP.getDist(curStartP) / area.speed #print "start[%s, %s] end[%s, %s] speed[%s] dist[%s] duration[%s]"%(curStartP.x, curStartP.y, \ # curEndP.x, curEndP.y, area.speed, dist, duration) #计算生成时间 durationGen = curStartP.getDist(startP) / area.speed if durationGen > self.duration: break #优化,把初始化位置都设到屏幕外半个身位 realStartP = startP + (-direct) * (levelData.width / 2.0) realDuration = curEndP.getDist(realStartP) / area.speed if duration > longestDuration: longestDuration = duration if index in (0, 2): spacePerHori = self.spacePerHori / 2 elif index == 1: i2spacePerHori = [ self.spacePerHori + 300, self.spacePerHori + 450, self.spacePerHori + 650, self.spacePerHori + 750 ] spacePerHori = i2spacePerHori[i - 1] else: spacePerHori = self.spacePerHori self.genFishDatas.append(fish_array.FishInitData(0, level, levelData.order, initRot, \ realStartP.x, realStartP.y, realDuration, levelData.getMulti(), levelData.getPickedRate(), 0, \ pbAppendRoute([], 0, area.speed, realDuration + TOLERATE_LAG_SECS), fish_array.FISH_ARRAY_APPEAR_TICK + (duration - realDuration)*1000)) curStartP = curStartP + (-direct) * ( (spacePerHori if area.speed == self.speedPerHori else self.spacePerVert) + levelData.width / 2.0) self.duration = longestDuration + TOLERATE_LAG_SECS super(FishArray, self).genFishs()
def generate(self): idx = 0 canGetArea4specail = FISh_INIT_AREAS[:] for level, levelData in enumerate(FISH_LEVELS_DATA): for i in xrange(self.maxSampleCount): sampleFishs = [] seqMode, countRange = levelData.getModeNCount() appearCount = random.randint(countRange[0], countRange[1]) #出生区域 if level == GOLD_TIME_FISH_LEVEL: goldTimeAreas = FISh_INIT_AREAS[-3:] goldTimeAreas.append(FISh_INIT_AREAS[0]) area = random.choice(goldTimeAreas) elif level in SPECIAL_AREA_LEVELS: if not canGetArea4specail: canGetArea4specail = FISh_INIT_AREAS[:] area = random.choice(canGetArea4specail) canGetArea4specail.remove(area) else: area = random.choice(FISh_INIT_AREAS) #log(u'start area[%s] areas[%s]'%(area, FISh_INIT_AREAS)) startP, direct, _ = area.getPointNDirect() rad = direct.toRadian() #log(u'start point[%s] direct[%s] rad[%s]'%(startP, direct, rad)) # flag #startP = startP + (-direct) * (levelData.width/2.0) #生成鱼 prevFish = None initRot = None firstSpeed = None firstX = None firstY = None firstDuration = None route = [] #log(u'fish level[%d] appear count[%d]'%(level, appearCount)) for i in xrange(appearCount): #计算dice点数 if appearCount == 1 and random.random( ) < levelData.dice_odds: dice = random.randint(1, 6) else: dice = 0 fish = None #第一只鱼才需要生成route if not prevFish: #随机一个旋转次数 rotTimes = levelData.getRotTimes() #初始朝向 initRot = math.degrees(rad) #初始速度 firstSpeed = levelData.getSpeed() #持续时间 firstDuration = duration = levelData.getPerTime() #log(u'first pos[%s,%s] rot[%s] rad[%s] speed[%s] duration[%s]'%(startP.x, startP.y, initRot, rad, firstSpeed, duration)) pbAppendRoute(route, 0, firstSpeed, duration) #log(u'dir change count[%s]'%(rotTimes)) if seqMode != FISH_SEQMODE_SIDEBYSIDE: lastIdx = rotTimes - 1 for j in xrange(rotTimes): rot = levelData.getRot() rotSpeed = levelData.getRotSpeed() if rot < 0: rotSpeed = -rotSpeed duration = levelData.getPerTime() speed = levelData.getSpeed() #算出旋转的时间 rotDuration = float(rot) / rotSpeed # log(u'rotate angle[%s] rotSpeed[%s] speed[%s] rotDuration[%s] totalDuration[%s]'% # (rot, rotSpeed, speed, rotDuration, duration)) pbAppendRoute(route, rotSpeed, speed, rotDuration) deltaDuration = duration - rotDuration #还剩余时间就是无角速度的移动 if deltaDuration > 0: pbAppendRoute(route, 0, speed, deltaDuration) #log(u'move extend sec[%s]'%(deltaDuration)) elif j == lastIdx: pbAppendRoute(route, 0, speed, 0) firstDuration += duration #最后一个节点设得更远,移动30秒试试 extSec = FISH_EXT_DURATION route[-1].duration += extSec firstDuration += extSec prevFish = fish = fish_array.FishInitData(idx, level, levelData.order, initRot, \ startP.x, startP.y, firstDuration, levelData.getMulti() * (dice if dice > 0 else 1), levelData.getPickedRate(), dice, route, 0) else: #复制路径 timestampDelta = 0 route = copy.deepcopy(prevFish.route) offsetPos = direct.perp() duration = firstDuration if seqMode == FISH_SEQMODE_ONEBYONE: #根据间隔计算速度和时间差 #timestampDelta = int(((levelData.width + levelData.seq_offset)*i/float(firstSpeed))*1000) offsetDir = -direct offset = (levelData.width + levelData.seq_offset) * i offsetPos = offsetDir * offset deltaDuration = offset / float(firstSpeed) route[0].duration += deltaDuration duration += deltaDuration elif seqMode == FISH_SEQMODE_SIDEBYSIDE: #根据方向计算间隔 offsetDir = direct.rotateSelfByRadian( -math.pi / 2.0).normalize() offsetPos = offsetDir * (levelData.height + levelData.seq_offset) * i elif seqMode == FISH_SEQMODE_COLONY: #根据方向计算间隔 offsetDir = direct.rotateSelfByRadian( random.uniform(math.pi / 2, math.pi + math.pi / 2)).normalize() offsetPos = offsetDir * random.randint( levelData.height, levelData.seq_offset + levelData.height) # straightRoute = [] # for rp in route: # if not rp.rotSpeed and rp.speed <= 100: # straightRoute.append(rp) # for rp in straightRoute: # swingCount = random.randint(1,4) # if not swingCount: # continue # durationPerSwing = rp.duration / (swingCount*2) # speed = rp.speed # idx = route.index(rp) # route.remove(rp) # for i in xrange(swingCount): # rotSpeed = random.randint(-10,10) # route.insert(idx, RouteNode(rotSpeed, speed, durationPerSwing)) # route.insert(idx, RouteNode(-rotSpeed, speed, durationPerSwing)) else: offsetPos.x = offsetPos.y = 0 print '--------------------------------------------' print 'x:%s ---- y:%s' % (startP.x, startP.y) print '--------------------------------------------' fish = fish_array.FishInitData(idx, level, levelData.order, initRot, \ startP.x + offsetPos.x, startP.y + offsetPos.y, \ duration, levelData.getMulti() * (dice if dice > 0 else 1), levelData.getPickedRate(), dice, route, timestampDelta) idx += 1 sampleFishs.append(fish) self.level2fishs[level].append(sampleFishs)
def genFishs(self): longestDuration = 0 self.genFishDatas = [] for area in self.init_areas: #获取出生点、方向、离终点的距离数据 startP, direct, endP = area.initArea.getPointNDirect() #获取初始角度 rad = direct.toRadian() initRot = math.degrees(rad) count = 0 level = -1 curStartP = startP while True: #获取等级,相邻不同鱼 fishLevels = [l for l in area.fishLevels if l != level] if not fishLevels: level = area.fishLevels[0] else: level = random.choice(fishLevels) levelData = FISH_LEVELS_DATA[level] #让鱼不要出现在屏幕内 curStartP = curStartP + (-direct) * (levelData.width / 2.0) curEndP = endP + direct * (levelData.width / 2.0) #计算鱼生存时间 duration = curEndP.getDist(curStartP) / area.speed #print "start[%s, %s] end[%s, %s] speed[%s] dist[%s] duration[%s]"%(curStartP.x, curStartP.y, \ # curEndP.x, curEndP.y, area.speed, dist, duration) #计算生成时间 durationGen = curStartP.getDist(startP) / area.speed if durationGen > self.duration: break #优化,把初始化位置都设到屏幕外半个身位 realStartP = startP + (-direct) * (levelData.width / 2.0) realDuration = curEndP.getDist(realStartP) / area.speed if duration > longestDuration: longestDuration = duration self.genFishDatas.append(fish_array.FishInitData(0, level, levelData.order, initRot, \ realStartP.x, realStartP.y, realDuration, levelData.getMulti(), levelData.getPickedRate(), 0, \ pbAppendRoute([], 0, area.speed, realDuration + TOLERATE_LAG_SECS), fish_array.FISH_ARRAY_APPEAR_TICK + (duration - realDuration)*1000)) curStartP = curStartP + (-direct) * (area.spacePerHori + levelData.width / 2.0) for area in self.other_init_areas: startP, direct, endP = area.initArea.getPointNDirect() rad = direct.toRadian() initRot = math.degrees(rad) level = random.choice(area.fishLevels) levelData = FISH_LEVELS_DATA[level] count4height, count4width = area.counts space4height, space4width = area.spaces startPs = [] #纵向鱼生成 startP4Hs = [] if count4height % 2: startP4Hs.append((startP, direct, endP)) for i in xrange(count4height / 2): addLen = (i + 1) * (levelData.height / 2.0 + space4height) realStartP = copy.deepcopy(startP) realStartP.y += addLen realEndP = copy.deepcopy(endP) realEndP.y += addLen startP4Hs.append((realStartP, direct, realEndP)) realStartP = copy.deepcopy(startP) realStartP.y -= addLen realEndP = copy.deepcopy(endP) realEndP.y -= addLen startP4Hs.append((realStartP, direct, realEndP)) #横向鱼生成 if count4width % 2: startPs.extend(startP4Hs) for i in xrange(count4width / 2): addLen = (i + 1) * (levelData.width / 2.0 + space4width) for _startP, _direct, _endP in startP4Hs: realStartP = copy.deepcopy(_startP) realStartP.x += addLen startPs.append((realStartP, _direct, realEndP)) realStartP = copy.deepcopy(_startP) realStartP.x -= addLen startPs.append((realStartP, _direct, realEndP)) for fishData in startPs: #让鱼不要出现在屏幕内 startP, direct, endP = fishData curStartP = startP + (-direct) * (levelData.width / 2.0) curEndP = endP + direct * (levelData.width / 2.0) #计算鱼生存时间 duration = curEndP.getDist(curStartP) / area.speed #计算生成时间 durationGen = curStartP.getDist(startP) / area.speed #优化,把初始化位置都设到屏幕外半个身位 realStartP = startP + (-direct) * (levelData.width / 2.0) realDuration = curEndP.getDist(realStartP) / area.speed if duration > longestDuration: longestDuration = duration self.genFishDatas.append(fish_array.FishInitData(0, level, levelData.order, initRot, \ realStartP.x, realStartP.y, realDuration, levelData.getMulti(), levelData.getPickedRate(), 0, \ pbAppendRoute([], 0, area.speed, realDuration + TOLERATE_LAG_SECS), fish_array.FISH_ARRAY_APPEAR_TICK + (duration - realDuration)*1000)) self.duration = longestDuration + TOLERATE_LAG_SECS super(FishArray, self).genFishs()