File: xnetserv-notix.tcl

package info (click to toggle)
netmaze 0.81+jpg0.82-14.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,208 kB
  • ctags: 915
  • sloc: ansic: 8,277; tcl: 1,223; makefile: 66; sh: 26
file content (513 lines) | stat: -rw-r--r-- 13,794 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/usr/bin/wish8.5 -f


##### SET UP PROCEDURES

proc unimplemented {} {
  toplevel .u
  label .u.msg -text "This function is not yet implemented."
  pack .u.msg
  button .u.b -text "OK" -command {
    destroy .u
    return 0
  }
  pack .u.b
  wm title .u "xnetserv: unimplemented function"
}

proc setcolor {id value} {
  global teams isdlgbox
  
  # set teams($id) $value
  
  switch $teams($id) {
    0 {set color Yellow}
    1 {set color Red}
    2 {set color Green}
    3 {set color Blue}
    4 {set color SandyBrown}
    5 {set color DarkOliveGreen}
    6 {set color Orange}
    7 {set color DarkOrchid}
    8 {set color Pink}
    9 {set color VioletRed}
    10 {set color LightBlue}
    11 {set color LightCyan}
    12 {set color RosyBrown}
    13 {set color IndianRed}
    14 {set color DeepPink}
    15 {set color LightPink}
    16 {set color yellowGreen}
    17 {set color orchid}
    18 {set color lavender}
    19 {set color lemonchiffon}
    20 {set color YellowGreen}
    21 {set color khaki}
    22 {set color DarkKhaki}
    23 {set color violet}
    24 {set color plum}
    25 {set color DarkOrchid}
    26 {set color DarkViolet}
    27 {set color PaleTurquoise}
    28 {set color Turquoise}
    29 {set color darkturquoise}
    30 {set color coral}
    31 {set color Black}
  }
    
  if {$isdlgbox($id)==1} {
    .players.${id}.dlgbox.sample config -bg $color
  }
}

proc playerbox {id} {
  global ids teams names allplayers isdlgbox
  toplevel .players.${id}.dlgbox
  set isdlgbox($id) 1
  label .players.${id}.dlgbox.l -text "Player $names($id), id $id.\n\
    Select Dismiss below to close this box.\n\
    Select Terminate to kick this player out\n\
    of the game.   You can select the player's\n\
    team below.  A sample of the color of that\n\
    player will be displayed."
  scale .players.${id}.dlgbox.team -label "Team number" -from 0 -to 31 \
    -orient horizontal -digits 0 -command "setcolor $id" -variable teams($id)
  label .players.${id}.dlgbox.sample -text "Player color" -fg black
  setcolor $id 0
  button .players.${id}.dlgbox.b1 -text "Dismiss" -command "\
    destroy .players.$id.dlgbox;\
    set isdlgbox($id) 0"
  button .players.${id}.dlgbox.b2 -text "Terminate" -command "\
    destroy .players.$id.dlgbox;\
    puts \"5 $id\";\
    set isdlgbox($id) 0"
  pack .players.${id}.dlgbox.l
  pack .players.${id}.dlgbox.team .players.${id}.dlgbox.sample -fill x
  pack .players.${id}.dlgbox.b1 .players.${id}.dlgbox.b2
  wm title .players.${id}.dlgbox "xnetserv: info on $names($id) ($id)"
}

proc destroymainmenu {} {
  destroy .option1 .option2 .players .misc .options .msg .inmsg
}

proc sethelp {message} {
  global helpmsg helpon
  # Sets help message, if possible....
  if {$helpon} {
    if {$helpmsg!=$message} {
      set helpmsg $message
    }
  }
}
  
