added licence (GPLv3, and all previous versions except one uploaded to Abandonia...
[awish.git] / asm / game / utils.awa
blobc54a507c5b61d5e2b15d74c66fabe573ea4a8b97
1 /*
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14  */
15 ; out:
16 ;  item_id: item id or 0 (be careful: it DESTROYS item_id tvar)
17 ;  rst_result: item thread id
18 ;  stack: item id (or 0)
20 proc: game_item_at_prof
21 locals: .cnt, .type
22   set [.cnt], 1
23 .loop:
24   ; only running thread
25   sta [.cnt]
26   jne .next, THREAD_STATE_RUNNING
27   ; only items
28   get [.cnt], item_id, [.type]
29   jlt .next, [.type], ITEM_KEY
30   jgt .next, [.type], ITEM_GLOVES
31   ; only not activated
32   get [.cnt], activated
33   jne .next, 0
34   ; only standing on tile
35   get [.cnt], pos_tx
36   jne .next, 0
37   get [.cnt], pos_ty
38   jne .next, 0
39   ; only our coords
40   get [.cnt], pos_x
41   jne .next, [pos_x]
42   get [.cnt], pos_y
43   jne .next, [pos_y]
44   ; found
45   set [item_id], [.type]
46   set [rst_result], [.cnt]
47   ret [.type]
48 .next:
49   add [.cnt], 1
50   jlt .loop, [.cnt], 257
51   set [item_id], 0
52   set [rst_result], 0
53   ret 0
54 endp: game_item_at_prof
57 prof_fix_position:
58   jeq .quit, [prof_item], 0
59   set pos_x, [pos_x], [prof_item]
60   set pos_y, [pos_y], [prof_item]
61 .quit:
62   ret
65 prof_turn_to_player:
66   jeq .quit, [spr_num], PSPR_FACING
67   set [spr_num], PSPR_TURNING
68   bsr game_next_frame
69   bsr game_next_frame
70   set [spr_dir], DIR_LEFT
71   set [spr_num], PSPR_FACING
72   bsr game_next_frame
73   bsr game_next_frame
74 .quit:
75   set [prof_dir], PDIR_FACING
76   ret
79 ; set 'prof_dir' to new dir and call this
80 prof_turn_to_dir:
81   jeq prof_turn_to_player, [prof_dir], PDIR_FACING
82   ; to left or to right, maybe thru 'facing'
83   jeq .looking_to_player, [spr_num], PSPR_FACING
84   jeq .looking_left, [spr_dir], DIR_LEFT
85   ;
86 .looking_right:
87   jeq .quit, [prof_dir], PDIR_RIGHT ; ok
88   ; want to turn left
89   bsr prof_turn_to_player
90   set [prof_dir], PDIR_LEFT
91 .to_left:
92 .complete_turning:
93   set [spr_num], PSPR_TURNING
94   bsr game_next_frame
95   bsr game_next_frame
96   set [spr_num], PSPR_STANDING
97   bsr game_next_frame
98   bsr game_next_frame
99   ret
100   ;
101 .looking_left:
102   jeq .quit, [prof_dir], PDIR_LEFT ; ok
103   ; want to turn right
104   bsr prof_turn_to_player
105   set [prof_dir], PDIR_RIGHT
106 .to_right:
107   set [spr_dir], DIR_RIGHT
108   jmp .complete_turning
109   ;
110 .looking_to_player:
111   ; left or right
112   jeq .to_left, [prof_dir], PDIR_LEFT
113   jmp .to_right
114   ;
115 .quit:
116   ret
119 prof_allowed_move_in_dir:
120   psh [pos_x]
121   add [prof_dir]
122   psh -666
123   mgb
124   jne game_no_no, 0
125   ret
128 ;proc: prof_allowed_move_in_dir_check
129 ;locals: .t
130 ;  add [pos_x], [prof_dir], [.t]
131 ;  mgb [.t], -666, [.t]
132 ;  ret [.t]
133 ;endp: prof_allowed_move_in_dir_check
135 ; funny hack
136 prof_allowed_move_in_dir_check:
137   psh [pos_x]
138   add [prof_dir]
139   psh -666
140   mgb
141   ;swp
142   rol -2
143   ret
146 proc: game_do_pick
147 arg: .profdir
148   set [spr_num], PSPR_TAKING0
149   bsr game_next_frame
150   bsr game_next_frame
151   set [spr_num], PSPR_TAKING1
152   bsr game_next_frame
153   bsr game_next_frame
154   ;
155   bsr game_item_at_prof
156   drp ; result
157   ;
158   jeq .nopitem, [prof_item], 0
159   ; put prof's item
160   get [prof_item], item_id
161   jeq .a_fan, ITEM_FAN
162   set spr_dir, DIR_LEFT, [prof_item]
163   jmp .put_item
164 .a_fan:
165   jeq .put_item, [.profdir], PDIR_FACING
166   set spr_dir, DIR_LEFT, [prof_item]
167   jeq .put_item, [.profdir], PDIR_LEFT
168   set spr_dir, DIR_RIGHT, [prof_item]
169 .put_item:
170   set pos_x, [pos_x], [prof_item]
171   set pos_y, [pos_y], [prof_item]
172   set pos_tx, 0, [prof_item]
173   set pos_ty, 0, [prof_item]
174   res [prof_item]
175   set [prof_item], 0
176 .nopitem:
177   jeq .done, [rst_result], 0
178   ; take item
179   set [prof_item], [rst_result]
180   sus [prof_item]
181 .done:
182   set [spr_num], PSPR_TAKING0
183   bsr game_next_frame
184   bsr game_next_frame
185   set [spr_num], PSPR_FACING
186   bsr game_next_frame
187   bsr game_next_frame
188   ret
189 endp: game_do_pick
192 proc: get_tile_at_dy
193 arg: .dy
194 locals: .x, .y
195   add [prof_dir], [pos_x], [.x]
196   add [.dy], [pos_y], [.y]
197   mgf [.x], [.y], [.dy]
198   ret [.dy]
199 endp: get_tile_at_dy
202 proc: set_tile_at_dy
203 arg: .dy, .tile
204 locals: .x, .y
205   add [prof_dir], [pos_x], [.x]
206   add [.dy], [pos_y], [.y]
207   msf [.x], [.y], [.tile]
208   ret
209 endp: set_tile_at_dy
212 proc: get_tile_at_dx_dy
213 arg: .dx, .dy
214 locals: .x, .y
215   add [prof_dir], [pos_x], [.x]
216   add [.x], [.dx]
217   add [.dy], [pos_y], [.y]
218   mgf [.x], [.y], [.dy]
219   ret [.dy]
220 endp: get_tile_at_dx_dy
223 proc: set_tile_at_dx_dy
224 arg: .tile, .dx, .dy
225 locals: .x, .y
226   add [prof_dir], [pos_x], [.x]
227   add [.x], [.dx]
228   add [.dy], [pos_y], [.y]
229   msf [.x], [.y], [.tile]
230   ret
231 endp: set_tile_at_dx_dy
234 ; in:
235 ;  .dy: direction
236 proc: game_lift_all_items
237 arg: .dy
238 locals: .cnt, .type
239   set [.cnt], 1
240 .loop:
241   ; only running thread
242   sta [.cnt]
243   jne .next, THREAD_STATE_RUNNING
244   ; only items
245   get [.cnt], item_id, [.type]
246   jlt .next, [.type], ITEM_KEY
247   jgt .next, [.type], ITEM_GLOVES
248   get [.cnt], liftdown
249   jne .fixitem, 0
250   ; only not activated
251   ;get [.cnt], activated
252   ;jne .next, 0
253   ; only standing on tile
254   get [.cnt], pos_tx
255   jne .next, 0
256   get [.cnt], pos_ty
257   jne .next, 0
258   ; only our coords
259   get [.cnt], pos_x
260   jne .next, [pos_x]
261   get [.cnt], pos_y
262   jne .next, [pos_y]
263   ; found, kick it up
264 .fixitem:
265   set pos_ty, [.dy], [.cnt]
266   set liftdown, 0, [.cnt]
267 .next:
268   add [.cnt], 1
269   jlt .loop, [.cnt], 257
270   ret
271 endp: game_lift_all_items