mDelx, mDelz = mSx - xm, mSz - zm #distance from monster mDist = math.sqrt(mDelx**2 + mDelz**2) mDx = -0.1 * mDelx / mDist mDz = -0.1 * mDelz / mDist monst.rotateIncY(100.0 / mDist) if mDist > 100: #far away so teleport it nearer mSx, mSz = xm + 100 * random.random() - 50, zm + 100 * random.random( ) - 50 if mSx < -mapwidth / 2: mSx = -mapwidth / 2 if mSx > mapwidth / 2: mSx = mapwidth / 2 if mSz < -mapdepth / 2: mSz = -mapdepth / 2 if mSz > mapdepth / 2: mSz = mapdepth / 2 if mDist < 3: #it's got you, return to GO xm, ym, zm = 0, mymap.calcHeight(0, 0) + avhgt, 0 clash = mymap.clashTest(mSx, mSy, mSz, 1.5) if clash[0]: # returns the components of normal vector if clash nx, ny, nz = clash[1], clash[2], clash[3] # move it away a bit to stop it getting trapped inside if it has tunelled jDist = clash[4] + 0.1 mSx, mSy, mSz = mSx + jDist * nx, mSy + jDist * ny, mSz + jDist * nz # use R = I - 2(N.I)N rfact = 2.05 * ( nx * mDx + ny * mDy + nz * mDz ) #small extra boost by using value > 2 to top up energy in defiance of 1st LOT mDx, mDy, mDz = mDx - rfact * nx, mDy - rfact * ny * 0.8, mDz - rfact * nz # stop the speed increasing too much if mDx > 0.4: dsz = 0.4 if mDy > 0.1: dsx = 0.05
drx = sx - rx if abs(drx) > max_speed: drx = drx / abs(drx) * max_speed dry = sy - ry if abs(dry) > max_speed: dry = dry / abs(dry) * max_speed rx += drx ry += dry monster.position(rx, ry, maphalf) dsy -= gravity sx += dsx sy += dsy sz += dsz # now uses the clashTest method from elevationMap clash = mymap.clashTest(sx, sy, sz, radius) # bouncing physics if clash[0]: # returns the components of normal vector if clash nx, ny, nz = clash[1], clash[2], clash[3] # move it away a bit to stop it getting trapped inside if it has tunelled jDist = clash[4] + 0.2 sx, sy, sz = sx + jDist * nx, sy + jDist * ny, sz + jDist * nz # use R = I - 2(N.I)N rfact = 2.05 * ( nx * dsx + ny * dsy + nz * dsz ) #small extra boost by using value > 2 to top up energy in defiance of 1st LOT dsx, dsy, dsz = dsx - rfact * nx, dsy - rfact * ny, dsz - rfact * nz # stop the speed increasing too much if dsz > 0.4: dsz = 0.4 if dsx > 0.3: dsx = 0.2
mDy -= gravity mDelx,mDelz = mSx-xm, mSz-zm #distance from monster mDist = math.sqrt(mDelx**2 + mDelz**2) mDx = -0.1*mDelx/mDist mDz = -0.1*mDelz/mDist monst.rotateIncY(100.0/mDist) if mDist > 100: #far away so teleport it nearer mSx, mSz = xm + 100*random.random() - 50, zm + 100*random.random() - 50 if mSx < -mapwidth/2: mSx = -mapwidth/2 if mSx > mapwidth/2: mSx = mapwidth/2 if mSz < -mapdepth/2: mSz = -mapdepth/2 if mSz > mapdepth/2: mSz = mapdepth/2 if mDist < 3: #it's got you, return to GO xm, ym, zm = 0, mymap.calcHeight(0,0) + avhgt, 0 clash = mymap.clashTest(mSx, mSy, mSz, 1.5) if clash[0]: # returns the components of normal vector if clash nx, ny, nz = clash[1], clash[2], clash[3] # move it away a bit to stop it getting trapped inside if it has tunelled jDist = clash[4] + 0.1 mSx, mSy, mSz = mSx + jDist*nx, mSy + jDist*ny, mSz + jDist*nz # use R = I - 2(N.I)N rfact = 2.05*(nx*mDx + ny*mDy + nz*mDz) #small extra boost by using value > 2 to top up energy in defiance of 1st LOT mDx, mDy, mDz = mDx - rfact*nx, mDy - rfact*ny*0.8, mDz - rfact*nz # stop the speed increasing too much if mDx > 0.4: dsz = 0.4 if mDy > 0.1: dsx = 0.05 if mDz > 0.4: mDz = 0.4 mSx += mDx
#monster movement drx = sx - rx if abs(drx) > max_speed: drx = drx/abs(drx) * max_speed dry = sy - ry if abs(dry) > max_speed: dry = dry/abs(dry) * max_speed rx += drx ry += dry monster.position(rx, ry, -maphalf) dsy -= gravity sx += dsx sy += dsy sz += dsz # now uses the clashTest method from elevationMap clash = mymap.clashTest(sx, sy, sz, radius) # bouncing physics if clash[0]: # returns the components of normal vector if clash nx, ny, nz = clash[1], clash[2], clash[3] # move it away a bit to stop it getting trapped inside if it has tunelled jDist = clash[4] + 0.1 sx, sy, sz = sx - jDist*nx, sy - jDist*ny, sz - jDist*nz # use R = I - 2(N.I)N rfact = 2.05*(nx*dsx + ny*dsy + nz*dsz) #small extra boost by using value > 2 to top up energy in defiance of 1st LOT dsx, dsy, dsz = dsx - rfact*nx, dsy - rfact*ny, dsz - rfact*nz # stop the speed increasing too much if dsz > 0.4: dsz = 0.4 if dsx > 0.3: dsx = 0.2 if dsz > maxdsz: dsz = maxdsz
mDy -= gravity mDelx,mDelz = mSx+xm, mSz+zm #distance from monster mDist = math.sqrt(mDelx**2 + mDelz**2) mDx -= 0.01*mDelx/mDist mDz -= 0.01*mDelz/mDist monst.rotateIncY(100.0/mDist) if mDist > 100: #far away so teleport it nearer mSx, mSz = -xm + 100*random.random() - 50, -zm + 100*random.random() - 50 if mSx < -mapwidth/2: mSx = -mapwidth/2 if mSx > mapwidth/2: mSx = mapwidth/2 if mSz < -mapdepth/2: mSz = -mapdepth/2 if mSz > mapdepth/2: mSz = mapdepth/2 if mDist < 3: #it's got you, return to GO xm, ym, zm = 0, -(mymap.calcHeight(0,0)+avhgt), 0 clash = mymap.clashTest(monst.x, monst.y, monst.z, 1.5) if clash[0]: # returns the components of normal vector if clash nx, ny, nz = clash[1], clash[2], clash[3] # move it away a bit to stop it getting trapped inside if it has tunelled jDist = clash[4] + 0.1 mSx, mSy, mSz = mSx - jDist*nx, mSy - jDist*ny, mSz - jDist*nz # use R = I - 2(N.I)N rfact = 2.05*(nx*mDx + ny*mDy + nz*mDz) #small extra boost by using value > 2 to top up energy in defiance of 1st LOT mDx, mDy, mDz = mDx - rfact*nx, mDy - rfact*ny*0.7, mDz - rfact*nz # stop the speed increasing too much if mDx > 0.4: dsz = 0.4 if mDy > 0.1: dsx = 0.05 if mDz > 0.4: mDz = 0.4 mSx += mDx