NetDlg.cpp 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // NetDlg.CPP
  19. // Project: Nostril (aka Postal)
  20. //
  21. // History:
  22. // 04/08/97 JMI Started.
  23. //
  24. // 05/20/97 JMI Now ms_pguiOK and ms_pguiCancel use SAFE_REF() so the
  25. // current GUI can, optionally, not have one or both GUI
  26. // items.
  27. //
  28. // 05/25/97 JMI Integrated newest TCP/IP CNetServer/Client interface.
  29. // Still need to detect when the game is starting.
  30. // GUI could still use a little cleaning up.
  31. //
  32. // 05/26/97 JMI Removed case for NetMsg::DuplicateName in reason
  33. // for JoinDeny message (it no longer exists).
  34. //
  35. // 05/26/97 JMI Added sending and receiving of START_REALM.
  36. // Removed psNumPlayers and psMyPlayerNum parameters.
  37. //
  38. // 05/27/97 JMI Added sending and receiving of LOAD_REALM before
  39. // START_REALM.
  40. // Fixed bug in OnLoginMsg() which was telling the client
  41. // the wrong net ID.
  42. // Upgraded to utilize new functions RSocket::SetPort() and
  43. // CNet::OpenPeerData().
  44. //
  45. // 05/27/97 JMI OnJoinedMsg() now only adds the players' names to the
  46. // listbox of players.
  47. //
  48. // 05/28/97 JMI Integrated new dialog.
  49. //
  50. // 05/28/97 JMI Changed so the LOAD_REALM message motivates the leaving
  51. // of the dialog interface (was START_REALM that did it
  52. // before).
  53. //
  54. // 06/11/97 JMI Removed unnecessary code in OnLoadRealmMsg().
  55. // Now uses Play_GetNextLevel to get realm names.
  56. //
  57. // 06/11/97 JMI Now centers client/server dialogs on screen.
  58. //
  59. // 06/13/97 MJR Restructed lots of stuff to consolidate client and
  60. // server code into a single function.
  61. // Implimented new version of START_GAME message.
  62. // Added code to get/release resources for new deluxe
  63. // dialog box.
  64. //
  65. // 06/15/97 JMI Now deactivates client items in listbox as they are
  66. // added.
  67. // Also, now, instead of simply squashing the dialog to
  68. // exclude server controls in client mode, we simply hide
  69. // the server controls and center the remaining controls
  70. // in the dialog without altering its size.
  71. //
  72. // 06/16/97 JMI Now uses g_fontSmall for dialogs.
  73. //
  74. // MJR Added use of watchdog timer for network blocking callbacks.
  75. //
  76. // JMI Changed use of g_fontSmall to g_fontBig.
  77. //
  78. // 06/17/97 JMI Now only allows the server to process a DLG_OK.
  79. //
  80. // 06/30/97 MJR Switched over to new GuiItem.h-supplied macros.
  81. //
  82. // 07/14/97 BRH Changed call to Play_GetRealmInfo to include the
  83. // new challenge mode parameter.
  84. //
  85. // 08/02/97 JMI Added an icon to watchdog timer for network blocking
  86. // callbacks and made it generally callable.
  87. //
  88. // 08/04/97 BRH Added protocol parameter to net calls.
  89. //
  90. // 08/11/97 JMI Changed a little of the structure of SetUpDlg() to allow
  91. // for more than just two types of dialogs.
  92. // Also, changed UpdateDialog() to use an RProcessGui which
  93. // simplifies that down to nearly nothing. #if 0'd out old
  94. // stuff for now.
  95. // Checking in so Mike can compile so we can check his stuff
  96. // in so I can check this out again.
  97. //
  98. // MJR Modified to use new browsing functions and moved all
  99. // the find-the-host code into a separate function.
  100. //
  101. // JMI Now adds the items from the browse list to the dialog
  102. // (and drops them too (theoretically) ).
  103. //
  104. // 08/12/97 JMI Dropped hosts were not setting the repaginate flag in
  105. // UpdateListBox(). Fixed.
  106. // DoNetGameDialog() now loops nearly all logic while
  107. // browsing and no errors.
  108. //
  109. // 08/13/97 JMI Cleaned up some more stuff.
  110. // MJR Fixed bugs that kept us in a browsing loop.
  111. //
  112. // 08/14/97 JMI Added global difficulty parameter to Play_GetRealmInfo().
  113. // I imagine it's not actually used for multiplayer levels
  114. // until we have options to play cooperative.
  115. //
  116. // 08/19/97 JMI Added some unfinished usage of the multiplayer options
  117. // updating. Needs to be updated to the correct spot,
  118. // either the game settings or the message. Probably the
  119. // game settings which then get copied to the message.
  120. //
  121. // 08/19/97 JMI Now uses the g_GameSettings for the multiplayer options.
  122. // Tinkered with chat messages that I thought weren't
  123. // working but then realized they were being ignored b/c
  124. // the UpdateDialog() function was being called by the
  125. // net blocking callback so hopefully I didn't ruin anything
  126. // in that process... .
  127. // Genericized SetupDlg() to use UploadLinks().
  128. //
  129. // 08/19/97 JMI Added a flag to UpdateDialog() indicating that it should
  130. // not clear any GUIs. This is useful for when
  131. // UpdateDialog() is called via callback (so we don't loose
  132. // any of the pressages processed via the callback).
  133. //
  134. // 08/20/97 JMI Up/DownloadLinkInteger() are now hardwired to assume
  135. // 'signed' type for String links b/c bool had a warning
  136. // for the trickery I was using to determine if the type
  137. // was signed.
  138. //
  139. // 08/20/97 JMI Now chat history is a listbox.
  140. // Clicking the 'Send Chat' button moves focus back to the
  141. // edit field for better feedback.
  142. // Limits chats to 20.
  143. // Now updates client's view of host's multiplay options.
  144. // Now the server can select a player (for disconnection)
  145. // from the players list.
  146. // Implemented host option to disconnect a player.
  147. // Newest player is EnsureVisible()d in player list now.
  148. // Newest chat is EnsureVisible()d in chat list now.
  149. // Now sets number of connected players on two GUIs so
  150. // there can be a 3D effect.
  151. // Sends and responds to SETUP_GAME message.
  152. // Sets options in START_GAME message from dialog settings
  153. // now.
  154. // Updates settings even if dialog aborted for consistency.
  155. //
  156. // 08/21/97 JMI Temporarily hardwired 'Rejuvenate' to ON and disallowed
  157. // user modifications of the checkbox.
  158. //
  159. // 08/21/97 JMI Changed call to Update() to UpdateSystem() and occurrences
  160. // of rspUpdateDisplay() to UpdateDisplay().
  161. //
  162. // 08/23/97 MJR Lots of changes in how errors are handled and other such
  163. // stuff.
  164. //
  165. // 08/23/97 JMI Now list box items appear with a barely perceptible
  166. // shadow that makes them alot easier to read.
  167. // Player colors now displayed as color descriptions.
  168. //
  169. // 08/25/97 JMI Now the chat text edit field limits the text to the
  170. // message's capacity for text.
  171. // Also, got rid of some '***'s.
  172. //
  173. // 08/25/97 JMI Now removes areas on chat items in chat box to make
  174. // for the border lines we hide to get more space.
  175. //
  176. // 08/27/97 JMI Changed NetProbIcons functions to NetProbGui functions.
  177. // Also, instead of a DrawNetProbGui() there's a
  178. // GetNetProbGui() so you can draw it, move it, change the
  179. // text, etc.
  180. //
  181. // 09/01/97 MJR Nearing the end of a huge overhaul of networking.
  182. //
  183. // 09/02/97 MJR It appears to work well after much testing, tuning, and
  184. // debugging.
  185. //
  186. // 09/03/97 MJR Fixed a problem that made browsing for yourself fail
  187. // once in a while (the timeout timer was too short).
  188. //
  189. // 09/06/97 JMI Now sets the Client's Game Options text field to use
  190. // the text shadow. Also, selects the first item in the
  191. // level browser box in the Server's dialog.
  192. // Also, combined the status field and the chat box into
  193. // one 'Net Console'.
  194. //
  195. // 09/06/97 JMI Now filters out the high word of ie.lKey when checking
  196. // for enter. This filters out the the key flags (i.e.,
  197. // shift, control, alt, system).
  198. //
  199. // 09/07/97 JMI Now BrowseForHosts() updates the display before
  200. // displaying the browse dialog.
  201. //
  202. // 09/08/97 MJR Fixed bug where net prob gui was showing up
  203. // immediately instead of after the watchdog expired.
  204. // Also cleaned up the erasing of the net prob gui.
  205. //
  206. // 09/09/97 JMI Now initializes ms_pguiRetry to NULL in DlgBeGone().
  207. //
  208. // 09/09/97 MJR Now detects protocol-not-supported and dispalys a
  209. // specific msg box describing the problem.
  210. //
  211. // 09/11/97 MJR Now client checks to see whether the level that was
  212. // specified in the SETUP_GAME message is available, and
  213. // automatically sends a chat message that complains if it
  214. // isn't, in the hope that the host player will change it.
  215. //
  216. // Now the host will send the realm name if only a single
  217. // realm is available, which tells everyone to only play
  218. // that realm.
  219. //
  220. // Changed the dropped message, which was incorrectly using
  221. // the error message as the player's name.
  222. //
  223. // 09/11/97 JMI Now only adds the SPECIFIC_MP_REALM_TEXT choice to the
  224. // host's level browser if ENABLE_PLAY_SPECIFIC_REALMS_ONLY
  225. // is defined. Otherwise, it chooses the first item in the
  226. // listbox.
  227. //
  228. // 09/12/97 MJR Reversed the #if for which method to use for filling
  229. // hood listbox.
  230. //
  231. // Added hardwired realm stuff to SetupGame() when we're
  232. // in the specific realm mode.
  233. //
  234. // 09/18/97 JMI There were some /"s instead of \"s. If someone had
  235. // their syntax coloring on useful colors, they would've
  236. // seen this blaringly obvious color collage. :)
  237. //
  238. // 09/24/97 JMI Fixed OnSetupGameMsg() to handle checking to see if the
  239. // local machine has the required realm using the title
  240. // that is passed in the SetupGame msg (was originally
  241. // intended to use filenames but got hosed).
  242. //
  243. // 09/26/97 JMI Now '-' is blocked at the UpdateDialog() level and never
  244. // reaches the GUIs so that the time limit and kill limit
  245. // edit fields cannot have a negative number enter in them
  246. // (of course you could enter the letter 'a' which looks
  247. // wrong too but I know one cares about that (you'd have to
  248. // be STUPID to do that but even smart people enter negative
  249. // numbers for time and kill limits) ).
  250. // Also, this keeps players from using '-' in their chat
  251. // strings.
  252. //
  253. // 11/20/97 JMI Added cooperative flags and associated checkboxes. Now
  254. // displays info on client side regarding cooperative mode
  255. // and carries all the necessary flagage around between
  256. // messages regarding cooperative mode as well. Also, now
  257. // the level browse listbox can show either deathmatch or
  258. // cooperative levels (controlled by the user's cooperative
  259. // checkbox setting).
  260. // Also, the user can choose operate checkbox deciding
  261. // whether to allow cooperative play mode (which is missiles
  262. // and bullets pass through fellow players).
  263. //
  264. // 11/25/97 JMI Added platform conflict message and more informative
  265. // version conflict messages (also added version conflict
  266. // message for server -- previous the error did not propagate
  267. // to this level).
  268. //
  269. // 11/25/97 JMI Changed the server dialog .GUI to be loaded from the HD
  270. // instead of from the VD so we can guarantee the new asset
  271. // gets loaded (since they'll use their old Postal disc, we
  272. // cannot load the .GUI from the CD).
  273. //
  274. //////////////////////////////////////////////////////////////////////////////
  275. //
  276. // Deals with networking dialogs. There is an interface for the server,
  277. // client, and browser dialogs that deal heavily with user input, and GUI
  278. // output. The client/server messages are now handled by the black-box-like
  279. // lower level.
  280. //
  281. //////////////////////////////////////////////////////////////////////////////
  282. #include "RSPiX.h"
  283. #include "CompileOptions.h"
  284. #include "GameSettings.h"
  285. #include "game.h"
  286. #include "play.h"
  287. #include "update.h"
  288. #include "NetDlg.h"
  289. #include "netbrowse.h"
  290. //////////////////////////////////////////////////////////////////////////////
  291. // Module specific macros.
  292. //////////////////////////////////////////////////////////////////////////////
  293. // Common.
  294. #define GUI_ID_OK 1001
  295. #define GUI_ID_CANCEL 1002
  296. // Common Client/Server dialog.
  297. #define GUI_ID_PLAYERS_LISTBOX 1010
  298. #define GUI_ID_CHAT_TEXT 1013
  299. #define GUI_ID_CHAT_SEND 1015
  300. #define GUI_ID_NET_CONSOLE 1016
  301. #define GUI_ID_PLAYERS_STATIC1 7000
  302. #define GUI_ID_PLAYERS_STATIC2 7001
  303. // Client dialog.
  304. #define GUI_ID_OPTIONS_STATIC 2000
  305. #define GUI_ID_RETRY 9000
  306. // Server dialog.
  307. #define GUI_ID_DISCONNECT_PLAYER 6000
  308. //#define GUI_ID_RESPAWN_PLAYERS 3000
  309. #define GUI_ID_ENABLE_TIME_LIMIT 4000
  310. #define GUI_ID_EDIT_TIME_LIMIT 4001
  311. #define GUI_ID_ENABLE_KILL_LIMIT 5000
  312. #define GUI_ID_EDIT_KILL_LIMIT 5001
  313. #define GUI_ID_LEVEL_LISTBOX 1020
  314. #define GUI_ID_SHOW_COOP_LEVELS 8000
  315. #define GUI_ID_ENABLE_COOP_MODE 9000
  316. // Browser dialog.
  317. #define GUI_ID_HOST_LISTBOX 1010
  318. #define SERVER_GUI "res/shell/Server.gui"
  319. #define CLIENT_GUI "res/shell/Client.gui"
  320. #define BROWSER_GUI "res/shell/Browser.gui"
  321. #define GUI_DIR "menu/"
  322. // Note that this is evaluated at compile time, so even
  323. // if the ptr is NULL, it will work.
  324. #define MAX_STATUS_STR GUI_MAX_STR
  325. // Maximum number of chat strings in the box.
  326. #define MAX_CHAT_STRINGS 20
  327. // Time between game setup net sends.
  328. #define OPTIONS_SEND_FREQUENCY 2000
  329. // Amount of time that must elapse after the first "Cancel" before an
  330. // addition "Cancel" wil be recognized.
  331. #define CANCEL_DELAY_TIME 1500
  332. // If status hasn't been updated for this long, then we will display the default message
  333. #define STATUS_MAX_DISPLAY_TIME 2000
  334. // NOTE: If we add anymore icons, we might as well just spend the extra few
  335. // minutes making a separate file for them.
  336. // Net problems icon file.
  337. #define NET_PROB_GUI_FILE "menu/netprob.gui"
  338. // Initial location for net problems icon on screen.
  339. #define NET_PROB_GUI_X 10
  340. #define NET_PROB_GUI_Y 40
  341. #define NET_PROB_TEXT_SHADOW_COLOR_INDEX 4
  342. #define TERMINATING_GUI_ID 0x80000000
  343. // Note that the following color indices are from the 'Artie Zone' of colors
  344. // in the menu palette and, therefore, should not change.
  345. #define LIST_ITEM_TEXT_SHADOW_COLOR_INDEX 220
  346. // This has been remapped to a Win32 color to make it stand out more
  347. #define NET_STATUS_MSG_COLOR_INDEX 247
  348. //////////////////////////////////////////////////////////////////////////////
  349. // THIS ENTIRE SECTION OF STRINGS SHOULD BE MOVED TO LOCALIZE.CPP!!!
  350. //////////////////////////////////////////////////////////////////////////////
  351. // Client-specific messages displayed in client dialog's status area
  352. char* g_pszClientStat_NameTooLongForChat = "Name too long, can't chat";
  353. char* g_pszClientStat_YouWereDropped = "Connection was lost";
  354. char* g_pszClientStat_SomeoneDropped_s = "\"%s\" is no longer connected";
  355. char* g_pszClientStat_ServerAborted = "Game aborted";
  356. char* g_pszClientStat_ServerStarted = "Starting game";
  357. char* g_pszClientStat_Opened = "Opened connection";
  358. char* g_pszClientStat_Connected = "Connected";
  359. char* g_pszClientStat_JoinDenyTooMany = "Can't join -- too many players";
  360. char* g_pszClientStat_JoinDenyLowBandwidth = "Can't join -- your bandwidth is too low";
  361. char* g_pszClientStat_JoinDenyCantDropIn = "Can't join -- game has already started";
  362. char* g_pszClientStat_JoinDenyUnknown = "Can't join -- don't know why";
  363. char* g_pszClientStat_JoinAccepted = "Joined";
  364. char* g_pszClientStat_LoginAccepted_hd = "Logged in (ID = %hd)";
  365. char* g_pszClientStat_Startup = "Trying to connect...";
  366. char* g_pszClientStat_Default = "Ready";
  367. char* g_pszClientStat_Error_s = "Network error (%s) -- aborting";
  368. char* g_pszClientStat_Retrying = "Retry...";
  369. // Server-specific messages displayed in server dialog's status area
  370. char* g_pszServerStat_InvalidDropReq_hd = "Ignored invalid drop request (ID = %hd)";
  371. char* g_pszServerStat_AcceptedClient = "\"%s\" has joined";
  372. char* g_pszServerStat_CantAcceptJoinReq = "Can't grant join request (too many players)";
  373. char* g_pszServerStat_InvalidChangeReq_hd = "Ignored invalid change request (ID = %hd)";
  374. char* g_pszServerStat_LoginAccepted_hd = "Login accepted (id = %hd)";
  375. char* g_pszServerStat_LoginDeniedVersion_ld = "Login denied (obsolete client version %ld)";
  376. char* g_pszServerStat_LoginDeniedMagic = "Login denied (invalid signature)";
  377. char* g_pszServerStat_Startup = "Setting up connection...";
  378. char* g_pszServerStat_Default = "Ready";
  379. char* g_pszServerStat_CantDropSelf = "You can't drop yourself";
  380. char* g_pszServerStat_PlayerErr = "Player was dropped (bad connection)";
  381. // General messages displayed in client or server dialog's status area
  382. char* g_pszNetStat_Aborting = "Aborting...";
  383. char* g_pszNetStat_Starting = "Starting game...";
  384. char* g_pszNetStat_AttemptToDrop_s = "Attempting to drop \"%s\"";
  385. char* g_pszNetStat_UnhandledMsg = "Ignoring extraneous message";
  386. char* g_pszNetStat_NoError = "No errors";
  387. char* g_pszNetStat_ReceiveError = "Network error (can't receive data)";
  388. char* g_pszNetStat_InQFullError = "Network error (input buffer is full)";
  389. char* g_pszNetStat_OutQFullError = "Network error (output buffer is full)";
  390. char* g_pszNetStat_SendError = "Network error (can't send data)";
  391. char* g_pszNetStat_InQReadError = "Network error (can't read data)";
  392. char* g_pszNetStat_OutQWriteError = "Network error (couldn't write data)";
  393. char* g_pszNetStat_ConnectionError = "Network error (bad connection)";
  394. char* g_pszNetStat_TimeoutError = "Network error (time-out)";
  395. char* g_pszNetStat_ListenError = "Network error (can't listen)";
  396. char* g_pszNetStat_ConnectError = "Network error (can't connect)";
  397. char* g_pszNetStat_ConnectTimeoutError = "Network error (connection attempt timed-out)";
  398. char* g_pszNetStat_ClientVersionMismatchError_lu_lu = "Version mismatch--dropping (Host ver is %lu -- Our ver is %lu)";
  399. char* g_pszNetStat_ServerVersionMismatchError_lu_lu = "Version mismatch--dropping client (Client ver is %lu -- Our ver is %lu)";
  400. char* g_pszNetStat_CantOpenPeerSocketError = "Network error (couldn't connect to other players)";
  401. char* g_pszNetStat_LoginDeniedError = "Login failed";
  402. char* g_pszNetStat_JoinDeniedError = "Host refused join request";
  403. char* g_pszNetStat_UnknownError = "Network error (general failure)";
  404. char* g_pszNetStat_ProgramError = "Network error (generic failure)";
  405. #if defined(WIN32)
  406. char* g_pszNetStat_ClientPlatformMismatchError = "Cannot login to host because it is a Mac";
  407. char* g_pszNetStat_ServerPlatformMismatchError = "Cannot allow client to connect because it is a Mac";
  408. #else
  409. char* g_pszNetStat_ClientPlatformMismatchError = "Cannot login to host because it is a PC";
  410. char* g_pszNetStat_ServerPlatformMismatchError = "Cannot allow client to connect because it is a PC";
  411. #endif
  412. // This is what we say when the user has chosen a protocol that is not supported.
  413. // There are two variations: one for if the user only has one choice (because we
  414. // only support that one) and the other for if the user can try another choice.
  415. char* g_pszNetOnlyProtocolUnsupported_s =
  416. "Your system does not support \"%s\", which is the required network protocol.\n"
  417. "\n"
  418. "This protocol must be added to your system before multiplayer mode can be used.";
  419. char* g_pszNetProtocolUnsupported_s =
  420. "Your system does not support \"%s\", which is the currently selected network protocol.\n"
  421. "\n"
  422. "Either add this protocol to your system or choose a different protocol from the "
  423. "multiplayer options menu.";
  424. // Text which is used to dynamically update one of the text fields on the client or server dialog
  425. char* g_pszNetDlg_ConnectedPlayers_d = "Connected Players: %d";
  426. // Text which is used for the net problems GUI.
  427. // WARNING: This is an EXTERN and is used by other modules!
  428. char* g_pszNetProb_General =
  429. "Network not responding.\nYou can wait or\npress " NET_PROB_GUI_ABORT_KEY_TEXT" to abort";
  430. // Text to prefix net status messages, if any.
  431. char* g_pszNetStatusMsgPrefix = "> ";
  432. //////////////////////////////////////////////////////////////////////////////
  433. // Module specific typedefs.
  434. //////////////////////////////////////////////////////////////////////////////
  435. // Dialog actions
  436. typedef enum
  437. {
  438. DLG_NOTHING,
  439. DLG_OK,
  440. DLG_CANCEL,
  441. DLG_CHAT,
  442. DLG_DISCONNECT_PLAYER,
  443. DLG_OPTIONS_UPDATED,
  444. DLG_RETRY
  445. } DLG_ACTION;
  446. // Dialog types.
  447. typedef enum
  448. {
  449. DLG_SERVER,
  450. DLG_CLIENT,
  451. DLG_BROWSER
  452. } DLG_TYPE;
  453. // GUI/Var Link
  454. typedef struct
  455. {
  456. typedef enum
  457. {
  458. Long,
  459. ULong,
  460. Short,
  461. UShort,
  462. Char,
  463. UChar,
  464. String,
  465. Bool,
  466. Gui
  467. } Type;
  468. long lId; // ID of GUI to link to.
  469. Type type;
  470. #if 1
  471. union
  472. {
  473. void* pvLink;
  474. long* pl;
  475. ULONG* pul;
  476. short* ps;
  477. USHORT* pus;
  478. char* pc;
  479. UCHAR* puc;
  480. char* psz;
  481. bool* pb;
  482. RGuiItem** ppgui;
  483. };
  484. #else
  485. void* pvLink;
  486. #endif
  487. } GuiLink;
  488. //////////////////////////////////////////////////////////////////////////////
  489. // Module specific (static) variables / Instantiate class statics.
  490. //////////////////////////////////////////////////////////////////////////////
  491. // Common.
  492. static RGuiItem* ms_pguiRoot = NULL; // Root of GUI tree for network interface.
  493. static RGuiItem* ms_pguiOk = NULL; // GUI that, once 'clicked', indicates acceptance.
  494. static RGuiItem* ms_pguiCancel = NULL; // GUI that, once 'clicked', indicates rejection.
  495. // Client/Server common.
  496. static RListBox* ms_plbPlayers = NULL; // Listbox used by both types of net dialogs.
  497. static RListBox* ms_plbNetConsole = NULL; // List of net console (chat and status) strings.
  498. static RGuiItem* ms_pguiChatText = NULL; // Chat text.
  499. static RGuiItem* ms_pguiChatSend = NULL; // Chat send button.
  500. // Server.
  501. static RListBox* ms_plbLevelBrowse = NULL; // Level browse listbox.
  502. static RGuiItem* ms_pguiDisconnectPlayer = NULL; // Disconnect a player button.
  503. static RMultiBtn* ms_pmbCoopLevels = NULL; // Coop levels checkbox.
  504. static RMultiBtn* ms_pmbCoopMode = NULL; // Coop mode checkbox.
  505. // Client.
  506. static RGuiItem* ms_pguiOptions = NULL; // Options shown on client dialog.
  507. static RGuiItem* ms_pguiRetry = NULL; // Retry button on client dialog shown when needed.
  508. // Browser.
  509. static RListBox* ms_plbHostBrowse = NULL; // Browse for host listbox.
  510. // Other static vars.
  511. static long ms_lWatchdogTime = 0; // Watchdog timer
  512. static bool ms_bNetBlockingAbort = false; // Net blocking abort flag
  513. static long ms_lNumConsoleEntries = 0; // Track number of chat items.
  514. static bool ms_bGotSetupMsg = false;
  515. static short ms_sSetupRealmNum = 0;
  516. static char ms_szSetupRealmFile[Net::MaxRealmNameSize];
  517. static long ms_lSetupLastChatComplaint = 0;
  518. static long ms_lNextOptionsUpdateTime; // Next time to send an options update.
  519. static RTxt* ms_ptxtNetProb = NULL; // Net problem GUI.
  520. static bool m_bNetWatchdogExpired = false; // Whether net blocking expired
  521. static bool ms_bCoopLevels = false; // true, to use cooperative levels.
  522. // This is used by UpdateDialog() to perform GUI processing.
  523. static RProcessGui ms_pgDoGui;
  524. // Linkable vars.
  525. static bool ms_bTimeLimit = false; // Enable Time Limit if true.
  526. static bool ms_bKillLimit = false; // Enable Kill Limit if true.
  527. static bool ms_bCoopMode = false; // true, for cooperative mode (false for deathmatch).
  528. static GuiLink ms_aglServerLinkage[] =
  529. {
  530. { GUI_ID_DISCONNECT_PLAYER, GuiLink::Gui, &ms_pguiDisconnectPlayer, },
  531. // { GUI_ID_RESPAWN_PLAYERS, GuiLink::Short, &g_GameSettings.m_sHostRejuvenate, },
  532. { GUI_ID_ENABLE_TIME_LIMIT, GuiLink::Bool, &ms_bTimeLimit, },
  533. { GUI_ID_EDIT_TIME_LIMIT, GuiLink::Short, &g_GameSettings.m_sHostTimeLimit, },
  534. { GUI_ID_ENABLE_KILL_LIMIT, GuiLink::Bool, &ms_bKillLimit, },
  535. { GUI_ID_EDIT_KILL_LIMIT, GuiLink::Short, &g_GameSettings.m_sHostKillLimit, },
  536. { GUI_ID_LEVEL_LISTBOX, GuiLink::Gui, &ms_plbLevelBrowse, },
  537. { GUI_ID_SHOW_COOP_LEVELS, GuiLink::Gui, &ms_pmbCoopLevels, },
  538. { GUI_ID_ENABLE_COOP_MODE, GuiLink::Gui, &ms_pmbCoopMode, },
  539. { GUI_ID_ENABLE_COOP_MODE, GuiLink::Bool, &ms_bCoopMode, },
  540. { static_cast<long>(TERMINATING_GUI_ID), }, // Terminator.
  541. };
  542. static GuiLink ms_aglClientLinkage[] =
  543. {
  544. { GUI_ID_OPTIONS_STATIC, GuiLink::Gui, &ms_pguiOptions, },
  545. { GUI_ID_RETRY, GuiLink::Gui, &ms_pguiRetry, },
  546. { static_cast<long>(TERMINATING_GUI_ID), }, // Terminator.
  547. };
  548. static GuiLink ms_aglClientServerLinkage[] =
  549. {
  550. { GUI_ID_PLAYERS_LISTBOX, GuiLink::Gui, &ms_plbPlayers, },
  551. { GUI_ID_CHAT_TEXT, GuiLink::Gui, &ms_pguiChatText, },
  552. { GUI_ID_CHAT_SEND, GuiLink::Gui, &ms_pguiChatSend, },
  553. { GUI_ID_NET_CONSOLE, GuiLink::Gui, &ms_plbNetConsole, },
  554. { GUI_ID_OK, GuiLink::Gui, &ms_pguiOk, },
  555. { GUI_ID_CANCEL, GuiLink::Gui, &ms_pguiCancel, },
  556. { static_cast<long>(TERMINATING_GUI_ID), }, // Terminator.
  557. };
  558. static GuiLink ms_aglBrowserLinkage[] =
  559. {
  560. { GUI_ID_HOST_LISTBOX, GuiLink::Gui, &ms_plbHostBrowse, },
  561. { GUI_ID_OK, GuiLink::Gui, &ms_pguiOk, },
  562. { GUI_ID_CANCEL, GuiLink::Gui, &ms_pguiCancel, },
  563. { static_cast<long>(TERMINATING_GUI_ID), }, // Terminator.
  564. };
  565. //////////////////////////////////////////////////////////////////////////////
  566. // Module specific (static) protos.
  567. //////////////////////////////////////////////////////////////////////////////
  568. static void AddConsoleMsg( // Returns nothing.
  569. bool bChat, // In: true for a chat message, or false for others (like net status).
  570. const char* pszFrmt, // In: sprintf style formatting.
  571. ...); // In: Optional arguments based on context of pszFrmt.
  572. // Get dialog resource
  573. short DlgGetRes( // Returns 0 if successfull, non-zero otherwise
  574. RGuiItem* pgui); // I/O: Pointer to gui item
  575. // Release dialog resource
  576. void DlgReleaseRes( // Returns 0 if successfull, non-zero otherwise
  577. RGuiItem* pgui); // I/O: Pointer to gui item
  578. // Net blocking callback
  579. static short NetBlockingCallback(void); // Returns 0 to continue normally, 1 to abort
  580. static short BrowseForHost(
  581. CNetServer* pserver, // I/O: Server interface or NULL if none
  582. RSocket::Address* paddress); // Out: Address returned here (if successfull)
  583. static short FindSpecificSystem(
  584. RSocket::Address* paddress); // Out: Address returned here (if successfull)
  585. static short BrowseForSelf(
  586. CNetServer* pserver, // I/O: Server interface
  587. RSocket::Address* paddress); // Out: Address returned here (if successfull)
  588. //////////////////////////////////////////////////////////////////////////////
  589. // Helper for UploadLinks() to handle all integer vars.
  590. //////////////////////////////////////////////////////////////////////////////
  591. template <class Int> // Templated input type (can be unsigned).
  592. void UploadLinkInteger( // Returns nothing.
  593. RGuiItem* pgui, // In: GUI to upload to.
  594. Int i) // In: Input val to upload to GUI.
  595. {
  596. ASSERT(pgui);
  597. switch (pgui->m_type)
  598. {
  599. case RGuiItem::MultiBtn:
  600. {
  601. RMultiBtn* pmb = (RMultiBtn*)pgui;
  602. if (i)
  603. {
  604. pmb->m_sState = 1;
  605. }
  606. else
  607. {
  608. pmb->m_sState = 2;
  609. }
  610. break;
  611. }
  612. case RGuiItem::PushBtn:
  613. {
  614. RPushBtn* ppushbtn = (RPushBtn*)pgui;
  615. if (i)
  616. {
  617. ppushbtn->m_state = RPushBtn::On;
  618. }
  619. else
  620. {
  621. ppushbtn->m_state = RPushBtn::Off;
  622. }
  623. break;
  624. }
  625. default:
  626. #if 0
  627. // Determine if signed (this is wierd but may work) . . .
  628. if ((Int)-1 < 0)
  629. {
  630. // Signed.
  631. pgui->SetText("%ld", (long)i);
  632. }
  633. else
  634. {
  635. // Unsigned.
  636. pgui->SetText("%lu", (ULONG)i);
  637. }
  638. #else
  639. // Hardwire to signed b/c bool was displaying a warning regarding the
  640. // above comparison to determine the [un]signed nature of the templated
  641. // type.
  642. pgui->SetText("%ld", (long)i);
  643. #endif
  644. break;
  645. }
  646. pgui->Compose();
  647. }
  648. //////////////////////////////////////////////////////////////////////////////
  649. // Helper for DownloadLinks() to handle all integer vars.
  650. //////////////////////////////////////////////////////////////////////////////
  651. template <class Int> // Templated output type (can be unsigned).
  652. void DownloadLinkInteger( // Returns nothing.
  653. RGuiItem* pgui, // In: GUI to download from.
  654. Int* pi) // Out: Input val to download into from GUI.
  655. {
  656. ASSERT(pgui);
  657. switch (pgui->m_type)
  658. {
  659. case RGuiItem::MultiBtn:
  660. {
  661. RMultiBtn* pmb = (RMultiBtn*)pgui;
  662. *pi = (pmb->m_sState == 1) ? 1 : 0;
  663. break;
  664. }
  665. case RGuiItem::PushBtn:
  666. {
  667. RPushBtn* ppushbtn = (RPushBtn*)pgui;
  668. *pi = (ppushbtn->m_state == RPushBtn::On) ? 1 : 0;
  669. break;
  670. }
  671. default:
  672. #if 0
  673. // Determine if signed (this is wierd but may work) . . .
  674. if ((Int)-1 < 0)
  675. {
  676. // Signed.
  677. *pi = pgui->GetVal();
  678. }
  679. else
  680. {
  681. // Unsigned.
  682. *pi = strtoul(pgui->m_szText, NULL, 0);
  683. }
  684. #else
  685. // Hardwire to signed b/c bool was displaying a warning regarding the
  686. // above comparison to determine the [un]signed nature of the templated
  687. // type.
  688. *pi = pgui->GetVal();
  689. #endif
  690. break;
  691. }
  692. pgui->Compose();
  693. }
  694. //////////////////////////////////////////////////////////////////////////////
  695. //
  696. // Upload variables to their GUI links.
  697. //
  698. //////////////////////////////////////////////////////////////////////////////
  699. static void UploadLinks( // Returns nothing.
  700. GuiLink* pagl, // In: Links to upload.
  701. RGuiItem* pguiRoot) // In: GUI to upload to.
  702. {
  703. ASSERT(pguiRoot);
  704. while (pagl->lId != TERMINATING_GUI_ID)
  705. {
  706. // Get item.
  707. RGuiItem* pgui = pguiRoot->GetItemFromId(pagl->lId);
  708. if (pgui)
  709. {
  710. switch (pagl->type)
  711. {
  712. case GuiLink::Long:
  713. UploadLinkInteger(pgui, *pagl->pl);
  714. break;
  715. case GuiLink::ULong:
  716. UploadLinkInteger(pgui, *pagl->pul);
  717. break;
  718. case GuiLink::Short:
  719. UploadLinkInteger(pgui, *pagl->ps);
  720. break;
  721. case GuiLink::UShort:
  722. UploadLinkInteger(pgui, *pagl->pus);
  723. break;
  724. case GuiLink::Char:
  725. UploadLinkInteger(pgui, *pagl->pc);
  726. break;
  727. case GuiLink::UChar:
  728. UploadLinkInteger(pgui, *pagl->puc);
  729. break;
  730. case GuiLink::Bool:
  731. UploadLinkInteger(pgui, *pagl->pb);
  732. break;
  733. case GuiLink::String:
  734. pgui->SetText("%s", pagl->psz );
  735. break;
  736. // This may seem backward but it's more convenient.
  737. case GuiLink::Gui:
  738. *pagl->ppgui = pgui;
  739. break;
  740. default:
  741. TRACE("UploadLinks(): Type %d not supported.\n",
  742. pagl->type);
  743. break;
  744. }
  745. }
  746. else
  747. {
  748. TRACE("UploadLinks(): GUI ID %d does not exist.\n", pagl->lId);
  749. }
  750. // Next.
  751. pagl++;
  752. }
  753. }
  754. //////////////////////////////////////////////////////////////////////////////
  755. //
  756. // Download variables to their GUI links.
  757. //
  758. //////////////////////////////////////////////////////////////////////////////
  759. static void DownloadLinks( // Returns nothing.
  760. GuiLink* pagl, // In: Links to upload.
  761. RGuiItem* pguiRoot) // In: GUI to upload to.
  762. {
  763. ASSERT(pguiRoot);
  764. while (pagl->lId != TERMINATING_GUI_ID)
  765. {
  766. // Get item.
  767. RGuiItem* pgui = pguiRoot->GetItemFromId(pagl->lId);
  768. if (pgui)
  769. {
  770. switch (pagl->type)
  771. {
  772. case GuiLink::Long:
  773. DownloadLinkInteger(pgui, pagl->pl);
  774. break;
  775. case GuiLink::ULong:
  776. DownloadLinkInteger(pgui, pagl->pul);
  777. break;
  778. case GuiLink::Short:
  779. DownloadLinkInteger(pgui, pagl->ps);
  780. break;
  781. case GuiLink::UShort:
  782. DownloadLinkInteger(pgui, pagl->pus);
  783. break;
  784. case GuiLink::Char:
  785. DownloadLinkInteger(pgui, pagl->pc);
  786. break;
  787. case GuiLink::UChar:
  788. DownloadLinkInteger(pgui, pagl->puc);
  789. break;
  790. case GuiLink::Bool:
  791. DownloadLinkInteger(pgui, pagl->pb);
  792. break;
  793. case GuiLink::String:
  794. strcpy( pagl->psz, pgui->m_szText );
  795. break;
  796. case GuiLink::Gui:
  797. break;
  798. default:
  799. TRACE("DownloadLinks(): Type %d not yet supported.\n",
  800. pagl->type);
  801. break;
  802. }
  803. }
  804. else
  805. {
  806. TRACE("DownloadLinks(): GUI ID %d does not exist.\n", pagl->lId);
  807. }
  808. // Next.
  809. pagl++;
  810. }
  811. }
  812. //////////////////////////////////////////////////////////////////////////////
  813. //
  814. // Macro to activate certain parms to make our list item GUI's look nice and
  815. // readable.
  816. //
  817. //////////////////////////////////////////////////////////////////////////////
  818. inline void MakeMoreReadable( // Returns nothing.
  819. RGuiItem* pgui) // In: GUI to shadowify using global values.
  820. {
  821. ASSERT(pgui);
  822. if (pgui)
  823. {
  824. pgui->m_sTextEffects = RGuiItem::Shadow;
  825. pgui->m_u32TextShadowColor = LIST_ITEM_TEXT_SHADOW_COLOR_INDEX;
  826. pgui->m_sShowFocus = FALSE;
  827. }
  828. }
  829. //////////////////////////////////////////////////////////////////////////////
  830. //
  831. // [Re]compose Options GUI with word wrap preserving original word wrap status.
  832. //
  833. //////////////////////////////////////////////////////////////////////////////
  834. inline void ComposeOptions(void)
  835. {
  836. // Most likely someone has already checked this before calling this but
  837. // it doesn't hurt to be safe.
  838. if (ms_pguiOptions)
  839. {
  840. // Store old word wrap status so we can restore it when done.
  841. short sWordWrapWas = (ms_pguiOptions->m_pprint->m_eModes & RPrint::WORD_WRAP) ? TRUE : FALSE;
  842. // Enable word wrap (not accessible from GUI editor currently).
  843. ms_pguiOptions->m_pprint->SetWordWrap(TRUE);
  844. ms_pguiOptions->Compose();
  845. // Reset word wrap.
  846. ms_pguiOptions->m_pprint->SetWordWrap(sWordWrapWas);
  847. }
  848. }
  849. //////////////////////////////////////////////////////////////////////////////
  850. //
  851. // Clean/Reset Client dialog to defaults.
  852. //
  853. //////////////////////////////////////////////////////////////////////////////
  854. static void CleanClientDlg(
  855. CNetClient* pnet,
  856. bool bRetryButton)
  857. {
  858. if (ms_pguiOptions)
  859. {
  860. // Set no-connection text for options GUI.
  861. ms_pguiOptions->SetText("Not currently connected to a host.");
  862. // Repaginate now!
  863. ComposeOptions();
  864. }
  865. if (ms_plbPlayers)
  866. {
  867. // Empty players listbox.
  868. ms_plbPlayers->RemoveAll();
  869. }
  870. int iNumPlayers = (pnet) ? pnet->GetNumPlayers() : 0;
  871. // This cheese updates the number of players displayed. I cannot remember why we did not simply
  872. // make this one text shadowed GUI.
  873. RGuiItem* pguiConnected = ms_pguiRoot->GetItemFromId(GUI_ID_PLAYERS_STATIC1);
  874. RSP_SAFE_GUI_REF_VOID(pguiConnected, SetText(g_pszNetDlg_ConnectedPlayers_d, iNumPlayers));
  875. RSP_SAFE_GUI_REF_VOID(pguiConnected, Compose());
  876. pguiConnected = ms_pguiRoot->GetItemFromId(GUI_ID_PLAYERS_STATIC2);
  877. RSP_SAFE_GUI_REF_VOID(pguiConnected, SetText(g_pszNetDlg_ConnectedPlayers_d, iNumPlayers));
  878. RSP_SAFE_GUI_REF_VOID(pguiConnected, Compose());
  879. if (ms_pguiRetry)
  880. {
  881. // Show as specified by caller.
  882. ms_pguiRetry->SetVisible(bRetryButton ? TRUE : FALSE);
  883. }
  884. }
  885. //////////////////////////////////////////////////////////////////////////////
  886. //
  887. // Show the selectable network levels in the hood to play listbox.
  888. //
  889. //////////////////////////////////////////////////////////////////////////////
  890. static short ShowLevels(void) // Returns 0 on success.
  891. {
  892. short sRes = 0; // Assume success.
  893. if (ms_plbLevelBrowse != NULL)
  894. {
  895. // Must be listbox.
  896. ASSERT(ms_plbLevelBrowse->m_type == RGuiItem::ListBox);
  897. // Get rid of existing entries.
  898. ms_plbLevelBrowse->RemoveAll();
  899. #if !defined(ENABLE_PLAY_SPECIFIC_REALMS_ONLY)
  900. // Add all the available realms.
  901. char szRealm[RSP_MAX_PATH+1];
  902. char szTitle[512];
  903. short i = 0;
  904. while (sRes == 0)
  905. {
  906. // Get realm name from realm prefs file
  907. sRes = Play_GetRealmInfo(true, ms_bCoopLevels, false, false, i, g_GameSettings.m_sDifficulty, szRealm, sizeof(szRealm), szTitle, sizeof(szTitle));
  908. if (sRes == 1)
  909. {
  910. // That's it.
  911. sRes = 0;
  912. break;
  913. }
  914. else if (sRes == 0)
  915. {
  916. // Add to listbox.
  917. RGuiItem* pguiLevel = ms_plbLevelBrowse->AddString(szTitle);
  918. if (pguiLevel != NULL)
  919. {
  920. // Activate shadow parameters.
  921. MakeMoreReadable(pguiLevel);
  922. pguiLevel->Compose();
  923. pguiLevel->m_lId = i;
  924. }
  925. else
  926. {
  927. TRACE("ShowLevels(): Failed to add string to listbox.\n");
  928. sRes = -1;
  929. }
  930. }
  931. i++;
  932. }
  933. #else
  934. // Add the one and only level option.
  935. RGuiItem* pguiLevel = ms_plbLevelBrowse->AddString(SPECIFIC_MP_REALM_TEXT);
  936. if (pguiLevel != NULL)
  937. {
  938. // Activate shadow parameters.
  939. MakeMoreReadable(pguiLevel);
  940. pguiLevel->Compose();
  941. pguiLevel->m_lId = SPECIFIC_MP_REALM_NUM;
  942. }
  943. else
  944. {
  945. TRACE("ShowLevels(): Failed to add string to listbox.\n");
  946. sRes = -1;
  947. }
  948. #endif // ENABLE_PLAY_SPECIFIC_REALMS_ONLY
  949. // Select the first item in the listbox.
  950. ms_plbLevelBrowse->SetSel(ms_plbLevelBrowse->GetFirst() );
  951. // Update listbox.
  952. ms_plbLevelBrowse->AdjustContents();
  953. }
  954. return sRes;
  955. }
  956. //////////////////////////////////////////////////////////////////////////////
  957. //
  958. // Be done with the current dialog and related settings.
  959. //
  960. //////////////////////////////////////////////////////////////////////////////
  961. static void DlgBeGone(void)
  962. {
  963. // Clean up processor.
  964. ms_pgDoGui.Unprepare();
  965. if (ms_pguiRoot != NULL)
  966. {
  967. delete ms_pguiRoot;
  968. }
  969. ms_pguiRoot = NULL;
  970. ms_pguiOk = NULL;
  971. ms_pguiCancel = NULL;
  972. ms_plbPlayers = NULL;
  973. ms_plbNetConsole = NULL;
  974. ms_pguiChatText = NULL;
  975. ms_pguiChatSend = NULL;
  976. ms_plbLevelBrowse = NULL;
  977. ms_pguiOptions = NULL;
  978. ms_pguiDisconnectPlayer = NULL;
  979. ms_pmbCoopLevels = NULL;
  980. ms_pmbCoopMode = NULL;
  981. ms_plbHostBrowse = NULL;
  982. ms_pguiRetry = NULL;
  983. }
  984. //////////////////////////////////////////////////////////////////////////////
  985. //
  986. // Setup the specified dialog and related settings.
  987. //
  988. //////////////////////////////////////////////////////////////////////////////
  989. static short SetupDlg( // Returns 0 on success.
  990. char* pszGuiFile, // In: Full path to GUI file.
  991. DLG_TYPE type) // In: Type of dialog.
  992. {
  993. short sRes = 0; // Assume success.
  994. // Make sure everything is clean.
  995. DlgBeGone();
  996. // Anh...reset these here.
  997. ms_lNumConsoleEntries = 0;
  998. ms_lNextOptionsUpdateTime = 0;
  999. // Create a dialog. We have to take this approach instea of the nicer
  1000. // LoadInstantiate() method because we need to install callbacks for
  1001. // getting and releasing resources, which we can only do if we already
  1002. // have the gui item (the dialog in this case). With LoadInstantiate(),
  1003. // the dialog doesn't exist until after the call, and by then the resource
  1004. // has already been loaded (or failed to load in this case because the
  1005. // gui stuff can't possibly know the correct path for loading it).
  1006. ms_pguiRoot = new RDlg;
  1007. if (ms_pguiRoot != NULL)
  1008. {
  1009. // Setup callbacks for getting and releasing resources
  1010. ms_pguiRoot->m_fnGetRes = DlgGetRes;
  1011. ms_pguiRoot->m_fnReleaseRes = DlgReleaseRes;
  1012. // Set font. The size in this call matters not b/c each GUI item
  1013. // has a size it uses when printing.
  1014. ms_pguiRoot->m_pprint->SetFont(15, &g_fontPostal);
  1015. // Load the gui description file
  1016. sRes = ms_pguiRoot->Load(pszGuiFile);
  1017. if (sRes == 0)
  1018. {
  1019. // Set focus to first child control or none if there's none.
  1020. ms_pguiRoot->SetFocus(ms_pguiRoot->m_listguiChildren.GetHead());
  1021. // The special case stuff.
  1022. switch (type)
  1023. {
  1024. case DLG_CLIENT:
  1025. {
  1026. // Upload client stuff.
  1027. UploadLinks(ms_aglClientLinkage, ms_pguiRoot);
  1028. // Upload stuff common to client/server.
  1029. UploadLinks(ms_aglClientServerLinkage, ms_pguiRoot);
  1030. // If exists . . .
  1031. if (ms_plbPlayers != NULL)
  1032. {
  1033. // Must be listbox.
  1034. ASSERT(ms_plbPlayers->m_type == RGuiItem::ListBox);
  1035. // Get rid of existing entries.
  1036. ms_plbPlayers->RemoveAll();
  1037. }
  1038. // If exists . . .
  1039. if (ms_pguiChatText != NULL)
  1040. {
  1041. // Must be edit.
  1042. ASSERT(ms_pguiChatText->m_type == RGuiItem::Edit);
  1043. // Limit text.
  1044. ( (REdit*)ms_pguiChatText)->m_sMaxText = Net::MaxChatSize - 1;
  1045. }
  1046. // Make the options GUI more readable.
  1047. if (ms_pguiOptions)
  1048. {
  1049. MakeMoreReadable(ms_pguiOptions);
  1050. }
  1051. // Set title text.
  1052. ms_pguiRoot->SetText(
  1053. "%s [%s]",
  1054. ms_pguiRoot->m_szText,
  1055. g_GameSettings.m_szPlayerName);
  1056. ms_pguiRoot->Compose();
  1057. // Start with a clean slate.
  1058. CleanClientDlg(0, false);
  1059. break;
  1060. }
  1061. case DLG_SERVER:
  1062. {
  1063. // Upload server stuff.
  1064. UploadLinks(ms_aglServerLinkage, ms_pguiRoot);
  1065. // Upload stuff common to client/server.
  1066. UploadLinks(ms_aglClientServerLinkage, ms_pguiRoot);
  1067. // If exists . . .
  1068. if (ms_plbPlayers != NULL)
  1069. {
  1070. // Must be listbox.
  1071. ASSERT(ms_plbPlayers->m_type == RGuiItem::ListBox);
  1072. // Get rid of existing entries.
  1073. ms_plbPlayers->RemoveAll();
  1074. }
  1075. // If exists . . .
  1076. if (ms_pguiChatText != NULL)
  1077. {
  1078. // Must be edit.
  1079. ASSERT(ms_pguiChatText->m_type == RGuiItem::Edit);
  1080. // Limit text.
  1081. ( (REdit*)ms_pguiChatText)->m_sMaxText = Net::MaxChatSize - 1;
  1082. }
  1083. // If exists . . .
  1084. if (ms_pmbCoopLevels)
  1085. {
  1086. // Must be multibtn.
  1087. ASSERT(ms_pmbCoopLevels->m_type == RGuiItem::MultiBtn);
  1088. // Set state to last choice.
  1089. ms_pmbCoopLevels->m_sState = (ms_bCoopLevels == false) ? 1 : 2;
  1090. // Recompose with new state.
  1091. ms_pmbCoopLevels->Compose();
  1092. }
  1093. // If exists . . .
  1094. if (ms_pmbCoopMode)
  1095. {
  1096. // Must be multibtn.
  1097. ASSERT(ms_pmbCoopMode->m_type == RGuiItem::MultiBtn);
  1098. // Set state to last choice.
  1099. ms_pmbCoopMode->m_sState = (ms_bCoopMode == false) ? 2 : 1;
  1100. // Recompose with new state.
  1101. ms_pmbCoopMode->Compose();
  1102. }
  1103. // Fill level browser listbox.
  1104. ShowLevels();
  1105. // Set title text.
  1106. ms_pguiRoot->SetText(
  1107. "%s [%s]",
  1108. ms_pguiRoot->m_szText,
  1109. g_GameSettings.m_szPlayerName);
  1110. ms_pguiRoot->Compose();
  1111. break;
  1112. }
  1113. case DLG_BROWSER:
  1114. {
  1115. // Upload browser linkage.
  1116. UploadLinks(ms_aglBrowserLinkage, ms_pguiRoot);
  1117. // If exists . . .
  1118. if (ms_plbHostBrowse)
  1119. {
  1120. // Must be listbox.
  1121. ASSERT(ms_plbHostBrowse->m_type == RGuiItem::ListBox);
  1122. // Get rid of existing entries.
  1123. ms_plbHostBrowse->RemoveAll();
  1124. }
  1125. break;
  1126. }
  1127. }
  1128. // Center.
  1129. ms_pguiRoot->Move(
  1130. g_pimScreenBuf->m_sWidth / 2 - ms_pguiRoot->m_im.m_sWidth / 2,
  1131. g_pimScreenBuf->m_sHeight / 2 - ms_pguiRoot->m_im.m_sHeight / 2);
  1132. // Make visible.
  1133. ms_pguiRoot->SetVisible(TRUE);
  1134. // Prepare GUI processor.
  1135. ms_pgDoGui.m_sFlags = RProcessGui::NoCleanScreen;
  1136. ms_pgDoGui.Prepare(ms_pguiRoot, ms_pguiOk, ms_pguiCancel);
  1137. }
  1138. else
  1139. {
  1140. TRACE("SetupDlg(): Failed to load .gui file!\n");
  1141. }
  1142. }
  1143. else
  1144. {
  1145. TRACE("SetupDlg(): Failed to allocate RDlg!\n");
  1146. sRes = -1;
  1147. }
  1148. // If any errors . . .
  1149. if (sRes != 0)
  1150. {
  1151. DlgBeGone();
  1152. }
  1153. return sRes;
  1154. }
  1155. //////////////////////////////////////////////////////////////////////////////
  1156. //
  1157. // Get dialog resource
  1158. //
  1159. //////////////////////////////////////////////////////////////////////////////
  1160. short DlgGetRes( // Returns 0 if successfull, non-zero otherwise
  1161. RGuiItem* pgui) // I/O: Pointer to gui item
  1162. {
  1163. short sResult = 0;
  1164. // Release resources first (just in case)
  1165. DlgReleaseRes(pgui);
  1166. // Allocate and load new resources. We get the name of the file (which
  1167. // is ASSUMED to have NO PATH!!) from the gui itself, then tack on the
  1168. // path we need and get the resource from the resource manager.
  1169. char szFile[RSP_MAX_PATH * 2];
  1170. sprintf(szFile, "%s%s", GUI_DIR, pgui->m_szBkdResName);
  1171. if (rspGetResource(&g_resmgrShell, szFile, &pgui->m_pimBkdRes) == 0)
  1172. {
  1173. // Set palette
  1174. ASSERT(pgui->m_pimBkdRes->m_pPalette != NULL);
  1175. ASSERT(pgui->m_pimBkdRes->m_pPalette->m_type == RPal::PDIB);
  1176. rspSetPaletteEntries(
  1177. 0,
  1178. 230,
  1179. pgui->m_pimBkdRes->m_pPalette->Red(0),
  1180. pgui->m_pimBkdRes->m_pPalette->Green(0),
  1181. pgui->m_pimBkdRes->m_pPalette->Blue(0),
  1182. pgui->m_pimBkdRes->m_pPalette->m_sPalEntrySize);
  1183. // Update hardware palette.
  1184. rspUpdatePalette();
  1185. }
  1186. else
  1187. {
  1188. sResult = -1;
  1189. TRACE("DlgGetRes(): Failed to open file '%s'\n", FullPathVD(szFile));
  1190. }
  1191. return sResult;
  1192. }
  1193. //////////////////////////////////////////////////////////////////////////////
  1194. //
  1195. // Release dialog resource
  1196. //
  1197. //////////////////////////////////////////////////////////////////////////////
  1198. void DlgReleaseRes( // Returns 0 if successfull, non-zero otherwise
  1199. RGuiItem* pgui) // I/O: Pointer to gui item
  1200. {
  1201. rspReleaseResource(&g_resmgrShell, &pgui->m_pimBkdRes);
  1202. }
  1203. //////////////////////////////////////////////////////////////////////////////
  1204. //
  1205. // Process GUI tree through one iteration.
  1206. //
  1207. //////////////////////////////////////////////////////////////////////////////
  1208. static DLG_ACTION UpdateDialog( // Returns dialog action
  1209. RGuiItem* pguiRoot, // In: GUI to process via user input.
  1210. bool bReset) // In: If false, does not reset any
  1211. // GUIs before returning their action.
  1212. // Note that this means that the next
  1213. // time through, we'll probably return
  1214. // the same action.
  1215. {
  1216. // Get next input event.
  1217. RInputEvent ie;
  1218. // Make sure we start with no event.
  1219. ie.type = RInputEvent::None;
  1220. rspGetNextInputEvent(&ie);
  1221. // Block minus sign right here (note that this will keep players
  1222. // from being able to use '-' in chat strings).
  1223. if (ie.type == RInputEvent::Key)
  1224. {
  1225. switch (ie.lKey & 0x0000FFFF)
  1226. {
  1227. case '-':
  1228. ie.type = RInputEvent::None;
  1229. break;
  1230. }
  1231. }
  1232. // When we lose the focus, this might need to be updated.
  1233. // Should we do it everytime?
  1234. rspNameBuffers(&g_pimScreenBuf);
  1235. // Process GUI through an iteration.
  1236. long lPressedId = ms_pgDoGui.DoModeless(pguiRoot, &ie, g_pimScreenBuf);
  1237. // If OK chosen or enter pressed . . .
  1238. if (lPressedId == GUI_ID_OK || (ie.type == RInputEvent::Key && (ie.lKey & 0x0000FFFF) == '\r') )
  1239. {
  1240. // If there's a focus . . .
  1241. if (RGuiItem::ms_pguiFocus)
  1242. {
  1243. // If in chatbox . . .
  1244. if (RGuiItem::ms_pguiFocus == ms_pguiChatText)
  1245. {
  1246. // Send chat.
  1247. RSP_SAFE_GUI_REF_VOID(ms_pguiChatSend, SetClicked(TRUE) );
  1248. }
  1249. else if (RGuiItem::ms_pguiFocus == ms_pguiRetry)
  1250. {
  1251. RSP_SAFE_GUI_REF_VOID(ms_pguiRetry, SetClicked(TRUE) );
  1252. }
  1253. else
  1254. {
  1255. // OK.
  1256. RSP_SAFE_GUI_REF_VOID(ms_pguiOk, SetClicked(TRUE) );
  1257. }
  1258. }
  1259. else
  1260. {
  1261. // OK.
  1262. RSP_SAFE_GUI_REF_VOID(ms_pguiOk, SetClicked(TRUE) );
  1263. }
  1264. }
  1265. // Return proper action based on what the user clicked on
  1266. DLG_ACTION action = DLG_NOTHING;
  1267. if (RSP_SAFE_GUI_REF(ms_pguiOk, IsClicked()))
  1268. {
  1269. if (bReset)
  1270. {
  1271. RSP_SAFE_GUI_REF_VOID(ms_pguiOk, SetClicked(FALSE));
  1272. }
  1273. action = DLG_OK;
  1274. }
  1275. if (RSP_SAFE_GUI_REF(ms_pguiCancel, IsClicked()))
  1276. {
  1277. if (bReset)
  1278. {
  1279. RSP_SAFE_GUI_REF_VOID(ms_pguiCancel, SetClicked(FALSE));
  1280. }
  1281. action = DLG_CANCEL;
  1282. }
  1283. if (RSP_SAFE_GUI_REF(ms_pguiRetry, IsClicked() ) )
  1284. {
  1285. if (bReset)
  1286. {
  1287. RSP_SAFE_GUI_REF_VOID(ms_pguiRetry, SetClicked(FALSE) );
  1288. }
  1289. action = DLG_RETRY;
  1290. }
  1291. if (RSP_SAFE_GUI_REF(ms_pguiChatSend, IsClicked()))
  1292. {
  1293. if (bReset)
  1294. {
  1295. if (ms_pguiChatSend)
  1296. {
  1297. ms_pguiChatSend->SetClicked(FALSE);
  1298. // If focus is on the chat send . . .
  1299. if (RGuiItem::ms_pguiFocus == ms_pguiChatSend && ms_pguiChatText)
  1300. {
  1301. // Switch back to the edit...makes sense on the user interface, I think.
  1302. RGuiItem::SetFocus(ms_pguiChatText);
  1303. }
  1304. }
  1305. }
  1306. action = DLG_CHAT;
  1307. }
  1308. if (RSP_SAFE_GUI_REF(ms_pguiDisconnectPlayer, IsClicked() ) )
  1309. {
  1310. if (bReset)
  1311. {
  1312. RSP_SAFE_GUI_REF_VOID(ms_pguiDisconnectPlayer, SetClicked(FALSE) );
  1313. }
  1314. action = DLG_DISCONNECT_PLAYER;
  1315. }
  1316. // If no other action . . .
  1317. if (action == DLG_NOTHING)
  1318. {
  1319. // Temporarily timed based. ***
  1320. long lCurTime = rspGetMilliseconds();
  1321. if (lCurTime > ms_lNextOptionsUpdateTime)
  1322. {
  1323. if (bReset)
  1324. {
  1325. ms_lNextOptionsUpdateTime = lCurTime + OPTIONS_SEND_FREQUENCY;
  1326. }
  1327. action = DLG_OPTIONS_UPDATED;
  1328. }
  1329. }
  1330. if (ms_pmbCoopLevels)
  1331. {
  1332. // If not pressed . . .
  1333. if (ms_pmbCoopLevels->m_sState > 0)
  1334. {
  1335. bool bCoopLevels = (ms_pmbCoopLevels->m_sState == 2) ? true : false;
  1336. // If status has changed . . .
  1337. if (bCoopLevels != ms_bCoopLevels)
  1338. {
  1339. // Set new value.
  1340. ms_bCoopLevels = bCoopLevels;
  1341. // Repaginate.
  1342. ShowLevels();
  1343. // If showing deathmatch levels . . .
  1344. if (bCoopLevels == false && ms_pmbCoopMode)
  1345. {
  1346. // Since cooperative mode in deathmatch levels makes little
  1347. // sense, let's automagically switch to deathmatch mode for
  1348. // convenience sake.
  1349. ms_pmbCoopMode->m_sState = 2;
  1350. ms_pmbCoopMode->Compose();
  1351. }
  1352. }
  1353. }
  1354. }
  1355. return action;
  1356. }
  1357. //////////////////////////////////////////////////////////////////////////////
  1358. //
  1359. // Update hosts listbox for browser.
  1360. //
  1361. //////////////////////////////////////////////////////////////////////////////
  1362. static short UpdateListBox( // Returns 0 on success.
  1363. RListBox* plb, // In: Browser listbox.
  1364. CNetBrowse::Hosts* phostslistPersist, // In: Hosts.
  1365. CNetBrowse::Hosts* phostslistAdded, // In: Hosts to add to listbox.
  1366. CNetBrowse::Hosts* phostslistDropped) // In: Hosts to drop from listbox.
  1367. {
  1368. short sResult = 0; // Assume success.
  1369. if (plb)
  1370. {
  1371. CNetBrowse::Hosts::Pointer i;
  1372. bool bRepaginate = false;
  1373. // Look for unGUIed entries . . .
  1374. for (i = phostslistPersist->GetHead(); i; i = phostslistPersist->GetNext(i))
  1375. {
  1376. CNetBrowse::CHost* phost = &(phostslistPersist->GetData(i) );
  1377. // If not yet GUIed . . .
  1378. if ( !(phost->m_u32User) )
  1379. {
  1380. RGuiItem* pgui = plb->AddString(phost->m_acName);
  1381. if (pgui)
  1382. {
  1383. // Activate shadow parameters.
  1384. MakeMoreReadable(pgui);
  1385. pgui->Compose();
  1386. // Point GUI at entry.
  1387. pgui->m_ulUserData = (ULONG)phost;
  1388. // Successfully added entry.
  1389. phost->m_u32User = (U32)pgui;
  1390. // Note that we updated the dialog and will need to re-adjust
  1391. // fields and recompose.
  1392. bRepaginate = true;
  1393. }
  1394. else
  1395. {
  1396. TRACE("UpdateListBox(): Failed to add a host to the listbox.\n");
  1397. // sResult = -1; // Error?
  1398. }
  1399. }
  1400. }
  1401. // For whatever it's worth, let's do dropped first so it'll be a little
  1402. // faster. On second thought, let's not. It just seems a bit scary. Like,
  1403. // if a host gets added and then dropped on the same iteration, it will not
  1404. // be in the listbox and I was using that conidition to hopefully flag bugs.
  1405. // Note that we use the m_u32User field of the host structure to store a pointer
  1406. // to the corresponding GUI item. Since we know these host items get copied
  1407. // from the added list to the main list and then, possibly, to the dropped list
  1408. // this value will remain with the host all the way to the dropped list.
  1409. while (phostslistDropped->GetHead())
  1410. {
  1411. // Get pointer to first host in list
  1412. U32 u32User = phostslistDropped->GetHeadData().m_u32User;
  1413. // We should have already been using this.
  1414. ASSERT(u32User);
  1415. if (u32User)
  1416. {
  1417. // Remove the corresponding GUI list entry from the listbox.
  1418. plb->RemoveItem((RGuiItem*)(u32User));
  1419. // Note that we updated the dialog and will need to re-adjust
  1420. // fields and recompose.
  1421. bRepaginate = true;
  1422. }
  1423. // Remove first host in list
  1424. phostslistDropped->RemoveHead();
  1425. }
  1426. if (bRepaginate)
  1427. {
  1428. plb->AdjustContents();
  1429. }
  1430. // If there is a selection . . .
  1431. if (plb->GetSel() )
  1432. {
  1433. // Currently, we view this as a choice. Let's simply set the OK button
  1434. // so it'll be easy to switch back and forth between these two methods.
  1435. RSP_SAFE_GUI_REF_VOID(ms_pguiOk, SetClicked(TRUE) );
  1436. }
  1437. // Empty the added list, which we don't use, but we don't want it to keep
  1438. // growing either, and it's our responsibility to empty it.
  1439. while (phostslistAdded->GetHead())
  1440. phostslistAdded->RemoveHead();
  1441. }
  1442. else
  1443. {
  1444. TRACE("UpdateListBox(): No listbox to update.\n");
  1445. sResult = -1;
  1446. }
  1447. return sResult;
  1448. }
  1449. //////////////////////////////////////////////////////////////////////////////
  1450. //
  1451. // Add a console message.
  1452. //
  1453. //////////////////////////////////////////////////////////////////////////////
  1454. static void AddConsoleMsg( // Returns nothing.
  1455. bool bChat, // In: true for a chat message, or false for others (like net status).
  1456. const char* pszFrmt, // In: sprintf style formatting.
  1457. ...) // In: Optional arguments based on context of pszFrmt.
  1458. {
  1459. if (ms_plbNetConsole != NULL)
  1460. {
  1461. char szOutput[MAX_STATUS_STR];
  1462. va_list varp;
  1463. va_start(varp, pszFrmt);
  1464. vsprintf(szOutput, pszFrmt, varp);
  1465. va_end(varp);
  1466. U32 u32TextColor = ms_plbNetConsole->m_u32TextColor;
  1467. char szMsg[MAX_STATUS_STR];
  1468. // If it's a chat message . . .
  1469. if (bChat)
  1470. {
  1471. // Verbatim is good (already includes name and such).
  1472. strcpy(szMsg, szOutput);
  1473. }
  1474. else
  1475. {
  1476. // Otherwise, add optional prefix.
  1477. sprintf(szMsg, "%s%s", g_pszNetStatusMsgPrefix, szOutput);
  1478. // And use alternate color.
  1479. u32TextColor = NET_STATUS_MSG_COLOR_INDEX;
  1480. }
  1481. bool bRepaginate = false; // true to repaginate the listbox.
  1482. // If this one will put us over . . .
  1483. if (ms_lNumConsoleEntries >= MAX_CHAT_STRINGS)
  1484. {
  1485. // Get the oldest chat.
  1486. RGuiItem* pguiOldestConsoleMsg = ms_plbNetConsole->GetFirst();
  1487. if (pguiOldestConsoleMsg)
  1488. {
  1489. // Remove it.
  1490. ms_plbNetConsole->RemoveItem(pguiOldestConsoleMsg);
  1491. // Repaginate.
  1492. bRepaginate = true;
  1493. }
  1494. }
  1495. RGuiItem* pguiConsoleMsg = ms_plbNetConsole->AddString(szMsg);
  1496. if (pguiConsoleMsg)
  1497. {
  1498. // Another chat.
  1499. ms_lNumConsoleEntries++;
  1500. // Store the old border thickness so we know how much we can reduce
  1501. // these.
  1502. short sOrigTotalBorderThickness = pguiConsoleMsg->GetTopLeftBorderThickness() + pguiConsoleMsg->GetBottomRightBorderThickness();
  1503. // No lines.
  1504. pguiConsoleMsg->m_sBorderThickness = 0;
  1505. // Adjust color.
  1506. pguiConsoleMsg->m_u32TextColor = u32TextColor;
  1507. // Activate shadow parameters.
  1508. MakeMoreReadable(pguiConsoleMsg);
  1509. // Recreate without space for border lines . . .
  1510. if (pguiConsoleMsg->Create(
  1511. pguiConsoleMsg->m_sX,
  1512. pguiConsoleMsg->m_sY,
  1513. // We want to keep these as small as possible so we can fit as many as possible.
  1514. // Subtract the border thickness since we don't use it but add 1 for text shadow effect.
  1515. pguiConsoleMsg->m_im.m_sWidth - sOrigTotalBorderThickness + 1,
  1516. pguiConsoleMsg->m_im.m_sHeight - sOrigTotalBorderThickness + 1,
  1517. pguiConsoleMsg->m_im.m_sDepth) == 0)
  1518. {
  1519. // We cannot click on these.
  1520. pguiConsoleMsg->m_sActive = FALSE;
  1521. pguiConsoleMsg->SetActive(FALSE);
  1522. // Repaginate now, now.
  1523. ms_plbNetConsole->AdjustContents();
  1524. // Ensure the new item is visible.
  1525. ms_plbNetConsole->EnsureVisible(pguiConsoleMsg);
  1526. }
  1527. else
  1528. {
  1529. // This is useless then.
  1530. delete pguiConsoleMsg;
  1531. pguiConsoleMsg = NULL;
  1532. }
  1533. }
  1534. else
  1535. {
  1536. // If we need to repaginate anyways . . .
  1537. if (bRepaginate)
  1538. {
  1539. // Repaginate now even though we failed to add a new string.
  1540. ms_plbNetConsole->AdjustContents();
  1541. }
  1542. }
  1543. }
  1544. }
  1545. //////////////////////////////////////////////////////////////////////////////
  1546. //
  1547. // Get descriptive text associated with specified error message
  1548. //
  1549. //////////////////////////////////////////////////////////////////////////////
  1550. extern const char* NetErrorText( // Returns pointer to text
  1551. NetMsg* pmsg) // In: Error message
  1552. {
  1553. static char szStaticErrorText[512];
  1554. char* pText = "";
  1555. if (pmsg->msg.nothing.ucType == NetMsg::ERR)
  1556. {
  1557. switch (pmsg->msg.err.error)
  1558. {
  1559. case NetMsg::NoError:
  1560. pText = g_pszNetStat_NoError;
  1561. break;
  1562. case NetMsg::ReceiveError:
  1563. pText = g_pszNetStat_ReceiveError;
  1564. break;
  1565. case NetMsg::InQFullError:
  1566. pText = g_pszNetStat_InQFullError;
  1567. break;
  1568. case NetMsg::OutQFullError:
  1569. pText = g_pszNetStat_OutQFullError;
  1570. break;
  1571. case NetMsg::SendError:
  1572. pText = g_pszNetStat_SendError;
  1573. break;
  1574. case NetMsg::InQReadError:
  1575. pText = g_pszNetStat_InQReadError;
  1576. break;
  1577. case NetMsg::OutQWriteError:
  1578. pText = g_pszNetStat_OutQWriteError;
  1579. break;
  1580. case NetMsg::ConnectionError:
  1581. pText = g_pszNetStat_ConnectionError;
  1582. break;
  1583. case NetMsg::TimeoutError:
  1584. pText = g_pszNetStat_TimeoutError;
  1585. break;
  1586. case NetMsg::ListenError:
  1587. pText = g_pszNetStat_ListenError;
  1588. break;
  1589. case NetMsg::CantConnectError:
  1590. pText = g_pszNetStat_ConnectError;
  1591. break;
  1592. case NetMsg::ConnectTimeoutError:
  1593. pText = g_pszNetStat_ConnectTimeoutError;
  1594. break;
  1595. case NetMsg::ServerVersionMismatchError:
  1596. pText = szStaticErrorText;
  1597. sprintf(pText, g_pszNetStat_ServerVersionMismatchError_lu_lu, pmsg->msg.err.ulParam, CNetMsgr::CurVersionNum & ~CNetMsgr::MacVersionBit);
  1598. break;
  1599. case NetMsg::ClientVersionMismatchError:
  1600. pText = szStaticErrorText;
  1601. sprintf(pText, g_pszNetStat_ClientVersionMismatchError_lu_lu, pmsg->msg.err.ulParam, CNetMsgr::CurVersionNum & ~CNetMsgr::MacVersionBit);
  1602. break;
  1603. case NetMsg::LoginDeniedError:
  1604. pText = g_pszNetStat_LoginDeniedError;
  1605. break;
  1606. case NetMsg::JoinDeniedError:
  1607. pText = g_pszNetStat_JoinDeniedError;
  1608. break;
  1609. case NetMsg::CantOpenPeerSocketError:
  1610. pText = g_pszNetStat_CantOpenPeerSocketError;
  1611. break;
  1612. case NetMsg::ServerPlatformMismatchError:
  1613. pText = g_pszNetStat_ServerPlatformMismatchError;
  1614. break;
  1615. case NetMsg::ClientPlatformMismatchError:
  1616. pText = g_pszNetStat_ClientPlatformMismatchError;
  1617. break;
  1618. default:
  1619. pText = g_pszNetStat_UnknownError;
  1620. break;
  1621. }
  1622. }
  1623. return pText;
  1624. }
  1625. //////////////////////////////////////////////////////////////////////////////
  1626. //
  1627. // Get the realm filename from the realm title, using the INI.
  1628. //
  1629. //////////////////////////////////////////////////////////////////////////////
  1630. static short GetRealmFileFromRealmTitle( // Returns 0, if found; non-zero
  1631. // otherwise.
  1632. bool bCoopLevel, // In: true, if a coop level; false, if deathmatch level.
  1633. char* pszRealmTitle, // In: Realm title.
  1634. char* pszRealmFileName, // Out: Realm filename.
  1635. short sMaxLen) // In: Max space available at
  1636. // pszRealmFileName.
  1637. {
  1638. short sResult = 0; // Assume success.
  1639. RPrefs prefsRealm;
  1640. // Try opening the realms.ini file on the HD path first, if that fails go to the CD
  1641. sResult = prefsRealm.Open(FullPathHD(g_GameSettings.m_pszRealmPrefsFile), "rt");
  1642. if (sResult != 0)
  1643. sResult = prefsRealm.Open(FullPathCD(g_GameSettings.m_pszRealmPrefsFile), "rt");
  1644. if (sResult == 0)
  1645. {
  1646. // Try each realm section until we find the title we're looking for
  1647. // or we find an empty entry.
  1648. // Multiplayer sections are named "RealmNet1, "RealmNet2", etc.
  1649. // Multiplayer realm entry is always "Realm".
  1650. // The title is always "Title".
  1651. short sRealmNum = 1;
  1652. char szRealmTitle[512];
  1653. char szSection[512];
  1654. bool bFound = false;
  1655. do
  1656. {
  1657. // Form section name.
  1658. sprintf(
  1659. szSection,
  1660. "Realm%sNet%d",
  1661. bCoopLevel ? "Coop" : "",
  1662. sRealmNum++);
  1663. // Safety; renitialize.
  1664. szRealmTitle[0] = '\0';
  1665. // Get the title for this net realm.
  1666. prefsRealm.GetVal(szSection, "Title", "", szRealmTitle);
  1667. // If this is the title of the realm we're looking for . . .
  1668. if (rspStricmp(szRealmTitle, pszRealmTitle) == 0)
  1669. {
  1670. // Found it; get filename.
  1671. // Note that there's no real way to use the sMaxLen.
  1672. prefsRealm.GetVal(szSection, "Realm", "", pszRealmFileName);
  1673. // Done.
  1674. bFound = true;
  1675. }
  1676. } while (szRealmTitle[0] != '\0' && bFound == false);
  1677. // If we didn't find it . . .
  1678. if (bFound == false)
  1679. {
  1680. // Let the caller know.
  1681. sResult = 1;
  1682. }
  1683. }
  1684. return sResult;
  1685. }
  1686. //////////////////////////////////////////////////////////////////////////////
  1687. //
  1688. // Client has been dropped
  1689. //
  1690. //////////////////////////////////////////////////////////////////////////////
  1691. static void OnDroppedMsg(
  1692. CNetClient* pnet, // In: Network interface.
  1693. NetMsg* pmsg) // In: Dropped msg from client to remove.
  1694. {
  1695. ASSERT(pmsg->msg.nothing.ucType == NetMsg::DROPPED);
  1696. // Print message while player's info is still in our database
  1697. if (pmsg->msg.dropped.id == Net::InvalidID)
  1698. AddConsoleMsg(false, "%s", g_pszClientStat_YouWereDropped);
  1699. else
  1700. AddConsoleMsg(false, g_pszClientStat_SomeoneDropped_s, pnet->GetPlayerName(pmsg->msg.dropped.id));
  1701. // Update number of players displayed.
  1702. RGuiItem* pguiConnected = ms_pguiRoot->GetItemFromId(GUI_ID_PLAYERS_STATIC1);
  1703. RSP_SAFE_GUI_REF_VOID(pguiConnected, SetText(g_pszNetDlg_ConnectedPlayers_d, pnet->GetNumPlayers()));
  1704. RSP_SAFE_GUI_REF_VOID(pguiConnected, Compose());
  1705. pguiConnected = ms_pguiRoot->GetItemFromId(GUI_ID_PLAYERS_STATIC2);
  1706. RSP_SAFE_GUI_REF_VOID(pguiConnected, SetText(g_pszNetDlg_ConnectedPlayers_d, pnet->GetNumPlayers()));
  1707. RSP_SAFE_GUI_REF_VOID(pguiConnected, Compose());
  1708. // Get client's GUI . . .
  1709. RGuiItem* pguiClient = ms_plbPlayers->GetItemFromId( long(pmsg->msg.dropped.id));
  1710. if (pguiClient != NULL)
  1711. {
  1712. // Remove the item.
  1713. ms_plbPlayers->RemoveItem(pguiClient);
  1714. ms_plbPlayers->AdjustContents();
  1715. }
  1716. else
  1717. {
  1718. TRACE("RemoveClient(): We didn't even know of this client!!\n");
  1719. }
  1720. }
  1721. //////////////////////////////////////////////////////////////////////////////
  1722. //
  1723. // Player has joined
  1724. //
  1725. //////////////////////////////////////////////////////////////////////////////
  1726. static short OnJoinedMsg( // Returns 0 on success.
  1727. CNetClient* pnet, // In: Network interface.
  1728. NetMsg* pmsg, // In: Joined msg from client to add.
  1729. bool bServer) // In: true if in server mode; false if client.
  1730. {
  1731. short sRes = 0; // Assume success.
  1732. ASSERT(pmsg->msg.nothing.ucType == NetMsg::JOINED);
  1733. UCHAR ucColorIndex = pmsg->msg.joined.ucColor;
  1734. if (ucColorIndex >= CGameSettings::ms_sNumPlayerColorDescriptions)
  1735. ucColorIndex = 0;
  1736. // Add info to listbox.
  1737. char szPlayer[256];
  1738. sprintf(szPlayer, "%s (%s)", pmsg->msg.joined.acName, CGameSettings::ms_apszPlayerColorDescriptions[ucColorIndex]);
  1739. // Update number of players displayed.
  1740. RGuiItem* pguiConnected = ms_pguiRoot->GetItemFromId(GUI_ID_PLAYERS_STATIC1);
  1741. RSP_SAFE_GUI_REF_VOID(pguiConnected, SetText(g_pszNetDlg_ConnectedPlayers_d, pnet->GetNumPlayers()));
  1742. RSP_SAFE_GUI_REF_VOID(pguiConnected, Compose() );
  1743. pguiConnected = ms_pguiRoot->GetItemFromId(GUI_ID_PLAYERS_STATIC2);
  1744. RSP_SAFE_GUI_REF_VOID(pguiConnected, SetText(g_pszNetDlg_ConnectedPlayers_d, pnet->GetNumPlayers()));
  1745. RSP_SAFE_GUI_REF_VOID(pguiConnected, Compose() );
  1746. RGuiItem* pguiClient = ms_plbPlayers->AddString(szPlayer);
  1747. if (pguiClient != NULL)
  1748. {
  1749. // Let's be able to identify this client by its net ID.
  1750. pguiClient->m_lId = (long)pmsg->msg.joined.id;
  1751. // Don't allow the user to select these in client mode . . .
  1752. if (bServer == false)
  1753. {
  1754. pguiClient->m_sActive = FALSE;
  1755. pguiClient->SetActive(FALSE);
  1756. }
  1757. // Activate shadow parameters.
  1758. MakeMoreReadable(pguiClient);
  1759. pguiClient->Compose();
  1760. ms_plbPlayers->AdjustContents();
  1761. ms_plbPlayers->EnsureVisible(pguiClient);
  1762. }
  1763. else
  1764. {
  1765. TRACE("OnJoinedMsg(): ms_plbPlayers->AddString() failed.\n");
  1766. sRes = -1;
  1767. }
  1768. return sRes;
  1769. }
  1770. //////////////////////////////////////////////////////////////////////////////
  1771. //
  1772. // Player has changed info
  1773. //
  1774. //////////////////////////////////////////////////////////////////////////////
  1775. static short OnChangedMsg( // Returns 0 on success.
  1776. CNetClient* pnet, // In: Network interface.
  1777. NetMsg* pmsg) // In: Changed msg
  1778. {
  1779. short sRes = 0; // Assume success.
  1780. ASSERT(pmsg->msg.nothing.ucType == NetMsg::CHANGED);
  1781. TRACE("OnChangedMsg(): Changes are not yet refelected in the gui's!\n");
  1782. return sRes;
  1783. }
  1784. //////////////////////////////////////////////////////////////////////////////
  1785. //
  1786. // Incoming chat message
  1787. //
  1788. //////////////////////////////////////////////////////////////////////////////
  1789. static void OnChatMsg(
  1790. CNetClient* pnet, // In: Network interface.
  1791. NetMsg* pmsg) // In: Chat msg.
  1792. {
  1793. ASSERT(pmsg->msg.nothing.ucType == NetMsg::CHAT);
  1794. AddConsoleMsg(true, "%s", pmsg->msg.chat.acText);
  1795. }
  1796. //////////////////////////////////////////////////////////////////////////////
  1797. //
  1798. // Handles SetupGame message.
  1799. //
  1800. //////////////////////////////////////////////////////////////////////////////
  1801. static void OnSetupGameMsg(
  1802. CNetClient* pnet, // In: Network interface.
  1803. NetMsg* pmsg) // In: Message.
  1804. {
  1805. // Paginate into user displayable single string.
  1806. if (ms_pguiOptions)
  1807. {
  1808. // Check whether this message contains the same realm info as the last such
  1809. // message, if there was one. In other words, is there new realm info?
  1810. bool bNewRealm = false;
  1811. if (ms_bGotSetupMsg)
  1812. {
  1813. if ((pmsg->msg.setupGame.sRealmNum != ms_sSetupRealmNum) ||
  1814. (rspStricmp(pmsg->msg.setupGame.acRealmFile, ms_szSetupRealmFile) != 0))
  1815. {
  1816. bNewRealm = true;
  1817. }
  1818. }
  1819. else
  1820. {
  1821. bNewRealm = true;
  1822. }
  1823. // If it's new, save it for next time
  1824. if (bNewRealm)
  1825. {
  1826. ms_sSetupRealmNum = pmsg->msg.setupGame.sRealmNum;
  1827. strcpy(ms_szSetupRealmFile, pmsg->msg.setupGame.acRealmFile);
  1828. ms_bGotSetupMsg = true;
  1829. }
  1830. // Determine whether the selected realm is available
  1831. bool bAvailable = false;
  1832. #ifdef ENABLE_PLAY_SPECIFIC_REALMS_ONLY
  1833. // If there's only specific realms available, we simply check for
  1834. // the hardwired values.
  1835. if (pmsg->msg.setupGame.sRealmNum > -1)
  1836. {
  1837. if (pmsg->msg.setupGame.sRealmNum == SPECIFIC_MP_REALM_NUM)
  1838. bAvailable = true;
  1839. }
  1840. else
  1841. {
  1842. if (rspStricmp(pmsg->msg.setupGame.acRealmFile, SPECIFIC_MP_REALM_TEXT) == 0)
  1843. bAvailable = true;
  1844. }
  1845. #else
  1846. // If it's a full version of the game, we still need to see if the specified
  1847. // file is available because the host may be trying to use an add-on realm
  1848. // that wasn't on the original CD.
  1849. if (pmsg->msg.setupGame.sRealmNum > -1)
  1850. {
  1851. // Convert realm number to file name
  1852. char szFile[RSP_MAX_PATH];
  1853. if (Play_GetRealmInfo(
  1854. true, // Multiplayer
  1855. pmsg->msg.setupGame.sCoopLevels ? true : false, // Cooperative or Deathmatch levels
  1856. false, // Not gauntlet
  1857. false, // Not new single player Add On levels
  1858. pmsg->msg.setupGame.sRealmNum,
  1859. pmsg->msg.setupGame.sDifficulty,
  1860. szFile,
  1861. sizeof(szFile)) == 0)
  1862. {
  1863. // Check if file is available
  1864. if (CRealm::DoesFileExist(szFile))
  1865. bAvailable = true;
  1866. }
  1867. }
  1868. else
  1869. {
  1870. // acRealmFile, due to a bizzarre twist of events, is never a filename
  1871. // but, rather, the title of the realm. The only way we can get a filename
  1872. // from this title is to scan the NetRealm's in the INI.
  1873. char szRealmFileName[RSP_MAX_PATH];
  1874. if (GetRealmFileFromRealmTitle(
  1875. pmsg->msg.setupGame.sCoopLevels ? true : false, // Cooperative or Deathmatch levels
  1876. pmsg->msg.setupGame.acRealmFile,
  1877. szRealmFileName,
  1878. sizeof(szRealmFileName) ) == 0)
  1879. {
  1880. // Check if file is available
  1881. if (CRealm::DoesFileExist(szRealmFileName))
  1882. bAvailable = true;
  1883. }
  1884. }
  1885. #endif
  1886. // If level is available, display info about it. Otherwise, say it isn't available!
  1887. if (bAvailable)
  1888. {
  1889. char szCoop[256] = "";
  1890. if (pmsg->msg.setupGame.sCoopLevels != 0)
  1891. {
  1892. strcpy(szCoop, "Cooperative ");
  1893. }
  1894. char szRealm[256];
  1895. if (pmsg->msg.setupGame.sRealmNum > -1)
  1896. {
  1897. // Use number.
  1898. sprintf(szRealm, "Level %hd", pmsg->msg.setupGame.sRealmNum);
  1899. }
  1900. else
  1901. {
  1902. // Use string.
  1903. sprintf(szRealm, "\"%s\"", pmsg->msg.setupGame.acRealmFile);
  1904. }
  1905. char szPlayMode[256];
  1906. if (pmsg->msg.setupGame.sCoopMode)
  1907. {
  1908. strcpy(szPlayMode, "Cooperative");
  1909. }
  1910. else
  1911. {
  1912. strcpy(szPlayMode, "Deathmatch");
  1913. }
  1914. char szTimeLimit[256];
  1915. if (pmsg->msg.setupGame.sTimeLimit > 0)
  1916. {
  1917. sprintf(szTimeLimit, "a time limit of %hd", pmsg->msg.setupGame.sTimeLimit);
  1918. }
  1919. else
  1920. {
  1921. sprintf(szTimeLimit, "no time limit");
  1922. }
  1923. char szKillLimit[256];
  1924. if (pmsg->msg.setupGame.sKillLimit > 0)
  1925. {
  1926. sprintf(szKillLimit, "a kill limit of %hd", pmsg->msg.setupGame.sKillLimit);
  1927. }
  1928. else
  1929. {
  1930. sprintf(szKillLimit, "no kill limit");
  1931. }
  1932. ms_pguiOptions->SetText(
  1933. "Host has selected %s%s in %s mode on difficulty %hd with %s and %s.",
  1934. szCoop,
  1935. szRealm,
  1936. szPlayMode,
  1937. pmsg->msg.setupGame.sDifficulty,
  1938. szTimeLimit,
  1939. szKillLimit);
  1940. }
  1941. else
  1942. {
  1943. // Tell the local user that the selected level is not available
  1944. ms_pguiOptions->SetText("Host has selected a level you don't have!");
  1945. // Send a text message to all players (we'd like to send it to just the host,
  1946. // but we don't support that, and maybe it's nice that the other players see
  1947. // it, too). The message complains that the selected level is not available.
  1948. // Note that we only send this when the realm info has changed, so as to avoid
  1949. // sending an endless stream of the same messages over and over.
  1950. if (bNewRealm)
  1951. {
  1952. char szTmp[1024];
  1953. #ifdef ENABLE_PLAY_SPECIFIC_REALMS_ONLY
  1954. sprintf(szTmp, "%s\"%s\" only has the \""SPECIFIC_MP_REALM_TEXT"\" hood -- please pick it!",
  1955. g_pszNetStatusMsgPrefix,
  1956. pnet->GetPlayerName(pnet->GetID()));
  1957. pnet->SendText(szTmp);
  1958. #else
  1959. sprintf(szTmp, "%s\"%s\" does not have the selected hood -- please pick another one!",
  1960. g_pszNetStatusMsgPrefix,
  1961. pnet->GetPlayerName(pnet->GetID()));
  1962. pnet->SendText(szTmp);
  1963. #endif
  1964. }
  1965. }
  1966. // Compose options GUI with word wrap preserving original word wrap status.
  1967. ComposeOptions();
  1968. }
  1969. }
  1970. //////////////////////////////////////////////////////////////////////////////
  1971. //
  1972. // Explain the fact that the procol the user chose is not supported
  1973. //
  1974. //////////////////////////////////////////////////////////////////////////////
  1975. void ProtoNotSupported(void)
  1976. {
  1977. // Check if there's only one protocol or more than one
  1978. if (RSocket::FirstProtocol + 1 == RSocket::NumProtocols)
  1979. {
  1980. // Only one protocol
  1981. rspMsgBox(
  1982. RSP_MB_BUT_OK | RSP_MB_ICN_INFO,
  1983. g_pszAppName,
  1984. g_pszNetOnlyProtocolUnsupported_s,
  1985. RSocket::GetProtoName());
  1986. }
  1987. else
  1988. {
  1989. // More than one protocol
  1990. rspMsgBox(
  1991. RSP_MB_BUT_OK | RSP_MB_ICN_INFO,
  1992. g_pszAppName,
  1993. g_pszNetProtocolUnsupported_s,
  1994. RSocket::GetProtoName());
  1995. }
  1996. }
  1997. //////////////////////////////////////////////////////////////////////////////
  1998. //
  1999. // Do network game dialog
  2000. //
  2001. //////////////////////////////////////////////////////////////////////////////
  2002. extern short DoNetGameDialog( // Returns 0 if successfull, non-zero otherwise.
  2003. CNetClient* pclient, // I/O: Client interface
  2004. bool bBrowse, // In: Whether to browse (true) or connect (false)
  2005. CNetServer* pserver, // I/O: Server interface or NULL if not server
  2006. NetMsg* pmsgOut) // Out: NetMsg::NOTHING or NetMsg::START_GAME
  2007. {
  2008. ASSERT(pclient != NULL);
  2009. short sResult = 0; // Assume success.
  2010. // Under Win95 with DirectX, certain problems have come up due to a
  2011. // combination of our hogging the CPU and DirectX adding to that hogging,
  2012. // and us taking over the screen via DirectX, all of which is a problem
  2013. // when the dialup-networking dialog tries to display itself. In an
  2014. // attempt to alleviate some of this, we go to a lower-level of CPU hogging
  2015. // during the network dialogs, and restore the level afterwards.
  2016. rspSetDoSystemMode(RSP_DOSYSTEM_TOLERATEOS);
  2017. // Init globals
  2018. ms_pguiRoot = NULL;
  2019. ms_pguiOk = NULL;
  2020. ms_pguiCancel = NULL;
  2021. ms_plbPlayers = NULL;
  2022. ms_plbNetConsole = NULL;
  2023. ms_pguiChatText = NULL;
  2024. ms_pguiChatSend = NULL;
  2025. ms_plbLevelBrowse = NULL;
  2026. ms_pmbCoopLevels = NULL;
  2027. ms_pmbCoopMode = NULL;
  2028. ms_pguiRetry = NULL;
  2029. ms_lWatchdogTime = 0;
  2030. ms_bNetBlockingAbort = false;
  2031. // We haven't received a setup msg yet
  2032. ms_bGotSetupMsg = false;
  2033. ClearNetProb();
  2034. // Make sure vars with built in flagging get reflected properly.
  2035. // No time limit is flagged as 0 or negative
  2036. if (g_GameSettings.m_sHostTimeLimit > 0)
  2037. {
  2038. ms_bTimeLimit = true;
  2039. }
  2040. else
  2041. {
  2042. ms_bTimeLimit = false;
  2043. g_GameSettings.m_sHostTimeLimit = -g_GameSettings.m_sHostTimeLimit;
  2044. }
  2045. // No kill limit is flagged as 0 or negative
  2046. if (g_GameSettings.m_sHostKillLimit > 0)
  2047. {
  2048. ms_bKillLimit = true;
  2049. }
  2050. else
  2051. {
  2052. ms_bKillLimit = false;
  2053. g_GameSettings.m_sHostKillLimit = -g_GameSettings.m_sHostKillLimit;
  2054. }
  2055. // Save current mouse cursor level and then force it to be visible
  2056. short sCursorLevel = rspGetMouseCursorShowLevel();
  2057. rspSetMouseCursorShowLevel(1);
  2058. // Clear any events that might be in the queue
  2059. rspClearAllInputEvents();
  2060. // *************************** TEMP
  2061. // bBrowse = true;
  2062. // *************************** TEMP
  2063. // If we're browsing, this loop continues until the user aborts or starts.
  2064. // If we're not browsing, this loop only goes through once.
  2065. bool bAbort = false;
  2066. bool bStart = false;
  2067. do {
  2068. // Call this periodically to let it know we're not locked up
  2069. NetBlockingWatchdog();
  2070. // Default to returning a NetMsg::NOTHING
  2071. pmsgOut->msg.nothing.ucType = NetMsg::NOTHING;
  2072. // If there's a server, start it up
  2073. if (pserver)
  2074. sResult = pserver->Startup(g_GameSettings.m_usServerPort, g_GameSettings.m_szHostName, &NetBlockingCallback);
  2075. if (sResult == 0)
  2076. {
  2077. // Host address
  2078. RSocket::Address addressHost;
  2079. // If we're browsing, do it before the client dialog since it uses a dialog of its own
  2080. if (bBrowse)
  2081. sResult = BrowseForHost(pserver, &addressHost);
  2082. if (sResult == 0)
  2083. {
  2084. // Setup dialog in client or server mode, as appropriate
  2085. sResult = SetupDlg(pserver ? FullPathHD(SERVER_GUI) : FullPathVD(CLIENT_GUI), pserver ? DLG_SERVER : DLG_CLIENT);
  2086. if (sResult == 0)
  2087. {
  2088. // Display "startup" messages
  2089. if (pserver)
  2090. AddConsoleMsg(false, "%s", g_pszServerStat_Startup);
  2091. else
  2092. AddConsoleMsg(false, g_pszClientStat_Startup);
  2093. // Update the dialog so it shows up on the screen. We ignore the
  2094. // return since we aren't ready to deal with it here. This seems
  2095. // okay since the dialog has just shown up, so nobody (except Jeff)
  2096. // could possibly hit a key or mouse button on the first frame.
  2097. UpdateDialog(ms_pguiRoot, true);
  2098. // If we didn't browse for a host, we still need to find one
  2099. if (!bBrowse)
  2100. {
  2101. if (pserver)
  2102. {
  2103. // Try to find the local server
  2104. sResult = BrowseForSelf(pserver, &addressHost);
  2105. }
  2106. else
  2107. {
  2108. // The local client tries to find a remote server
  2109. sResult = FindSpecificSystem(&addressHost);
  2110. }
  2111. }
  2112. if (sResult == 0)
  2113. {
  2114. // This loop is used for when the user hits the "RETRY" button on the client dialog
  2115. bool bRetry = false;
  2116. do {
  2117. // Clear retry flag each time so we don't accidentally get caught in a loop
  2118. bRetry = false;
  2119. // Startup client
  2120. pclient->Startup(&NetBlockingCallback);
  2121. // Start the (asynchronous) process of joining the specified host. We
  2122. // specifically check for the "protocol not supported" error and handle
  2123. // that as a special case, because users are likely to screw that up.
  2124. // Any other errors will be discovered via our normal GetMsg() loop.
  2125. sResult = pclient->StartJoinProcess(
  2126. &addressHost,
  2127. g_GameSettings.m_szPlayerName,
  2128. g_GameSettings.m_sPlayerColorIndex,
  2129. 0,
  2130. g_GameSettings.m_sNetBandwidth);
  2131. if (sResult != RSocket::errNotSupported)
  2132. {
  2133. //------------------------------------------------------------------------------
  2134. // This may appear to be a rather bizarre loop, and maybe it is, but it seemed
  2135. // like a really good idea at the time...
  2136. //
  2137. // It handles three primary tasks. First, if this machine is a server, it does
  2138. // all the server stuff. Then, it does the client stuff (remember that a server
  2139. // is always a client, but a client is not necessarily a server). Finally, it
  2140. // does the dialog and user-input stuff, which again is slightly different
  2141. // depending on whether it's a client or a server.
  2142. //
  2143. // The alternative approach was to have separate functions to handle the client
  2144. // and server ends. It was originally that way, but there was so much
  2145. // similarity between the two that it seemed better to merge them, thereby
  2146. // avoiding having to maintain two very similar sets of code.
  2147. //
  2148. // A key point about this loop is that you should NEVER use a 'break' to get
  2149. // out of the loop. In fact, you should rarely exit the loop in any kind of
  2150. // direct manner. Instead, you should always try to wait until the client
  2151. // and server (if any) have been allowed to "gracefully" finish whatever it
  2152. // is they need to get done.
  2153. //
  2154. // Note that if the user clicks the "ABORT" button in the dialog, we don't
  2155. // immediately end the loop, either. We simply set the flags and hope the
  2156. // loop will end itself soon enough. If, after a certain amount of time, the
  2157. // loop hasn't ended, the user can click "ABORT" again to end, and this time
  2158. // we end immediately. At some point, this should perhaps be replaced by a
  2159. // timer which automatically ends the loop after a certain amount of time.
  2160. //------------------------------------------------------------------------------
  2161. bool bEndClientCleanly = false;
  2162. bool bEndServerCleanly = false;
  2163. bool bServerDone = pserver ? false : true;
  2164. bool bClientDone = false;
  2165. bool bUserAbortNow = false;
  2166. long lCancelDelay;
  2167. DLG_ACTION action;
  2168. NetMsg msg;
  2169. while (!(bClientDone && bServerDone && (bAbort || bStart || bRetry)) && !bUserAbortNow)
  2170. {
  2171. // Always and forever
  2172. UpdateSystem();
  2173. // Call this periodically to let it know we're not locked up
  2174. NetBlockingWatchdog();
  2175. //------------------------------------------------------------------------------
  2176. // Do dialog and any other user-input stuff
  2177. //------------------------------------------------------------------------------
  2178. // Update GUI
  2179. action = UpdateDialog(ms_pguiRoot, true);
  2180. // If user wants to quit, simulate a cancel. Note that this is NOT an event
  2181. // but a flag, so once it starts, we'll continue to get CANCEL actions each
  2182. // time we get to this point. This is exactly what we want, since this type
  2183. // of quit indicates that the user doesn't want to wait around to quit.
  2184. if (rspGetQuitStatus() || NetBlockingWasAborted())
  2185. action = DLG_CANCEL;
  2186. // Handle action
  2187. switch (action)
  2188. {
  2189. // Nothing to do
  2190. case DLG_NOTHING:
  2191. break;
  2192. // OK was pressed. This only exists on server dialog! Note that we don't
  2193. // allow OK to be used if either it or Cancel were already used.
  2194. case DLG_OK:
  2195. // Must be server. . .
  2196. if (pserver)
  2197. {
  2198. // Make sure neither OK nor Cancel were pressed, and that we have at least 1 client
  2199. if (!bStart && !bAbort && pserver->GetNumberOfClients())
  2200. {
  2201. // Download GUI vals.
  2202. DownloadLinks(ms_aglServerLinkage, ms_pguiRoot);
  2203. // Feedback
  2204. PlaySample(g_smidMenuItemSelect, SampleMaster::UserFeedBack);
  2205. AddConsoleMsg(false, "%s", g_pszNetStat_Starting);
  2206. // Get pointer to game selection GUI
  2207. RGuiItem* pguiSel = RSP_SAFE_GUI_REF(ms_plbLevelBrowse, GetSel());
  2208. // Start game using specified settings
  2209. pserver->StartGame(
  2210. pclient->GetID(),
  2211. #ifdef ENABLE_PLAY_SPECIFIC_REALMS_ONLY
  2212. -1,
  2213. SPECIFIC_MP_REALM_FILE,
  2214. #else
  2215. RSP_SAFE_GUI_REF(pguiSel, m_lId),
  2216. "",
  2217. #endif
  2218. g_GameSettings.m_sDifficulty,
  2219. g_GameSettings.m_sHostRejuvenate,
  2220. ms_bTimeLimit ? g_GameSettings.m_sHostTimeLimit : -g_GameSettings.m_sHostTimeLimit,
  2221. ms_bKillLimit ? g_GameSettings.m_sHostKillLimit : -g_GameSettings.m_sHostKillLimit,
  2222. ms_bCoopLevels ? 1 : 0,
  2223. ms_bCoopMode ? 1 : 0,
  2224. g_GameSettings.m_sNetTimePerFrame,
  2225. g_GameSettings.m_sNetMaxFrameLag);
  2226. // End server cleanly (client will end when it gets start message)
  2227. bEndServerCleanly = true;
  2228. }
  2229. }
  2230. break;
  2231. // Cancel was pressed. Note that in server mode, we allow cancel to be
  2232. // pressed even after OK was pressed. We merely send another message
  2233. // to the clients saying to abort the game that was already started.
  2234. case DLG_CANCEL:
  2235. // If cancel was already pressed, we ignore additional cancel's until
  2236. // the timer has expired, at which point they can hit cancel again.
  2237. // The idea is to give the cancel process time to finish cleanly.
  2238. if (bAbort)
  2239. {
  2240. if ((rspGetMilliseconds() - lCancelDelay) > CANCEL_DELAY_TIME)
  2241. {
  2242. // Feedback sound
  2243. PlaySample(g_smidMenuItemSelect, SampleMaster::UserFeedBack);
  2244. // User has aborted, exit immediately
  2245. bUserAbortNow = true;
  2246. }
  2247. }
  2248. else
  2249. {
  2250. // Pressed cancel, so set flag and init delay timer
  2251. bAbort = true;
  2252. lCancelDelay = rspGetMilliseconds();
  2253. // Feedback
  2254. AddConsoleMsg(false, "%s", g_pszNetStat_Aborting);
  2255. PlaySample(g_smidMenuItemSelect, SampleMaster::UserFeedBack);
  2256. if (pserver)
  2257. {
  2258. // Download GUI vals anyways for consistency.
  2259. DownloadLinks(ms_aglServerLinkage, ms_pguiRoot);
  2260. // Tell players to abort game
  2261. pserver->AbortGame(NetMsg::UserAbortedGame);
  2262. }
  2263. else
  2264. {
  2265. // Drop out of game
  2266. pclient->Drop();
  2267. // Start with a clean slate (no RETRY button because user selected ABORT)
  2268. CleanClientDlg(pclient, false);
  2269. }
  2270. // End server and client cleanly
  2271. bEndServerCleanly = true;
  2272. bEndClientCleanly = true;
  2273. }
  2274. break;
  2275. // Chat was pressed. We don't allow this if OK or Cancel were pressed.
  2276. case DLG_CHAT:
  2277. // Make sure neither Ok nor Cancel were pressed
  2278. if (!bStart && !bAbort)
  2279. {
  2280. // Make sure text field exists
  2281. if (ms_pguiChatText != NULL)
  2282. {
  2283. // See if there's any text
  2284. if (ms_pguiChatText->m_szText[0] != '\0')
  2285. {
  2286. // Sent chat
  2287. pclient->SendChat(ms_pguiChatText->m_szText);
  2288. // Clear text
  2289. ms_pguiChatText->SetText("");
  2290. ms_pguiChatText->Compose();
  2291. }
  2292. }
  2293. }
  2294. break;
  2295. // Disconnect player was pressed. We don't allow this if OK or Cancel were pressed.
  2296. case DLG_DISCONNECT_PLAYER:
  2297. // Make sure neither Ok nor Cancel were pressed
  2298. if (!bStart && !bAbort)
  2299. {
  2300. // If there's a selection . . .
  2301. RGuiItem* pguiPlayerSel = ms_plbPlayers->GetSel();
  2302. if (pguiPlayerSel)
  2303. {
  2304. Net::ID id = (UCHAR)pguiPlayerSel->m_lId;
  2305. // Don't be a moron -- don't drop yourself
  2306. if (id == pclient->GetID())
  2307. {
  2308. // Tell user he can't drop himself
  2309. AddConsoleMsg(false, "%s", g_pszServerStat_CantDropSelf);
  2310. }
  2311. else
  2312. {
  2313. // Drop player (must get his name BEFORE he is dropped!)
  2314. AddConsoleMsg(false, g_pszNetStat_AttemptToDrop_s, pserver->GetPlayerName(id));
  2315. pserver->DropClient(id);
  2316. }
  2317. }
  2318. }
  2319. break;
  2320. // Send an options update. We don't allow this if OK or Cancel were pressed.
  2321. case DLG_OPTIONS_UPDATED:
  2322. if (pserver && !bStart && !bAbort)
  2323. {
  2324. // Get current settings into their variables.
  2325. DownloadLinks(ms_aglServerLinkage, ms_pguiRoot);
  2326. // Get pointer to game selection GUI
  2327. RGuiItem* pguiSel = RSP_SAFE_GUI_REF(ms_plbLevelBrowse, GetSel());
  2328. // Setup game
  2329. pserver->SetupGame(
  2330. #ifdef ENABLE_PLAY_SPECIFIC_REALMS_ONLY
  2331. -1,
  2332. SPECIFIC_MP_REALM_TEXT,
  2333. #else
  2334. pguiSel ? -1 : 0,
  2335. pguiSel ? pguiSel->m_szText : "",
  2336. #endif
  2337. g_GameSettings.m_sDifficulty,
  2338. g_GameSettings.m_sHostRejuvenate,
  2339. ms_bTimeLimit ? g_GameSettings.m_sHostTimeLimit : -g_GameSettings.m_sHostTimeLimit,
  2340. ms_bKillLimit ? g_GameSettings.m_sHostKillLimit : -g_GameSettings.m_sHostKillLimit,
  2341. ms_bCoopLevels ? 1 : 0,
  2342. ms_bCoopMode ? 1 : 0);
  2343. }
  2344. break;
  2345. case DLG_RETRY:
  2346. if (!bStart && !bAbort)
  2347. {
  2348. // Clean the slate (without RETRY since they just hit it and may not need it again)
  2349. CleanClientDlg(pclient, false);
  2350. // Feedback
  2351. PlaySample(g_smidMenuItemSelect, SampleMaster::UserFeedBack);
  2352. AddConsoleMsg(false, "%s", g_pszClientStat_Retrying);
  2353. AddConsoleMsg(false, g_pszClientStat_Startup);
  2354. // End server and client cleanly
  2355. bEndServerCleanly = true;
  2356. bEndClientCleanly = true;
  2357. // Set retry flag
  2358. bRetry = true;
  2359. }
  2360. break;
  2361. default:
  2362. TRACE("DoNetGameDialog(): Unknown dialog action!\n");
  2363. break;
  2364. }
  2365. //------------------------------------------------------------------------------
  2366. // Do server stuff.
  2367. //
  2368. // Note that bServerDone should rarely be set to true directly. Instead, set
  2369. // bEndServerCleanly to true, which will try to end the server cleanly, and will
  2370. // eventually result in bServerDone being set to true.
  2371. //
  2372. // It's okay to ignore incoming messages when trying to end because they will
  2373. // still be received and buffered, they just won't be processed here. Instead,
  2374. // they'll be processed in the game loop. Of course, if the game never actually
  2375. // gets started they won't ever be processed, but then it doesn't matter.
  2376. //------------------------------------------------------------------------------
  2377. if (pserver && !bServerDone)
  2378. {
  2379. pserver->Update();
  2380. // Check if we're trying to end
  2381. if (bEndServerCleanly)
  2382. {
  2383. // If there isn't anything more to send, then we're done
  2384. if (!pserver->IsMoreToSend())
  2385. bServerDone = true;
  2386. }
  2387. else
  2388. {
  2389. // Process messages
  2390. pserver->GetMsg(&msg);
  2391. switch(msg.msg.nothing.ucType)
  2392. {
  2393. case NetMsg::ERR:
  2394. if (msg.ucSenderID == Net::InvalidID)
  2395. {
  2396. // A server occurred occurred
  2397. AddConsoleMsg(false, NetErrorText(&msg));
  2398. // Play a sound of badness
  2399. PlaySample(g_smidEmptyWeapon, SampleMaster::UserFeedBack);
  2400. switch (msg.msg.nothing.ucType)
  2401. {
  2402. // These messages are NOT so horrible and don't
  2403. // require us to clean up and be done.
  2404. // They are merely notifications.
  2405. case NetMsg::ServerVersionMismatchError:
  2406. case NetMsg::ServerPlatformMismatchError:
  2407. break;
  2408. // These messages are so horrible and don't
  2409. // require us to clean up and be done.
  2410. default:
  2411. // End server and client cleanly
  2412. bEndServerCleanly = true;
  2413. bEndClientCleanly = true;
  2414. break;
  2415. }
  2416. }
  2417. else if (msg.ucSenderID == pclient->GetID())
  2418. {
  2419. // The error occurred on the local client. We registered the
  2420. // local client with the server, so it will have aborted the game.
  2421. AddConsoleMsg(false, NetErrorText(&msg));
  2422. // Play a sound of badness
  2423. PlaySample(g_smidEmptyWeapon, SampleMaster::UserFeedBack);
  2424. // End server and client cleanly
  2425. bEndServerCleanly = true;
  2426. bEndClientCleanly = true;
  2427. }
  2428. else
  2429. {
  2430. // The error occurred on a remote client, so the server will
  2431. // have dropped that client.
  2432. AddConsoleMsg(false, g_pszServerStat_PlayerErr);
  2433. }
  2434. break;
  2435. default:
  2436. break;
  2437. }
  2438. }
  2439. }
  2440. //------------------------------------------------------------------------------
  2441. // Do client stuff
  2442. //
  2443. // Note that bClientDone should rarely be set to true directly. Instead, set
  2444. // bEndClientCleanly to true, which will try to end the client cleanly, and will
  2445. // eventually result in bClientDone being set to true.
  2446. //------------------------------------------------------------------------------
  2447. if (!bClientDone)
  2448. {
  2449. pclient->Update();
  2450. // Check if we're trying to end
  2451. if (bEndClientCleanly)
  2452. {
  2453. // If there isn't anything more to send, then we're done
  2454. if (!pclient->IsMoreToSend())
  2455. bClientDone = true;
  2456. }
  2457. else
  2458. {
  2459. // Assume no problems
  2460. short sProblem = 0;
  2461. // Process messages from server
  2462. pclient->GetMsg(&msg);
  2463. switch(msg.msg.nothing.ucType)
  2464. {
  2465. case NetMsg::NOTHING:
  2466. break;
  2467. case NetMsg::ERR:
  2468. AddConsoleMsg(false, NetErrorText(&msg));
  2469. // Attempt to drop nicely
  2470. pclient->Drop();
  2471. // End server and client cleanly
  2472. bEndServerCleanly = true;
  2473. bEndClientCleanly = true;
  2474. // Play a sound of badness and clean the slate (and show RETRY button)
  2475. PlaySample(g_smidEmptyWeapon, SampleMaster::UserFeedBack);
  2476. CleanClientDlg(pclient, true);
  2477. break;
  2478. case NetMsg::STAT:
  2479. switch (msg.msg.stat.status)
  2480. {
  2481. case NetMsg::Opened:
  2482. AddConsoleMsg(false, "%s", g_pszClientStat_Opened);
  2483. break;
  2484. case NetMsg::Connected:
  2485. AddConsoleMsg(false, "%s", g_pszClientStat_Connected);
  2486. break;
  2487. case NetMsg::LoginAccepted:
  2488. // If we are the server's local client, register ourself so
  2489. // that it realizes that if there's an error on our connection,
  2490. // it will abort the whole game.
  2491. if (pserver)
  2492. pserver->SetLocalClientID(pclient->GetID());
  2493. AddConsoleMsg(false, g_pszClientStat_LoginAccepted_hd, (short)pclient->GetID());
  2494. break;
  2495. case NetMsg::JoinAccepted:
  2496. AddConsoleMsg(false, "%s", g_pszClientStat_JoinAccepted);
  2497. break;
  2498. default:
  2499. break;
  2500. }
  2501. break;
  2502. case NetMsg::JOINED:
  2503. if (OnJoinedMsg(pclient, &msg, pserver ? true : false) != 0)
  2504. {
  2505. // A program error occurred, so tell them something vague
  2506. AddConsoleMsg(false, g_pszNetStat_ProgramError);
  2507. // Attempt to drop nicely
  2508. pclient->Drop();
  2509. // End server and client cleanly
  2510. bEndServerCleanly = true;
  2511. bEndClientCleanly = true;
  2512. // Play a sound of badness and clean the slate (and show RETRY button)
  2513. PlaySample(g_smidEmptyWeapon, SampleMaster::UserFeedBack);
  2514. CleanClientDlg(pclient, true);
  2515. }
  2516. break;
  2517. case NetMsg::DROPPED:
  2518. OnDroppedMsg(pclient, &msg);
  2519. // If we were dropped, end immediately
  2520. if (msg.msg.dropped.id == Net::InvalidID)
  2521. {
  2522. // Play a sound of badness and clean the slate (and show RETRY button)
  2523. PlaySample(g_smidEmptyWeapon, SampleMaster::UserFeedBack);
  2524. CleanClientDlg(pclient, true);
  2525. bClientDone = true;
  2526. }
  2527. break;
  2528. case NetMsg::CHAT:
  2529. OnChatMsg(pclient, &msg);
  2530. break;
  2531. case NetMsg::SETUP_GAME:
  2532. OnSetupGameMsg(pclient, &msg);
  2533. break;
  2534. case NetMsg::START_GAME:
  2535. // Return this message to caller
  2536. *pmsgOut = msg;
  2537. // Game has been started
  2538. bStart = true;
  2539. // End immediately (no need to wait)
  2540. bClientDone = true;
  2541. break;
  2542. case NetMsg::ABORT_GAME:
  2543. AddConsoleMsg(false, "%s", g_pszClientStat_ServerAborted);
  2544. // Play a sound of badness and clean the slate (and show RETRY button)
  2545. PlaySample(g_smidEmptyWeapon, SampleMaster::UserFeedBack);
  2546. CleanClientDlg(pclient, true);
  2547. // End immediately (no need to wait)
  2548. bClientDone = true;
  2549. break;
  2550. default:
  2551. AddConsoleMsg(false, "%s", g_pszNetStat_UnhandledMsg);
  2552. break;
  2553. }
  2554. }
  2555. }
  2556. } // while()
  2557. }
  2558. else
  2559. {
  2560. // Display msg box that says the protocol is not supported
  2561. ProtoNotSupported();
  2562. }
  2563. // If we're not starting, shutdown client
  2564. if (!bStart)
  2565. pclient->Shutdown();
  2566. } while (bRetry && !sResult);
  2567. }
  2568. // Get rid of dialog
  2569. DlgBeGone();
  2570. // Call this periodically to let it know we're not locked up
  2571. NetBlockingWatchdog();
  2572. }
  2573. else
  2574. {
  2575. TRACE("DoNetGameDialog(): Failed to setup dialog.\n");
  2576. }
  2577. }
  2578. // If not starting, shutdown server
  2579. if (!bStart && pserver)
  2580. pserver->Shutdown();
  2581. }
  2582. else
  2583. {
  2584. // Handle the "protocol not supported" error as a special case because users
  2585. // are likely to screw that up.
  2586. if (sResult == RSocket::errNotSupported)
  2587. {
  2588. // Display msg box that says the protocol is not supported
  2589. ProtoNotSupported();
  2590. }
  2591. }
  2592. } while (!sResult && (bBrowse && !bAbort && !bStart));
  2593. // Call this one last time on the way out
  2594. NetBlockingWatchdog();
  2595. // Make sure vars with built in flagging get reflected properly.
  2596. if (ms_bTimeLimit == false)
  2597. {
  2598. // Flag no time limit as a negative or zero.
  2599. g_GameSettings.m_sHostTimeLimit = -g_GameSettings.m_sHostTimeLimit;
  2600. }
  2601. if (ms_bKillLimit == false)
  2602. {
  2603. // Flag no kill limit as a negative or zero.
  2604. g_GameSettings.m_sHostKillLimit = -g_GameSettings.m_sHostKillLimit;
  2605. }
  2606. // Clear any events that might be in the queue
  2607. rspClearAllInputEvents();
  2608. // Restore mouse cursor show level
  2609. rspSetMouseCursorShowLevel(sCursorLevel);
  2610. // Go back to normal CPU mode
  2611. #if defined(_DEBUG)
  2612. // Wake CPU.
  2613. rspSetDoSystemMode(RSP_DOSYSTEM_TOLERATEOS);
  2614. #else
  2615. // Return CPU to us.
  2616. rspSetDoSystemMode(RSP_DOSYSTEM_HOGCPU);
  2617. #endif
  2618. return sResult;
  2619. }
  2620. //////////////////////////////////////////////////////////////////////////////
  2621. //
  2622. // Browse for a host.
  2623. //
  2624. // This is normally only used in client-only mode, but it is easier to test
  2625. // and debug if we can also use it in client-server mode. That's why this
  2626. // takes a pserver pointer.
  2627. //
  2628. //////////////////////////////////////////////////////////////////////////////
  2629. static short BrowseForHost(
  2630. CNetServer* pserver, // I/O: Server interface or NULL if none
  2631. RSocket::Address* paddress) // Out: Address returned here (if successfull)
  2632. {
  2633. short sResult = 0;
  2634. // Start with empty list of hosts
  2635. CNetBrowse::Hosts hostsAll;
  2636. CNetBrowse::Hosts hostsAdded;
  2637. CNetBrowse::Hosts hostsDropped;
  2638. NetBlockingWatchdog();
  2639. // Create browser and start it up
  2640. CNetBrowse browse;
  2641. sResult = browse.Startup(g_GameSettings.m_usServerPort, &NetBlockingCallback);
  2642. if (sResult == 0)
  2643. {
  2644. sResult = SetupDlg(FullPathVD(BROWSER_GUI), DLG_BROWSER);
  2645. if (sResult == 0)
  2646. {
  2647. // Since we don't let RProcessGUI draw cleaned up the screen, we should.
  2648. rspUpdateDisplay();
  2649. DLG_ACTION action = DLG_NOTHING;
  2650. // Loop until error, user abort, or user choice . . .
  2651. while ((sResult == 0) && action != DLG_OK && action != DLG_CANCEL)
  2652. {
  2653. // Do default processing.
  2654. UpdateSystem();
  2655. // Update watchdog timer for net blocking.
  2656. NetBlockingWatchdog();
  2657. // Update server interface, if available.
  2658. if (pserver)
  2659. pserver->Update();
  2660. // Check for new or lost host games.
  2661. browse.Update(&hostsAll, &hostsAdded, &hostsDropped);
  2662. // Update listbox via added and dropped hosts lists.
  2663. sResult = UpdateListBox(ms_plbHostBrowse, &hostsAll, &hostsAdded, &hostsDropped);
  2664. if (sResult == 0)
  2665. {
  2666. // Update the dialog (user input and GUI output).
  2667. action = UpdateDialog(ms_pguiRoot, true);
  2668. }
  2669. // If quitting . . .
  2670. if (rspGetQuitStatus() != FALSE)
  2671. sResult = 1;
  2672. }
  2673. // If no error . . .
  2674. if (sResult == 0)
  2675. {
  2676. switch (action)
  2677. {
  2678. case DLG_OK:
  2679. {
  2680. // Play feedback sound
  2681. PlaySample(g_smidMenuItemSelect, SampleMaster::UserFeedBack);
  2682. // Get selection . . .
  2683. RGuiItem* pguiSel = ms_plbHostBrowse->GetSel();
  2684. // If there's no selection . . .
  2685. if (pguiSel == NULL)
  2686. {
  2687. // Take the first.
  2688. pguiSel = ms_plbHostBrowse->GetFirst();
  2689. }
  2690. // If tehre's a selection . . .
  2691. if (pguiSel)
  2692. {
  2693. // Get corresponding host.
  2694. CNetBrowse::CHost* phost = (CNetBrowse::CHost*)(pguiSel->m_ulUserData);
  2695. ASSERT(phost);
  2696. // Get host address.
  2697. *paddress = phost->m_address;
  2698. // Success.
  2699. }
  2700. else
  2701. {
  2702. TRACE("BrowseForHost(): No host selected.\n");
  2703. sResult = -1;
  2704. }
  2705. break;
  2706. }
  2707. case DLG_CANCEL:
  2708. // Play feedback sound
  2709. PlaySample(g_smidMenuItemSelect, SampleMaster::UserFeedBack);
  2710. // Cancelled.
  2711. sResult = 1;
  2712. break;
  2713. default:
  2714. TRACE("BrowseForHost(): Unknown action.\n");
  2715. sResult = 1;
  2716. break;
  2717. }
  2718. }
  2719. // Done with dialog.
  2720. DlgBeGone();
  2721. }
  2722. // Stop browsing
  2723. NetBlockingWatchdog();
  2724. browse.Shutdown();
  2725. }
  2726. else
  2727. {
  2728. // Handle the "protocol not supported" error as a special case because users
  2729. // are likely to screw that up.
  2730. if (sResult == RSocket::errNotSupported)
  2731. {
  2732. // Display msg box that says the protocol is not supported
  2733. ProtoNotSupported();
  2734. }
  2735. }
  2736. return sResult;
  2737. }
  2738. //////////////////////////////////////////////////////////////////////////////
  2739. //
  2740. // Try to connect to specified host
  2741. //
  2742. //////////////////////////////////////////////////////////////////////////////
  2743. static short FindSpecificSystem(
  2744. RSocket::Address* paddress) // Out: Address returned here (if successfull)
  2745. {
  2746. short sResult = 0;
  2747. // Lookup the specified host (by name or dotted address) and port
  2748. sResult = CNetBrowse::LookupHost(
  2749. g_GameSettings.m_szServerName,
  2750. g_GameSettings.m_usServerPort,
  2751. paddress);
  2752. if (sResult == 0)
  2753. {
  2754. // Success!
  2755. }
  2756. else
  2757. {
  2758. rspMsgBox(
  2759. RSP_MB_BUT_OK | RSP_MB_ICN_INFO,
  2760. g_pszAppName,
  2761. "The specified computer ('%s') could not be found. Please verify that the specified name or address is correct.",
  2762. g_GameSettings.m_szServerName);
  2763. }
  2764. return sResult;
  2765. }
  2766. //////////////////////////////////////////////////////////////////////////////
  2767. //
  2768. // Try to find the local server. This is necessary because there isn't any
  2769. // foolproof method by which to simply lookup the local computer's address.
  2770. // Instead, we browse for ourselves, just like any other client would.
  2771. //
  2772. //////////////////////////////////////////////////////////////////////////////
  2773. static short BrowseForSelf(
  2774. CNetServer* pserver, // I/O: Server interface
  2775. RSocket::Address* paddress) // Out: Address returned here (if successfull)
  2776. {
  2777. ASSERT(pserver);
  2778. short sResult = 0;
  2779. // Start with empty list of hosts
  2780. CNetBrowse::Hosts hostsAll;
  2781. CNetBrowse::Hosts hostsAdded;
  2782. CNetBrowse::Hosts hostsDropped;
  2783. NetBlockingWatchdog();
  2784. // Create browser and start it up
  2785. bool bFoundSelf = false;
  2786. CNetBrowse browse;
  2787. sResult = browse.Startup(g_GameSettings.m_usServerPort, &NetBlockingCallback);
  2788. if (sResult == 0)
  2789. {
  2790. // Wait for our own broadcast
  2791. long lTime = rspGetMilliseconds() + Net::BroadcastDropTime;
  2792. while (!bFoundSelf && (rspGetMilliseconds() < lTime))
  2793. {
  2794. UpdateSystem();
  2795. NetBlockingWatchdog();
  2796. pserver->Update();
  2797. browse.Update(&hostsAll, &hostsAdded, &hostsDropped);
  2798. // Try to find ourself in the list
  2799. for (CNetBrowse::Hosts::Pointer p = hostsAll.GetHead(); p; p = hostsAll.GetNext(p))
  2800. {
  2801. CNetBrowse::CHost* phost;
  2802. phost = &hostsAll.GetData(p);
  2803. if ((strcmp(phost->m_acName, pserver->GetHostName()) == 0) && (phost->m_lMagic == pserver->GetHostMagic()))
  2804. {
  2805. // Return this host's address
  2806. *paddress = phost->m_address;
  2807. bFoundSelf = true;
  2808. break;
  2809. }
  2810. }
  2811. }
  2812. // If we didn't find ourself, set the error flag
  2813. if (!bFoundSelf)
  2814. {
  2815. sResult = -1;
  2816. TRACE("BrowseForSelf(): Couldn't find myself!\n");
  2817. }
  2818. // Stop browsing
  2819. NetBlockingWatchdog();
  2820. browse.Shutdown();
  2821. }
  2822. // If this failed, put up a msgbox
  2823. if (sResult != 0)
  2824. {
  2825. rspMsgBox(
  2826. RSP_MB_BUT_OK | RSP_MB_ICN_INFO,
  2827. g_pszAppName,
  2828. "This computer's network address could not be determined. If this is a multi-homed host, you may need to disable all but one address.");
  2829. }
  2830. return sResult;
  2831. }
  2832. //////////////////////////////////////////////////////////////////////////////
  2833. //
  2834. // Net blocking watchdog. Call this periodically to let the watchdog know
  2835. // that the program hasn't locked up.
  2836. //
  2837. //////////////////////////////////////////////////////////////////////////////
  2838. extern void NetBlockingWatchdog(void)
  2839. {
  2840. // Reset timer to current time
  2841. ms_lWatchdogTime = rspGetMilliseconds();
  2842. // If net blocking had expired, then clear it so the net prob gui goes
  2843. // away (since we got here, we're obviously not blocking anymore)
  2844. if (m_bNetWatchdogExpired)
  2845. {
  2846. // Clear flag
  2847. m_bNetWatchdogExpired = false;
  2848. // Erase net prob gui
  2849. RGuiItem* ptxt = GetNetProbGUI();
  2850. if (ptxt)
  2851. {
  2852. // This will PROBABLY work, but it does assume that something else
  2853. // was drawn to the buffer after the last time we drew the net gui.
  2854. // If nothing was drawn to the buffer, the gui will still be there!
  2855. rspUpdateDisplay(ptxt->m_sX, ptxt->m_sY, ptxt->m_im.m_sWidth, ptxt->m_im.m_sHeight);
  2856. }
  2857. }
  2858. }
  2859. //////////////////////////////////////////////////////////////////////////////
  2860. //
  2861. // Check if a blocked network operation was aborted.
  2862. //
  2863. //////////////////////////////////////////////////////////////////////////////
  2864. extern bool NetBlockingWasAborted(void)
  2865. {
  2866. return ms_bNetBlockingAbort;
  2867. }
  2868. //////////////////////////////////////////////////////////////////////////////
  2869. //
  2870. // Net blocking callback
  2871. //
  2872. //////////////////////////////////////////////////////////////////////////////
  2873. static short NetBlockingCallback(void) // Returns 0 to continue normally, 1 to abort
  2874. {
  2875. // We only need to grab this ptr once (it is guaranteed not to move).
  2876. // Once we have this pointer to the Key status array, we can use it to
  2877. // check on a key's status. The important thing would be NOT to clear
  2878. // any key's status so we don't affect the input module (input.cpp) or
  2879. // the play loop (play.cpp:PlayRealm()).
  2880. static U8* pau8KeyStatus = rspGetKeyStatusArray();
  2881. // Assume we won't abort
  2882. short sAbort = 0;
  2883. // It's always a good idea to do this
  2884. UpdateSystem();
  2885. // System-specific quit always aborts immediately
  2886. if (rspGetQuitStatus())
  2887. sAbort = 1;
  2888. // If the watchdog hasn't already expired, then check it. If it has
  2889. // already expired, then we need to see if the user hit the abort key.
  2890. if (!m_bNetWatchdogExpired)
  2891. {
  2892. // If the watchdog function hasn't been called in a too-long time, then we
  2893. // assume the network stuff is blocking (although it could be that someone
  2894. // forgot to call the watchdog function) and we display the net problem gui.
  2895. if ((rspGetMilliseconds() - ms_lWatchdogTime) > g_GameSettings.m_lNetMaxBlockingTime)
  2896. {
  2897. // Set flag to indicate that there is a problem. This servers two
  2898. // purposes: (1) it let's us know that the net prob gui is being
  2899. // displayed and we should check for the abort key, and (2) it let's
  2900. // other parts of the program determine whether or not to draw the
  2901. // net prob gui on top of whatever else is being drawn to the screen.
  2902. m_bNetWatchdogExpired = true;
  2903. }
  2904. }
  2905. // If watchdog is expired, draw the net prob gui and check for an abort key press
  2906. if (m_bNetWatchdogExpired)
  2907. {
  2908. // Display net problem gui
  2909. RGuiItem* ptxt = GetNetProbGUI();
  2910. if (ptxt)
  2911. {
  2912. // Set to display general error and abort instructions
  2913. ptxt->SetText("%s", g_pszNetProb_General);
  2914. ptxt->Compose();
  2915. // Center it so it gets erased by whatever menu or dialog is being displayed
  2916. ptxt->Move(
  2917. (g_pimScreenBuf->m_sWidth / 2) - (ptxt->m_im.m_sWidth / 2),
  2918. (g_pimScreenBuf->m_sHeight / 2) - (ptxt->m_im.m_sHeight / 2));
  2919. // Create temporary image of what's currently in the buffer
  2920. bool bGotImage = false;
  2921. RImage image;
  2922. if (image.CreateImage(ptxt->m_im.m_sWidth, ptxt->m_im.m_sHeight, ptxt->m_im.m_type) == 0)
  2923. {
  2924. rspLockBuffer();
  2925. rspBlit(g_pimScreenBuf, &image, ptxt->m_sX, ptxt->m_sY, 0, 0, image.m_sWidth, image.m_sHeight);
  2926. bGotImage = true;
  2927. rspUnlockBuffer();
  2928. }
  2929. // Note that the GUI locks the buffer, if appropriate.
  2930. ptxt->Draw(g_pimScreenBuf);
  2931. rspUpdateDisplay(ptxt->m_sX, ptxt->m_sY, ptxt->m_im.m_sWidth, ptxt->m_im.m_sHeight);
  2932. if (bGotImage)
  2933. {
  2934. rspLockBuffer();
  2935. rspBlit(&image, g_pimScreenBuf, 0, 0, ptxt->m_sX, ptxt->m_sY, image.m_sWidth, image.m_sHeight);
  2936. rspUnlockBuffer();
  2937. }
  2938. }
  2939. // Check for the abort key
  2940. if (pau8KeyStatus[NET_PROB_GUI_ABORT_SK_KEY])
  2941. sAbort = 1;
  2942. }
  2943. // If aborting, set our own flag, too
  2944. if (sAbort)
  2945. {
  2946. // Set flag
  2947. ms_bNetBlockingAbort = true;
  2948. }
  2949. return sAbort;
  2950. }
  2951. //////////////////////////////////////////////////////////////////////////////
  2952. //
  2953. // Initialize the net problems GUI.
  2954. // Note that this is nearly instantaneous (no file access) if it has already
  2955. // been called. That is, calling this twice in a row is fine. Just make
  2956. // sure you call KillNetProbGUI() when done (although, even that is not
  2957. // essential).
  2958. //
  2959. //////////////////////////////////////////////////////////////////////////////
  2960. extern short InitNetProbGUI(void)
  2961. {
  2962. short sRes = 0; // Assume success.
  2963. KillNetProbGUI();
  2964. sRes = rspGetResource(&g_resmgrShell, NET_PROB_GUI_FILE, &ms_ptxtNetProb);
  2965. if (sRes == 0)
  2966. {
  2967. // Set some intial stuff.
  2968. ms_ptxtNetProb->m_sTextEffects = RGuiItem::Shadow;
  2969. ms_ptxtNetProb->m_u32TextShadowColor = NET_PROB_TEXT_SHADOW_COLOR_INDEX;
  2970. ms_ptxtNetProb->m_sX = NET_PROB_GUI_X;
  2971. ms_ptxtNetProb->m_sY = NET_PROB_GUI_Y;
  2972. // Set generic initial text.
  2973. ms_ptxtNetProb->SetText("%s", g_pszNetProb_General);
  2974. ms_ptxtNetProb->Compose();
  2975. }
  2976. else
  2977. {
  2978. TRACE("InitNetProbGUI(): Error loading Net Prob GUI \"%s\".\n",
  2979. NET_PROB_GUI_FILE);
  2980. }
  2981. return sRes;
  2982. }
  2983. //////////////////////////////////////////////////////////////////////////////
  2984. //
  2985. // Kill the net problems GUI.
  2986. //
  2987. //////////////////////////////////////////////////////////////////////////////
  2988. extern void KillNetProbGUI(void)
  2989. {
  2990. if (ms_ptxtNetProb)
  2991. {
  2992. rspReleaseResource(&g_resmgrShell, &ms_ptxtNetProb);
  2993. }
  2994. }
  2995. //////////////////////////////////////////////////////////////////////////////
  2996. //
  2997. // Get the net prob GUI which is used to notify the user of network problems.
  2998. // You can move it, draw it, change its text, or whatever. It's just up to
  2999. // you to avoid having a problem with other things updating its settings while
  3000. // you are.
  3001. // Note that this is automagically drawn to the screen via the blocking
  3002. // callback.
  3003. //
  3004. //////////////////////////////////////////////////////////////////////////////
  3005. extern RTxt* GetNetProbGUI(void)
  3006. {
  3007. return ms_ptxtNetProb;
  3008. }
  3009. //////////////////////////////////////////////////////////////////////////////
  3010. //
  3011. // Determine whether there is a net problem. Set via blocking callback.
  3012. // Cleared by you when you call ClearNetProb().
  3013. //
  3014. //////////////////////////////////////////////////////////////////////////////
  3015. extern bool IsNetProb(void) // Returns true, if net problem; false otherwise.
  3016. {
  3017. return m_bNetWatchdogExpired;
  3018. }
  3019. extern void ClearNetProb(void)
  3020. {
  3021. m_bNetWatchdogExpired = false;
  3022. }
  3023. ///////////////////////////////////////////////////////////////////////////////
  3024. // EOF
  3025. ///////////////////////////////////////////////////////////////////////////////