Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prog/ - RPG

Name: Anonymous 2010-10-29 15:44

Lets program a roleplaying game together. Each post must contain either a new function or an edited version of an old function in psudocode. I will start:

int dealDamage(int hp, int damage){
    return hp - damage;
}

Name: Anonymous 2010-10-29 16:36

>>10
define tell(char*)
define main_menu(void)

Name: Anonymous 2010-10-29 16:44


//    Command format:
//    Commands:
//        SAY    {argument}
//        MOVE {argument} -- Direction north, northeast, ettc
//        WAIT {time_in_hours}
//        ATTACK    {enemy_id},{weapon},{target_location_on_enemy}
//Said command functions will return an int 0 = succeed; 1 = error
//////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int parse(char *x){
    int size = strcspn(x," ")-1;
    char *command;
    char *arg;
    char **argv;
    int i,j;
    if(size<=0)
        return 1;
   
    command = malloc(sizeof(char) *(size));
    for(i=0;i<size;i++)
        command[i]=x[i];
    arg = malloc(sizeof(char) *(sizeof(x)-size));
    for(i=size;i<sizeof(x);i++){
        arg[i-size]=x[i];
    }
   
    if(strncmp(command,"SAY",sizeof(command))){
        free(command);
        return say_function(arg);
    }
    else if(strncmp(command,"MOVE",sizeof(command))){
        free(command);
        return move_function(arg);
    }
    else if(strncmp(command,"WAIT",sizeof(command))){
        free(command);
        return wait_function(arg);
    }
    else if(strncmp(command,"ATTACK",sizeof(command))){
        argv = malloc(sizeof(char *) *3);
        for(i=0;i<3;i++){
            size = strcspn(arg,",")-1;
            argv[i] = malloc(sizeof(char) *size);
            for(j=0;i<size;i++)
                argv[i][j] = arg[j];
            //
            free(command);
            command = malloc(sizeof(char)*(sizeof(arg)-size));
            for(i=size;i<sizeof(arg);i++)
                command[i-size]=arg[i];
            free(arg);
            arg = malloc(sizeof(char)*sizeof(command));
            for(i=0;i<sizeof(command);i++)
                arg[i]=command[i];
        }
        free(command);
        free(arg);
        return attack_function(argv);
    }
       
       
    free(command);
    free(arg);
    return -1;
}


you guys can optimize it,add to it and define the other functions in it

Name: Anonymous 2010-10-29 17:32

Here's some ENTERPRISE Common Lisp RPG skeleton I wrote in the last 15 minutes:

(defclass damageable-mixin ()
  ((damage-accumulated :accessor total-damage :initform 0)))

(defgeneric damage (object &key)
  (:documentation "Inflict damage upon an object in some way"))

(defmethod damage ((object damageable-mixin) &key amount)
  (with-accessors ((total total-damage)) object
    (incf total amount)))

(defgeneric consume-damage (object &key)
  (:documentation "Remove damage from an object"))

(defmethod consume-damage ((object damageable-mixin) &key amount)
  (with-accessors ((total total-damage)) object
    (let ((consumed-amount (if amount total 0)))
      (decf total consumed-amount))))

(defclass game-character (damageable-mixin)
  ((hitpoints :accessor hitpoints :initarg :hitpoints)))

(defmethod deal-accumulated-damage ((char game-character))
  (with-accessors ((hp hitpoints) (dmg total-damage)) char   
    (decf hp dmg)
    (consume-damage char :amount dmg)))

(defmethod damage ((char game-character) &key)
  (call-next-method)
  (deal-accumulated-damage char))

(defgeneric heal (character amount)
  (:documentation "Heal a character by some amount"))

(defmethod heal ((char game-character) amount)
  (deal-accumulated-damage char)
  (with-accessors ((hp hitpoints)) char
    (incf hp amount)))


(defclass player-character (game-character) ())
(defclass non-playable-character (game-character) ())
(defclass monster (non-playable-character) ())

;; item may not have hp, but it can still accumulate damage,
;; it may break eventually depending on item's implementation
(defclass item (damageable-mixin) ())

(defmethod damage :before ((player player-character) &key amount)
  (format t "You received ~A damage.~&" amount))

(defmethod heal :before ((player player-character) amount)
  (format t "You were healed by ~A hitpoints.~&" amount))

(defmethod show-hitpoints ((player player-character))
  (format t "You now have ~A hitpoints.~&" (hitpoints player)))

(defmethod damage :after ((player player-character) &key)
  (show-hitpoints player))
(defmethod heal :after ((player player-character) amount)
  (show-hitpoints player))

(let ((player (make-instance 'player-character :hitpoints 100)))
  (damage player :amount 10)
  (heal player 9.99))

;; Outputs:
;You received 10 damage.
;You now have 90 hitpoints.
;You were healed by 9.99 hitpoints.
;You now have 99.99 hitpoints.


It's overengineered, however it is imagined to allow one to define all kinds of things that can be damaged, and thigns that have HPs, each with more general and more specific behaviours as the game designer wishes. Items are meant to be able to receive damage, but they don't have to have a set HP, instead how damage is handled is left to the developer. Playable and non-playable characters do have HP and the damage is reflected by changing the HP, however such behaviour can be easily overridden by defining more specific methods on the characters.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List