-
Notifications
You must be signed in to change notification settings - Fork 0
/
talk.c
113 lines (96 loc) · 2.29 KB
/
talk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include "types.h"
#include "defs.h"
static void use(Player *p, char *cmd)
{
Item *t;
if (t = (Item*) find(&p->items, cmd)) {
if (!prop[t->type].active)
prop[t->type].use(p, t);
} else {
if (t = (Item*) find(&p->room->items, cmd))
if (!prop[t->type].take && !prop[t->type].active)
prop[t->type].use(p, t);
}
}
static void ptake(Player *p, char *cmd)
{
Item *t;
if (t = (Item*) find(&p->room->items, cmd))
if (prop[t->type].take)
push(&p->items, take(&p->room->items, t));
}
static void drop(Player *p, char *cmd)
{
Item *t;
if (t = (Item*) find(&p->items, cmd)) {
if (t == p->hand)
p->hand = NULL;
push(&p->room->items, take(&p->items, t));
}
}
static void attack(Player *p, char *cmd)
{
Player *t;
if (t = (Player*) find(&p->room->plrs, cmd))
if (p->hand && prop[p->hand->type].attack)
prop[p->hand->type].use(t, p->hand);
}
static void hold(Player *p, char *cmd)
{
p->hand = (Item*) find(&p->items, cmd);
}
int request(Player *p)
{
char cmd[BUF], *arg;
fgets(cmd, BUF, p->in);
if (arg = strchr(cmd, '\r'))
*arg = '\0';
if (arg = strchr(cmd, '\n'))
*arg = '\0';
if (arg = strchr(cmd, ' '))
*arg++ = '\0';
pthread_mutex_lock(&p->room->lock);
if (strcmp(cmd, "exit") == 0)
p->health = 0;
if (arg) {
if (strcmp(cmd, "use") == 0)
use(p, arg);
else if (strcmp(cmd, "attack") == 0)
attack(p, arg);
else if (strcmp(cmd, "take") == 0)
ptake(p, arg);
else if (strcmp(cmd, "drop") == 0)
drop(p, arg);
else if (strcmp(cmd, "hold") == 0)
hold(p, arg);
}
pthread_mutex_unlock(&p->room->lock);
return 0;
}
void answer(Player *p)
{
FILE *f = p->out;
Room *room = p->room;
List *t;
fprintf(f, "\n\rHealth: %d\tHunger: %d\tThirst: %d\n\r\n\r",
p->health, p->hunger, p->thirst);
fprintf(f, "Equipment:\n\r");
for (t = p->items; t; t = t->next)
fprintf(f, p->hand == (Item*) t?"* %s\n\r":" %s\n\r", t->name);
pthread_mutex_lock(&room->lock);
fprintf(f, "\n%s:\n\r", room->hdr.name);
for (t = room->items; t; t = t->next)
fprintf(f, " %s\n\r", t->name);
fprintf(f, "\n\r");
for (t = room->plrs; t; t = t->next)
if ((Player*) t != p)
fprintf(f, " %s\n\r", t->name);
pthread_mutex_unlock(&room->lock);
fprintf(f, "> ");
fflush(f);
}