1 #include <stdlib.h>
2
3 #include "types.h"
4 #include "util.h"
5
6 #include "Bill.h"
7 #include "Computer.h"
8 #include "Horde.h"
9 #include "Network.h"
10 #include "Game.h"
11 #include "OS.h"
12 #include "UI.h"
13
14 #define OS_OFFSET 4 /* offset of screen from 0,0 */
15 #define BORDER(size) (size / 10) /* at least this far from a side */
16
17 #define MIN_PC 6 /* type >= MIN_PC means the computer is a PC */
18
19 static const char *cpuname[] = {"toaster", "maccpu", "nextcpu", "sgicpu",
20 "suncpu", "os2cpu", "bsdcpu"};
21
22 #define NUM_SYS (sizeof(cpuname) / sizeof(cpuname[0]))
23
24 static Picture *pictures[NUM_SYS]; /* array of cpu pictures */
25 static int width, height;
26
27
28 static int
determineOS(Computer * computer)29 determineOS(Computer *computer) {
30 if (computer->type < MIN_PC)
31 return computer->type;
32 else
33 return OS_randpc();
34 }
35
36 int
Computer_setup(Computer * computer,int index)37 Computer_setup(Computer *computer, int index) {
38 int j, counter = 0, flag;
39 int x, y;
40 int screensize = Game_screensize();
41 int border = BORDER(screensize);
42 do {
43 if (++counter > 4000)
44 return 0;
45 x = RAND(border, screensize - border - width);
46 y = RAND(border, screensize - border - height);
47 flag = 1;
48 /* check for conflicting computer placement */
49 for (j = 0; j < index && flag; j++) {
50 Computer *c = Network_get_computer(j);
51 int twidth = width - BILL_OFFSET_X + Bill_width();
52 if (UI_intersect(x, y, twidth, height,
53 c->x, c->y, twidth, height))
54 flag = 0;
55 }
56 } while (!flag);
57 computer->x = x;
58 computer->y = y;
59 computer->type = RAND(1, NUM_SYS - 1);
60 computer->os = determineOS(computer);
61 computer->busy = 0;
62 computer->stray = NULL;
63 return 1;
64 }
65
66 int
Computer_on(Computer * computer,int locx,int locy)67 Computer_on(Computer *computer, int locx, int locy) {
68 return (abs(locx - computer->x) < width &&
69 abs(locy - computer->y) < height);
70 }
71
72 int
Computer_compatible(Computer * computer,int system)73 Computer_compatible(Computer *computer, int system) {
74 return (computer->type == system ||
75 (computer->type >= MIN_PC && OS_ispc(system)));
76 }
77
78 void
Computer_draw(Computer * computer)79 Computer_draw(Computer *computer) {
80 UI_draw(pictures[computer->type], computer->x, computer->y);
81 if (computer->os != OS_OFF)
82 OS_draw(computer->os,
83 computer->x + OS_OFFSET, computer->y + OS_OFFSET);
84 }
85
86 void
Computer_load_pix()87 Computer_load_pix() {
88 unsigned int i;
89 for (i = 0; i < NUM_SYS; i++)
90 UI_load_picture(cpuname[i], 1, &pictures[i]);
91 width = UI_picture_width(pictures[0]);
92 height = UI_picture_height(pictures[0]);
93 }
94
95 int
Computer_width()96 Computer_width() {
97 return width;
98 }
99
100 int
Computer_height()101 Computer_height() {
102 return height;
103 }
104