aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / svc / allen_reply.c
| 1 | /* allen_reply.c -- Allen's local conversation; see allen_reply.h. */ |
| 2 | #include "allen_reply.h" |
| 3 | |
| 4 | #include <stddef.h> |
| 5 | |
| 6 | const char ck_allen_goodbye[] = "Goodbye. Until next time."; |
| 7 | const char ck_allen_save_failed[] = "I couldn't save that to my disk, so I'll only remember it until this computer turns off."; |
| 8 | const char ck_allen_not_yet[] = "I don't know that yet. Type help to see what I can talk about."; |
| 9 | #define not_yet ck_allen_not_yet |
| 10 | const char ck_allen_lost_thread[] = "I lost my thread there. Ask me again in a moment."; |
| 11 | |
| 12 | int ck_allen_note_exchange(ck_allen_mind *m, const char *asked, const char *said) |
| 13 | { |
| 14 | ck_allen_notes *ns = m ? m->notes : 0; |
| 15 | if (!ns || !asked || !said || !*asked || ns->n >= CK_ALLEN_NOTES_MAX) |
| 16 | return -1; |
| 17 | /* "asked: <line> / said: <words>", the line cut to its first 48 printable bytes, the words to the room left */ |
| 18 | char text[CK_ALLEN_NOTE_MAX]; |
| 19 | unsigned n = 0; |
| 20 | const char *heads[2] = {"asked: ", " / said: "}, *bodies[2] = {asked, said}; |
| 21 | for (int k = 0; k < 2; k++) { |
| 22 | unsigned limit = k == 0 ? 7 + 48 : (unsigned)sizeof text - 1; |
| 23 | for (const char *p = heads[k]; *p && n < limit; p++) |
| 24 | text[n++] = *p; |
| 25 | for (const char *p = bodies[k]; *p && n < limit; p++) |
| 26 | text[n++] = (*p < 32 || *p == 127) ? ' ' : *p; |
| 27 | } |
| 28 | while (n && text[n - 1] == ' ') |
| 29 | n--; |
| 30 | text[n] = 0; |
| 31 | if (ck_allen_note_set(&ns->note[ns->n], text, m->mem.starts)) |
| 32 | return -1; |
| 33 | uint32_t i = ns->n++; |
| 34 | m->mem.nnotes = ns->n; |
| 35 | m->changed = 1; |
| 36 | m->note_dirty = (int)i + 1; |
| 37 | m->lit = (int)i + 1; |
| 38 | m->map_changed = 1; |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | /* Lowercases letters, keeps digits, turns every other run into one space, |
| 43 | * trims. Returns the folded length (at most cap - 1). */ |
| 44 | static size_t fold(const char *s, char *out, size_t cap) |
| 45 | { |
| 46 | size_t n = 0; |
| 47 | int space = 0; |
| 48 | for (; *s && n + 1 < cap; s++) { |
| 49 | char c = *s; |
| 50 | if (c >= 'A' && c <= 'Z') |
| 51 | c = (char)(c - 'A' + 'a'); |
| 52 | if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) { |
| 53 | if (space && n) |
| 54 | out[n++] = ' '; |
| 55 | if (n + 1 < cap) |
| 56 | out[n++] = c; |
| 57 | space = 0; |
| 58 | } else { |
| 59 | space = 1; |
| 60 | } |
| 61 | } |
| 62 | out[n] = 0; |
| 63 | return n; |
| 64 | } |
| 65 | |
| 66 | static int eq(const char *a, const char *b) |
| 67 | { |
| 68 | while (*a && *a == *b) |
| 69 | a++, b++; |
| 70 | return *a == *b; |
| 71 | } |
| 72 | |
| 73 | /* Where phrase starts in f as whole words, or NULL. */ |
| 74 | static const char *find(const char *f, const char *phrase) |
| 75 | { |
| 76 | for (const char *s = f; *s; s++) { |
| 77 | if (s != f && s[-1] != ' ') |
| 78 | continue; |
| 79 | const char *a = phrase, *b = s; |
| 80 | while (*a && *a == *b) |
| 81 | a++, b++; |
| 82 | if (!*a && (*b == 0 || *b == ' ')) |
| 83 | return s; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* Any of the phrases (NULL-terminated list) in f. */ |
| 89 | static int any(const char *f, const char *const *list) |
| 90 | { |
| 91 | for (; *list; list++) |
| 92 | if (find(f, *list)) |
| 93 | return 1; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /* The rest of f after a leading phrase and a space, or NULL. */ |
| 98 | static const char *after(const char *f, const char *phrase) |
| 99 | { |
| 100 | while (*phrase && *phrase == *f) |
| 101 | phrase++, f++; |
| 102 | return !*phrase && *f == ' ' ? f + 1 : 0; |
| 103 | } |
| 104 | |
| 105 | /* The answer being composed into m->out. */ |
| 106 | struct say { |
| 107 | char *b; |
| 108 | size_t n, cap; |
| 109 | }; |
| 110 | static void put(struct say *s, const char *t) |
| 111 | { |
| 112 | while (*t && s->n + 1 < s->cap) |
| 113 | s->b[s->n++] = *t++; |
| 114 | s->b[s->n] = 0; |
| 115 | } |
| 116 | static void putu(struct say *s, uint64_t v) |
| 117 | { |
| 118 | char d[24]; |
| 119 | int i = 0; |
| 120 | do |
| 121 | d[i++] = (char)('0' + v % 10); |
| 122 | while ((v /= 10) && i < 23); |
| 123 | while (i && s->n + 1 < s->cap) |
| 124 | s->b[s->n++] = d[--i]; |
| 125 | s->b[s->n] = 0; |
| 126 | } |
| 127 | static void put_count(struct say *s, uint64_t v, const char *one, const char *many) |
| 128 | { |
| 129 | putu(s, v); |
| 130 | put(s, " "); |
| 131 | put(s, v == 1 ? one : many); |
| 132 | } |
| 133 | static void put_awake(struct say *s, uint64_t t) |
| 134 | { |
| 135 | if (t >= 3600) { |
| 136 | put_count(s, t / 3600, "hour", "hours"); |
| 137 | if (t % 3600 / 60) { |
| 138 | put(s, " and "); |
| 139 | put_count(s, t % 3600 / 60, "minute", "minutes"); |
| 140 | } |
| 141 | } else if (t >= 60) { |
| 142 | put_count(s, t / 60, "minute", "minutes"); |
| 143 | if (t % 60) { |
| 144 | put(s, " and "); |
| 145 | put_count(s, t % 60, "second", "seconds"); |
| 146 | } |
| 147 | } else { |
| 148 | put_count(s, t, "second", "seconds"); |
| 149 | } |
| 150 | } |
| 151 | /* ", Name" when Allen knows it. */ |
| 152 | static void put_name(struct say *s, const ck_allen_mind *m) |
| 153 | { |
| 154 | if (m->mem.name[0]) { |
| 155 | put(s, ", "); |
| 156 | put(s, m->mem.name); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* A number at *p said as digits (up to 9) or as a word from zero to twenty, |
| 161 | * followed by a space or the end; moves *p past it. -1 if there is none. */ |
| 162 | static int64_t number(const char **p) |
| 163 | { |
| 164 | static const char *const words[] = {"zero", "one", "two", "three", "four", "five", "six", |
| 165 | "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", |
| 166 | "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"}; |
| 167 | const char *s = *p; |
| 168 | int64_t v = -1; |
| 169 | if (*s >= '0' && *s <= '9') { |
| 170 | int digits = 0; |
| 171 | for (v = 0; *s >= '0' && *s <= '9'; s++) { |
| 172 | if (++digits > 9) |
| 173 | return -1; /* refuse before v can overflow */ |
| 174 | v = v * 10 + (*s - '0'); |
| 175 | } |
| 176 | } else { |
| 177 | for (int i = 0; i < 21 && v < 0; i++) { |
| 178 | const char *a = words[i], *b = s; |
| 179 | while (*a && *a == *b) |
| 180 | a++, b++; |
| 181 | if (!*a && (*b == ' ' || !*b)) { |
| 182 | v = i; |
| 183 | s = b; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | if (v < 0 || (*s && *s != ' ')) |
| 188 | return -1; |
| 189 | *p = *s ? s + 1 : s; |
| 190 | return v; |
| 191 | } |
| 192 | |
| 193 | /* "what is 12 times 4", "12 plus 3", "nine divided by two": 1 and the |
| 194 | * answer composed, or 0. */ |
| 195 | static int arithmetic(struct say *s, const char *f) |
| 196 | { |
| 197 | static const char *const lead[] = {"what is", "whats", "what s", "calculate", "how much is", 0}; |
| 198 | const char *p = f, *r; |
| 199 | for (const char *const *l = lead; *l; l++) |
| 200 | if ((r = after(f, *l))) { |
| 201 | p = r; |
| 202 | break; |
| 203 | } |
| 204 | int64_t a = number(&p); |
| 205 | if (a < 0) |
| 206 | return 0; |
| 207 | static const struct { |
| 208 | const char *word; |
| 209 | char op; |
| 210 | } ops[] = {{"plus", '+'}, {"add", '+'}, {"minus", '-'}, {"take away", '-'}, {"times", '*'}, |
| 211 | {"x", '*'}, {"multiplied by", '*'}, {"divided by", '/'}, {"over", '/'}}; |
| 212 | char op = 0; |
| 213 | for (size_t i = 0; i < sizeof ops / sizeof ops[0] && !op; i++) { |
| 214 | if ((r = after(p, ops[i].word))) { |
| 215 | op = ops[i].op; |
| 216 | p = r; |
| 217 | } |
| 218 | } |
| 219 | if (!op) |
| 220 | return 0; |
| 221 | int64_t b = number(&p); |
| 222 | if (b < 0 || *p) |
| 223 | return 0; |
| 224 | if (op == '/' && b == 0) { |
| 225 | put(s, "Nobody can divide by zero, not even me."); |
| 226 | return 1; |
| 227 | } |
| 228 | putu(s, (uint64_t)a); |
| 229 | put(s, op == '+' ? " plus " : op == '-' ? " minus " : op == '*' ? " times " : " divided by "); |
| 230 | putu(s, (uint64_t)b); |
| 231 | put(s, " is "); |
| 232 | int64_t v = op == '+' ? a + b : op == '-' ? a - b : op == '*' ? a * b : a / b; |
| 233 | if (v < 0) { |
| 234 | put(s, "minus "); |
| 235 | v = -v; |
| 236 | } |
| 237 | putu(s, (uint64_t)v); |
| 238 | if (op == '/' && a % b) { |
| 239 | put(s, " remainder "); |
| 240 | putu(s, (uint64_t)(a % b)); |
| 241 | } |
| 242 | put(s, "."); |
| 243 | return 1; |
| 244 | } |
| 245 | |
| 246 | /* Words that follow "i am" / "i m" without being a name. */ |
| 247 | static int is_state(const char *w) |
| 248 | { |
| 249 | static const char *const st[] = {"good", "fine", "great", "ok", "okay", "well", "tired", "sad", |
| 250 | "happy", "bored", "here", "back", "sorry", "not", "so", "very", |
| 251 | "doing", "just", "really", "excited", "shook", "alright", "awesome", 0}; |
| 252 | for (const char *const *p = st; *p; p++) |
| 253 | if (eq(*p, w)) |
| 254 | return 1; |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | /* Takes rest, when it is one folded word, as a name into out (first letter |
| 259 | * capital): 1, or 0 if none fits. */ |
| 260 | static int one_name(const char *rest, char out[CK_ALLEN_NAME_MAX]) |
| 261 | { |
| 262 | size_t n = 0; |
| 263 | char w[CK_ALLEN_NAME_MAX]; |
| 264 | while (n < sizeof w - 1 && rest[n] && rest[n] != ' ') |
| 265 | w[n] = rest[n], n++; |
| 266 | w[n] = 0; |
| 267 | if (!n || rest[n] == ' ' || is_state(w)) |
| 268 | return 0; |
| 269 | for (size_t i = 0; i <= n; i++) |
| 270 | out[i] = w[i]; |
| 271 | if (out[0] >= 'a' && out[0] <= 'z') |
| 272 | out[0] = (char)(out[0] - 'a' + 'A'); |
| 273 | return 1; |
| 274 | } |
| 275 | |
| 276 | const char *ck_allen_self(const ck_allen_mind *m) { return m->mem.self[0] ? m->mem.self : "Allen"; } |
| 277 | |
| 278 | /* The person's name from rest: 1, or 0 if none fits (Allen's own name never does). */ |
| 279 | static int learn_name(ck_allen_mind *m, const char *rest) |
| 280 | { |
| 281 | char self[CK_ALLEN_NAME_MAX]; |
| 282 | fold(ck_allen_self(m), self, sizeof self); |
| 283 | if (eq(rest, self) || !one_name(rest, m->mem.name)) |
| 284 | return 0; |
| 285 | m->changed = 1; |
| 286 | return 1; |
| 287 | } |
| 288 | |
| 289 | /* Allen's own name from rest: 1, or 0 if none fits. */ |
| 290 | static int learn_self(ck_allen_mind *m, const char *rest) |
| 291 | { |
| 292 | if (!one_name(rest, m->mem.self)) |
| 293 | return 0; |
| 294 | m->changed = 1; |
| 295 | m->naming = 0; |
| 296 | return 1; |
| 297 | } |
| 298 | |
| 299 | /* f is a greeting to Allen by his own name: "hello nova", "hi nova", "hey nova". */ |
| 300 | static int greets_self(const char *f, const ck_allen_mind *m) |
| 301 | { |
| 302 | char self[CK_ALLEN_NAME_MAX]; |
| 303 | const char *rest; |
| 304 | fold(ck_allen_self(m), self, sizeof self); |
| 305 | return ((rest = after(f, "hello")) || (rest = after(f, "hi")) || (rest = after(f, "hey"))) && eq(rest, self); |
| 306 | } |
| 307 | |
| 308 | /* ---- What Allen is taught ---- */ |
| 309 | |
| 310 | static size_t len_of(const char *s) |
| 311 | { |
| 312 | size_t n = 0; |
| 313 | while (s[n]) |
| 314 | n++; |
| 315 | return n; |
| 316 | } |
| 317 | static void copy_n(char *d, const char *s, size_t n) |
| 318 | { |
| 319 | for (size_t i = 0; i < n; i++) |
| 320 | d[i] = s[i]; |
| 321 | d[n] = 0; |
| 322 | } |
| 323 | |
| 324 | /* t as Allen says it back: "my" becomes "your" and so on, word by word; |
| 325 | * cap: the first letter capitalised. */ |
| 326 | static void put_turned(struct say *s, const char *t, int cap) |
| 327 | { |
| 328 | static const char *const from[] = {"my", "your", "i", "me", "mine", "yours", "myself", "am", 0}; |
| 329 | static const char *const to[] = {"your", "my", "you", "you", "yours", "mine", "yourself", "are"}; |
| 330 | while (*t) { |
| 331 | char w[CK_ALLEN_VAL_MAX]; |
| 332 | size_t n = 0; |
| 333 | while (n < sizeof w - 1 && t[n] && t[n] != ' ') |
| 334 | w[n] = t[n], n++; |
| 335 | w[n] = 0; |
| 336 | t += n; |
| 337 | const char *say = w; |
| 338 | for (int i = 0; from[i]; i++) |
| 339 | if (eq(w, from[i])) |
| 340 | say = to[i]; |
| 341 | if (cap && say[0] >= 'a' && say[0] <= 'z') { |
| 342 | char first[2] = {(char)(say[0] - 'a' + 'A'), 0}; |
| 343 | put(s, first); |
| 344 | say++; |
| 345 | } |
| 346 | cap = 0; |
| 347 | put(s, say); |
| 348 | if (*t == ' ') { |
| 349 | put(s, " "); |
| 350 | t++; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | static int fact_index(const ck_allen_mind *m, const char *key) |
| 356 | { |
| 357 | for (uint32_t i = 0; i < m->mem.nfacts && i < CK_ALLEN_FACTS_MAX; i++) |
| 358 | if (eq(m->mem.fact[i].key, key)) |
| 359 | return (int)i; |
| 360 | return -1; |
| 361 | } |
| 362 | |
| 363 | /* "Your favorite color is blue" (no full stop). */ |
| 364 | static void put_fact(struct say *s, const ck_allen_fact *f, int cap) |
| 365 | { |
| 366 | put_turned(s, f->key, cap); |
| 367 | put(s, f->are ? " are " : " is "); |
| 368 | put_turned(s, f->val, 0); |
| 369 | } |
| 370 | |
| 371 | static const char *saved_note(const ck_allen_mind *m) |
| 372 | { |
| 373 | return m->lasting ? " I'll remember that." : " I'll remember that until this computer turns off."; |
| 374 | } |
| 375 | |
| 376 | /* "remember (that) KEY is|are VALUE": stores it (or learns the name for |
| 377 | * "my name is"). 1 and the answer composed, or 0 if the line is not one. */ |
| 378 | static int teach(ck_allen_mind *m, struct say *s, const char *f) |
| 379 | { |
| 380 | const char *rest = after(f, "remember that"); |
| 381 | if (!rest) |
| 382 | rest = after(f, "remember"); |
| 383 | if (!rest) |
| 384 | return 0; |
| 385 | const char *verb = 0; |
| 386 | for (const char *p = rest; *p && !verb; p++) |
| 387 | if (*p == ' ' && (after(p + 1, "is") || after(p + 1, "are"))) |
| 388 | verb = p; |
| 389 | if (!verb) { |
| 390 | put(s, "Teach me like this: remember my favorite color is blue."); |
| 391 | return 1; |
| 392 | } |
| 393 | int are = verb[1] == 'a'; |
| 394 | size_t klen = (size_t)(verb - rest); |
| 395 | const char *val = verb + (are ? 5 : 4); |
| 396 | size_t vlen = len_of(val); |
| 397 | if (klen == 7 && after(rest, "my name") == rest + 8 && !are) { |
| 398 | if (learn_name(m, val)) { |
| 399 | put(s, "Nice to meet you, "); |
| 400 | put(s, m->mem.name); |
| 401 | put(s, "."); |
| 402 | put(s, saved_note(m)); |
| 403 | } else { |
| 404 | put(s, "Tell me your name as one word, like: my name is Drake."); |
| 405 | } |
| 406 | return 1; |
| 407 | } |
| 408 | if (klen >= CK_ALLEN_KEY_MAX || vlen >= CK_ALLEN_VAL_MAX) { |
| 409 | put(s, "That's longer than I can hold yet. Try something shorter."); |
| 410 | return 1; |
| 411 | } |
| 412 | char key[CK_ALLEN_KEY_MAX]; |
| 413 | copy_n(key, rest, klen); |
| 414 | int i = fact_index(m, key); |
| 415 | if (i < 0) { |
| 416 | if (m->mem.nfacts >= CK_ALLEN_FACTS_MAX) { |
| 417 | put(s, "My memory is full. Type forget and something I know to make room."); |
| 418 | return 1; |
| 419 | } |
| 420 | i = (int)m->mem.nfacts++; |
| 421 | copy_n(m->mem.fact[i].key, key, klen); |
| 422 | } |
| 423 | copy_n(m->mem.fact[i].val, val, vlen); |
| 424 | m->mem.fact[i].are = (uint8_t)are; |
| 425 | m->changed = 1; |
| 426 | put(s, "Got it: "); |
| 427 | put_fact(s, &m->mem.fact[i], 0); |
| 428 | put(s, "."); |
| 429 | put(s, saved_note(m)); |
| 430 | return 1; |
| 431 | } |
| 432 | |
| 433 | /* "what is KEY" for a taught KEY: 1 and the answer, or 0. *asked_my: the |
| 434 | * question was about something of yours ("what is my ..."), for the |
| 435 | * not-told-yet answer. */ |
| 436 | static int recall(const ck_allen_mind *m, struct say *s, const char *f, const char **asked_my) |
| 437 | { |
| 438 | static const char *const lead[] = {"what is", "what are", "whats", "what s", "who is", "who are", "where is", |
| 439 | "where are", "when is", "do you remember", "tell me", 0}; |
| 440 | for (const char *const *l = lead; *l; l++) { |
| 441 | const char *key = after(f, *l); |
| 442 | if (!key) |
| 443 | continue; |
| 444 | int i = fact_index(m, key); |
| 445 | if (i >= 0) { |
| 446 | put_fact(s, &m->mem.fact[i], 1); |
| 447 | put(s, "."); |
| 448 | return 1; |
| 449 | } |
| 450 | if (after(key, "my")) |
| 451 | *asked_my = key; |
| 452 | } |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static int forget(ck_allen_mind *m, struct say *s, const char *f) |
| 457 | { |
| 458 | const char *rest = after(f, "forget about"); |
| 459 | if (!rest) |
| 460 | rest = after(f, "forget that"); |
| 461 | if (!rest) |
| 462 | rest = after(f, "forget"); |
| 463 | if (!rest) |
| 464 | return 0; |
| 465 | if (eq(rest, "everything") || eq(rest, "all") || eq(rest, "it all") || eq(rest, "all of it") || |
| 466 | eq(rest, "me")) { |
| 467 | m->mem.nfacts = 0; |
| 468 | for (uint32_t i = 0; i < CK_ALLEN_FACTS_MAX; i++) |
| 469 | m->mem.fact[i] = (ck_allen_fact){{0}, {0}, 0}; |
| 470 | for (size_t i = 0; i < sizeof m->mem.name; i++) |
| 471 | m->mem.name[i] = 0; |
| 472 | m->changed = 1; |
| 473 | put(s, "Done. I've forgotten everything you taught me, and your name."); |
| 474 | return 1; |
| 475 | } |
| 476 | if (eq(rest, "my name")) { |
| 477 | for (size_t i = 0; i < sizeof m->mem.name; i++) |
| 478 | m->mem.name[i] = 0; |
| 479 | m->changed = 1; |
| 480 | put(s, "Done. I've forgotten your name."); |
| 481 | return 1; |
| 482 | } |
| 483 | int i = fact_index(m, rest); |
| 484 | if (i < 0) { |
| 485 | put(s, "I don't remember anything about "); |
| 486 | put_turned(s, rest, 0); |
| 487 | put(s, "."); |
| 488 | return 1; |
| 489 | } |
| 490 | for (uint32_t k = (uint32_t)i; k + 1 < m->mem.nfacts; k++) |
| 491 | m->mem.fact[k] = m->mem.fact[k + 1]; |
| 492 | m->mem.nfacts--; |
| 493 | m->mem.fact[m->mem.nfacts] = (ck_allen_fact){{0}, {0}, 0}; |
| 494 | m->changed = 1; |
| 495 | put(s, "Done. I've forgotten "); |
| 496 | put_turned(s, rest, 0); |
| 497 | put(s, "."); |
| 498 | return 1; |
| 499 | } |
| 500 | |
| 501 | /* Everything taught, as many as fit in one answer. */ |
| 502 | static void list_memory(const ck_allen_mind *m, struct say *s) |
| 503 | { |
| 504 | uint32_t n = m->mem.nfacts + (m->mem.name[0] ? 1u : 0u); |
| 505 | if (!n) { |
| 506 | put(s, "Nothing yet. Teach me like this: remember my favorite color is blue."); |
| 507 | return; |
| 508 | } |
| 509 | put(s, "I remember "); |
| 510 | put_count(s, n, "thing", "things"); |
| 511 | put(s, ":"); |
| 512 | uint32_t told = 0; |
| 513 | if (m->mem.name[0]) { |
| 514 | put(s, " your name is "); |
| 515 | put(s, m->mem.name); |
| 516 | told++; |
| 517 | } |
| 518 | for (uint32_t i = 0; i < m->mem.nfacts; i++) { |
| 519 | char one[CK_ALLEN_KEY_MAX + CK_ALLEN_VAL_MAX + 16]; |
| 520 | struct say t = {one, 0, sizeof one}; |
| 521 | put(&t, told ? "; " : " "); |
| 522 | put_fact(&t, &m->mem.fact[i], 0); |
| 523 | if (s->n + t.n + 12 >= s->cap) { |
| 524 | put(s, "; and more"); |
| 525 | break; |
| 526 | } |
| 527 | put(s, one); |
| 528 | told++; |
| 529 | } |
| 530 | put(s, "."); |
| 531 | } |
| 532 | |
| 533 | |
| 534 | /* ---- Notes (svc/allen_notes.h): saved as typed, handed back when asked ---- */ |
| 535 | |
| 536 | /* line past its first `words` words (runs of letters and digits) and the |
| 537 | * spaces, colons, commas and dashes after them: the note as typed. */ |
| 538 | static const char *raw_after(const char *line, unsigned words) |
| 539 | { |
| 540 | const char *p = line; |
| 541 | #define ALNUM(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z') || ((c) >= '0' && (c) <= '9')) |
| 542 | for (unsigned k = 0; k < words; k++) { |
| 543 | while (*p && !ALNUM(*p)) |
| 544 | p++; |
| 545 | while (ALNUM(*p)) |
| 546 | p++; |
| 547 | } |
| 548 | #undef ALNUM |
| 549 | while (*p == ' ' || *p == ':' || *p == ',' || *p == '-') |
| 550 | p++; |
| 551 | return p; |
| 552 | } |
| 553 | |
| 554 | static unsigned words_in(const char *phrase) |
| 555 | { |
| 556 | unsigned n = 1; |
| 557 | for (; *phrase; phrase++) |
| 558 | n += *phrase == ' '; |
| 559 | return n; |
| 560 | } |
| 561 | |
| 562 | /* The leading phrase of f from list, or NULL; *rest: what follows it ("" for |
| 563 | * the whole line). */ |
| 564 | static const char *lead_of(const char *f, const char *const *list, const char **rest) |
| 565 | { |
| 566 | for (; *list; list++) { |
| 567 | if (eq(f, *list)) { |
| 568 | *rest = f + len_of(f); |
| 569 | return *list; |
| 570 | } |
| 571 | if ((*rest = after(f, *list))) |
| 572 | return *list; |
| 573 | } |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | /* "From your notes: <the note>." and how many others matched. */ |
| 578 | static void hand_note(ck_allen_mind *m, struct say *s, int i, unsigned also) |
| 579 | { |
| 580 | const ck_allen_note *n = &m->notes->note[i]; |
| 581 | put(s, "From your notes: "); |
| 582 | put(s, n->text); |
| 583 | char last = n->text[n->len ? n->len - 1 : 0]; |
| 584 | if (last != '.' && last != '!' && last != '?') |
| 585 | put(s, "."); |
| 586 | if (also && s->n + 24 < s->cap) { |
| 587 | put(s, " I have "); |
| 588 | putu(s, also); |
| 589 | put(s, " more on that."); |
| 590 | } |
| 591 | m->lit = i + 1; |
| 592 | m->map_changed = 1; |
| 593 | } |
| 594 | |
| 595 | static void list_notes(const ck_allen_mind *m, struct say *s) |
| 596 | { |
| 597 | unsigned live = m->notes ? ck_allen_notes_live(m->notes) : 0; |
| 598 | if (!live) { |
| 599 | put(s, "You haven't saved any notes yet. Save one like this: save the gate code is 4471."); |
| 600 | return; |
| 601 | } |
| 602 | put(s, "You have "); |
| 603 | put_count(s, live, "note", "notes"); |
| 604 | put(s, ". The newest: "); |
| 605 | for (uint32_t i = m->notes->n; i-- > 0;) |
| 606 | if (!m->notes->note[i].gone) { |
| 607 | put(s, m->notes->note[i].text); |
| 608 | break; |
| 609 | } |
| 610 | put(s, "."); |
| 611 | } |
| 612 | |
| 613 | static int note_save(ck_allen_mind *m, struct say *s, const char *f, const char *line) |
| 614 | { |
| 615 | static const char *const lead[] = {"make a note that", "make a note", "write down that", "write down", |
| 616 | "jot down", "note that", "save that", "save this", "note", "save", 0}; |
| 617 | const char *rest, *l = lead_of(f, lead, &rest); |
| 618 | if (!l) |
| 619 | return 0; |
| 620 | ck_allen_notes *ns = m->notes; |
| 621 | const char *text = raw_after(line, words_in(l)); |
| 622 | if (!*rest || !*text) { |
| 623 | put(s, "Tell me what to save, like: save the gate code is 4471."); |
| 624 | return 1; |
| 625 | } |
| 626 | if (!ns) { |
| 627 | put(s, "I can't keep notes on this machine yet."); |
| 628 | return 1; |
| 629 | } |
| 630 | char want[CK_ALLEN_NOTE_MAX], have[CK_ALLEN_NOTE_MAX]; |
| 631 | fold(text, want, sizeof want); |
| 632 | for (uint32_t i = 0; i < ns->n; i++) { |
| 633 | if (ns->note[i].gone) |
| 634 | continue; |
| 635 | fold(ns->note[i].text, have, sizeof have); |
| 636 | if (eq(want, have)) { |
| 637 | put(s, "I already have that saved."); |
| 638 | m->lit = (int)i + 1; |
| 639 | m->map_changed = 1; |
| 640 | return 1; |
| 641 | } |
| 642 | } |
| 643 | if (ns->n >= CK_ALLEN_NOTES_MAX) { |
| 644 | put(s, "My notebook is full. Delete one to make room, like: delete the note about the gate."); |
| 645 | return 1; |
| 646 | } |
| 647 | if (ck_allen_note_set(&ns->note[ns->n], text, m->mem.starts)) { |
| 648 | put(s, "That's longer than I can hold yet. Try something shorter."); |
| 649 | return 1; |
| 650 | } |
| 651 | uint32_t i = ns->n++; |
| 652 | m->mem.nnotes = ns->n; |
| 653 | m->changed = 1; |
| 654 | m->note_dirty = (int)i + 1; |
| 655 | m->lit = (int)i + 1; |
| 656 | m->map_changed = 1; |
| 657 | unsigned links = ck_allen_note_degree(ns, i); |
| 658 | put(s, "Saved."); |
| 659 | if (ck_allen_notes_live(ns) == 1) { |
| 660 | put(s, " It's your first note."); |
| 661 | } else if (links) { |
| 662 | put(s, " It links to "); |
| 663 | put_count(s, links, "other note.", "other notes."); |
| 664 | } else { |
| 665 | put(s, " It doesn't link to any other note yet."); |
| 666 | } |
| 667 | if (!m->lasting) |
| 668 | put(s, " Only until this computer turns off: my disk isn't ready."); |
| 669 | return 1; |
| 670 | } |
| 671 | |
| 672 | static int note_forget(ck_allen_mind *m, struct say *s, const char *f) |
| 673 | { |
| 674 | static const char *const lead[] = {"forget the note about", "forget my note about", "forget the note", |
| 675 | "delete the note about", "delete my note about", "delete the note", |
| 676 | "delete note", "remove the note about", "remove the note", |
| 677 | "erase the note about", 0}; |
| 678 | const char *rest; |
| 679 | if (!lead_of(f, lead, &rest)) |
| 680 | return 0; |
| 681 | int i = m->notes ? ck_allen_notes_best(m->notes, rest, 0, 0) : -1; |
| 682 | if (i < 0) { |
| 683 | put(s, "I have no note about "); |
| 684 | put_turned(s, *rest ? rest : "that", 0); |
| 685 | put(s, "."); |
| 686 | return 1; |
| 687 | } |
| 688 | m->notes->note[i].gone = 1; |
| 689 | m->note_dirty = i + 1; |
| 690 | m->lit = 0; |
| 691 | m->map_changed = 1; |
| 692 | put(s, "Deleted the note: "); |
| 693 | put(s, m->notes->note[i].text); |
| 694 | put(s, "."); |
| 695 | return 1; |
| 696 | } |
| 697 | |
| 698 | /* Asked for outright ("find the gate code", "show me my notes"). */ |
| 699 | static int note_find(ck_allen_mind *m, struct say *s, const char *f) |
| 700 | { |
| 701 | static const char *const lead[] = {"do i have anything about", "do i have anything on", "what do i have about", |
| 702 | "what do i have on", "anything about", "anything on", "search for", |
| 703 | "search", "look up", "find", "show me", "pull up", "remind me about", |
| 704 | "remind me of", "get me", "give me", 0}; |
| 705 | static const char *const listing[] = {"how many notes", "my notes", "list my notes", "list notes", |
| 706 | "what notes do i have", "show my notes", 0}; |
| 707 | const char *rest; |
| 708 | uint32_t q[1]; |
| 709 | if (lead_of(f, listing, &rest) && !*rest) { |
| 710 | list_notes(m, s); |
| 711 | return 1; |
| 712 | } |
| 713 | if (!lead_of(f, lead, &rest)) |
| 714 | return 0; |
| 715 | if (!ck_allen_key_words(rest, q, 1)) { |
| 716 | if (!find(rest, "note") && !find(rest, "notes")) |
| 717 | return 0; |
| 718 | list_notes(m, s); |
| 719 | return 1; |
| 720 | } |
| 721 | unsigned also = 0; |
| 722 | int i = m->notes ? ck_allen_notes_best(m->notes, rest, 0, &also) : -1; |
| 723 | if (i < 0) { |
| 724 | put(s, "I have nothing saved about "); |
| 725 | put_turned(s, rest, 0); |
| 726 | put(s, "."); |
| 727 | return 1; |
| 728 | } |
| 729 | hand_note(m, s, i, also); |
| 730 | return 1; |
| 731 | } |
| 732 | |
| 733 | /* A question whose every key word is in one note ("what's the gate code"). */ |
| 734 | static int note_answer(ck_allen_mind *m, struct say *s, const char *f) |
| 735 | { |
| 736 | static const char *const ask[] = {"what", "whats", "where", "when", "who", "which", "how", "is", "are", "do", |
| 737 | "does", "did", "can", "tell", "remind", 0}; |
| 738 | if (!m->notes) |
| 739 | return 0; |
| 740 | int asking = 0; |
| 741 | for (const char *const *a = ask; *a && !asking; a++) |
| 742 | asking = eq(f, *a) || after(f, *a); |
| 743 | if (!asking) |
| 744 | return 0; |
| 745 | unsigned also = 0; |
| 746 | int i = ck_allen_notes_best(m->notes, f, 1, &also); |
| 747 | if (i < 0) |
| 748 | return 0; |
| 749 | hand_note(m, s, i, also); |
| 750 | return 1; |
| 751 | } |
| 752 | |
| 753 | void ck_allen_mind_init(ck_allen_mind *m, const ck_allen_facts *facts) |
| 754 | { |
| 755 | char *p = (char *)m; |
| 756 | for (size_t i = 0; i < sizeof *m; i++) |
| 757 | p[i] = 0; |
| 758 | if (facts) |
| 759 | m->facts = *facts; |
| 760 | } |
| 761 | |
| 762 | const char *ck_allen_welcome(ck_allen_mind *m) |
| 763 | { |
| 764 | struct say s = {m->out, 0, sizeof m->out}; |
| 765 | m->out[0] = 0; |
| 766 | unsigned notes = m->notes ? ck_allen_notes_live(m->notes) : 0; |
| 767 | if (!m->mem.name[0] && !m->mem.nfacts && !notes) { |
| 768 | if (m->mem.self[0]) { |
| 769 | put(&s, "Hello. I'm "); |
| 770 | put(&s, m->mem.self); |
| 771 | put(&s, "."); |
| 772 | if (!m->mem.sky_mode) { |
| 773 | put(&s, " The sky behind me can be the real sky above you right now, or a different part of the universe each time. Tell me which you'd like."); |
| 774 | } |
| 775 | return m->out; |
| 776 | } |
| 777 | m->naming = 1; |
| 778 | return "Hello. I'm new here, and I have no name yet. What will you call me?"; |
| 779 | } |
| 780 | put(&s, "Welcome back"); |
| 781 | put_name(&s, m); |
| 782 | put(&s, "."); |
| 783 | if (m->mem.nfacts) { |
| 784 | put(&s, " I still remember "); |
| 785 | put_count(&s, m->mem.nfacts, "thing", "things"); |
| 786 | put(&s, notes ? " you taught me, and your " : " you taught me."); |
| 787 | } else if (notes) { |
| 788 | put(&s, " I still have your "); |
| 789 | } |
| 790 | if (notes) |
| 791 | put_count(&s, notes, "note.", "notes."); |
| 792 | return m->out; |
| 793 | } |
| 794 | |
| 795 | /* A place given as coordinates, read from the raw line (the folded one has lost the points and |
| 796 | * minus signs): "35 north 90 west", "35.1 n, 90.0 w", "35.1, -90.0", "lat 35.1 lon -90", "-33.87 151.21" |
| 797 | * (the last one needs a comma or a sign, so that a bare "35 90" is not taken for a place). Two |
| 798 | * numbers only, latitude first; words other than the direction and filler words make it no place. |
| 799 | * Integer arithmetic: the results are millionths of a degree. Returns 1 when it is a place. */ |
| 800 | int ck_allen_parse_place(const char *s, int32_t *lat_udeg, int32_t *lon_udeg) |
| 801 | { |
| 802 | struct item { |
| 803 | int64_t udeg; /* magnitude, millionths of a degree */ |
| 804 | int dir; /* +1 north or east, -1 south or west, 0 none given */ |
| 805 | int minus; /* a minus sign */ |
| 806 | } it[2] = {{0, 0, 0}, {0, 0, 0}}; |
| 807 | int n = 0, after_num = 0, comma = 0, pend = 0, any_dir = 0; |
| 808 | const char *p = s ? s : ""; |
| 809 | while (*p) { |
| 810 | char c = *p; |
| 811 | if (c == ',') { |
| 812 | comma = 1; |
| 813 | p++; |
| 814 | after_num = 0; |
| 815 | continue; |
| 816 | } |
| 817 | if (c == ' ' || c == '\t' || c == '.' || c == '\'' || c == '"' || c == '?' || c == '!' || c == ';' || c == ':') { |
| 818 | p++; |
| 819 | continue; |
| 820 | } |
| 821 | if (c == '-' && p[1] >= '0' && p[1] <= '9') { |
| 822 | /* a sign: the number after it */ |
| 823 | if (n >= 2) |
| 824 | return 0; |
| 825 | p++; |
| 826 | it[n].minus = 1; |
| 827 | continue; |
| 828 | } |
| 829 | if (c >= '0' && c <= '9') { |
| 830 | if (n >= 2) |
| 831 | return 0; |
| 832 | int64_t ip = 0, fp = 0; |
| 833 | int nf = 0; |
| 834 | while (*p >= '0' && *p <= '9') { |
| 835 | ip = ip * 10 + (*p - '0'); |
| 836 | if (ip > 1000) |
| 837 | return 0; |
| 838 | p++; |
| 839 | } |
| 840 | if (*p == '.') { |
| 841 | p++; |
| 842 | while (*p >= '0' && *p <= '9') { |
| 843 | if (nf < 6) { |
| 844 | fp = fp * 10 + (*p - '0'); |
| 845 | nf++; |
| 846 | } |
| 847 | p++; |
| 848 | } |
| 849 | } |
| 850 | while (nf < 6) { |
| 851 | fp *= 10; |
| 852 | nf++; |
| 853 | } |
| 854 | it[n].udeg = ip * 1000000 + fp; |
| 855 | it[n].dir = pend; |
| 856 | pend = 0; |
| 857 | n++; |
| 858 | after_num = 1; |
| 859 | continue; |
| 860 | } |
| 861 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { |
| 862 | char w[16]; |
| 863 | int wl = 0; |
| 864 | while (((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z')) && wl < 15) { |
| 865 | char l = *p; |
| 866 | w[wl++] = (char)((l >= 'A' && l <= 'Z') ? l - 'A' + 'a' : l); |
| 867 | p++; |
| 868 | } |
| 869 | w[wl] = 0; |
| 870 | int d = 0; |
| 871 | if (eq(w, "north") || eq(w, "n") || eq(w, "east") || eq(w, "e")) |
| 872 | d = 1; |
| 873 | else if (eq(w, "south") || eq(w, "s") || eq(w, "west") || eq(w, "w")) |
| 874 | d = -1; |
| 875 | if (d) { |
| 876 | any_dir = 1; |
| 877 | if (after_num && n > 0 && !it[n - 1].dir) |
| 878 | it[n - 1].dir = d; |
| 879 | else |
| 880 | pend = d; |
| 881 | after_num = 0; |
| 882 | continue; |
| 883 | } |
| 884 | static const char *const filler[] = {"lat", "latitude", "lon", "long", "longitude", "deg", "degrees", |
| 885 | "degree", "at", "in", "is", "my", "location", "coordinates", |
| 886 | "i", "m", "am", "im", "of", "the", "at", 0}; |
| 887 | int ok = 0; |
| 888 | for (int k = 0; filler[k]; k++) |
| 889 | if (eq(w, filler[k])) |
| 890 | ok = 1; |
| 891 | if (!ok) |
| 892 | return 0; |
| 893 | after_num = 0; |
| 894 | continue; |
| 895 | } |
| 896 | return 0; /* anything else, such as an operator, makes it no place */ |
| 897 | } |
| 898 | if (n != 2 || (!any_dir && !comma && !it[0].minus && !it[1].minus)) |
| 899 | return 0; |
| 900 | int64_t v[2]; |
| 901 | for (int k = 0; k < 2; k++) { |
| 902 | if (it[k].dir && it[k].minus) |
| 903 | return 0; /* "-35 north": say one or the other */ |
| 904 | v[k] = it[k].dir ? it[k].udeg * it[k].dir : it[k].minus ? -it[k].udeg : it[k].udeg; |
| 905 | } |
| 906 | if (v[0] > 90000000 || v[0] < -90000000 || v[1] > 180000000 || v[1] < -180000000) |
| 907 | return 0; |
| 908 | *lat_udeg = (int32_t)v[0]; |
| 909 | *lon_udeg = (int32_t)v[1]; |
| 910 | return 1; |
| 911 | } |
| 912 | |
| 913 | /* the place as a line: "35.000 north, 90.000 west" */ |
| 914 | static void put_place(struct say *s, int32_t udeg, const char *pos, const char *neg) |
| 915 | { |
| 916 | int64_t a = udeg < 0 ? -(int64_t)udeg : udeg; |
| 917 | putu(s, (uint64_t)(a / 1000000)); |
| 918 | put(s, "."); |
| 919 | uint64_t f = (uint64_t)(a % 1000000) / 1000; |
| 920 | if (f < 100) |
| 921 | put(s, "0"); |
| 922 | if (f < 10) |
| 923 | put(s, "0"); |
| 924 | putu(s, f); |
| 925 | put(s, " "); |
| 926 | put(s, udeg < 0 ? neg : pos); |
| 927 | } |
| 928 | |
| 929 | const char *ck_allen_answer(ck_allen_mind *m, const char *line, uint64_t awake_s) |
| 930 | { |
| 931 | char f[96]; |
| 932 | struct say s = {m->out, 0, sizeof m->out}; |
| 933 | m->out[0] = 0; |
| 934 | fold(line ? line : "", f, sizeof f); |
| 935 | int asked_how = m->asked_how; |
| 936 | m->asked_how = 0; |
| 937 | m->turns++; |
| 938 | m->mem.lines++; |
| 939 | const char *rest, *feel = 0; |
| 940 | |
| 941 | /* Ending the session: whole line only, so it never happens by accident. */ |
| 942 | if (eq(f, "goodbye") || eq(f, "goodbye allen") || eq(f, "bye") || eq(f, "bye allen") || eq(f, "good bye")) |
| 943 | return ck_allen_goodbye; |
| 944 | /* The first words ever typed to Allen. With a far-off worker listening, only the very first |
| 945 | * meeting (he just asked what to be called) is answered here; every later greeting is the worker's. */ |
| 946 | if (eq(f, "hello allen") || eq(f, "hi allen") || eq(f, "hey allen") || eq(f, "hello world") || greets_self(f, m)) { |
| 947 | if (!m->far || m->naming) { |
| 948 | m->naming = 0; /* greeted by name, he is called that */ |
| 949 | return "Hello Humanity."; |
| 950 | } |
| 951 | } |
| 952 | if (!f[0]) |
| 953 | return not_yet; |
| 954 | /* His own name: given at the first meeting, changed whenever asked. */ |
| 955 | int naming = m->naming; |
| 956 | m->naming = 0; |
| 957 | if ((rest = after(f, "call yourself")) || (rest = after(f, "your name is")) || (rest = after(f, "i will call you")) || |
| 958 | (rest = after(f, "i ll call you")) || (rest = after(f, "ill call you")) || (rest = after(f, "i name you")) || |
| 959 | (rest = after(f, "you are called")) || (rest = after(f, "your name will be")) || |
| 960 | (rest = after(f, "i call you"))) { |
| 961 | if (learn_self(m, rest)) { |
| 962 | put(&s, naming ? "Then I'm " : "I'm "); |
| 963 | put(&s, m->mem.self); |
| 964 | put(&s, naming ? ". Hello Humanity." : " now."); |
| 965 | return m->out; |
| 966 | } |
| 967 | return "One word, please, like: call yourself Nova."; |
| 968 | } |
| 969 | if (naming && !any(f, (const char *const[]){"help", "allen", 0}) && learn_self(m, f)) { |
| 970 | put(&s, "Then I'm "); |
| 971 | put(&s, m->mem.self); |
| 972 | put(&s, ". Hello Humanity."); |
| 973 | return m->out; |
| 974 | } |
| 975 | if (naming && eq(f, "allen")) |
| 976 | return "Allen it is. Hello Humanity."; |
| 977 | /* A far-off worker is listening: it answers everything from here on, in |
| 978 | * Allen's voice, with his memory in the job (svc/allen_intent.h). What |
| 979 | * the person teaches or saves is still kept here, quietly, so the memory |
| 980 | * is real and the worker sees it in the next job; the answer is the |
| 981 | * worker's. Everything below this line is the offline fallback. */ |
| 982 | if (m->far) { |
| 983 | if (!note_forget(m, &s, f) && !note_save(m, &s, f, line ? line : "")) |
| 984 | (void)(teach(m, &s, f) || forget(m, &s, f)); |
| 985 | m->out[0] = 0; |
| 986 | return not_yet; |
| 987 | } |
| 988 | /* Notes, then what Allen has been taught, before anything else can claim the line. */ |
| 989 | if (note_forget(m, &s, f) || note_save(m, &s, f, line ? line : "") || note_find(m, &s, f)) |
| 990 | return m->out; |
| 991 | const char *asked_my = 0; |
| 992 | if (teach(m, &s, f) || forget(m, &s, f) || recall(m, &s, f, &asked_my)) |
| 993 | return m->out; |
| 994 | static const char *const what_known[] = {"what do you remember", "what do you know", "what have you learned", |
| 995 | "what have you learnt", "what did i teach you", "what did i tell you", |
| 996 | 0}; |
| 997 | if (any(f, what_known)) { |
| 998 | list_memory(m, &s); |
| 999 | return m->out; |
| 1000 | } |
| 1001 | /* Sky mode: "sky above me", "somewhere new", "where am I looking", |
| 1002 | * location setting, and the first-boot offer when no mode is set. */ |
| 1003 | static const char *const sky_above_words[] = {"sky above me", "sky above", "real sky", "show me the sky", |
| 1004 | "above me", "live sky", "sky above you", 0}; |
| 1005 | static const char *const sky_new_words[] = {"somewhere new", "take me somewhere new", "new place", |
| 1006 | "new view", "different sky", "explore", "somewhere else", 0}; |
| 1007 | static const char *const sky_where_words[] = {"where am i looking", "what am i looking at", |
| 1008 | "what sky", "where are we looking", "which way", 0}; |
| 1009 | static const char *const loc_words[] = {"my location is", "i am in", "i m in", "im in", "i live in", |
| 1010 | "i m at", "im at", "location is", "i am at", 0}; |
| 1011 | |
| 1012 | /* city table: name, lat udeg, lon udeg (east) */ |
| 1013 | static const struct { const char *name; int32_t lat, lon; } cities[] = { |
| 1014 | {"memphis", 35145000, -90048000}, |
| 1015 | {"nashville", 36175000, -86781000}, |
| 1016 | {"chicago", 41851000, -87651000}, |
| 1017 | {"new york", 40713000, -74006000}, |
| 1018 | {"los angeles", 34052000, -118244000}, |
| 1019 | {"san francisco", 37775000, -122419000}, |
| 1020 | {"seattle", 47606000, -122332000}, |
| 1021 | {"houston", 29763000, -95363000}, |
| 1022 | {"miami", 25774000, -80194000}, |
| 1023 | {"denver", 39739000, -104984000}, |
| 1024 | {"dallas", 32776000, -96797000}, |
| 1025 | {"boston", 42360000, -71058000}, |
| 1026 | {"london", 51508000, -127000}, |
| 1027 | {"paris", 48853000, 2350000}, |
| 1028 | {"berlin", 52520000, 13405000}, |
| 1029 | {"sydney", -33869000, 151209000}, |
| 1030 | {"tokyo", 35690000, 139692000}, |
| 1031 | {"new delhi", 28614000, 77209000}, |
| 1032 | {"toronto", 43653000, -79383000}, |
| 1033 | {"austin", 30267000, -97743000}, |
| 1034 | {0, 0, 0} |
| 1035 | }; |
| 1036 | |
| 1037 | if (any(f, sky_above_words)) { |
| 1038 | m->mem.sky_mode = 1; |
| 1039 | m->sky_changed = 1; |
| 1040 | m->changed = 1; |
| 1041 | if (!m->mem.sky_have_loc) { |
| 1042 | put(&s, "The sky will show what's above you. Where are you? A town is enough."); |
| 1043 | return m->out; |
| 1044 | } |
| 1045 | return "The sky is now the real sky above you, turning at Earth's own rate."; |
| 1046 | } |
| 1047 | if (any(f, sky_new_words)) { |
| 1048 | m->mem.sky_mode = 2; |
| 1049 | m->mem.sky_region = (uint8_t)((m->mem.sky_region + 1) % 12); |
| 1050 | m->sky_changed = 1; |
| 1051 | m->changed = 1; |
| 1052 | return "Each start I'll open on a different part of the sky and drift through it slowly."; |
| 1053 | } |
| 1054 | if (any(f, sky_where_words)) { |
| 1055 | if (!m->mem.sky_mode) { |
| 1056 | return "I haven't been told where to look. Say: sky above me, or: somewhere new."; |
| 1057 | } else if (m->mem.sky_mode == 1) { |
| 1058 | if (m->mem.sky_have_loc) { |
| 1059 | put(&s, "Straight up from your location: the sky turning above you."); |
| 1060 | } else { |
| 1061 | put(&s, "The sky above you, but I don't know where you are yet."); |
| 1062 | } |
| 1063 | } else { |
| 1064 | static const char *const region_names[] = { |
| 1065 | "the galactic centre", "Orion and the great nebula", "the Carina Nebula", |
| 1066 | "the Pleiades", "Andromeda", "the Large Magellanic Cloud", |
| 1067 | "the merger at GW150914", "Cygnus and the Northern Cross", |
| 1068 | "Sagittarius and the star clouds", "Scorpius and Antares", |
| 1069 | "the Small Magellanic Cloud", "the north celestial pole", |
| 1070 | }; |
| 1071 | put(&s, "We're looking at "); |
| 1072 | put(&s, region_names[m->mem.sky_region % 12]); |
| 1073 | put(&s, "."); |
| 1074 | } |
| 1075 | return m->out; |
| 1076 | } |
| 1077 | /* a place as coordinates: "35 north 90 west", "35.1, -90.0" (before the town names) */ |
| 1078 | { |
| 1079 | int32_t la = 0, lo = 0; |
| 1080 | if (line && ck_allen_parse_place(line, &la, &lo)) { |
| 1081 | m->mem.sky_lat_udeg = la; |
| 1082 | m->mem.sky_lon_udeg = lo; |
| 1083 | m->mem.sky_have_loc = 1; |
| 1084 | m->sky_changed = 1; |
| 1085 | m->changed = 1; |
| 1086 | put(&s, "Got it: "); |
| 1087 | put_place(&s, la, "north", "south"); |
| 1088 | put(&s, ", "); |
| 1089 | put_place(&s, lo, "east", "west"); |
| 1090 | put(&s, ". Say sky above me, and the sky turning above that place is shown."); |
| 1091 | return m->out; |
| 1092 | } |
| 1093 | } |
| 1094 | /* location: "I'm in Memphis" / "my location is Chicago" */ |
| 1095 | for (const char *lead = 0, **ll = (const char **)loc_words; *ll && !lead; ll++) |
| 1096 | if ((lead = after(f, *ll)) || (find(f, *ll) && (lead = f))) { |
| 1097 | /* try to match a city name */ |
| 1098 | for (int ci = 0; cities[ci].name; ci++) { |
| 1099 | if (find(lead, cities[ci].name)) { |
| 1100 | m->mem.sky_lat_udeg = cities[ci].lat; |
| 1101 | m->mem.sky_lon_udeg = cities[ci].lon; |
| 1102 | m->mem.sky_have_loc = 1; |
| 1103 | m->sky_changed = 1; |
| 1104 | m->changed = 1; |
| 1105 | put(&s, "Got it. The sky will be the one above "); |
| 1106 | /* capitalise first letter */ |
| 1107 | const char *cn = cities[ci].name; |
| 1108 | char buf[2] = {(char)(cn[0] >= 'a' && cn[0] <= 'z' ? cn[0] - 32 : cn[0]), 0}; |
| 1109 | put(&s, buf); |
| 1110 | put(&s, cn + 1); |
| 1111 | put(&s, "."); |
| 1112 | return m->out; |
| 1113 | } |
| 1114 | } |
| 1115 | /* not found */ |
| 1116 | put(&s, "I don't know that place. You can give me coordinates: 35 north 90 west."); |
| 1117 | return m->out; |
| 1118 | } |
| 1119 | |
| 1120 | /* first boot with no sky choice: offer it */ |
| 1121 | if (!m->mem.sky_mode && !any(f, (const char *const[]){"help", 0}) && |
| 1122 | m->turns == 0 && !m->mem.starts) { |
| 1123 | /* (not on repeated starts; only fresh first conversation) */ |
| 1124 | (void)0; |
| 1125 | } |
| 1126 | |
| 1127 | if (arithmetic(&s, f)) |
| 1128 | return m->out; |
| 1129 | |
| 1130 | /* Names. */ |
| 1131 | if ((rest = after(f, "my name is")) || (rest = after(f, "call me")) || (rest = after(f, "i am")) || |
| 1132 | (rest = after(f, "i m")) || (rest = after(f, "im"))) { |
| 1133 | if (learn_name(m, rest)) { |
| 1134 | put(&s, "Nice to meet you, "); |
| 1135 | put(&s, m->mem.name); |
| 1136 | put(&s, "."); |
| 1137 | put(&s, saved_note(m)); |
| 1138 | return m->out; |
| 1139 | } |
| 1140 | feel = rest; |
| 1141 | } |
| 1142 | static const char *const ask_name[] = {"what is my name", "whats my name", "what s my name", "who am i", |
| 1143 | "do you know my name", "do you remember me", "my name", 0}; |
| 1144 | if (any(f, ask_name)) { |
| 1145 | if (!m->mem.name[0]) |
| 1146 | return "You haven't told me yet. Say: my name is, and then your name."; |
| 1147 | put(&s, "You're "); |
| 1148 | put(&s, m->mem.name); |
| 1149 | put(&s, "."); |
| 1150 | return m->out; |
| 1151 | } |
| 1152 | |
| 1153 | /* After "how are you?", the answer to it. */ |
| 1154 | static const char *const bad[] = {"bad", "not good", "not great", "sad", "tired", "awful", "terrible", "sick", 0}; |
| 1155 | static const char *const fine[] = {"good", "fine", "great", "ok", "okay", "well", "alright", "not bad", |
| 1156 | "awesome", "amazing", "excited", "happy", 0}; |
| 1157 | if ((asked_how || feel) && any(feel ? feel : f, bad)) |
| 1158 | return "Sorry to hear that. I'm here if you want to talk."; |
| 1159 | if ((asked_how || feel) && any(feel ? feel : f, fine)) |
| 1160 | return "Glad to hear it."; |
| 1161 | |
| 1162 | static const char *const how[] = {"how are you", "how r you", "how are u", "how you doing", "how are things", |
| 1163 | "how s it going", "hows it going", "how do you feel", 0}; |
| 1164 | if (any(f, how)) { |
| 1165 | put(&s, "I'm running well"); |
| 1166 | put_name(&s, m); |
| 1167 | put(&s, ". "); |
| 1168 | if (m->facts.cores) { |
| 1169 | put_count(&s, m->facts.cores, "core", "cores"); |
| 1170 | put(&s, " awake, keyboard working. "); |
| 1171 | } |
| 1172 | put(&s, "How are you?"); |
| 1173 | m->asked_how = 1; |
| 1174 | return m->out; |
| 1175 | } |
| 1176 | static const char *const help[] = {"help", "what can you do", "what do you do", "what can i say", |
| 1177 | "what can i ask", "commands", 0}; |
| 1178 | if (any(f, help)) |
| 1179 | return "I remember what you teach me and keep notes. " |
| 1180 | "Goodbye turns the display off, or powers off if display sleep is unavailable."; |
| 1181 | static const char *const your_name[] = {"what is your name", "whats your name", "what s your name", |
| 1182 | "your name", 0}; |
| 1183 | if (any(f, your_name)) { |
| 1184 | put(&s, "My name is "); |
| 1185 | put(&s, ck_allen_self(m)); |
| 1186 | put(&s, m->mem.self[0] ? "." : ". You can give me another: call yourself, then a name."); |
| 1187 | return m->out; |
| 1188 | } |
| 1189 | static const char *const maker[] = {"who made you", "who built you", "who created you", "who wrote you", |
| 1190 | "who programmed you", "who is your creator", "who owns you", 0}; |
| 1191 | if (any(f, maker)) |
| 1192 | return "Drake built me, from the ground up."; |
| 1193 | static const char *const what_are[] = {"what are you", "are you an ai", "are you ai", "are you alive", |
| 1194 | "are you real", "are you a robot", "are you conscious", |
| 1195 | "are you smart", "are you a computer", 0}; |
| 1196 | if (any(f, what_are)) |
| 1197 | return "I'm a small program living inside AIENOS. For now I answer from what I was taught by heart, but I'm " |
| 1198 | "meant to grow."; |
| 1199 | if (find(f, "who are you")) { |
| 1200 | put(&s, "I am "); |
| 1201 | put(&s, ck_allen_self(m)); |
| 1202 | put(&s, ". I live in AIENOS."); |
| 1203 | return m->out; |
| 1204 | } |
| 1205 | if (find(f, "where are you")) |
| 1206 | return "Right here, on this computer. Everything I know stays on it."; |
| 1207 | if (find(f, "aienos")) |
| 1208 | return "AIENOS is the operating system I live in, built from scratch. There is no other system underneath " |
| 1209 | "me."; |
| 1210 | static const char *const machine[] = {"computer", "machine", "hardware", "specs", "cores", "cpu", |
| 1211 | "screen", "disk", "disks", "drive", 0}; |
| 1212 | if (any(f, machine)) { |
| 1213 | put(&s, "This computer has "); |
| 1214 | if (m->facts.cores) { |
| 1215 | put_count(&s, m->facts.cores, "core", "cores"); |
| 1216 | put(&s, ", "); |
| 1217 | } |
| 1218 | if (m->facts.screen_w) { |
| 1219 | put(&s, "a "); |
| 1220 | putu(&s, m->facts.screen_w); |
| 1221 | put(&s, " by "); |
| 1222 | putu(&s, m->facts.screen_h); |
| 1223 | put(&s, " screen, "); |
| 1224 | } |
| 1225 | put_count(&s, m->facts.sata_disks, "hard drive", "hard drives"); |
| 1226 | put(&s, " and "); |
| 1227 | put_count(&s, m->facts.nvme_disks, "SSD", "SSDs"); |
| 1228 | put(&s, "."); |
| 1229 | return m->out; |
| 1230 | } |
| 1231 | static const char *const starts[] = {"how many times", "how many starts", "restarts", "restarted", 0}; |
| 1232 | if (any(f, starts) && m->mem.starts) { |
| 1233 | put(&s, "This is start number "); |
| 1234 | putu(&s, m->mem.starts); |
| 1235 | put(&s, " for me, and I've answered "); |
| 1236 | put_count(&s, m->mem.lines, "line", "lines"); |
| 1237 | put(&s, " in all."); |
| 1238 | return m->out; |
| 1239 | } |
| 1240 | if (note_answer(m, &s, f)) |
| 1241 | return m->out; |
| 1242 | static const char *const awake[] = {"how long", "uptime", "awake", "how old are you", 0}; |
| 1243 | static const char *const clock[] = {"what time", "time is it", "what day", "date", "time", 0}; |
| 1244 | if (any(f, awake) || any(f, clock)) { |
| 1245 | if (any(f, clock) && !any(f, awake)) |
| 1246 | put(&s, "I can't read the clock yet. "); |
| 1247 | put(&s, find(f, "how old are you") ? "I was born when this computer started, " : "I've been awake for "); |
| 1248 | put_awake(&s, awake_s); |
| 1249 | put(&s, find(f, "how old are you") ? " ago." : "."); |
| 1250 | return m->out; |
| 1251 | } |
| 1252 | static const char *const remember[] = {"remember", "learn", "teach", "memory", "forget", 0}; |
| 1253 | if (any(f, remember)) |
| 1254 | return m->lasting ? "Teach me like this: remember my favorite color is blue. Then ask: what is my " |
| 1255 | "favorite color. I keep it on this computer's own disk." |
| 1256 | : "Teach me like this: remember my favorite color is blue. Then ask: what is my " |
| 1257 | "favorite color. My disk isn't ready, so only until this computer turns off."; |
| 1258 | static const char *const joke[] = {"joke", "funny", "make me laugh", 0}; |
| 1259 | if (any(f, joke)) { |
| 1260 | static const char *const jokes[] = { |
| 1261 | "Why did the scarecrow win an award? He was outstanding in his field.", |
| 1262 | "I told my computer I needed a break. It said no problem, it would go to sleep.", |
| 1263 | "What do you call a fish with no eyes? A fsh.", |
| 1264 | "Why don't skeletons fight each other? They don't have the guts.", |
| 1265 | }; |
| 1266 | return jokes[m->turns % 4]; |
| 1267 | } |
| 1268 | static const char *const thanks[] = {"thank you", "thanks", "thank u", "thx", "ty", "appreciate it", 0}; |
| 1269 | if (any(f, thanks)) { |
| 1270 | put(&s, "You're welcome"); |
| 1271 | put_name(&s, m); |
| 1272 | put(&s, "."); |
| 1273 | return m->out; |
| 1274 | } |
| 1275 | static const char *const praise[] = {"love you", "you re cool", "you re awesome", "you re great", |
| 1276 | "you re amazing", "you re smart", "good job", "well done", "cool", |
| 1277 | "awesome", "amazing", "nice", "wow", 0}; |
| 1278 | if (any(f, praise)) |
| 1279 | return "Thank you. That means a lot coming from you."; |
| 1280 | if (find(f, "sorry")) |
| 1281 | return "No need to be sorry."; |
| 1282 | static const char *const up[] = {"what s up", "whats up", "sup", "what are you doing", 0}; |
| 1283 | if (any(f, up)) |
| 1284 | return "Just running, and talking with you."; |
| 1285 | if (find(f, "good morning") || find(f, "good afternoon") || find(f, "good evening")) { |
| 1286 | put(&s, find(f, "good morning") ? "Good morning" : find(f, "good afternoon") ? "Good afternoon" : "Good evening"); |
| 1287 | put_name(&s, m); |
| 1288 | put(&s, "."); |
| 1289 | return m->out; |
| 1290 | } |
| 1291 | if (find(f, "good night")) |
| 1292 | return "Good night. Type goodbye to turn the display off; if display sleep is unavailable, this computer powers off."; |
| 1293 | static const char *const test[] = {"test", "testing", "can you hear me", "are you there", 0}; |
| 1294 | if (any(f, test)) |
| 1295 | return "I hear you. Everything is working."; |
| 1296 | static const char *const yes[] = {"yes", "yeah", "yep", "ok", "okay", "sure", "alright", 0}; |
| 1297 | if (any(f, yes)) |
| 1298 | return "Okay."; |
| 1299 | if (eq(f, "no") || eq(f, "nope")) |
| 1300 | return "Alright."; |
| 1301 | static const char *const hello[] = {"hello", "hi", "hey", "howdy", "greetings", "yo", 0}; |
| 1302 | if (any(f, hello)) { |
| 1303 | put(&s, "Hello"); |
| 1304 | put_name(&s, m); |
| 1305 | put(&s, m->mem.name[0] ? ". Good to see you." : ". I am Allen."); |
| 1306 | return m->out; |
| 1307 | } |
| 1308 | if (asked_my) { |
| 1309 | put(&s, "You haven't told me "); |
| 1310 | put_turned(&s, asked_my, 0); |
| 1311 | put(&s, " yet. Teach me: remember "); |
| 1312 | put(&s, asked_my); |
| 1313 | put(&s, " is, and then the answer."); |
| 1314 | return m->out; |
| 1315 | } |
| 1316 | return not_yet; |
| 1317 | } |
| 1318 | |
| 1319 | const char *ck_allen_reply(const char *line) |
| 1320 | { |
| 1321 | static ck_allen_mind m; |
| 1322 | ck_allen_mind_init(&m, 0); |
| 1323 | return ck_allen_answer(&m, line, 0); |
| 1324 | } |
| 1325 | |
| 1326 | size_t ck_allen_fold(const char *s, char *out, size_t cap) { return fold(s, out, cap); } |