proc addplayerfunc {name id addvars} {
  # allplayers is a list containing all unique IDs.
  # a unique ID is $id_$name
  global allplayers teams names ids \
    isgip numplayers isdlgbox
  if {$addvars} {
    # ONLY add to the vars if this is a NEW creation
    lappend allplayers ${id}
    set names(${id}) $name
    set ids(${id}) $id
    incr numplayers
    set teams(${id}) $id
    set isdlgbox($id) 0
  }
  if {!$isgip} {
    # Only draw stuff if game is not in progress
    frame .players.${id}


    button .players.${id}.name -text $name -command "playerbox $id"
    bind .players.${id}.name <Any-Motion> {
      sethelp "These buttons let you view info on the player\nor\
        terminate the player's connection to the game."
    }

    entry .players.${id}.team -width 5 -relief sunken -textvariable \
      teams(${id})
    bind .players.${id}.team <Any-Motion> {
      sethelp "This lets you set the team a given player is on.\nAll\
        players with the same team number will be on the same team."
    }

    bind .players.${id}.team <Return> "setcolor $id 0"

    pack .players.${id} -in .players -fill x
    pack .players.${id}.name -side left -in .players.${id}
    pack .players.${id}.team -in .players.${id} -side right -padx 1m
  }
}

proc drawplayer {name id} {addplayerfunc $name $id 0}

proc addplayer {name id} {
  addplayerfunc $name $id 1
}

proc rmplayer {name id} {
  global allplayers names ids teams numplayers isgip
  incr numplayers -1
  unset names(${id})
  unset ids(${id})
  unset teams(${id})
  unset isdlgbox($id)
  set listindex [lsearch -exact $allplayers ${id}]
  set allplayers [lreplace $allplayers $listindex $listindex]
  unset listindex
  if {!$isgip} {
    destroy .players.${id}
  }
}

proc resetplayers {} {
  global allplayers names ids teams numplayers isgip isdlgbox
  if {$numplayers} {
    unset allplayers
    unset names
    unset ids
    unset teams
    unset isdlgbox
  }
  set numplayers 0
  if {!$isgip} {
    destroymainmenu
    mainmenu
  }
}

proc startgame {} {
  global isgip
  destroymainmenu
  frame .gip
  label .gip.l -text "Netmaze game is in progress!\nOptions are disabled while\
     game is running.\nThis is Netmaze net protocol version 0.81" -fg white \
     -bg black
  label .gip.lp -image NMLogo -bg black -highlightthickness 0
  button .gip.b -text "Abort game" -fg white -bg black \
    -highlightthickness 0 -command {
    puts "3"
  }
  label .gip.msgs -textvariable dispmsg -fg green -bg black
  pack .gip -fill both
  pack .gip.l .gip.lp .gip.b .gip.msgs -in .gip -fill both
  set isgip 1
  update idletasks
}

proc stopgame {} {
  global isgip
  if {$isgip} {
    destroy .gip
  }
  set isgip 0
  mainmenu
  update idletasks
}

proc resetteams {} {
  # resets all teams to their defaults, that is, their ids
  global teams ids allplayers numplayers
  if {$numplayers} {
    foreach thisplayer $allplayers {
      set teams($thisplayer) $ids($thisplayer)
    }
  }
}

