alot of code changed; first try to move level loader out of C engine; not working yet
[awish.git] / asm / game / utils.awa
blob31068487d071d806c3f8acb7df9614bea3cb729e
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   rst FRST_GET_MAX_THREAD_ID
51   jle .loop, [.cnt], [rst_result]
52   set [item_id], 0
53   set [rst_result], 0
54   ret 0
55 endp: game_item_at_prof
58 prof_fix_position:
59   jeq .quit, [prof_item], 0
60   set pos_x, [pos_x], [prof_item]
61   set pos_y, [pos_y], [prof_item]
62 .quit:
63   ret
66 prof_turn_to_player:
67   jeq .quit, [spr_num], PSPR_FACING
68   set [spr_num], PSPR_TURNING
69   bsr game_next_frame
70   bsr game_next_frame
71   set [spr_dir], DIR_LEFT
72   set [spr_num], PSPR_FACING
73   bsr game_next_frame
74   bsr game_next_frame
75 .quit:
76   set [prof_dir], PDIR_FACING
77   ret
80 ; set 'prof_dir' to new dir and call this
81 prof_turn_to_dir:
82   jeq prof_turn_to_player, [prof_dir], PDIR_FACING
83   ; to left or to right, maybe thru 'facing'
84   jeq .looking_to_player, [spr_num], PSPR_FACING
85   jeq .looking_left, [spr_dir], DIR_LEFT
86   ;
87 ;;;.looking_right:
88   jeq .quit, [prof_dir], PDIR_RIGHT ; ok
89   ; want to turn left
90   bsr prof_turn_to_player
91   set [prof_dir], PDIR_LEFT
92 .to_left:
93 .complete_turning:
94   set [spr_num], PSPR_TURNING
95   bsr game_next_frame
96   bsr game_next_frame
97   set [spr_num], PSPR_STANDING
98   bsr game_next_frame
99   bsr game_next_frame
100   ret
101   ;
102 .looking_left:
103   jeq .quit, [prof_dir], PDIR_LEFT ; ok
104   ; want to turn right
105   bsr prof_turn_to_player
106   set [prof_dir], PDIR_RIGHT
107 .to_right:
108   set [spr_dir], DIR_RIGHT
109   jmp .complete_turning
110   ;
111 .looking_to_player:
112   ; left or right
113   jeq .to_left, [prof_dir], PDIR_LEFT
114   jmp .to_right
115   ;
116 .quit:
117   ret
120 prof_allowed_move_in_dir:
121   psh [pos_x]
122   add [prof_dir]
123   psh -666
124   mgb
125   jne game_no_no, 0
126   ret
129 ;proc: prof_allowed_move_in_dir_check
130 ;locals: .t
131 ;  add [pos_x], [prof_dir], [.t]
132 ;  mgb [.t], -666, [.t]
133 ;  ret [.t]
134 ;endp: prof_allowed_move_in_dir_check
136 ; funny hack
137 prof_allowed_move_in_dir_check:
138   psh [pos_x]
139   add [prof_dir]
140   psh -666
141   mgb
142   ;swp
143   rol -2
144   ret
147 proc: game_do_pick
148 arg: .profdir
149   set [spr_num], PSPR_TAKING0
150   bsr game_next_frame
151   bsr game_next_frame
152   set [spr_num], PSPR_TAKING1
153   bsr game_next_frame
154   bsr game_next_frame
155   ;
156   bsr game_item_at_prof
157   drp ; result
158   ;
159   jeq .nopitem, [prof_item], 0
160   ; put prof's item
161   get [prof_item], item_id
162   jeq .a_fan, ITEM_FAN
163   set spr_dir, DIR_LEFT, [prof_item]
164   jmp .put_item
165 .a_fan:
166   jeq .put_item, [.profdir], PDIR_FACING
167   set spr_dir, DIR_LEFT, [prof_item]
168   jeq .put_item, [.profdir], PDIR_LEFT
169   set spr_dir, DIR_RIGHT, [prof_item]
170 .put_item:
171   set pos_x, [pos_x], [prof_item]
172   set pos_y, [pos_y], [prof_item]
173   set pos_tx, 0, [prof_item]
174   set pos_ty, 0, [prof_item]
175   res [prof_item]
176   set [prof_item], 0
177 .nopitem:
178   jeq .done, [rst_result], 0
179   ; take item
180   set [prof_item], [rst_result]
181   sus [prof_item]
182 .done:
183   set [spr_num], PSPR_TAKING0
184   bsr game_next_frame
185   bsr game_next_frame
186   set [spr_num], PSPR_FACING
187   bsr game_next_frame
188   bsr game_next_frame
189   bsr set_item_name_vars, [prof_item]
190   ret
191 endp: game_do_pick
194 proc: get_tile_at_dy
195 arg: .dy
196 locals: .x, .y
197   add [prof_dir], [pos_x], [.x]
198   add [.dy], [pos_y], [.y]
199   mgf [.x], [.y], [.dy]
200   ret [.dy]
201 endp: get_tile_at_dy
204 proc: set_tile_at_dy
205 arg: .dy, .tile
206 locals: .x, .y
207   add [prof_dir], [pos_x], [.x]
208   add [.dy], [pos_y], [.y]
209   msf [.x], [.y], [.tile]
210   ret
211 endp: set_tile_at_dy
214 proc: get_tile_at_dx_dy
215 arg: .dx, .dy
216 locals: .x, .y
217   add [prof_dir], [pos_x], [.x]
218   add [.x], [.dx]
219   add [.dy], [pos_y], [.y]
220   mgf [.x], [.y], [.dy]
221   ret [.dy]
222 endp: get_tile_at_dx_dy
225 proc: set_tile_at_dx_dy
226 arg: .tile, .dx, .dy
227 locals: .x, .y
228   add [prof_dir], [pos_x], [.x]
229   add [.x], [.dx]
230   add [.dy], [pos_y], [.y]
231   msf [.x], [.y], [.tile]
232   ret
233 endp: set_tile_at_dx_dy
236 ; in:
237 ;  .dy: direction
238 proc: game_lift_all_items
239 arg: .dy
240 locals: .cnt, .type
241   set [.cnt], 1
242 .loop:
243   ; only running thread
244   sta [.cnt]
245   jne .next, THREAD_STATE_RUNNING
246   ; only items
247   get [.cnt], item_id, [.type]
248   jlt .next, [.type], ITEM_KEY
249   ;jgt .next, [.type], ITEM_GLOVES
250   get [.cnt], liftdown
251   jne .fixitem, 0
252   ; only not activated
253   ;get [.cnt], activated
254   ;jne .next, 0
255   ; only standing on tile
256   get [.cnt], pos_tx
257   jne .next, 0
258   get [.cnt], pos_ty
259   jne .next, 0
260   ; only our coords
261   get [.cnt], pos_x
262   jne .next, [pos_x]
263   get [.cnt], pos_y
264   jne .next, [pos_y]
265   ; found, kick it up
266 .fixitem:
267   set pos_ty, [.dy], [.cnt]
268   set liftdown, 0, [.cnt]
269 .next:
270   add [.cnt], 1
271   rst FRST_GET_MAX_THREAD_ID
272   jle .loop, [.cnt], [rst_result]
273   ret
274 endp: game_lift_all_items