Skip to content

Instantly share code, notes, and snippets.

@SalatielSauer
Last active March 20, 2023 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SalatielSauer/0dca585f395f5e326f035679286ac086 to your computer and use it in GitHub Desktop.
Save SalatielSauer/0dca585f395f5e326f035679286ac086 to your computer and use it in GitHub Desktop.
A UI for Sauerbraten that generates the files needed to load a map as a mapmodel.
_fs_debug = 0
_fs_appendline = [
if (!=s $arg1 "") [
if $_fs_debug [echo (concat "^f0append ^f2" @arg1)]
append _fs_linesbuff (concatword "[" $arg1 "]")
]
]
_fs_open = [
_fs_linesbuff = []
newgui _ [
guieditor @arg1 -1 1
textfocus @arg1
textload @arg1
cleargui
]
showgui _
if $arg2 [
sleep 0 [
arg1 = (escape $textshow)
arg2 = @arg1
nodebug [do (substr (strreplace (strreplace (strreplace (strreplace (concatword "^n" $textshow) ";" "^n") "//" "^^/^^/") "^n" "]^n _fs_appendline [") "[ ]" "^^n") 1)]
@arg2
]
]
]
_fs_write = [
newgui _ [
local content
content = @arg2
guifield content 1
textfocus content
textsave @arg1
cleargui
]
showgui _
sleep 0 [
@arg3
]
]
// .MTL reader (just /exec the .mtl file)
# = [ echo "^f8Reading MTL file" ]
map_Kd = [
append _mtm_textures (strreplace (strreplace $arg1 "packages\" "") "\" "/")
echo (concat "^f8Added^f2" $arg1 "^f8to textures list")
]
newmtl = [
append _mtm_slots $arg1
echo (concat "^f8Added^f2" $arg1 "^f8to slots list")
]
// .MD5MESH reader
_mtm_readMD5 = [
_fs_open $arg1 [
local bones
bones = (unescape (substr $arg1 (strstr $arg1 "{") (strstr $arg1 "}")))
bones = (sublist (concatword "[" (strreplace $bones "^n" "] [")) 1)
bones = (strreplace $bones "^/^/" "")
echo (looplistconcat joint (substr $bones 0 (strstr $bones "}")) [at $joint 0])
]
]
_mtm_uservars = [mdlcollide mdlfullbright mdlcullface mdlscale mdlspin mdlyaw mdlpitch]
_mtm_uservarstype = [guicheckbox guicheckbox guicheckbox guifield guifield guislider guislider]
_mtm_uservarsdefault = [0 0 1 400 0 0 0]
_mtm_applyuservars = [
local cmds values cfg
cmds = (looplistconcat line $arg1 [at $line 0])
values = (looplistconcat line $arg1 [format "[%1]" (sublist $line 1)])
loop index (listlen $cmds) [
local line cmd val keepval
cmd = (at $cmds $index)
val = (at $values $index)
nodebug [append cfg (format "^n%1 %2" $cmd (? (= (indexof $_mtm_uservars $cmd) -1) $val $[_mtm_@cmd]))]
]
// append missing vars
looplist usercmd $_mtm_uservars [
if (= (indexof $cmds $usercmd) -1) [
cfg = (concatword $cfg (format "^n %1 %2" $usercmd $[_mtm_@usercmd]))
]
]
result (unescape $cfg)
]
writemapmodel = [
local path _mtm_objskin cfgcontent textureid
textureid = 0
_mtm_textures = []
_mtm_slots = []
path = (? (= (strstr $arg1 "/") -1) (concatword "maptomodel/" $arg1) $arg1)
writeobj (concatword $path "/model")
exec (concatword $path "/model.mtl")
_mtm_objskin = (sublist (looplistconcatword slot $_mtm_slots [
result (format "objskin ^"%1^" ^"%2^"^n" $slot (at $_mtm_textures $textureid))
textureid = (+ $textureid 1)
]) 1)
// reset settings for every new mapmodel
looplist uservar $_mtm_uservars [
[_mtm_@uservar] = (at $_mtm_uservarsdefault (indexof $_mtm_uservars $uservar))
]
cfgcontent = (_mtm_applyuservars ["objload model.obj" "objdir ../" @(format "[objskin %1]" $_mtm_objskin) "mdlscale 300" "mdlcollide 1" "// by salatiel"])
_fs_write (concatword $path "/obj.cfg") (escape $cfgcontent)
_mtm_lastpath = $path
]
_mtm_settings_sliders = 1
newgui _mtm_settings [
guistayopen [
guicheckbox "input with sliders" _mtm_settings_sliders
guilist [
guimodelpreview (mapmodelname (at (entget) 2)) "mapmodel" [] 3
guilist [
looplist t [
"collision 0" "fullbright 0" "cullface 1"
"scale 400" "spin 0" "yaw 0" "pitch 0"
] [
guitext (format "^f4mdl^f7%1 ^f~(^f4default: %2^f~): " (at $t 0) (at $t 1)) 0
]
]
guilist [
guicheckbox "" _mtm_mdlcollide
guicheckbox "" _mtm_mdlfullbright
guicheckbox "" _mtm_mdlcullface
if (_mtm_settings_sliders) [
guistrut 6 1
guislider _mtm_mdlscale 1 526; guislider _mtm_mdlspin 0 128
guislider _mtm_mdlyaw 0 280; guislider _mtm_mdlpitch 0 280
] [
guifield _mtm_mdlscale 5; guifield _mtm_mdlspin 5
guifield _mtm_mdlyaw 5; guifield _mtm_mdlpitch 5
]
]
]
guibutton (concatword "^f0save ^f8" (strreplace $arg1 "packages/models/../../" "")) [
local cfgcontent cfgpath cfgfolder cfgname modelpath targetpath shouldcopy keeporiginalpath
targetpath = @arg1
shouldcopy = (= (strstr $targetpath "../") -1)
cfgname = (strreplace $targetpath "/" " ") // treat as a list
cfgfolder = (at $cfgname (- (listlen $cfgname) 2)) // get the cfg folder
cfgname = (sublist $cfgname (- (listlen $cfgname) 1)) // get the actual cfg file name
keeporiginalpath = (=s $cfgname "md2.cfg")
modelpath = (strreplace $targetpath (concatword "/" $cfgname) (? (&& $keeporiginalpath (!=s $cfgfolder "maptomodel")) "/maptomodel" ""))
// special case for md2 files since they are handled differently
// md2dir and md2load do not exist, so we save the modified version very close to the official version,
// (just one folder deeper) that way it (somehow) reads the tris.md2 and skins while not replacing the original.
if $keeporiginalpath [
cfgcontent = (_mtm_applyuservars $_fs_linesbuff)
cfgpath = (concatword @modelpath "/md2.cfg")
echo $cfgpath
] [
// any other model format
modelpath = (strreplace @modelpath "packages/models/" "")
cfgcontent = (concatword (? @shouldcopy (format "%1dir ^"%2^" // original mapmodel^n" (strreplace @cfgname ".cfg" "") $modelpath) "") (_mtm_applyuservars $_fs_linesbuff))
cfgpath = (? @shouldcopy (strreplace @targetpath "packages/models" "maptomodel") @targetpath)
]
_fs_write $cfgpath (escape (strreplace $cfgcontent "^^/^^/" "//")) [
if @shouldcopy [
local newmodelpath
newmodelpath = (? (=s @@cfgname "md2.cfg") (concatword "../../" @@modelpath) (concatword "../../maptomodel/" @@modelpath))
mmodel $newmodelpath
//echo $newmodelpath
entset mapmodel (at (entget) 1) (mapmodelfind $newmodelpath)
echo (concatword "^f0the ^f7" @@cfgname " ^f0of this official mapmodel (^f7#" (at (entget) 2) "^f0) has been replaced by a copy")
echo (concatword "^f0new ^f7" @@cfgname " ^f0file saved in ^f7home/" @@modelpath)
] [
clearmodel (mapmodelname (at (entget) 2))
showgui _mtm_settings
]
entautoview
]
]
]
] "^f8Map^f7To^f8Model ^f~config"
maptomodel = [
if (=s (at (entget) 0) "mapmodel") [
local modelpath modelformat
modelpath = (concatword "packages/models/" (mapmodelname (at (entget) 2)))
// find cfg for current model
loopfiles type $modelpath "cfg" [
if (!= (indexof [md2 md3 md5 obj smd iqm] $type) -1) [
modelformat = $type
]
]
if (getalias modelformat) [
cleargui
// read the cfg content and set the gui vars
_fs_open (format "%1/%2.cfg" $modelpath $modelformat) [
looplist line $_fs_linesbuff [
local cmd
cmd = (sublist $line 0 1)
if (!= (indexof $_mtm_uservars $cmd) -1) [
[_mtm_@cmd] = (sublist $line 1)
]
]
sleep 0 [showgui _mtm_settings; arg1 = @arg2]
]
] [echo "could not find model cfg"; showgui _mtm_settings]
] [
if (!=s $arg1 "") [
writemapmodel $arg1
sleep 0 [
local path
path = (concatword "../../" $_mtm_lastpath)
mmodel $path
newent mapmodel (mapmodelfind $path)
entset mapmodel 0
]
]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment