首页 > 其他分享 >maxscript 自动寻路

maxscript 自动寻路

时间:2022-10-09 15:23:01浏览次数:48  
标签:do string maxscript wirecolor -- wireColor 自动 pressed 寻路

try(destroyDialog autoFinderRoll)catch()

rollout autoFinderRoll "遍历寻路"
(
	button btnCreateScene "创建场景" width:120
	
	button btnSetGround "设置地面[灰色]" width:120
	button btnSetWall "设置墙体[黑色]" width:120
	button btnSetPlayer "设置玩家[黄色]" width:120
	button btnSetTarget "设置目标[蓝色]" width:120
	button btnStartFind "开始寻路" width:120
	button btnReset "重置-路径[绿色]" width:120
	button btnResetDeadPath "重置-死路[红色]" width:120

	button btnCreateSceneRandom "创建随机场景运行" width:140
	button btnRandomBoom "随机爆炸一次" width:140

	local _playerObject
	local _targetObject

	local groundColor = gray
	local wallColor = black
	local playerColor = yellow
	local targetColor = blue
	local pathColor = green
	local errorColor = red
	on btnCreateScene pressed do 
	(
		delete objects
		xCount = 12
		yCount = 12
		size = 10
		for x = 1 to xCount do 
		(
			for y = 1 to yCount do 
			(
				objName = x as string + "_" +  y as string
				
				p = Plane length:size width:size widthsegs:1 lengthsegs:1 name:objName
				p.pos = [x * size, y * size, 0] 
				p.wirecolor = groundColor
				--锁住物体不让手动移动
				setTransformLockFlags p #all
			)
		)
		max select
	)

	
	fn getNode objName = 
	(
		o = getNodeByName objName
		--if o != undefined do select o
		return o
	)

	--在物体周边2个点中,获取可用的物体
	fn getNearOkObjects curObj =
	(
		xy = FilterString curObj.name "_"
		x = xy[1] as integer
		y = xy[2] as integer
		p1 = getNode ((x - 1) as string + "_" + (y + 1) as string)
		p2 = getNode ((x - 0) as string + "_" + (y + 1) as string)
		p3 = getNode ((x + 0) as string + "_" + (y + 1) as string)
		p4 = getNode ((x - 1) as string + "_" + (y + 0) as string)
		p6 = getNode ((x + 1) as string + "_" + (y + 0) as string)
		p7 = getNode ((x - 1) as string + "_" + (y - 0) as string)
		p8 = getNode ((x - 0) as string + "_" + (y - 1) as string)
		p9 = getNode ((x + 1) as string + "_" + (y - 1) as string)
		arr = #(p1,p2,p3,p4,p6,p7,p8,p9)
		reult = #()
		for i in arr do
		(
			if i == undefined do continue
			if i.wireColor == playerColor do continue --不能是自己
			if i.wireColor == errorColor do continue --死路
			if i.wireColor == wallColor do continue --墙体
			if i.wireColor == pathColor do continue --走过的
			append reult i
		)
		return reult
	)

	on btnSetGround pressed do for i in selection do i.wirecolor = groundColor
	on btnSetWall pressed do for i in selection do i.wirecolor = wallColor
	on btnSetPlayer pressed do 
	(
		for i in objects do if i.wirecolor == playerColor do i.wirecolor = groundColor
		if selection[1] != undefined do  
		(
			selection[1].wirecolor = playerColor
			_playerObject = selection[1]
		)
	)
	on btnSetTarget pressed do 
	(
		for i in objects do if i.wirecolor == targetColor do i.wirecolor = groundColor
		if selection[1] != undefined do  
		(
			selection[1].wirecolor = targetColor
	  		_targetObject = selection[1]
		)
	)
 
	fn find =
	(
		times = 0 
		local _player = _playerObject
		local _target = _targetObject
		local state = #init
		while true do
		(
			dis = distance _player _target 
			if dis < 20 do exit
			times += 1
			if times > 2000 do 
			(
				state = #timeout
				exit
			)
			--
			nearObjs = getNearOkObjects _player
			tmpDis = #()
			for i in nearObjs do
			(
				dis = distance i _target
				--print ("最近物体: " + i as string)
				append tmpDis dis
			)
			if tmpDis.count == 0 do
			(
				print "死路,周围无可行走的点"
				if times == 1 then
				(
					--1次表示绝路
					state = #over
				)
				else
				(
					_player.wireColor = errorColor
					state = #dead
				)
				
				exit
			)
			minDis = amin tmpDis
			index = findItem tmpDis minDis
			--找到了一个最短物体,玩家就变成了他
			if index != 0 then 
			(
				_player = nearObjs[index]
				_player.wireColor = pathColor
				--
				sleep 0.001
				windows.processPostedMessages()
				redrawViews()
			)
			state = #ok
		)
		print ("循环次数:" + times as string)
		if times == 0 then state = #zeroTimes
		return state
	)

	on btnStartFind pressed do
	( 
		for i = 1 to 500 do 
		(
			if keyboard.escPressed do exit
			btnReset.pressed()
			state = find()
			if state == #zeroTimes do exit
			if state == #timeout do exit
			if state == #ok do exit
			if state == #over do exit
		)
	)

	on btnResetDeadPath pressed do
	(
		for i in objects do if i.wireColor == errorColor do 
			 i.wireColor = groundColor
	)
	on btnReset pressed do
	(
		for i in objects do 
		(
			if i.wireColor == pathColor do  i.wireColor = groundColor
			if i.wireColor == playerColor do  _playerObject = i
			if i.wireColor == targetColor do   _targetObject = i 
		)
		try _targetObject.wireColor = targetColor catch()
	)


	on btnCreateSceneRandom pressed do
	(
		btnCreateSceneRandom.enabled = false
		btnCreateScene.pressed()

		for i in objects do
		(
			if random 1 2 == 1 then i.wirecolor = wallColor
			else if random 1 3 == 1 then i.wirecolor = groundColor
		)
		
		while true do
		(
			p = objects[random 1 objects.count]
		 	t = objects[random 1 objects.count]
			if p != t do
			(
				_playerObject = p
				_targetObject = t
				_playerObject.wirecolor = playerColor
				_targetObject.wirecolor = targetColor
				exit
			)
		) 
		btnStartFind.pressed()
		btnCreateSceneRandom.enabled = true
	)

	on btnRandomBoom pressed do
	(
		while true do
		(
			obj = objects[random 1 objects.count] 
			if obj.wireColor == wallColor or obj.wireColor == groundColor do 
			(
				print ("爆炸:" + obj as string)
				obj.wireColor = groundColor
				--
				s = Sphere radius:6 pos:obj.pos
				sleep 0.1
				windows.processPostedMessages()
				redrawViews()
				delete s
				
				exit
			)
		)
	)

)
createDialog autoFinderRoll 200 280

