protoverse

A metaverse protocol
git clone git://jb55.com/protoverse
Log | Files | Refs | README | LICENSE

commit 4141930b82a49864b402eb19371b206eb77f0236
parent 96978f737a38b5587e04360ebd78684ec26346f5
Author: William Casarin <jb55@jb55.com>
Date:   Fri,  3 Jul 2020 09:46:26 -0700

describe group

Diffstat:
Mdescribe.c | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Mdescribe.h | 2+-
Mprotoverse.c | 2+-
3 files changed, 115 insertions(+), 4 deletions(-)

diff --git a/describe.c b/describe.c @@ -212,10 +212,98 @@ static int describe_room(struct describe *desc) return 1; } +static int describe_amount(struct describe *desc, int nobjs) +{ + int ok; + + if (nobjs == 1) { + ok = push_word(desc->strs, "a single"); + if (!ok) return 0; + } else if (nobjs == 2) { + ok = push_word(desc->strs, "a couple"); + if (!ok) return 0; + } else if (nobjs == 3) { + ok = push_word(desc->strs, "three"); + if (!ok) return 0; + } else if (nobjs == 4) { + ok = push_word(desc->strs, "four"); + if (!ok) return 0; + } else if (nobjs == 5) { + ok = push_word(desc->strs, "five"); + if (!ok) return 0; + } else { + ok = push_word(desc->strs, "many"); + if (!ok) return 0; + } + + return 1; +} + +static int describe_object_name(struct cursor *strs, struct cell *cell) +{ + return push_word(strs, cell->type == C_OBJECT + ? object_type_str(cell->obj_type) + : cell_type_str(cell->type)); +} + static int describe_group(struct describe *desc) { - (void)desc; - return 0; + int i, ok, nobjs; + struct cell *cell; + + nobjs = desc->cell->n_children; + + ok = push_word(desc->strs, "There"); + if (!ok) return 0; + + if (nobjs == 1) { + ok = push_word(desc->strs, "is"); + if (!ok) return 0; + } + else { + ok = push_word(desc->strs, "are"); + if (!ok) return 0; + } + + ok = describe_amount(desc, nobjs); + + ok = push_word(desc->strs, "object"); + if (!ok) return 0; + + if (nobjs > 1) { + ok = push_str(desc->strs, "s:"); + if (!ok) return 0; + } + else { + push_str(desc->strs, ":"); + if (!ok) return 0; + } + + ok = push_word(desc->strs, "a"); + if (!ok) return 0; + + for (i = 0; i < nobjs; i++) { + cell = get_cell(desc->parsed->cells, + desc->cell->children[i]); + assert(cell); + + if (i > 0) { + if (i == nobjs-1) { + ok = push_word(desc->strs, "and"); + if (!ok) return 0; + } + else if (i != nobjs-1) { + ok = push_str(desc->strs, ","); + if (!ok) return 0; + } + + } + + ok = describe_object_name(desc->strs, cell); + if (!ok) return 0; + } + + return 1; } static int describe_object(struct describe *desc) @@ -251,3 +339,26 @@ int describe_cell(struct cell *cell, struct parser *parsed, struct cursor *strbu return 1; } + + +int describe_cells(struct cell *cell, struct parser *parsed, struct cursor *strs, int max_depth, int depth) +{ + int ok; + + if (depth > max_depth) + return 1; + + ok = describe_cell(cell, parsed, strs); + if (!ok) return 0; + + push_str(strs, ".\n"); + + if (cell->n_children == 0) + return 1; + + /* TODO: for each cell ? for now we just care about the group */ + cell = get_cell(parsed->cells, cell->children[0]); + assert(cell); + + return describe_cells(cell, parsed, strs, max_depth, depth+1); +} diff --git a/describe.h b/describe.h @@ -5,6 +5,6 @@ #include "parse.h" int describe_cell(struct cell *cell, struct parser *parsed, struct cursor *strbuf); - +int describe_cells(struct cell *cell, struct parser *parsed, struct cursor *strs, int max_depth, int depth); #endif diff --git a/protoverse.c b/protoverse.c @@ -108,7 +108,7 @@ static int describe(struct parser *parser, u16 root_cell) make_cursor((u8*)strbuf, (u8*)strbuf + sizeof(strbuf), &strs); - describe_cell(cell, parser, &strs); + describe_cells(cell, parser, &strs, 2, 0); printf("\n\ndescription\n-----------\n\n%s\n", strbuf);