proc mainmenu {} {
  global newmaze allplayers names ids numplayers teams BEATDIV dispmsg
  wm title . "xnetserv 0.82"
  
  frame .option1

  # netserv option 1
  
  button .option1.b -text "Load/Reinit maze:" -command {
    puts "1 $newmaze" }
  bind .option1.b <Any-Motion> {
    sethelp "This will load a new maze (if the\nentry\
      box has a filename in it) or reinitialize\nthe\
      current maze."
  }


  entry .option1.e -width 20 -textvariable newmaze
  bind .option1.e <Any-Motion> {
    sethelp "Type the name of a maze to load here,\n\
      or leave blank to use the default maze."
  }

  bind .option1.e <Return> {
    puts "1 $newmaze" }

  pack .option1 -side top -padx 1m -pady 1m
  pack .option1.b .option1.e -in .option1 -side left
  
  # netserv option 2
  
  frame .option2

  button .option2.b -text "Start game" -command {
    if {$numplayers} {
      # First....do the options.
      # clear all options
      puts "98"
      
      if {$BOUNCE} {puts "21"}
      if {$DECAY} {puts "22"}
      if {$MULTISHOT} {puts "23"}
      if {$HURTS2SHOOT} {puts "24"}
      if {$REPOWERONKILL} {puts "25"}
      if {$FASTHEAL} {puts "26"}
      if {$FASTWALK} {puts "27"}
      if {$CLOAK} {puts "29"}
      if {$DECSCORE} {puts "32"}
      if {$TEAMSHOTHURT} {puts "34"}
      
      if {$BEATDIV>=2} {puts "7"}
      if {$BEATDIV==4} {puts "7"}
      
      set startcmd "2"
      # Cycle through teams....
      foreach thisplayer $allplayers {
        lappend cmdlist $teams($thisplayer)
      }
      set i [expr [llength $cmdlist] -1]
      while {$i >= 0} {
        append startcmd " "
        append startcmd [lindex $cmdlist $i]
        incr i -1
      }
      puts $startcmd
      unset startcmd thisplayer cmdlist i
    }
  }
  bind .option2.b <Any-Motion> {
    sethelp "Start a game with the current settings."
  }

  button .option2.reset -text "Reset teams" -command resetteams
  bind .option2.reset <Any-Motion> {
    sethelp "Reset teams so that everyone is\nplaying\
      individually.  (back to how they were set\nprior\
      to any modification)"
  }
  
  button .option2.refresh -text "Refresh" -command {
    puts "96"
  }
  bind .option2.refresh <Any-Motion> {
    sethelp "Will refresh xnetserv's team listing in case it somehow got\
             corrupted."
  }

  pack .option2 -side top -padx 1m -pady 1m -side top
  pack .option2.b .option2.reset .option2.refresh -side left -in .option2 -padx 1m
  
  frame .players -relief ridge -borderwidth 4
  frame .players.labels
  label .players.labels.name -text "Players:"
  label .players.labels.team -text "Team:"
  pack .players -side top
  pack .players.labels -side top -in .players -fill x
  pack .players.labels.name -side left -in .players.labels
  pack .players.labels.team -side right -in .players.labels
  
  if {$numplayers} {
    foreach thisplayer $allplayers {
      drawplayer $names($thisplayer) $ids($thisplayer)
    }
  }
  
  ############ THE OPTIONS!
  
  frame .options -relief ridge -borderwidth 4
  frame .options.m
  frame .options.a
  pack .options -side top -fill x
  pack .options.m .options.a -fill x -in .options
  frame .options.m.f1
  frame .options.m.f2
  frame .options.a.f3

  checkbutton .options.m.f1.bounce -text Bounce -variable BOUNCE
  checkbutton .options.m.f1.decay -text Decay -variable DECAY
  checkbutton .options.m.f1.multishot -text "Multi-shot" -variable MULTISHOT
  checkbutton .options.m.f1.hurts2shoot -text "Hurts to shoot" -variable HURTS2SHOOT
  checkbutton .options.m.f1.repoweronkill -text "Repower on kill" -variable REPOWERONKILL
  checkbutton .options.m.f2.fastheal -text "Fast heal" -variable FASTHEAL
  checkbutton .options.m.f2.fastwalk -text "Fast walk" -variable FASTWALK
  checkbutton .options.m.f2.cloak -text "Cloaking" -variable CLOAK
  checkbutton .options.m.f2.decscore -text "Decrease score" -variable DECSCORE
  checkbutton .options.m.f2.teamshothurt -text "Team shots hurt" -variable TEAMSHOTHURT
  
  button .options.a.f3.beatbutton -text "Beat divider" -command {
    if {1==$BEATDIV} {
      set BEATDIV 2
    } elseif {$BEATDIV==2} {
      set BEATDIV 4
    } elseif {$BEATDIV==4} {
      set BEATDIV 1
    }
  }
  label .options.a.f3.beatlabel -textvariable BEATDIV
  
  pack .options.m.f1 .options.m.f2 -side left -fill x
  pack .options.a.f3 -fill x
  pack .options.m.f1.bounce .options.m.f1.decay -in .options.m.f1 -anchor w
  pack .options.m.f1.multishot .options.m.f1.hurts2shoot -in .options.m.f1 -anchor w
  pack .options.m.f1.repoweronkill -in .options.m.f1 -anchor w
  pack .options.m.f2.fastheal -in .options.m.f2 -anchor w
  pack .options.m.f2.fastwalk .options.m.f2.cloak -in .options.m.f2 -anchor w
  pack .options.m.f2.decscore .options.m.f2.teamshothurt -in .options.m.f2 -anchor w
  pack .options.a.f3.beatbutton .options.a.f3.beatlabel -in .options.a.f3 -anchor w -side left
  
  # messages
  
  frame .msg
  pack .msg -side top -fill x
  entry .msg.entry -textvariable MSGSEND
  pack .msg.entry -fill x -in .msg
  bind .msg.entry <Return> {
    set transcmd "97 "
    append transcmd $MSGSEND
    puts $transcmd
    unset transcmd
  }
  bind .msg.entry <Any-Motion> {
    sethelp "Type a message here and press enter to send\nit\
             to the xterms of all players."
  }
  
  # quit

  frame .misc
    
  button .misc.b -text "Quit xnetserv" -command {
    puts "9"
    exit
  }
  button .misc.help -text "Online help" -command {
    if {!$helpon} {
      toplevel .help
      label .help.intro -text "xnetserv by John Goerzen\nOnline help\nMove\
        the mouse over any item\nand help will appear here."
      frame .help.m
      label .help.m.l -textvariable helpmsg -relief sunken -borderwidth 4
      button .help.button -text "Dismiss" -command {
        destroy .help
        set helpon 0
      }
      label .help.author -text "E-mail: <jgoerzen@complete.org>"
      wm title .help "xnetserv help"
      set helpon 1
      pack .help.intro
      pack .help.m -fill x
      pack .help.button .help.author
      pack .help.m.l -in .help.m -side left
    }
  }
      
    
  pack .misc -side top -padx 1m -pady 1m -side top
  pack .misc.b .misc.help -side left -in .misc
  
  # Messages from netserv
  
  frame .inmsg
  label .inmsg.l -textvariable dispmsg -fg blue
  
  pack .inmsg -side top -padx 1m -pady 1m -fill x
  pack .inmsg.l -side left -in .inmsg -fill x
  
}