标签:do,string,maxscript,wirecolor,--,wireColor,自动,pressed,寻路
From: https://www.cnblogs.com/trykle/p/16772247.html

相关文章

  • 项目需求|10~15万|自动上料系统—将物料通过机械手臂挂在挂钩上
    项目需求:自动上料系统(将物料通过机械手臂挂在挂钩上)​需求内容:1、利用3D视觉技术(点云配置或其他方法)识别挂钩的空间位置,包含x,y,z坐标。2、利用3D视觉技术识别挂钩姿态,判断挂......
  • Springboot自动配置原理
    一个boot项目启动类有个@SpringBootApplication注解,查看此注解主要包括@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan三个注解@SpringBootConfigura......
  • web自动化环境搭建
    1、先确认本地电脑对应浏览器版本2、下载浏览器版本对应的驱动 谷歌浏览器驱动下载地址:http://chromedriver.storage.googleapis.com/index.html火狐浏览器驱......
  • 做接口自动化前你需要准备什么?
    自动化测试,最近些年可谓是大火。招聘上的要求也好,培训班的广告也罢,比比皆是,足以说明它在业内的火爆程度。虽然说会写自动化测试也不能说明你牛批,但是你不会的话,那么......
  • ubuntu ens33网卡掉线问题配置系统重启后自动加载网卡驱动
     cd/lib/systemd/system 进入system目录找到 rc-local.service文件catrc-local.service#SPDX-License-Identifier:LGPL-2.1-or-later##Thisfile......
  • NET添加数据映射的两种方式(手动Controller和自动AutoMapper)
    手动添加映射在Controllers目录下找到所需要添加映射的xxxcontroller.cs文件,然后手动添加数据的映射:usingSystem;usingSystem.Linq;usingAutoMapper;usingMic......
  • Ansible 自动化最佳实践
    Ansible自动化最佳实践                         版本标识V1编制单位李斌编制日期2022年......
  • drf自动生成路由、on_delete、登录认证
    目录回顾一、drf入门1.前后端开发模式2.API接口:前后端交互的媒介3.接口测试工具:postman4.restful规范十条5.drf快速入门6.必须继承APIView写视图类二、序列化组件1.功能三......
  • 自动生成路由、登录接口与登录认证
    路由自己写的路由path('books/',views.BookView.as_view({'get':'list','post':'create'})),path('admin/<int:pk>',views.BookView.as_view({'get':'retrieve',......
  • Shell脚本实现自动化安装
    在Linux环境下,实现多个软件包的自动化安装步骤:1、找到软件包,提取软件包的名字建立解压路径(为了让我们更清楚的找到我们的文件)pkg=`find${PACKAGE_PATH}-name*BIN*.tar.gz......