Debian Codegraph @

terps/hugo/source/heobject.c

raw symbols SCIP
1 /*
2 HEOBJECT.C
3
4 Object/property/attribute management functions:
5
6 Child PutAttributes
7 Children SetAttribute
8 Elder Sibling
9 GetAttributes TestAttribute
10 GetProp Youngest
11 GrandParent
12 MoveObj
13 Name
14 Parent
15 PropAddr
16
17 for the Hugo Engine
18
19 Copyright (c) 1995-2009 by Kent Tessman
20 */
21
22
23 #include "heheader.h"
24
25 #if defined (DEBUGGER)
26 int CheckObjectRange(int obj);
27 #endif
28
29 int display_object = -1; /* i.e., non-existent (yet) */
30 char display_needs_repaint = 0; /* for display object */
31 int display_pointer_x = 0, display_pointer_y = 0;
32
33
34 /* CHECKOBJECTRANGE
35
36 Shorthand since many of these object functions may call
37 CheckinRange() if the debugger is running and runtime_warnings
38 is set.
39 */
40 #if defined (DEBUGGER)
41 int CheckObjectRange(int obj)
42 {
43 if (runtime_warnings)
44 {
45 return CheckinRange((unsigned)obj, (unsigned)objects, "object");
46 }
47 else
48 return true;
49 }
50 #endif
51
52
53 /* CHILD */
54
55 int Child(int obj)
56 {
57 int c;
58
59 #if defined (DEBUGGER)
60 if (!CheckObjectRange(obj)) return 0;
61 #endif
62 if (obj<0 || obj>=objects) return 0;
63
64 defseg = objtable;
65
66 c = PeekWord(2 + obj*object_size + object_size - 4);
67
68 defseg = gameseg;
69
70 return c;
71 }
72
73
74 /* CHILDREN */
75
76 int Children(int obj)
77 {
78 int count = 0;
79 int nextobj;
80
81 if (obj<0 || obj>=objects) return 0;
82
83 nextobj = Child(obj);
84 while (nextobj)
85 {count++;
86 nextobj = Sibling(nextobj);}
87 return count;
88 }
89
90
91 /* ELDER */
92
93 int Elder(int obj)
94 {
95 int lastobj;
96 int p, cp;
97
98 if (obj<0 || obj>=objects) return 0;
99
100 p = Parent(obj);
101 cp = Child(p);
102
103 if (p==0 || cp==obj)
104 return 0;
105
106 lastobj = cp;
107 while (Sibling(lastobj) != obj)
108 lastobj = Sibling(lastobj);
109
110 return lastobj;
111 }
112
113
114 /* GETATTRIBUTES
115
116 Returns one of four sets of 32 attributes.
117 */
118
119 unsigned long GetAttributes(int obj, int attribute_set)
120 {
121 unsigned long a;
122
123 defseg = objtable;
124
125 #if defined (DEBUGGER)
126 if (!CheckObjectRange(obj)) return 0;
127 #endif
128 if (obj<0 || obj>=objects) return 0;
129
130 a = (unsigned long)PeekWord(2 + obj*object_size + attribute_set*4)
131 + (unsigned long)PeekWord(2 + obj*object_size + attribute_set*4 + 2)*65536L;
132
133 defseg = gameseg;
134
135 return a;
136 }
137
138
139 /* GETPROP
140
141 Returns the value of '<obj>.<p> #<n>' If <s> is true, the self global
142 is not set to <obj> in order to facilitate <obj>..<p> calls.
143 */
144
145 int GetProp(int obj, int p, int n, char s)
146 {
147 char objonly, /* no verbroutine given in before, etc. */
148 isadditive = 0, /* before, after, etc. */
149 gotone = 0, /* when a match has been made */
150 getpropaddress = 0; /* when getting &object.property */
151 int i;
152 int tempself,
153 objtype, /* i.e., what we're matching to */
154 flag = 0;
155 int g = 0;
156 int templocals[MAXLOCALS];
157 int temp_stack_depth;
158 char tempinexpr = inexpr;
159 unsigned int pa, /* property address */
160 offset = 0;
161 long inprop, /* code position in complex property */
162 returnaddr;
163 #if defined (DEBUGGER)
164 long orig_inprop;
165 int tempdbnest;
166
167 /* Don't check a possible non-existent display object (-1) */
168 if (obj!=-1 || display_object!=-1) CheckObjectRange(obj);
169 #endif
170 /* This way either -1 (the non-existent display object) or a too-high
171 object will fail
172 */
173 if (obj<0 || obj>=objects) return 0;
174
175 /* The display object, which is automatically created by the compiler,
176 did not exist pre-v2.4
177 */
178 if ((obj==display_object) && game_version>=24)
179 {
180 /* There are no actual default "properties" per se on the
181 display object--but reading them returns certain data about
182 the current state of display affairs
183 */
184
185 /* no display.<prop> #2, etc. */
186 if (n==1 && p<=pointer_y)
187 {
188 if (p==screenwidth)
189 #if defined (GLK) && defined (ACTUAL_LINELENGTH)
190 g = ACTUAL_LINELENGTH();
191 #else
192 g = SCREENWIDTH/FIXEDCHARWIDTH;
193 #endif
194 else if (p==screenheight)
195 /* ACTUAL_SCREENHEIGHT can be set to a non-portable function if
196 SCREENHEIGHT and SCREENHEIGHT have been set to large values in
197 order to force the non-portable layer to handle wrapping and
198 scrolling (as in the Glk port).
199 */
200 #if defined (ACTUAL_SCREENHEIGHT)
201 g = ACTUAL_SCREENHEIGHT();
202 #else
203 g = SCREENHEIGHT/FIXEDLINEHEIGHT;
204 #endif
205 else if (p==linelength)
206 /* ACTUAL_LINELENGTH functions similarly to ACTUAL_SCREENWIDTH,
207 above.
208 */
209 #if defined (ACTUAL_LINELENGTH)
210 g = ACTUAL_LINELENGTH();
211 #else
212 g = physical_windowwidth/FIXEDCHARWIDTH;
213 #endif
214 else if (p==windowlines)
215 g = physical_windowheight/FIXEDLINEHEIGHT;
216 else if (p==cursor_column)
217 g = (currentpos+1+hugo_textwidth(pbuffer))/FIXEDCHARWIDTH;
218 else if (p==cursor_row)
219 g = currentline;
220 else if (p==hasgraphics)
221 g = hugo_hasgraphics();
222 else if (p==title_caption)
223 g = FindWord(game_title);
224 else if (p==hasvideo)
225 #if !defined (COMPILE_V25)
226 g = hugo_hasvideo();
227 #else
228 g = 0;
229 #endif
230 else if (p==needs_repaint)
231 g = display_needs_repaint;
232 else if (p==pointer_x)
233 g = display_pointer_x;
234 else if (p==pointer_y)
235 g = display_pointer_y;
236 else
237 g = 0;
238
239 return g;
240 }
241 }
242
243 /* To avoid prematurely getting an address in &obj.prop.prop */
244 if (getaddress && MEM(codeptr)!=DECIMAL_T)
245 getpropaddress = true;
246
247 tempself = var[self];
248 if (!s) var[self] = obj;
249
250 temp_stack_depth = stack_depth;
251
252
253 GetNextProp:
254
255 pa = PropAddr(obj, p, offset);
256
257 defseg = proptable;
258
259 /* If the object doesn't have property p, see if there's a
260 default value.
261 */
262 if (!pa)
263 {
264 if (offset) goto NoMorePropMatches;
265
266 if (getpropaddress) /* if an &value */
267 g = 0;
268 else
269 g = PeekWord(p * 2 + 2);
270 }
271
272 else
273 {
274 /* Property is a value... */
275 if (Peek(pa+1) < PROP_ROUTINE)
276 {
277 if (getaddress || (int)Peek(pa+1) < n || n<=0)
278 {
279 #if defined (DEBUGGER)
280 if (n!=1)
281 CheckinRange(n, (int)Peek(pa+1), "property element");
282 #endif
283 g = 0;
284 }
285 else
286 g = PeekWord(pa + n * 2);
287 }
288
289 /* ...or a property routine */
290 else
291 {
292 /* Check if this is an additive property */
293 defseg = proptable;
294 if (Peek(2 + Peek(0)*2 + p)&ADDITIVE_FLAG)
295 isadditive = true;
296
297 /* If an &value, return the address of the
298 property routine.
299 */
300 if (getpropaddress)
301 {
302 g = PeekWord(pa+2);
303 goto NoMorePropMatches;
304 }
305 else
306 {
307 #if defined (DEBUGGER)
308 if (debug_eval)
309 {
310 debug_eval_error = true;
311 DebugMessageBox("Expression Error",
312 "Property routine illegal in watch/assignment");
313 defseg = gameseg;
314 return 0;
315 }
316 #endif
317 /* If not a complex property such as
318 before or after:
319 */
320 if ((game_version>=22 && (Peek(2 + Peek(0)*2 + p)&COMPLEX_FLAG)==0) || (game_version<22 && p!=before && p!=after))
321 {
322 ret = 1;
323 returnaddr = codeptr;
324
325 /* Check to see if this is a valid tail-recursive return... */
326 if (tail_recursion==TAIL_RECURSION_PROPERTY && MEM(codeptr)==EOL_T)
327 {
328 PassLocals(0);
329 tail_recursion_addr = (long)PeekWord(pa+2)*address_scale;
330 return 0;
331 }
332 /* ...but if we're not immediately followed by and end-of-line marker,
333 or another property value, cancel the pending tail-recursion
334 */
335 else if (MEM(codeptr)!=DECIMAL_T)
336 {
337 tail_recursion = 0;
338 }
339
340 for (i=0; i<MAXLOCALS; i++)
341 templocals[i] = var[MAXGLOBALS+i];
342
343 PassLocals(0);
344
345 SetStackFrame(stack_depth+1, RUNROUTINE_BLOCK, 0, 0);
346 #if defined (DEBUGGER)
347 tempdbnest = dbnest;
348 DebugRunRoutine((long)PeekWord(pa+2)*address_scale);
349 dbnest = tempdbnest;
350 #else
351 RunRoutine((long)PeekWord(pa+2)*address_scale);
352 #endif
353
354 retflag = 0;
355 codeptr = returnaddr;
356 g = ret;
357 ret = 0;
358 }
359
360 /* Complex property: */
361 else
362 {
363 for (i=0; i<MAXLOCALS; i++)
364 templocals[i] = var[MAXGLOBALS+i];
365
366 inprop = (long)PeekWord(pa + 2)*address_scale;
367 #ifdef DEBUGGER
368 orig_inprop = inprop;
369 #endif
370 defseg = gameseg;
371 while (Peek(inprop)!=CLOSE_BRACE_T)
372 {
373 returnaddr = codeptr;
374
375 codeptr = inprop;
376 objonly = false;
377 objtype = GetValue();
378 inprop = codeptr;
379 codeptr = returnaddr;
380
381 flag = 0;
382
383 /* If only an object (or other variable) is
384 given, and no verbroutine
385 */
386 if (Peek(inprop)==JUMP_T)
387 {
388 objonly = true;
389 if (!gotone && obj==objtype) flag = 1;
390 }
391
392 /* Otherwise, one or more verbroutines are
393 specified
394 */
395 else
396 {
397 while (Peek(inprop)!=JUMP_T)
398 {
399 if (PeekWord(inprop+1)==(unsigned int)var[verbroutine] ||
400
401 /* This is necessary because of the awkward way the pre-v2.2
402 differentiated non-verbroutine blocks, i.e., with Parse
403 */
404 ((game_version<22) && PeekWord(inprop+1)==(unsigned int)parseaddr && !gotone))
405 {
406 if (obj==objtype) flag = 1;
407 }
408 inprop += 3;
409 }
410 }
411
412 if (flag==1)
413 {
414 gotone = true;
415 ret = 1;
416 returnaddr = codeptr;
417
418 SetStackFrame(stack_depth, RUNROUTINE_BLOCK, 0, 0);
419
420 PassLocals(0);
421 #if defined (DEBUGGER)
422 /* Prevent premature stopping */
423 if (debugger_step_over && !debugger_finish)
424 debugger_run = true;
425
426 if (IsBreakpoint(orig_inprop))
427 complex_prop_breakpoint = true;
428
429 sprintf(debug_line, "Calling: %s.%s", objectname[obj], propertyname[p]);
430 trace_complex_prop_routine = true;
431
432 tempdbnest = dbnest;
433 DebugRunRoutine(inprop+3);
434 dbnest = tempdbnest;
435 #else
436 RunRoutine(inprop+3);
437 #endif
438 retflag = 0;
439 codeptr = returnaddr;
440 g = ret;
441 ret = 0;
442 }
443
444 /* The following used to read "if (!flag || objonly..."
445 meaning that any non-verbroutine related routines
446 would fall through regardless of whether they returned
447 true or false. I don't recall the rationale for this,
448 and have therefore removed it.
449 */
450 if (!flag || (objonly && !g) || ((game_version<22) && PeekWord(inprop-2)==(unsigned int)parseaddr))
451 inprop = (long)PeekWord(inprop+1)*address_scale;
452 else break;
453 }
454 }
455
456 for (i=0; i<MAXLOCALS; i++)
457 var[MAXGLOBALS+i] = templocals[i];
458
459 if (isadditive && !g)
460 {
461 offset = pa + 4;
462 gotone = false;
463 goto GetNextProp;
464 }
465 }
466 }
467 }
468
469 NoMorePropMatches:
470
471 defseg = gameseg;
472
473 var[self] = tempself;
474 inexpr = tempinexpr;
475 stack_depth = temp_stack_depth;
476
477 return g;
478 }
479
480
481 /* GRANDPARENT
482
483 Returns the value of the last object above <obj> in the tree
484 before object 0.
485 */
486
487 int GrandParent(int obj)
488 {
489 int nextobj;
490
491 #if defined (DEBUGGER)
492 if (!CheckObjectRange(obj)) return 0;
493 #endif
494 if (obj<0 || obj>=objects) return 0;
495
496 defseg = objtable;
497 while ((nextobj = PeekWord(2 + obj*object_size + object_size-8)) != 0)
498 obj = nextobj;
499 defseg = gameseg;
500
501 return obj;
502 }
503
504
505 /* MOVEOBJ */
506
507 void MoveObj(int obj, int p)
508 {
509 int oldparent, prevobj, s;
510 unsigned int objaddr, parentaddr, lastobjaddr;
511
512 if (obj==p) return;
513 if (obj<0 || obj>=objects) return;
514
515 oldparent = Parent(obj);
516 /* if (oldparent==p) return; */
517
518 objaddr = 2 + obj*object_size;
519
520 /* First, detach the object from its old parent and siblings... */
521
522 prevobj = Elder(obj);
523 s = Sibling(obj);
524 defseg = objtable;
525 if (prevobj) /* sibling */
526 PokeWord(2 + prevobj*object_size + object_size-6, s);
527 else /* child */
528 PokeWord(2 + oldparent*object_size + object_size-4, s);
529
530
531 /* Then move it to the new parent... */
532
533 defseg = objtable;
534 PokeWord(objaddr + object_size-8, p); /* new parent */
535 PokeWord(objaddr + object_size-6, 0); /* erase old sibling */
536
537 /* Only operate on the new parent if it isn't object 0 */
538 if (p!=0)
539 {
540
541 /* Object is sole child, or... */
542 if (Child(p)==0)
543 {
544 parentaddr = 2 + p*object_size;
545 defseg = objtable;
546 PokeWord(parentaddr + object_size-4, obj);
547 }
548
549 /* ...object is next sibling. */
550 else
551 {
552 lastobjaddr = 2 + Youngest(p)*object_size;
553 defseg = objtable;
554 PokeWord(lastobjaddr + object_size-6, obj);
555 }
556 }
557 }
558
559
560 /* NAME */
561
562 char *Name(int obj)
563 {
564 int p;
565
566 p = GetProp(obj, 0, 1, 0);
567
568 if (p)
569 return GetWord((unsigned int)p);
570 else
571 return 0;
572 }
573
574
575 /* PARENT */
576
577 int Parent(int obj)
578 {
579 int p;
580
581 #if defined (DEBUGGER)
582 if (!CheckObjectRange(obj)) return 0;
583 #endif
584 if (obj<0 || obj>=objects) return 0;
585
586 defseg = objtable;
587
588 p = PeekWord(2 + obj*object_size + object_size-8);
589
590 defseg = gameseg;
591
592 return p;
593 }
594
595
596 /* PROPADDR
597
598 Returns address of <obj>.<p> (with <offset> provided for additive
599 properties--i.e. subsequent calls with the same <obj> and <p>.
600 */
601
602 unsigned int PropAddr(int obj, int p, unsigned int offset)
603 {
604 unsigned char c;
605 int proplen;
606 unsigned int ptr;
607
608 #if defined (DEBUGGER)
609 /* Don't check any non-existent display object (-1) */
610 if (p!=-1) CheckinRange(p, properties, "property");
611 CheckObjectRange(obj);
612 #endif
613 /* This way either -1 (the non-existent display object) or a too-high
614 object will fail
615 */
616 if (obj<0 || obj>=objects) return 0;
617
618 defseg = objtable;
619
620 /* Position in the property table...
621
622 i.e., ptr = PeekWord(2 + obj*object_size + (object_size-2));
623 */
624 ptr = PeekWord(object_size*(obj+1));
625
626 /* ...unless a position has already been given */
627 if (offset) ptr = offset;
628
629 defseg = proptable;
630
631 c = Peek(ptr);
632 while (c != PROP_END && c != (unsigned char)p)
633 {
634 proplen = Peek(ptr + 1);
635
636 /* Property routine address is 1 word */
637 if (proplen==PROP_ROUTINE) proplen = 1;
638
639 ptr += proplen * 2 + 2;
640 c = Peek(ptr);
641 }
642
643 defseg = gameseg;
644
645 if (c==PROP_END)
646 return 0;
647 else
648 return ptr;
649 }
650
651
652 /* PUTATTRIBUTES
653
654 Writes (puts) one of four sets of 32 attributes.
655 */
656
657 void PutAttributes(int obj, unsigned long a, int attribute_set)
658 {
659 unsigned int lword, hword;
660
661 hword = (unsigned int)(a/65536L);
662 lword = (unsigned int)(a%65536L);
663
664 defseg = objtable;
665
666 PokeWord(2 + obj*object_size + attribute_set*4, lword);
667 PokeWord(2 + obj*object_size + attribute_set*4 + 2, hword);
668
669 defseg = gameseg;
670 }
671
672
673 /* SETATTRIBUTE */
674
675 void SetAttribute(int obj, int attr, int c) /* c = 1 for set, 0 for clear */
676 {
677 unsigned long a, mask;
678
679 #if defined (DEBUGGER)
680 if (!CheckObjectRange(obj)) return;
681 #endif
682 if (obj<0 || obj>=objects) return;
683
684 a = GetAttributes(obj, attr/32);
685
686 mask = 1L<<(long)(attr%32);
687
688 if (c==1)
689 a = a | mask;
690 else
691 {
692 if (a & mask)
693 a = a ^ mask;
694 }
695
696 PutAttributes(obj, a, attr/32);
697 }
698
699
700 /* SIBLING */
701
702 int Sibling(int obj)
703 {
704 int s;
705
706 #if defined (DEBUGGER)
707 if (!CheckObjectRange(obj)) return 0;
708 #endif
709 if (obj<0 || obj>=objects) return 0;
710
711 defseg = objtable;
712
713 s = PeekWord(2+ obj*object_size + object_size-6);
714
715 defseg = gameseg;
716
717 return s;
718 }
719
720
721 /* TESTATTRIBUTE */
722
723 int TestAttribute(int obj, int attr, int nattr)
724 {
725 unsigned long a, mask, ta;
726
727 #if defined (DEBUGGER)
728 if (!CheckObjectRange(obj)) return 0;
729 #endif
730 if (obj<0 || obj>=objects) return 0;
731
732 a = GetAttributes(obj, attr/32);
733
734 mask = 1L<<(attr%32);
735
736 ta = a & mask;
737 if (ta) ta = 1;
738
739 if (nattr) ta = ta ^ 1;
740
741 return (int)ta;
742 }
743
744
745 /* YOUNGEST */
746
747 int Youngest(int obj)
748 {
749 int nextobj;
750
751 if (Child(obj)==0) return 0;
752
753 nextobj = Child(obj);
754
755 while (Sibling(nextobj))
756 nextobj = Sibling(nextobj);
757
758 return nextobj;
759 }