]>
git.zerfleddert.de Git - micropolis/blob - src/sim/w_sprite.c
3 * Micropolis, Unix Version. This game was released for the Unix platform
4 * in or about 1990 and has been modified for inclusion in the One Laptop
5 * Per Child program. Copyright (C) 1989 - 2007 Electronic Arts Inc. If
6 * you need assistance with this program, you may contact:
7 * http://wiki.laptop.org/go/Micropolis or email micropolis@laptop.org.
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or (at
12 * your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details. You should have received a
18 * copy of the GNU General Public License along with this program. If
19 * not, see <http://www.gnu.org/licenses/>.
21 * ADDITIONAL TERMS per GNU GPL Section 7
23 * No trademark or publicity rights are granted. This license does NOT
24 * give you any right, title or interest in the trademark SimCity or any
25 * other Electronic Arts trademark. You may not distribute any
26 * modification of this program using the trademark SimCity or claim any
27 * affliation or association with Electronic Arts Inc. or its employees.
29 * Any propagation or conveyance of this program must include this
30 * copyright notice and these terms.
32 * If you convey this program (or any modifications of it) and assume
33 * contractual liability for the program to recipients of it, you agree
34 * to indemnify Electronic Arts for any liability that those contractual
35 * assumptions impose on Electronic Arts.
37 * You may not misrepresent the origins of this program; modified
38 * versions of the program must be marked as such and not identified as
39 * the original program.
41 * This disclaimer supplements the one included in the General Public
42 * License. TO THE FULLEST EXTENT PERMISSIBLE UNDER APPLICABLE LAW, THIS
43 * PROGRAM IS PROVIDED TO YOU "AS IS," WITH ALL FAULTS, WITHOUT WARRANTY
44 * OF ANY KIND, AND YOUR USE IS AT YOUR SOLE RISK. THE ENTIRE RISK OF
45 * SATISFACTORY QUALITY AND PERFORMANCE RESIDES WITH YOU. ELECTRONIC ARTS
46 * DISCLAIMS ANY AND ALL EXPRESS, IMPLIED OR STATUTORY WARRANTIES,
47 * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY, SATISFACTORY QUALITY,
48 * FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT OF THIRD PARTY
49 * RIGHTS, AND WARRANTIES (IF ANY) ARISING FROM A COURSE OF DEALING,
50 * USAGE, OR TRADE PRACTICE. ELECTRONIC ARTS DOES NOT WARRANT AGAINST
51 * INTERFERENCE WITH YOUR ENJOYMENT OF THE PROGRAM; THAT THE PROGRAM WILL
52 * MEET YOUR REQUIREMENTS; THAT OPERATION OF THE PROGRAM WILL BE
53 * UNINTERRUPTED OR ERROR-FREE, OR THAT THE PROGRAM WILL BE COMPATIBLE
54 * WITH THIRD PARTY SOFTWARE OR THAT ANY ERRORS IN THE PROGRAM WILL BE
55 * CORRECTED. NO ORAL OR WRITTEN ADVICE PROVIDED BY ELECTRONIC ARTS OR
56 * ANY AUTHORIZED REPRESENTATIVE SHALL CREATE A WARRANTY. SOME
57 * JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF OR LIMITATIONS ON IMPLIED
58 * WARRANTIES OR THE LIMITATIONS ON THE APPLICABLE STATUTORY RIGHTS OF A
59 * CONSUMER, SO SOME OR ALL OF THE ABOVE EXCLUSIONS AND LIMITATIONS MAY
65 Tcl_HashTable SpriteCmds
;
70 SimSprite
*GlobalSprites
[OBJN
];
72 SimSprite
*NewSprite(char *name
, int type
, int x
, int y
);
75 #define TRA_GROOVE_X -39
76 #define TRA_GROOVE_Y 6
77 #define BUS_GROOVE_X -39
78 #define BUS_GROOVE_Y 6
80 #define SPRITECMD_ACCESS_INT(var) \
81 int SpriteCmd##var(SPRITE_ARGS) { \
83 if ((argc != 2) && (argc != 3)) return (TCL_ERROR); \
85 if (Tcl_GetInt(interp, argv[2], &val) != TCL_OK) return (TCL_ERROR); \
88 sprintf(interp->result, "%d", sprite->var); \
93 #define SPRITECMD_GET_STR(var) \
94 int SpriteCmd##var(SPRITE_ARGS) { \
95 sprintf(interp->result, "%s", sprite->var); \
101 DoSpriteCmd(CLIENT_ARGS
)
103 SimSprite
*sprite
= (SimSprite
*) clientData
;
112 if (ent
= Tcl_FindHashEntry(&SpriteCmds
, argv
[1])) {
113 cmd
= (int (*)())ent
->clientData
;
114 Tk_Preserve((ClientData
) sprite
);
115 result
= cmd(sprite
, interp
, argc
, argv
);
116 Tk_Release((ClientData
) sprite
);
118 Tcl_AppendResult(interp
, "unknown command name: \"",
119 argv
[0], " ", argv
[1], "\".", (char *) NULL
);
127 SpriteCmd(CLIENT_ARGS
)
133 (Tcl_GetInt(interp
, argv
[2], &type
) != TCL_OK
) ||
134 (type
< 1) || (type
>= OBJN
)) {
138 sprite
= NewSprite(argv
[1], type
, 0, 0);
141 Tcl_CreateCommand(interp
, sprite
->name
,
142 DoSpriteCmd
, (ClientData
) sprite
, (void (*)()) NULL
);
144 interp
->result
= sprite
->name
;
149 SPRITECMD_GET_STR(name
)
150 SPRITECMD_ACCESS_INT(type
)
151 SPRITECMD_ACCESS_INT(frame
)
152 SPRITECMD_ACCESS_INT(x
)
153 SPRITECMD_ACCESS_INT(y
)
154 SPRITECMD_ACCESS_INT(width
)
155 SPRITECMD_ACCESS_INT(height
)
156 SPRITECMD_ACCESS_INT(x_offset
)
157 SPRITECMD_ACCESS_INT(y_offset
)
158 SPRITECMD_ACCESS_INT(x_hot
)
159 SPRITECMD_ACCESS_INT(y_hot
)
160 SPRITECMD_ACCESS_INT(orig_x
)
161 SPRITECMD_ACCESS_INT(orig_y
)
162 SPRITECMD_ACCESS_INT(dest_x
)
163 SPRITECMD_ACCESS_INT(dest_y
)
164 SPRITECMD_ACCESS_INT(count
)
165 SPRITECMD_ACCESS_INT(sound_count
)
166 SPRITECMD_ACCESS_INT(dir
)
167 SPRITECMD_ACCESS_INT(new_dir
)
168 SPRITECMD_ACCESS_INT(step
)
169 SPRITECMD_ACCESS_INT(flag
)
170 SPRITECMD_ACCESS_INT(control
)
171 SPRITECMD_ACCESS_INT(turn
)
172 SPRITECMD_ACCESS_INT(accel
)
173 SPRITECMD_ACCESS_INT(speed
)
176 int SpriteCmdExplode(SPRITE_ARGS
)
178 ExplodeSprite(sprite
);
183 int SpriteCmdInit(SPRITE_ARGS
)
190 if ((Tcl_GetInt(interp
, argv
[2], &x
) != TCL_OK
) ||
191 (x
< 0) || (x
>= (WORLD_X
<<4)) ||
192 (Tcl_GetInt(interp
, argv
[3], &y
) != TCL_OK
) ||
193 (y
< 0) || (y
>= (WORLD_Y
<<4))) {
196 InitSprite(sprite
, x
, y
);
201 sprite_command_init()
205 Tcl_CreateCommand(tk_mainInterp
, "sprite", SpriteCmd
,
206 (ClientData
)NULL
, (void (*)()) NULL
);
208 Tcl_InitHashTable(&SpriteCmds
, TCL_STRING_KEYS
);
210 #define SPRITE_CMD(cmd) HASHED_CMD(Sprite, cmd)
219 SPRITE_CMD(x_offset
);
220 SPRITE_CMD(y_offset
);
228 SPRITE_CMD(sound_count
);
240 for (i
= 0; i
< OBJN
; i
++) {
241 GlobalSprites
[i
] = NULL
;
246 SimSprite
*FreeSprites
= NULL
;
249 NewSprite(char *name
, int type
, int x
, int y
)
254 sprite
= FreeSprites
;
255 FreeSprites
= sprite
->next
;
257 sprite
= (SimSprite
*)ckalloc(sizeof (SimSprite
));
260 sprite
->name
= (char *)ckalloc(strlen(name
) + 1);
261 strcpy(sprite
->name
, name
);
264 InitSprite(sprite
, x
, y
);
266 sim
->sprites
++; sprite
->next
= sim
->sprite
; sim
->sprite
= sprite
;
272 InitSprite(SimSprite
*sprite
, int x
, int y
)
274 sprite
->x
= x
; sprite
->y
= y
;
276 sprite
->orig_x
= sprite
->orig_y
= 0;
277 sprite
->dest_x
= sprite
->dest_y
= 0;
278 sprite
->count
= sprite
->sound_count
= 0;
279 sprite
->dir
= sprite
->new_dir
= 0;
280 sprite
->step
= sprite
->flag
= 0;
281 sprite
->control
= -1;
286 if (GlobalSprites
[sprite
->type
] == NULL
) {
287 GlobalSprites
[sprite
->type
] = sprite
;
290 switch (sprite
->type
) {
293 sprite
->width
= sprite
->height
= 32;
294 sprite
->x_offset
= 32; sprite
->y_offset
= -16;
295 sprite
->x_hot
= 40; sprite
->y_hot
= -8;
301 sprite
->width
= sprite
->height
= 48;
302 sprite
->x_offset
= 32; sprite
->y_offset
= -16;
303 sprite
->x_hot
= 48; sprite
->y_hot
= 0;
304 if (x
< (4 <<4)) sprite
->frame
= 3;
305 else if (x
>= ((WORLD_X
- 4) <<4)) sprite
->frame
= 7;
306 else if (y
< (4 <<4)) sprite
->frame
= 5;
307 else if (y
>= ((WORLD_Y
- 4) <<4)) sprite
->frame
= 1;
308 else sprite
->frame
= 3;
309 sprite
->new_dir
= sprite
->frame
;
315 sprite
->width
= sprite
->height
= 48;
316 sprite
->x_offset
= 24; sprite
->y_offset
= 0;
317 sprite
->x_hot
= 40; sprite
->y_hot
= 16;
318 if (x
> ((WORLD_X
<<4) / 2)) {
319 if (y
> ((WORLD_Y
<<4) / 2)) sprite
->frame
= 10;
320 else sprite
->frame
= 7;
321 } else if (y
> ((WORLD_Y
<<4) / 2)) sprite
->frame
= 1;
322 else sprite
->frame
= 4;
323 sprite
->count
= 1000;
324 sprite
->dest_x
= PolMaxX
<<4;
325 sprite
->dest_y
= PolMaxY
<<4;
326 sprite
->orig_x
= sprite
->x
;
327 sprite
->orig_y
= sprite
->y
;
331 sprite
->width
= sprite
->height
= 32;
332 sprite
->x_offset
= 32; sprite
->y_offset
= -16;
333 sprite
->x_hot
= 40; sprite
->y_hot
= -8;
335 sprite
->count
= 1500;
336 sprite
->dest_x
= Rand((WORLD_X
<<4) - 1);
337 sprite
->dest_y
= Rand((WORLD_Y
<<4) - 1);
338 sprite
->orig_x
= x
- 30;
343 sprite
->width
= sprite
->height
= 48;
344 sprite
->x_offset
= 24; sprite
->y_offset
= 0;
345 sprite
->x_hot
= 48; sprite
->y_hot
= 16;
346 if (x
> ((WORLD_X
- 20) <<4)) {
347 sprite
->x
-= 100 + 48;
348 sprite
->dest_x
= sprite
->x
- 200;
351 sprite
->dest_x
= sprite
->x
+ 200;
354 sprite
->dest_y
= sprite
->y
;
358 sprite
->width
= sprite
->height
= 48;
359 sprite
->x_offset
= 24; sprite
->y_offset
= 0;
360 sprite
->x_hot
= 40; sprite
->y_hot
= 36;
366 sprite
->width
= sprite
->height
= 48;
367 sprite
->x_offset
= 24; sprite
->y_offset
= 0;
368 sprite
->x_hot
= 40; sprite
->y_hot
= 16;
373 sprite
->width
= sprite
->height
= 32;
374 sprite
->x_offset
= 30; sprite
->y_offset
= -18;
375 sprite
->x_hot
= 40; sprite
->y_hot
= -8;
388 for (sprite
= sim
->sprite
; sprite
!= NULL
; sprite
= sprite
->next
) {
394 DestroySprite(SimSprite
*sprite
)
399 for (view
= sim
->editor
; view
!= NULL
; view
= view
->next
)
400 if (view
->follow
== sprite
)
403 if (GlobalSprites
[sprite
->type
] == sprite
) {
404 GlobalSprites
[sprite
->type
] = (SimSprite
*)NULL
;
407 if (sprite
->name
!= NULL
) {
408 ckfree(sprite
->name
);
412 for (sp
= &sim
->sprite
; *sp
!= NULL
; sp
= &((*sp
)->next
)) {
413 if (sprite
== (*sp
)) {
419 sprite
->next
= FreeSprites
;
420 FreeSprites
= sprite
;
429 if (((sprite
= GlobalSprites
[type
]) == NULL
) ||
430 (sprite
->frame
== 0))
431 return (SimSprite
*)NULL
;
438 MakeSprite(int type
, int x
, int y
)
442 if ((sprite
= GlobalSprites
[type
]) == NULL
) {
443 sprite
= NewSprite("", type
, x
, y
);
445 InitSprite(sprite
, x
, y
);
452 MakeNewSprite(int type
, int x
, int y
)
456 sprite
= NewSprite("", type
, x
, y
);
461 DrawObjects(SimView
*view
)
465 /* XXX: sort these by layer */
467 if (z = Oframe[TRA]) DrawTrain(view, z);
468 if (z = Oframe[SHI]) DrawBoat(view, z);
469 if (z = Oframe[GOD]) DrawMonster(view, z);
470 if (z = Oframe[COP]) DrawCopter(view, z);
471 if (z = Oframe[AIR]) DrawPlane(view, z);
472 if (z = Oframe[TOR]) DrawTor(view, z);
473 if (z = Oframe[EXP]) DrawExp(view, z);
476 for (sprite
= sim
->sprite
; sprite
!= NULL
; sprite
= sprite
->next
) {
477 DrawSprite(view
, sprite
);
482 DrawSprite(SimView
*view
, SimSprite
*sprite
)
487 if (sprite
->frame
== 0)
490 i
= (sprite
->frame
- 1) * 2;
491 pict
= view
->x
->objects
[sprite
->type
][i
];
492 mask
= view
->x
->objects
[sprite
->type
][i
+ 1];
495 - ((view
->tile_x
<<4) - view
->screen_x
)
498 - ((view
->tile_y
<<4) - view
->screen_y
)
501 XSetClipMask(view
->x
->dpy
, view
->x
->gc
, mask
);
502 XSetClipOrigin(view
->x
->dpy
, view
->x
->gc
, x
, y
);
503 XCopyArea(view
->x
->dpy
, pict
, view
->pixmap2
, view
->x
->gc
,
504 0, 0, sprite
->width
, sprite
->height
, x
, y
);
505 XSetClipMask(view
->x
->dpy
, view
->x
->gc
, None
);
506 XSetClipOrigin(view
->x
->dpy
, view
->x
->gc
, 0, 0);
510 short GetChar(int x
, int y
)
514 if (!TestBounds(x
, y
))
517 return(Map
[x
][y
] & LOMASK
);
521 short TurnTo(int p
, int d
)
523 if (p
== d
) return(p
);
525 if ((d
- p
) < 4) p
++;
528 if ((p
- d
) < 4) p
--;
536 TryOther(int Tpoo
, int Told
, int Tnew
)
542 if (Tnew
!= z
) return(0);
543 if ((Tpoo
== POWERBASE
) || (Tpoo
== POWERBASE
+ 1) ||
544 (Tpoo
== RAILBASE
) || (Tpoo
== RAILBASE
+ 1))
550 short SpriteNotInBounds(SimSprite
*sprite
)
552 int x
= sprite
->x
+ sprite
->x_hot
;
553 int y
= sprite
->y
+ sprite
->y_hot
;
555 if ((x
< 0) || (y
< 0) ||
556 (x
>= (WORLD_X
<<4)) ||
557 (y
>= (WORLD_Y
<<4))) {
564 short GetDir(int orgX
, int orgY
, int desX
, int desY
)
566 static short Gdtab
[13] = { 0, 3, 2, 1, 3, 4, 5, 7, 6, 5, 7, 8, 1 };
572 if (dispY
< 0) z
= 11;
575 if (dispY
< 0) z
= 2;
577 if (dispX
< 0) dispX
= -dispX
;
578 if (dispY
< 0) dispY
= -dispY
;
580 absDist
= dispX
+ dispY
;
582 if ((dispX
<<1) < dispY
) z
++;
583 else if ((dispY
<<1) < dispY
) z
--;
585 if ((z
< 0) || (z
> 12)) z
= 0;
591 GetDis(int x1
, int y1
, int x2
, int y2
)
593 register short dispX
, dispY
;
595 if (x1
> x2
) dispX
= x1
- x2
;
596 else dispX
= x2
- x1
;
597 if (y1
> y2
) dispY
= y1
- y2
;
598 else dispY
= y2
- y1
;
600 return (dispX
+ dispY
);
604 int CheckSpriteCollision(SimSprite
*s1
, SimSprite
*s2
)
606 if ((s1
->frame
!= 0) && (s2
->frame
!= 0) &&
607 GetDis(s1
->x
+ s1
->x_hot
, s1
->y
+ s1
->y_hot
,
608 s2
->x
+ s2
->x_hot
, s2
->y
+ s2
->y_hot
) < 30)
618 if (!SimSpeed
) return;
621 for (sprite
= sim
->sprite
; sprite
!= NULL
;) {
623 switch (sprite
->type
) {
625 DoTrainSprite(sprite
);
628 DoCopterSprite(sprite
);
631 DoAirplaneSprite(sprite
);
634 DoShipSprite(sprite
);
637 DoMonsterSprite(sprite
);
640 DoTornadoSprite(sprite
);
643 DoExplosionSprite(sprite
);
649 sprite
= sprite
->next
;
651 if (sprite
->name
[0] == '\0') {
652 SimSprite
*s
= sprite
;
653 sprite
= sprite
->next
;
656 sprite
= sprite
->next
;
663 DoTrainSprite(SimSprite
*sprite
)
665 static short Cx
[4] = { 0, 16, 0, -16 };
666 static short Cy
[4] = { -16, 0, 16, 0 };
667 static short Dx
[5] = { 0, 4, 0, -4, 0 };
668 static short Dy
[5] = { -4, 0, 4, 0, 0 };
669 static short TrainPic2
[5] = { 1, 2, 1, 2, 5 };
670 register short z
, dir
, dir2
;
673 if ((sprite
->frame
== 3) || (sprite
->frame
== 4))
674 sprite
->frame
= TrainPic2
[sprite
->dir
];
675 sprite
->x
+= Dx
[sprite
->dir
];
676 sprite
->y
+= Dy
[sprite
->dir
];
679 for (z
= dir
; z
< (dir
+ 4); z
++) {
681 if (sprite
->dir
!= 4) {
682 if (dir2
== ((sprite
->dir
+ 2) & 3)) continue;
684 c
= GetChar(sprite
->x
+ Cx
[dir2
] + 48,
685 sprite
->y
+ Cy
[dir2
]);
686 if (((c
>= RAILBASE
) && (c
<= LASTRAIL
)) || /* track? */
687 (c
== RAILVPOWERH
) ||
688 (c
== RAILHPOWERV
)) {
689 if ((sprite
->dir
!= dir2
) &&
690 (sprite
->dir
!= 4)) {
691 if ((sprite
->dir
+ dir2
) == 3)
696 sprite
->frame
= TrainPic2
[dir2
];
698 if ((c
== RAILBASE
) || (c
== (RAILBASE
+ 1)))
704 if (sprite
->dir
== 4) {
713 DoCopterSprite(SimSprite
*sprite
)
715 static short CDx
[9] = { 0, 0, 3, 5, 3, 0, -3, -5, -3 };
716 static short CDy
[9] = { 0, -5, -3, 0, 3, 5, 3, 0, -3 };
717 register short z
, d
, x
, y
;
719 if (sprite
->sound_count
> 0) sprite
->sound_count
--;
721 if (sprite
->control
< 0) {
723 if (sprite
->count
> 0) sprite
->count
--;
725 if (!sprite
->count
) {
726 /* Attract copter to monster and tornado so it blows up more often */
727 SimSprite
*s
= GetSprite(GOD
);
729 sprite
->dest_x
= s
->x
;
730 sprite
->dest_y
= s
->y
;
734 sprite
->dest_x
= s
->x
;
735 sprite
->dest_y
= s
->y
;
737 sprite
->dest_x
= sprite
->orig_x
;
738 sprite
->dest_y
= sprite
->orig_y
;
742 if (!sprite
->count
) { /* land */
743 GetDir(sprite
->x
, sprite
->y
, sprite
->orig_x
, sprite
->orig_y
);
750 GetDir(sprite
->x
, sprite
->y
, sprite
->dest_x
, sprite
->dest_y
);
752 sprite
->dest_x
= sprite
->orig_x
;
753 sprite
->dest_y
= sprite
->orig_y
;
754 sprite
->control
= -1;
758 if (!sprite
->sound_count
) { /* send report */
759 x
= (sprite
->x
+ 48) >>5;
762 (x
< (WORLD_X
>>1)) &&
764 (y
< (WORLD_Y
>>1))) {
765 /* Don changed from 160 to 170 to shut the #$%#$% thing up! */
766 if ((TrfDensity
[x
][y
] > 170) && ((Rand16() & 7) == 0)) {
767 SendMesAt(-41, (x
<<1) + 1, (y
<<1) + 1);
768 MakeSound("city", "HeavyTraffic"); /* chopper */
769 sprite
->sound_count
= 200;
775 d
= GetDir(sprite
->x
, sprite
->y
, sprite
->dest_x
, sprite
->dest_y
);
785 DoAirplaneSprite(SimSprite
*sprite
)
787 static short CDx
[12] = { 0, 0, 6, 8, 6, 0, -6, -8, -6, 8, 8, 8 };
788 static short CDy
[12] = { 0, -8, -6, 0, 6, 8, 6, 0, -6, 0, 0, 0 };
795 if (z
> 8) { /* TakeOff */
799 } else { /* goto destination */
800 d
= GetDir(sprite
->x
, sprite
->y
, sprite
->dest_x
, sprite
->dest_y
);
806 if (absDist
< 50) { /* at destination */
807 sprite
->dest_x
= Rand((WORLD_X
* 16) + 100) - 50;
808 sprite
->dest_y
= Rand((WORLD_Y
* 16) + 100) - 50;
811 /* deh added test for !Disasters */
816 for (s
= sim
->sprite
; s
!= NULL
; s
= s
->next
) {
817 if ((s
->frame
!= 0) &&
820 (s
->type
== AIR
))) &&
821 CheckSpriteCollision(sprite
, s
)) {
827 ExplodeSprite(sprite
);
832 if (SpriteNotInBounds(sprite
)) sprite
->frame
= 0;
836 DoShipSprite(SimSprite
*sprite
)
838 static short BDx
[9] = { 0, 0, 1, 1, 1, 0, -1, -1, -1 };
839 static short BDy
[9] = { 0, -1, -1, 0, 1, 1, 1, 0, -1 };
840 static short BPx
[9] = { 0, 0, 2, 2, 2, 0, -2, -2, -2 };
841 static short BPy
[9] = { 0, -2, -2, 0, 2, 2, 2, 0, -2 };
842 static short BtClrTab
[8] = { RIVER
, CHANNEL
, POWERBASE
, POWERBASE
+ 1,
843 RAILBASE
, RAILBASE
+ 1, BRWH
, BRWV
};
844 register short x
, y
, z
, t
= RIVER
;
847 if (sprite
->sound_count
> 0) sprite
->sound_count
--;
848 if (!sprite
->sound_count
) {
849 if ((Rand16() & 3) == 1) {
850 if ((ScenarioID
== 2) && /* San Francisco */
852 MakeSound("city", "HonkHonk-Low");
854 MakeSound("city", "HonkHonk-Low");
857 sprite
->sound_count
= 200;
860 if (sprite
->count
> 0) sprite
->count
--;
861 if (!sprite
->count
) {
863 if (sprite
->frame
!= sprite
->new_dir
) {
864 sprite
->frame
= TurnTo(sprite
->frame
, sprite
->new_dir
);
868 for (pem
= tem
; pem
< (tem
+ 8); pem
++) {
871 if (z
== sprite
->dir
) continue;
872 x
= ((sprite
->x
+ (48 - 1)) >>4) + BDx
[z
];
873 y
= (sprite
->y
>>4) + BDy
[z
];
874 if (TestBounds(x
, y
)) {
875 t
= Map
[x
][y
] & LOMASK
;
876 if ((t
== CHANNEL
) || (t
== BRWH
) || (t
== BRWV
) ||
877 TryOther(t
, sprite
->dir
, z
)) {
879 sprite
->frame
= TurnTo(sprite
->frame
, sprite
->new_dir
);
881 if (sprite
->dir
> 8) sprite
->dir
-= 8;
886 if (pem
== (tem
+ 8)) {
888 sprite
->new_dir
= (Rand16() & 7) + 1;
892 if (z
== sprite
->new_dir
) {
897 if (SpriteNotInBounds(sprite
)) {
902 for (z
= 0; z
< 8; z
++) {
903 if (t
== BtClrTab
[z
]) break;
905 ExplodeSprite(sprite
);
906 Destroy(sprite
->x
+ 48, sprite
->y
);
913 DoMonsterSprite(SimSprite
*sprite
)
915 static short Gx
[5] = { 2, 2, -2, -2, 0 };
916 static short Gy
[5] = { -2, 2, 2, -2, 0 };
917 static short ND1
[4] = { 0, 1, 2, 3 };
918 static short ND2
[4] = { 1, 2, 3, 0 };
919 static short nn1
[4] = { 2, 5, 8, 11 };
920 static short nn2
[4] = { 11, 2, 5, 8 };
921 register short d
, z
, c
;
923 if (sprite
->sound_count
> 0) sprite
->sound_count
--;
925 if (sprite
->control
< 0) {
926 /* business as usual */
928 if (sprite
->control
== -2) {
929 d
= (sprite
->frame
- 1) / 3;
930 z
= (sprite
->frame
- 1) % 3;
931 if (z
== 2) sprite
->step
= 0;
932 if (z
== 0) sprite
->step
= 1;
933 if (sprite
->step
) z
++;
935 c
= GetDir(sprite
->x
, sprite
->y
, sprite
->dest_x
, sprite
->dest_y
);
937 sprite
->control
= -1;
938 sprite
->count
= 1000;
940 sprite
->dest_x
= sprite
->orig_x
;
941 sprite
->dest_y
= sprite
->orig_y
;
944 if (((c
!= d
) && (!Rand(5))) ||
946 int diff
= (c
- d
) & 3;
947 if ((diff
== 1) || (diff
== 3)) {
950 if (Rand16() & 1) d
++; else d
--;
955 if (Rand16() & 1) d
++; else d
--;
962 d
= (sprite
->frame
- 1) / 3;
964 if (d
< 4) { /* turn n s e w */
965 z
= (sprite
->frame
- 1) % 3;
966 if (z
== 2) sprite
->step
= 0;
967 if (z
== 0) sprite
->step
= 1;
968 if (sprite
->step
) z
++;
970 GetDir(sprite
->x
, sprite
->y
, sprite
->dest_x
, sprite
->dest_y
);
972 if (sprite
->flag
== 0) {
974 sprite
->dest_x
= sprite
->orig_x
;
975 sprite
->dest_y
= sprite
->orig_y
;
981 c
= GetDir(sprite
->x
, sprite
->y
, sprite
->dest_x
, sprite
->dest_y
);
983 if ((c
!= d
) && (!Rand(10))) {
984 if (Rand16() & 1) z
= ND1
[d
];
987 if (!sprite
->sound_count
) {
988 MakeSound("city", "Monster"); /* monster */
989 sprite
->sound_count
= 50 + Rand(100);
996 if (!(Rand16() & 3)) {
997 if (Rand16() & 1) z
= nn1
[z
];
1005 /* somebody's taken control of the monster */
1007 d
= sprite
->control
;
1008 z
= (sprite
->frame
- 1) % 3;
1010 if (z
== 2) sprite
->step
= 0;
1011 if (z
== 0) sprite
->step
= 1;
1012 if (sprite
->step
) z
++;
1016 z
= (((d
* 3) + z
) + 1);
1023 if (sprite
->count
> 0) sprite
->count
--;
1024 c
= GetChar(sprite
->x
+ sprite
->x_hot
, sprite
->y
+ sprite
->y_hot
);
1027 (sprite
->count
!= 0) &&
1028 (sprite
->control
== -1))) {
1029 sprite
->frame
= 0; /* kill zilla */
1033 for (s
= sim
->sprite
; s
!= NULL
; s
= s
->next
) {
1034 if ((s
->frame
!= 0) &&
1035 ((s
->type
== AIR
) ||
1038 (s
->type
== TRA
)) &&
1039 CheckSpriteCollision(sprite
, s
)) {
1045 Destroy(sprite
->x
+ 48, sprite
->y
+ 16);
1049 DoTornadoSprite(SimSprite
*sprite
)
1051 static short CDx
[9] = { 2, 3, 2, 0, -2, -3 };
1052 static short CDy
[9] = { -2, 0, 2, 3, 2, 0 };
1057 if (z
== 2) /* cycle animation... post Rel */
1070 if (sprite
->count
> 0) sprite
->count
--;
1075 for (s
= sim
->sprite
; s
!= NULL
; s
= s
->next
) {
1076 if ((s
->frame
!= 0) &&
1077 ((s
->type
== AIR
) ||
1080 (s
->type
== TRA
)) &&
1081 CheckSpriteCollision(sprite
, s
)) {
1088 sprite
->x
+= CDx
[z
];
1089 sprite
->y
+= CDy
[z
];
1090 if (SpriteNotInBounds(sprite
)) sprite
->frame
= 0;
1092 if ((sprite
->count
!= 0) &&
1096 Destroy(sprite
->x
+ 48, sprite
->y
+ 40);
1100 DoExplosionSprite(SimSprite
*sprite
)
1105 if (sprite
->frame
== 1) {
1106 MakeSound("city", "Explosion-High"); /* explosion */
1107 x
= (sprite
->x
>>4) + 3;
1108 y
= (sprite
->y
>>4);
1109 SendMesAt(32, x
, y
);
1114 if (sprite
->frame
> 6) {
1117 StartFire(sprite
->x
+ 48 - 8, sprite
->y
+ 16);
1118 StartFire(sprite
->x
+ 48 - 24, sprite
->y
);
1119 StartFire(sprite
->x
+ 48 + 8, sprite
->y
);
1120 StartFire(sprite
->x
+ 48 - 24, sprite
->y
+ 32);
1121 StartFire(sprite
->x
+ 48 + 8, sprite
->y
+ 32);
1127 DoBusSprite(SimSprite
*sprite
)
1129 static short Dx
[5] = { 0, 1, 0, -1, 0 };
1130 static short Dy
[5] = { -1, 0, 1, 0, 0 };
1131 static short Dir2Frame
[4] = { 1, 2, 1, 2 };
1132 register int dir
, dir2
;
1133 int c
, dx
, dy
, crossed
, tx
, ty
, otx
, oty
;
1138 printf("Bus dir %d turn %d frame %d\n",
1139 sprite
->dir
, sprite
->turn
, sprite
->frame
);
1143 if (sprite
->turn
< 0) { /* ccw */
1144 if (sprite
->dir
& 1) { /* up or down */
1146 } else { /* left or right */
1150 sprite
->dir
= (sprite
->dir
- 1) & 3;
1152 if (sprite
->dir
& 1) { /* up or down */
1154 } else { /* left or right */
1158 sprite
->dir
= (sprite
->dir
+ 1) & 3;
1163 if ((sprite
->frame
== 3) || (sprite
->frame
== 4)) {
1165 sprite
->frame
= Dir2Frame
[sprite
->dir
];
1169 if (sprite
->speed
== 0) {
1172 } else { /* cruise at traffic speed */
1174 tx
= (sprite
->x
+ sprite
->x_hot
) >>5;
1175 ty
= (sprite
->y
+ sprite
->y_hot
) >>5;
1177 (tx
< (WORLD_X
>>1)) &&
1179 (ty
< (WORLD_Y
>>1))) {
1180 z
= TrfDensity
[tx
][ty
] >>6;
1197 if (speed
> sprite
->speed
)
1198 speed
= sprite
->speed
;
1204 if (speed
> 1) speed
= 1;
1205 dx
= Dx
[sprite
->dir
] * speed
;
1206 dy
= Dy
[sprite
->dir
] * speed
;
1208 dx
= Dx
[sprite
->dir
] * speed
;
1209 dy
= Dy
[sprite
->dir
] * speed
;
1211 tx
= (sprite
->x
+ sprite
->x_hot
) >>4;
1212 ty
= (sprite
->y
+ sprite
->y_hot
) >>4;
1214 /* drift into the right lane */
1215 switch (sprite
->dir
) {
1217 z
= ((tx
<<4) + 4) - (sprite
->x
+ sprite
->x_hot
);
1219 else if (z
> 0) dx
= 1;
1221 printf("moving up x %x z %d dx %d\n", sprite
->x
+ sprite
->x_hot
, z
, dx
);
1225 z
= ((ty
<<4) + 4) - (sprite
->y
+ sprite
->y_hot
);
1227 else if (z
> 0) dy
= 1;
1229 printf("moving right y %x z %d dy %d\n", sprite
->y
+ sprite
->y_hot
, z
, dy
);
1233 z
= ((tx
<<4)) - (sprite
->x
+ sprite
->x_hot
);
1235 else if (z
> 0) dx
= 1;
1237 printf("moving down x %x z %d dx %d\n", sprite
->x
+ sprite
->x_hot
, z
, dx
);
1241 z
= ((ty
<<4)) - (sprite
->y
+ sprite
->y_hot
);
1243 else if (z
> 0) dy
= 1;
1245 printf("moving left y %x z %d dy %d\n", sprite
->y
+ sprite
->y_hot
, z
, dy
);
1252 printf("speed dx %d dy %d\n", dx
, dy
);
1257 otx
= (sprite
->x
+ sprite
->x_hot
+ (Dx
[sprite
->dir
] * AHEAD
)) >>4;
1258 oty
= (sprite
->y
+ sprite
->y_hot
+ (Dy
[sprite
->dir
] * AHEAD
)) >>4;
1259 if (otx
< 0) otx
= 0; else if (otx
>= WORLD_X
) otx
= WORLD_X
- 1;
1260 if (oty
< 0) oty
= 0; else if (oty
>= WORLD_Y
) oty
= WORLD_Y
- 1;
1262 tx
= (sprite
->x
+ sprite
->x_hot
+ dx
+ (Dx
[sprite
->dir
] * AHEAD
)) >>4;
1263 ty
= (sprite
->y
+ sprite
->y_hot
+ dy
+ (Dy
[sprite
->dir
] * AHEAD
)) >>4;
1264 if (tx
< 0) tx
= 0; else if (tx
>= WORLD_X
) tx
= WORLD_X
- 1;
1265 if (ty
< 0) ty
= 0; else if (ty
>= WORLD_Y
) ty
= WORLD_Y
- 1;
1267 if ((tx
!= otx
) || (ty
!= oty
)) {
1269 printf("drive from tile %d %d to %d %d\n",
1272 z
= CanDriveOn(tx
, ty
);
1274 /* can't drive forward into a new tile */
1276 bulldozer_tool(NULL
, tx
, ty
);
1280 /* drive forward into a new tile */
1291 tx
= (sprite
->x
+ sprite
->x_hot
+ dx
) >>4;
1292 ty
= (sprite
->y
+ sprite
->y_hot
+ dy
) >>4;
1293 z
= CanDriveOn(tx
, ty
);
1295 /* cool, cruise along */
1300 /* something in the way */
1311 for (s
= sim
->sprite
; s
!= NULL
; s
= s
->next
) {
1312 if ((sprite
!= s
) &&
1314 ((s
->type
== BUS
) ||
1315 ((s
->type
== TRA
) &&
1316 (s
->frame
!= 5))) &&
1317 CheckSpriteCollision(sprite
, s
)) {
1323 ExplodeSprite(sprite
);
1329 CanDriveOn(int x
, int y
)
1333 if (!TestBounds(x
, y
))
1336 tile
= Map
[x
][y
] & LOMASK
;
1338 if (((tile
>= ROADBASE
) &&
1339 (tile
<= LASTROAD
) &&
1342 (tile
== HRAILROAD
) ||
1343 (tile
== VRAILROAD
))
1346 if ((tile
== DIRT
) || tally(tile
))
1353 ExplodeSprite(SimSprite
*sprite
)
1359 x
= sprite
->x
+ sprite
->x_hot
;
1360 y
= sprite
->y
+ sprite
->y_hot
;
1361 MakeExplosionAt(x
, y
);
1366 switch (sprite
->type
) {
1370 SendMesAt(-24, x
, y
);
1375 SendMesAt(-25, x
, y
);
1380 SendMesAt(-26, x
, y
);
1385 SendMesAt(-27, x
, y
);
1390 SendMesAt(-26, x
, y
); /* XXX for now */
1393 MakeSound("city", "Explosion-High"); /* explosion */
1400 if ((x
== POWERBASE
) || (x
== POWERBASE
+ 1) ||
1401 (x
== RAILBASE
) || (x
== RAILBASE
+ 1) ||
1402 (x
== BRWH
) || (x
== BRWV
))
1409 Destroy(int ox
, int oy
)
1415 if (!TestBounds(x
, y
))
1419 if (t
>= TREEBASE
) {
1420 /* TILE_IS_BRIDGE(t) */
1421 if (!(z
& BURNBIT
)) {
1422 if ((t
>= ROADBASE
) && (t
<= LASTROAD
))
1429 MakeExplosionAt(ox
, oy
);
1435 Map
[x
][y
] = (DoAnimation
1437 : (LASTTINYEXP
- 3)) | BULLBIT
| ANIMBIT
;
1442 OFireZone(int Xloc
, int Yloc
, int ch
)
1444 register short Xtem
, Ytem
;
1447 RateOGMem
[Xloc
>>3][Yloc
>>3] -= 20;
1453 if (ch
== AIRPORT
) XYmax
= 5;
1456 for (x
= -1; x
< XYmax
; x
++)
1457 for (y
= -1; y
< XYmax
; y
++) {
1460 if ((Map
[Xtem
][Ytem
] & LOMASK
) >= ROADBASE
)
1461 Map
[Xtem
][Ytem
] |= BULLBIT
;
1466 StartFire(int x
, int y
)
1472 if ((x
>= WORLD_X
) ||
1478 if ((!(z
& BURNBIT
)) && (t
!= 0)) return;
1479 if (z
& ZONEBIT
) return;
1480 Map
[x
][y
] = FIRE
+ (Rand16() & 3) + ANIMBIT
;
1484 GenerateTrain(int x
, int y
)
1486 if ((TotalPop
> 20) &&
1487 (GetSprite(TRA
) == NULL
) &&
1489 MakeSprite(TRA
, (x
<<4) + TRA_GROOVE_X
, (y
<<4) + TRA_GROOVE_Y
);
1494 GenerateBus(int x
, int y
)
1496 if ((GetSprite(BUS
) == NULL
) &&
1498 MakeSprite(BUS
, (x
<<4) + BUS_GROOVE_X
, (y
<<4) + BUS_GROOVE_Y
);
1505 register short x
, y
;
1507 if (!(Rand16() & 3))
1508 for (x
= 4; x
< WORLD_X
- 2; x
++)
1509 if (Map
[x
][0] == CHANNEL
) {
1513 if (!(Rand16() & 3))
1514 for (y
= 1; y
< WORLD_Y
- 2; y
++)
1515 if (Map
[0][y
] == CHANNEL
) {
1519 if (!(Rand16() & 3))
1520 for (x
= 4; x
< WORLD_X
- 2; x
++)
1521 if (Map
[x
][WORLD_Y
- 1] == CHANNEL
) {
1522 MakeShipHere(x
, WORLD_Y
- 1);
1525 if (!(Rand16() & 3))
1526 for (y
= 1; y
< WORLD_Y
- 2; y
++)
1527 if (Map
[WORLD_X
- 1][y
] == CHANNEL
) {
1528 MakeShipHere(WORLD_X
- 1, y
);
1534 MakeShipHere(int x
, int y
, int z
)
1536 MakeSprite(SHI
, (x
<<4) - (48 - 1), (y
<<4));
1542 register x
, y
, z
, done
= 0;
1545 if ((sprite
= GetSprite(GOD
)) != NULL
) {
1546 sprite
->sound_count
= 1;
1547 sprite
->count
= 1000;
1548 sprite
->dest_x
= PolMaxX
<<4;
1549 sprite
->dest_y
= PolMaxY
<<4;
1553 for (z
= 0; z
< 300; z
++) {
1554 x
= Rand(WORLD_X
- 20) + 10;
1555 y
= Rand(WORLD_Y
- 10) + 5;
1556 if ((Map
[x
][y
] == RIVER
) || (Map
[x
][y
] == RIVER
+ BULLBIT
)) {
1563 MonsterHere(60, 50);
1567 MonsterHere(int x
, int y
)
1571 MakeSprite(GOD
, (x
<<4) + 48, (y
<<4));
1573 SendMesAt(-21, x
+ 5, y
);
1577 GenerateCopter(int x
, int y
)
1579 if (GetSprite(COP
) != NULL
) return;
1581 MakeSprite(COP
, (x
<<4), (y
<<4) + 30);
1585 GeneratePlane(int x
, int y
)
1587 if (GetSprite(AIR
) != NULL
) return;
1589 MakeSprite(AIR
, (x
<<4) + 48, (y
<<4) + 12);
1596 if (GetSprite(AIR
) == NULL
) {
1599 x
= Rand(WORLD_X
- 20) + 10;
1600 y
= Rand(WORLD_Y
- 10) + 5;
1602 GeneratePlane(x
, y
);
1605 ExplodeSprite(GetSprite(AIR
));
1615 if ((sprite
= GetSprite(TOR
)) != NULL
) {
1616 sprite
->count
= 200;
1620 x
= Rand((WORLD_X
<<4) - 800) + 400;
1621 y
= Rand((WORLD_Y
<<4) - 200) + 100;
1622 MakeSprite(TOR
, x
, y
);
1624 SendMesAt(-22, (x
>>4) + 3, (y
>>4) + 2);
1628 MakeExplosion(int x
, int y
)
1630 if ((x
>= 0) && (x
< WORLD_X
) &&
1631 (y
>= 0) && (y
< WORLD_Y
)) {
1632 MakeExplosionAt((x
<< 4) + 8, (y
<< 4) + 8);
1637 MakeExplosionAt(int x
, int y
)
1639 MakeNewSprite(EXP
, x
- 40, y
- 16);