Mercurial > rinkhals
annotate gamelib/misc.py @ 533:7addf41b6abb
Allow backtracking on ladders
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Fri, 27 Nov 2009 22:09:54 +0000 |
parents | f84ad10a9625 |
children | d16ed2a8a33e |
rev | line source |
---|---|
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
1 # Holder for misc useful classes |
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
2 |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
3 import random |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
4 |
336 | 5 from pygame.locals import KEYDOWN, K_ESCAPE |
6 from pgu import gui | |
431
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
7 from pgu.algo import getline |
336 | 8 |
427
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
9 import serializer |
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
10 |
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
11 class Position(serializer.Simplifiable): |
456
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
12 """2D/3D position / vector. Assumed immutable.""" |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
13 |
427
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
14 SIMPLIFY = ['x', 'y', 'z'] |
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
15 |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
16 def __init__(self, x, y, z=0): |
100 | 17 self.x = x |
18 self.y = y | |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
19 self.z = z |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
20 |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
21 def to_tile_tuple(self): |
100 | 22 return self.x, self.y |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
23 |
456
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
24 def to_3d_tuple(self): |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
25 return self.x, self.y, self.z |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
26 |
100 | 27 def dist(self, b): |
28 """Gives the distance to another position""" | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
29 |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
30 return max(abs(self.x - b.x), abs(self.y - b.y), abs(self.z - b.z)) |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
31 |
100 | 32 def __sub__(self, b): |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
33 return Position(self.x - b.x, self.y - b.y, self.z - b.z) |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
34 |
100 | 35 def __add__(self, b): |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
36 return Position(self.x + b.x, self.y + b.y, self.z + b.z) |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
37 |
100 | 38 def left_of(self, b): |
39 return self.x < b.x | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
40 |
100 | 41 def right_of(self, b): |
42 return self.x > b.x | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
43 |
456
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
44 def __hash__(self): |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
45 return hash(self.to_3d_tuple()) |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
46 |
100 | 47 def __eq__(self, b): |
456
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
48 return self.to_3d_tuple() == b.to_3d_tuple() |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
49 |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
50 def __str__(self): |
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
51 return "<Position: %s>" % (self.to_3d_tuple(),) |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
52 |
431
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
53 def intermediate_positions(self, b): |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
54 """Only operates in two dimensions.""" |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
55 if max(abs(self.x - b.x), abs(self.y - b.y)) <= 1: |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
56 # pgu gets this case wrong on occasion. |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
57 return [b] |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
58 start = self.to_tile_tuple() |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
59 end = b.to_tile_tuple() |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
60 points = getline(start, end) |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
61 points.remove(start) # exclude start_pos |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
62 if end not in points: |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
63 # Rounding errors in getline cause this |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
64 points.append(end) |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
65 return [Position(p[0], p[1]) for p in points] |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
66 |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
67 class WeightedSelection(object): |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
68 def __init__(self, weightings=None): |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
69 self.weightings = [] |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
70 self.total = 0 |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
71 if weightings: |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
72 for item, weight in weightings: |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
73 self.weightings.append((item, weight)) |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
74 self.total += weight |
456
96dbf2c8506e
Factor position cache out into its own class, make Position more useful.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
75 |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
76 def choose(self): |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
77 roll = random.uniform(0, self.total) |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
78 for item, weight in self.weightings: |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
79 if roll < weight: |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
80 return item |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
81 roll -= weight |
336 | 82 |
83 class CheckDialog(gui.Dialog): | |
84 def __init__(self, **params): | |
347
35f09e0ccd16
Make 'Esc' cancel the 'Are You Sure?' dialog. Fix dialog title
Neil Muller <drnlmuller@gmail.com>
parents:
336
diff
changeset
|
85 title = gui.Label('Are You Sure?') |
336 | 86 self.do_quit = False |
87 self.running = True | |
88 tbl = gui.Table() | |
89 tbl.tr() | |
90 tbl.td(gui.Label("Do you REALLY want to exit this game?"), colspan=2) | |
91 tbl.tr() | |
92 tbl.td(gui.Spacer(0, 15), colspan=2) | |
93 tbl.tr() | |
94 yes_button = gui.Button('Yes') | |
95 yes_button.connect(gui.CLICK, self.clicked, True) | |
96 no_button = gui.Button('No') | |
97 no_button.connect(gui.CLICK, self.clicked, False) | |
98 tbl.td(no_button, align=-1) | |
99 tbl.td(yes_button, align=1) | |
100 gui.Dialog.__init__(self, title, tbl, **params) | |
101 | |
102 def clicked(self, val): | |
103 self.do_quit = val | |
104 self.running = False | |
105 self.close() | |
106 | |
107 def event(self, e): | |
108 if e.type == KEYDOWN and e.key == K_ESCAPE: | |
347
35f09e0ccd16
Make 'Esc' cancel the 'Are You Sure?' dialog. Fix dialog title
Neil Muller <drnlmuller@gmail.com>
parents:
336
diff
changeset
|
109 self.clicked(False) |
336 | 110 return True |
111 return gui.Dialog.event(self, e) | |
112 | |
113 def check_exit(): | |
114 dialog = CheckDialog() | |
115 dialog.open() | |
116 return dialog | |
117 | |
519
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
118 # Utility layout functions |
336 | 119 |
519
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
120 def make_box(text): |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
121 style = { |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
122 'border' : 1 |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
123 } |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
124 word = gui.Label(text, style=style) |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
125 return word |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
126 |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
127 def fix_widths(doc): |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
128 """Loop through all the widgets in the doc, and set the |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
129 width of the labels to max + 10""" |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
130 # We need to do this because of possible font issues |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
131 max_width = 0 |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
132 for thing in doc.widgets: |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
133 if hasattr(thing, 'style'): |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
134 # A label |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
135 if thing.style.width > max_width: |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
136 max_width = thing.style.width |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
137 for thing in doc.widgets: |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
138 if hasattr(thing, 'style'): |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
139 thing.style.width = max_width + 10 |
f84ad10a9625
Remove move tool. Add basic controls reference dialog
Neil Muller <drnlmuller@gmail.com>
parents:
456
diff
changeset
|
140 |