proc intro {} {

  wm title . "Welcome to Netmaze!"
  label .li -text "Welcome to Netmaze!  Click below to begin" -bg midnightblue \
   -fg white
  pack .li
  label .l -text "Version 0.82 (0.81 compat)" -bg black -fg red
  . config -bg black
  image create photo NMLogo -file nmlogo.gif
  button .b -image NMLogo -highlightthickness 0 -fg black -bg black -highlightbackground black -command {
    destroy .b .li
    destroy .l
    set isgip 0
    . config -bg #d9d9d9
    mainmenu
    return 0 
  }
  pack .b .l
}

proc inevhandler {} {
  global dispmsg
  
  gets stdin line
  switch $line {
    MESSAGE {
      gets stdin inline
      set dispmsg "$inline"
    }
    MESSAGECAT {
      gets stdin inline
      append dispmsg "\n$inline"
    }
    CONNECTIONLIST {
      resetplayers
      gets stdin inline
      while {$inline!="ENDCONNECTIONLIST"} {
        gets stdin playernum
        gets stdin playername
        gets stdin playerhost
        addplayer $playername $playernum
        gets stdin inline
      }
      update idletasks
    }
    STARTGAME {
      startgame
    }
    STOPGAME {
      stopgame
    }
  }
}




# This is it, folks: everything starts here.

set numplayers 0
set isgip 1
set helpon 0
set helpmsg "This is xnetserv by John Goerzen."
# isgip is set to true so that messages don't try to draw on the box.
# When the main box appears, isgip is set to 0.

# default options

set BOUNCE 0
set DECAY 0
set HURTS2SHOOT 0
set MULTISHOT 1
set REPOWERONKILL 0
set FASTHEAL 1
set FASTWALK 1
set CLOAK 0
set DECSCORE 0
set TEAMSHOTHURT 0

set BEATDIV 1

set dispmsg "Welcome to xnetserv!"

intro

fileevent stdin readable {inevhandler}