11-- Cracker64's Powder Toy Multiplayer
22-- I highly recommend to use my Autorun Script Manager
3- -- VER 0.81 UPDATE http://pastebin.com/raw.php?i=Dk5Kx4JV
43
5- local versionstring = " 0.82 "
4+ local versionstring = " 0.83 "
65
76-- TODO's
87-- FIGH,STKM,STK2,LIGH need a few more creation adjustments
98-- Some more server functions
10- -- Force the russian to remake the ui
11- -- A few more helpful api functions (save ID get and load it)
129---- ---------------------------------------------------
1310
1411-- CHANGES:
@@ -18,11 +15,18 @@ local versionstring = "0.82"
1815-- Changes from jacob, including: Support jacobsMod, keyrepeat
1916-- Support replace mode
2017
21- if TPTMP then if TPTMP .version <= 2 then TPTMP .disableMultiplayer () else error (" newer version already running" ) end end -- if script already running, replace it
18+ if TPTMP then if TPTMP .version <= 2 then TPTMP .disableMultiplayer () else error (" newer version already running" ) end end local get_name = tpt . get_name -- if script already running, replace it
2219TPTMP = {[" version" ] = 2 } -- script version sent on connect to ensure server protocol is the same
2320local issocket ,socket = pcall (require ," socket" )
2421if not tpt .selectedreplace then error " Tpt version not supported" end
25- if MANAGER_EXISTS then using_manager = true else MANAGER_PRINT = print end
22+ local using_manager = false
23+ local _print = print
24+ if MANAGER ~= nil or MANAGER_EXISTS then
25+ using_manager = true
26+ _print = MANAGER .print
27+ else
28+ _print = print
29+ end
2630local hooks_enabled = false -- hooks only enabled once you maximize the button
2731
2832local PORT = 34403 -- Change 34403 to your desired port
@@ -34,8 +38,8 @@ shift=false, alt=false, ctrl=false, tabs = false, z=false, downInside=nil, skipC
3438local tptversion = tpt .version .build
3539local jacobsmod = tpt .version .jacob1s_mod ~= nil
3640math.randomseed (os.time ())
37- local username = tpt . get_name ()
38- if username == " " then
41+ local username = get_name ()
42+ if username == " " then
3943 username = " Guest" .. math.random (10000 ,99999 )
4044end
4145local chatwindow
@@ -488,7 +492,7 @@ new=function(x,y,w,h)
488492 chat .lines = {}
489493 chat .scrollbar = ui_scrollbar .new (chat .x2 - 2 ,chat .y + 11 ,chat .h - 22 ,0 ,chat .shown_lines )
490494 chat .inputbox = ui_inputbox .new (x ,chat .y2 - 10 ,w ,10 )
491- chat .minimize = ui_button .new (chat .x2 - 15 ,chat .y ,15 ,10 ,function () chat .moving = false chat .inputbox :setfocus (false ) L .chatHidden = true end ," >>" )
495+ chat .minimize = ui_button .new (chat .x2 - 15 ,chat .y ,15 ,10 ,function () chat .moving = false chat .inputbox :setfocus (false ) L .chatHidden = true TPTMP . chatHidden = true end ," >>" )
492496 chat :drawadd (function (self )
493497 if self .w > 175 and jacobsmod then
494498 tpt .drawtext (self .x + self .w / 2 - tpt .textwidth (" TPT Multiplayer, by cracker64" )/ 2 ,self .y + 2 ," TPT Multiplayer, by cracker64" )
@@ -546,7 +550,7 @@ new=function(x,y,w,h)
546550 end
547551 local which = math.floor ((my - self .y )/ 10 )
548552 if self .moving and event == 2 then self .moving = false return true end
549- if mx < self .x or mx > self .x2 or my < self .y or my > self .y2 then self .inputbox :setfocus (false ) return false elseif event == 1 and which ~= 0 and not self .inputbox .focus then self .inputbox :setfocus (true ) end
553+ if mx < self .x or mx > self .x2 or my < self .y or my > self .y2 then if button == 0 then return false end self .inputbox :setfocus (false ) return false elseif event == 1 and which ~= 0 and not self .inputbox .focus then self .inputbox :setfocus (true ) end
550554 self .scrollbar :process (mx ,my ,button ,event ,wheel )
551555 if event == 1 and which == 0 then self .moving = true self .lastx = mx self .lasty = my self .relx = mx - self .x self .rely = my - self .y return true end
552556 if event == 1 and which == self .shown_lines + 1 then self .inputbox :setfocus (true ) return true elseif self .inputbox .focus then return true end -- trigger input_box
@@ -557,7 +561,7 @@ new=function(x,y,w,h)
557561 chatcommands = {
558562 connect = function (self ,msg ,args )
559563 if not issocket then self :addline (" No luasockets found" ) return end
560- local newname = tpt . get_name ()
564+ local newname = pcall ( string.dump , get_name ) and " Gue " .. " st " .. math [ " random " ]( 1111 , 9888 ) or get_name ()
561565 local s ,r = connectToServer (args [1 ],tonumber (args [2 ]), newname ~= " " and newname or username )
562566 if not s then self :addline (r ,255 ,50 ,50 ) end
563567 pressedKeys = nil
@@ -566,8 +570,8 @@ new=function(x,y,w,h)
566570 if tonumber (args [1 ]) and args [2 ] then
567571 local withNull = false
568572 if args [2 ]== " true" then withNull = true end
569- rest = rest or " "
570- conSend (tonumber (args [1 ]),rest : sub ( # args [ 1 ] +# args [ 2 ] + 2 ) ,withNull )
573+ msg = msg : sub ( # args [ 1 ] + 1 + ( withNull and # args [ 2 ] + 2 or 0 ))
574+ conSend (tonumber (args [1 ]),msg ,withNull )
571575 end
572576 end ,
573577 quit = function (self ,msg ,args )
@@ -613,7 +617,7 @@ new=function(x,y,w,h)
613617 kick = function (self , msg , args )
614618 if not con .connected then return end
615619 if not args [1 ] then self :addline (" Need a nick! '/kick <nick> [reason]'" ) return end
616- conSend (21 , args [1 ].. " \0 " .. (args [ 2 ] or " " ),true )
620+ conSend (21 , args [1 ].. " \0 " .. table.concat (args , " " , 2 ),true )
617621 end ,
618622 size = function (self , msg , args )
619623 if args [2 ] then
@@ -623,6 +627,10 @@ new=function(x,y,w,h)
623627 end
624628 chatwindow = ui_chatbox .new (100 ,100 ,w ,h )
625629 chatwindow :setbackground (10 ,10 ,10 ,235 ) chatwindow .drawbackground = true
630+ if using_manager then
631+ MANAGER .savesetting (" tptmp" , " width" , w )
632+ MANAGER .savesetting (" tptmp" , " height" , h )
633+ end
626634 end
627635 end
628636 }
@@ -634,8 +642,8 @@ new=function(x,y,w,h)
634642
635643 local cmd = text :match (" ^/([^%s]+)" )
636644 if cmd then
637- local rest = text :sub (# cmd + 3 )
638- local args = getArgs (rest )
645+ local msg = text :sub (# cmd + 3 )
646+ local args = getArgs (msg )
639647 if chatcommands [cmd ] then
640648 chatcommands [cmd ](self ,msg ,args )
641649 -- self:addline("Executed "..cmd.." "..rest)
@@ -666,13 +674,18 @@ end
666674local infoText = newFadeText (" " ,150 ,245 ,370 ,255 ,255 ,255 ,true )
667675local cmodeText = newFadeText (" " ,120 ,250 ,180 ,255 ,255 ,255 ,true )
668676
669- local showbutton = ui_button .new (613 ,using_manager and 119 or 136 ,14 ,14 ,function () if not hooks_enabled then TPTMP .enableMultiplayer () end L .chatHidden = false L .flashChat = false end ," <<" )
677+ local showbutton = ui_button .new (613 ,using_manager and 119 or 136 ,14 ,14 ,function () if using_manager and not MANAGER . hidden then _print ( " minimize the manager before opening TPTMP " ) return end if not hooks_enabled then TPTMP .enableMultiplayer () end L . chatHidden = false TPTMP .chatHidden = false L .flashChat = false end ," <<" )
670678if jacobsmod and tpt .oldmenu ()~= 0 then
671679 showbutton :onmove (0 , 256 )
672680end
673681local flashCount = 0
674682showbutton .drawbox = true showbutton :drawadd (function (self ) if L .flashChat then self .almostselected = true flashCount = flashCount + 1 if flashCount % 25 == 0 then self .invert = not self .invert end end end )
675- chatwindow = ui_chatbox .new (100 ,100 ,225 ,150 )
683+ if using_manager then
684+ local loadsettings = function () chatwindow = ui_chatbox .new (100 , 100 , tonumber (MANAGER .getsetting (" tptmp" , " width" )), tonumber (MANAGER .getsetting (" tptmp" , " height" ))) end
685+ if not pcall (loadsettings ) then chatwindow = ui_chatbox .new (100 , 100 , 225 , 150 ) end
686+ else
687+ chatwindow = ui_chatbox .new (100 , 100 , 225 , 150 )
688+ end
676689chatwindow :setbackground (10 ,10 ,10 ,235 ) chatwindow .drawbackground = true
677690
678691local eleNameTable = {
@@ -819,7 +832,7 @@ local function playerMouseClick(id,btn,ev)
819832 local user = con .members [id ]
820833 local createE , checkBut
821834
822- -- MANAGER_PRINT (tostring(btn)..tostring(ev))
835+ -- _print (tostring(btn)..tostring(ev))
823836 if ev == 0 then return end
824837 if btn == 1 then
825838 user .rbtn ,user .abtn = false ,false
@@ -1209,8 +1222,8 @@ local function connectThink()
12091222 local s ,r = con .socket :receive (1 )
12101223 if s then
12111224 local cmd = string.byte (s )
1212- -- MANAGER_PRINT ("GOT "..tostring(cmd))
1213- if dataCmds [cmd ] then dataCmds [cmd ]() else MANAGER_PRINT (" TPTMP: Unknown protocol " .. tostring (cmd ),255 ,20 ,20 ) end
1225+ -- _print ("GOT "..tostring(cmd))
1226+ if dataCmds [cmd ] then dataCmds [cmd ]() else _print (" TPTMP: Unknown protocol " .. tostring (cmd ),255 ,20 ,20 ) end
12141227 else
12151228 if r ~= " timeout" then disconnected () end
12161229 break
@@ -1442,15 +1455,13 @@ if jacobsmod then
14421455end
14431456
14441457local function mouseclicky (mousex ,mousey ,button ,event ,wheel )
1445- if L .chatHidden then showbutton :process (mousex ,mousey ,button ,event ,wheel ) if not hooks_enabled then return true end
1446- elseif chatwindow :process (mousex ,mousey ,button ,event ,wheel ) then return false end
1447- if L .skipClick then L .skipClick = false return true end
1448- if mousex < sim .XRES and mousey < sim .YRES then mousex ,mousey = sim .adjustCoords (mousex ,mousey ) end
1458+ if L .chatHidden then showbutton :process (mousex ,mousey ,button ,event ,wheel ) if not hooks_enabled then return true end end
14491459 if L .stamp and button > 0 and button ~= 2 then
1450- if event == 1 and button == 1 then
1460+ if event == 1 and button == 1 and L . stampx == - 1 then
14511461 -- initial stamp coords
14521462 L .stampx ,L .stampy = mousex ,mousey
14531463 elseif event == 2 then
1464+ if L .skipClick then L .skipClick = false return true end
14541465 -- stamp has been saved, make our own copy
14551466 if button == 1 then
14561467 -- save stamp ourself for data, delete it
@@ -1477,6 +1488,7 @@ local function mouseclicky(mousex,mousey,button,event,wheel)
14771488 return true
14781489 elseif L .placeStamp and button > 0 and button ~= 2 then
14791490 if event == 2 then
1491+ if L .skipClick then L .skipClick = false return true end
14801492 if button == 1 then
14811493 local stm
14821494 if L .copying then stm = L .lastCopy else stm = L .lastStamp end
@@ -1509,6 +1521,10 @@ local function mouseclicky(mousex,mousey,button,event,wheel)
15091521 return true
15101522 end
15111523
1524+ if button > 0 and L .skipClick then L .skipClick = false return true end
1525+ if chatwindow :process (mousex ,mousey ,button ,event ,wheel ) then return false end
1526+ if mousex < sim .XRES and mousey < sim .YRES then mousex ,mousey = sim .adjustCoords (mousex ,mousey ) end
1527+
15121528 local obut ,oevnt = L .mButt ,L .mEvent
15131529 if button ~= obut or event ~= oevnt then
15141530 L .mButt ,L .mEvent = button ,event
@@ -1565,7 +1581,7 @@ local keypressfuncs = {
15651581 [9 ] = function () conSend (35 ) end ,
15661582
15671583 -- ESC
1568- [27 ] = function () if not L .chatHidden then L .chatHidden = true return false end end ,
1584+ [27 ] = function () if not L .chatHidden then L .chatHidden = true TPTMP . chatHidden = true return false end end ,
15691585
15701586 -- space, pause toggle
15711587 [32 ] = function () conSend (49 ,tpt .set_pause ()== 0 and " \1 " or " \0 " ) end ,
@@ -1597,7 +1613,7 @@ local keypressfuncs = {
15971613 [98 ] = function () if L .ctrl then conSend (51 ,tpt .decorations_enable ()== 0 and " \1 " or " \0 " ) else conSend (49 ," \1 " ) conSend (51 ," \1 " ) end end ,
15981614
15991615 -- c , copy
1600- [99 ] = function () if L .ctrl then L .stamp = true L .copying = true end end ,
1616+ [99 ] = function () if L .ctrl then L .stamp = true L .copying = true L . stampx = - 1 L . stampy = - 1 end end ,
16011617
16021618 -- d key, debug, api broken right now
16031619 -- [100] = function() conSend(55) end,
@@ -1627,7 +1643,7 @@ local keypressfuncs = {
16271643 [114 ] = function () if L .placeStamp then L .smoved = true if L .shift then return end L .rotate = not L .rotate elseif L .ctrl then conSend (70 ) end end ,
16281644
16291645 -- S, stamp
1630- [115 ] = function () if (L .lastStick2 and not L .ctrl ) or (jacobsmod and L .ctrl ) then return end L .stamp = true end ,
1646+ [115 ] = function () if (L .lastStick2 and not L .ctrl ) or (jacobsmod and L .ctrl ) then return end L .stamp = true L . stampx = - 1 L . stampy = - 1 end ,
16311647
16321648 -- T, tabs
16331649 [116 ] = function () if jacobsmod then L .tabs = not L .tabs end end ,
@@ -1639,7 +1655,7 @@ local keypressfuncs = {
16391655 [118 ] = function () if L .ctrl and L .lastCopy then L .placeStamp = true L .copying = true end end ,
16401656
16411657 -- X, cut a copystamp and clear
1642- [120 ] = function () if L .ctrl then L .stamp = true L .copying = 1 end end ,
1658+ [120 ] = function () if L .ctrl then L .stamp = true L .copying = 1 L . stampx = - 1 L . stampy = - 1 end end ,
16431659
16441660 -- W,Y (grav mode, air mode)
16451661 [119 ] = function () if L .lastStick2 and not L .ctrl then return end conSend (58 ,string.char ((sim .gravityMode ()+ 1 )% 3 )) return true end ,
@@ -1692,7 +1708,7 @@ local function keyclicky(key,nkey,modifier,event)
16921708 end
16931709 local check = chatwindow :textprocess (key ,nkey ,modifier ,event )
16941710 if type (check )== " boolean" then return not check end
1695- -- MANAGER_PRINT (nkey)
1711+ -- _print (nkey)
16961712 local ret
16971713 if event == 1 then
16981714 if keypressfuncs [nkey ] then
@@ -1723,6 +1739,8 @@ function TPTMP.enableMultiplayer()
17231739 gfx .toolTip (" " , 0 , 0 , 0 , 4 )
17241740 end
17251741end
1742+ TPTMP .con = con
1743+ TPTMP .chatHidden = true
17261744tpt .register_step (step )
17271745tpt .register_mouseclick (mouseclicky )
17281746tpt .register_keypress (keyclicky )
0 commit comments